The Graphics class is abstract. It is just a collection of specifications for
low-level drawing methods. The graphics object also contains a reference to the area of ram where the bit image
will be constructed and it also contains the current clipping region. It is used for all types of painting
including placing components, canvas drawing, and laying out text.
The most often use the Graphics object is as a parameter to your paint, paintComponent or print method.
You often cast the abstract Graphics to an actual Graphics2D object to access additional methods. Have a look at the Screws application to understand the basics of drawing.
Learning More
Round Polygons
Graphics has methods drawRoundRect and drawRoundRect to draw rectangles with rounded corners, but there are no built-in
methods to do the same for
arbitrary polygons, including triangles. Here is the code taken from the connectors
downloadable package to do it:
The math behind that program may be a bit baffling. This diagram might help. The techinque for computing the rounding
uses elementary geometry, the pythagorean theorem,
and elementary trigonometry.
Operations on the Entire Image
Oracle’s Javadoc on
Graphics class : available:
Oracle’s Javadoc on
Graphics2D class : available:
Oracle’s Javadoc on
Graphics2D.setTransform : Beware, cascaded transforms are specified in reverse order. : available:
Oracle’s Javadoc on
Graphics2D.addRenderingHints : available:
Oracle’s Javadoc on
setClip class :
Graphics.setClip replaces the clip region.
clip merges/intersecs this clip with the existing clip region. : available:
Oracle’s Javadoc on
Graphics2D.clip :
setClip replaces the clip region.
clip merges/intersecs this clip with the existing clip region. : available:
Oracle’s Javadoc on
Graphics.create : Used to prevent permanently modifying your caller’s
AffineTransform or clip region. : available:
Oracle’s Javadoc on
Area class : Useful to create drop cloths — odd shaped clip regions. : available:
Oracle’s Javadoc on
Composite class : Controls how new painting interacts with what is already painted. : available:
Oracle’s Javadoc on
AlphaComposite class : Controls how new painting interacts with what is already painted. : available:
Lines
Oracle’s Javadoc on
Graphics.drawLine : available:
Oracle’s Javadoc on
BasicStroke class : available:
Rectangles
Oracle’s Javadoc on
Graphics.clearRect : available:
Oracle’s Javadoc on
Graphics.drawRect} : available:
Oracle’s Javadoc on
Graphics.fillRect : available:
Oracle’s Javadoc on
Rectangle class : available:
Oracle’s Javadoc on
RoundRectangle2D.Double : available:
Text
Oracle’s Javadoc on
Graphics.drawString :
the Graphics. drawString method behaves differently from other drawing methods. Usually the x,y is the upper left corner. For drawString, it is the left end of the string baseline. : available:
Oracle’s Javadoc on
FontRenderContext class : available:
Oracle’s Javadoc on
LineMetrics class : available:
Ovals, Ellipse and Circles
Oracle’s Javadoc on
Graphics.drawArc :
Beware. Angles are measured in degrees counter clockwise from 3 o’clock. It does not take start and end angle but start angle and arc size in degrees (end-start). Angles may be negative. : available:
Oracle’s Javadoc on
Graphics.fillArc :
Beware. Angles are measured in degrees counter clockwise from 3 o’clock. It does not take start and end angle but start angle and arc size in degrees (end-start). Angles may be negative. : available:
Oracle’s Javadoc on
Arc2D.Double : available:
Oracle’s Javadoc on
Graphics.fillOval : available:
Oracle’s Javadoc on
Ellipse2D.Double : This is an inner class to describe an ellipse or circle. Note that you specify the upper left corner of the enclosing rectangle, not the centre. : available:
Images
Oracle’s Javadoc on
Graphics.drawImage : available:
Oracle’s Javadoc on
BufferedImage class : available:
Oracle’s Javadoc on
ImageIO class : Used to read and write png, jpg and gif files. : available:
Oracle’s Javadoc on
BufferedImage.getRaster : Used to manipulate individual pixels. : available:
Arbitrary Shapes
Oracle’s Javadoc on
Shape class : Interface you can pass to
draw for drawing arbitrary shape outlines, solids and clip regions. Check out the long list of implementors. : available:
Oracle’s Javadoc on
Path2D.Double :
Path2D is available in Java 1.6+. It replaces the legacy
GeneralPath. : available:
Oracle’s Javadoc on
Graphics2D.draw(Shape) : Draw a line around an arbitrary shape. : available:
Oracle’s Javadoc on
Graphics.drawPolygon : Draws outline of a closed polygon. : available:
Oracle’s Javadoc on
Graphics.drawPolyline : Draws open polygon, series of line segments. : available:
Oracle’s Javadoc on
Polygon class : available: