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.

Recommended Answers

All 8 Replies

You might want to elaborate a little on "can't get this to work". That's a bit vague.

You probably want to look at two things.
1) Consider that these are declaring new car objects, which immediately go out of scope.

public CarComponent()
   {
       Car car1 = new Car(HUNDRED, HUNDRED);
       Car car2 = new Car(200, 400);
   }

2) Consider the placements of the cars in relation to your specified frame size.

Sorry about being vague. Simply put, nothing happens when I run the mover except for me getting an error message. If it will help, heres the error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at timer.CarComponent.paintComponent(CarComponent.java:30)
at javax.swing.JComponent.paint(JComponent.java:1005)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4970)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4916)
at javax.swing.JComponent.paint(JComponent.java:995)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1709)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
at java.awt.Component.dispatchEventImpl(Component.java:4031)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at timer.CarComponent.moveBy(CarComponent.java:36)
at timer.CarMover$1TimerListener.actionPerformed(CarMover.java:28)
at javax.swing.Timer.fireActionPerformed(Timer.java:271)
at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Also: Changing the starting points of the cars had no effect.

The first point I mentioned is causing the error. The second point will become apparent after you fix the first.

I'm not sure what you mean by going out of scope.

You declared 2 car objects at the class level, but you are declaring 2 new ones in the constructor

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()
   {
[B]       // You are declaring new objects here with the same name
       // instead of initializing the ones you already declared
       // They drop out of scope after the constructor is done[/B]
       Car car1 = new Car(HUNDRED, HUNDRED);
       Car car2 = new Car(200, 400);
      
    }
       
   public void paintComponent(Graphics g)
   {
        
      super.paintComponent(g);
       Graphics2D g2 = (Graphics2D) g;
[B]       // so these are still null[/B]
       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();
   } 
   [B]// These remain null because you don't initialize them[/B]
   public Car car1;
   public Car car2;
   public static final int HUNDRED = 100;
}

Hepl plz in need code of this picture

Screenshot_20220604-092034_Adobe_Acrobat.jpg

commented: Make a new discussion. Read the rules and show your code so far. +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.