Results 1 to 17 of 17
  1. #1
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Methods doing some really odd things:

    First I will describe my problem then break it down the best I can to make it understandable. I am sending a button into the parameter if a method and checking what ImageIcon it is equal too. It is resulting is some very weird behavior like firing an if statement on a button that is suppose to be null or getting the x and y position of several buttons when it is just suppose to get one. Lets just jump into the code:

    I am making a matrix of buttons that will be later used as a path finding algorithm. They are created in the View class like so:

    Java Code:
    JPanel matrixPan(){
    		matrixBtn = new Mybuttons[25][25];
    		JPanel panel = new JPanel();
    		panel.setSize(550,550); 
    		panel.setLayout(new GridLayout(25,25));
    		//creating a 25x25 matrix of buttons
    		for(int i=0; i<25; i++){
    			for(int j=0;j<25;j++)
    			{	//use myButtons to create the matrix of buttons
     
    				panel.add(matrixBtn[i][j] = new Mybuttons());
    				matrixBtn[i][j].setxCoordinate(i);
    				matrixBtn[i][j].setyCoordinate(j);
    				//setting the size of the matrix
    				size++;
    				model.setSize(size);
    			}
    		}
    		panel.setVisible(true);
    As each button is pressed the ImageIcon changes but the following code:

    Java Code:
    try{
    				for(int i=0; i<25;i++){
    					for(int j=0;j<25;j++){
    				 if(e.getSource()==view.matrixBtn[i][j]){
     
    					 //if count is greater than 3, reset it because there are
    					 //only 3 button colors to choose from
    					 if(count[i][j]>=3){
    						 count[i][j]=0;
    					 }
    					view.matrixBtn[i][j].setIcon(button.setImage(count[i][j]++));  
    				 }
    			   }
    It then sets the buttons state/ImageIcon in the Mybuttons Class

    Java Code:
    /*getImage gives each button a color, red is the end point
    	yellow is being searched and green is the start point
    	it get called in the controller class on line 49*/
    	ImageIcon setImage(int count){
     
    		if(count==0){
    			setState(greenIcon);
    			model.setStartNode(true);
    			return greenIcon;
    		}
    		else if(count==1){
    			setState(redIcon);
    			model.setTargetNode(true);
    			return redIcon;
    		}
    		else if (count==2){
    			setState(yellowIcon);
    			model.setBarrier(true);
    			return yellowIcon;
    		}
    		else
    			count=0;
    		    setState(null);
    			return null;
    	}
    Assuming there is one Red button selected (Target button), One Green (Starting position) and however many yellow buttons (barriers) selected should be ok. I am testing that method now and some interesting thing are occurring.

    Once the Enter button is selected:
    Java Code:
    if(e.getSource()==view.enterBtn){
     
    					for(int i=0;i<25;i++){
    						for(int j=0;j<25;j++){
    						//Validates matrix to see if there are more then one
    						 //red or green button pressed
    						 checkMartixValues(view.matrixBtn[i][j]);
    						}
    					}
     
    					bfs(view.matrixBtn[xCoordinate][yCoordinate]);
     
     
    					System.out.println("Start Button " +view.matrixBtn[xCoordinate][yCoordinate].model.getStartNode());
     
    				}
    It check to make sure there is not more then one start or end point. While checking I set each buttons I want to get their position. Currently there are several print statements there for testing.

    Java Code:
    void checkMartixValues(Mybuttons matrixBtn){
    		 //resetting count so that only
    		 //red yellow and green icons can populate
     
    				if(matrixBtn.getState()==Mybuttons.redIcon)
    				{
    					countRedBtn++;
    					System.out.print("red Icon " +countRedBtn);
    				}
    				if(matrixBtn.getState()==Mybuttons.greenIcon){
    					countGreenBtn++;
    					System.out.println(xCoordinate+" "+yCoordinate);
    					xCoordinate = matrixBtn.getxCoordinate();
    					yCoordinate = matrixBtn.getyCoordinate();
    					System.out.print("green Icon ");
    				}
    				if(matrixBtn.getState()==Mybuttons.yellowIcon){
    					System.out.println("Barrier");
    				}
    				/*
    				else{
    					System.out.println("else statement green "+countGreenBtn);
    					System.out.println("else statement red "+countRedBtn);
    				}
    				*/
    		 if(countRedBtn >2 || countGreenBtn>2){
    			 System.out.println(" opps that was a mistake");
    		 }
     
    	}
    The output is:
    Java Code:
    Barrier
    Barrier
    Barrier
    Barrier
    Enter Button false
    Even when there are the correct buttons selected. I have put several println statements in the other methods to see if they are working correctly and they are. However if I just select just one green button I the output is:

    Java Code:
    green Icon  opps that was a mistake
    24 22
    green Icon  opps that was a mistake
    24 23
    green Icon  opps that was a mistake
    Start Button false
    Just a reminder in the enter buttons actionListener I checked the state of the start button like so:
    Java Code:
    System.out.println("Start Button " +view.matrixBtn[xCoordinate][yCoordinate].model.getStartNode());
    I set the x and y coordinates in the above method

    Oddly it is giving me a new x and y coordinate so the if statement is saying the button passed to it is in fact a green button, but still printing the false when I try to confirm it.

    Maybe I should try a whole new approach altogether in handling this situation but I cannot think of one. I keep reverting back to similar logic. Advice?

    I cross posted this a few days ago but no one respond so I thought I would see if you guys might be able to help. I did my best not to confuse anyone.

    Weird behavior in my program
    Last edited by jocdrew21; 12-11-2014 at 10:34 AM.

  2. #2
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Methods doing some really odd things:

    I just noticed two others commented on my other post on this site. Some of their advice suggests that I need to change my design some. I will post the changes soon. It will take me a few hours because I am not at home.

    http://www.java-forums.org/new-java/...ign-issue.html

    Thanks for the help in advance.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Methods doing some really odd things:

    Java Code:
    if(countRedBtn >2 || countGreenBtn>2){
                 System.out.println(" opps that was a mistake");  //<<< Add values here
             }
    It'd be useful if the error message showed the value(s) that were wrong.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Methods doing some really odd things:

    I have spent days on this program and I cannot get the below method to function correctly. I do not know what else to do, therefore I am posting the entire program in hopes someone can point me in the right direction. If you would like to run it on your IDE here is the gitHub link

    https://github.com/drewjocham/BFS-and-DFS-Program.git

    model
    Java Code:
    package path.finding;
    
    import java.awt.Color;
    
    public class Model {
    
    	Color color;
    	Mybuttons button;
    	Boolean  visted,startNode,targetNode,barrier;
    	
    	int size;
    
    	public boolean getBarrier() {
    		return barrier;
    	}
    
    	public void setBarrier(boolean barrier) {
    		this.barrier = barrier;
    	}
    
    	public boolean getStartNode() {
    		return startNode;
    	}
    
    	public void setStartNode(boolean startNode) {
    		this.startNode = startNode;
    	}
    
    	public boolean getTargetNode() {
    		return targetNode;
    	}
    
    	public void setTargetNode(boolean targetNode) {
    		this.targetNode = targetNode;
    	}
    
    	public Boolean getVisted() {
    		return visted;
    	}
    
    	public void setVisted(Boolean visted) {
    		this.visted = visted;
    	}
    	
    	public void setSize(int size){
    		this.size=size;
    	}
    	
    	public int getSize(){
    		
    		return size;
    	}
    	
    }
    view:
    Java Code:
    package path.finding;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    
    import javax.swing.*;
    
    public class View extends Mybuttons{
    	
    	private static final long serialVersionUID = 1L;
    	JFrame frame;
    	JPanel matrixPanel, optionsPanel;
    	Mybuttons[][] matrixBtn;
    	Model model;
    	JButton enterBtn;
    	JButton resetBtn;
    	int size;
    	
    	View(){
    		super();
    		model = new Model();
    		frame = new JFrame("See BFS and DFS in action");
    		frame.setSize(900,500);
    		frame.setLayout(new BorderLayout());
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		matrixPanel=matrixPan();
    		optionsPanel=optionsPan();
    		frame.add(matrixPanel,BorderLayout.EAST);
    		frame.add(optionsPanel,BorderLayout.LINE_END);
    		//frame.pack();
    		frame.setResizable(false);
    		frame.setVisible(true);
    	}
    	
    	JPanel matrixPan(){
    		matrixBtn = new Mybuttons[25][25];
    		JPanel panel = new JPanel();
    		panel.setSize(550,550); 
    		panel.setLayout(new GridLayout(25,25));
    		//creating a 25x25 matrix of buttons
    		for(int i=0; i<25; i++){
    			for(int j=0;j<25;j++)
    			{	
    				//use myButtons to create the matrix of buttons
    				panel.add(matrixBtn[i][j] = new Mybuttons());
    				matrixBtn[i][j].setxCoordinate(i);
    				matrixBtn[i][j].setyCoordinate(j);
    				//setting the size of the matrix
    				size++;
    				model.setSize(size);
    			}
    		}
    		panel.setVisible(true);
    
    		return panel;
    	}
    	
    	JPanel optionsPan(){
    		
    		String[] searchOptions ={"Breath First Search","Depth First Search"};
    		JComboBox box = new JComboBox(searchOptions);
    		JLabel boxLabel = new JLabel("Pick a search algorithm");
    		enterBtn = new JButton("Enter");
    		resetBtn = new JButton("Reset");
    
    		//this panel will hold all the buttons and drop-down box
    		JPanel panel=new JPanel();
    		panel.setLayout(new BoxLayout(panel,BoxLayout.LINE_AXIS));
    		panel.setSize(150,125);
    		
    		JPanel optionPan = new JPanel();
    		optionPan.setLayout(new GridLayout(3,1));
    		optionPan.setSize(25,25);
    		optionPan.add(boxLabel);
    		optionPan.add(box);
    		optionPan.add(resetBtn);
    		optionPan.add(enterBtn);
    		
    		panel.add(optionPan);
    
    		return panel;
    	}
    
    }
    Java Code:
    package path.finding;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    
    public class Controller {
    
    	/*The view class holds the GUI and creates a matrix of MyButtons.
    	 *Each of MyButtons will be treated as a node in the Controller Class
    	 *Consider MyButtons as the Model in the MVC design
    	 * */
    	View view;
    	Mybuttons button = new Mybuttons();
    	Mybuttons b;
    	int xCoordinate,yCoordinate;
    	Model model;
    	int[][] count=new int[25][25];
    	int countRedBtn=0;
    	int countGreenBtn=0;
    	
    	Controller(View view, Model model){
    		
    		this.view = view;
    		this.model = model;
    		Actions action = new Actions();
    
    		//button.addActionListener(new Actions());
    		
    		for(int i=0;i<25;i++){
    			for(int j=0;j<25;j++){
    		view.matrixBtn[i][j].addActionListener(action);
    			}
    		}
    		view.enterBtn.addActionListener(new EnterBtnActions());
    		view.resetBtn.addActionListener(new ResetBtnActions());
    	}
    	
    	class EnterBtnActions implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    			if(e.getSource()==view.enterBtn){
    				for(int i=0;i<25;i++){
    					for(int j=0; j<25;j++){
    						checkMartixValues(view.matrixBtn[i][j]);
    					}
    				}
    			}
    			
    			bfs(view.matrixBtn[xCoordinate][yCoordinate]);
    			
    			//System.out.println(xCoordinate+" "+yCoordinate);
    			System.out.println("Start Button is " +view.matrixBtn[xCoordinate][yCoordinate].model.getStartNode());
    	
    		}
    		
    	}
    	
    	class ResetBtnActions implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			 if(e.getSource()==view.resetBtn){
    				 
    				 resetButtons();
    				 System.out.println("reset Fired");
    			 }
    		}
    		
    	}
    	
    	class Actions implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    		
    				for(int i=0; i<25;i++){
    					for(int j=0;j<25;j++){
    				 if(e.getSource()==view.matrixBtn[i][j]){
    
    					 //if count is greater than 3, reset it because there are
    					 //only 3 button colors to choose from
    					 if(count[i][j]>=3){
    						 count[i][j]=0;
    					 }
    					view.matrixBtn[i][j].setIcon(button.setImage(count[i][j]++));  
    				 }
    			   }	
    		  }	
    	  }
    
    	}
    	/*Checks to make sure there is only one Red, and one Green selected
    	 * in the button matrix.Also sets the x and y coordinates of the 
    	 * start button so bfs and dfs know where to start.
    	 * */
    	void checkMartixValues(Mybuttons matrixBtn){
    				
    				if(matrixBtn.getState()==Mybuttons.redIcon)
    				{
    					countRedBtn++;
    					System.out.print("red Icon " +countRedBtn);
    				}
    				else if(matrixBtn.getState()==Mybuttons.greenIcon){
    					countGreenBtn++;
    					System.out.println(xCoordinate+" "+yCoordinate);
    					xCoordinate = matrixBtn.getxCoordinate();
    					yCoordinate = matrixBtn.getyCoordinate();
    					System.out.print(xCoordinate+" "+xCoordinate);
    					System.out.print("green Icon ");
    				}
    				else if(matrixBtn.getState()==Mybuttons.yellowIcon){
    					System.out.println("Barrier");
    				}
    				else if((matrixBtn.getState()==null)){
    					
    					System.out.println("null");
    				}
    				else{
    					System.out.println("else statement green "+countGreenBtn);
    					System.out.println("else statement red "+countRedBtn);
    				}
    				
    		 if(countRedBtn >2 || countGreenBtn>2){
    			 System.out.println("Green occured"+countGreenBtn);
    			 System.out.println("Red occured" +countRedBtn);
    		 }
    		 
    	}
    
    	void resetButtons(){
    		
    		for(int k=0;k<25;k++){
    			for(int l=0;l<25;l++){				
    			//reset the buttons to original color
    			view.matrixBtn[k][l].setIcon(button.resetButton());	
    			}
    	  }
    
    	}
    	
    	private Mybuttons getUnvisitedChildNode(Mybuttons b){
    		
    		int x= b.getxCoordinate();
    		int j=0;
    		
    		while(j< model.getSize()){
    			if(view.matrixBtn[x][j].model.visted==false &&
    				view.matrixBtn[x][j].model.getBarrier()==false){
    				view.matrixBtn[x][j].setIcon(button.setButtonToBlueWhileSearched());
    				return view.matrixBtn[x][j];
    			}
    		}
    		j++;
    		
    		return null;
    	}
    	//Breath First Search 
    	void bfs(Mybuttons button){
    		Queue<Mybuttons> q = new LinkedList<Mybuttons>();
    		q.add(button);
    		button.setIcon(button.setButtonToBlueWhileSearched());
    		button.model.visted=true;
    		button.setIcon(null);
    		while(!q.isEmpty() && !targetFound(button)){
    			
    		Mybuttons b = (Mybuttons)q.remove();
    		Mybuttons child = null;	
    		
    			while((child=getUnvisitedChildNode(b))!=null){
    				child.model.visted=true;
    				//turn child to a color here or print a value
    				q.add(child);
    				
    				if(child.model.getTargetNode()==true){
    						System.out.println("Found it");
    						break;
    				}
    				button.setIcon(null);
    			}
    		}
    		
    	}	
    	/*Depth First Search
    	 * */
    	void dfs(Mybuttons buttons){
    		
    		Stack<Mybuttons> s = new Stack<Mybuttons>();	
    	}
    	
    	boolean targetFound(Mybuttons button){
    		
    		if(button.model.getTargetNode()==true){
    			return true;
    		}
    		else 
    			return 
    				false;
    	}
    }
    Java Code:
    package path.finding;
    
    import javax.swing.SwingUtilities;
    
    public class Main {
    	
    	public static void main(String args[]){
    		
    		 SwingUtilities.invokeLater(new Runnable()
    		 {
    		    View view =new View();
    		    Model model = new Model();
    		    public void run()
    		    {
    		       new Controller(view,model);
    		    }
    		    });
    		
    	}
    
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Methods doing some really odd things:

    cannot get the below method to function correctly.
    A few questions about your problem:
    what is the name of the method?
    what does the method currently do?
    what do you want the method to do differently?

    The class definition for Mybuttons is missing.
    Last edited by Norm; 12-11-2014 at 10:06 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Methods doing some really odd things:

    what is the name of the method?
    void checkMartixValues(Mybuttons matrixBtn)

    what does the method currently do?
    There is matrix of buttons press once== green, press twice==red press third time== yellow. Red is is Target Node, Green is the starting position and yellow is a barrier.

    The method is suppose make sure there is only one start and one end point. While doing so it finds the x and y coordinate of the start position of the search algorithms know which node to start looking from.

    what do you want the method to do differently?
    I press it without selecting anything null is printed out this is correct because in the MyButtons Constructor I set it to null as they are created.

    lets say I select one green, one red and several yellow and seems all buttons are set to a barrier. I placed several println statements in the method to debug it. It only made me more confused!
    Java Code:
    Barrier
    Barrier
    Barrier
    Barrier
    Barrier
    Start Button is false
    If I select only one red and one green the if statement fires everytime in the method saying all buttons are red
    Java Code:
     
    Red occured623
    red Icon 624
    Green occured0
    Red occured624
    red Icon 625
    Green occured0
    Red occured625
    Start Button is false
    The "Start Button is false" is called in enter button actionListener to see if indeed it is finding the start position.

    Mybutton Class
    Java Code:
    package path.finding;
    
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    
    class Mybuttons extends JButton {
    	
    	private static final long serialVersionUID = 1L;
    	Model model;
    		
    	public static ImageIcon greenIcon = new ImageIcon("green.png");
    	public static ImageIcon redIcon = new ImageIcon("red.png");
    	public static ImageIcon yellowIcon = new ImageIcon("yellow.png");
    	public ImageIcon blueIcon = new ImageIcon("blue.png");
    	private ImageIcon resetIcon = new ImageIcon("null");
    	static ImageIcon state;
    	private int xCoordinate,yCoordinate;
    	
    	
    	Mybuttons(){
    		model = new Model();
    		model.barrier=false;
    		model.startNode=false;
    		model.targetNode=false;
    		model.visted=false;
    		setState(null);
    	}
    	
    	/*getImage gives each button a color, red is the end point
    	yellow is being searched and green is the start point
    	it get called in the controller class on line 49*/
    	ImageIcon setImage(int count){
    
    		if(count==0){
    			setState(greenIcon);
    			model.setStartNode(true);
    			model.setBarrier(false);
    			model.setTargetNode(false);
    			return greenIcon;
    		}
    		else if(count==1){
    			setState(redIcon);
    			model.setTargetNode(true);
    			model.setStartNode(false);
    			model.setBarrier(false);
    			return redIcon;
    		}
    		else if (count==2){
    			setState(yellowIcon);
    			model.setBarrier(true);
    			model.setTargetNode(false);
    			model.setStartNode(false);
    			return yellowIcon;
    		}
    		else{
    			return null;
    		}
    	}
    	
    	//returns a null ImageIcon when the reset button is pressed in the
    	// Controller Class
    	ImageIcon resetButton(){
    		
    		return resetIcon;
    	}
    	
    	public void setState(ImageIcon state){
    			Mybuttons.state = state;
    	}
    	
    	public ImageIcon getState(){
    		
    		return state;
    	}
    	
    	/*Set and get x coordinates so the matrix can be searched
    	 *in the controller class. By each button beware of its position
    	 *in the matrix a simple nested for loop can search columns and rows
    	 *from its position to check if they have been visited yet*/
    	public int getxCoordinate() {
    		return xCoordinate;
    	}
    	public void setxCoordinate(int xCoordinate) {
    		this.xCoordinate = xCoordinate;
    	}
    	public int getyCoordinate() {
    		return yCoordinate;
    	}
    	public void setyCoordinate(int yCoordinate) {
    		this.yCoordinate = yCoordinate;
    	}
    	
    	ImageIcon setButtonToBlueWhileSearched(){
    		return blueIcon;
    	}
    	
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Methods doing some really odd things:

    Some comments:
    The code should NOT use hardcoded magic numbers like 25. The size of the arrays should be set in one variable and that value used in place of the magic number 25. The reason is for testing it will be easier to debug if the size of the arrays is much smaller.

    The code allows a user to set more than one green and more than one red square. If only one is allowed, then after a red (or green) is chosen, that should be removed from the list of choices so it can't be chosen again.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Methods doing some really odd things:

    The code should NOT use hardcoded magic numbers like 25.
    So that's what GregBrannon was talking about a few posts ago.
    I know better then to do that, no excuse why I still did it.

    The code allows a user to set more than one green and more than one red square. If only one is allowed, then after a red (or green) is chosen, that should be removed from the list of choices so it can't be chosen again.
    I think instead of using a matrix of JButtons I will make a matrix of JToggleButtons. I think your advice will be easier to implement that way. Once I get home I will knock this out. Thanks and I am sure I will be posting again soon :)

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

    Default Re: Methods doing some really odd things:

    Quote Originally Posted by jocdrew21 View Post
    So that's what GregBrannon was talking about a few posts ago.
    I know better then to do that, no excuse why I still did it.
    And I told you about it 3 days ago in http://www.java-forums.org/new-java/...ign-issue.html

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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Methods doing some really odd things:

    instead of using a matrix of JButtons I will make a matrix of JToggleButtons
    I don't see how that will help. How many different states (colors) can a matrix square have? If more than 2, the toggle button won't work (it has 2 states).
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Methods doing some really odd things:

    You are right about the JTogglebuttons, I left it as a JButton.

    I did what you suggested and it is still doing the SAME THING!!! This is how I let the program know if the button was already selected.

    Java Code:
    class Actions implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    		
    				for(int i=0; i<size;i++){
    					for(int j=0;j<size;j++){
    				 if(e.getSource()==view.matrixBtn[i][j]){
    				
    					 setButton(view.matrixBtn[i][j]);
    					 
    				 }
    			   }	
    		  }	
    	  }
    Java Code:
    void setButton(Mybuttons btn){
    		//green Icon (start point)
    		if((counter==0)){
    			
    			 btn.setIcon(button.setImage(counter));
    			 setGreenBtn(true);
    			 setRedBtn(false);
    			 model.setStartNode(true);
    			 model.setBarrier(false);
    			 model.setTargetNode(false);
    			 counter=1;
    		 }
    		//red Icon (end point)
    		 else if((counter==1) && (greenBtnIsSelected()) && (redBtnIsSelected())){
    			 btn.setIcon(button.setImage(counter));
    			 setGreenBtn(true);
    			 setRedBtn(true);
    			 model.setStartNode(false);
    			 model.setBarrier(false);
    			 model.setTargetNode(true);
    			 counter=2;
    		 }
    		//yellow Icon (barrier)
    		 else if((counter==2) && (greenBtnIsSelected()) && (redBtnIsSelected())){
    			 btn.setIcon(button.setImage(counter));
    			 model.setStartNode(false);
    			 model.setBarrier(false);
    			 model.setTargetNode(true);
    			 counter=2;
    		 }
    	}
    SetIcon is in the Mybuttons Class
    Java Code:
    /*getImage gives each button a color, red is the end point
    	yellow is being searched and green is the start point
    	it get called in the controller class on line 49*/
    	ImageIcon setImage(int count){
    
    		if(count==0){
    			setState(greenIcon);
    			model.setStartNode(true);
    			model.setBarrier(false);
    			model.setTargetNode(false);
    			return greenIcon;
    		}
    		else if(count==1){
    			setState(redIcon);
    			model.setTargetNode(true);
    			model.setStartNode(false);
    			model.setBarrier(false);
    			return redIcon;
    		}
    		else if (count==2){
    			setState(yellowIcon);
    			model.setBarrier(true);
    			model.setTargetNode(false);
    			model.setStartNode(false);
    			return yellowIcon;
    		}
    		else{
    			return null;
    		}
    	}
    Setters and getters in Controller class
    Java Code:
    boolean redBtnIsSelected(){
    		
    		return redBtn;
    		
    	}
    	boolean greenBtnIsSelected(){
    		
    		return greenBtn;
    	}
    	
    	void setGreenBtn(boolean btn){
    		if(btn=true){
    			greenBtn=true;
    		}
    		else{
    			greenBtn=false;
    		}
    		
    	}
    	
    	void setRedBtn(boolean btn){
    		if(btn=true){
    			redBtn=true;
    		}
    		else{
    			redBtn=false;
    		}
    		
    	}
    Now that the buttons cannot be selected wrongly I am getting the following output:

    Java Code:
    Barrier
    Barrier
    Barrier
    Barrier
    Start Button is false
    From the following method:
    Java Code:
    void checkMartixValues(Mybuttons matrixBtn){
    				
    				if(matrixBtn.getState()==Mybuttons.redIcon)
    				{
    					countRedBtn++;
    					System.out.print("red Icon " +countRedBtn);
    				}
    				else if(matrixBtn.getState()==Mybuttons.greenIcon){
    					countGreenBtn++;
    					System.out.println(xCoordinate+" "+yCoordinate);
    					xCoordinate = matrixBtn.getxCoordinate();
    					yCoordinate = matrixBtn.getyCoordinate();
    					System.out.print(xCoordinate+" "+xCoordinate);
    					System.out.print("green Icon ");
    				}
    				else if(matrixBtn.getState()==Mybuttons.yellowIcon){
    					System.out.println("Barrier");
    				}
    				else if((matrixBtn.getState()==null)){
    					
    					System.out.println("null");
    				}
    				else{
    					System.out.println("else statement green "+countGreenBtn);
    					System.out.println("else statement red "+countRedBtn);
    				}
    				
    		 if(countRedBtn >2 || countGreenBtn>2){
    			 System.out.println("Green occured"+countGreenBtn);
    			 System.out.println("Red occured" +countRedBtn);
    		 }
    		 
    	}
    which is called when the enter buttons is pressed:
    Java Code:
    class EnterBtnActions implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    			if(e.getSource()==view.enterBtn){
    				for(int i=0;i<model.arraySize();i++){
    					for(int j=0; j<model.arraySize();j++){
    						//sets the buttons to start,end or barrier
    						//through Icons
    						checkMartixValues(view.matrixBtn[i][j]);
    					}
    				}
    			}
    			
    			bfs(view.matrixBtn[xCoordinate][yCoordinate]);
    			
    			//System.out.println(xCoordinate+" "+yCoordinate);
    			System.out.println("Start Button is " +view.matrixBtn[xCoordinate][yCoordinate].model.getStartNode());
    	
    		}
    		
    	}
    A lot of the println statements are just for debugging.

    I am trying really hard on this but I am going in circles, please help.

  12. #12
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Methods doing some really odd things:

    GOT IT!!!!!!!!!!!!!!!!!

    I moved the following method to a different ActionListener. Basically when you click on a matrix button it is sent to the method right away apposed to waiting for the enter buttons to be pressed and checking the whole matrix.

    Java Code:
    class Actions implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    		
    				for(int i=0; i<model.arraySize();i++){
    					for(int j=0;j<model.arraySize();j++){
    				 if(e.getSource()==view.matrixBtn[i][j]){
    				
    					 setButton(view.matrixBtn[i][j]);
    					 checkMartixValues(view.matrixBtn[i][j]);
    				 }
    			   }	
    		  }	
    	  }

  13. #13
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Methods doing some really odd things:

    This program is giving me a headache!!! Why in the world would this happen?

    I select a button in a matrix and print out its location then once I hit the enter button it move the y value random number of values.

    Java Code:
    class EnterBtnActions implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    			if(e.getSource()==view.enterBtn){
    				System.out.println(xCoordinate+" "+yCoordinate);
    				System.out.println("Start Button is " +
    				view.matrixBtn[xCoordinate][yCoordinate].model.getStartNode());
    			}
    			
    			//bfs(view.matrixBtn[xCoordinate][yCoordinate]);
    			
    			
    	
    		}
    		
    	}
    output
    Java Code:
    8 8 green Icon
     red Icon 0
    Barrier
    Barrier
    8 7 green Icon
    
    Start Button is false
    of course the button will be false if the Y value is changing!!!!
    Last edited by jocdrew21; 12-12-2014 at 10:14 PM.

  14. #14
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Methods doing some really odd things:

    Last post for the night, I need a break!

    I am setting the start node via this:
    Java Code:
    void setButton(Mybuttons btn){
    		//green Icon (start point)
    		if((counter==0)){
    			
    			 btn.setIcon(button.setImage(counter));
    			 setGreenBtn(true);
    			 setRedBtn(false);
    			 model.setStartNode(true);
    			 model.setBarrier(false);
    			 model.setTargetNode(false);
    			 counter=1;
    		 }
    		//red Icon (end point)
    		 else if((counter==1) && (greenBtnIsSelected()) && (redBtnIsSelected())){
    			 btn.setIcon(button.setImage(counter));
    			 setGreenBtn(true);
    			 setRedBtn(true);
    			 model.setStartNode(false);
    			 model.setBarrier(false);
    			 model.setTargetNode(true);
    			 counter=2;
    		 }
    		//yellow Icon (barrier)
    		 else if((counter==2) && (greenBtnIsSelected()) && (redBtnIsSelected())){
    			 btn.setIcon(button.setImage(counter));
    			 model.setStartNode(false);
    			 model.setBarrier(false);
    			 model.setTargetNode(true);
    			 counter=2;
    		 }
    	}
    However whenever I select [0][0] in the matrix then print out the following whenever I hit the enter buttons:

    Java Code:
    class EnterBtnActions implements ActionListener{
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    			if(e.getSource()==view.enterBtn){
    				System.out.println(xCoordinate+" "+yCoordinate);
    				System.out.println("Start Button is " +
    				view.matrixBtn[xCoordinate][yCoordinate].model.getStartNode() +" "+ 
    				view.matrixBtn[0][0].model.getStartNode());
    			}
    			
    			//bfs(view.matrixBtn[xCoordinate][yCoordinate]);
    			
    			
    	
    		}
    		
    	}
    I get the following output:
    Java Code:
    0 0green Icon red Icon 0Barrier
    Barrier
    Barrier
    0 0
    Start Button is false false
    reset Fired
    Even if the y value is somehow shifting values the button is still not being set despite being given a model per button in the constructor:

    Java Code:
    Mybuttons(){
    		model = new Model();
    		model.barrier=false;
    		model.startNode=false;
    		model.targetNode=false;
    		model.visted=false;
    		setState(null);
    	}
    I really need advice....

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Methods doing some really odd things:

    How many different instances are created of the Model class? Which instance does the code call to save values and get values?

    Hint for debugging to see what the code is doing:
    Add println() statements in the getStartNode and setStartNode methods that print the value of startNode and the value of "this". For example:
    Java Code:
          System.out.println("setStart val="+startNode + ", this="+this);   //<<<<<<<<<<<<
    The value of "this" will be unique for each instance of the Model class allowing you to see which instance has what value of startNode.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Methods doing some really odd things:

    Now I got it!!!

    It is working flawlessly, to bad it took me hours to figure it out. Thanks again norm.

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Methods doing some really odd things:

    Quote Originally Posted by jocdrew21 View Post
    to bad it took me hours to figure it out.
    It was most certainly not "too bad" - it is a good thing to take your time to solve something or else you don't learn nearly as much. Don't let it hurt your ego, eventually you'll become a wizard. In a couple of years.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Fixes for things that bug me?
    By jhsachs in forum NetBeans
    Replies: 0
    Last Post: 09-18-2013, 07:14 PM
  2. Need Help. with two simple things
    By Fred1 in forum New To Java
    Replies: 32
    Last Post: 04-28-2011, 03:35 AM
  3. XML, and other things.
    By Tortex in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 06:53 PM

Posting Permissions

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