Sei sulla pagina 1di 13

Usage

Arcs can be drawn in an applet by instancing the Arc2D class (using a


constructor from either the Arc2D.Float or the Arc2D.Double inner classes,)
and then using the draw method of the java.awt.Graphics2D class. An OPEN
arc can be filled use the fill method of the java.awt.Graphics2D class.
Package
java.awt.geom
Common Constructors
Arc2D.Double ()
Creates an OPEN Arc2D with a size of (0, 0) at a location of (0, 0), and
angle (0, 0).
Arc2D.Double (int Type)
Creates an Arc2D of the specified Type with a size of (0, 0) at a location of
(0, 0), and angle (0, 0).
Arc2D.Double (double X, double Y, double Width, double Height, double
Theta, double Delta, int Type)
Creates an Arc2D of the specified Type with a size of (Width, Height) at
location (X, Y), and angle (Theta, Delta).
Arc2D.Float ()
Creates an OPEN Arc2D with a size of (0, 0) at a location of (0, 0), and
angle (0, 0).
Arc2D.Float (int Type)
Creates an Arc2D of the specified Type with a size of (0, 0) at a location of
(0, 0), and angle (0, 0).
Arc2D.Float (float X, float Y, float Width, float Height, float Theta, float Delta,
int Type)
Creates an Arc2D of the specified Type with a size of (Width, Height) at
location (X, Y), and angle (Theta, Delta).
Common Methods
contains (double Xp, double Yp)
Returns true iff the point (Xp, Yp) is within the Arc.
contains (double X, double Y, double Width, double Height)
Returns true iff the Arc (X, Y, Width, Height) is entirely within the Arc.
intersects (double X, double Y, double Width, double Height)
Returns true iff the Arc (X, Y, Width, Height) intersects the Arc.
setArc (double X, double Y, double Width, double Height, double Theta,
double Delta, int Type)
Sets the location, bounding size, arc size, and type of the arc.
Arguments
Delta
The size of the arc, in rectangular degrees.
Height
Vertical size of the bounding rectangle of the Arc, in pixels
Width
Horizontal size of the bounding rectangle of the Arc, in pixels
Theta
The beginning angle of the arc, in rectangular degrees.
Type
One of Arc2D.OPEN, Arc2D.CHORD, Arc2D.PIE.
X
Horizontal position of upper left corner of the bounding rectangle, in pixels
Y
Vertical position of upper left corner of the bounding rectangle, in pixels
Xp
Horizontal location of the point, in pixels.
Yp
Vertical location of the point, in pixels.
Applet - Arc2D1

Source Code - Arc2D1.java


import java.applet.Applet;
import java.awt.*;
import java.awt.geom.*;

