0

I am having problems with 2 classes, each class has an applet which has a thread, but I want to run both classes in one applet so that at the end have an animation of the race. I need to start both threads in one class.

TortuleThread

public class TortuleThread extends Applet implements Runnable {
    public int y= 0;
    Thread thread;
     public void init(){
            thread = new Thread(this);
            thread.start();
            resize(800,700);
        }

     public void paint(Graphics g){
            g.setColor(Color.green);
            g.fillOval(80, 30+y, 50, 50);
            g.fillOval(88, 70+y, 30, 30);
            g.fillOval(70, 50+y, 20, 20);
            g.fillOval(120, 50+y, 20, 20);
            g.fillOval(85, 25+y, 20, 20);
            g.fillOval(105, 25+y, 20, 20);

            g.setColor(Color.BLACK);
            g.fillOval(92, 80+y, 6, 6);
            g.fillOval(105, 80+y, 6, 6);

            y=y+15;
        }
    public void run()
    {
    int i=0;
    System.out.println("start tortule");

    while(i<5)
    {
        repaint();
        try{
            Thread.sleep(5000);
            System.out.println("Tortule");
        }catch(InterruptedException ex)
        {   
        }
        i++;
    }
    System.out.println("end tortule");
}
}

HareThread

public class HareThread extends Applet implements Runnable {
    public int y = 0;
    Thread thread;

    public void init() {
        thread = new Thread(this);
        thread.start();
        resize(800, 700);
    }

    public void paint(Graphics g) {
        g.setColor(Color.gray);
        g.fillOval(58, 3 + y, 8, 30);
        g.fillOval(68, 3 + y, 8, 30);
        g.fillRect(55, 30 + y, 20, 20);
        g.fillRect(40, 50 + y, 30, 30);
        g.setColor(Color.white);
        g.fillOval(60, 32 + y, 6, 6);
        g.fillOval(68, 32 + y, 6, 6);
        g.setColor(Color.red);
        g.fillOval(64, 40 + y, 6, 6);
        g.setColor(Color.gray);
        g.fillOval(30, 60 + y, 15, 15);

        y=y+15;
    }

    public void run() {
        int i = 0;
        System.out.println("start hare");
        while (i < 5) {
            repaint();
            try {
                Thread.sleep(2000);
                System.out.println("hare");
            } catch (InterruptedException ex) {
            }
            i++;
        }
        System.out.println("end the hare");
    }
}

GraphicRace

public class GraphicRace extends Applet{

    public static void main (String [] args)
    {
    TortuleThread tortule = new TortuleThread();
    Thread hare = new Thread(new HareThread());
    tortule.start();
    hare.start();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Refactor -- Tortoise and Hare should be non-applet, non-GUI classes. Drive them both with one GUI animation loop. Use another class for the Applet, one that displays the state of the Tortoise and Hare objects. And also, if at all possible abandon need to use Applets as it is an abandoned orphan technology. – Hovercraft Full Of Eels Apr 05 '17 at 00:13
  • @HovercraftFullOfEels is probably right. If you want to continue with that design, have you thought about CountDownLatch or Semaphore classes? – Adonis Apr 05 '17 at 08:43
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many .. – Andrew Thompson Apr 05 '17 at 23:02
  • .. good reasons to abandon AWT components in favor of Swing. 4) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 5) `GraphicRace` - an applet would not typically have a `main` method. Or rather, when being run as an applet in a web browser, the `main` method will not be called. – Andrew Thompson Apr 05 '17 at 23:03

0 Answers0