Results 1 to 6 of 6
- 03-03-2010, 04:07 PM #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.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.
- 03-03-2010, 05:04 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Why are you creating new instances of the Car class everywhere in the drawer? It should refer to the same objects, not new ones.
- 03-04-2010, 02:41 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
Still need some help if possible.
- 03-04-2010, 02:41 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
- 03-04-2010, 02:50 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
He means this:
kind regards,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); }
Jos
- 03-04-2010, 02:57 PM #6
Similar Threads
-
moving square
By blindfolded in forum New To JavaReplies: 5Last Post: 01-22-2010, 05:58 PM -
Moving Box
By anilanar in forum New To JavaReplies: 2Last Post: 08-30-2009, 12:29 PM -
Blank Screen while navigating from one screen to another
By mohana.krishna in forum Java ServletReplies: 0Last Post: 03-03-2009, 05:03 PM -
Moving textboxes
By GabWit in forum New To JavaReplies: 2Last Post: 01-26-2009, 04:07 PM -
moving a file
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 07:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks