3

I'm doing the exercise 10.1, page 476 from the book Java: How To Program, Early Objects by Paul and Harvey Deitel (10th Edition).

Modify the MyLine, MyOval and MyRectangle classes of GUI to create the class hierarchy as shown in picture below. Classes of the MyShape hierarchy should be “smart” shape classes that know how to draw themselves (if provided with a Graphics object that tells them where to draw). Once the program creates an object from this hierarchy, it can manipulate it polymorphically for the rest of its lifetime as a MyShape.

enter image description here

The original MyLine, MyOval and MyRectangle which we're supposed to modify, all inherit from JPanel and therefore easily paint themselves with the help of paintComponent().

However in the exercise MyLine, MyOval and MyRectangle must inherit from a class other than JPanel (either MyShape or MyBoundedShape).

I'm thinking how to approach this. One solution would be creating another class, say MyPanel which would inherit from JPanel. Then the constructor in MyPanel would receive a MyShape object (MyLine, MyOval or MyRectangle) and the draw it using paintComponent(). In that case MyPanel would also have to know what kind of object is MyShape so I'd have to perform multiple checks with either instanceof or pass a parameter to MyPanel indicating exactly what shape I'm interested in so paintComponent() knows what to draw.

I'm really not sure what is the best practice in such case.

1 Answer 1

3

Using instanceOf will work but condemns you to writing procedural code. If you want to write flexible code consider sticking with polymorphism.

MyPanel would also have to know what kind of object is MyShape

Well no. MyPanel would only have to know that it holds some MyShape object on which it can call paintComponent(). The object being called knows what to do so the calling object doesn't have to.

That's what's cool about polymorphism. Exactly what to do is something else's problem.

Imagine an animal trainer is putting on a show. As each animal walks out she has to turn away from the audience to see which came out. She tells the dog "bark", and it barks. She tells the duck "quack", and it quacks. She tells the cat "meow", and it ignores her, because it's a cat.

If she had trained them with polymorphism in mind she wouldn't have to turn around. The duck would come out, she'd say "speak", and it would quack. The dog would come out, she'd say "speak", and it would bark. The cat would come out, she'd say "speak", and the cat would lick it self.

Done this way the trainer doesn't even need to know which animal she's talking to.

3
  • How can I implement your suggestion? I currently have paintComponent() for each child class. I created MyPanel class whose constructor receives a MyShape object. In MyPanel I also have a paintComponent() method which calls MyShape's paintComponent(). But this doesn't work as my app freezes immediately after appears and nothing is drawn on JFrame, just a blank window.
    – Yos
    Commented Nov 14, 2017 at 7:07
  • 1
    That's a debugging question. Stack Overflow in the place for that. My personal recommendation is to write the simplest thing that could possibly work, test it, and slowly transform it until it's like this. Test each step. Don't do a lot of work without knowing that it still works. Commented Nov 14, 2017 at 13:06
  • I actually figured it out: each shape needed to implement draw() method which draws and fills color and then the paintComponent() from MyPanel will call draw(). As you mentioned because of polymorphism the shape will be correctly drawn based to the draw() method implementation of each subclass.
    – Yos
    Commented Nov 14, 2017 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.