public class Arc2D1 extends Applet {

private Arc2D Pie;


private Arc2D Chord;
private Arc2D Arc;
private Arc2D Pie2;
private Arc2D Chord2;
private Arc2D Arc2;

public Arc2D1 () {
Pie = new Arc2D.Double ();
Pie.setArc (20., 30., 150., 130.,
90., 45., Arc2D.PIE);
Chord = new Arc2D.Double (20., 30., 150., 130.,
0., 60., Arc2D.CHORD);
Arc = new Arc2D.Double (20., 30., 150., 130.,
225., 90., Arc2D.OPEN);
Pie2 = new Arc2D.Double (200., 30., 150., 130.,
90., 45., Arc2D.PIE);
Chord2 = new Arc2D.Double (200., 30., 150., 130.,
0., 60., Arc2D.CHORD);
Arc2 = new Arc2D.Double (200., 30., 150., 130.,
225., 90., Arc2D.OPEN);
}

public void paint (Graphics g) {


Graphics2D g2D = (Graphics2D) g;
boolean Contained;
Contained = Arc.contains (80., 130.);
g2D.drawString ("contains = " + Contained, 20, 175);

Contained = Arc.contains (130., 160.);


g2D.drawString ("contains = " + Contained, 90, 195);

Contained = Chord.contains (140., 60.);


g2D.drawString ("contains = " + Contained, 120, 115);

Contained = Pie.contains (40., 60.);


g2D.drawString ("contains = " + Contained, 20, 115);

Contained = Arc2.intersects (235., 125., 20., 40.);


g2D.drawString ("intersects = " + Contained, 235, 175);

Contained = Arc2.contains (270., 125., 20., 10.);


g2D.drawString ("contains = " + Contained, 270, 195);

Contained = Chord2.intersects (330., 10., 20., 30.);


g2D.drawString ("intersects = " + Contained, 330, 115);

Contained = Pie2.contains (250., 40., 20., 20.);


g2D.drawString ("contains = " + Contained, 230, 115);

g2D.draw (Arc);
g2D.draw (Chord);
g2D.draw (Pie);
g2D.fill (Arc2);
g2D.fill (Chord2);
g2D.fill (Pie2);

g2D.setColor (Color.green);
g2D.drawLine (130 - 10, 160, 130 + 10, 160);
g2D.drawLine (130, 160 - 10, 130, 160 + 10);
g2D.drawLine (80 - 10, 130, 80 + 10, 130);
g2D.drawLine (80, 130 - 10, 80, 130 + 10);
g2D.drawLine (140 - 10, 60, 140 + 10, 60);
g2D.drawLine (140, 60 - 10, 140, 60 + 10);
g2D.drawLine (40 - 10, 60, 40 + 10, 60);
g2D.drawLine (40, 60 - 10, 40, 60 + 10);
g2D.drawRect (235, 125, 20, 30);
g2D.drawRect (270, 125, 20, 10);
g2D.drawRect (330, 10, 20, 30);
g2D.drawRect (250, 40, 20, 30);
}

}
Usage:
Cubic curves can be drawn in an applet by instancing the CubicCurve2D
class (using a constructor from either the CubicCurve2D.Float or the
CubicCurve2D.Double inner classes,) and then using the draw method of the
java.awt.Graphics2D class. The curve is tangent to the line (X1, Y1) to (CX1,
CY1) at (X1, Y1), and is tangent to the line (X2, Y2) to (CX2, CY2) at (X2, Y2).

Package:
java.awt.geom

Common Constructors:
CubicCurve2D.Double ()
Creates a CubicCurve2D from (0, 0) to (0, 0), with control points (0,0) and
(0,0).
CubicCurve2D.Double (double X1, double Y1, double CX1,
double CY1, double CX2, double CY2, double X2, double Y2)
Creates a CubicCurve2D from (X1, Y1) to (X2, Y2), with control points
(CX1, CY1) and (CX2, CY2).
CubicCurve2D.Float ()
Creates a CubicCurve2D from (0, 0) to (0, 0), with control points (0,0) and
(0,0).
CubicCurve2D.Float (float X1, float Y1, float CX1, float
CY1, float CX2, float CY2, float X2, float Y2)
Creates a CubicCurve2D from (X1, Y1) to (X2, Y2), with control points
(CX1, CY1) and (CX2, CY2).

Common Methods:
intersects (double X, double Y, double Width, double Height)
Returns true iff the rectangle described by (X, Y, Width, Height) intersects
the CubicCurve.
setCurve (double X1, double Y1, double CX1, double CY1,
double CX2, double CY2, double X2, double Y2)
Sets the ends of the CubicCurve as (X1, Y1) and (X2, Y2).

Arguments:
CX1
Horizontal location of the first control point of the CubicCurve, in pixels.
CX2
Horizontal location of the second control point of the CubicCurve, in pixels.
CY1
Vertical location of the first control point of the CubicCurve, in pixels.
CY2
Vertical location of the second control point of the CubicCurve, in pixels.
Height
Vertical size of the bounding rectangle of the CubicCurve, in pixels
Width
Horizontal size of the bounding rectangle of the CubicCurve, in pixels
X
Horizontal position of upper left corner of the rectangle, in pixels
X1
Horizontal location of one end of the CubicCurve, in pixels.
X2
Horizontal location of the other end of the CubicCurve, in pixels.
Y
Vertical position of upper left corner of the rectangle, in pixels
Y1
Vertical location of one end of the CubicCurve, in pixels.
Y2
Vertical location of the other end of the CubicCurve, in pixels.

