Java Drawing
Java Drawing
Java Drawing
Usage
The Graphics clearRect method can be used to clear a rectangular area.
Class
java.awt.Graphics
Arguments
Height
The vertical size of the rectangle in pixels.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.
Usage
The Graphics clipRect method can be used to restrict drawing to a
rectangular area.
Class
java.awt.Graphics
Prototype
void clipRect (int X, int Y, int Width, int Height)
Arguments
Height
The vertical size of the rectangle in pixels.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.
Applet - Clip1
Drawing Arcs
Usage:
Vertical location of the upper left corner of the rectangle, in pixels. The
Graphics drawArc method can be used to draw arcs which are portions of
ellipses, and the Graphics fillArc method can be used to draw pie shapes which
are portions of filled ellipses.
Class:
Graphics
Example Methods:
drawArc (int X, int Y, int Width, int Height, int Theta, int
Delta)
fillArc (int X, int Y, int Width, int Height, int Theta, int
Delta)
Arguments:
Delta
The size of the arc, in rectangular degrees.
Height
The vertical size of the oval, in pixels.
Theta
The beginning angle of the arc, in rectangular degrees.
Width
The horizontal size of the oval, in pixels.
X
Horizontal location of the upper left corner of the bounding rectangle, in
pixels.
Y
Vertical location of the upper left corner of the bounding rectangle, in
pixels.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
}
Drawing Images
Usage:
An image (.jpg, .jpeg, or .gif) can be displayed using the drawImage method
after being loaded with the getImage method of the Applet.
Class:
Graphics
Example Methods:
drawImage (Image Img, int X, int Y, ImageObserver Observer)
drawImage (Image Img, int X, int Y, Color BgColor,
ImageObserver Observer)
drawImage (Image Img, int X, int Y, int Width, int Height,
ImageObserver Observer)
drawImage (Image Img, int X, int Y, int Width, int Height,
Color BgColor, ImageObserver Observer)
drawImage (Image img, int Dx1, int Dy1, int Dx2, int Dy2,
int Sx1, int Sy1, int Sx2, int sy2, ImageObserver Observer)
drawImage (Image img, int Dx1, int Dy1, int Dx2, int Dy2,
int Sx1, int Sy1, int Sx2, int sy2, Color BgColor,
ImageObserver Observer)
Arguments:
BgColor
Background color - used to fill transparent regions of the image.
Dx1
Horizontal location of the first edge of the destination rectangle, in pixels.
Dx2
Horizontal location of the second edge of the destination rectangle, in
pixels.
Dy1
Vertical location of the first edge of the destination rectangle, in pixels.
Dy2
Vertical location of the second edge of the destination rectangle, in pixels.
Height
The vertical size of the bounding rectangle in pixels.
Sx1
Horizontal location of the first edge of the source rectangle, in pixels.
Sx2
Horizontal location of the second edge of the source rectangle, in pixels.
Sy1
Vertical location of the first edge of the source rectangle, in pixels.
Sy2
Vertical location of the second edge of the source rectangle, in pixels.
Width
The horizontal size of the bounding rectangle in pixels.
X
Horizontal location of the upper left corner of the bounding rectangle, in
pixels.
Y
Vertical location of the upper left corner of the bounding rectangle, in
pixels.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
import java.net.*;
Usage
The Graphics drawLine method can be used to draw straight lines.
Class
java.awt.Graphics
Example Methods
void drawLine (int X1, int Y1, int X2, int Y2)
Arguments
X1
Horizontal location of one end of the line, in pixels.
X2
Horizontal location of the other end of the line, in pixels.
Y1
Vertical location of one end of the line, in pixels.
Y2
Vertical location of the other end of the line, in pixels.
Example
Code
import java.applet.Applet;
import java.awt.*;
}
Usage:
The Graphics drawOval method can be used to draw ovals, and Graphics
fillOval method can be used to draw filled ovals. In both cases, the location of
the oval is defined by the upper left corner of a rectangle which bounds the oval.
Class:
Graphics
Example Methods:
drawOval (int X, int Y, int Width, int Height)
fillOval (int X, int Y, int Width, int Height)
Arguments:
Height
The vertical size of the oval, in pixels.
Width
The horizontal size of the oval, in pixels.
X
Horizontal location of the upper left corner of the bounding rectangle, in
pixels.
Y
Vertical location of the upper left corner of the bounding rectangle, in
pixels.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
Usage
The Graphics drawPolygon method can be used to draw outline polygons,
and the Graphics fillPolygon method can be used to draw filled polygons.
Class
java.awt.Graphics
Prototype
void drawPolygon (int[] XArray, int[] YArray, int N)
void fillPolygon (int[] XArray, int[] YArray, int N)
Arguments
XArray
Array of the horizontal locations of the vertices of the polygon, in pixels.
YArray
Array of the vertical locations of the vertices of the polygon, in pixels.
N
Number of points.
Applet - Polygon1
}
Applet - Polygon2
import java.applet.Applet;
import java.awt.*;
}
Usage:
The Graphics drawPolyline method can be used to draw polylines.
Class:
Graphics
Example Methods:
drawPolyline (int[] XArray, int[] YArray, int N)
Arguments:
XArray
Array of the horizontal locations of the vertices of the polygon, in pixels.
YArray
Array of the vertical locations of the vertices of the polygon, in pixels.
N
Number of points.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
Usage
The Graphics drawRect method can be used to draw outline rectangles, and
the Graphics fillRect method can be used to draw filled rectangles.
Class
java.awt.Graphics
Example Methods
void drawRect (int X, int Y, int Width, int Height)
void fillRect (int X, int Y, int Width, int Height)
Arguments
Height
The vertical size of the rectangle in pixels.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.
Example
Code
import java.applet.Applet;
import java.awt.*;
}
Usage:
The Graphics drawRoundRect method can be used to draw outline rectangles
with rounded corners, and the Graphics fillRoundRect method can be used to
draw filled rectangles with rounded corners.
Class:
Graphics
Example Methods:
drawRoundRect (int X, int Y, int Width, int Height, int
OvalWidth, int OvalHeight)
fillRoundRect (int X, int Y, int Width, int Height, int
OvalWidth, int OvalHeight)
Arguments:
Height
The vertical size of the rectangle in pixels.
OvalHeight
The vertical size of the oval from which the corners are formed, in pixels.
OvalWidth
The horizontal size of the oval from which the corners are formed, in
pixels.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
}
Usage:
The Graphics fill3DRect method can be used to draw filled rectangles, which
is shaded to appear either raised or lowered.
Class:
Graphics
Example Methods:
fill3DRect (int X, int Y, int Width, int Height, boolean
Raised)
Arguments:
Height
The vertical size of the rectangle in pixels.
Raised
If true, the "button" appears raised; if false, the "button" appears lowered.
Width
The horizontal size of the rectangle in pixels.
X
Horizontal location of the upper left corner of the rectangle, in pixels.
Y
Vertical location of the upper left corner of the rectangle, in pixels.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
}
The Graphics drawLine method can be used to print text.
Class
Graphics
Example Methods
drawString (java.lang.String Text, int X, int Y)
Arguments
Text
The text do be drawn
X
Horizontal location of the left end of the text baseline, in pixels.
Y
Vertical location of the left end of the text baseline, in pixels.
Example
Code
import java.applet.Applet;
import java.awt.*;
public class String1 extends Applet {
}
Usage:
The drawing color can be set with the Graphics setColor method.
Class:
Graphics
Example Methods:
setColor (Color C)
Arguments:
C
The new rendering color.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
}
Usage:
The Graphics drawLine method can be used to print text.
Class:
Graphics
Example Methods:
setFont (Font F)
Arguments:
F
A text font to be used for future text drawing.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
}
Usage:
In the default drawing mode, drawing is opaque - that is, each new draw
replaces the old pixel value with the current drawing color. In the XOR mode, set
by the setXOR method, the pixel color alternates between this graphics context's
current color and the new specified color. Logical pixel operations are performed
in the XOR mode, which alternates pixels between the current color and a
specified XOR color. When drawing operations are performed, pixels which are
the current color are changed to the specified color, and vice versa. Pixels that
are of colors other than those two colors are changed in an unpredictable but
reversible manner; if the same figure is drawn twice, then all pixels are restored
to their original values. The default drawing mode can be restore with the
setPaintMode method.
Class:
Graphics
Example Methods:
setXORMode (Color C)
setPaintMode ()
Arguments:
C
The new XOR color.
Example:
Code:
import java.applet.Applet;
import java.awt.*;
}
Usage:
The Graphics translate method can be used to move the origin of a Graphics
object context.
Class:
Graphics
Example Methods:
translate (int X, int Y)
Arguments:
X
Horizontal location of the new origin in the old coordinate system, in
pixels.
Y
Vertical location of the new origin in the old coordinate system, in pixels.
Example:
Code:
import java.applet.Applet;
import java.awt.*;