我和我的同学正在尝试编写代码,以同时移动所有对象。我们有一个Picture类,内部有变量:car, wheels, cabin, driver
。
当我们使用for循环时,它们一个接一个地移动。我们需要的是使它们同时移动。您能否建议一个可以同时适用于所有人的循环,因为我们现在得到的是执行第一行,而剩下的第一行。完成后,移至下一行。
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Picture
{
private Square wall;
private Square window;
private Triangle roof;
private Circle sun;
private boolean drawn;
private Circle wheels;
private rectangle body;
private Square cabin;
private Person driver;
private Circle wheels2;
/**
* Constructor for objects of class Picture
*/
public Picture()
{
wall = new Square();
window = new Square();
roof = new Triangle();
sun = new Circle();
drawn = false;
wheels = new Circle();
wheels2 = new Circle();
driver = new Person();
cabin = new Square();
body = new rectangle();
}
/**
* Draw this picture.
*/
public void draw()
{
driver.changeSize(80, 60);
driver.moveHorizontal(30);
driver.moveVertical(30);
driver.makeVisible();
body.changeSize(220, 60);
body.moveHorizontal(10);
body.moveVertical(20);
body.makeVisible();
wheels2.changeSize(60);
wheels2.moveHorizontal(130);
wheels2.moveVertical(10);
wheels2.makeVisible();
wheels.changeSize(60);
wheels.moveHorizontal(10);
wheels.moveVertical(10);
wheels.makeVisible();
cabin.changeSize(60);
cabin.moveHorizontal(10);
cabin.moveVertical(20);
cabin.makeVisible();
while(true)
{
driver.slowMoveHorizontal(100);
body.slowMoveHorizontal(100);
wheels.slowMoveHorizontal(100);
wheels2.slowMoveHorizontal(100);
cabin.slowMoveHorizontal(100);
driver.slowMoveHorizontal(-100);
body.slowMoveHorizontal(-100);
wheels.slowMoveHorizontal(-100);
wheels2.slowMoveHorizontal(-100);
cabin.slowMoveHorizontal(-100);
}
}
}
/**
* Change this picture to black/white display
*/
public void setBlackAndWhite()
{
wall.changeColor("black");
window.changeColor("white");
roof.changeColor("black");
sun.changeColor("black");
}*/
/**
* Change this picture to use color display
*/
public void setColor()
{
wall.changeColor("red");
window.changeColor("black");
roof.changeColor("green");
sun.changeColor("yellow");
}
}
答案 0 :(得分:0)
似乎您需要一些并行性。您可以在不同的线程中移动每个组件。但这也不能确保完全并行,但是至少比顺序操作要好。