Results 1 to 3 of 3
Thread: Drawing a car
- 10-25-2012, 04:54 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Drawing a car
I have this:
}Java Code:package car; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import javax.swing.JFrame; public class Car { private int xLeft, yTop; /** * 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 void Car(int x, int y) { xLeft = x; yTop = y; } /** * Draws the car * @param g2 the graphics component */ public void draw(Graphics2D g2) { Rectangle body = new Rectangle(xLeft, yTop - 10, 60, 10); Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10, yTop + 10, 10, 10); Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10); // The bottom of the front windsheild Point2D.Double p1 = new Point2D.Double(xLeft + 10, yTop + 10); // The Front of the roof Point2D.Double p2 = new Point2D.Double(xLeft + 20, yTop); Point2D.Double p3 = new Point2D.Double(xLeft + 40, yTop); Point2D.Double p4 = new Point2D.Double(xLeft + 50, yTop + 10); Line2D.Double frontWindshield = new Line2D.Double(p1, p2); Line2D.Double roofTop = new Line2D.Double(p2, p3); Line2D.Double rearWindshield = new Line2D.Double(p3, p4); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield); }
And I have this:
My only problems are on the lines where car1 and car2 are in the paintComponent() method there is an error that says actual and formal argument lists differ in length.Java Code:package car; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; 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); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("Two Cars"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); CarComponent component = new CarComponent(); frame.add(component); } }
- 10-25-2012, 09:03 AM #2
Member
- Join Date
- Aug 2012
- Posts
- 8
- Rep Power
- 0
- 10-25-2012, 09:51 AM #3
Re: Drawing a car
You should avoid constructing objects in a painting method override, as painting methods are called multiple times. Construct the Cars elsewhere and call repaint() when done. If needed, you can set an existing Car object's coordinates before drawing it, or you can create a method draw(Graphics2D, x, y).
Also, your painting method override is lacking a call to the super implementation.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
help in drawing tex
By Rampletero in forum Advanced JavaReplies: 8Last Post: 03-10-2012, 05:17 PM -
need help for drawing...
By nice7 in forum New To JavaReplies: 4Last Post: 12-06-2011, 03:34 PM -
Drawing an arc
By berkeleybross in forum Java 2DReplies: 10Last Post: 12-09-2010, 01:32 AM -
Drawing a map
By Karp in forum AWT / SwingReplies: 4Last Post: 11-07-2008, 12:26 PM -
Help with 2-D Drawing
By Deathmonger in forum New To JavaReplies: 4Last Post: 06-18-2008, 02:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks