473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Battleships game

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
2 1736
r035198x
13,262 8TB
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
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

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

Similar topics

21
by: BlackHawke | last post by:
My name is Nick Soutter, I own a small game development company (www.aepoxgames.net) making our first game (www.andromedaonline.net) in java. I am writing because we are having a very...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
7
by: Brandon J. Van Every | last post by:
Anyone know of any "good" open source C# game projects out there? Something that actually has a game engine and some content done, so I can just fiddle with it and do interesting / goofy things. ...
1
by: fowle040 | last post by:
I underlined and bold print my files. I need to know how to make this code into a working game. The object of the game is to have two players 1- belle and 2-beast. I want them to lose and gain...
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
4
by: ragaman123 | last post by:
Hello there, I have currently started a project in java, a battleships game. I am having trouble deciding how to deploy the grid and have the pictures of ships hidden under each picture of blue...
5
by: Kraken | last post by:
Hi, i have a bit of a problem here. I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file...
2
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.