【发布时间】:2019-03-09 05:42:34
【问题描述】:
我正试图让火车穿过我的 java 窗口,但遇到了严重的问题。我有一个火车课程,我在其中制作了火车,还有一个司机课程应该移动火车。我需要让整个火车从右向左移动,直到它“通过”屏幕的左边缘。然后添加一个 if 语句来更改 dx 以便火车在右侧重新启动。以下是我尝试过的,但它不起作用。谁能帮帮我??
public class Driver extends GraphicsProgram
{
//~ Instance/static variables .............................................
private static final int N_STEPS = 1000;
private static final int PAUSE_TIME = 20;
private static final double TRAIN_LENGTH = 320;
//~ Constructor ...........................................................
// ----------------------------------------------------------
/**
* The run() method of the Driver Class.
* Creates an instance of the Train Class.
* Responsible for animating the train across the screen.
*/
public void run()
{
Train train = new Train(getGCanvas());
for (int i = 0; i < N_STEPS; i++) {
train.move(-100, 0);
pause(PAUSE_TIME);
}
【问题讨论】:
-
this 也是您的问题,还是有相同任务的其他人?
-
是别人有同样的任务,这是我唯一一次发布这个问题。你有什么建议吗?我已经工作了几个小时,但似乎无法弄清楚如何在屏幕上移动对象:(
-
我只能想象(正如另一个线程中所建议的那样),您需要调用一些方法来告诉它重绘屏幕。
GraphicsProgram不是标准的 Java 类,所以我不知道那个方法是什么。 -
当您说“它不起作用”时,您需要更具体一些,因为不清楚问题出在哪里。火车根本不渲染吗?它是完全静止的吗?它会在边缘停止吗?它是否过冲或环绕?您是否收到异常消息?
-
为什么在 train.move(-100, 0) i 中使用固定值;在 for 循环中?
标签: java