Basic HTML version of Foils prepared Sept 21 1998

Foil 15 Designing the Classes

From Java Tutorial 98- 3: Graphics and the Abstract Windowing Toolkit NAVO Tutorial -- Sept 23 1998. by Geoffrey C. Fox, Nancy McCracken

Moving Rectangles showing an applet thread and parent/child classes for Foil 15
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.



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sat Nov 28 1998