//Comment out the following package statement to compile separately.
package com.javaworld.media.j2d;
/**
* Example06 illustrates some basics of Java 2D.
* This version is compliant with Java 1.2 Beta 3, Jul 1998.
* Please refer to:
* http://www.javaworld.com/javaworld/jw-08-1998/jw-08-media.html
*
* @author Bill Day
* @version 1.0
* @see java.awt.Font
* @see java.awt.Graphics2D
* @see java.awt.Shape
* @see java.awt.font.StyledString
* @see java.awt.geom.AffineTransform
* @see java.awt.geom.GeneralPath
**/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
public class Example06 extends Frame {
/**
* Instantiates an Example06 object.
**/
public static void main(String args[]) {
new Example06();
}
/**
* Our Example06 constructor sets the frame's size, adds the
* visual components, and then makes them visible to the user.
* It uses an adapter class to deal with the user closing
* the frame.
**/
public Example06() {
//Title our frame.
super("Java 2D Example06");
//Set the size for this frame.
setSize(330,270);
//We need to turn on the visibility of our frame
//by setting the Visible parameter to true.
setVisible(true);
//Now, we want to be sure we properly dispose of resources
//this frame is using when the window is closed. We use
//an anonymous inner class adapter for this.
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{dispose(); System.exit(0);}
}
);
}
/**
* The paint method is where the real magic is. We use
* StyledString.getStringOutline() to get the Shape of
* some text, which we then use to clip our GeneralPath.
**/
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
//Again, we use anti-aliasing if possible.
g2d.setRenderingHints(Graphics2D.ANTIALIASING,
Graphics2D.ANTIALIAS_ON);
//We reuse our GeneralPath filled shape. We translate
//and rotate this shape as we did before.
GeneralPath path = new GeneralPath(GeneralPath.EVEN_ODD);
path.moveTo(0.0f,0.0f);
path.lineTo(0.0f,125.0f);
path.quadTo(100.0f,100.0f,225.0f,125.0f);
path.curveTo(260.0f,100.0f,130.0f,50.0f,225.0f,0.0f);
path.closePath();
AffineTransform at = new AffineTransform();
at.setToRotation(-Math.PI/8.0);
g2d.transform(at);
at.setToTranslation(0.0f,150.0f);
g2d.transform(at);
//Now, let's use some of the Java font and text support.
//Note that you need to be sure you have the same fonts I
//use in the example (Times New Roman True Type) if you
//execute this example code.
Font exFont = new Font("TimesRoman",Font.PLAIN,80);
g2d.setFont(exFont);
//Now, we want to create a string and then use its outline
//to clip our green GeneralPath object drawn above. We
//do this by creating a StyledString, getting its StringOutline
//(which is a Shape object like GeneralPath), then setting
//the clipping shape to be the string and filling the path.
StyledString exString = new StyledString ("JavaWorld", exFont);
Shape exShape = exString.getStringOutline();
g2d.setClip(exShape);
g2d.setColor(Color.green);
g2d.fill(path);
}
}