Java Reference
In-Depth Information
Figure 11•4. Drawing and Þlling shapes with the Java 2D API
Example 11•6: Shapes.java
package com.davidflanagan.examples.graphics;
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
/** A demonstration of Java2D shapes */
public class Shapes implements GraphicsExample {
static final int WIDTH = 725, HEIGHT = 250;
// Size of our example
public String getName() {return "Shapes";}
// From GraphicsExample
public int getWidth() { return WIDTH; }
// From GraphicsExample
public int getHeight() { return HEIGHT; }
// From GraphicsExample
Shape[] shapes = new Shape[] {
// A straight line segment
new Line2D.Float(0, 0, 100, 100),
// A quadratic bezier curve. Two end points and one control point
new QuadCurve2D.Float(0, 0, 80, 15, 100, 100),
// A cubic bezier curve. Two end points and two control points
new CubicCurve2D.Float(0, 0, 80, 15, 10, 90, 100, 100),
// A 120 degree portion of an ellipse
new Arc2D.Float(-30, 0, 100, 100, 60, -120, Arc2D.OPEN),
// A 120 degree portion of an ellipse, closed with a chord
new Arc2D.Float(-30, 0, 100, 100, 60, -120, Arc2D.CHORD),
// A 120 degree pie slice of an ellipse
new Arc2D.Float(-30, 0, 100, 100, 60, -120, Arc2D.PIE),
// An ellipse
new Ellipse2D.Float(0, 20, 100, 60),
// A rectangle
new Rectangle2D.Float(0, 20, 100, 60),
// A rectangle with rounded corners
new RoundRectangle2D.Float(0, 20, 100, 60, 15, 15),
// A triangle
new Polygon(new int[] { 0, 0, 100 }, new int[] {20, 80, 80}, 3),
// A random polygon, initialized in code below
null,
// A spiral: an instance of a custom Shape implementation
new Spiral(50, 50, 5, 0, 50, 4*Math.PI),
};
{
// Initialize the null shape above as a Polygon with random points
Polygon p = new Polygon();
Search WWH ::




Custom Search