Simple Drawing Part II

You have seen how to draw shapes such as lines and rectangles using the graphics object. These notes take a quick look at using colours and then explain where to find further information.

But first...

Classes? Objects?

You may be noticing that words like "class" and "object" are starting to be used. So what are they? Well, as the course moves on you will find out in detail but for now we can say:

An example of a class is Class Graphics which provides the graphics objects you have been using for drawing (and which have been referenced by a variable called 'g'). Other classes are class String, class Date, class Vector, class Button, class Panel,... In fact, Java comes with more than 3,000 classes.

The services provided by a class are used by calling methods of an object of the class. This is what is happening when you write something like:

g.drawLine(0,0,300,300) ;

The service or method is named drawLine and you are requesting the object g to perform the service.

The great thing about classes is that you can just use them - all you need to know is the class name, the list of methods and a small amount of Java syntax for working with objects. You don't need to know how the methods work, just that they are available. This allows you to start writing some interesting programs early on without having to know how to create classes. We will see how to design and create our own classes in the second half of term.

Colour

When using a graphics object the method setColor is used to set the drawing colour (note the spelling of colour in setColor - this is correct). The following code will draw a red rectangle:

g.setColor(Color.red) ;
g.drawRect(150,150,50,50) ;

SetColor requires a Color to be given as an argument. The default Colors are:

 Color.black  Color.magenta
 Color.blue  Color.orange
 Color.cyan  Color.pink
 Color.darkGray  Color.red
 Color.gray  Color.white
 Color.green  Color.yellow
 Color.lightGray  

Colors can also be specified using Red, Blue and Green (RGB) values, allowing you to pick any colour you want to work with. For example, to create your own colour use:

Color myColor = new Color(10,100,200) ; // Sort of greenish/blue
g.setColor(myColor) ; // Use the color

The three argument values are integers in the range 0-255, representing the mix of red, blue and green values.

More features

To find out more you need to browse the Java on-line documentation. The Java JDK comes complete with a large amount of documentation in the form of web pages which you look at with a web browser. Over time you need to become familiar with this documentation and how to best use it. Now is the time to start! Do not worry if you see things you don't yet understand; as time goes on it will make more sense.

You can find out about using graphics objects by looking for Class Graphics which is part of the package java.awt.

While you're there, also take a look at class Color.

Browse around and look at all the other classes - you will be surprised at all the things they can do.