Example:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class CubicCurve2D1 extends JApplet {


private CubicCurve2D Shape1;
private CubicCurve2D Shape2;
private float X1;
private float Y1;
private float X2;
private float Y2;
private float CX1;
private float CY1;
private float CX2;
private float CY2;
private double DX1;
private double DY1;
private double DX2;
private double DY2;
private double DCX1;
private double DCY1;
private double DCX2;
private double DCY2;

public CubicCurve2D1 () {
X1 = 10.F;
Y1 = 20.F;
X2 = 150.F;
Y2 = 130.F;
CX1 = 20.F;
CY1 = 90.F;
CX2 = 140.F;
CY2 = 100.F;
DX1 = 160.;
DY1 = 20.;
DX2 = 230.;
DY2 = 40.;
DCX1 = 160.;
DCY1 = 120.;
DCX2 = 240.;
DCY2 = 90.;
Shape1 = new CubicCurve2D.Float (X1, Y1, CX1, CY1, CX2, CY2,
X2, Y2);
Shape2 = new CubicCurve2D.Double ();
Shape2.setCurve (DX1, DY1, DCX1, DCY1, DCX2, DCY2, DX2, DY2);
}

public void paint (Graphics g) {


Graphics2D g2D;

g2D = (Graphics2D) g;

g2D.setColor (Color.cyan);
g2D.drawLine ((int)X1, (int)Y1, (int)CX1, (int)CY1);
g2D.drawLine ((int)X2, (int)Y2, (int)CX2, (int)CY2);
g2D.drawLine ((int)DX1, (int)DY1, (int)DCX1, (int)DCY1);
g2D.drawLine ((int)DX2, (int)DY2, (int)DCX2, (int)DCY2);

g2D.setColor (Color.black);
g2D.draw (Shape1);
g2D.draw (Shape2);
}

}
Usage
Ellipses can be drawn in an applet by instancing the Ellipse2D class (using
a constructor from either the Ellipse2D.Float or the Ellipse2D.Double inner
classes,) and then using the draw method of the java.awt.Graphics2D class.
An ellipse can be filled using the fill method of the java.awt.Graphics2D
class.
Package
java.awt.geom
Common Constructors
Ellipse2D.Double ()
Creates a Ellipse2D with a size of (0, 0) at a location of (0, 0).
Ellipse2D.Double (double X, double Y, double Width, double Height)
Creates a Ellipse2D with a size of (Width, Height) at location (X, Y).
Ellipse2D.Float ()
Creates a Ellipse2D with a size of (0, 0) at a location of (0, 0).
Ellipse2D.Float (float X, float Y, float Width, float Height)
Creates a Ellipse2D with a size of (Width, Height) at location (X, Y).
Common Methods
contains (double Xp, double Yp)
Returns true iff the point (Xp, Yp) is within the Ellipse.
contains (double X, double Y, double Width, double Height)
Returns true iff the Ellipse (X, Y, Width, Height) is entirely within the
Ellipse.
intersects (double X, double Y, double Width, double Height)
Returns true iff the Ellipse (X, Y, Width, Height) intersects the Ellipse.
setFrame (double X, double Y, double Width, double Height)
Sets the location and size of the Ellipse.
Arguments
Height
Vertical size of the Ellipse, in pixels
Width
Horizontal size of the Ellipse, in pixels
X
Horizontal position of upper left corner, in pixels
Y
Vertical position of upper left corner, in pixels
X1
Horizontal location of one end of the line, in pixels.
X2
Horizontal location of the other end of the line, in pixels.
Xp
Horizontal location of the point, in pixels.
Y1
Vertical location of one end of the line, in pixels.
Y2
Vertical location of the other end of the line, in pixels.
Yp
Vertical location of the point, in pixels.
Applet - Ellipse2D1

Source Code - Ellipse2D1.java


