Moving Cars across the screen, help!
Following is the code I have, cars don't move like they should across the screen.
Car Class
Code:
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
* Set draw instructions for car objects.
*
* @author
* @version
*/
public class Car
{
/**
* Constructs a car with given top-left corner.
* @param a the x-coordinate of the top-left corner
* @param b the y coordinate of the top-left corner
*/
public Car(int a, int b)
{
aLeft = a;
bTop = b;
}
/**
* Draws the car.
* @param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
Rectangle body
= new Rectangle(aLeft, bTop + 10, 60, 10);
Ellipse2D.Double frontTire
= new Ellipse2D.Double(aLeft + 10, bTop + 20, 10, 10);
Ellipse2D.Double rearTire
= new Ellipse2D.Double(aLeft + 40, bTop + 20, 10, 10);
Point2D.Double r1
= new Point2D.Double(aLeft + 10, bTop +10);
Point2D.Double r2
= new Point2D.Double(aLeft + 20, bTop);
Point2D.Double r3
= new Point2D.Double(aLeft + 40, bTop);
Point2D.Double r4
= new Point2D.Double(aLeft + 50, bTop + 10);
Line2D.Double frontW
= new Line2D.Double(r1, r2);
Line2D.Double roofTop
= new Line2D.Double(r2, r3);
Line2D.Double rearW
= new Line2D.Double(r3, r4);
g2.draw(body);
g2.draw(frontTire);
g2.draw(rearTire);
g2.draw(frontW);
g2.draw(rearW);
g2.draw(roofTop);
}
public void translate(int x, int y)
{
aLeft += x;
bTop += y;
}
private int aLeft;
private int bTop;
}
CarComponent Class
Code:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/**
* Draws two car objects. *
* @author
* @version
*/
public class CarComponent extends JComponent
{
/**
*@param g the graphics context
*/
public CarComponent()
{
Car car1 = new Car(HUNDRED, HUNDRED);
Car car2 = new Car(200, 400);
}
public void paintComponent(Graphics g)
{
Car car1 = new Car(HUNDRED, HUNDRED);
Car car2 = new Car(200, 400);
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
car1.draw(g2);
car2.draw(g2);
}
public void moveBy(int dx, int dy)
{
Car car1 = new Car(HUNDRED, HUNDRED);
Car car2 = new Car(200, 400);
car1.translate(dx, dy);
int backward = dx * (-1);
repaint();
car2.translate(backward, dy);
repaint();
}
public Car car1 = new Car(HUNDRED, HUNDRED);;
public Car car2 = new Car(200, 400);;
public static final int HUNDRED = 100;
}
CarViewer/Mover Class
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JFrame;
import javax.swing.Timer;
public class CarMover
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Two Animated Cars");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final CarComponent component = new CarComponent();
frame.add(component);
frame.setVisible(true);
class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
component.moveBy(10, 5);
}
}
ActionListener listener = new TimerListener();
final int DELAY = 5; // Milliseconds between timer ticks
Timer t = new Timer(DELAY, listener);
t.start();
}
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;
}
Please help me out and tell me what is missing or needs to be tweaked! Thanks.