3

enter image description here

As you can see from the image, I need to delete the old lines but I'm not sure how. Been stuck on this for a few days. I am pretty sure the problem is I am moving the lines and circles wrong (using setLine, setFrame respectively), since I replaced the car with a rectangle and used translate() and it worked OK. Below is some minimal code to replicate this. Your help is greatly appreciated. (part 1 is the component, part 2 is the car class, and the main program just sets up a Jframe and and the part 1 component).

import java.awt.*;
import javax.swing.JComponent;
import java.util.*;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.Timer;
public class VehicleComponent extends JComponent
{
   //Car class which is an arraylist of line segments and a couple circles
 Car car;
 // The constructor initializes the car to the location 300, 300 on a Jframe
     public VehicleComponent()
  {
     car = new Car(300, 300);
      drive();
  }

//to be painted on a jframe
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
        car.draw(g2);


}

//this function will be called in timer triggered actionListener. will move the car forward by certain pixels per given seconds
public void drive()
{
        car.move();
        repaint();

}
}        

public class Car extends Vehicle
{

ArrayList<Line2D.Double> detailLines;
ArrayList<Ellipse2D.Double> tires;
private int height;
private int length;
private int velocity;


public Car(int xStartingPosition, int yStartingPosition)
{
    super(xStartingPosition, yStartingPosition);
    detailLines = new ArrayList<Line2D.Double>();
    tires = new ArrayList<Ellipse2D.Double>();
    setVelocity();
    height = 5;
    length = 14;


}
/** draws the car */
public void draw(Graphics2D g2){
    BasicStroke tireStroke = new BasicStroke(3);
    BasicStroke vehicleStroke = new BasicStroke(2);
    initializeLines();
    initializeTires();

    g2.setStroke(vehicleStroke);
    for (Line2D.Double e : detailLines){
                g2.draw(e);
    }
    g2.setStroke(tireStroke);
    for (Ellipse2D.Double e: tires){
        g2.draw(e);
    }
}



private void initializeLines()
{
    detailLines.add(new Line2D.Double((getX()-10), (getY()+10), (getX()+50), (getY()+10)));//hood
    detailLines.add(new Line2D.Double((getX() + 60), getY(), (getX()+50), (getY()+10)));//hoodtwo
    detailLines.add(new Line2D.Double((getX()+50), (getY()+10), (getX()+70), (getY()+30)));//Passengerwindshield
    detailLines.add(new Line2D.Double((getX()+70), (getY()+30), (getX() + 80), (getY() + 20)));//hood three
    detailLines.add(new Line2D.Double((getX() + 70), (getY() + 30), (getX()+100), (getY()+30)));//front
    detailLines.add(new Line2D.Double((getX()+100), (getY()+30), (getX()+100), (getY()+40)));//front two
    detailLines.add(new Line2D.Double((getX()+100), (getY()+30), (getX() + 110), (getY() + 20)));//sideLine horizontal
    detailLines.add(new Line2D.Double((getX() + 60), (getY()+20), (getX()-20), (getY() + 20)));//windowsperator
    detailLines.add(new Line2D.Double((getX()+30), (getY()+10), (getX() +30), (getY() +20)));//striketrhough
    detailLines.add(new Line2D.Double((getX()), (getY()), (getX() +60), (getY())));//roof
    detailLines.add(new Line2D.Double((getX()+60), (getY()), (getX() +80), (getY() +20)));//border top
    detailLines.add(new Line2D.Double((getX()+80), (getY()+20), (getX() +110), (getY() +20)));//border 3
    detailLines.add(new Line2D.Double((getX()+110), (getY()+20), (getX() +110), (getY() +30)));//border4
    detailLines.add(new Line2D.Double((getX()+110), (getY()+30), (getX() +100), (getY() +40)));//border5
    detailLines.add(new Line2D.Double((getX()+100), (getY()+40), (getX() - 30), (getY() +40)));//border6
    detailLines.add(new Line2D.Double((getX()-30), (getY()+40), (getX() - 30), (getY() +30)));//border7
    detailLines.add(new Line2D.Double((getX()-30), (getY()+30), (getX()), (getY())));//border8

}
private void initializeTires()
{
    tires.add(new Ellipse2D.Double((getX()-10), getY()+30, 20, 20));
    tires.add(new Ellipse2D.Double((getX()+65), getY()+30, 20, 20));
}

public void move()
{

    for(Line2D.Double e : detailLines){
        e.setLine((e.getX1()+velocity), (e.getY1()), (e.getX2()+velocity), (e.getY2()));

    }
    for(Ellipse2D.Double e : tires)
    {
        e.setFrame((e.getX()+velocity), (e.getY()), e.getWidth(), e.getHeight());
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Abdi Osman
  • 43
  • 1
  • 7

4 Answers4

2

The reason is that you need to clear the drawing surface every time you draw by calling clearRect:

g2.clearRect(0, 0, getWidth(), getHeight() );

Although Line2D objects persist between calls to draw, they don't actually undraw themselves from the buffer - they simple remember their state for the next time you want to draw them.

devrobf
  • 6,973
  • 2
  • 32
  • 46
  • but I don't have a rectangle, i have an array of lines. Could there be a problem with my move() method in the car class? Thats where all problems seem to be pointing. – Abdi Osman Oct 30 '12 at 21:41
  • 1
    I think you're misunderstanding. The `Graphics2D` object provides an interface to a rectangular pixel buffer. Calls you make to `g2` modify pixels on the buffer, usually "drawing" shapes such as lines. When you call `g2.draw(e);` you are telling it to change the pixels along that line to black - however, the `Line2D` class is in *no way* responsible for those pixels. You as the programmer are in control of the pixel array, so every time `draw` is called you must clear the buffer to white to start drawing again. `clearRect` just means set a rectangle (in this case, the whole image) to white. – devrobf Oct 31 '12 at 15:11
  • Thanks alot, I'ms starting to get it. – Abdi Osman Nov 02 '12 at 21:40
2
public void paintComponent(Graphics g)
{
    // Very important, paint the component BG etc.
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    // ...
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Use g2.fillRect() to erase the content of the panel before drawing the next frame.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
1

This is the Common behavior of JComponent. In your case the following should work:

Graphics.clearRect(0,0, getWidth(),getHeight());
Robin
  • 3,512
  • 10
  • 39
  • 73