The AWT supports many graphics methods. All graphics
are drawn relative to a window. This can be the main window of an applet, a
child window of an applet, or a stand-alone application window. A graphics
context is encapsulated by the Graphics
Class and it’s obtained two ways:
·
It is passed to
an applet when one of its various methods, such as paint() or update(),is called.
·
It is returned
by the getGraphics() methods of
component.
The Graphics class defines a number of drawing
function. Each shape can be drawn edge-only or filled. Object are drawn and
filled in the currently select graphics color, which is black by default. The
several drawing methods :
·
Drawing
Lines: Line are draw by means
of the drawline() method:
void drawLine(int startX,int startY, int
endX,endY)
Therefore drawLine() display a line in the current
drawing color that beings at startX
startY ends at endX,endY.
· Drawing
Rectangles: The drawRect() and
fillrect() methods display an outline and filled rectangle, respectively.
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. To draw a
rounded rectangle, use drawRoundRect() and fillRoundRect(), both are shown here:
void
fillRoundRect(int top,int left,int width,int height,int xDiam,int yDim)
A rounded rectangle has rounded corners; the upper
left corner of the rectangle is at top, left. The dimension of the rectangle is
specified by width and height.
· Drawing Ellipses
and Circle: To draw an ellipse, use
drawOval(). To fill an ellipse, use fillOval(). These methods are shown here:
void
drawOval(int top,int left,int width,int height)
void
fillOval(int top,int left,int width,int height)
The ellipse is drawn within a bounding rectangle
whose upper–left corner is specified by top, left and whose width and height
are specified by width and height.
· Drawing polygon:
It is possible to draw arbitrarily shaped
figures using drawPolygon().
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.
Working
with Color: Java supports
color in a portable, device-independent fashion. The AWT color system allows
you to specify any color you want. The color is supported by various hardware
devices. Color is encapsulate by the color
class.
Color defines several constants (for example
Color.black) to specify a number of common colors. We can also create our own
colors, using one of the color constructors.
Color(int red,int green,int blue)
Color(int rgbValue)
Color(float red,float green,float blue)
·
Color Methods:
That color class defines several methods that help manipulate colors:
getRed(),getGreen(),getBlue()
We can obtain the red, green, and blue components of
a color independently using getRed(), getGreen(), and getBlue() as show here:
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 bit integer.
· Setting
the Current Graphics color: By default, graphics object are drawn in the current
foreground color. You can change this color by calling the Graphics method
setColor()
void setColor(Color
newcolor)
You can obtain the current color by calling getColor() shown
here:
Color
getColor();
No comments:
Write comments