Graphics Methods in Java

Andrew's Avatar author of Graphics Methods in Java
This is an article on Graphics Methods in Java in Java.

Drawing Lines



The drawLine method belongs to the Graphics class.

This example draws a fan of twenty lines from the bottom center of the Applet along the top edge. The applet's getSize method is used to find the dimensions of the applet.
Code: JAVA
import java.awt.*;
import java.applet.*;
public class LineExample extends Applet
{
    public LineExample()
    {
        this.setBackground(new Color(255, 255, 255));
        this.setForeground(new Color(255, 0, 0));
    }
    public void paint(Graphics g)
    {
        // Get the width and height of the Applet
        int width = this.getSize().width;
        int height = this.getSize().height;
        // Fan 20 lines from the bottom-middle, across the top
        for (int counter=0; counter<=width; counter+=(width/20))
            g.drawLine(width/2, height, counter, 0);
    }
}

Drawing Ovals



The drawOval method belongs to the Graphics class.

The drawOval method takes four parameters; x, y, width, and height. The following example draws an oval, half the size of the applet, in the center of the applet.
Code: JAVA
import java.awt.*;
import java.applet.*;
public class OvalExample extends Applet
{
    public OvalExample()
    {
        this.setBackground(new Color(255, 255, 255));
        this.setForeground(new Color(255, 0, 0));
    }
    public void paint(Graphics g)
    {
        // Get the width and height of the Applet
        int width = this.getSize().width;
        int height = this.getSize().height;
        g.drawOval(width/4, height/4, width/2, height/2);
    }
}

Freehand Drawing



In the previous examples, the paint method was used to display graphics on the applet. The paint method has a parameter for the graphics context, but you may wish to use the graphics context in other methods that don't have a graphics parameter. The getGraphics method is used to return the graphics context for a component, which in this case is the applet.

Graphics g = this.getGraphics();

Freehand Example:



The following example uses the getGraphics method for free-hand drawing. The example uses a MouseListener, and a MouseMotionListener, which are part of the Java 1.1 Event Model.
Code: JAVA
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class FreehandExample extends Applet implements MouseListener, MouseMotionListener
{
    private Point start, end;
    public FreehandExample()
    {
        this.setBackground(new Color(255, 204, 0));
        this.setForeground(new Color(51, 153, 204));
        // Add the Event Listeners
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }
    // Capture the mousePressed event
    public void mousePressed(MouseEvent e)
    {
        start = new Point(e.getX(), e.getY());
    }
    // Define the other events for MouseListener
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
   
    // Capture the mouseDrag event
    public void mouseDragged(MouseEvent e)
    {
        // Get the current state of the graphics
        Graphics g = this.getGraphics();
       
        end = new Point(e.getX(), e.getY());
        g.drawLine(start.x, start.y, end.x, end.y);
        start = end;
    }
    // Define the other events for MouseMotionListener
    public void mouseMoved(MouseEvent e) {}
}