13 Replies - 1721 Views - Last Post: 23 March 2011 - 08:49 PM Rate Topic: -----

#1 teckno101  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 02-January 11

Array Update in Paint Component Towers Of Hanoi

Posted 18 March 2011 - 11:25 AM

When you hit the play button you can see the game playing in the array but it doesn't update in the graphics panel. Why!?!?!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
//Max Matthews
public class Hanoi extends JFrame
{
	JButton play = new JButton("Play");
	JButton pause = new JButton("Pause");
	JButton step = new JButton("Step");
	JButton pole1 = new JButton("         " +
			"                          1                " +
			"                ");
	JButton pole2 = new JButton("2");
	JButton pole3 = new JButton("           " +
			"                        3   " +
			"                                ");
	JSlider slider = new JSlider(1,10,2);
	int numOfDisks;
	int[][] game;
	int totalGoal =0;
	int topDisk=0;
	boolean moving=false;
	static Color[] colors = {Color.black, Color.blue,
		Color.PINK, Color.green, Color.magenta, Color.YELLOW, Color.RED, Color.cyan};
	
	public static void main(String args[])
	{
		Hanoi window = new Hanoi();
		window.setVisible(true); //makes the window visible
		window.setResizable(false);
		
		Random rgen = new Random();
		for (int i=0; i<colors.length; i++) {
		    int randomPosition = rgen.nextInt(colors.length);
		    Color temp = colors[i];
		    colors[i] = colors[randomPosition];
		    colors[randomPosition] = temp;
		}
	}
	
	public Hanoi()
	{
		setTitle("Towers of Hanoi"); //sets the title of window to Bank Teller
    	setSize(800,500); //Sets the size of the window
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Makes it so the "X" button works
    	setLocation(0,0); //makes it appear at a certain place on the screen
    	
    	Container contentPane = getContentPane(); //new ContentPane
    	contentPane.setLayout(new BorderLayout());
    	
    	MyDrawPanel drawPanel = new MyDrawPanel();//new DrawPanel
    	contentPane.add(drawPanel, BorderLayout.CENTER);
    	    	
        Container buttonPane = new Container();
    	buttonPane.setLayout(new FlowLayout());
    	contentPane.add(buttonPane, BorderLayout.NORTH);
    	buttonPane.add(play);
    	buttonPane.add(pause);
    	buttonPane.add(step);
    	buttonPane.add(slider);
    	
    	
        Container buttonPaneTowers = new Container();
    	buttonPaneTowers.setLayout(new BorderLayout());
    	contentPane.add(buttonPaneTowers, BorderLayout.SOUTH);
    	buttonPaneTowers.add(pole1, BorderLayout.WEST);
    	buttonPaneTowers.add(pole2, BorderLayout.CENTER);
    	buttonPaneTowers.add(pole3, BorderLayout.EAST);
    	
    	pause.setEnabled(false);
    	
    	String ansString = JOptionPane.showInputDialog
    		(null, "Number of Disks (1-8)");
    	
    	try
    	{
        	numOfDisks = Integer.parseInt(ansString);
    	}
    	catch (NumberFormatException E)
    	{
    		JOptionPane.showMessageDialog
    			(new JFrame(), "Invalid Number. Now exiting.");
    		System.exit(5);
    	}
    	
    	if (numOfDisks<1 || numOfDisks>8)
    	{
    		JOptionPane.showMessageDialog
				(new JFrame(), "Invalid Number. Now exiting.");
    		System.exit(5);
    	}
    	
    	game = new int[numOfDisks][3];
    	for (int row=0;row<game.length;row++)
    		for (int col=0;col<game[0].length;col++)
    			game[row][col]=0;
    	
    	int numToInsert=8;
    	for (int i=numOfDisks; i>0; i--)
    	{
    		game[i-1][0]=numToInsert;
    		numToInsert--;
    	}
    	    	
    	Button1 b1Listener = new Button1();
    	Button2 b2Listener = new Button2();
    	Button3 b3Listener = new Button3();
		pole1.addActionListener(b1Listener);
		pole2.addActionListener(b2Listener);
		pole3.addActionListener(b3Listener);
    	Play playListener = new Play();
    	Pause pauseListener = new Pause();
    	Step stepListener = new Step();
		play.addActionListener(playListener);
		pause.addActionListener(pauseListener);
		step.addActionListener(stepListener);
		
    	for (int row=0;row<game.length;row++)
    	{
    		totalGoal+=game[row][0];
    	}
    	
	}
	
