| ★ wanayoo — archive 1999 http://javaboutique.internet.com/tutorials/Java2D-Tutorial_3.html | Nouvelle recherche | Portail wanayoo |
|
![]()
Java2D: An Introduction and Tutorialby Marty HallTable of Contents 2. Drawing Shapes in Java2D2.1 Drawing Shapes: OverviewWith 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 ClassesArguments 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:
2.3 Drawing Shapes: Example CodeDownload 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
[Move on to the next part of the article] This article first appeared in November, 1998
Copyright 1998 Mecklermedia Corporation. |