Page 3 of 4 FirstFirst 1234 LastLast
Results 41 to 60 of 77
Like Tree2Likes

Thread: ActionListener to display different ImageIcons:

  1. #41
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Norm I did try that before and yes it did get rid of the run time error however it didn't change that current state of another button.

  2. #42
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    It's very hard to suggest any coding techniques without a design for the program and the code that has the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #43
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Now I am able to start with the logic, thank you guys so much. However I was unable to change the state of the button during run time. However the code below showed me the buttons are talking and I can accomplish what I need to now.

    Just out of curiosity, what did I do wrong?

    Java Code:
    public class AStar extends Screen
    {
    	AStar()
    	{
    		matrixButtons[0][0].setIcon(wall);
    		
    	if(matrixButtons[0][0].getIcon() != null)
    	{
    		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();	
    	}
    		
    }

  4. #44
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:


  5. #45
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    Post a small, complete program that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  6. #46
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: ActionListener to display different ImageIcons:

    When we offer help, we not only want to help you with your immediate problem, but also develop good coding practices. Astar should not extend Screen because it offers no additional functionality. Can you explain why you are extending it? If it is to be able to access matrixButtons, a better way might be to make it an inner class. An inner class and its enclosing class have access to each others fields and methods, even those declared private.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #47
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    I am very intrigued, inner class in which class? Yes I extended Screen to be able to access matrixButtons because when I tried to inherit matrixButtons to AStar I had an error inside my constructor. I cannot thank you enough for the continued advice. I started C++ this same way and now I am able to contribute to questions on that forum, hopefully I will be able to do the same here.

  8. #48
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: ActionListener to display different ImageIcons:

    Probably a good idea. The funny thing is that the OP ignored my first suggestion and focused on the second. But it's now OBE.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #49
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: ActionListener to display different ImageIcons:

    You can read about them in the tutorials --> Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects). Another method which might be simpler since you're not that familiar with Java is to pass an instance of the Screen class (or whatever class that has the matrix) to the constructor of the Astar class. Then you can use that to access the matrixButton array.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #50
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Jim i have read the link but I am still a little confused. I will do some more research because I really need to be able to access variables in the matrixbuttons class. As you can tell from my bottom post they are very important. Do not beat me up on the implementation yet, I will incorporate a helper method.




    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; 
    		
    		int f_value; // G + heuristic
    		int g_value; //movement cost
    		int heuristic; //distance from current node to end node
     
    		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:
    import java.util.ArrayList;
    
    public class AStar extends MatrixButtons
    {
    	ArrayList openList;
    	ArrayList closedList;
    		
    	/* 
    	 if movement is diagonal g_value is set to 15
    	 else it is vertical and set to 10
    	 */
    	int setGvalue(MatrixButtons[][] current[][],int row, int col)
    	{
    		//diagonal top left
    		current[row-1][col-1].g_value=15;
    		//diagonal top right
    		current[row-1][col+1].g_value = 15;
    		//diagonal bottom right
    		current[row+1][col+1].g_value = 15;
    		//diagonal bottom left
    		current[row+1][col-1].g_value = 15;
    		
    		//vertical top
    		current[row-1][col].g_value = 10;
    		//vertical right
    		current[row][col+1].g_value = 10;
    		//vertical bottom
    		current[row+1][col].g_value = 10;
    		//vertical left
    		current[row][col-1].g_value = 10;
    		
    	}
    	/*
    	 Total distance from start button to end button. Walls
    	 will not be considered as barriers.
    	 */
    	int getHeuristic(MatrixButtons[][] current[][], int row, int col)
    	{
    		
    		return g_value;
    	}
    	/*
    	 getFvalue returns getHeuristic() + getGvalue() to later decide
    	 the best move to make next
    	 * */
    	int getFvalue()
    	{
    		return getHeuristic() + getGvalue();
    	}
    	
    	public static void main(String[] args)
    	{
    		new AStar();	
    	}
    		
    }

  11. #51
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    How will the AStar class be used? Why does it have a main() method?
    If you don't understand my response, don't ignore it, ask a question.

  12. #52
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    AStar will be the logic, post #44 has a youtube video of what I am trying to do.

    It has a main method because it will be the last method which will implement all the others.

  13. #53
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    Why does it extend the MatrixButtons class? What objects go in the 20x20 array? AStar or MatrixButtons?
    If you don't understand my response, don't ignore it, ask a question.

  14. #54
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    It did extend Screen which worked until I tired:
    Java Code:
    int setGvalue(MatrixButtons[][] current[][],int row, int col)
        {
            //diagonal top left
            current[row-1][col-1].g_value=15;
    which is extened from:
    Java Code:
    public class MatrixButtons extends JButton implements ActionListener
    {
        //Icons to be displayed in box[][]
            ImageIcon wall; 
            ImageIcon begin; 
            ImageIcon end; 
             
            int f_value; // G + heuristic
            int g_value; //movement cost
            int heuristic; //distance from current node to end node
    mattrixButtons[][] have icons set to it which will later be used to figure out its state.

    I decided to change it on Jims advice but I have still not implemented the inner classes he was referring to in post #49.
    Last edited by jocdrew21; 05-26-2014 at 10:55 PM.

  15. #55
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    ???? No idea what you are saying or if you are asking a question or having any problem.
    If you don't understand my response, don't ignore it, ask a question.

  16. #56
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Why does it extend the MatrixButtons class? What objects go in the 20x20 array? AStar or MatrixButtons?
    I extended MatrixButtons because I need to use them in AStar class. Each matrixButton[][] must be able to access variables like int g_value, int f_value etc.
    These values will change while run time as well.

    ???? No idea what you are saying or if you are asking a question or having any problem.
    I made the mistake of assuming this is somewhat like a conversation. I will treat each post as if it is completely new explain my question and objective each time.

  17. #57
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    I extended MatrixButtons because I need to use them in AStar class
    What objects go in the 20x20 array?
    If you don't understand my response, don't ignore it, ask a question.

  18. #58
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Norm I think this should clear up your question:

    Please take a look at line 59, this method add's a martixButton onto an ArrayList "openList" and now I need to access a variable within the button after it is on the list. I need to sort the buttons by that variable, return the button with the lowest variable, place the unused buttons onto another ArrayList "closedList", then clear openList. Recursion will be later used to keep the ball rolling to that method from the findPath() method.

    Java Code:
    import java.util.ArrayList;
    
    public class AStar extends MatrixButtons
    {
    	ArrayList<MatrixButtons> openList = new ArrayList<MatrixButtons>();
    	ArrayList<Integer> closedList = new ArrayList<Integer>();
    	
    	//current button
    	MatrixButtons[][] current;
    	MatrixButtons[][] endButton;
    	MatrixButtons[][] startButton;
    	MatrixButtons[][] temp;
    	MatrixButtons[][] bestNextMove;
    	
    	//these variables will be used to find the rows and columns
    	//in order to get the heuristic
    	int startRow;
    	int startCol;
    	int endRow;
    	int endCol;
    	
    	//loop though all buttons and finds the end icon
    	void setEndButton()
    	{
    	
    		for(int i=0;i<20;i++)
    		{
    			for(int j =0;j<20;j++)
    			{
    				//find end icon and setting it to endButton
    				if(temp[i][j].getIcon().equals(end))
    				{
    					endButton[i][j]=temp[i][j];
    					endRow = i;//will be used to find the heuristic
    					endCol = j;//will be used to find the heuristic
    				}
    			}
    		}
    	}
    	//loops through all the buttons and finds the start icon
    	void setStartButton()
    	{
    		for(int i=0;i<20;i++)
    		{
    			for(int j =0;j<20;j++)
    			{
    				//find start icon and setting it to startButton
    				if(temp[i][j].getIcon().equals(begin))
    				{
    					startButton[i][j]=temp[i][j];
    					startRow = i;//will be used to find the heuristic
    					startCol = j;//will be used to find the heuristic
    				}
    			}
    		}
    	}
    		
    	/* 
    	 if movement is diagonal g_value is set to 15
    	 else it is vertical and set to 10
    	 This method gets the current nodes row and column and
    	 sets the g_value instead of using a loop 
    	 */
    	void setGvalue(int row, int col)
    	{
    		//diagonal top left
    		current[row-1][col-1].g_value=15;
    		//diagonal top right
    		current[row-1][col+1].g_value=15;
    		//diagonal bottom right
    		current[row+1][col+1].g_value=15;
    		//diagonal bottom left
    		current[row+1][col-1].g_value=15;
    		
    		//vertical top
    		current[row-1][col].g_value=10;
    		//vertical right
    		current[row][col+1].g_value=10;
    		//vertical bottom
    		current[row+1][col].g_value=10;
    		//vertical left
    		current[row][col-1].g_value=10;
    		
    	}
    	/*
    	 Total distance from start button to end button. Walls
    	 will not be considered as barriers.
    	 */
    	
    	int getHeuristic(MatrixButtons[][] h, int row, int col)
    	{
    		return h[row][col].heuristic = (endRow-row) + (endCol-col);
    	}
    	
    	/*
    	 getFvalue returns getHeuristic() + getGvalue() to later decide
    	 the best move to make next
    	 * */
    	int getFvalue(MatrixButtons[][] f,int row, int col)
    	{
    		return f[row][col].f_value = f[row][col].g_value 
    			   + getHeuristic(f,row,col);	
    	}
    	
    	/*this method will receive the row and column of the current button
    	  and return a matrixButton with a lesser f_value by placing each option
    	  on the ArrayList openList, sorting openList least to greatest, and
    	  returning the first matrixButton on the list. The rest of the buttons will
    	  be moved to the closed list so they are never searched again and the
    	  open listed is cleared again.
    	  
    	  */
    	MatrixButtons findNextMove(int row, int col)
    	{
    		//adding elements on the open list to be checked
    		//diagonal top left
    		openList.add(current[row-1][col-1]);
    		//diagonal top right
    		openList.add(current[row-1][col+1]);
    		//diagonal bottom right
    		openList.add(current[row+1][col+1]);
    		//diagonal bottom left
    		openList.add(current[row+1][col-1]);
    		//vertical top
    		openList.add(current[row-1][col]);
    		//vertical right
    		openList.add(current[row][col+1]);
    		//vertical bottom
    		openList.add(current[row+1][col]);
    		//vertical left
    		openList.add(current[row][col-1]);
    		
    		for(int i=0; i<openList.size();i++)
    		{
    			int value = openList.f_value.get(i); //error under f_value
    			
    		}
    		//return null;
    		
    	}
    	
    	void findPath()
    	{
    		//setting current button to start button
    		current = startButton;
    
    		int row = startRow;
    		int col = startCol;
    		int value;
    		
    		setGvalue(row,col);//setting g_values to start buttons position
    		
    		while(current != endButton)
    		{
    			
    			
    			
    		}
    		
    	}
    	
    	public static void main(String[] args)
    	{
    		new AStar();	
    	}
    		
    }

  19. #59
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    I still don't see why AStar needs to extend MatrixButtons. Each instance of AStar has ONE MatrixButtons object with it.
    If you don't understand my response, don't ignore it, ask a question.

  20. #60
    jocdrew21 is online now Senior Member
    Join Date
    Jan 2014
    Posts
    107
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    Because I need to be able to access the Icons

Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

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