Java graphics build on top of the java.awt.*
packages (AWT - Another Windows Toolkit). More sophisticated and
elaborate graphics came with version Java 1.2 (the javax.swing.*
packages that we discuss in the main track) but they essentially
extend the AWT.
In this section we give a brief introduction to purely AWT graphics
techniques. This provides a background for the javax.swing
graphics but also, since many browsers still provide only a Java
1.1 level JVM, you might want to program some of your applets with
these techniques.
Components
The basic building block of Java graphics is the abstract Component
class. (The javax.swing
components are also sub-classes of Component).
Concrete subclasses of Component
include :.
- Container
provides for holding other sub-components.
- Panel
is a container sub-class that can provide a drawing surface but
generally is intended for holding other components such as buttons
and labels.
- LayoutManager
subclasses arrange the components, such as placing a group of
buttons in a n x m grid arrangement.
- Canvas is a component usually placed in a panel and used for
drawing graphics.
The java.applet.Applet
class is a sub-class of Panel
as shown by this inheritance chart:
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.Container
|
+--java.awt.Panel
|
+--java.applet.Applet
Like all Component
subclasses, the applet overrrides the paint(Graphics
g) method and it is usually where drawing commands are put.
For example, :
|
import
java.applet.Applet;
import java.awt.Graphics;
/** Example demonstrating drawPolyline().
**/
public class SimpleDrawApplet extends
Applet
{
public void paint
(Graphics g) {
g.drawRect (13,13,24,24);
g.drawOval (15,15,35,35);
}
} // class SimpleDrawApplet |
To display a component, the JVM graphics engine
will invoke the paint
method (or indirectly via an invokation of the update
method, which we will discuss later). If the component is a Container
sub-class, such as panel,
then it in turn will call the paint
methods of all the components that it holds. Note that all the
components have paint()
commands but typcially, you only override paint(Graphics)
for Panel
and Canvas
classes.
Graphics Context
The
paint(Graphics g)method includes an instance of the Graphics
class as an argument . The graphics context represents a drawing
surface and all of its graphics settings such as the current foreground
and background colors, the fonts to be used for strings, etc. graphical
user interface (GUI). The class is referred to as the graphics context
class since it provides the context under which all graphics commands
operate.
The graphics context does not need to represent a
visible component but can be an off-screen image as we will see
later in the double buffering discussion.
Graphics Commands
Here are some of the drawing methods in the Graphics
class that
- drawLine(int
x1, int y1, int x2, int y2)
Draw a line between points (x1,y1) and (x2,y2)
- drawArc(int
x,int y,int width, int height,
int startAngle, int
arcAngle)
- setColor(Color
c)
Set the current pen color, where c has several standard choices
such as
Color.black, Color.blue, Color.red, etc.
- fillRect(int
x, int y, int width, int height)
Fills the specified rectangle with the current color.
See the API
Specifications for other methods in the Graphics
class.
Note that the the coordinate system goes as follows:
- Origin (0,0)- top left hand corner
- x (in pixels) - increases towards the right
- y (in pixels) - increases towards the bottom
where width and height values come from the getSize()
method, which returns an instance of the Dimension class, and getWidth()
and getHeight()
methods, which became available as of version 1.2.
So, for example in the instruction
drawRect(x,y,width,height)
(x,y) are
the coordinates of the top left corner and the bottom right corner
will be at (x+width,y+height).
Similarly,
drawOval(x,y,width,height)
draws an oval bounded by the rectangle specified by these
arguments.
Latest update: Oct. 27, 2004
|