Results 1 to 4 of 4
  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Post Setting different colors to objects

    Hi All!
    Need to make two cars appear in different colors, here is my (unfinished/wrong) code:
    Car4 class:
    Java Code:
     import java.awt.Color;
    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.
     * @author FOX
     *
     */
    public class Car4 {
    	/**
    	 * Constructs a car with a given top-left corner.
    	 * @param x the x-coordinate of the top-left corner
    	 * @param y the y-coordinate of the top-left corner
    	 */
    	public Car4 (int x, int y)
    	{
    		xLeft = x;
    		yTop = y;
    	}
    	/**
    	 * Draws the car.
    	 * @param g2 the graphics context 
    	 */
    	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.setColor(Color.BLACK);
    		g2.fill(body);
    		g2.fill(frontTire);
    		g2.fill(rearTire);
    		g2.draw(frontWindshield);
    		g2.draw(roofTop);
    		g2.draw(rearWindshield);	
    	}
    	
    	private int xLeft;
    	private int yTop;
    }
    CarComponet4 class:
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
    /**
     * This component draws two car shapes.
     * @author FOX
     *
     */
    
    public class CarComponent4 extends JComponent {
    	public void paintComponent (Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		Car4 car1 = new Car4(0,0);
    		
    		int x = getWidth() - 60;
    		int y = getHeight() - 30;
    		
    		Car4 car2 = new Car4 (x,y);
    		
    		car1.draw(g2);
    		
    		car2.draw(g2);
    		
    	}
    }
    CarViewer4 class:
    Java Code:
    import javax.swing.JFrame;
    
    public class CarViewer4 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		JFrame frame = new JFrame();
    		
    		frame.setSize(300, 400);
    		frame.setTitle("Two cars");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		CarComponent4 component = new CarComponent4();
    		frame.add(component);
    		
    		frame.setVisible(true);
    	}
    
    }
    As you can see if I make a color change in Car4 class it gives us both cars with the same color. I suppose that I have to change CarComponent4 class too. Any suggestions?
    Regards,

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Setting different colors to objects

    You should give Car4 a Color field and allow this field to be set either via a constructor or a setColor(Color color) method. Then use this field in its draw method to set the Graphics object's Color.

    Also, you shouldn't be creating Car4 objects in the paintComponent method. They should be fields of the CarComponent4 class and already be made in the CarComponent4 class's constructor.

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Re: Setting different colors to objects

    so I added:
    Java Code:
    public void setColor (Color color)
    	{
    		
    	}
    now I don't quite understand how I should use it in public void draw method....I already have g2.setColor(Color.BLACK) field here so don't know where it has to be changed :( I am doing a lot of exercising with the same problem(painting different objects with different colors) therefore have to understand it...can you expand your explanation please?
    Regards,

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Setting different colors to objects

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How do i add colors
    By ytrewqc in forum New To Java
    Replies: 1
    Last Post: 11-01-2011, 02:15 PM
  2. Convert 24 bit colors to 16 bit
    By i4ba1 in forum Advanced Java
    Replies: 2
    Last Post: 12-09-2010, 01:23 AM
  3. need help setting up GUI objects
    By robertbob in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 03:03 AM
  4. In Case You Want Colors!
    By angryboy in forum Forum Lobby
    Replies: 0
    Last Post: 08-29-2009, 10:06 PM
  5. Moving objects - setting different speed
    By Antigol in forum Java 2D
    Replies: 2
    Last Post: 08-14-2009, 01:14 AM

Tags for this Thread

Posting Permissions

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