Results 1 to 6 of 6
  1. #1
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default 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.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    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. #3
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Still need some help if possible.

  4. #4
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    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. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Keno777 View Post
    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. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    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);
    Ramya:cool:

Similar Threads

  1. moving square
    By blindfolded in forum New To Java
    Replies: 5
    Last Post: 01-22-2010, 06:58 PM
  2. Moving Box
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-30-2009, 01:29 PM
  3. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 06:03 PM
  4. Moving textboxes
    By GabWit in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 05:07 PM
  5. moving a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:52 PM

Posting Permissions

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