无法移动图像

时间:2013-10-28 06:41:52

标签: java awt acm-java-libraries

我正在尝试从右向左移动图像。我试图移动的图像是 drawTrain方法在另一个类中。我想我必须使用drawTrain方法和move()方法。 但是,我不知道该怎么做..或者我完全错了,必须有另一个解决方案。到目前为止,我试图使用像

这样的东西
train.drawTrain.move(movex, 0);
train.drawTrain(0, 0).move(movex, 0);
好吧..失败..我试图搜索和谷歌和一切,但我只是坐着花了几个小时。如果我想要做的是对的,我怎么能在另一个类中移动一个方法?如果没有,我做错了什么以及如何解决它们?

我的司机班

public class Driver extends GraphicsProgram
{
//~ Instance/static variables .............................................

//~ 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());
    int movex = -20;

    while(true)
    {
        train.drawTrain(1400, 100);
        train.move(movex, 0);
        pause(60);
    }

}
//~ Methods ...............................................................
}

训练课..我试图在Driver类中调用此类中的方法。

import acm.graphics.*;
import java.awt.*;
import acm.util.*;
import acm.program.*;

// -------------------------------------------------------------------------
/**
 *  This is the Train class with the basic methods already created.
 *  Write the correct code for all of the methods, and replace this comment.
 *  Please refer to the assignment for more instructions.
 * 
 */
public class Train extends GraphicsProgram
{
//~ Instance/static variables .............................................

private GCanvas canvas;

/* Dimensions of the frame of a train car */
private static final double CAR_WIDTH = 75;
private static final double CAR_HEIGHT = 36;

/* Distance from the bottom of a train car to the track below it */
private static final double CAR_BASELINE = 10;

/* Width of the connector, which overlaps between successive cars */
private static final double CONNECTOR = 6;

/* Radius of the wheels on each car */
private static final double WHEEL_RADIUS = 8;

/* Distance from the edge of the frame to the center of the wheel */
private static final double WHEEL_INSET = 16;

/* Dimensions of the cab on the engine */
private static final double CAB_WIDTH = 35;
private static final double CAB_HEIGHT = 8;

/* Dimensions of the smokestack and its distance from the front */
private static final double SMOKESTACK_WIDTH = 8;
private static final double SMOKESTACK_HEIGHT = 8;
private static final double SMOKESTACK_INSET = 8;

/* Dimensions of the door panels on the boxcar */
private static final double DOOR_WIDTH = 18;
private static final double DOOR_HEIGHT = 32;

/* Dimensions of the cupola on the caboose */
private static final double CUPOLA_WIDTH = 35;
private static final double CUPOLA_HEIGHT = 8;

//~ Constructor ...........................................................

// ----------------------------------------------------------
/**
 * Creates a new Train object.
 * @param myCanvas the GCanvas where the train will be drawn.
 */
public Train(GCanvas myCanvas)
{
    canvas = myCanvas;
}

//~ Methods ...............................................................

/**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 */
public void drawTrain(double x, double y)
{
    double trainWidth = 4 * CAR_WIDTH + 6 * CONNECTOR;
    double dx = CAR_WIDTH + CONNECTOR;
    drawEngine(x, y);
    drawBoxCar(x + dx, y, Color.GREEN);
    drawBoxCar(x + dx + dx, y, Color.BLUE);
    drawCaboose(x + 3 * dx, y);


}

//Private Methods

/**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 */
private void drawEngine(double x, double y)
{
    drawCarFrame(x, y, Color.BLACK);
    drawSmokeStack(x, y - CAR_HEIGHT - 11, Color.BLACK);
    drawEngineCab(x + 11, y - CAR_HEIGHT - 11, Color.BLACK);
    drawCowCatcher(x, y - 10);
}

/**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 * @param color explain variable "color"
 */
private void drawBoxCar(double x, double y, Color color)
{
    drawCarFrame(x, y, color);

    double xRight = x + CONNECTOR + CAR_WIDTH / 2;
    double xLeft = xRight - DOOR_WIDTH;
    double yDoor = y - CAR_BASELINE - DOOR_HEIGHT;
    canvas.add(new GRect(xLeft, yDoor, DOOR_WIDTH, DOOR_HEIGHT));
    canvas.add(new GRect(xRight, yDoor, DOOR_WIDTH, DOOR_HEIGHT));


}

/**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 */
private void drawCaboose(double x, double y)
{
    drawCarFrame(x, y, Color.RED);
    drawCupola(x + CONNECTOR, y - CAR_HEIGHT - CAR_BASELINE, Color.RED);
}

/**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 */
private void drawCarFrame(double x, double y, Color color)
{
    double x0 = x + CONNECTOR;
    double y0 = y - CAR_BASELINE;
    double top = y0 - CAR_HEIGHT;
    GLine frame = new GLine(x, y0, x + CAR_WIDTH + 2 * CONNECTOR, y0);
    canvas.add(frame);
    drawWheel(x0 + WHEEL_INSET, y - WHEEL_RADIUS);
    drawWheel(x0 + CAR_WIDTH - WHEEL_INSET, y - WHEEL_RADIUS);
    GRect r = new GRect(x0, top, CAR_WIDTH, CAR_HEIGHT);
    r.setFilled(true);
    r.setFillColor(color);
    canvas.add(r);
}

/**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 */
private void drawWheel(double x, double y)
{
    double r = WHEEL_RADIUS;
    GOval wheel = new GOval(x - r, y - r, 2 * r, 2 * r);
    wheel.setFilled(true);
    wheel.setFillColor(Color.GRAY);
    canvas.add(wheel);
}

/**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 */
private void drawSmokeStack(double x, double y, Color color)
{
    GRect smokeStack = new GRect(x + 15, y - SMOKESTACK_HEIGHT, SMOKESTACK_WIDTH, SMOKESTACK_HEIGHT);
    smokeStack.setFilled(true);
    smokeStack.setFillColor(Color.BLACK);
    canvas.add(smokeStack);
}

/**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 */
private void drawEngineCab(double x, double y, Color color)
{
    GRect cab = new GRect(x + CAB_WIDTH, y - CAB_HEIGHT, CAB_WIDTH, CAB_HEIGHT);
    cab.setFilled(true);
    cab.setFillColor(Color.BLACK);
    canvas.add(cab);
}

/**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 */
private void drawCowCatcher(double x, double y)
{
    GLine cowCatcher1 = new GLine(x + CONNECTOR, y - CAR_HEIGHT / 2, x, y);
    canvas.add(cowCatcher1);
    GLine cowCatcher2 = new GLine(x + CONNECTOR, y - CAR_HEIGHT / 2, x + CONNECTOR / 2, y);
    canvas.add(cowCatcher2);
}

  /**
 * This is a placeholder javadoc comment.
 * @param x explain variable "x"
 * @param y explain variable "y"
 */
private void drawCupola(double x, double y, Color color)
{
    GRect cupola = new GRect(x + 20, y - CUPOLA_HEIGHT, CUPOLA_WIDTH, CUPOLA_HEIGHT);
    cupola.setFilled(true);
    cupola.setFillColor(Color.RED);
    canvas.add(cupola);
}
}

0 个答案:

没有答案