Working With Graphics -


java.awt.Graphics class provides various methods to draw on components such as Applet. Commonly used methods of Graphics class are as follows -

1- Drawing Line-

drawLine() method is used to draw a line on Applet window. It has the following syntax -
public abstract void drawLine(int x1, int y1, int x2, int y2) 
Here (x1, y1) are the starting point of line and (x2, y2) are the ending point of line.

Example -

import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
   
    public void paint(Graphics g)
    {
       g.drawLine(100, 100, 300, 100);
    }
    
}

Output -


2- Drawing Rectangles/Square -

drawRect () and fillRect() methods are used to draw outlined and filled rectangle/square respectively on Applet window. Rectangle/square will be filled by the current color of applet. ( default color is black)
It has the following syntax -
     public void drawRect(int x, int y, int width, int height) 
     public void fillRect(int x, int y, int width, int height)
    
Where -
      x - the x coordinate of the rectangle to be drawn.
      y - the y coordinate of the rectangle to be drawn.
      width - the width of the rectangle to be drawn.
      height - the height of the rectangle to be drawn.
Note -
for drawing Square the width and height should be same.

Example -

import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
   
    public void paint(Graphics g)
    {
       g.drawRect(10,10, 100, 50);      // Rectangle
       g.fillRect(10,100,100,100);       // Square
    }
    
}

Output-


3- Drawing Oval/Circle -

drawOval() and fillOval() methods are used to draw outlined and filled Oval/Circle. It has the following syntax -
public abstract void drawOval(int x, int y, int width, int height) 
public abstract void fillOval(int x, int y, int width, int height) 
    
Note - Oval/Circle is drawn inside a rectangle/Square.
Where -
      (x, y) indicates the top left corner of rectangle/square.
      width and height are the width and height of rectangle/circle.
If the width and height have the same value then it will be a square and oval inside it will be a circle otherwise it will be an ellipses.
For example - drawOval(100,100,200,100) will draw an Oval like this -

Example -

import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
   
    public void paint(Graphics g)
    {
       g.drawOval(10,10,200,100);
       g.drawOval(20,110,100,100);
    }
    
}

Output -


4- Drawing Rectangle/Circle with round corner -

drawRoundRect() and fillRoundRect() methods are used to draw an outlined and filled rectangles with round corner. It has the following syntax -
public abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) 
public abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) 
    
Where -
      x - the x coordinate of the rectangle.
      y - the y coordinate of the rectangle.
      width - the width of the rectangle.
      height - the height of the rectangle.
      arcWidth - the horizontal diameter of the arc at the four corners.
      arcHeight - the vertical diameter of the arc at the four corners.

Example -

import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
   
    public void paint(Graphics g)
    {
       g.drawRoundRect(100,100, 200,150, 50,80);
    }
    
}

Output -


5- Drawing Arcs -

drawArc() and fillArc() methods are used to draw an outlined and filled arcs.It has the following syntax -
public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) 
public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) 
Where -
      x - the x coordinate of the upper-left corner of the arc to be drawn.
      y - the y coordinate of the upper-left corner of the arc to be drawn.
      width - the width of the arc to be drawn.
      height - the height of the arc to be drawn.
      startAngle - the beginning angle in degree.
      arcAngle - the angular extent of the arc, relative to the start angle in degree.
Actually Arc is bounded by Rectangle whose top left corner is specified by (x, y), and width and height are specified by width and height. The resulting arc begins at startAngle and extends for arcAngle degrees.
The arc is drawn anticlockwise if the arcAngle is positive, and anticlockwise if arcAngle is negative.
To understand it clearly have a look at the below figure -

Here we have drawn an Arc bounded by a rectangle whose top left position is at (10,10), and width and height are 200 and 150 pixels respectively. Arc startAngle is 90 degree and arcAngle(Angle length) is also 90 degree.
i.e - g.drawArc(10,10,200,150,90,90);
Note that the rectangle within which Arc is bounded will be invisible. Here we have shown it to just to make you understand the concept.

Example -

import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
   
    public void paint(Graphics g)
    {
       g.drawArc(10,10,200,150,90,90);
       g.fillArc(100,100,200,150,0,180);
    }
    
}

Output -