Designing the Classes
We want to make applets that can have objects of different shape moving across the applet window.
- The first applet will have two rectangles.
- We will represent each rectangle as an instance of a class mRectangle.
- Then the applet will have a thread that loops, and each time around the loop, will draw the rectangles in a new position.
We will design a hierarchy of classes to represent various shapes (including rectangles).
- We define a parent class for movable point objects. Each instance of this class is represented by an x,y location, by a dx,dy offset for the object to move, and a color for the object. This example also illustrates data encapsulation, where users of the class must use methods to get or set data of the class.
- A child class will represent rectangles by adding variables for width and height. It will also override the paint method to draw rectangles.
- Other shapes are easily constructed as children of the rectangle class by overriding the paint method to draw a new shape.