# Thread: Moving Cars across the screen, help!

1. Member
Join Date
Oct 2009
Posts
27
Rep Power
0

## Moving Cars across the screen, help!

Following is the code I have, cars don't move like they should across the screen.
Car Class

Java 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

Java 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

Java 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.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;
}```

2. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
8
Why are you creating new instances of the Car class everywhere in the drawer? It should refer to the same objects, not new ones.

3. Member
Join Date
Oct 2009
Posts
27
Rep Power
0
Still need some help if possible.

4. Member
Join Date
Oct 2009
Posts
27
Rep Power
0
Originally Posted by StormyWaters
Why are you creating new instances of the Car class everywhere in the drawer? It should refer to the same objects, not new ones.
What you mean?

5. Originally Posted by Keno777
What you mean?
He means this:

Java Code:
```public void paintComponent(Graphics g)
{
Car car1 = new Car(HUNDRED, HUNDRED); // <-- this new car
Car car2 = new Car(200, 400); // <-- and this one
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
car1.draw(g2);
car2.draw(g2);

}```
kind regards,

Jos

6. Hi,
Please put comments above the method what you are doing.No one will have time to scan and understand the problem.

why it has been initialized multiple times like this below?

Car car1 = new Car(HUNDRED, HUNDRED);
Car car2 = new Car(200, 400);

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•