Zoo tutorials: [ SQL | Linux | XML ]
ProgZoo: [ Java | C# | VB | C++ | Perl ]
Log in

A Gentle Introduction to
Java Programming

Drawing and filling rectangles. Using colour.

 

Graphics

Drawing and filling rectangles. Using colour.

1. [ Java ] Drawing rectangles and using colour


Big

draw
The word draw is often used to indicate that only the outline shows
pen and stroke are associated with drawing
fill
Typically fill is used to indicate that the shape is filled in a solid way
brush and fill are associated with filling

The Graphics class permits us to draw on a surface. Useful methods of this class:

fillRect(200,0,100,400)
Fills the rectangle. The top left corner of the rectangle is at 200,0 the width and height are 100 and 400.
setColor(Color.red)
This method doesn't do anything immediately, but the draw and fill operation will be in red from now on. There are a handful of colours we can use: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow.
setColor(new Color(255,128,128))
This sets the color to an awful pink. The three numbers are the Red, Green and Blue components. The maximum value for each component is 255 - so 255,128,128 means full red, half green and half blue.

We produce three rectangles. One uses drawRect, the others use fillRect.

three rectangles