import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class Ellipse2D1 extends JApplet {

private Ellipse2D Shape1;


private Ellipse2D Shape2;
private Ellipse2D Shape3;
private Ellipse2D Shape4;

public Ellipse2D1 () {
Shape1 = new Ellipse2D.Double ();
Shape1.setFrame (20., 20., 100., 50.);
Shape2 = new Ellipse2D.Double (140., 20., 100., 50.);
Shape3 = new Ellipse2D.Float ();
Shape3.setFrame (260., 20., 100., 50.);
Shape4 = new Ellipse2D.Float (380.F, 20.F, 100.F, 50.F);
}

public void paint (Graphics g) {


Graphics2D g2D;
boolean Contained;

g2D = (Graphics2D) g;

g2D.draw (Shape1);
Contained = Shape1.contains (20., 20.);
g2D.drawString ("contains = " + Contained, 20, 90);

g2D.fill (Shape2);
Contained = Shape2.contains (160., 10., 30., 40.);
g2D.drawString ("contains = " + Contained, 140, 90);

g2D.draw (Shape3);
Contained = Shape3.intersects (280., 10., 30., 50.);
g2D.drawString ("intersects = " + Contained, 260, 90);

g2D.draw (Shape4);

g2D.setColor (Color.blue);
g2D.drawLine (20 - 10, 20, 20 + 10, 20);
g2D.drawLine (20, 20 - 10, 20, 20 + 10);
g2D.drawRect (160, 10, 30, 40);
g2D.drawRect (280, 10, 30, 50);
}

}
Usage:
Arbitrary shapes can be drawn in an applet by instancing the GeneralPath
class, and then using the draw method or the fill method of the
java.awt.Graphics2D class.
Package:
java.awt.geom
Subclass of
Java 1.2-1.5
java.lang.Object
java.awt.geom.GeneralPath (implements Shape, Clonable)

Java 6
java.lang.Object
java.lang.Path3D (implements Shape, Clonable)
java.lang.Path3D.Float (implements Serializable)
java.awt.geom.GeneralPath
Common Constructors:
GeneralPath ()
Creates an empty GeneralPath
GeneralPath (Shape S)
Creates a GeneralPath from the Shape
Common Methods:
void lineTo (float X, float Y)
Adds a point to the path by drawing a straight line from the current
coordinates to the new specified coordinates.
void moveTo (float X, float Y)
Adds a point to the path by moving to the specified coordinates.
Arguments:
X
Horizontal position of the point, in pixels
Y
Vertical position of the point, in pixels
Example:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.GeneralPath;

