Connecting Tech Pros Worldwide Help | Site Map

Thread - Hare and Tortoise Program

Newbie
 
Join Date: Mar 2007
Posts: 5
#1: Jul 2 '07
Hello, newbie here...

I'm writing this program but when I click the start button which should initiate either the Hare or the Tortoise, it does not, this is the first time I use threads, so the problem might be the way I'm passing the value.

can someone please take a look at this code and maybe give me some guidance on what I'm doing wrong.

Please note there are 3 separate files with the corresponding classes, I separated it with ===>
Thank You
JC
Expand|Select|Wrap|Line Numbers
  1. package turtoiseandhare;
  2.  
  3. import java.awt.*;
  4. import java.*;
  5. import javax.*;
  6. import javax.swing.*;
  7. import javax.swing.border.*;
  8. import javax.accessibility.*;
  9. import java.util.*;
  10. import java.awt.event.*;
  11.  
  12. public class TurtoiseAndHare extends JFrame
  13. {
  14.     private ImageIcon hare;
  15.     private ImageIcon tortoise;
  16.  
  17.     private ImageIcon start;
  18.     private ImageIcon finish;
  19.     int turtle_x = 0;
  20.     int turtle_y = 159;
  21.     int hare_x = 0;
  22.     int hare_y = 35;
  23.     int ha_w;
  24.     int ha_h;
  25.     int to_w;
  26.     int to_h;
  27.     float fastplod;
  28.     float slip;
  29.     float slowplod;
  30.  
  31.     float sleep;
  32.     float bighop;
  33.     float bigslip;
  34.     float smallhop;
  35.     float smallslip;
  36.  
  37.     Display p;
  38.     Move mo;
  39.     TnHThread tornhare;
  40.     Button StartB;
  41.     Button Stop;
  42.     String message = "Who will win this amazing race!";
  43.  
  44.     public TurtoiseAndHare()
  45.     {
  46.         setTitle ("Tortoise and the Hare race!");
  47.         setSize(1000, 405);
  48.         mo = new Move(this);
  49.  
  50.         Container contentPane = this.getContentPane();
  51.         getContentPane().setLayout(new BorderLayout());
  52.  
  53.         p = new Display();
  54.  
  55.  
  56.         JPanel q = new JPanel();
  57.         q.setBorder(BorderFactory.createLineBorder(Color.BLUE));
  58.         StartB = new Button("  Start  ");
  59.         q.add(StartB);
  60.  
  61.         JPanel m = new JPanel();
  62.         m.setBorder(BorderFactory.createLineBorder(Color.BLUE));
  63.         m.setBackground(Color.getHSBColor(25,55,45));
  64.         JLabel msg = new JLabel(message);
  65.         m.add(msg);
  66.  
  67.         hare = new ImageIcon(getClass().getResource("hare.jpg"));
  68.         tortoise = new ImageIcon(getClass().getResource("Turtle.jpg"));
  69.         start = new ImageIcon(getClass().getResource("start.jpg"));
  70.         finish = new ImageIcon(getClass().getResource("finish.jpg"));
  71.  
  72.         contentPane.add(p, BorderLayout.CENTER);
  73.         contentPane.add(q, BorderLayout.SOUTH);
  74.         contentPane.add(m, BorderLayout.NORTH);
  75.  
  76.         Handler handler = new Handler();
  77.         StartB.addActionListener(handler);
  78.     }
  79.     class Display extends Canvas
  80.     {
  81.         public void paint(Graphics  g)
  82.         {
  83.             ha_w = hare.getIconWidth();
  84.             ha_h = hare.getIconHeight();
  85.             to_w = tortoise.getIconWidth();
  86.             to_h = tortoise.getIconHeight();
  87.  
  88.            // super.paint(g);
  89.            for (int i = 0; i < 780; i++)
  90.            {
  91.                tortoise.paintIcon(p, g, i+5, turtle_y);
  92.  
  93.            };
  94.             tortoise.paintIcon(p, g, turtle_x, turtle_y);
  95.             hare.paintIcon(p, g, hare_x, hare_y);
  96.             start.paintIcon(p, g, 125, 0);
  97.             finish.paintIcon(p, g, 850, 0);
  98.  
  99.             g.drawString(message,170,55);
  100.         }
  101.     } //end of Display
  102.     public void raicing(int animal, int x, int y)
  103.     {
  104.         if (animal == 1)
  105.         {
  106.             hare_x = x;
  107.             hare_y = y;
  108.             message = "the hare is at position " + hare_x;
  109.         }
  110.         if (animal == 2)
  111.         {
  112.             turtle_x = x;
  113.             turtle_y = y;
  114.             message = "The Turtoise is at position " + turtle_x;
  115.         }
  116.         p.repaint();
  117.     }//END of Raicing
  118.     public static void main(String[] args) 
  119.     {
  120.         // TODO Auto-generated method stub
  121.         TurtoiseAndHare myrace = new TurtoiseAndHare();
  122.         myrace.setVisible(true);
  123.   myrace.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  124.     }//END of Main
  125.  
  126.     class Handler implements ActionListener
  127.     {
  128.         public void actionPerformed (ActionEvent e)
  129.         {
  130.             int send;
  131.             Random startNo = new Random();
  132.             send = 1 + startNo.nextInt(2);
  133.             //start button was pressed
  134.             if (send == 1)
  135.             {
  136.                 TnHThread h = new TnHThread(send,mo);
  137.                 h.start();
  138.             }
  139.             else if (send == 2)
  140.             {
  141.                 TnHThread t = new TnHThread(send,mo);
  142.                 t.start();
  143.             }
  144.         }
  145.     }
  146. }
  147.  
  148. ==================================>
  149. MOVE class
  150.  
  151. package Tortoiseandhare;
  152.  
  153. import java.util.Random;
  154.  
  155. public class Move {
  156.  
  157.   private boolean lock = true; //1st
  158.  
  159.   TortoiseAndHare frame;
  160.  
  161.   public Move(TortoiseAndHare fr) {frame = fr;}
  162.  
  163.   public void moving(int animal, int hare_x, int hare_y)
  164.   {
  165.       if (animal == 1)moving_hare(animal, hare_x, hare_y);
  166.       if (animal == 2)moving_Tortoise(animal, hare_x, hare_y);
  167.   }//End of moving
  168.   public void moving_hare(int animal, int hare_x, int hare_y)
  169.   {
  170.       hare_start();
  171.       go(animal, hare_x, hare_y);
  172.       hare_stop();
  173.   }//End of Moving_hare
  174.   public synchronized void hare_start()
  175.   {
  176.       while (lock == false)
  177.       {
  178.           try
  179.           {
  180.               wait();
  181.           }
  182.           catch (InterruptedException e)
  183.           {
  184.               System.out.println("Interrupted Exception on the hare");
  185.           }
  186.       }//End Of while
  187.       lock = false;
  188.       notifyAll();
  189.   }//END of Synch hare_start
  190.  
  191.   public synchronized void hare_stop()
  192.   {
  193.       lock = true; //3rd
  194.       notifyAll();
  195.   }//END of hare_stop
  196.  
  197.   public void moving_Tortoise(int animal, int hare_x, int hare_y)
  198.   {
  199.       Tortoise_start();
  200.       go(animal, hare_x, hare_y);
  201.       Tortoise_stop();
  202.   }//end of Tortoise_start
  203.   public synchronized void Tortoise_start()
  204.   {
  205.       while (lock == false) //7th
  206.       {
  207.           try
  208.           {
  209.               wait();
  210.           }
  211.           catch(InterruptedException e)
  212.           {
  213.               System.out.println("Interrupted Exception on the Tortoise");
  214.           }
  215.       }//END of While
  216.           lock = false; //4th
  217.           notifyAll();
  218.       }   //End of Synch Tortoise_start
  219.  
  220.       public synchronized void Tortoise_stop()
  221.       {
  222.           lock = true; //5th
  223.           notifyAll();
  224.       }
  225.       public void go(int animal, int hare_x, int hare_y)
  226.       {
  227.           if (animal == 1) //if animal == hare
  228.           {
  229.               hare_y = 35;
  230.  
  231.               float RandomNo;
  232.               Random RandNumber = new Random();
  233.               RandomNo = RandNumber.nextFloat(); 
  234.  
  235.               if ( (RandomNo > 0) && (RandomNo <= 0.2) ) //Sleep
  236.               {
  237.                   //No Move at all
  238.                   try{Thread.sleep(1000);}
  239.                   catch(Exception e){System.err.println("Exception at the Sleeping Thread");}
  240.  
  241.                    System.out.println("moving number from Move.go. animal 1  " + animal+ " x = "+hare_x+" y is "+hare_y);
  242.               }
  243.               else if ( (RandomNo > 0.2) && (RandomNo <= 0.4 ) ) //Big Hop
  244.               {
  245.                   System.out.println(" Hare (RandomNo > 0.2) && (RandomNo <= 0.4 ) ");
  246.                   //7 Position to the right
  247.                   hare_x = hare_x + 7;
  248.                   frame.setLocation(hare_x, hare_y);
  249.                   try{Thread.sleep(100);}
  250.                   catch(Exception e){System.err.println("Exception at the Sleeping Thread");}
  251.  
  252.               }
  253.               else if ( (RandomNo > 0.4) && (RandomNo <= 0.5) ) //Big slip
  254.               {
  255.                   //10 Position to the left
  256.                   hare_x = hare_x - 10;
  257.                   frame.setLocation(hare_x, hare_y);
  258.                   try{Thread.sleep(100);}
  259.                   catch(Exception e){System.err.println("Exception at the Sleeping Thread");}
  260.  
  261.               }
  262.               else if ( (RandomNo > 0.5) && (RandomNo <= 0.8) ) //Small hop
  263.               {
  264.                   //1 position to the right
  265.                   hare_x = hare_x + 1;
  266.                   frame.setLocation(hare_x, hare_y);
  267.                   try{Thread.sleep(100);}
  268.                   catch(Exception e){System.err.println("Exception at the Sleeping Thread");}
  269.               }
  270.               else if ( (RandomNo > 0.8) && (RandomNo <= 1.0) ) //small Slip
  271.               {
  272.                   //2 position to the left
  273.                   hare_x = hare_x - 2;
  274.                   frame.setLocation(hare_x, hare_y);
  275.                   try{Thread.sleep(100);}
  276.                   catch(Exception e){System.err.println("Exception at the Sleeping Thread");}
  277.                                 }
  278.               if (hare_x > 770)
  279.               {
  280.                   System.out.println("The Hare Wins the Race");
  281.               }
  282.  
  283.           }
  284.           if (animal == 2) //if animal == Tortoise
  285. {
  286.   hare_y = 159;
  287.   float RandomNo;
  288.   Random RandNumber = new Random();
  289. Math.abs(RandomNo = RandNumber.nextFloat()); 
  290.  
  291.  
  292.   RandomNo = RandNumber.nextFloat();
  293.   if ( (RandomNo > 0) && (RandomNo <= .5) ) //Fast Plod Right
  294.   {
  295.       //2 Position to the Right
  296.       hare_x = hare_x + 2;
  297.       frame.setLocation(hare_x, hare_y);
  298.       try{Thread.sleep(100);}
  299.                   catch(Exception e){System.err.println("Exception at the Sleeping Thread");}
  300.   }
  301.   else if ( (RandomNo > .5) && (RandomNo <= .7) ) //Slip left
  302.   {
  303.       //Slip 4 Position to the left
  304.       hare_x = hare_x - 4;
  305.       frame.setLocation(hare_x, hare_y);
  306.       try{Thread.sleep(100);}
  307.                   catch(Exception e){System.err.println("Exception at the Sleeping Thread");}
  308.   }
  309.   else if( (RandomNo > .7) && (RandomNo <= 1)) //Slow Plod Right
  310.   {
  311.       //1 Position to the right
  312.       hare_x = hare_x + 1;
  313.       frame.setLocation(hare_x, hare_y);
  314.       try{Thread.sleep(100);}
  315.                   catch(Exception e){System.err.println("Exception at the Sleeping Thread");}
  316.   }
  317.   if (hare_x > 770)
  318.   {
  319.    System.out.println("The Tortoise Wins the Race");
  320.    try{Thread.sleep(100);}
  321.                   catch(Exception e){System.err.println("Exception at the Sleeping Thread");}
  322.   }
  323. }
  324.       }//END of go
  325. }//End of Class Move
  326.  
  327.  
  328. ==================>
  329. THREAD CLASS
  330.  
  331. package turtoiseandhare;
  332.  
  333. import java.util.Random;
  334. public class TnHThread extends Thread
  335. {
  336.     public int animal;
  337.     private int x;
  338.     private int y;
  339.  
  340.     Move m;
  341.  
  342.     public TnHThread(int tr, Move m1)
  343.       {
  344.       animal = tr;
  345.  
  346.         m = m1;
  347.         if (animal == 1){x = 0; y = 35;}
  348.         if (animal == 2){x = 0; y = 158;}
  349.     }
  350.  
  351.     public void run()
  352.     {
  353.         m.moving(animal, x, y);
  354.     }
  355. }
  356.  
  357.  
  358. ====================>
  359.  
Reply