I'm trying to create two car objects, that move across the frame, in opposite directions, at different heights so they don't collide. This type of programming is new to me, and after following the only example I'm provided with, I still can't get this to work. All the code I wrote for the 3 individual classes is provided.
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 LevelSix
* @version May 12, 2008
*/
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;
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
/**
* Draws two car objects. *
* @author LevelSix
* @version May 12, 2008
*/
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)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
car1.draw(g2);
car2.draw(g2);
}
public void moveBy(int dx, int dy)
{
car1.translate(dx, dy);
int backward = dx * (-1);
repaint();
car2.translate(backward, dy);
repaint();
}
public Car car1;
public Car car2;
public static final int HUNDRED = 100;
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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(1, 0);
}
}
ActionListener listener = new TimerListener();
final int DELAY = 100; // 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;
}
Help would be greatly appreciated.