だから、プログラムは学校向けで、私はいくつかの問題を抱えています。基本的な前提は、形状、サイズ、色を選択してから、形状を描くか、画面全体でthread.sleepしていないのは
私のプログラムは、thread.sleep(画面上を即座に動く)なしで正常に動作します。私がthread.sleepを追加したとき、それは動作していなかったので、すぐに最後に移動する前に10秒待っていました。
周りのテストの後、forループが1000になってから10秒間待機した後、1000個のスペースを瞬時に移動したときのforループとの相関があるようですが、forループが100だったとき、即座に100のスペースを移動します。
私の理解では、forループに再び入る前に10ミリ秒待つだけでした。
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();
}
}
}
[Event Dispatch Thread](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html)をブロックしているため、UIが更新されません。 「GUIを更新する前のスレッドスリープ」(http://stackoverflow.com/a/14837743/5221149)を参照してください。 – Andreas
私はそのスレッドで何が起こっているのか本当に理解していません。私はそれを読んで、私は今なぜ動作していないのか理解していますが、私は実際にそれを修正する方法が分かりません。 –