2

I want to change the DefaultDisplay class of the GridWorld GUI so I can change the image of an actor during a run, and since I am using a jar file of the code I am creating a subclass CustomDisplay of DefaultDisplay.

However, since creating a subclass would be changing the name, I would have to create subclasses of the classes that use DefaultDisplay so that they would be updated to use CustomDisplay instead of DefaultDisplay. For example, I would have to make a subclass of DisplayMap named CustomMap that instead of this:

private Display defaultDisplay = new DefaultDisplay();

said this:

private Display customDisplay = new CustomDisplay();

and then any other class that used DisplayMap would have to be changed to use CustomMap. It seems very inefficient to make subclasses of all of these classes just because they use classes that I change.

Is it possible to make it so that CustomDisplay will automatically be used instead of DefaultDisplay so I only have to change the one class?

Notes

  1. The classes I am talking about are not on the normal javadoc, but can be found here.
  2. I checked through Extending GridWorld & found a lot of information, but nothing that would help with this.
Community
  • 1
  • 1
i .
  • 485
  • 2
  • 10
  • 23
  • 1
    The whole idea of inheritance is that your `CustomDisplay` class **IS-A** `DefaultDisplay`, and thus none of the other classes need to know you have subclassed it. Thus, there is no need to create subclasses any other class. – Thorn G Jan 30 '13 at 05:14
  • BTW - See [Extending GridWorld](http://www.horstmann.com/gridworld/extending-gridworld.html). Also, there is no `DefaultDisplay` listed in the [docs](http://www.horstmann.com/gridworld/javadoc/). – Andrew Thompson Jan 30 '13 at 05:30
  • 1
    I checked Extending GridWorld first, I found a lot of information, but nothing that would help me. And the GUI is not listed in the official docs. It can be found [elsewhere](http://www.fangengine.org/images/docs/api/info/gridworld/gui/package-summary.html) – i . Jan 30 '13 at 05:46

1 Answers1

1

This question seems to be stale, but I think a generic answer can help any who find it.

The question is about GridWorld, a simple framework used for teaching.

The OP want to make a specific change, to solve an unstated problem by introducing a new display class, but cannot change existing classes. Hence the question becomes how to introduce the new display class?

Apply OO thinking. Does the framework support the introduction of a new display class? We should check the documentation and if that fails, check the framework's public class interfaces: is there a method anywhere that sets the display? We could examine the grid, the actor, and maybe others used by or that use these.

Despite what the OP says, there is a clear statement (where @Andrew Thompson said) that addresses this requirement:

"The rendering algorithm is as follows:

  1. Check whether there is a class whose name is the name of the class with the suffix Display added, and that implements the info.gridworld.gui. Display interface. If so, its draw method is called. This is the same mechanism that was used in the MBS case study. (If you decide to supply such a class, you should extend the convenience class info.gridworld.gui.AbstractDisplay that automatically handles scaling and rotation.)"

This is what is needed for this problem.

There a couple of "learnings".

  1. If you are reading documentation that seems related (in this case it mentions the Display interface), and you don't understand it, then investigate it until you do understand.

  2. If your idea for a problem solution hits a brick wall, go back to re-evaluate the branches in the path that led you there, before investing too much effort.

  3. Your teacher (in such a low level subject) does not set impossible tasks (unless they have made a huge stuff-up). So 2. applies.

  4. This is related to debugging: our hardest bugs come from assumptions that we didn't know we made. We probably made them out of lazy thinking or ignorance. So revisit the thinking and learn about the techniques and technologies involved. Understand what is going on before you try to change it.

andy256
  • 2,821
  • 2
  • 13
  • 19