/*PathIterator01.java 12/12/99
Copyright 1999, R.G.Baldwin
Illustrates use of the GeneralPath class and the
PathIterator class.
Draws a 4-inch by 4-inch Frame on the screen.
Translates the orgin to the center of the Frame.
Draws a pair of X and Y-axes centered on the new origin.
Draws a GeneralPath object on the Frame. The object is
a diamond whose vertices are at plus and minus one-half
inch on each of the axes. The vertices are located at
the N, S, E, and W positions.
Uses getPathIterator() to get a PathIterator on the
GeneralPath (diamond) object.
Extracts information from the PathIterator to populate
another GeneralPath object that is offset by one inch
from the original in both the X and Y directions.
Draws the new GeneralPath object in red.
Tested using JDK 1.2.2 under WinNT Workstation 4.0
*******************************************/
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
class PathIterator01{
public static void main(String[] args){
GUI guiObj = new GUI();
}//end main
}//end controlling class PathIterator01
class GUI extends Frame{
int res;//store screen resolution here
static final int ds = 72;//default scale, 72 units/inch
static final int hSize = 4;//horizonal size = 4 inches
static final int vSize = 4;//vertical size = 4 inches
GUI(){//constructor
//Get screen resolution
res = Toolkit.getDefaultToolkit().
getScreenResolution();
//Set Frame size
this.setSize(hSize*res,vSize*res);
this.setVisible(true);
this.setTitle("Copyright 1999, R.G.Baldwin");
//Window listener to terminate program.
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
}//end constructor
//Override the paint() method
public void paint(Graphics g){
//Downcast the Graphics object to a Graphics2D object
Graphics2D g2 = (Graphics2D)g;
//Scale device space to produce inches on the screen
// based on actual screen resolution.
g2.scale((double)res/72,(double)res/72);
//Translate the origin to the center of the Frame
g2.translate((hSize/2)*ds,(vSize/2)*ds);
//Draw x-axis
g2.draw(new Line2D.Double(-1.5*ds,0.0,1.5*ds,0.0));
//Draw y-axis
g2.draw(new Line2D.Double(0.0,-1.5*ds,0.0,1.5*ds));
//Use the GeneralPath class to instantiate a diamond
// object whose vertices are at plus and minus
// one-half inch on each of the axes. The
// vertices in the N, S, E, and W positions, centered
// about the origin.
GeneralPath thePath = new GeneralPath();
thePath.moveTo(0.5f*ds,0.0f*ds);
thePath.lineTo(0.0f*ds,0.5f*ds);
thePath.lineTo(-0.5f*ds,0.0f*ds);
thePath.lineTo(0.0f*ds,-0.5f*ds);
thePath.closePath();
//Now draw the diamond on the screen in black
g2.draw(thePath);
//Get a PathIterator object on the diamond
PathIterator theIterator =
thePath.getPathIterator(null);
//Use this array to store segment coordinate data
float[] theData = new float[6];
int theType;//store type of segment here
//Get a new GeneralPath object. Populate it based on
// coordinates and segment types extracted from the
// original GeneralPath object but offset the
// coordinate values by one inch in both X and Y.
GeneralPath newPath = new GeneralPath();
//Iterate on the original GeneralPath object and
// get information to populate the new
// GeneralPath object.
while(!theIterator.isDone()){//while not done
//Get type of segment and coordinate values
// for the current segment
theType = theIterator.currentSegment(theData);
//Process the current segment to populate a new
// segment of the new GeneralPath object.
switch(theType){
case PathIterator.SEG_MOVETO :
System.out.println("SEG_MOVETO");
newPath.moveTo(theData[0]+1.0f*ds,
theData[1]+1.0f*ds);
break;
case PathIterator.SEG_LINETO :
System.out.println("SEG_LINETO");
newPath.lineTo(theData[0]+1.0f*ds,
theData[1]+1.0f*ds);
break;
case PathIterator.SEG_QUADTO :
System.out.println("Not supported here");
//Will illustrate SEG_QUADTO in later lesson
break;
case PathIterator.SEG_CUBICTO :
System.out.println("Not supported here");
//Will illustrate SEG_CUBICTO in later lesson
break;
case PathIterator.SEG_CLOSE :
System.out.println("SEG_CLOSE");
newPath.closePath();
break;
}//end switch
//Get the next segment
theIterator.next();
}//end while loop
g2.setColor(Color.red);
g2.draw(newPath);
}//end overridden paint()
}//end class GUI
//==================================//
Figure 17
|