If you look at line 74 you will see my actionPerformed method where I want to switch the icon displayed on the JButton. I know I still have to add the images to the button however I am running into a few issues here.
Therefore my main question is, am I even approaching this problem correctly? In my next class it will be vital that I can decipher what each icon is display on the JButton. I am working on programing A* but I have to get everything talking first.
Please take a look at line 74, any advice would be greatly appreciated.
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Screen implements ActionListener { JButton start; JButton reset; JButton box[][] = new JButton[20][20]; JLabel lbl; //Icons to be displayed in box[][] ImageIcon wall; ImageIcon begin; ImageIcon end; //counter how many time box[][] was pressed to enable //the Image Icons private int counter=0; Screen() { //main frame JFrame j = new JFrame("A* Algorithm"); //setting main frame j.setSize(1200,500); j.setLayout(new BorderLayout()); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create Panel for multi array buttons JPanel p = new JPanel(); p.setLayout(new GridLayout(20,20)); //create command buttons "start" and "restart" JPanel CmdBtns = new JPanel(); //create buttons matrix of buttons for(int row=0;row<20;row++) { for(int col=0;col<20;col++) { //create new matrix buttons box[row][col] = new JButton(); //add buttons to panel p.add(box[row][col]); } } //create start button start = new JButton("Start"); CmdBtns.add(start); //create reset button reset = new JButton("Reset"); CmdBtns.add(reset); //Centering matrix buttons onto main JFrame j.add(p,BorderLayout.CENTER); //adding start and restart buttons j.add(CmdBtns,BorderLayout.SOUTH); j.setVisible(true); } //handling matrix buttons public void actionPerformed(ActionEvent ae) { //counting how many times button the the matrix was pressed counter++; //reseting counter to zero if the buttons was pressed three times counter%=3; if(ae.getActionCommand().equals(box)) { switch(counter) { case 0: //display wall icon break; case 1: //display start icon break; case 2: //display end icon break; default: break; } } } public static void main(String[] args) { new Screen(); } }