Aqua Phoenix
     >>  Lectures >>  Java 4  
 

Navigator
   
 
       
   

4.7 Graphics Object

The Graphics object is predominantly used to paint primitive geometric objects, like lines, arcs, polygons, rectangles, ellipses, as well as images and text. Using these primitive operations, it is possible to custom-design GUI components. Even pre-defined GUI components are merely collections of primitive paint operations, e.g. a Button is a series of filled rectangles, lines, and text. Combined with a MouseListener, a visual clicking effect is achieved.

Virtually any area in a GUI-style Java application qualifies as an area that can be painted on. This also includes existing objects, e.g. Buttons, Lists, etc. Imagine the Graphics object as a grafiti can, with which you can deface existing GUIs.

All GUI components in Java inherit from the super-class Component, which implements methods for painting using the Graphics object. Existing objects that extend Component include GUI components like Button, Canvas, etc., as well as containers like Panel, Applet, etc.

The most important method for painting is public void paint(Graphics g) . The Graphics object is passed by the superclass, and can be used to paint in the context of the class implementing the method.

Another method named public void update(Graphics g) is predominantly used for removing some flicker from consecutive paint method calls. Typically, method paint is simply called from within update.

4.7.1 Template for a painting Applet

import java.applet.*;
import java.awt.*;
import java.util.*;

public class paintingApplet extends Applet {

  public void init() {

  }

  public void paint(Graphics g) {

  }

  public void update(Graphics g) {

    paint(g);
  }
}
The above applet can be used as a canvas for painting, by simply adding the appropriate primitive paint operations to the paint method.

Some examples of method calls to object Graphics follow:

  • Line: g.drawLine(int x1, int y1, int x2, int y2); e.g. g.drawLine(0, 0, 100, 100);
  • Rectange: g.drawRect(int x1, int y1, int width, int height); e.g. g.drawRect(10, 20, 50, 60);
  • Setting Colors: g.setColor(Color c); e.g. g.setColor(Color.red); or g.setColor(new Color(60, 120, 187));
  • Filled Rectangle (similar to Rectangle): g.fillRect(int x1, int y1, int width, int height); e.g. g.fillRect(10, 20, 50, 60);

The coordinate system for an applet and its components bears the (0,0) point in the upper-left-hand corner of the applet canvas. Hence, a line drawn for coordinates 0,0,100,100 begins the line in the left-upper-hand corner, and draws it towards the right-lower-hand corner.

4.7.2 Color

Colors in Java are defined in a separate class by the name of Color. The Color class defines a number of common colors, which can be used in a static context:
  • Color.black or Color.BLACK
  • Color.blue or Color.BLUE
  • Color.cyan or Color.CYAN
  • Color.DARK_GRAY or Color.darkGray
  • Color.gray or Color.GRAY
  • Color.green or Color.GREEN
  • Color.LIGHT_GRAY or Color.lightGray
  • Color.magenta or Color.MAGENTA
  • Color.orange or Color.ORANGE
  • Color.pink or Color.PINK
  • Color.red or Color.RED
  • Color.white or Color.WHITE
  • Color.yellow or Color.YELLOW

Custom colors can also be created using the RGB (red, green, blue) base colors, e.g.:

Color myBlueColor = new Color(20, 40, 230);
Color myReddishTone = new Color(196, 4, 55);

4.7.3 Example 1

  public void paint(Graphics g) {

    // J
    g.setColor(Color.blue);
    g.fillRect(10, 10, 50, 10);
    g.setColor(Color.green);
    g.drawArc(-40, -90, 100, 200, 270, 90);
    g.drawArc(-40, -88, 98, 196, 270, 90);
    g.drawArc(-40, -86, 96, 192, 270, 90);
    g.drawArc(-40, -84, 94, 188, 270, 90);

    // A
    g.setColor(Color.gray);
    g.fillRect(80, 10, 50, 100);
    g.setColor(Color.yellow);
    g.fillRect(95, 25, 20, 35);
    g.fillRect(95, 75, 20, 35);

    // V
    g.setColor(new Color(0, 174, 255));
    g.drawLine(150, 10, 175, 110);
    g.drawLine(151, 10, 176, 110);
    g.setColor(new Color(0, 184, 255));
    g.drawLine(200, 10, 175, 110);
    g.setColor(new Color(0, 194, 255));
    g.drawLine(200, 10, 175, 110);
    g.setColor(new Color(0, 204, 255));
    g.drawLine(202, 10, 177, 110);
    g.setColor(new Color(0, 214, 255));
    g.drawLine(202, 10, 177, 110);
    g.setColor(new Color(0, 224, 255));
    g.drawLine(204, 10, 179, 110);
    g.setColor(new Color(0, 234, 255));
    g.drawLine(204, 10, 179, 110);
    g.setColor(new Color(0, 244, 255));
    g.drawLine(206, 10, 181, 110);
    g.setColor(new Color(0, 254, 255));
    g.drawLine(206, 10, 181, 110);

    // A
    g.setColor(Color.magenta);
    g.fillArc(220, 10, 50, 100, 0, 180);
    g.fillRect(220, 60, 50, 50);
    g.setColor(Color.red);
    g.fillArc(235, 25, 20, 70, 0, 180);
    g.fillRect(235, 75, 20, 35);
  }
The above code results in the following applet:

  SimpleGraphics.java  

4.7.4 Example 2

The above example shows an implementation of using the paint method in an applet. The method could just as well have been used in a custom Canvas. Canvases are often used as canvases to draw on, because they are initially blank. A custom-designed Canvas may look like:

import java.applet.*;
import java.awt.*;
import java.util.*;

public class myCanvas extends Canvas {

  public void init() {

    setSize(100, 50);
  }

  public void paint(Graphics g) {

    g.setColor( ... );
    g.fillRect( ... );
  }

  public void update(Graphics g) {

    paint(g);
  }
}
This newly created object can be instantiated and added to a Layout as if it were a Button or List.