Graphics Class in Java

 Graphics Class in Java

Guys you can draw lines, shapes, characters, and images to the screen inside  your applet. Most of the graphics operations in Java are methods defined in the Graphics class. You don’t have to create an instance of Graphics in order to draw something in your applet; in your applet’s paint() method , you are given a Graphics object. By drawing on that object, you draw onto your applet and the results appear on screen. The Graphics class is part of the java.awt package, so if your applet does any painting (as it usually will), make sure you import that class at the beginning of your Java file:

The Graphics Coordinate System

  • To draw an object on the screen, you call one of the drawing methods available in the Graphics class. All the drawing methods have arguments representing endpoints, corners, or starting locations of the object as values in the applet’s coordinate system—for example, a line starts at the points 10,10 and ends at the points 20,20.
  • Java’s coordinate system has the origin (0,0) in the top left corner. Positive x values are to the right, and positive y values are down. All pixel values are integers; there are no partial or fractionalpixels. Below shows how you might draw a simple square by using this coordinate system.

Graphics Coordinate System

Java’s coordinate system is different from many painting and layout programs that have theirx and y in the bottom left. If you’re not used to working with this upside-down graphics system, it may take some practice to get familiar with it.

Drawing and Filling

The Graphics class provides a set of simple built-in graphics primitives for drawing:

  • Lines
  • Rectangles
  • Polygons
  • Ovals
  • Arcs


To draw straight lines, use the drawLine method. drawLine takes four arguments: the x and y coordinates of the starting point and the x and y coordinates of the ending point.

Line in java


The Java graphics primitives provide not just one, but three kinds of rectangles:

  •  Plain rectangles
  •  Rounded rectangles, which are rectangles with rounded corners
  •  Three-dimensional rectangles, which are drawn with a shaded border

For each of these rectangles, you have two methods to choose from: one that draws the rectangle in outline form, and one that draws the rectangle filled with color.
To draw a plain rectangle, use either the drawRect or fillRect methods. Both take fourarguments: the x and y coordinates of the top left corner of the rectangle, and the width and height of the rectangle to draw. For example, the following paint() method draws two squares the left one is an outline and the right one is filled

Rounded rectangles are, as you might expect, rectangles with rounded edges. The drawRoundRect and fillRoundRect methods to draw rounded rectangles are similar to regular rectangles except that rounded rectangles have two extra arguments for the width and height of the angle of the corners. Those two arguments determine how far along the edges of the rectangle the arc for the corner will start; the first for the angle along the horizontal plane, the second for the vertical.
Larger values for the angle width and height make the overall rectangle more rounded; valuesequal to the width and height of the rectangle itself produce a circle. Figure 9.4 shows some examples of rounded corners.


Here’s a paint method that draws two rounded rectangles: one as an outline with a rounded corner 10 pixels square; the other, filled, with a rounded corner 20 pixels square

Finally, there are three-dimensional rectangles. These rectangles aren’t really 3D; instead, they have a shadow effect that makes them appear either raised or indented from the surface of the applet. Three-dimensional rectangles have four arguments for the x and y of the start position and the width and height of the rectangle. The fifth argument is a boolean indicating whether the 3D effect is to raise the rectangle (true) or indent it (false). As with the other rectangles, there are also different methods for drawing and filling: draw3DRect and fill3DRect. Here’s code
to produce two of them—the left one indented, the right one raised



Polygons are shapes with an unlimited number of sides. To draw a polygon, you need a set of x and y coordinates, and the drawing method then starts at one, draws a line to the second, then a line to the third, and so on.
As with rectangles, you can draw an outline or a filled polygon (the drawPolygon and fillPolygon methods, respectively). You also have a choice of how you want to indicate the list of coordinates—either as arrays of x and y coordinates or as an instance of the Polygon class.
Using the first method, the drawPolygon and fillPolygon methods take three arguments:
n An array of integers representing x coordinates
n An array of integers representing y coordinates
n An integer for the total number of points
The x and y arrays should, of course, have the same number of elements.
Here’s an example of drawing a polygon’s outline by using this method


Note that Java does not automatically close the polygon; if you want to complete the shape, you have to include the starting point of the polygon at the end of the array. Drawing a filled polygon, however, joins the starting and ending points.

The second way of calling drawPolygon and fillPolygon is to use a Polygon object. The Polygon class is useful if you intend to add points to the polygon or if you’re building the polygon on the fly. The Polygon class enables you to treat the polygon as an object rather than having to deal with individual arrays.
To create a polygon object you can either create an empty polygon:

or create a polygon from a set of points using integer arrays, as in the previous example:

Once you have a polygon object, you can append points to the polygon as you need to:

Then, to draw the polygon, just use the polygon object as an argument to drawPolygon or
fillPolygon. Here’s that previous example, rewritten this time with a Polygon object. You’ll also
fill this polygon rather than just drawing its outline .


Use ovals to draw ellipses or circles. Ovals are just like rectangles with overly rounded corners. In fact, you draw them using the same four arguments: the x and y of the top corner, and the width and height of the oval itself. Note that, because you’re drawing an oval, the starting point is some distance to the left and up from the actual outline of the oval itself. Again, if you think of it as a rectangle, it’s easier to place.
As with the other drawing operations, the drawOval method draws an outline of an oval, and the fillOval method draws a filled oval.
Here’s an example of two ovals, a circle and an ellipse.



Of the drawing operations, arcs are the most complex to construct, which is why I saved them for last. An arc is a part of a oval; in fact, the easiest way to think of an arc is as a section of a complete oval.

The drawArc method takes six arguments: the starting corner, the width and height, the angle at which to start the arc, and the degrees to draw it before stopping. Once again, there is a drawArc method to draw the arc’s outline and the fillArc to fill the arc. Filled arcs are drawn as if theywere sections of a pie; instead of joining the two endpoints, both endpoints are joined to thecenter of the circle.

Let’s start with a simple arc, a C shape on a circle as below


To construct the method to draw this arc, the first thing you do is think of it as a complete circle.Then you find the x and y coordinates and the width and height of that circle. Those four valuesare the first four arguments to the drawArc or fillArc methods.

To get the last two arguments, think in degrees around the circle. Zero degrees is at 3 o’clock,90 degrees is at 12 o’clock, 180 at 9 o’clock, and 270 at 6 o’clock. The start of the arc is the degreevalue of the start of the arc. In this example, the starting point is the top of the C at 90 degrees;90 is the fifth argument.The sixth and last argument is another degree value indicating how far around the circle to sweepand the direction to go in (it’s not the ending degree angle, as you might think). In this case,because you’re going halfway around the circle, you’re sweeping 180 degrees—and 180 istherefore the last argument in the arc. The important part is that you’re sweeping 180 degrees counterclockwise, which is in the positive direction in Java. If you are drawing a backwards C,you sweep 180 degrees in the negative direction, and the last argument is –180. See above Figure

Here’s the code for this example; you’ll draw an outline of the C and a filled C to its right, as
shown in above Figure