Assignment #4: Geometric Objects


This assignment is about GUIs, custom drawing, inheritance, abstract classes and being creative. You will have to draw a picture using java.awt.Graphics objects.

Your task:
Write a program, that can draw simple geometric objects, and put some of these objects together to draw a picture like the following one:

As you can see: the picture is composed using three different elements:

You can also see, that the artistic level could easily be increased, and you should do so (see 'bonus point competition').
'java.awt.Graphics' offers much more than i used here, e.g. drawing polygons, arcs etc., have a look at the sun.java.com documentation !

The minimum requirement for this assignment is to draw an image containing at least a circle, a rectangle, a line. Additionally the drawing must be programmed inside of a pre defined software framework i'll give to you.

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. The graphics class contains an array of GraphicsObjects, also a class provided by me.
GraphicsObjects is an abstract class. It contains an abstract method 'paintComponent(Graphics g)', along with some fields. For each different geometric object you must define your own class, inherited from GraphicsObject, therefore implementing 'paintComponent(Graphics g)'. You will therefore at least define 3 classes, Circle, Rectangle, Line.

To draw the image, simply add appropriate instances of your classes to the GraphicsObject-array in the class GraphicsEngine. In order to do so, GraphicsEngine offers a method 'addGraphicsObject(GraphicsObject go)'. You don't have to do anything inside the class GraphicsEngine or GraphicsObject, in fact you are not allowed. I only gave the source code to you to have a look into it. In real life you wouldn't be given the source, but the class file only.


My class 'GraphicsEngine', though eventually being the class that triggers the drawing on the screen, does not know about the elements to be drawn. So how can it be useful ? Here comes the trick, and here comes inheritance: all the geometrical objects you have to draw onto the screen must be inherited from the class GeometricObject, which offers the 'paintComponent' declaration. Hence a class inherited from GraphicsObject has to implement 'paintComponent' and can draw itself. Read it again: it can draw themself. Here's the trick, and the wonderful thing about 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 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 'paintComponent(Graphics g)', being defined in GraphicsObject. In 'paint' you can do whatever you want, but you have to use the 'Graphics' object g, which will be provided by the class 'GraphicsEngine'.


To conclude: You are given 2 classes: You have to write at least 4 classes:


The Bonus Point Competition

You get up to 3(!) points for creativity. Creativity can be shown in the actual picture, but also in the techniques (i.e. the methods of awt.Graphics) used.

I will give an additional bonus point for the picture that i like best (yes, this bonus point is my subjective choice ! :-)


The code:

HERE