★ wanayoo — archive 1999 http://javaboutique.internet.com/tutorials/Java2D-Tutorial_3.htmlNouvelle recherche | Portail wanayoo
internet.com logo To JavaBoutique Home

 Java News
 Latest Applets
 Applet Categories
 Applet Index
 Applet Help
 Submit Applets
 Other Resources
 How-To Java
 Java Reviews
 Java FAQs
 Java Forum
 Java IDEs
To Webdeveloper.com
 Mecklermedia
 Internet.com
 Magazines
 Trade Shows
 Advertise
 Corporate
 Information
 Search
 Subscribe

internet.com
BrowserWatch
CyberAtlas
E-Commerce Guide
IntranetDesignMag
InternetNews.com
InternetProductWatch
InternetShopper
Internet World
JavaBoutique
JavaScript Source
JustSMIL
PCWebopaedia.com
ScriptSearch
SearchEngineWatch
ServerWatch
Stroud's CWSApps
The List
The WDVL
WebCompare.com
WebDeveloper.com
WebReference.com


How-To Java

Java2D: An Introduction and Tutorial

by Marty Hall
Table of Contents

2. Drawing Shapes in Java2D

2.1 Drawing Shapes: Overview

With the AWT, you generally drew a shape by calling the drawXxx or fillXxx method of the Graphics object. In Java2D, you generally create a Shape object, then call either the draw or fill method of the Graphics2D object, supplying the Shape object as an argument. For example:
public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2d = (Graphics2D)g;
  // Assume x, y, and diameter are instance variables
  Ellipse2D.Double circle =
    new Ellipse2D.double(x, y, diameter, diameter);
  g2d.fill(circle);
  ...
}
You can still call the drawXxx methods if you like, however. This is necessary for drawString and drawImage, and possibly convenient for draw3DRect. Several classes have similar versions that store coordinates as either double precision numbers (Xxx.Double) or single precision numbers (Xxx.Float). The idea is that single precision coordinates might be slightly faster to manipulate on some platforms.

2.2 Shape Classes

Arguments to the Graphics2D draw and fill methods must implement the Shape interface. You can create your own shapes, of course, but following are the major built-in ones:
  • Arc2D.Double, Arc2D.Float
  • Area
  • CubicCurve2D.Double, CubicCurve2D.Float
  • Ellipse2D.Double, Ellipse2D.Float
  • GeneralPath
  • Line2D.Double, Line2D.Float
  • Polygon
  • QuadCurve2D.Double, QuadCurve2D.Float
  • Rectangle2D.Double, Rectangle2D.Float, Rectangle
  • RoundRectangle2D.Double, RoundRectangle2D.Float

2.3 Drawing Shapes: Example Code

Download the source: ShapeExample.java, WindowUtilities.java, and ExitListener.java.

ShapeExample.java

import com.sun.java.swing.*; // For JPanel, etc.
import java.awt.*;           // For Graphics, etc.
import java.awt.geom.*;      // For Ellipse2D, etc.

/** An example of drawing/filling shapes with Java2D in Java 1.2.
 *  1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
 */

public class ShapeExample extends JPanel {
  private Ellipse2D.Double circle =
    new Ellipse2D.Double(10, 10, 350, 350);
  private Rectangle2D.Double square =
    new Rectangle2D.Double(10, 10, 350, 350);

  public void paintComponent(Graphics g) {
    clear(g);
    Graphics2D g2d = (Graphics2D)g;
    g2d.fill(circle);
    g2d.draw(square);
  }

  // super.paintComponent clears offscreen pixmap,
  // since we're using double buffering by default.

  protected void clear(Graphics g) {
    super.paintComponent(g);
  }

  protected Ellipse2D.Double getCircle() {
    return(circle);
  }

  public static void main(String[] args) {
    WindowUtilities.openInJFrame(new ShapeExample(), 380, 400);
  }
}

WindowUtilities.java

import com.sun.java.swing.*;
import java.awt.*;

/** A few utilities that simplify testing of windows in Swing.
 *  1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
 */

public class WindowUtilities {

  /** A simplified way to see a JPanel or other Container.
   *  Pops up a JFrame with specified Container as the content pane.
   */

  public static JFrame openInJFrame(Container content,
                                    int width,
                                    int height,
                                    String title,
                                    Color bgColor) {
    JFrame frame = new JFrame(title);
    frame.setBackground(bgColor);
    content.setBackground(bgColor);
    frame.setSize(width, height);
    frame.setContentPane(content);
    frame.addWindowListener(new ExitListener());
    frame.setVisible(true);
    return(frame);
  }

  /** Uses Color.white as the background color. */

  public static JFrame openInJFrame(Container content,
                                    int width,
                                    int height,
                                    String title) {
    return(openInJFrame(content, width, height, title, Color.white));
  }

  /** Uses Color.white as the background color, and the
   *  name of the Container's class as the JFrame title.
   */

  public static JFrame openInJFrame(Container content,
                                    int width,
                                    int height) {
    return(openInJFrame(content, width, height,
                        content.getClass().getName(),
                        Color.white));
  }
}

ExitListener.java

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

/** A listener that you attach to the top-level Frame or JFrame of
 *  your application, so quitting the frame exits the application.
 *  1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
 */

public class ExitListener extends WindowAdapter {
  public void windowClosing(WindowEvent event) {
    System.exit(0);
  }
}

2.4 Drawing Shapes: Example Output

Output: drawing shapes in Java2D


[Move on to the next part of the article]

This article first appeared in November, 1998

Free Web Developer Info!

Copyright 1998 Mecklermedia Corporation.
All Rights Reserved. Legal Notices.