Working With Graphics
Working With Graphics
Drawing Lines
Lines are drawn by means of the drawLine( ) method, shown here:
void drawLine(int startX, int startY, int endX, int endY)
Drawing Rectangles
The drawRect( ) and fillRect( ) methods display an outlined and filled
rectangle, respectively.
They are shown here:
void drawRect(int top, int left, int width, int height)
void fillRect(int top, int left, int width, int height)
The upper-left corner of the rectangle is at top,left.
The dimensions of the rectangle are specified by width and height.
Example:
g.drawOval(10, 10, 50, 50);
g.fillOval(100, 10, 75, 50);
Drawing Arcs
Arcs can be drawn with drawArc( ) and fillArc( ), shown here:
void drawArc(int top, int left, int width, int height, int startAngle, int
sweepAngle)
void fillArc(int top, int left, int width, int height, int startAngle, int
sweepAngle)
Example:
g.drawArc(10, 40, 70, 70, 0, 75);
g.fillArc(100, 40, 70, 70, 0, 75);
Drawing Polygons
It is possible to draw arbitrarily shaped figures using drawPolygon( ) and
fillPolygon( ), shown here:
void drawPolygon(int x[ ], int y[ ], int numPoints)
void fillPolygon(int x[ ], int y[ ], int numPoints)
The polygon’s endpoints are specified by the coordinate pairs contained
within the x and y arrays.
The number of points defined by x and y is specified by numPoints.
There are alternative forms of these methods in which the polygon is
specified by a Polygon object.
Example:
int xpoints[] = {30, 200, 30, 200, 30};
int ypoints[] = {30, 30, 200, 200, 30};
int num = 5;
g.drawPolygon(xpoints, ypoints, num);
Sizing Graphics
Often, you will want to size a graphics object to fit the current size of the
window in which it is drawn.
To do so, first obtain the current dimensions of the window by calling
getSize( ) on the window object.
It returns the dimensions of the window encapsulated within a Dimension
object.
Once you have the current size of the window, you can scale your
graphical output accordingly.
The first constructor takes three integers that specify the color as a mix of
red, green, and blue.
These values must be between 0 and 255, as in this example:
new Color(255, 100, 100); // light red.
The second color constructor takes a single integer that contains the mix
of red, green, and blue packed into an integer.
The integer is organized with red in bits 16 to 23, green in bits 8 to 15,
and blue in bits 0 to 7.
Here is an example of this constructor:
int newRed = (0xff000000 | (0xc0 << 16) | (0x00 << 8) | 0x00);
Color darkRed = new Color(newRed);
The final constructor, Color(float, float, float), takes three float values
(between 0.0 and 1.0) that specify the relative mix of red, green, and blue.
Once you have created a color, you can use it to set the foreground and/or
background color by using the setForeground( ) and setBackground( )
methods.
You can also select it as the current drawing color.
Color Methods
The Color class defines several methods that help manipulate colors.
int getRed( )
int getGreen( )
int getBlue( )
Each of these methods returns the RGB color component found in the
invoking Color object in the lower 8 bits of an integer.
getRGB( )
g.setColor(c1);
g.drawLine(0, 0, 100, 100);
g.drawLine(0, 100, 100, 0);
g.setColor(c2);
g.drawLine(40, 25, 250, 180);
g.drawLine(75, 90, 400, 400);
g.setColor(c3);
g.drawLine(20, 150, 400, 40);
g.drawLine(5, 290, 80, 19);
g.setColor(Color.red);
g.drawOval(10, 10, 50, 50);
g.fillOval(70, 90, 140, 100);
g.setColor(Color.blue);
g.drawOval(190, 10, 90, 30);
g.drawRect(10, 10, 60, 50);
g.setColor(Color.cyan);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
}
}