Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What makes the lower car stay on the bottom right corner of the window?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

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

    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
     
    /*
     * 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);
     
    		g2.draw(body);
    		g2.draw(frontTire);
    		g2.draw(rearTire);
    		g2.draw(frontWindshield);
    		g2.draw(roofTop);
    		g2.draw(rearWindshield);
    	}
    }

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
     
    /*
     * 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);
     
    		car1.draw(g2);
    		car2.draw(g2);
    	}
    }

    import javax.swing.JFrame;
     
    public class CarViewer 
    {
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame();
     
    		frame.setSize(300,400);
    		frame.setTitle("Two cars");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		CarComponent component = new CarComponent();
    		frame.add(component);
     
    		frame.setVisible(true);
    	}
     
    }

    Thanks a lot.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: What makes the lower car stay on the bottom right corner of the window?

    what part of the code makes the second car stay in the lower right corner of the screen.
    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.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: August 27th, 2012, 09:19 PM
  2. how to ignore upper and lower case
    By mohamed_saleh2012 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 10th, 2012, 02:49 PM
  3. Replies: 1
    Last Post: December 17th, 2011, 02:32 AM
  4. Listing the alphabet in lower and uppercase
    By Nemphiz in forum Object Oriented Programming
    Replies: 2
    Last Post: May 25th, 2010, 05:25 PM
  5. JFrame declared as setAlwaysOnTop doesn't stay on top during slide show
    By ravindra_appikatla in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 09:08 AM