-
Design flaw, any ideas on how I should set up this class hierarchy?
Hello everyone, I'm implementing a superclasss Vehicle and subclasses Car and Truck. I'm suppose to implement a method draw in the class RandomDrawing. This draw method can take in differne types of vehicles, and i'm painting a car and truck randomly on a screen multiple times. I alrady have everyhting coded and working right but I am rethinking my design. My design is, I have RandomDrawing as a superclass of Vehicle and Car and Truck are subclasses of vehicle. But really I don't see why RandomDrawing should even be part of the class hierarchy. Any ideas on how I can implement this so in my RandomDrawing class which extends jFrame can do the following:
public void paint(Graphics g)
{
Car c = new Car();
draw(c);
Truck t = new Truck();
draw(t);
}
Right now i have it set up as...
public void paint(Graphics g)
{
Car c = new Car();
c1.draw(jPanel1.getGraphics(),r.nextInt(sP.x),r.nextInt(sP.y));
Truck t = new Truck();
.
.
.
}
Thanks,
-Cory
-
Why can't you just make RandomDrawing a seperate class?
RandomDrawing r = new RandomDrawing();
Car c = new Car();
r.draw(c);
I don't really see the confusion. I think you're making things more difficult than you need to.
-
Acutally now I got it to work so now i only have to put, draw(c1)....but now that you bring it up, that does make sense. I should just have RandomDrawing as a single class but when I tried to make the transformation, I get this error:
"RandomDrawing.java": paint(java.awt.Graphics) in car_and_truck.Vehicle cannot be applied to (java.awt.Graphics,int,int) at line 24, column 6
Code:
public void draw(Vehicle v, int x, int y)
{
v.paint(jPanel1.getGraphics(), r.nextInt(sP.x),r.nextInt(sP.y));
}
I got that error lasttime,but i fix it by creating a blank function called
public void paint(Graphics g, int x, int y)
{
//blank
}
But this isn't fixing the problem. Its not liking that I added 2 new params to the paint function.
Note: My Vechile/Car/Truck classes all have the function
public void paint(Graphics g, int x, int y);
Any idea's why this is? Thanks.
-
maybe you cannot override the paint() method? I dont know, I dont really mess with graphics in java a whole lot.
-
I just left it as is, it works good and it seems like more over head to create a Random object each time you want to print a car or truck. This way you can just type in draw(car,...);
Thanks for the help though!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks