JAVA 2D PACKAGE

CONTENTS

1. Introduction and example



1. INTRODUCTION AND EXAMPLE

The java 2-D API comprises a set of 2-D grapgics packages. The Java code in Table 1 gives an example usinmg methods contained in the java.awt.geom package (the example is taken from Dietel and Dietel). Pints to not:

  1. Drawing with the Jaca2D API is done using an instance of the class Graphics2D (contained in java.awt) which is a sub-class of Grpahics and thus inherits all the methods defined in the Graphics class. In the code we cast the Graphics reference g to a Graphic2D reference, called g2d, to allow acces to the Java2D capabilities such as translate, rotate, setColor and fill.
  2. The translate method defines the origin of the drawing to which operations like rotate are referencd.
  3. The method fill causes the shape to appear on the screen --- the general path methods described below simply define the shape.
  4. The GeneralPath class is used to describe shapes made-up of lines and curves, the code describes an instance of this class called shape.
  5. The class GeneralPath includes the methods:
    1. moveTo to define the start point of the shape,
    2. lineTo to define the following points describing the shape in terms of straight lines between these points, and
    3. closePath to "draw" a line from the last point defined by lineTo to the start.
// TUMBLING STAR
// Frans Coenen
// Friday 8 may 2003
// The University of Liverpool, UK

/* Based on example given by Deitel and Deital */

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

// Java extension packages
import javax.swing.*;

public class TumblingStar extends JFrame {

    // ------------------- FIELDS ------------------------

    /* None */

    // ------------------ CONSTRUCTORS -------------------

    public TumblingStar() {
        super("TumblingStar");
        getContentPane().setBackground(Color.yellow);
        setSize(400,400);
        setVisible(true);
        }

    // ------------------ METHODS ------------------------

    /* PAINT */

    /** Draws general paths. */

    public void paint(Graphics g) {
        super.paint(g);
        int xPoints[] = {55, 67, 109, 73, 83, 55, 27, 37, 1, 43};
        int yPoints[] = {0, 36, 36, 54, 96, 72, 96, 54, 36, 36};

        // Create 2D by casting g to graphics2D
        Graphics2D g2d=(Graphics2D) g;

        // Create a star from a series of points
        GeneralPath star = new GeneralPath();

        // Set the initiakl coordinates of the general Path
        star.moveTo(xPoints[0],yPoints[0]);

        // Create the star. Note: this does not draw the star.
        for(int index=1;index < xPoints.length;index++)
            star.lineTo(xPoints[index],yPoints[index]);

        // Close the shape
        star.closePath();

        // translate the origin (200,200)
        g2d.translate(200,200);

        // rotate around origin and draw stars in random colours
        for (int index=1;index<=20;index++) {
            // Rotate coordinate system
            g2d.rotate(Math.PI/10.0);
            // Set random drawing color
            g2d.setColor(new Color((int) (Math.random()*256), (int) (Math.random()*256),
            		(int) (Math.random()*256)));
            // Draw dilled star
            rest();
            g2d.fill(star);
            }
        }

    private void rest() {
	for (double index=1.0;index<=20000.0;index++) System.out.print(".");
        }

    /* MAIN METHOD */

    public static void main(String args[]) {
        TumblingStar application = new TumblingStar();

        application.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        }
    }

Table 1: Java2D example code

TIMBLINF STAR WINDOW

Figure 1 :Output from code presented in Table 1