该程序具有20面模具并在移动7个点对象之前滚动模具,然后移动点,骰子在水平方向上滚动像素数。当顶点(dot [0])的右边缘触及窗口时,我试图让点竞赛停止。一切正常,但是当顶点的左边缘到达窗口而不是右边缘时,比赛停止。我相信错误是在while条件下,我把/ *放在顶点的右边缘* /我也遇到了使用函数来确定数组中任何Dot的最大x坐标的问题。 (如果最大值超过终点线,则比赛结束。)我放置的函数是:public static int leadingXCoordinate(Dot [] dotList)
public class FlyMe {
private final static int GWINDOW_HEIGHT = 800;
private final static int GWINDOW_WIDTH = 450;
private static JFrame mainFrame;
private static GCanvas dotCourse;
public static void main(String[] args) throws InterruptedException {
createWindows();
Dot [] dots = new Dot [7];
for (int i = 0; i < dots.length; i++){
dots[i] = new Dot(10, 114*i, 70, FlyMe.getRandomColor(), dotCourse);
}
Dice die = new Dice(20);
while (/* the right edge of the top dot */ dots[0].getX() < GWINDOW_WIDTH)
{
for (int i = 0; i < dots.length; i++)
{
int hstep = die.roll();
dots[i].moveDot(dots[i].getX() + hstep, dots[i].getY());
Thread.sleep(50);
}
}
// Creates JFrame window and graphics canvas for animation
public static Color getRandomColor() {
Random r = new Random();
return new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
}
/**
* Determine the x-coordinate of the right-most Dot.
*
* @param dotList
* an array of the balloons being scanned
* @return the maximum x-coordinate of any baloon in the arrays
*/
// public static int leadingXCoordinate( Dot[] dotList ) {
//
// }
private static void createWindows() {
mainFrame = new JFrame("Dot Races");
mainFrame.setSize(GWINDOW_WIDTH, GWINDOW_HEIGHT);
mainFrame.setLocation(0, 0);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dotCourse = new GCanvas(); // drawing space
dotCourse.setPreferredSize(new Dimension(GWINDOW_WIDTH, GWINDOW_HEIGHT));
dotCourse.setLocation(0, 0);
dotCourse.setBackground(Color.white);
mainFrame.add(dotCourse);
mainFrame.pack();
mainFrame.setVisible(true);
}// CreateWindows
}// FlyMe