所以,该程序是针对学校的,我遇到了一些问题,基本前提是必须让用户选择形状,大小和颜色,然后绘制形状或使其动画以在屏幕上移动。
我的程序在没有thread.sleep的情况下工作正常(它会立即在屏幕上移动)。当我添加thread.sleep它没有工作并等待10秒后才立即移动到最后。
经过测试后,似乎与for循环有一些相关性,因为当for循环为1000时它等待10秒然后立即移动1000个空格但是当for循环为100时它只需要一秒钟立即移动100个空间。
我的理解是它应该等待10毫秒才能再次进入for循环。
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 ();
}
}
}
答案 0 :(得分:1)
您屏蔽Event Dispatch Thread,因此无法更新用户界面 例如。见Thread Sleeping Before GUI Updating。
改为使用Swing Timer。