Case Study: A simple package for drawing shapes

Our goals in this lecture include the following:

The Shapes package

We will start by playing with some software developed specifically for this course, which we will refer to as Shape for the time being (eventually, we will have a package csa120 and will refer to this part of the package as csa120.shape) A copy of these classes has been placed in each student's home directory on patel2.

This package will provide us with some very basic graphical primitives so that we can create drawings as well as animations. A subset of the full package is described as a UML Diagram (though this subset does not encorporate the concept of groups, discussed below). You may wish to become familiar with the formal documentation for the package. This describes all of the available Classes, including the variables and methods associated with each class.

We will highlight three core classes of this package.


Creating and Manipulating Objects in BlueJ

BlueJ offers us an interactive way to create and experiment with objects. For classes in your project, you can instantiate (i.e., create) an object from a class as follows. When you "right-click" the mouse (or what we will call "apple-click" on the Macintosh's), a popup menu appears. At the top of that menu, you will see one or more constructors for the given class. A constructor will always have a method name identical to that of the Class, and it may specify one or more parameters which are expected. (Note: if the class is abstract or an interface, than it can not be instantiated and no such constructors will be listed). If you select a constructor, and specify appropriate parameters if expected, then an object of that class will be created and added to the bottom of the screen (the "workbench") and assigned an identifier specific to that object. For example, if you instantiate an object from class Rectangle, it might be assigned an identifier "rectangl1" by BlueJ.

Once an object is on the workbench, you can call any of its methods through another popup menu. You will find a listing of all methods directly implemented by that class, as well as additional popup menus listing methods which are included due to a superclass of the current class. For example, if you create a Rectangle in our package, you will find methods such as getWidth directly, and additional methods inherited from parent classes, such as the method setFillColor inherited from FillableShape, or the method move inherited from Shape. If you invoke a method which requires one or more parameters, then a dialog box will appear in which you specify the desired parameters. If you wish to pass one of your existing workbench objects as a parameter, you can do so by either typing its identifier, or by clicking on the desired object.


Recording Your Actions, as Converted to Java Code

Though the interactive manipulation of objects, as discussed above, is convenient at first, we will soon need to transition toward writing true Java programs. To begin this transition, we did the following exercise in class. BlueJ actually will allow you to "record" your interactive manipulations, transcribing them to true Java syntax.

Therefore, we decided to work as a class to design a (very simple) car (see image), while recording our actions. To record your own actions, do the following.

We will continue by discussing the details of Java syntax in the next lecture.
Michael Goldwasser
Last modified: Sunday, 22 February 2004