	private class MyDrawPanel extends JPanel
    {
    	public void paintComponent (Graphics g)
    	{    	
    		g.drawString("Disk Moving: " + topDisk, 330,25);
    		g.setColor(Color.GRAY);
    		g.fillRect(50,335,700,50);
    		
    		g.setColor(Color.GRAY);
    		g.fillRect(150, 67, 17, 270);
    		g.fillRect(375, 67, 17, 270);
    		g.fillRect(620, 67, 17, 270);
    		    		
    		g.setColor(Color.BLUE);
    		for (int row=game.length-1;row>=0;row--)
        	{
        		g.setColor(Color.blue);
        		if (game[row][0]==8){g.setColor(colors[7]);
        			g.fillRoundRect(67, 100+(29*(row+(8-numOfDisks))), 180, 25, 10, 10);}
        		if (game[row][0]==7){g.setColor(colors[6]);
        			g.fillRoundRect(77,100+(29*(row+(8-numOfDisks))),160,25, 10, 10);}
        		if (game[row][0]==6){g.setColor(colors[5]);
        			g.fillRoundRect(87,100+(29*(row+(8-numOfDisks))),140,25, 10, 10);}
        		if (game[row][0]==5){g.setColor(colors[4]);
        			g.fillRoundRect(97,100+(29*(row+(8-numOfDisks))),120,25, 10, 10);}
        		if (game[row][0]==4){g.setColor(colors[3]);
        			g.fillRoundRect(107,100+(29*(row+(8-numOfDisks))),100,25, 10, 10);}
        		if (game[row][0]==3){g.setColor(colors[2]);
        			g.fillRoundRect(117,100+(29*(row+(8-numOfDisks))),80,25, 10, 10);}
        		if (game[row][0]==2){g.setColor(colors[1]);
        			g.fillRoundRect(127,100+(29*(row+(8-numOfDisks))),60,25, 10, 10);}
        		if (game[row][0]==1){g.setColor(colors[0]);
        			g.fillRoundRect(137,100+(29*(row+(8-numOfDisks))),40,25, 10, 10);}
        	}
    		for (int row=0;row<game.length;row++)
        	{
        		g.setColor(Color.blue);
        		if (game[row][1]==8){g.setColor(colors[7]);
        			g.fillRoundRect(293,100+(29*(row+(8-numOfDisks))),180,25, 10, 10);}
        		if (game[row][1]==7){g.setColor(colors[6]);
        			g.fillRoundRect(303,100+(29*(row+(8-numOfDisks))),160,25, 10, 10);}
        		if (game[row][1]==6){g.setColor(colors[5]);
        			g.fillRoundRect(313,100+(29*(row+(8-numOfDisks))),140,25, 10, 10);}
        		if (game[row][1]==5){g.setColor(colors[4]);
        			g.fillRoundRect(323,100+(29*(row+(8-numOfDisks))),120,25, 10, 10);}
        		if (game[row][1]==4){g.setColor(colors[3]);
        			g.fillRoundRect(333,100+(29*(row+(8-numOfDisks))),100,25, 10, 10);}
        		if (game[row][1]==3){g.setColor(colors[2]);
        			g.fillRoundRect(343,100+(29*(row+(8-numOfDisks))),80,25, 10, 10);}
        		if (game[row][1]==2){g.setColor(colors[1]);
        			g.fillRoundRect(353,100+(29*(row+(8-numOfDisks))),60,25, 10, 10);}
        		if (game[row][1]==1){g.setColor(colors[0]);
        			g.fillRoundRect(363,100+(29*(row+(8-numOfDisks))),40,25, 10, 10);}
        	}   
      		for (int row=0;row<game.length;row++)
        	{
        		g.setColor(Color.blue);
        		if (game[row][2]==8){g.setColor(colors[7]);
        			g.fillRoundRect(67+470,100+(29*(row+(8-numOfDisks))),180,25, 10, 10);}
        		if (game[row][2]==7){g.setColor(colors[6]);
        			g.fillRoundRect(77+471,100+(29*(row+(8-numOfDisks))),160,25, 10, 10);}
        		if (game[row][2]==6){g.setColor(colors[5]);
        			g.fillRoundRect(87+471,100+(29*(row+(8-numOfDisks))),140,25, 10, 10);}
        		if (game[row][2]==5){g.setColor(colors[4]);
        			g.fillRoundRect(97+471,100+(29*(row+(8-numOfDisks))),120,25, 10, 10);}
        		if (game[row][2]==4){g.setColor(colors[3]);
        			g.fillRoundRect(107+471,100+(29*(row+(8-numOfDisks))),100,25, 10, 10);}
        		if (game[row][2]==3){g.setColor(colors[2]);
        			g.fillRoundRect(117+471,100+(29*(row+(8-numOfDisks))),80,25, 10, 10);}
        		if (game[row][2]==2){g.setColor(colors[1]);
        			g.fillRoundRect(127+471,100+(29*(row+(8-numOfDisks))),60,25, 10, 10);}
        		if (game[row][2]==1){g.setColor(colors[0]);
        			g.fillRoundRect(137+471,100+(29*(row+(8-numOfDisks))),40,25, 10, 10);}
        	}
      		
      		System.out.println("Repainting");
    			
    	}
    }
	
