Graphics Methods
The java.awt.Graphics
class provides numerous methods for drawing
shapes and changing colors. These methods can be accessed through a reference to the
current Graphics object. In an Applet
, the Graphics
object is
obtained through the method getGraphics()
.
- No method is available to determine the absolute location of an Applet in a browser
window; however, the method
getSize()
inherited from the Component
class is available to determine the width and height of the Applet.
- The graphics context has an origin (0,0) initially located at the upper left of the
window, with x increasing from left to right and y increasing from top to bottom.
- The (x,y) coordinates in a graphics context represent pixels.
1. Drawing Operations
- copyArea( left, top, width, height, deltaX, deltaY )
- drawArc( left, top, width, height, startAngle, deltaAngle )
- fillArc( left, top, width, height, startAngle, deltaAngle )
- drawImage( image, left, top, observer )
- drawImage( image, left, top, width, height, observer )
- drawImage( image, left, top, backgroundColor, observer )
- drawImage( image, left, top, width, height, backgroundColor observer )
- drawLine( x1, y1, x2, y2 )
- drawOval( left, top, width, height )
- fillOval( left, top, width, height )
- drawPolygon( xArray, yArray, numPoints )
- drawPolygon( polygon )
- fillPolygon( xArray, yArray, numPoints )
- fillPolygon( polygon )
- drawRect( left, top, width, height )
- fillRect( left, top, width, height )
- drawString( string left, bottom )
Example
2. Colors and Fonts
- setColor( color )
- getColor()
- setFont( Font )
- getFont()
setColor
and setFont
do not
persist to the next invocation of paint or the next time getGraphics
is called.
- Persistent
Font
and Color
changes are forced by calling the applets setFont
and setColor
methods (inherited from java.awt.Container
).
3. Drawing Mode
- setXORMode( color )
Sets the graphics context to paint in XOR mode: alternate between
the old color and the new color. Drawing a figure twice in a row is guaranteed to return
the pixels back to the original condition. Typically, XOR mode is set using the background
color, setXORMode(getBackground())
- setPaintMode()
Sets the graphics context to paint in normal mode.