By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Battleships game

P: 13
Hello everyone,

I am currently working on a battleships game and at the moment i am playing around with a interface which has various different buttons. The interface has a 2d grid of buttons and other buttons on it which make various noises when pressed.I am however having problems changeing the icon of any of the buttons. Iv put up all the code of it, i am sorry for putting the whole thing up but thought maybe if i left some out someone mightn be abe to figure it out. There is aslo a simpleSoundPlayer class for playing the sounds, but im not putting this up. You can see in the final if statement in the actionPerformed method at the end i am using the setIcon method to try and change the icon of the JButton but it is not working! Any ideas as to why would be greatly appreciated!Once again, im sorry for putting up piles and piles of code hope im not annoying anyone!
Thanking you,
Ragaman.
Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import java.awt.event.*;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.InputStream;
  5.  
  6. import javax.swing.*;
  7. import javax.swing.event.*;
  8. import javax.swing.border.*;
  9. public class FullScreenGame extends JFrame 
  10. implements ActionListener{
  11.  
  12. private GraphicsDevice graphicsDevice;
  13. private DisplayMode origDisplayMode;
  14. private JButton QuitButton;
  15. private JButton playButton;
  16. private JButton FlySoundbutton;
  17. private JButton GridButton;
  18. private JButton SoundButton;
  19. private JButton PauseButton;
  20. private JPanel buttonSpace;
  21. private JPanel secbuttonSpace;
  22. private SimpleSoundPlayer sound;
  23. private LineBorder scope_border;
  24. private SimpleSoundPlayer soundx;
  25. private SimpleSoundPlayer soundy;
  26. private SimpleSoundPlayer soundz;
  27. JPanel board = new JPanel(new GridLayout(5, 5));
  28.  
  29. public static void main(String[] args){
  30.  
  31. GraphicsEnvironment graphicsEnvironment = 
  32. GraphicsEnvironment.getLocalGraphicsEnvironment();
  33. GraphicsDevice[] devices = 
  34. graphicsEnvironment.getScreenDevices();
  35. for(int i = 0;i < 1;i++){
  36. System.out.println(devices[i]);
  37. }
  38.  
  39.  
  40. new FullScreenGame(devices[0]);
  41. }
  42.  
  43. public FullScreenGame(GraphicsDevice graphicsDevice){
  44.  
  45. this.graphicsDevice = graphicsDevice;
  46.  
  47. origDisplayMode = graphicsDevice.getDisplayMode();
  48.  
  49. ImageIcon Config = new ImageIcon("C:/unzipped/images/menu/config.png");
  50. JButton[][] battleshipgrid = new JButton[5][5];
  51. for(int i=0; i < battleshipgrid.length; i++) {
  52. for(int j=0; j < battleshipgrid.length; j++) {
  53.  
  54.      JButton GridButton = new JButton ("", Config);
  55.      battleshipgrid[i][j] = GridButton;
  56.      GridButton.addActionListener(this);
  57.      GridButton.setActionCommand("Bomb");
  58.     board.add(battleshipgrid[i][j]);
  59.  
  60. }
  61. }
  62.  
  63. ImageIcon Play = new ImageIcon("C:/unzipped/images/menu/play.png");
  64.     JButton playButton = new JButton ("Play");
  65.     playButton.setFocusable(false);
  66. playButton.addActionListener(this);
  67. playButton.setIcon(Play);
  68. JButton QuitButton = new JButton ("Quit");
  69. QuitButton.setFocusable(false);
  70. QuitButton.addActionListener(this);
  71. QuitButton.setFocusable(false);
  72. JButton SoundButton = new JButton ("Voice");
  73. SoundButton.setFocusable(false);
  74. SoundButton.addActionListener(this);
  75.  
  76. JButton FlySoundbutton = new JButton ("Fly");
  77. FlySoundbutton.setFocusable(false);
  78. FlySoundbutton.addActionListener(this);
  79. JButton myButton = new JButton("Change Man!");
  80. myButton.addActionListener(this);
  81. buttonSpace = new JPanel();
  82. secbuttonSpace = new JPanel();
  83. buttonSpace.add(QuitButton);
  84. secbuttonSpace.add(playButton);
  85. buttonSpace.add(FlySoundbutton);
  86. buttonSpace.add(SoundButton);
  87. buttonSpace.add(myButton);
  88.  
  89. FlySoundbutton.setActionCommand("Fly");
  90. SoundButton.setActionCommand("Voice");
  91.  
  92. playButton.setActionCommand("Play");
  93.  
  94.  
  95.  
  96. /*
  97. myButton.addMouseListener(new MouseAdapter(){
  98. public void mouseClicked(MouseEvent e){
  99.     ImageIcon Ear = new ImageIcon("C:/unzipped/images/ear.png");
  100. myButton.setIcon(Ear);
  101. getContentPane().add(myButton);
  102. }
  103. });
  104. */
  105.  
  106.  
  107. JLabel eastLabel = new JLabel(" Score ");
  108. eastLabel.setOpaque(true);
  109. buttonSpace.setBackground(Color.BLUE);
  110. secbuttonSpace.setBackground(Color.RED);
  111. getContentPane().add(buttonSpace,BorderLayout.WEST);
  112. getContentPane().add(secbuttonSpace,BorderLayout.NORTH);
  113.  
  114. getContentPane().add(board,BorderLayout.CENTER);
  115.  
  116. getContentPane().add(eastLabel,BorderLayout.EAST);
  117.  
  118.  
  119.  
  120. if (graphicsDevice.isFullScreenSupported()){
  121.  
  122. setUndecorated(true);
  123. setResizable(false);
  124.  
  125. graphicsDevice.setFullScreenWindow(this);
  126. validate();
  127. }else{
  128. System.out.println("Full-screen mode not supported");
  129.  
  130. }
  131.  
  132.  
  133. public void actionPerformed(ActionEvent e){
  134.  
  135.      String cmd = e.getActionCommand();
  136.     if (cmd.equals("Quit")){ 
  137.         System.exit(0);
  138.     }
  139.     else if (cmd.equals("Voice")){ 
  140.         sound = new SimpleSoundPlayer("C:/unzipped/sounds/robocop.wav");
  141. sound.getSamples();
  142. new Thread() {
  143.     public void run() {
  144.         InputStream stream =
  145. new ByteArrayInputStream(sound.getSamples());
  146.         sound.play(stream);
  147.     }
  148. }.start();
  149.     }
  150. else if (cmd.equals("Fly")){ 
  151.         soundy = new SimpleSoundPlayer("C:/unzipped/sounds/fly-bzz.wav");
  152. soundy.getSamples();
  153. new Thread() {
  154.     public void run() {
  155.         InputStream stream =
  156. new ByteArrayInputStream(soundy.getSamples());
  157.         soundy.play(stream);
  158.     }
  159. }.start();
  160.  
  161.  
  162. }
  163. else if (cmd.equals("Bomb")){ 
  164.         soundx = new SimpleSoundPlayer("C:/unzipped/sounds/bomb.wav");
  165. soundx.getSamples();
  166. new Thread() {
  167.     public void run() {
  168.         InputStream stream =
  169. new ByteArrayInputStream(soundx.getSamples());
  170.         soundx.play(stream);
  171.     }
  172. }.start();
  173.  
  174. }
  175. else if (cmd.equals("Play")){ 
  176.         soundz = new SimpleSoundPlayer("C:/unzipped/sounds/EvilLaugh.wav");
  177. soundz.getSamples();
  178. new Thread() {
  179.     public void run() {
  180.         InputStream stream =
  181. new ByteArrayInputStream(soundz.getSamples());
  182.         soundz.play(stream);
  183.     }
  184. }.start();
  185. ImageIcon Pause = new ImageIcon("C:/unzipped/images/menu/pause.png");
  186. JButton PauseButton = new JButton(Pause);
  187. playButton = PauseButton;
  188. PauseButton.setIcon(Pause);
  189.  
  190. }
  191. }
  192. }
  193.  
Dec 1 '06 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
Hello everyone,

I am currently working on a battleships game and at the moment i am playing around with a interface which has various different buttons. The interface has a 2d grid of buttons and other buttons on it which make various noises when pressed.I am however having problems changeing the icon of any of the buttons. Iv put up all the code of it, i am sorry for putting the whole thing up but thought maybe if i left some out someone mightn be abe to figure it out. There is aslo a simpleSoundPlayer class for playing the sounds, but im not putting this up. You can see in the final if statement in the actionPerformed method at the end i am using the setIcon method to try and change the icon of the JButton but it is not working! Any ideas as to why would be greatly appreciated!Once again, im sorry for putting up piles and piles of code hope im not annoying anyone!
Thanking you,
Ragaman.
Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import java.awt.event.*;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.InputStream;
  5.  
  6. import javax.swing.*;
  7. import javax.swing.event.*;
  8. import javax.swing.border.*;
  9. public class FullScreenGame extends JFrame 
  10. implements ActionListener{
  11.  
  12. private GraphicsDevice graphicsDevice;
  13. private DisplayMode origDisplayMode;
  14. private JButton QuitButton;
  15. private JButton playButton;
  16. private JButton FlySoundbutton;
  17. private JButton GridButton;
  18. private JButton SoundButton;
  19. private JButton PauseButton;
  20. private JPanel buttonSpace;
  21. private JPanel secbuttonSpace;
  22. private SimpleSoundPlayer sound;
  23. private LineBorder scope_border;
  24. private SimpleSoundPlayer soundx;
  25. private SimpleSoundPlayer soundy;
  26. private SimpleSoundPlayer soundz;
  27. JPanel board = new JPanel(new GridLayout(5, 5));
  28.  
  29. public static void main(String[] args){
  30.  
  31. GraphicsEnvironment graphicsEnvironment = 
  32. GraphicsEnvironment.getLocalGraphicsEnvironment();
  33. GraphicsDevice[] devices = 
  34. graphicsEnvironment.getScreenDevices();
  35. for(int i = 0;i < 1;i++){
  36. System.out.println(devices[i]);
  37. }
  38.  
  39.  
  40. new FullScreenGame(devices[0]);
  41. }
  42.  
  43. public FullScreenGame(GraphicsDevice graphicsDevice){
  44.  
  45. this.graphicsDevice = graphicsDevice;
  46.  
  47. origDisplayMode = graphicsDevice.getDisplayMode();
  48.  
  49. ImageIcon Config = new ImageIcon("C:/unzipped/images/menu/config.png");
  50. JButton[][] battleshipgrid = new JButton[5][5];
  51. for(int i=0; i < battleshipgrid.length; i++) {
  52. for(int j=0; j < battleshipgrid.length; j++) {
  53.  
  54.      JButton GridButton = new JButton ("", Config);
  55.      battleshipgrid[i][j] = GridButton;
  56.      GridButton.addActionListener(this);
  57.      GridButton.setActionCommand("Bomb");
  58.     board.add(battleshipgrid[i][j]);
  59.  
  60. }
  61. }
  62.  
  63. ImageIcon Play = new ImageIcon("C:/unzipped/images/menu/play.png");
  64.     JButton playButton = new JButton ("Play");
  65.     playButton.setFocusable(false);
  66. playButton.addActionListener(this);
  67. playButton.setIcon(Play);
  68. JButton QuitButton = new JButton ("Quit");
  69. QuitButton.setFocusable(false);
  70. QuitButton.addActionListener(this);
  71. QuitButton.setFocusable(false);
  72. JButton SoundButton = new JButton ("Voice");
  73. SoundButton.setFocusable(false);
  74. SoundButton.addActionListener(this);
  75.  
  76. JButton FlySoundbutton = new JButton ("Fly");
  77. FlySoundbutton.setFocusable(false);
  78. FlySoundbutton.addActionListener(this);
  79. JButton myButton = new JButton("Change Man!");
  80. myButton.addActionListener(this);
  81. buttonSpace = new JPanel();
  82. secbuttonSpace = new JPanel();
  83. buttonSpace.add(QuitButton);
  84. secbuttonSpace.add(playButton);
  85. buttonSpace.add(FlySoundbutton);
  86. buttonSpace.add(SoundButton);
  87. buttonSpace.add(myButton);
  88.  
  89. FlySoundbutton.setActionCommand("Fly");
  90. SoundButton.setActionCommand("Voice");
  91.  
  92. playButton.setActionCommand("Play");
  93.  
  94.  
  95.  
  96. /*
  97. myButton.addMouseListener(new MouseAdapter(){
  98. public void mouseClicked(MouseEvent e){
  99.     ImageIcon Ear = new ImageIcon("C:/unzipped/images/ear.png");
  100. myButton.setIcon(Ear);
  101. getContentPane().add(myButton);
  102. }
  103. });
  104. */
  105.  
  106.  
  107. JLabel eastLabel = new JLabel(" Score ");
  108. eastLabel.setOpaque(true);
  109. buttonSpace.setBackground(Color.BLUE);
  110. secbuttonSpace.setBackground(Color.RED);
  111. getContentPane().add(buttonSpace,BorderLayout.WEST);
  112. getContentPane().add(secbuttonSpace,BorderLayout.NORTH);
  113.  
  114. getContentPane().add(board,BorderLayout.CENTER);
  115.  
  116. getContentPane().add(eastLabel,BorderLayout.EAST);
  117.  
  118.  
  119.  
  120. if (graphicsDevice.isFullScreenSupported()){
  121.  
  122. setUndecorated(true);
  123. setResizable(false);
  124.  
  125. graphicsDevice.setFullScreenWindow(this);
  126. validate();
  127. }else{
  128. System.out.println("Full-screen mode not supported");
  129.  
  130. }
  131.  
  132.  
  133. public void actionPerformed(ActionEvent e){
  134.  
  135.      String cmd = e.getActionCommand();
  136.     if (cmd.equals("Quit")){ 
  137.         System.exit(0);
  138.     }
  139.     else if (cmd.equals("Voice")){ 
  140.         sound = new SimpleSoundPlayer("C:/unzipped/sounds/robocop.wav");
  141. sound.getSamples();
  142. new Thread() {
  143.     public void run() {
  144.         InputStream stream =
  145. new ByteArrayInputStream(sound.getSamples());
  146.         sound.play(stream);
  147.     }
  148. }.start();
  149.     }
  150. else if (cmd.equals("Fly")){ 
  151.         soundy = new SimpleSoundPlayer("C:/unzipped/sounds/fly-bzz.wav");
  152. soundy.getSamples();
  153. new Thread() {
  154.     public void run() {
  155.         InputStream stream =
  156. new ByteArrayInputStream(soundy.getSamples());
  157.         soundy.play(stream);
  158.     }
  159. }.start();
  160.  
  161.  
  162. }
  163. else if (cmd.equals("Bomb")){ 
  164.         soundx = new SimpleSoundPlayer("C:/unzipped/sounds/bomb.wav");
  165. soundx.getSamples();
  166. new Thread() {
  167.     public void run() {
  168.         InputStream stream =
  169. new ByteArrayInputStream(soundx.getSamples());
  170.         soundx.play(stream);
  171.     }
  172. }.start();
  173.  
  174. }
  175. else if (cmd.equals("Play")){ 
  176.         soundz = new SimpleSoundPlayer("C:/unzipped/sounds/EvilLaugh.wav");
  177. soundz.getSamples();
  178. new Thread() {
  179.     public void run() {
  180.         InputStream stream =
  181. new ByteArrayInputStream(soundz.getSamples());
  182.         soundz.play(stream);
  183.     }
  184. }.start();
  185. ImageIcon Pause = new ImageIcon("C:/unzipped/images/menu/pause.png");
  186. JButton PauseButton = new JButton(Pause);
  187. playButton = PauseButton;
  188. PauseButton.setIcon(Pause);
  189.  
  190. }
  191. }
  192. }
  193.  
Since you could not post code for the other class I won't be able to fully test your program but here is what I think:

After changing the Icon of the button, you need to reshow the frame again for the updated interface to reflect the icon change. Try to call setVisible(true); after changing the icon.
Dec 1 '06 #2

P: 13
Since you could not post code for the other class I won't be able to fully test your program but here is what I think:

After changing the Icon of the button, you need to reshow the frame again for the updated interface to reflect the icon change. Try to call setVisible(true); after changing the icon.

Thank you for your advise, i have tried using this however but still to no success.I think the problem is i have to get a reference to the button pressed in the actionPerformed method, and then use the setIcon and setVisible methods.However when i refer to a button in the actionPerformed method it is not the same button as in the main method.I will post the code for the sound player class, you also need the pictures and sounds, i will try put these up aswel.Any advice or helpful hints would be greatly appreciated,
Thanks a million,

Ragaman

Sound Player class:
import java.io.*;
import javax.sound.sampled.*;


public class SimpleSoundPlayer {

public static void main(String[] args) {
// load a sound
SimpleSoundPlayer sound =
new SimpleSoundPlayer("C:/unzipped/ch04src[1]/ch04src/sounds/fly-bzz.wav");

// create the stream to play
InputStream stream =
new ByteArrayInputStream(sound.getSamples());

// play the sound
sound.play(stream);

// exit
System.exit(0);
}

private AudioFormat format;
private byte[] samples;

/**
Opens a sound from a file.
*/
public SimpleSoundPlayer(String filename) {
try {
// open the audio input stream
AudioInputStream stream =
AudioSystem.getAudioInputStream(
new File(filename));

format = stream.getFormat();

// get the audio samples
samples = getSamples(stream);
}
catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}


/**
Gets the samples of this sound as a byte array.
*/
public byte[] getSamples() {
return samples;
}


/**
Gets the samples from an AudioInputStream as an array
of bytes.
*/
private byte[] getSamples(AudioInputStream audioStream) {
// get the number of bytes to read
int length = (int)(audioStream.getFrameLength() *
format.getFrameSize());

// read the entire stream
byte[] samples = new byte[length];
DataInputStream is = new DataInputStream(audioStream);
try {
is.readFully(samples);
}
catch (IOException ex) {
ex.printStackTrace();
}

// return the samples
return samples;
}


/**
Plays a stream. This method blocks (doesn't return) until
the sound is finished playing.
*/
public void play(InputStream source) {

// use a short, 100ms (1/10th sec) buffer for real-time
// change to the sound stream
int bufferSize = format.getFrameSize() *
Math.round(format.getSampleRate() / 10);
byte[] buffer = new byte[bufferSize];

// create a line to play to
SourceDataLine line;
try {
DataLine.Info info =
new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine)AudioSystem.getLine(info);
line.open(format, bufferSize);
}
catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}

// start the line
line.start();

// copy data to the line
try {
int numBytesRead = 0;
while (numBytesRead != -1) {
numBytesRead =
source.read(buffer, 0, buffer.length);
if (numBytesRead != -1) {
line.write(buffer, 0, numBytesRead);
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}

// wait until all data is played, then close the line
line.drain();
line.close();

}

}
Dec 8 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.