	private class Button1 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			repaint();

			if (moving)
			{
				int lowestLocation=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][0]==0)
					{
						lowestLocation=i;
					}
				}
				
				game[lowestLocation][0]=topDisk;
				topDisk=0;
				lowestLocation=0;
				moving=false;
				
				pole1.setEnabled(true);
				pole2.setEnabled(true);
				pole3.setEnabled(true);
				checkIfWon();
			}
			else
			{
				for (int i=0; i<game.length; i++)
				{
					if (game[i][0]!=0)
					{
						topDisk=game[i][0];
						game[i][0]=0;
						
						moving=true;
												     
						i=game.length+1;
					}
				}
				
				//find tower two top disk
				int locationTopDisk2=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][1]!=0)
					{
						locationTopDisk2=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk2==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk2][1])
					pole2.setEnabled(false);
				
				//find tower three top disk
				int locationTopDisk3=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][2]!=0)
					{
						locationTopDisk3=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk3==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk3][2])
					pole3.setEnabled(false);
			}
			printArray();
		}
	}
	
	private class Button2 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			repaint();
			
			if (moving)
			{
				int lowestLocation=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][1]==0)
					{
						lowestLocation=i;
					}
				}
				
				game[lowestLocation][1]=topDisk;
				topDisk=0;
				lowestLocation=0;
				moving=false;
				
				pole1.setEnabled(true);
				pole2.setEnabled(true);
				pole3.setEnabled(true);
				checkIfWon();
			}
			else
			{
				for (int i=0; i<game.length; i++)
				{
					if (game[i][1]!=0)
					{
						topDisk=game[i][1];
						game[i][1]=0;
						
						moving=true;
						     
						i=game.length+1; //makes the loop exit
					}
				}
				
				//find tower one top disk
				int locationTopDisk2=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][0]!=0)
					{
						locationTopDisk2=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk2==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk2][0])
					pole1.setEnabled(false);
				
				//find tower three top disk
				int locationTopDisk3=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][2]!=0)
					{
						locationTopDisk3=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk3==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk3][2])
					pole3.setEnabled(false);
			}
			printArray();
		}
	}
	
	private class Button3 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
				repaint();
				
				if (moving)
				{
					int lowestLocation=0;
					
					for (int i=0; i<game.length; i++)
					{
						if (game[i][2]==0)
						{
							lowestLocation=i;
						}
					}
					
					game[lowestLocation][2]=topDisk;
					topDisk=0;
					lowestLocation=0;
					moving=false;
					
					pole1.setEnabled(true);
					pole2.setEnabled(true);
					pole3.setEnabled(true);
					
					checkIfWon();
				}
				else
				{
					for (int i=0; i<game.length; i++)
					{
						if (game[i][2]!=0)
						{
							topDisk=game[i][2];
							game[i][2]=0;
							
							moving=true;
							     
							i=game.length+1;
						}
					}	
					
					//find tower two top disk
					int locationTopDisk2=0;
					for (int i=0; i<game.length; i++)
					{
						if (game[i][1]!=0)
						{
							locationTopDisk2=i;
							i=game.length+1;
						}
					}
					
					if (locationTopDisk2==0)
					{
						
					}
					else if(topDisk>game[locationTopDisk2][1])
						pole2.setEnabled(false);
					
					//find tower three top disk
					int locationTopDisk3=0;
					for (int i=0; i<game.length; i++)
					{
						if (game[i][0]!=0)
						{
							locationTopDisk3=i;
							i=game.length+1;
						}
					}
					
					if (locationTopDisk3==0)
					{
						
					}
					else if(topDisk>game[locationTopDisk3][0])
						pole1.setEnabled(false);
				}
				printArray();
				
		}
	}
	
		
	int temp;
	public void moveDisk(int startTower, int endTower)
	{	
		//System.out.println(startTower+"\t"+endTower);
		printArray();
		manySec(.5);
		repaint();
		//find the disk you are moving
		int topDisk=0;
		for (int i=0; i<game.length; i++)
		{
			if (game[i][startTower]==0)
			{
				repaint();
				topDisk=i;
			}
			else
			{
				topDisk=i;
				i=10; //makes the loop quit
				repaint();
			}	
		}
		
		//find where you are moving to on the tower
		int movingLocation=numOfDisks-1;
		for (int i=0; i<game.length; i++)
		{
			if (game[i][endTower]!=0)
			{
				movingLocation=i-1;
				i=game.length+1;
			}
			repaint();
		}
		
		temp=game[topDisk][startTower];
		game[topDisk][startTower]=0;
		game[movingLocation][endTower]=temp;
		repaint();
	}
	
	public void moveTower(int numDisks, int start, int end, int temp)
	{
		repaint();
		if (numDisks==1)
		{
			moveDisk(start,end);
			repaint();
		}
		else
		{
			moveTower(numDisks-1, start, temp, end);
			moveDisk(start, end);
			moveTower(numDisks-1, temp, end, start);
			
			repaint();
		}
		repaint();
	}
	
	 public static void manySec(double a)
	 {
		 int s = (int) Math.round(a*1000);
	     try
	     {
	       Thread.currentThread().sleep(s);
	     }
	     catch (InterruptedException e)
	     {
	       e.printStackTrace();
	     }
	 }

	private class Play implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			moveTower(8,0,2,1);
			repaint();
		}
	}
	
	private class Pause implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	private class Step implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	private void printArray()
	{
    	for (int row=0;row<game.length;row++)
    	{
    		for (int col=0;col<game[0].length;col++)
    		{
    			System.out.print(game[row][col]);
    		}
    		System.out.println();
    	}
    	System.out.println("\n");
	}
	
	private void checkIfWon()
	{
		int totalOfColumn=0;
    	for (int row=0;row<game.length;row++)
    	{
    		totalOfColumn+=game[row][2];
    	}
    	
    	if (totalGoal==totalOfColumn)
    	{
    		JOptionPane.showMessageDialog
			(new JFrame(), "You won! Now exiting.");
    		System.exit(0);
    	}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Array Update in Paint Component Towers Of Hanoi

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,673
  • Joined: 27-December 08

Re: Array Update in Paint Component Towers Of Hanoi

Posted 18 March 2011 - 12:09 PM

Always invoke the super.paintComponent() method when you override paintComponent(). It invokes key super class functionality for this method, resulting in a fresh re-draw on the screen.
Was This Post Helpful? 1
  • +
  • -

#3 teckno101  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 02-January 11

Re: Array Update in Paint Component Towers Of Hanoi

Posted 20 March 2011 - 09:01 AM

Sorry, I'm a new programmer. I'm familiar with super but where do I call it?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,673
  • Joined: 27-December 08

Re: Array Update in Paint Component Towers Of Hanoi

Posted 20 March 2011 - 09:03 AM

First thing in your paintComponent() method.
public void paintComponent(Graphics g){
    super.paintComponent(g);
    //now handle custom painting
}


Was This Post Helpful? 0
  • +
  • -

#5 teckno101  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 02-January 11

Re: Array Update in Paint Component Towers Of Hanoi

Posted 20 March 2011 - 09:07 AM

Okay, I think I figured out where to put it in

private class MyDrawPanel extends JPanel
    {
    	public void paintComponent (Graphics g)
    	{    	
    		super.paintComponent(g);
    		g.drawString("Disk Moving: " + topDisk, 330,25);
    		g.setColor(Color.GRAY);
    		g.fillRect(50,335,700,50);
    		
    		g.setColor(Color.GRAY);
    		g.fillRect(150, 67, 17, 270);
    		g.fillRect(375, 67, 17, 270);
    		g.fillRect(620, 67, 17, 270);
}}


but I'm still having problems. When you hit the play button you should see the disks moving in the GUI, instead the only move in the array.

Thanks for all your help
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Update in Paint Component Towers Of Hanoi

Posted 20 March 2011 - 12:11 PM

Your repaint(); statements in the 3 Button classes ask for a repaint of the button not the whole panel

You will have to pass the JPanel or the JFrame as parameter to your Button so that the actionPerformed() can do a repaint on it

class Button1 implements ActionListener
   JFrame myFrame;

   Button1(JFrame father) {
      myFrame = father;
   }

   public void actionPerformed(...
     myFrame.repaint();


Was This Post Helpful? 0
  • +
  • -

#7 teckno101  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 02-January 11

Re: Array Update in Paint Component Towers Of Hanoi

Posted 20 March 2011 - 12:56 PM

Same problem, the buttons work and update the GUI but not the play button
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
//Max Matthews
public class Hanoi extends JFrame
{
	JButton play = new JButton("Play");
	MyDrawPanel drawPanel;
	JButton pause = new JButton("Pause");
	JButton step = new JButton("Step");
	JButton pole1 = new JButton("         " +
			"                          1                " +
			"                ");
	JButton pole2 = new JButton("2");
	JButton pole3 = new JButton("           " +
			"                        3   " +
			"                                ");
	JSlider slider = new JSlider(1,10,2);
	int numOfDisks;
	int[][] game;
	int totalGoal =0;
	int topDisk=0;
	boolean moving=false;
	static Color[] colors = {Color.black, Color.blue,
		Color.PINK, Color.green, Color.magenta, Color.YELLOW, Color.RED, Color.cyan};
	
	public static void main(String args[])
	{
		Hanoi window = new Hanoi();
		window.setVisible(true); //makes the window visible
		window.setResizable(false);
		
		Random rgen = new Random();
		for (int i=0; i<colors.length; i++) {
		    int randomPosition = rgen.nextInt(colors.length);
		    Color temp = colors[i];
		    colors[i] = colors[randomPosition];
		    colors[randomPosition] = temp;
		}
	}
	
	public Hanoi()
	{
		setTitle("Towers of Hanoi"); //sets the title of window to Bank Teller
    	setSize(800,500); //Sets the size of the window
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Makes it so the "X" button works
    	setLocation(0,0); //makes it appear at a certain place on the screen
    	
    	Container contentPane = getContentPane(); //new ContentPane
    	contentPane.setLayout(new BorderLayout());
    	
    	drawPanel = new MyDrawPanel();//new DrawPanel
    	contentPane.add(drawPanel, BorderLayout.CENTER);
    	    	
        Container buttonPane = new Container();
    	buttonPane.setLayout(new FlowLayout());
    	contentPane.add(buttonPane, BorderLayout.NORTH);
    	buttonPane.add(play);
    	buttonPane.add(pause);
    	buttonPane.add(step);
    	buttonPane.add(slider);
    	
    	
        Container buttonPaneTowers = new Container();
    	buttonPaneTowers.setLayout(new BorderLayout());
    	contentPane.add(buttonPaneTowers, BorderLayout.SOUTH);
    	buttonPaneTowers.add(pole1, BorderLayout.WEST);
    	buttonPaneTowers.add(pole2, BorderLayout.CENTER);
    	buttonPaneTowers.add(pole3, BorderLayout.EAST);
    	
    	pause.setEnabled(false);
    	
    	String ansString = JOptionPane.showInputDialog
    		(null, "Number of Disks (1-8)");
    	
    	try
    	{
        	numOfDisks = Integer.parseInt(ansString);
    	}
    	catch (NumberFormatException E)
    	{
    		JOptionPane.showMessageDialog
    			(new JFrame(), "Invalid Number. Now exiting.");
    		System.exit(5);
    	}
    	
    	if (numOfDisks<1 || numOfDisks>8)
    	{
    		JOptionPane.showMessageDialog
				(new JFrame(), "Invalid Number. Now exiting.");
    		System.exit(5);
    	}
    	
    	game = new int[numOfDisks][3];
    	for (int row=0;row<game.length;row++)
    		for (int col=0;col<game[0].length;col++)
    			game[row][col]=0;
    	
    	int numToInsert=8;
    	for (int i=numOfDisks; i>0; i--)
    	{
    		game[i-1][0]=numToInsert;
    		numToInsert--;
    	}
    	    	
    	Button1 b1Listener = new Button1();
    	Button2 b2Listener = new Button2();
    	Button3 b3Listener = new Button3();
		pole1.addActionListener(b1Listener);
		pole2.addActionListener(b2Listener);
		pole3.addActionListener(b3Listener);
    	Play playListener = new Play();
    	Pause pauseListener = new Pause();
    	Step stepListener = new Step();
		play.addActionListener(playListener);
		pause.addActionListener(pauseListener);
		step.addActionListener(stepListener);
		
    	for (int row=0;row<game.length;row++)
    	{
    		totalGoal+=game[row][0];
    	}
    	
	}
	
	private class MyDrawPanel extends JPanel
    {
    	public void paintComponent (Graphics g)
    	{    	
    		super.paintComponent(g);
    		g.drawString("Disk Moving: " + topDisk, 330,25);
    		g.setColor(Color.GRAY);
    		g.fillRect(50,335,700,50);
    		
    		g.setColor(Color.GRAY);
    		g.fillRect(150, 67, 17, 270);
    		g.fillRect(375, 67, 17, 270);
    		g.fillRect(620, 67, 17, 270);
    		    		
    		g.setColor(Color.BLUE);
    		for (int row=game.length-1;row>=0;row--)
        	{
        		g.setColor(Color.blue);
        		if (game[row][0]==8){g.setColor(colors[7]);
        			g.fillRoundRect(67, 100+(29*(row+(8-numOfDisks))), 180, 25, 10, 10);}
        		if (game[row][0]==7){g.setColor(colors[6]);
        			g.fillRoundRect(77,100+(29*(row+(8-numOfDisks))),160,25, 10, 10);}
        		if (game[row][0]==6){g.setColor(colors[5]);
        			g.fillRoundRect(87,100+(29*(row+(8-numOfDisks))),140,25, 10, 10);}
        		if (game[row][0]==5){g.setColor(colors[4]);
        			g.fillRoundRect(97,100+(29*(row+(8-numOfDisks))),120,25, 10, 10);}
        		if (game[row][0]==4){g.setColor(colors[3]);
        			g.fillRoundRect(107,100+(29*(row+(8-numOfDisks))),100,25, 10, 10);}
        		if (game[row][0]==3){g.setColor(colors[2]);
        			g.fillRoundRect(117,100+(29*(row+(8-numOfDisks))),80,25, 10, 10);}
        		if (game[row][0]==2){g.setColor(colors[1]);
        			g.fillRoundRect(127,100+(29*(row+(8-numOfDisks))),60,25, 10, 10);}
        		if (game[row][0]==1){g.setColor(colors[0]);
        			g.fillRoundRect(137,100+(29*(row+(8-numOfDisks))),40,25, 10, 10);}
        	}
    		for (int row=0;row<game.length;row++)
        	{
        		g.setColor(Color.blue);
        		if (game[row][1]==8){g.setColor(colors[7]);
        			g.fillRoundRect(293,100+(29*(row+(8-numOfDisks))),180,25, 10, 10);}
        		if (game[row][1]==7){g.setColor(colors[6]);
        			g.fillRoundRect(303,100+(29*(row+(8-numOfDisks))),160,25, 10, 10);}
        		if (game[row][1]==6){g.setColor(colors[5]);
        			g.fillRoundRect(313,100+(29*(row+(8-numOfDisks))),140,25, 10, 10);}
        		if (game[row][1]==5){g.setColor(colors[4]);
        			g.fillRoundRect(323,100+(29*(row+(8-numOfDisks))),120,25, 10, 10);}
        		if (game[row][1]==4){g.setColor(colors[3]);
        			g.fillRoundRect(333,100+(29*(row+(8-numOfDisks))),100,25, 10, 10);}
        		if (game[row][1]==3){g.setColor(colors[2]);
        			g.fillRoundRect(343,100+(29*(row+(8-numOfDisks))),80,25, 10, 10);}
        		if (game[row][1]==2){g.setColor(colors[1]);
        			g.fillRoundRect(353,100+(29*(row+(8-numOfDisks))),60,25, 10, 10);}
        		if (game[row][1]==1){g.setColor(colors[0]);
        			g.fillRoundRect(363,100+(29*(row+(8-numOfDisks))),40,25, 10, 10);}
        	}   
      		for (int row=0;row<game.length;row++)
        	{
        		g.setColor(Color.blue);
        		if (game[row][2]==8){g.setColor(colors[7]);
        			g.fillRoundRect(67+470,100+(29*(row+(8-numOfDisks))),180,25, 10, 10);}
        		if (game[row][2]==7){g.setColor(colors[6]);
        			g.fillRoundRect(77+471,100+(29*(row+(8-numOfDisks))),160,25, 10, 10);}
        		if (game[row][2]==6){g.setColor(colors[5]);
        			g.fillRoundRect(87+471,100+(29*(row+(8-numOfDisks))),140,25, 10, 10);}
        		if (game[row][2]==5){g.setColor(colors[4]);
        			g.fillRoundRect(97+471,100+(29*(row+(8-numOfDisks))),120,25, 10, 10);}
        		if (game[row][2]==4){g.setColor(colors[3]);
        			g.fillRoundRect(107+471,100+(29*(row+(8-numOfDisks))),100,25, 10, 10);}
        		if (game[row][2]==3){g.setColor(colors[2]);
        			g.fillRoundRect(117+471,100+(29*(row+(8-numOfDisks))),80,25, 10, 10);}
        		if (game[row][2]==2){g.setColor(colors[1]);
        			g.fillRoundRect(127+471,100+(29*(row+(8-numOfDisks))),60,25, 10, 10);}
        		if (game[row][2]==1){g.setColor(colors[0]);
        			g.fillRoundRect(137+471,100+(29*(row+(8-numOfDisks))),40,25, 10, 10);}
        	}
      		
      		System.out.println("Repainting");
    			
    	}
    }
	
	private class Button1 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			repaint();

			if (moving)
			{
				int lowestLocation=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][0]==0)
					{
						lowestLocation=i;
					}
				}
				
				game[lowestLocation][0]=topDisk;
				topDisk=0;
				lowestLocation=0;
				moving=false;
				
				pole1.setEnabled(true);
				pole2.setEnabled(true);
				pole3.setEnabled(true);
				checkIfWon();
			}
			else
			{
				for (int i=0; i<game.length; i++)
				{
					if (game[i][0]!=0)
					{
						topDisk=game[i][0];
						game[i][0]=0;
						
						moving=true;
												     
						i=game.length+1;
					}
				}
				
				//find tower two top disk
				int locationTopDisk2=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][1]!=0)
					{
						locationTopDisk2=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk2==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk2][1])
					pole2.setEnabled(false);
				
				//find tower three top disk
				int locationTopDisk3=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][2]!=0)
					{
						locationTopDisk3=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk3==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk3][2])
					pole3.setEnabled(false);
			}
			printArray();
		}
	}
	
	private class Button2 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			repaint();
			
			if (moving)
			{
				int lowestLocation=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][1]==0)
					{
						lowestLocation=i;
					}
				}
				
				game[lowestLocation][1]=topDisk;
				topDisk=0;
				lowestLocation=0;
				moving=false;
				
				pole1.setEnabled(true);
				pole2.setEnabled(true);
				pole3.setEnabled(true);
				checkIfWon();
			}
			else
			{
				for (int i=0; i<game.length; i++)
				{
					if (game[i][1]!=0)
					{
						topDisk=game[i][1];
						game[i][1]=0;
						
						moving=true;
						     
						i=game.length+1; //makes the loop exit
					}
				}
				
				//find tower one top disk
				int locationTopDisk2=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][0]!=0)
					{
						locationTopDisk2=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk2==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk2][0])
					pole1.setEnabled(false);
				
				//find tower three top disk
				int locationTopDisk3=0;
				for (int i=0; i<game.length; i++)
				{
					if (game[i][2]!=0)
					{
						locationTopDisk3=i;
						i=game.length+1;
					}
				}
				
				if (locationTopDisk3==0)
				{
					
				}
				else if(topDisk>game[locationTopDisk3][2])
					pole3.setEnabled(false);
			}
			printArray();
		}
	}
	
	private class Button3 implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
				repaint();
				
				if (moving)
				{
					int lowestLocation=0;
					
					for (int i=0; i<game.length; i++)
					{
						if (game[i][2]==0)
						{
							lowestLocation=i;
						}
					}
					
					game[lowestLocation][2]=topDisk;
					topDisk=0;
					lowestLocation=0;
					moving=false;
					
					pole1.setEnabled(true);
					pole2.setEnabled(true);
					pole3.setEnabled(true);
					
					checkIfWon();
				}
				else
				{
					for (int i=0; i<game.length; i++)
					{
						if (game[i][2]!=0)
						{
							topDisk=game[i][2];
							game[i][2]=0;
							
							moving=true;
							     
							i=game.length+1;
						}
					}	
					
					//find tower two top disk
					int locationTopDisk2=0;
					for (int i=0; i<game.length; i++)
					{
						if (game[i][1]!=0)
						{
							locationTopDisk2=i;
							i=game.length+1;
						}
					}
					
					if (locationTopDisk2==0)
					{
						
					}
					else if(topDisk>game[locationTopDisk2][1])
						pole2.setEnabled(false);
					
					//find tower three top disk
					int locationTopDisk3=0;
					for (int i=0; i<game.length; i++)
					{
						if (game[i][0]!=0)
						{
							locationTopDisk3=i;
							i=game.length+1;
						}
					}
					
					if (locationTopDisk3==0)
					{
						
					}
					else if(topDisk>game[locationTopDisk3][0])
						pole1.setEnabled(false);
				}
				printArray();
				
		}
	}
	
		
	int temp;
	public void moveDisk(int startTower, int endTower)
	{	
		//System.out.println(startTower+"\t"+endTower);
		printArray();
		manySec(.5);
		drawPanel.repaint();
		//find the disk you are moving
		int topDisk=0;
		for (int i=0; i<game.length; i++)
		{
			if (game[i][startTower]==0)
			{
				drawPanel.repaint();
				topDisk=i;
			}
			else
			{
				topDisk=i;
				i=10; //makes the loop quit
				drawPanel.repaint();
			}	
		}
		
		//find where you are moving to on the tower
		int movingLocation=numOfDisks-1;
		for (int i=0; i<game.length; i++)
		{
			if (game[i][endTower]!=0)
			{
				movingLocation=i-1;
				i=game.length+1;
			}
			drawPanel.repaint();
		}
		
		temp=game[topDisk][startTower];
		game[topDisk][startTower]=0;
		game[movingLocation][endTower]=temp;
		drawPanel.repaint();
	}
	
	public void moveTower(int numDisks, int start, int end, int temp)
	{
		repaint();
		if (numDisks==1)
		{
			moveDisk(start,end);
			drawPanel.repaint();
		}
		else
		{
			moveTower(numDisks-1, start, temp, end);
			moveDisk(start, end);
			moveTower(numDisks-1, temp, end, start);
			
			drawPanel.repaint();
		}
		drawPanel.repaint();
	}
	
	 public static void manySec(double a)
	 {
		 int s = (int) Math.round(a*1000);
	     try
	     {
	       Thread.currentThread().sleep(s);
	     }
	     catch (InterruptedException e)
	     {
	       e.printStackTrace();
	     }
	 }

	private class Play implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			moveTower(8,0,2,1);
			drawPanel.repaint();
		}
	}
	
	private class Pause implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	private class Step implements ActionListener
	{		
		public void actionPerformed(ActionEvent e)
		{
			
		}
	}
	
	private void printArray()
	{
    	for (int row=0;row<game.length;row++)
    	{
    		for (int col=0;col<game[0].length;col++)
    		{
    			System.out.print(game[row][col]);
    		}
    		System.out.println();
    	}
    	System.out.println("\n");
	}
	
	private void checkIfWon()
	{
		int totalOfColumn=0;
    	for (int row=0;row<game.length;row++)
    	{
    		totalOfColumn+=game[row][2];
    	}
    	
    	if (totalGoal==totalOfColumn)
    	{
    		JOptionPane.showMessageDialog
			(new JFrame(), "You won! Now exiting.");
    		System.exit(0);
    	}
	}
}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Update in Paint Component Towers Of Hanoi

