Sto facendo l'esercizio 10.1, pagina 476 dal libro Java: Come programmare, Early Objects di Paul e Harvey Deitel (10a edizione).
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.
L'originale MyLine , MyOval e MyRectangle che dovremmo modificare, tutti ereditano da JPanel e quindi si colorano facilmente con l'aiuto di paintComponent() .
Tuttavia nell'esercizio MyLine , MyOval e MyRectangle devono ereditare da una classe diversa da JPanel (o MyShape o MyBoundedShape ).
Sto pensando a come affrontarlo. Una soluzione sarebbe la creazione di un'altra classe, ad esempio MyPanel che erediterebbe da JPanel . Quindi il costruttore in MyPanel riceverà un oggetto MyShape ( MyLine , MyOval o MyRectangle ) e lo disegna usando paintComponent() . In questo caso MyPanel dovrebbe anche sapere che tipo di oggetto è MyShape quindi dovrei eseguire più controlli con instanceof o passare un parametro a MyPanel indicando esattamente la forma a cui sono interessato quindi paintComponent() sa cosa disegnare.
Non sono davvero sicuro di quale sia la migliore pratica in questo caso.
