Java Notes

Graphics class


The Graphics class contains drawing methods. A Graphics object is passed to your paintComponent(Graphics g).

You need to write a paintComponent() method which contains all your drawing calls. The paintComponent() method should be defined in a JPanel.

You can set the color and font for all drawing.

The "draw" methods all draw lines, and the "fill" methods fill in the areas. It isn't necessary to use a draw method followed by a fill method, just use the fill method.

The rectangle, oval, and arc methods all define the shape starting from a point (x,y) in the upper left corner, and with a width of w and a height of h.

Arc angles start at 0 pointing to the right. They move counterclockwise to 360 when the angle is again horizontally to the right.

You can draw a circle by drawing an oval with the width equal to the height.


There are also Graphics methods for drawing images, polylines, polygons, and filling polygons. There are additional drawing and imaging methods in the Java 2 javax.swing.Graphics2D class. There are also packages for 3-D graphics and more advanced imaging with operations something like some that are available in Photoshop.