Posted 20 March 2011 - 01:29 PM

You will neee thread...

When you invoke repaint() it sends a message to the GUI, when you will have the time, invoke paint() with the good Graphics object.

That will be done AFTER the thread that fire ActionEvent is done which basically mean when the actionPerformed() method will be executed.

But your actionPerformed() calls moveTower() that calls moveTower()... until the moveTower() method finishes and return to the actionPerformed() that will finally ends no repaint() will be processed.

Not 100% accurate, actually it is more complicated than that, but the concept is there.
Was This Post Helpful? 0
  • +
  • -

#9 teckno101  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 02-January 11

Re: Array Update in Paint Component Towers Of Hanoi

Posted 20 March 2011 - 01:47 PM

Okay, I think I follow: repaint won't be called until the actionPerformed() is done. So how do I get it to do what I want it to?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Update in Paint Component Towers Of Hanoi

Posted 20 March 2011 - 02:05 PM

View Postteckno101, on 20 March 2011 - 03:47 PM, said:

Okay, I think I follow: repaint won't be called until the actionPerformed() is done. So how do I get it to do what I want it to?

Swing is not thread safe but it won't explode but you might have some synchonization problems
You will have to start your moveTower() process in another Thread

private class XYZ extends Thread  {

   public void run() {
       moveDisk(...) here
   }
}


