Results 1 to 13 of 13

Thread: Tower of Hanoi

  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Tower of Hanoi

    Hey, not sure if anyone can help because that at this point the code has lost shape..
    I am trying to move disc plates from one poll to another with mouse clicks, but I think it doesn't do that since it needs to draw the stacks again and I don't seem to be able to
    do that..

    Java Code:
    package Stacks_Towers_Of_Hanoi;
    
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Rectangle2D;
    import java.awt.geom.RectangularShape;
    import java.util.Stack;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    
    public class HanoiGui2 extends JFrame{
    	static Stack <Integer> a = new Stack <Integer>();
    	static Stack <Integer> b = new Stack <Integer>();
    	static Stack <Integer> c = new Stack <Integer>();
    	
    	private static final long serialVersionUID = 1L;
    	
    	
    	//sizes & locations
    	//polls
    	int xPoll=0;
    	int yPoll=320;
    	int widthOfPoll=20;
    	int heightOfPoll=280;
    	int xMargin = 200;
    	//discs
    	int xDisc = 134;
    	int yDisc = 580;
    	int widthDisc = 15;
    	int xDisc2 = 383;
    	int yDisc2 = 580;
    	int widthDisc2 = 150;
    	int xDisc3 = 583;
    	int yDisc3 = 580;
    	int widthDisc3 = 15;
    	int heightDisc = 20;
    	int addSize = 10;
    	static //from & to;
    	int from = 0;
    	static int to = 0;
    	static Stack <Integer> fromStack = null;
    	static Stack <Integer> toStack = null;
    	
    	
    	public static void main (String args[]){
    		//initialize poll a
    		//test
    		a.push(10);
    		a.push(9);
    		a.push(8);
    		a.push(7);
    		a.push(6);
    		a.push(5);
    		a.push(4);
    		a.push(3);
    		a.push(2);
    		a.push(1);
    	  //b.push(3);
    		//b.push(2);
    		//b.push(1);
    		c.push(3);
    		c.push(2);
    		c.push(1);
    		System.out.println(a.size());
    		
    		new HanoiGui2();
    		
    		
    	}
    	
    	private static boolean move(Stack<Integer> fromStack2, Stack<Integer> toStack2) {
    		if(!fromStack2.isEmpty() || !fromStack2.isEmpty() && fromStack2.peek()<toStack2.peek()){
    			toStack2.push(fromStack2.pop());
    			System.out.println("moved from: "+from+" to: "+to);
    			from=0; to=0;
    			return true;
            }else{
            	System.out.println("you can not move");
            }
    		from=0; to=0;
    		 return false;//אם לא אפשרי יחזיר לא נכו
    	}
    
    	public HanoiGui2(){
    		this.setTitle("Tower of Hanoi");
    		this.setSize(800,680);
    		this.setVisible(true);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.add(new Draw(),BorderLayout.CENTER);
    		
    		addMouseListener(new MouseAdapter()
    		{
    			public void mousePressed(MouseEvent e)
    			{
    				int x = e.getX();
    				int y = e.getY();
    				System.out.println(x+","+y);
    				mouseMapper(e);
    			}
    
    			private void mouseMapper(MouseEvent e) {
    				if(e.getY()>350 && e.getY()<630){
    					if(e.getX()>200 && e.getX()<230){//poll a
    						if(from==0){from=1;fromStack=a;}else if(to==0){to=1;toStack=a;}
    					}else
    						if(e.getX()>410 && e.getX()<430){//poll b
    							if(from==0){from=2;fromStack=b;}else if(to==0){to=2;toStack=b;}
    						}else
    							if(e.getX()>610 && e.getX()<630){//poll c
    								if(from==0){from=3;fromStack=c;}else if(to==0){to=3;toStack=c;}
    						}
    				}
    				if(from!=0 && to!=0){
    					move(fromStack,toStack);
    					
    				}
    				
    				System.out.println("From: "+from+" To: "+to);
    				
    			}
    		});
    	}
    	
    	
    	public class Draw extends JComponent{
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = 1L;
    
    		public void paint (Graphics g){
    			Graphics2D graph2 = (Graphics2D) g;
    			graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    			
    			//draw polls
    			graph2.setPaint(Color.BLACK);
    			//poll a
    			graph2.draw(new Rectangle2D.Double(xPoll+xMargin, yPoll,
    					widthOfPoll,
    					heightOfPoll));
    			xPoll+=xMargin;
    			//poll b
    			graph2.draw(new Rectangle2D.Double(xPoll+xMargin, yPoll,
    					widthOfPoll,
    					heightOfPoll));
    			xPoll+=xMargin;
    			//poll c
    			graph2.draw(new Rectangle2D.Double(xPoll+xMargin, yPoll,
    					widthOfPoll,
    					heightOfPoll));
    			xPoll+=xMargin;
    			
    			drawTask(graph2);
    
    				this.drawDiscs(graph2);
    				this.drawDiscs2(graph2);
    				this.drawDiscs3(graph2);
    		
    			//new
    			//move disks if there is a new move
    			
    			
    			
    			
    		}
    		
    		public void drawDiscs(Graphics2D graph2){
    			Stack<Integer>temp = new Stack<Integer>();
    			//draw a
    	        while(!a.isEmpty()){
    	            int cur = a.pop();
    	                 
    	            temp.push(cur);
    	        }
    	        while(!temp.isEmpty()){
    	            a.push(temp.pop());
    	            graph2.draw(new Rectangle2D.Double(xDisc,yDisc,widthDisc*a.peek()+10,heightDisc));
    	            xDisc+=7;
    				yDisc-=20;
    				//widthDisc-=10;
    	        }
    	        xDisc = 135;
    	    	yDisc = 580;
    	    	widthDisc = 15;
    		}
    		public void drawDiscs2(Graphics2D graph2){
    			Stack<Integer>temp = new Stack<Integer>();
    			//draw b
    	        while(!b.isEmpty()){
    	            int cur = b.pop();
    	                 
    	            temp.push(cur);
    	        }
    	        while(!temp.isEmpty()){
    	            b.push(temp.pop());
    	            graph2.draw(new Rectangle2D.Double(xDisc2,yDisc2,widthDisc*b.peek()+10,heightDisc));
    	            xDisc2+=7;
    				yDisc2-=20;
    				//widthDisc2-=10;
    	        }
    	        xDisc2 = 335;
    	    	yDisc2 = 580;
    	    	widthDisc2 = 15;
    		}
    		public void drawDiscs3(Graphics2D graph2){
    			Stack<Integer>temp = new Stack<Integer>();
    			//draw c
    	        while(!c.isEmpty()){
    	            int cur = c.pop();
    	                 
    	            temp.push(cur);
    	        }
    	        while(!temp.isEmpty()){
    	            c.push(temp.pop());
    	            graph2.draw(new Rectangle2D.Double(xDisc3,yDisc3,widthDisc3*c.peek()+10,heightDisc));
    	            xDisc3+=7;
    				yDisc3-=20;
    				//widthDisc3-=10;
    	        }
    	        xDisc3 = 535;
    	    	yDisc3 = 580;
    	    	widthDisc3 = 15;
    		}
    		
    		
    		public void drawCharter(Graphics2D graph2) {
    			 Font font = new Font("Serif", Font.PLAIN, 30);
    			 graph2.setFont(font);
    
    			 graph2.drawString("A", 198, 300); 
    			 
    			 Font font1 = new Font("Serif", Font.PLAIN, 30);
    			 graph2.setFont(font);
    
    			 graph2.drawString("B", 402, 300); 
    			 
    			 Font font2 = new Font("Serif", Font.PLAIN, 30);
    			 graph2.setFont(font);
    
    			 graph2.drawString("C", 602, 300); 
    		}
    		
    		public void drawTask(Graphics2D graph2){
    			Font font = new Font("Serif", Font.PLAIN, 40);
    			String str = "Please enter from poll and dest poll:";			
    			graph2.drawString(str, 300, 100);
    			
    		}
    		
    	     
    	     public boolean win(Stack <Integer>s3){
    		        Stack<Integer>chek = new Stack<Integer>();
    		        int i=1;
    		        boolean win = false;
    		        if(s3.isEmpty()) {
    		        	System.out.println("you dont win");
    		        	return false;
    		        }
    		        while(!s3.isEmpty() && s3.peek()==i){
    		         chek.push(s3.pop()); 
    		         i++;
    		       }
    		       if(s3.isEmpty()) win = true;
    
    		       while(!chek.isEmpty()){
    		           s3.push(chek.pop());
    		       }
    		       if(win==true) {
    		    	   System.out.println("you win");
    		       }
    		       return win;
    		    }
    	     
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Tower of Hanoi

    Before trying to analyze any of the code I see several problems:
    The variable names a,b,c should be more descriptive of what they contain.
    The method names: drawDiscs, drawDiscs2 and drawDiscs3 have the same problem. The names should describe what they do.
    Can there be one method that is passed args containing what is to be drawn and where to draw it.
    There are not enough comments in the code describing what the different parts of it is trying to do.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,217
    Rep Power
    12

    Default Re: Tower of Hanoi

    In addition to Norm's comments you use "magic numbers" throughout your program. These are numbers which are not assigned to any variables with meaningful names. This makes it harder for you (and impossible for us) to know/remember what you are doing. For example,
    Java Code:
                while(!temp.isEmpty()){
                    a.push(temp.pop());
                    graph2.draw(new Rectangle2D.Double(xDisc,yDisc,widthDisc*a.peek()+10,heightDisc));
                    xDisc+=7;
                    yDisc-=20;
                    //widthDisc-=10;
                }
    What do the values of 7, 20, and 10 mean? And it's a good practice to assign these to static variables and to have the
    values based off of each other. As an example, consider how one would lay out three squares side by side.

    Java Code:
       static int WIDTH = 100;
       static int HEIGHT = 100;
       static int SEPARATION_DISTANCE = 10;
       static int STARTING_X = 50;
       static int STARTING_Y = 100;
       static int FIRST_SQUARE_X = STARTING_X;
       static int SECOND_SQUARE_X = FIRST_SQUARE_X + WIDTH + SEPARATION_DISTANCE;
       static int THIRD_SQUARE_X = SECOND_SQUARE_X + WIDTH + SEPARATION_DISTANCE;
    If I want to alter the starting X value or their width, all other values are computed appropriately. Plus it helps
    to document your code.

    Regards,
    Jim
    Last edited by jim829; 03-20-2018 at 03:06 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Tower of Hanoi

    Quote Originally Posted by jim829 View Post
    In addition to Norm's comments you use "magic numbers" throughout your program. These are numbers which are not assigned to any variables with meaningful names. This makes it harder for you (and impossible for us) to know/remember what you are doing. For example,
    Java Code:
                while(!temp.isEmpty()){
                    a.push(temp.pop());
                    graph2.draw(new Rectangle2D.Double(xDisc,yDisc,widthDisc*a.peek()+10,heightDisc));
                    xDisc+=7;
                    yDisc-=20;
                    //widthDisc-=10;
                }
    What do the values of 7, 20, and 10 mean? And it's a good practice to assign these to static variables and to have the
    values based off of each other. As an example, consider how one would lay out three squares side by side.

    Java Code:
       static int WIDTH = 100;
       static int HEIGHT = 100;
       static int SEPARATION_DISTANCE = 10;
       static int STARTING_X = 50;
       static int STARTING_Y = 100;
       static int FIRST_SQUARE_X = STARTING_X;
       static int SECOND_SQUARE_X = FIRST_SQUARE_X + WIDTH + SEPARATION_DISTANCE;
       static int THIRD_SQUARE_X = SECOND_SQUARE_X + WIDTH + SEPARATION_DISTANCE;
    If I want to alter the starting X value or their width, all other values are computed appropriately. Plus it helps
    to document your code.

    Regards,
    Jim
    This is true, I guess I should have organized the code before as it seemed like chaos.
    I'm working on it right now and adding the comments.
    I re-did some functions and threw out allot of junk as as of now, the code does perform the move function properly which
    basically takes a disc from one stack to another after selecting with the mouse the origin and destination polls.
    My problem as for now is that the JFrame is not getting cleared whenever a move occurs..
    I was looking for a command to either reset/erase the JFrame before drawing again..
    I will attach the new code after some comments.

    Thanks.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Tower of Hanoi

    The next level of improvement would be to use more classes. One for the Towers and one for the discs.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Tower of Hanoi

    First, I recommend that you don't paint in the frame. In fact, it's considered bad form to extend JFrame. I would extend JPanel, and add the panel to your JFrame instance.

    But for now, your first statement in the paint() method should be super.paint(g). The overridden paint method does other things that need to be executed which it's not
    doing.

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

  7. #7
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Tower of Hanoi

    super.paint(g)!!!! Thank you! :)
    This is what I was looking for.

    Now, the discs are getting redrawn nicely.
    However, it deletes the polls after the first move for some reason..

    Here is the new and much more organized code:

    Java Code:
    package Stacks_Towers_Of_Hanoi;
    
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.geom.Rectangle2D;
    import java.awt.geom.RectangularShape;
    import java.util.Stack;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    
    public class HanoiGui2 extends JFrame implements MouseListener,MouseMotionListener{
    	static Stack <Integer> a = new Stack <Integer>();
    	static Stack <Integer> b = new Stack <Integer>();
    	static Stack <Integer> c = new Stack <Integer>();
    	static Stack <Integer> fromStack = null;
    	static Stack <Integer> toStack = null;	
    	private static final long serialVersionUID = 1L;
    	
    	
    	//sizes & locations
    	//polls
    	int xPoll=0;
    	int yPoll=320;
    	int widthOfPoll=17;
    	int heightOfPoll=280;
    	int xMargin = 200;
    	//discs
    	int xDisc = 134+48;
    	int yDisc = 580;
    	int widthDisc = 15;
    	int xDisc2 = 383;
    	int yDisc2 = 580;
    	int widthDisc2 = 150;
    	int xDisc3 = 583;
    	int yDisc3 = 580;
    	int widthDisc3 = 15;
    	int heightDisc = 20;
    	int addSize = 10;
    	//the from and to polls that were set
    	static int from = 0;
    	static int to = 0;
    
    	
    	
    	public static void main (String args[]){
    		//filling poll a with 3 discs
    		a.push(3);
    		a.push(2);
    		a.push(1);
    	    
    		System.out.println(a.size());
    		
    		//1. set the frame and add the mouse listener
    		new HanoiGui2();
    		//2.
    		
    		
    	}
    	
    	//constructor -----------
    	
    	public HanoiGui2(){
    		this.setTitle("Tower of Hanoi");
    		this.setSize(800,680);
    		this.setVisible(true);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		
    		addMouseListener(this);
    	}
    	
    	//move from one stack to another, choice is made by the mouse
    	
    	private static boolean move(Stack<Integer> fromStack2, Stack<Integer> toStack2) {
    		if(!fromStack2.isEmpty() || !fromStack2.isEmpty() && fromStack2.peek()<toStack2.peek()){
    			toStack2.push(fromStack2.pop());
    			System.out.println("moved from: "+from+" to: "+to);
    			from=0; to=0;
    			return true;
            }else{
            	System.out.println("you can not move");
            }
    		from=0; to=0;
    		 return false;
    	}
    	
    	
    	//win - will check if poll c is properly arranged
    	
    	public boolean win(Stack <Integer>s3){
            Stack<Integer>chek = new Stack<Integer>();
            int i=1;
            boolean win = false;
            if(s3.isEmpty()) {
            	System.out.println("you dont win");
            	return false;
            }
            while(!s3.isEmpty() && s3.peek()==i){
             chek.push(s3.pop()); 
             i++;
           }
           if(s3.isEmpty()) win = true;
    
           while(!chek.isEmpty()){
               s3.push(chek.pop());
           }
           if(win==true) {
        	   System.out.println("you win");
           }
           return win;
        }
    	
    	//mouse actions ------ >>
    		@Override
    		public void mouseDragged(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void mouseMoved(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    		
    		//for selecting the origin and destination poll
    		@Override
    		public void mouseClicked(MouseEvent e) {
    			System.out.println(e.getX()+" x&y "+e.getY());
    			//identifies which poll was pressed
    			if(e.getX()>200 && e.getX()<230){//poll a
    				System.out.println("poll a");
    				if(from==0){from=1;fromStack=a;}else if(to==0){to=1;toStack=a;}
    			}else
    				if(e.getX()>400 && e.getX()<420){//poll b
    					System.out.println("poll b");
    					if(from==0){from=2;fromStack=b;}else if(to==0){to=2;toStack=b;}
    				}else
    					if(e.getX()>600 && e.getX()<620){//poll c
    						System.out.println("poll c");
    						if(from==0){from=3;fromStack=c;}else if(to==0){to=3;toStack=c;}
    					}
    			//commit move
    			//test
    			System.out.println("move from: "+from+" to: "+to+" fromStack: " +fromStack+" toStack: "+toStack);
    			if(fromStack!=null && toStack!=null){
    				move(fromStack,toStack);
    				revalidate();
    				repaint();
    				//reset
    				fromStack=null; toStack=null;
    				
    			}
    			
    		}
    
    		@Override
    		public void mouseEntered(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void mouseExited(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void mousePressed(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void mouseReleased(MouseEvent arg0) {
    			// TODO Auto-generated method stub
    			
    		}
    	
    	//drawing the different components ------------------------------------
    		
    		public void paint (Graphics g){
    			super.paint(g);
    			Graphics2D graph2 = (Graphics2D) g;
    			graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    			
    			//draw polls
    			graph2.setPaint(Color.BLACK);
    			//poll a
    			graph2.draw(new Rectangle2D.Double(xPoll+xMargin, yPoll,
    					widthOfPoll,
    					heightOfPoll));
    			xPoll+=xMargin;
    			//poll b
    			graph2.draw(new Rectangle2D.Double(xPoll+xMargin, yPoll,
    					widthOfPoll,
    					heightOfPoll));
    			xPoll+=xMargin;
    			//poll c
    			graph2.draw(new Rectangle2D.Double(xPoll+xMargin, yPoll,
    					widthOfPoll,
    					heightOfPoll));
    			xPoll+=xMargin;
    			
    			//the question: enter origin & destination poll
    			drawTask(graph2);
    
    			this.drawDiscs1(graph2);
    			this.drawDiscs2(graph2);
    			this.drawDiscs3(graph2);
    		
    		}
    		
    		public void drawDiscs1(Graphics2D graph2){
    			Stack<Integer>temp = new Stack<Integer>();
    			//draw a
    	        while(!a.isEmpty()){
    	            int cur = a.pop();
    	                 
    	            temp.push(cur);
    	        }
    	        while(!temp.isEmpty()){
    	            a.push(temp.pop());
    	            graph2.draw(new Rectangle2D.Double(xDisc,yDisc,widthDisc*a.peek()+10,heightDisc));
    	            xDisc+=7;
    				yDisc-=20;
    				//widthDisc-=10;
    	        }
    	        xDisc = 135;
    	    	yDisc = 580;
    	    	widthDisc = 15;
    		}
    		public void drawDiscs2(Graphics2D graph2){
    			Stack<Integer>temp = new Stack<Integer>();
    			//draw b
    	        while(!b.isEmpty()){
    	            int cur = b.pop();
    	                 
    	            temp.push(cur);
    	        }
    	        while(!temp.isEmpty()){
    	            b.push(temp.pop());
    	            graph2.draw(new Rectangle2D.Double(xDisc2,yDisc2,widthDisc*b.peek()+10,heightDisc));
    	            xDisc2+=7;
    				yDisc2-=20;
    				//widthDisc2-=10;
    	        }
    	        xDisc2 = 335;
    	    	yDisc2 = 580;
    	    	widthDisc2 = 15;
    		}
    		public void drawDiscs3(Graphics2D graph2){
    			Stack<Integer>temp = new Stack<Integer>();
    			//draw c
    	        while(!c.isEmpty()){
    	            int cur = c.pop();
    	                 
    	            temp.push(cur);
    	        }
    	        while(!temp.isEmpty()){
    	            c.push(temp.pop());
    	            graph2.draw(new Rectangle2D.Double(xDisc3,yDisc3,widthDisc3*c.peek()+10,heightDisc));
    	            xDisc3+=7;
    				yDisc3-=20;
    				//widthDisc3-=10;
    	        }
    	        xDisc3 = 535;
    	    	yDisc3 = 580;
    	    	widthDisc3 = 15;
    		}
    		
    		//the letters for the polls
    		public void drawCharter(Graphics2D graph2) {
    			 Font font = new Font("Serif", Font.PLAIN, 30);
    			 graph2.setFont(font);
    			 graph2.drawString("A", 198, 300); 
    			 graph2.drawString("B", 402, 300); 
    			 graph2.drawString("C", 602, 300); 
    		}
    		
    		public void drawTask(Graphics2D graph2){
    			Font font = new Font("Serif", Font.PLAIN, 40);
    			graph2.setFont(font);
    			String str = "Enter from and dest poll:";			
    			graph2.drawString(str, 120, 100);
    			
    		}
    	// ---------------------------------------------------------------
    
    	
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Tower of Hanoi

    That may be a start.
    Still need to
    use better variable names than: a,b,c
    The drawDiscs methods need to be merged into a single method with the right args.
    The magic numbers need to be replaced with constant variables.
    There needs to be classes for the towers and discs
    Last edited by Norm; 03-21-2018 at 10:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Tower of Hanoi

    Yeah these are all good advises that are on my mind, and the positioning is all wrong as well.
    But right now I am really curios about why the polls disappear while only the discs are re-drawn correctly.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,217
    Rep Power
    12

    Default Re: Tower of Hanoi

    A quick look at the paint routine shows you are updating xpoll by xmargin each time paint is called. So that will eventually push the polls off the screen.

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

  11. #11
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Tower of Hanoi

    That's true

    Glad it is something which is easy to fix

    Thanks for the help.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Tower of Hanoi

    With Tower objects the paint method would only have these method calls:
    Java Code:
      drawTask(graph2);
      // Now draw the 3 towers
      tower1.draw(graph2);
      tower2.draw(graph2);
      tower3.draw(graph2);
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Tower of Hanoi

    Yeah I tend to agree towards this approach, on other non gui projects this is what I do normally.
    This is a joint work stepping into the world of GUI so since it is all new I had it all in one file.
    Next time this is what I do.

Similar Threads

  1. Towers of hanoi
    By faraa in forum New To Java
    Replies: 22
    Last Post: 04-01-2013, 10:29 PM
  2. Recursive and Tower of Hanoi!
    By xdrazkalnytex in forum New To Java
    Replies: 4
    Last Post: 04-02-2012, 05:51 PM
  3. Hanoi- HELP
    By xajaxworldx in forum Advanced Java
    Replies: 2
    Last Post: 05-06-2011, 05:22 AM
  4. Towers of Hanoi
    By myst in forum New To Java
    Replies: 1
    Last Post: 07-11-2010, 06:24 PM
  5. Replies: 5
    Last Post: 04-28-2010, 04:42 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
  •