♠ Posted by Unknown in Core Java at 10:57
Java Graphics [AWT Classes/methods]
Lines :
You
can draw a straight line by using the following method.
drawLine(x1,
y1, x2, y2);
The components (x1, y1) and (x2, y2)
are the starting and ending points of the line.
Retangles :
Java provides six methods for drawing
rectangles in outline or filled with color. You can draw plain rectangles,
rounded rectangles or 3D rectangles.
drawRect(x,
y, w, h);
fillRect(x,
y, w, h);
drawRoundRect(x,
y, w, h, aw, ah);
fillRoundRect(x,
y, w, h, aw, ah);
draw3DRect(x,
y, w, h, raised);
fill3DRect(x,
y, w, h, aw, raised);
The components x, y is the upper-left
corner of the rectangle, and w and h are the width and height of the rectangle.
Ovals :
In Java, the oval is drawn based on
its bounding rectangle, therefore give the parameters if you were to draw a
rectangle.
drawOval(x,
y, w, h);
fillOval(x,
y, w, h);
The parameters x and y indicate the
top-left corner of the bounding rectangle, and w and h indicate the width and
height, respectively, of the bounding rectangle.
Arcs :
Like an oval, an arc is drawn based
on its bounding rectangle. An arc is conceived as part of an oval.
drawArc(x,
y, w, h, angle1, angle2);
fillArc(x,
y, w, h, angle1, angle2);
The components x,y,w, and h are the
same as in the drawoval() method; the parameter angel1 is the starting angle;
angel2 is the spanning angle (that is, the ending angle is angle1+angle2).
Angles are measured in degree and follow the usual mathematical convention.
Polygon :
A polygon comprises a list of (x, y)
coordinate pairs, in which each pair defines a vertex of the polygon and two
successive pairs are the endpoints of a line that is a side of the polygon. The
first and final pairs of (x, y) points are joined by a line segment that closes
the polygon.
drawPolygon(x,
y, n);
fillPolygon(x,
y, n);
int
x[] = {20, 31, 42, 53, 64};
int
y[] = {30, 34, 56, 45, 25};
g.drawPolygon(x,
y, x.length);
Example:
import java.awt.*;
import java.awt.event.*;
public class GraphicalExample extends
Frame implements WindowListener
{
public
GraphicalExample()
{
addWindowListener(this);
}
public
void paint(Graphics g)
{
g.drawRect(30,
30, 100, 100);
g.drawRoundRect(140,
30, 100, 100, 60, 30);
g.setColor(Color.GRAY);
g.fill3DRect(30,
140, 100, 100, true);
}
public
void windowClosed(WindowEvent e)
{}
public
void windowDeiconified(WindowEvent e)
{}
public
void windowIconified(WindowEvent e)
{}
public
void windowActivated(WindowEvent e)
{}
public
void windowDeactivated(WindowEvent e)
{}
public
void windowOpened(WindowEvent e)
{}
public
void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
public
static void main(String[] args)
{
GraphicalExample
f = new GraphicalExample();
f.setSize(300,
300);
f.setVisible(true);
}
}
0 comments:
Post a Comment