I am trying to move an image from right to left. The image I am trying to move is drawTrain method in another class. I think I have to use drawTrain method with move() method. However, I do not know how to do that.. Or I'm totally wrong and there has to be another solution. So far, I tried to use something like
train.drawTrain.move(movex, 0);
train.drawTrain(0, 0).move(movex, 0);
well.. failed.. I tried to search and google and everything, but I'm just sitting and spending several hours. If what I am trying to do is right, how can I move a method in another class? If not, what did I do wrong and how can I fix them?
my driver class
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 ...............................................................
}
Train class.. and I am trying to call a method in this class in Driver class.
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);
}
}