What makes the lower car stay on the bottom right corner of the window?
Hey guys, I was writing and trying out this code that I found in a book but I don't understand what part of the code makes the second car stay in the lower right corner of the screen. Could anybody help?
* A car shape that can be positioned anywhere on the screen.
public class Car
private int xLeft;
private int yTop;
* Constructs a car with a given top left corner.
public Car(int x, int y)
xLeft = x;
yTop = y;
* Draws the car.
public void draw(Graphics2D g2)
Rectangle body = new Rectangle(xLeft, yTop + 10, 60,10);
Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10,10);
Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10,10);
// The bottom of the front windshield.
Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10);
// The front of the roof.
Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop);
// The rear of the roof
Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop);
// The bottom of the rear windshield
Point2D.Double r4 = new Point2D.Double(xLeft + 50, yTop + 10);
Line2D.Double frontWindshield = new Line2D.Double(r1,r2);
Line2D.Double roofTop = new Line2D.Double(r2,r3);
Line2D.Double rearWindshield = new Line2D.Double(r3,r4);
* This component draws two car shapes.
public class CarComponent extends JComponent
public void paintComponent(Graphics g)
Graphics2D g2 = (Graphics2D) g;
Car car1 = new Car(0,0);
int x = getWidth() - 60;
int y = getHeight() - 30;
Car car2 = new Car(x,y);
public class CarViewer
public static void main(String args)
JFrame frame = new JFrame();
CarComponent component = new CarComponent();
Thanks a lot.
Re: What makes the lower car stay on the bottom right corner of the window?
Where does the code change the x,y values used to display the second car? If the values are not changed the car won't move.
what part of the code makes the second car stay in the lower right corner of the screen.
Try debugging the code by adding some println statements that print out the values of the car's location as they are changed and used. The printout will show you what the computer sees abd help you understand what is happening.