Page 4 of 4 FirstFirst ... 234
Results 61 to 77 of 77
Like Tree2Likes

Thread: ActionListener to display different ImageIcons:

  1. #61
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    The icons are in the wrong place. Move them to where they will be loaded one time and their values passed to the buttons objects when the buttons are created. The icons should NOT be loaded 20x20 times. Load them once.
    If you don't understand my response, don't ignore it, ask a question.

  2. #62
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    They are not, post 23 has the code. ActionEvent handles them in a switch statement.

  3. #63
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    Add a println() statement next to where the icons are loaded, execute the code and count the number of outputs that are printed to see.
    If you don't understand my response, don't ignore it, ask a question.

  4. #64
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default Re: ActionListener to display different ImageIcons:

    How many times in your code do you do something like this?
    Java Code:
    new MatrixButtons();
    Everytime you create an instance of MatrixButtons or any class that extends it, you also do the following:
    Java Code:
    MatrixButtons()  {
         wall = new ImageIcon("wall.png");
         begin = new ImageIcon("begin.png");
         end = new ImageIcon("end.png");
         this.addActionListener(this);       
    }
    Each invocation of the constructor loads the icons. At one time, you did this 20 x 20 times.

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

  5. #65
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    I take it out of the constructor and set it to a global variable and I get errors because each button still needs an action listener.


    I am still having issues with the for loop and trying to access the variable f_value after it has been placed into an ArrayList, any suggestions?
    Java Code:
    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;
    	}

  6. #66
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    I get errors
    Please copy the full text of the error message and paste it here. It has important info about the error.
    each button still needs an action listener
    There shouldn't be any problem adding an action listener to each button.

    trying to access the variable f_value
    That variable is NOT part of the arraylist object. It is part of an object that is contained in the arraylist.
    Get the object from the arraylist and then access that object's f_value variable.
    If you don't understand my response, don't ignore it, ask a question.

  7. #67
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    got it thanks.

    Java Code:
    for(int i=0; i<openList.size();i++)
    		{
    			value = openList.get(i).f_value;
    			
    		}

  8. #68
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    I will post the full code in a moment but I think I just about have the A* algorithm programed. I will fix the noob mistakes once I get a few tweaks out. However I am scratching my head on how to get my start button to work. This is my attempt:

    Java Code:
    //handling start button with inner class
    		start.addActionListener(new ActionListener()
    		{
    
    			public void actionPerformed(ActionEvent ae) 
    			{
    				AStar star =new AStar(); //method with all the logic
    				
    				star.findEndButton();
    				star.findStartButton();
    				star.findPath();
    				star.markPath();
    			}
    
    		});
    Because when I did I got the following errors:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at AStar.findEndButton(AStar.java:41)
    	at Screen$1.actionPerformed(Screen.java:59)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6505)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:694)
    	at java.awt.EventQueue$3.run(EventQueue.java:692)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:708)
    	at java.awt.EventQueue$4.run(EventQueue.java:706)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

  9. #69
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at AStar.findEndButton(AStar.java:41)
    What variable on line 41 is null when line 41 is executed?
    If you don't understand my response, don't ignore it, ask a question.

  10. #70
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    The error is now on 39.

    When I do this, I am setting those buttons to the current, already made buttons but they are unitized, correct?

    Java Code:
    MatrixButtons[][] current;
    	MatrixButtons[][] endButton;
    	MatrixButtons[][] startButton;
    	MatrixButtons[][] temp;
    	MatrixButtons bestNextMove;
    Full code example

    Java Code:
    import java.awt.BorderLayout;
    import java.util.ArrayList;
    
    public class AStar extends MatrixButtons
    {
    	
    	ArrayList<MatrixButtons> openList = new ArrayList<MatrixButtons>();
    	ArrayList<MatrixButtons> tempList = new ArrayList<MatrixButtons>();
    	ArrayList<MatrixButtons> closedList = new ArrayList<MatrixButtons>();
    	
    	MatrixButtons[][] current;
    	MatrixButtons[][] endButton;
    	MatrixButtons[][] startButton;
    	MatrixButtons[][] temp;
    	MatrixButtons bestNextMove;
    	//stores the location of the next best move in order to find it
    	//inside tempList from another method
    	int nextMoveIndex; 
    	
    	//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 findEndButton()
    	{
    	
    		for(int i=0;i<20;i++)
    		{
    			for(int j =0;j<20;j++)
    			{
    				if(temp[i][j].getIcon()!= null)
    				{
    				//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 findStartButton()
    	{
    		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 setValues(int row, int col)
    	{
    		//diagonal top left
    		current[row-1][col-1].g_value=15;
    		current[row-1][col-1].heuristic = getHeuristic(current,row-1,col-1);
    		current[row-1][col-1].f_value = getFvalue(current,row-1,col-1);
    		//diagonal top right
    		current[row-1][col+1].g_value=15;
    		current[row-1][col+1].heuristic = getHeuristic(current,row-1,col+1);
    		current[row-1][col+1].f_value = getFvalue(current,row-1,col+1);
    		//diagonal bottom right
    		current[row+1][col+1].g_value=15;
    		current[row+1][col+1].heuristic = getHeuristic(current,row+1,col+1);
    		current[row+1][col+1].f_value = getFvalue(current,row+1,col+1);
    		//diagonal bottom left
    		current[row+1][col-1].g_value=15;
    		current[row+1][col-1].heuristic = getHeuristic(current,row+1,col-1);
    		current[row+1][col-1].f_value = getFvalue(current,row+1,col-1);
    		
    		//vertical top
    		current[row-1][col].g_value=10;
    		current[row-1][col].heuristic = getHeuristic(current,row-1,col);
    		current[row-1][col].f_value = getFvalue(current,row-1,col);
    		//vertical right
    		current[row][col+1].g_value=10;
    		current[row][col+1].heuristic = getHeuristic(current,row,col+1);
    		current[row][col+1].f_value = getFvalue(current,row,row+1);
    		//vertical bottom
    		current[row+1][col].g_value=10;
    		current[row+1][col].heuristic = getHeuristic(current,row+1,col);
    		current[row+1][col].f_value = getFvalue(current,row+1,col);
    		//vertical left
    		current[row][col-1].g_value=10;
    		current[row][col-1].heuristic = getHeuristic(current,row,col-1);
    		current[row][col-1].f_value = getFvalue(current,row,col-1);
    		
    	}
    	/*
    	 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.
    	  
    	  */
    	MatrixButtons findNextMove(int row, int col)
    	{
    		int value=0;
    		int temp=0;
    		
    		//adding elements on the temp list to be checked
    		//diagonal top left
    		tempList.add(current[row-1][col-1]);
    		//diagonal top right
    		tempList.add(current[row-1][col+1]);
    		//diagonal bottom right
    		tempList.add(current[row+1][col+1]);
    		//diagonal bottom left
    		tempList.add(current[row+1][col-1]);
    		//vertical top
    		tempList.add(current[row-1][col]);
    		//vertical right
    		tempList.add(current[row][col+1]);
    		//vertical bottom
    		tempList.add(current[row+1][col]);
    		//vertical left
    		tempList.add(current[row][col-1]);
    		
    		//setting temp to the first number
    		//assuming it is the smallest number
    		temp = tempList.get(0).f_value;
    		
    		for(int i=0; i<tempList.size();i++)
    		{
    			value = tempList.get(i).f_value;
    			
    			if(temp > value)
    			{
    				//temp is not equal to the smallest number again
    				temp=value;
    				bestNextMove = tempList.get(i);
    				nextMoveIndex =i;
    			}
    			
    		}
    		//returns the button with the smallest f_value
    		return bestNextMove; 
    	}
    	
    	//puts checked and unused buttons onto closed list
    	// and keeps used button on closed list.
    	void cleanTempList()
    	{
    		//moves the bestNextMove into the open list
    		openList.add(tempList.get(nextMoveIndex));
    		
    		//removes the bestNextMove from tempList
    		tempList.remove(nextMoveIndex);
    		
    		//store remaining buttons that were searched and
    		//found not to be the shortest path to closed List
    		for(int i=0;i<tempList.size();i++)
    		{
    			closedList.set(i, tempList.get(i));
    		}
    	
    		//clears tempList
    		tempList.clear();
    		
    	}
    	
    	void findPath()
    	{
    		//setting current button to start button
    		current = startButton;
    		JLabel found;
    		JPanel panel;
    		
    		int row = startRow;
    		int col = startCol;
    		
    		//setting g_value,and Heuristic to start buttons position
    		setValues(row,col);
    		
    		while(current != endButton)
    		{
    			current[row][col]=findNextMove(row,col);
    			
    			if(current==endButton)
    			{
    				JFrame j = new JFrame("FOUND!!!");
    				panel = new JPanel();
    				panel.setLayout(new BorderLayout());
    				found = new JLabel("END BUTTON WAS FOUND!!!");
    				panel.add(found);
    				j.add(panel);
    			}
    			cleanTempList();
    			findPath();
    		}
    		
    	}
    	
    	void markPath()
    	{
    		for(int i=0;i<openList.size();i++)
    		{
    		openList.get(i).setIcon(begin);
    		}
    	}
    	public static void main(String[] args)
    	{
    		new Screen();	
    	}
    }

  11. #71
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    The error is now on 39.
    What variable on line 39 has the null value?
    Why doesn't that variable have a valid value?
    If you don't understand my response, don't ignore it, ask a question.

  12. #72
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    I do not know why. I placed an if statement in there to check if it was null.

    When I do this, I am setting those buttons to the current, already made buttons but they are unitized, correct?

    Java Code:
    Java Code:
    MatrixButtons[][] current;
        MatrixButtons[][] endButton;
        MatrixButtons[][] startButton;
        MatrixButtons[][] temp;
        MatrixButtons bestNextMove;

  13. #73
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    The statements in the quote box define some variables, but do not assign them any value. Their values will be null.
    If you don't understand my response, don't ignore it, ask a question.

  14. #74
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    I honestly don't know what to assign them that's why I used the loop hoping it would ignore null buttons

  15. #75
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    I honestly don't know what to assign them
    Comment those definitions out if you don't know what to assign them.
    What are they supposed to be used for? Why have them?
    If you don't understand my response, don't ignore it, ask a question.

  16. #76
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Re: ActionListener to display different ImageIcons:

    I need them and how I want to use them?

    Current is the current button being check which will all 8 buttons around it will have there f_value checked.
    Temp is needed to find the end icon to set endButton to that and to set the row and col of the end button.

  17. #77
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: ActionListener to display different ImageIcons:

    Also posted at: Null pointer error JButton:
    If you don't understand my response, don't ignore it, ask a question.

Page 4 of 4 FirstFirst ... 234

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
  •