public class GeneralPath1 extends JApplet {


private java.awt.geom.GeneralPath Shape1;
private java.awt.geom.GeneralPath Shape2;

public GeneralPath1 () {
Shape1 = new java.awt.geom.GeneralPath ();
Shape2 = new java.awt.geom.GeneralPath ();
Shape1.moveTo (50, 50);
Shape1.lineTo (50, 150);
Shape1.lineTo (200, 50);
Shape1.lineTo (200, 150);
Shape1.lineTo (50, 50);
Shape1.moveTo (75, 50);
Shape1.lineTo (175, 50);
Shape1.moveTo (75, 150);
Shape1.lineTo (175, 150);
Shape2.moveTo (250, 50);
Shape2.lineTo (250, 150);
Shape2.lineTo (400, 50);
Shape2.lineTo (400, 150);
Shape2.lineTo (250, 50);
Shape2.moveTo (275, 50);
Shape2.lineTo (375, 50);
Shape2.moveTo (275, 150);
Shape2.lineTo (375, 150);
}

public void paint (Graphics g) {


Graphics2D g2D;

g2D = (Graphics2D) g;

g2D.draw (Shape1);
g2D.fill (Shape2);
}

Usage:
Lines can be drawn in an applet by instancing the Line2D class (using a
constructor from either the Line2D.Float or the Line2D.Double inner classes,)
and then using the draw method of the java.awt.Graphics2D class.
Package:
java.awt.geom
Subclass of
java.lang.Object
javax.awt.geom.Line2D
Common Constructors:
Line2D.Double ()
Creates a Line2D from (0, 0) to (0, 0).
Line2D.Double (double X1, double Y1, double X2, double Y2)
Creates a Line2D from (X1, Y1) to (X2, Y2).
Line2D.Float ()
Creates a Line2D from (0, 0) to (0, 0).
Line2D.Float (float X1, float Y1, float X2, float Y2)
Creates a Line2D from (X1, Y1) to (X2, Y2).
Common Methods:
intersects (double X, double Y, double Width, double Height)
Returns true iff the rectangle described by (X, Y, Width, Height) intersects
the Line.
intersectsLine (double X1, double Y1, double X2, double Y2)
Returns true iff the line from (X1, Y1) to (X2, Y2) intersects the Line.
intersectsLine (Line2D L)
Returns true iff the line L intersects the Line.
setLine (double X1, double Y1, double X2, double Y2)
Sets the ends of the line as (X1, Y1) and (X2, Y2).
Arguments:
Height
Vertical size of the bounding rectangle of the Line, in pixels
Width
Horizontal size of the bounding rectangle of the Line, in pixels
X
Horizontal position of upper left corner of the rectangle, in pixels
X1
Horizontal location of one end of the line, in pixels.
X2
Horizontal location of the other end of the line, in pixels.
Y
Vertical position of upper left corner of the rectangle, in pixels
Y1
Vertical location of one end of the line, in pixels.
Y2
Vertical location of the other end of the line, in pixels.
Example:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class Line2D1 extends JApplet {


private Line2D Shape1;
private Line2D Shape2;

public Line2D1 () {
Shape1 = new Line2D.Float (20.F, 30.F, 150.F, 130.F);
Shape2 = new Line2D.Double ();
Shape2.setLine (20.F, 60.F, 120.F, 20.F);
}

public void paint (Graphics g) {


Graphics2D g2D;
boolean Hit;
g2D = (Graphics2D) g;

Hit = Shape1.intersectsLine (Shape2);


g2D.drawString ("intersects = " + Hit, 20, 145);

g2D.draw (Shape1);
g2D.draw (Shape2);
}

Usage:
Points can be represented in an applet by instancing the Point2D class
(using a constructor from either the Point2D.Float or the Point2D.Double inner
classes.) Points do not implement the Shape interface, and cannot be drawn.
Points can be used to set locations of Shapes.

Package:
java.awt.geom

Common Constructors:
Point2D.Double ()
Creates a Point2D at (0, 0).
Point2D.Double (double X, double Y)
Creates a Point2D at (X, Y).
Point2D.Float ()
Creates a Point2D at (0, 0).
Point2D.Float (float X, float Y)
Creates a Point2D at (X, Y).

Common Methods:
getX ()
Returns the horizontal coordinate of the point.
getY ()
Returns the verticle coordinate of the point.
setLocation (double X, double Y)
Sets the location of the Point as (X, Y).

Arguments:
X
Horizontal location of the point, in pixels
Y
Vertical location of the point, in pixels
Example:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class Point2D1 extends JApplet {


private Line2D Shape1;
private Line2D Shape2;
private Point2D Point1;
private Point2D Point2;
private Point2D Point3;
private Point2D Point4;

public Point2D1 () {
Point1 = new Point2D.Float (20.F, 30.F);
Point2 = new Point2D.Float (150.F, 130.F);
Point3 = new Point2D.Double (20., 60.);
Point4 = new Point2D.Double ();
Point4.setLocation (120., 20.);
Shape1 = new Line2D.Float (Point1, Point2);
Shape2 = new Line2D.Double ();
Shape2.setLine (Point3, Point4);
}

public void paint (Graphics g) {


Graphics2D g2D;
boolean Hit;

g2D = (Graphics2D) g;

Hit = Shape1.intersectsLine (Shape2);


g2D.drawString ("intersects = " + Hit, 20, 145);

g2D.draw (Shape1);
g2D.draw (Shape2);
}

Potrebbero piacerti anche