Java is justifiably famous for its graphics abilities. However, the normal commands in Java to use graphics are a little complicated to use, so we will use the a special graphics package which has been designed to be straightforward to use.
Here is the standard Java documentation for the graphics package.
To use this graphics package, you must use an import statement import graphics.*;. This tells Java that you want to use the classes in the graphics package. (The * means that you can use all the classes in the package.)
Here is an example program:
import graphics.*;
public class DrawSquare
{
public static void main(String [] args)
{
Window win = new Window();
final int SIZE = 60;
Rectangle square = new Rectangle(0, 0, SIZE, SIZE);
win.draw(square);
}
}
|
The first thing you will notice is the import graphics.*; statement. This allows us to use the classes of the element package. One of these classes is Window. A Window, obviously enough, is a window you can draw on. A Rectangle is a Rectangle.
This particular Rectangle is called square and is constructed with four parameters, its x and y coordinates and its width and height.