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 ();
}
}
}