Page 2 of 4 FirstFirst 1234 LastLast
Results 21 to 40 of 77
Like Tree2Likes

Thread: ActionListener to display different ImageIcons:

  1. #21
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Solid information thank you. I will do some reading and YouTube browsing before I post again. Don't want to overstay my welcome here.

    Thank you.....

  2. #22
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: ActionListener to display different ImageIcons:

    You cannot overstay your welcome. I have seen threads that last over 200 posts. Check out the tutorials in my signature. They cover most of the basic listeners with examples.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #23
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    IT IS WORKING!!!!!!!!!!!!!!!!!!!!!!

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class Screen
    {
    	JButton start;
    	JButton reset;
    	MatrixButtons[][] matrixButtons = new MatrixButtons[20][20];
     
    	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
    				matrixButtons[row][col] = new MatrixButtons();
    				//add buttons to panel
    				p.add(matrixButtons[row][col]); //error on line
    			}
    		}
     
    		//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);
    	}
     
    	public static void main(String[] args)
    	{
    		new Screen();
    	}
     
    }
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
     
    public class MatrixButtons extends JButton implements ActionListener
    {
    	//Icons to be displayed in box[][]
    		ImageIcon wall; 
    		ImageIcon begin; 
    		ImageIcon end; 
     
    		byte value =0;
    		/*
    		 0:nothing
    		 1:wall
    		 2:begin
    		 3:end
    		 */
     
    		MatrixButtons()
    		{
    			wall = new ImageIcon("wall.png");
    			begin = new ImageIcon("begin.png");
    			end = new ImageIcon("end.png");
    			this.addActionListener(this);
    			
    		}
     
    		public void actionPerformed(ActionEvent ae) 
    		{
    			value++;
    			value%=4;//resets button after pressed 4 times
     
    			switch(value)
    			{
    			case 0:
    				//button has not been pressed
    				setIcon(null);   
    				break;
    			case 1:
    				//button has been pressed once
    				setIcon(wall); 
    				break;
    			case 2:
    				//button has been pressed twice
    				setIcon(begin);   
    				break;
    			case 3:
    				//button has been pressed three times
    				setIcon(end);
    				break;
    				default:
    					break;
    			}
     
    		}
     
    }

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    Comments:
    Instead of hardcoding 20 in several places, use a variable that defines the size of the array.
    Use the .length of the matrixButtons array when looping over the elements in the array instead of hardcoding the size as 20.

    You could pass the row,column to the MatrixButton class's constructor so each button will know where it is located.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Now I am looking around on how to assign a value to a button but I keep finding how to assign text values to buttons. I want to treat each button as if it is a node. Each button (node) will have to calculate three values which will sometimes times change on the heap.

    Java Code:
    //something like this
    int F;
    int heuristic = //this will be a loop counting the approx count of buttons from current button to "stop" icon
    int G = will equal 10 if the movement is vertical, 15 if it is diagonal assuming this button is picked from the current button
    matrixButtons[i][j].F = heuristic + G; //the lesser value will be chosen as the best next button to search for shortest path.

    I need these values to be in each button, but not written or displayed in the button. I hope I explained this clearly.

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    The values stored in the MatrixButton class will be privately held in the class and won't be shown.
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Gentlemen I am about to actually start working on the logic but naturally I have another issue.

    Java Code:
    public class AStar extends MatrixButtons
    {
    	
    	if(matrixButtons[0][0]=="wall.png")
    	{
    		matrixButtons[1][1]="stop.png";
    	}
    	else
    	{
    		
    	}
    
    }
    This is my new class AStar and this is my attempt to test the buttons working together the way I need them too.
    I tried to post a screen shot of the errors but for whatever reason it wouldn't work.

    line 3
    line 7 says matrixButtons[][] cannot be resolved into a variable
    line 14 says to add } to complete class body

    if I extended the class MatrixButtons I should be able to access the class variables. What am I missing?

    Java Code:
    public class AStar extends MatrixButtons
    {
    	
    	if(matrixButtons[0][0].getBackround()==wall)
    	{
    		matrixButtons[1][1].setBackgroundResource(end);
    	}
    	else
    	{
    		
    	}
    
    }
    I tired it this way too.
    Last edited by jocdrew21; 05-25-2014 at 07:46 AM.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    matrixButtons[][] cannot be resolved into a variable
    Where is the array: maxtrixButtons defined? Is it in scope where the code is using it?

    Hard to say what the problem is without the full text of the error message and all of the relevant code.

    Why do you need the AStar class?
    If you don't understand my response, don't ignore it, ask a question.

  9. #29
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: ActionListener to display different ImageIcons:

    Also, you can't put logic like that directly in a class body. It must be in a method or constructor as appropriate. Second, don't use == to compare strings, use equals().

    Regards,
    Jim
    Last edited by jim829; 05-25-2014 at 03:02 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #30
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Yes I should have used equals if I was comparing strings, C++ habit. However for these case which I will post below it is different. I made three classes, AStar will inherit MatrixButtons which will be used find the shortest path. In the last code example you will see my attempt to access the other classes buttons. It is VERY important that I can know the current state of each button, ie if it is a wall, start position (icon) or end position (icon). This logic in AStar class will depend on this entirely. The code currently in AStar is just debugging and just my attempt to see if I can talk to other buttons which failed.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
     
    public class Screen extends MatrixButtons
    {
    	JButton start;
    	JButton reset;
    	static int ROW =20;
    	static int COL =20;
    	MatrixButtons[][] matrixButtons = new MatrixButtons[ROW][COL];
     
    	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
    				matrixButtons[row][col] = new MatrixButtons();
    				//add buttons to panel
    				p.add(matrixButtons[row][col]); //error on line
    			}
    		}
     
    		//create start button
    		start = new JButton("Start");
    		CmdBtns.add(start);
    		
    		//handling start button with inner class
    		start.addActionListener(new ActionListener()
    		{
    
    			public void actionPerformed(ActionEvent ae) 
    			{
    				
    			}
    
    		});
     
    		//create reset button
    		reset = new JButton("Reset");
    		CmdBtns.add(reset);
    		
    		//handling reset button with inner class
    		reset.addActionListener(new ActionListener()
    		{
    
    			public void actionPerformed(ActionEvent ae) 
    			{
    				for(int i=0; i<ROW;i++)
    				{
    					for(int j=0; j<COL; j++)
    					{
    						matrixButtons[i][j].setIcon(null);
    					}
    			}
    				}
    
    		});
     
    		//Centering matrix buttons onto main JFrame
    		j.add(p,BorderLayout.CENTER);
     
    		//adding start and restart buttons
    		j.add(CmdBtns,BorderLayout.SOUTH);
     
    		j.setVisible(true);
    	}
    	
    	
    	public static void main(String[] args)
    	{
    		new Screen();
    	}
     
    }
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
     
    public class MatrixButtons extends JButton implements ActionListener
    {
    	//Icons to be displayed in box[][]
    		ImageIcon wall; 
    		ImageIcon begin; 
    		ImageIcon end; 
     
    		byte value =0;
    		/*
    		 0:nothing
    		 1:wall
    		 2:begin
    		 3:end
    		 */
     
    		MatrixButtons()
    		{
    			wall = new ImageIcon("wall.png");
    			begin = new ImageIcon("begin.png");
    			end = new ImageIcon("end.png");
    			this.addActionListener(this);
    			
    		}
     
    		public void actionPerformed(ActionEvent ae) 
    		{
    			value++;
    			value%=4;//resets button after pressed 4 times
     
    			switch(value)
    			{
    			case 0:
    				//button has not been pressed
    				setIcon(null);   
    				break;
    			case 1:
    				//button has been pressed once
    				setIcon(wall); 
    				break;
    			case 2:
    				//button has been pressed twice
    				setIcon(begin);   
    				break;
    			case 3:
    				//button has been pressed three times
    				setIcon(end);
    				break;
    				default:
    					break;
    			}
     
    		}
    
    }
    Java Code:
    public class AStar extends MatrixButtons
    {
    	
    	if(matrixButtons[0][0].getBackround()==wall)
    	{
    		matrixButtons[1][1].setBackgroundResource(end);
    	}
    	else
    	{
    		
    	}
    	
    }
    Last edited by jocdrew21; 05-25-2014 at 09:08 PM.

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    Does the posted code compile?
    Post the full text of any errors.

    current state of each button,
    The MatrixButtons class can have methods and/or variables that say what image it is showing and to change it.

    There are still hardcoded 20 used in the code instead of using ROW and COL or the array's .length field.
    Last edited by Norm; 05-25-2014 at 09:19 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #32
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    When I comment out AStar yes it compiles.

    Errors per line post #27

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    Was there a response to post#27?
    If you don't understand my response, don't ignore it, ask a question.

  14. #34
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Yes I responded to it in post #30.

    Java Code:
    import javax.swing.JButton;
    
    public class AStar extends Screen
    {
    	
    	if(matrixButtons[0][0].equals(wall))
    	{
    		matrixButtons[1][1].equals(end);
    	}
    	else
    	{
    		
    	}
    		
    }
    I extended Screen and was able to get the errors to go away from the variables mattrixButtons[][]. However it keeps saying syntax error add a } which makes not sense. Code looks the same as post#30 just extended Screen in AStar in stead of mattrixButtons.

    I am looking all over the internet and I keep getting hit on how to make JButtons etc. This is rather frustrating but its how I learn best. Any advice of why my class posted above is confused saying it would like additional } added to it when they are not belonging?

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    The statements inside of the AStar class must be inside of a method.
    If you don't understand my response, don't ignore it, ask a question.

  16. #36
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    That was a silly mistake, thank you. I look around I figured out I was trying to check the state of the button wrong here is my fix:
    Java Code:
    public class AStar extends Screen
    {
    	AStar()
    	{
    	if(matrixButtons[0][0].getIcon().equals(wall))
    	{
    		matrixButtons[1][1].setIcon(end);
    	}
    	else
    	{
    		matrixButtons[1][1].setIcon(null);
    	}
    	
    	}
    	
    	public static void main(String[] args)
    	{
    		new AStar();	
    	}
    		
    }
    Naturally it is not working. I am just trying to experiment accesses other buttons and changing there state during run time. I am trying to figure out how to apply the logic for the path finding algorithm.

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at AStar.<init>(AStar.java:6)
    	at AStar.main(AStar.java:20)
    Now every button is null to start off with, line 38 post #30 sets it to that. It has to be set to null so I am a little stumped. Suggestions?

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    Suggestions?
    Test if the array element is null BEFORE using it.
    If you don't understand my response, don't ignore it, ask a question.

  18. #38
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,423
    Rep Power
    5

    Default Re: ActionListener to display different ImageIcons:

    Quote Originally Posted by jocdrew21 View Post
    That was a silly mistake, thank you.
    Makes me wonder if you read our posts or just scan them. I mentioned that in post #29 yesterday.

    Regards,
    Jim
    Last edited by jim829; 05-26-2014 at 04:04 PM. Reason: typo
    Norm likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #39
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Sorry, I think I got stuck on the equals vs the == and hastily went to fix it.

  20. #40
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    That re-enforces a theory of mine: Responses should only mention ONE item at a time. OPs often stop reading after the first item.
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. Comparing ImageIcons
    By nubshat in forum New To Java
    Replies: 4
    Last Post: 03-10-2013, 04:56 AM
  2. Replies: 3
    Last Post: 01-31-2012, 12:01 AM
  3. Replies: 1
    Last Post: 05-31-2010, 08:46 AM
  4. Replies: 1
    Last Post: 03-09-2009, 03:18 AM
  5. Problems with ImageIcons
    By The_L in forum AWT / Swing
    Replies: 2
    Last Post: 10-24-2008, 02:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •