-2

So, the program is for school and i am having some problems, the basic premise is to have to user choose shape, size and colour, then draw the shape or animate it to move across the screen.

My programs works fine without the thread.sleep (it moves across the screen instantly). When i added the thread.sleep it was not working and waited 10 seconds before immediately moving to the end.

After testing around, it seems to have some correlation with the for loop it was in as when the for loop was to 1000 it waited 10 seconds then moved 1000 spaces instantly but when the for loop was 100 it only took one second to move 100 spaces instantly.

My understanding was that it should just wait 10 milliseconds before going into the for loop again.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class AppletAssignemntIsFucked extends Applet
    implements ActionListener
{
int colourChoice;
int shapeChoice;
int location;
int x, y;
TextField height = new TextField ("00");
TextField width = new TextField ("00");
boolean animating = false;

//declare the two types of checkboxes
CheckboxGroup shape, colour;
//declare all the shape checkboxes
Checkbox buttonSquare, buttonCircle, buttonRectangle, buttonOval;
//declare all the colour checkboxes
Checkbox buttonRed, buttonBlue, buttonGreen, buttonYellow;

Button buttonDraw, buttonAnimate, buttonReset;

public void init ()
{
    setBackground (Color.lightGray);

    shape = new CheckboxGroup ();
    colour = new CheckboxGroup ();

    buttonCircle = new Checkbox ("Circle", shape, true);
    add (buttonCircle);
    buttonSquare = new Checkbox ("Square", shape, false);
    add (buttonSquare);
    buttonRectangle = new Checkbox ("Rectangle", shape, false);
    add (buttonRectangle);
    buttonOval = new Checkbox ("Oval", shape, false);
    add (buttonOval);

    buttonRed = new Checkbox ("Red", colour, true);
    add (buttonRed);
    buttonBlue = new Checkbox ("Blue", colour, false);
    add (buttonBlue);
    buttonGreen = new Checkbox ("Green", colour, false);
    add (buttonGreen);
    buttonYellow = new Checkbox ("Yellow", colour, false);
    add (buttonYellow);

    buttonDraw = new Button ("draw");
    buttonDraw.addActionListener (this);
    add (buttonDraw);
    buttonAnimate = new Button ("animate");
    buttonAnimate.addActionListener (this);
    add (buttonAnimate);
    buttonReset = new Button ("reset");
    buttonReset.addActionListener (this);
    add (buttonReset);

    add (height);
    add (width);
}


public void paint (Graphics g)
{
    switch (colourChoice)
    {
        case 1:
            g.setColor (Color.red);
            break;
        case 2:
            g.setColor (Color.green);
            break;
        case 3:
            g.setColor (Color.yellow);
            break;
        case 4:
            g.setColor (Color.blue);
            break;
    }
    switch (shapeChoice)
    {
        case 1:
            g.fillOval (location, 80, x, x);
            break;
        case 2:
            g.fillRect (location, 80, x, x);
            break;
        case 3:
            g.fillRect (location, 80, x, y);
            break;
        case 4:
            g.fillOval (location, 80, x, y);
            break;
    }

}


public void actionPerformed (ActionEvent evt)
{

    y = Integer.parseInt (height.getText ());
    x = Integer.parseInt (width.getText ());
    //set the colour
    if (evt.getSource () == buttonAnimate)
    {
        for (int i = 0 ; i < 1000 ; i++)
        {
            try
            {
                Thread.sleep (10);         
            }
            catch (InterruptedException ex)
            {
                Thread.currentThread ().interrupt ();
            }
            location += 1;
            repaint ();
        }
    }
    if (evt.getSource () == buttonReset)
    {
        location = 10;
        repaint ();
    }
    if (evt.getSource () == buttonDraw)
    {
        if (buttonRed.getState () == true)
        {
            colourChoice = 1;

        }
        else if (buttonGreen.getState () == true)
        {
            colourChoice = 2;

        }
        else if (buttonYellow.getState () == true)
        {
            colourChoice = 3;

        }
        else if (buttonBlue.getState () == true)
        {
            colourChoice = 4;
        }
        //set the shape
        if (buttonCircle.getState () == true)
        {
            shapeChoice = 1;
        }
        else if (buttonSquare.getState () == true)
        {
            shapeChoice = 2;
        }
        else if (buttonRectangle.getState () == true)
        {
            shapeChoice = 3;
        }
        else if (buttonOval.getState () == true)
        {
            shapeChoice = 4;
        }

        repaint ();
    }


}

}

  • You are blocking the [Event Dispatch Thread](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html), so UI cannot get updated. See [Thread Sleeping Before GUI Updating](http://stackoverflow.com/a/14837743/5221149). – Andreas Apr 23 '16 at 01:10
  • I don't really understand what is going on in that thread. I read it and i understand why it is not working now but i have no idea on how to actually fix it – Ricky Sugden Apr 23 '16 at 01:13

1 Answers1

1

You are blocking the Event Dispatch Thread, so UI cannot get updated.
E.g. see Thread Sleeping Before GUI Updating.

Use a Swing Timer instead.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Can you give me an example of how to implement it? I read the page and looked online but i still don't really understand how it works and how to use it. – Ricky Sugden Apr 23 '16 at 01:26
  • Google "Swing Timers" and ye shall find lots of examples. – Andreas Apr 23 '16 at 01:29
  • 'timer.start (); for (int i = 0 ; i < 1000 ; i++) { timer = new Timer (10, this); location += 1; repaint (); timer.restart (); } timer.stop();' but i get null pointer exception when i try and run it – Ricky Sugden Apr 23 '16 at 01:50
  • You totally missed the point. When you create a timer, you specify a callback method, and the timer will call it. The entire point is that you **don't** do a loop, the timer will keep calling you, until you stop it. – Andreas Apr 23 '16 at 09:42