This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Problems with Constructors and Drawing a Car Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems with Constructors and Drawing a Car" Watch "Problems with Constructors and Drawing a Car" New topic

Problems with Constructors and Drawing a Car

Mathew Dodge

Joined: Jun 18, 2012
Posts: 2
Hello All,

I was attempting to write a code which would create a car class. Then display two cars on a screen. The problem is I keep getting a couple errors.
It seems that the car constructor is not being created before the CarComponent class creates the object. But I don't really know what to do..... Thanks!


Greg Charles

Joined: Oct 01, 2001
Posts: 2867

Hi Mathew, welcome to Java Ranch!

First, please use code tags when posting code samples. They make the code much easier to read. I've added them for you above.

Now, what kind of errors are you getting? Compile errors, run time exceptions, wrong behavior? On a quick scan, I see you defined your Car class with a capital C, but you've written the constructor with a lowercase c. The constructor name has to match the class name exactly, even the case. That mismatch will cause a compile error. If it still doesn't compile after you fix that, post the messages the compiler gives you and we'll try to figure it out together.
Darryl Burke

Joined: May 03, 2008
Posts: 4754

In addition to what Greg said, don't construct new Cars in the paintComponent(...) override. Painting methods are for painting, and painting alone. You have absolutely no control over how often or how many times that method will be called.

luck, db
There are no new questions, but there may be new answers.
Mathew Dodge

Joined: Jun 18, 2012
Posts: 2
Thanks to both of you! You're more helpful than my programming professor.... It was the lower case letter in the car constructor which was causing the error. That was something that I just wouldn't have seen! Thank you both again!
I agree. Here's the link:
subject: Problems with Constructors and Drawing a Car