Assignment #3: Geometric Objects
In this assignment the program will produce a graphical output, and YOU will draw the
objects by yourself. There is, of course, a helping class that handles the window and the necessary graphic management
Write a program, that can draw simple geometric objects, and put some of these objects together to draw a picture like the
As you can see: the picture is composed using three different elements:
As you also can see, the artistic level could easily be increased, feel free to do so.
I provide a class called 'GraphicsEngine' (a presumptuous name for such a class, i admit). This class does nothing else than
opening a window on the screen, size 500x500, and providing the actual drawing environment, which is a Java.awt.Graphics class.
You can imagine this Graphics-class as a set of paper and pencil, offering some methods to draw circles, ellipses, rectangles,
My class 'GraphicsEngine', though eventually being the class that triggers the physical drawing on the screen, does not know
upfront about the elements to be drawn. So how can it be useful ? Here comes the trick, and here comes inheritance: an additional
class is defined and provided by me, called 'GraphicsObject'. All the geometrical objects you have to draw onto the screen must be inherited from this
class, i.e. every object you want to display is a 'GraphicsObject'. What does 'GraphicsObject' offer ? Nothing else than the
general properties and abilities all the objects to be drawn have in common: they all have a position, a color, and they can draw themselves.
Read it again: they can draw themselves. Here's the trick, and the wonderful thing of object oriented programming: my class, the one
that shows the graphics, can't draw, since it doesn't know what to draw. But it will know the objects to be drawn, so it will will just
command the objects to draw themselves. All the work is on you, my class just gives the orders. I certainly like that.
'The objects can draw themselves' means: they possess a method, called 'paint(Graphics g)', being defined in GraphicsObject.
In 'paint' you can do whatever you want, but you
have to use the set of paper and pencil given to you in the form of the 'Graphics' object g, which will be provided by the
class 'GraphicsEngine'. Confused ?
How it all goes together:
- You will have to define one class, called 'GraphicControl', that is the main entrance point. 'GraphicControl' must possess an array of
GraphicObjects. This list must then be filled with GraphicObjects, more about that later. To make it easier: define it all static.
- GraphicControl then must start the GraphicsEngine, i.e. it must create an object of type 'GraphicsEngine'. The GraphicControl object
takes the array of GraphicObjects as an argument in the constructor, hence instantiating the object will be something like 'new GraphicsEngine(theList)'
- The GraphicsEngine will open a window and go through the list, calling each element's 'paint(Graphics g)' method. Since all objects in the list
are objects of type 'GraphicsObject', this method exists.
- The surprise: the class 'GraphicsObject' offers the method 'paint', but the method is empty. You have to write your own classes, inherited from
'GraphicsObject', providing an implementation of 'paint' that overrides the one given in 'GraphicsObject'. The method in 'GraphicsObject' will never be
used, it is just defined to tell the GraphicsEngine that such a method's name exists !
- You have to build at least three sub-classes of 'GraphicsObject', Circle, Rectangle, Line. They all must possess an appropriate constructor and the
method 'paint(Graphics g)'.
- You have to know how to use the paper and pencil, i.e. the Graphics g. A good hint: look into the API java.awt.Graphics. You'll find the methods
fillOval (for the circle), 'fillRect' (guess what for) and 'drawLine' (yes, for lines). Additionally you find a method setColor to set the color, which
is an expected behaviour for such a method. A sample implementation of paint to draw a green circle would typically be:
with x,y,diameter being variables you have defined before, e.g. fields of your class Circle.
- Having the classes 'Circle', 'Rectangle', 'Line', your class 'GraphicControl' can fill its list with these objects. Why is this possible ?
The list takes elements of 'GraphicObjects', so it can take every object that is inherited from 'GraphicObject', i.e. your 3 classes ! The objects
themselves are still Circles, Rectangles, Lines, but they come just with their common fields and methods, especially the method paint. The principal
of overriding is utilized here to select the correct drawing method when called by the GraphicsEngine
To conclude: You are given 2 classes:
You have to write 4 classes:
In GraphicsControl, you have to create objects of Circle, Rectangle, Line and pass it to a GraphicsEngine object. That's it.
- Circle, inherited from GraphicsObject
- Rectangle, inherited from GraphicsObject
- Line, inherited from GraphicsObject
Don't worry, you will intensively talk about this assignment in the lab and in Pauline's peer teacher section.
I am pretty sure that the additional information in Pauline's section is necessary to solve this assignment, so attend it!
Class 1: GraphicsEngine.java
Class 2: GraphicsObject.java
You can gain up to 3 bonus points (discretion of TA and me) for creative upgrades. Condition: they have to contain active GUI elements (JButtons, JSliders,...)
AND have to show some creativity (in a previous class somebody created a beach scene, with sail boats moved by a slider, and the sun going down, incl. color
change of the sky!).