|
-
Sep 20th, 2004, 12:54 PM
#1
Thread Starter
Hyperactive Member
Problem designing simple class
Hello everyone, i'm having troubles deciding how I should design my class heirarchy. Im' supose to do the following:
Implement a superclass Vehicle and subclasses Car and truck. A vehicle has a position on the screen, utilizing the class java.awt.Point. Implement a method draw inthe class RandomDrawing. This draw method can take in different types of vehicles and paint car and truck. In RandomDrawing class, generate four trucks and sick cars with randomly generated postions for drawing. You must atke advantage of inheritance, polymorphism and genericity.
Well I got all the code working and the function draw implemented. This is my class hierarchy.
Code:
(RandomDrawing) <---superclass
^
|
|
(Vehicle)
^
|
|
..................
| |
(Car) (Truck)
RandomDrawing extends JFrame, and this is where all the code is implemented and the objects are painted in this class by using the draw method inside the paint method, such as....
Code:
public void paint(Graphics g, int x, int y)
{
}
public void draw(Vehicle v, int x, int y)
{
v.paint(jPanel1.getGraphics(), r.nextInt(sP.x),r.nextInt(sP.y));
}
//draw vechicles
public void paint(Graphics g)
{
Car c[] = new Car[6];
Truck t[] = new Truck[4];
for(int i = 0; i < 6; i++)
{
c[i] = new Car();
draw(c[i],r.nextInt(sP.x),r.nextInt(sP.y));
}
But, now that I think about it, Why is RandomDrawing the Superclass of Vehcile, a Vehicle isn't a RandomDrawing. So I tried to make RandomDrawing its own seperate class but I was confused on how this should work. If RandomDrawing is its own seperate class, should it still be where all the work is done? Should I still make RandomDrawing extend JFrame? Or should I have a seperate .java file called Main or somthing and let it do all the creation of windows and such...
Thanks for listening
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
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
|
Click Here to Expand Forum to Full Width
|