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
- import java.awt.*;
- import java.awt.event.*;
- import java.io.ByteArrayInputStream;
- import java.io.InputStream;
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.border.*;
- public class FullScreenGame extends JFrame
- implements ActionListener{
- private GraphicsDevice graphicsDevice;
- private DisplayMode origDisplayMode;
- private JButton QuitButton;
- private JButton playButton;
- private JButton FlySoundbutton;
- private JButton GridButton;
- private JButton SoundButton;
- private JButton PauseButton;
- private JPanel buttonSpace;
- private JPanel secbuttonSpace;
- private SimpleSoundPlayer sound;
- private LineBorder scope_border;
- private SimpleSoundPlayer soundx;
- private SimpleSoundPlayer soundy;
- private SimpleSoundPlayer soundz;
- JPanel board = new JPanel(new GridLayout(5, 5));
- public static void main(String[] args){
- GraphicsEnvironment graphicsEnvironment =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- GraphicsDevice[] devices =
- graphicsEnvironment.getScreenDevices();
- for(int i = 0;i < 1;i++){
- System.out.println(devices[i]);
- }
- new FullScreenGame(devices[0]);
- }
- public FullScreenGame(GraphicsDevice graphicsDevice){
- this.graphicsDevice = graphicsDevice;
- origDisplayMode = graphicsDevice.getDisplayMode();
- ImageIcon Config = new ImageIcon("C:/unzipped/images/menu/config.png");
- JButton[][] battleshipgrid = new JButton[5][5];
- for(int i=0; i < battleshipgrid.length; i++) {
- for(int j=0; j < battleshipgrid.length; j++) {
- JButton GridButton = new JButton ("", Config);
- battleshipgrid[i][j] = GridButton;
- GridButton.addActionListener(this);
- GridButton.setActionCommand("Bomb");
- board.add(battleshipgrid[i][j]);
- }
- }
- ImageIcon Play = new ImageIcon("C:/unzipped/images/menu/play.png");
- JButton playButton = new JButton ("Play");
- playButton.setFocusable(false);
- playButton.addActionListener(this);
- playButton.setIcon(Play);
- JButton QuitButton = new JButton ("Quit");
- QuitButton.setFocusable(false);
- QuitButton.addActionListener(this);
- QuitButton.setFocusable(false);
- JButton SoundButton = new JButton ("Voice");
- SoundButton.setFocusable(false);
- SoundButton.addActionListener(this);
- JButton FlySoundbutton = new JButton ("Fly");
- FlySoundbutton.setFocusable(false);
- FlySoundbutton.addActionListener(this);
- JButton myButton = new JButton("Change Man!");
- myButton.addActionListener(this);
- buttonSpace = new JPanel();
- secbuttonSpace = new JPanel();
- buttonSpace.add(QuitButton);
- secbuttonSpace.add(playButton);
- buttonSpace.add(FlySoundbutton);
- buttonSpace.add(SoundButton);
- buttonSpace.add(myButton);
- FlySoundbutton.setActionCommand("Fly");
- SoundButton.setActionCommand("Voice");
- playButton.setActionCommand("Play");
- /*
- myButton.addMouseListener(new MouseAdapter(){
- public void mouseClicked(MouseEvent e){
- ImageIcon Ear = new ImageIcon("C:/unzipped/images/ear.png");
- myButton.setIcon(Ear);
- getContentPane().add(myButton);
- }
- });
- */
- JLabel eastLabel = new JLabel(" Score ");
- eastLabel.setOpaque(true);
- buttonSpace.setBackground(Color.BLUE);
- secbuttonSpace.setBackground(Color.RED);
- getContentPane().add(buttonSpace,BorderLayout.WEST);
- getContentPane().add(secbuttonSpace,BorderLayout.NORTH);
- getContentPane().add(board,BorderLayout.CENTER);
- getContentPane().add(eastLabel,BorderLayout.EAST);
- if (graphicsDevice.isFullScreenSupported()){
- setUndecorated(true);
- setResizable(false);
- graphicsDevice.setFullScreenWindow(this);
- validate();
- }else{
- System.out.println("Full-screen mode not supported");
- }
- }
- public void actionPerformed(ActionEvent e){
- String cmd = e.getActionCommand();
- if (cmd.equals("Quit")){
- System.exit(0);
- }
- else if (cmd.equals("Voice")){
- sound = new SimpleSoundPlayer("C:/unzipped/sounds/robocop.wav");
- sound.getSamples();
- new Thread() {
- public void run() {
- InputStream stream =
- new ByteArrayInputStream(sound.getSamples());
- sound.play(stream);
- }
- }.start();
- }
- else if (cmd.equals("Fly")){
- soundy = new SimpleSoundPlayer("C:/unzipped/sounds/fly-bzz.wav");
- soundy.getSamples();
- new Thread() {
- public void run() {
- InputStream stream =
- new ByteArrayInputStream(soundy.getSamples());
- soundy.play(stream);
- }
- }.start();
- }
- else if (cmd.equals("Bomb")){
- soundx = new SimpleSoundPlayer("C:/unzipped/sounds/bomb.wav");
- soundx.getSamples();
- new Thread() {
- public void run() {
- InputStream stream =
- new ByteArrayInputStream(soundx.getSamples());
- soundx.play(stream);
- }
- }.start();
- }
- else if (cmd.equals("Play")){
- soundz = new SimpleSoundPlayer("C:/unzipped/sounds/EvilLaugh.wav");
- soundz.getSamples();
- new Thread() {
- public void run() {
- InputStream stream =
- new ByteArrayInputStream(soundz.getSamples());
- soundz.play(stream);
- }
- }.start();
- ImageIcon Pause = new ImageIcon("C:/unzipped/images/menu/pause.png");
- JButton PauseButton = new JButton(Pause);
- playButton = PauseButton;
- PauseButton.setIcon(Pause);
- }
- }
- }