Design flaw, any ideas on how I should set up this class hierarchy?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Design flaw, any ideas on how I should set up this class hierarchy?

  1. #1
    Join Date
    Sep 2004
    Posts
    10

    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

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    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.

  3. #3
    Join Date
    Sep 2004
    Posts
    10
    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.

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    maybe you cannot override the paint() method? I dont know, I dont really mess with graphics in java a whole lot.

  5. #5
    Join Date
    Sep 2004
    Posts
    10

    Thumbs up

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center