In you actionPerformed
XYZ xyz = new XYZ();
xyz.start();

But I would redesign the whole thing
Separate your GUI elements from the logic

In the GUI a javax.swing.Timer that effectively calls repaint()
Was This Post Helpful? 1
  • +
  • -

#11 teckno101  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 02-January 11

Re: Array Update in Paint Component Towers Of Hanoi

Posted 20 March 2011 - 02:45 PM

Okay, I'll work on this. Thanks for all your help, I'll keep you updated.
Was This Post Helpful? 0
  • +
  • -

#12 teckno101  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 02-January 11

Re: Array Update in Paint Component Towers Of Hanoi

Posted 23 March 2011 - 07:54 PM

Thanks for all your help! I got it working.

I'm not sure of the etiquette here at DIC, should I delete this post or the code to prevent other people from copying it? I personally don't care if anyone takes it but I know you have a strict no homework policy.
Was This Post Helpful? 1
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10440
  • View blog
  • Posts: 38,673
  • Joined: 27-December 08

Re: Array Update in Paint Component Towers Of Hanoi

Posted 23 March 2011 - 08:39 PM

Nope. You're good leaving it. That way, others in the future may benefit from the advice offered in this thread. :)
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array Update in Paint Component Towers Of Hanoi

Posted 23 March 2011 - 08:49 PM

View Postteckno101, on 23 March 2011 - 09:54 PM, said:

Thanks for all your help! I got it working.

I'm not sure of the etiquette here at DIC, should I delete this post or the code to prevent other people from copying it? I personally don't care if anyone takes it but I know you have a strict no homework policy.

"No homework policiy" means we won't do your homework (from A to Z)
But we will be glad to help you with it if you have problem with it and show your efforts (the code YOU wrote)
Actually, posting your final solution can be helpful to others and may be let me or macosxnerd101 critize it :)

Happy coding :^:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1