Download this source file
/*
*/
// Jade.java - Java 2 app to show the drawing text with rotation
// and transpanency. Note the use of a Frame in main that allows
// Jade to be run as either a Java 2 applet or as an application.
// These sorts of text/graphic features enable good-looking programs
// and applets that don't require costly (and slow to download) GIFs
// or other images; instead the programs are small and fast!
// Note: Current browsers require or tags to force
// the browser to load the Java 2 Plug-in and not try to use the
// JVM (usually version 1.1.5) built into the browser. (AppletViewer
// recognizes all three tags just fine.)
// Adapted from "Java 2D Graphics", by Jonathan Knudsen, (C) 1999
// By O'Reilly & Assoc., pp. 121-122.
// Written by Wayne Pollock, Tampa Florida USA. (C) 1999.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Jade extends Applet
{
private static String msg1 = "java",
msg2 = " is fun!",
title = "Jade - Fancy Text Rendering Demo";
public static void main ( String [] args )
{
Frame frame = new Frame( title );
frame.setSize( 550, 260 );
//Center the Frame:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
int x = ( screenSize.width - frameSize.width ) / 2;
int y = ( screenSize.height - frameSize.height ) / 2;
frame.setLocation( x, y );
frame.setLayout( new BorderLayout() );
frame.addWindowListener( new WindowAdapter ()
{ public void windowClosing ( WindowEvent e )
{ System.exit( 0 ); }
}
);
Jade jade = new Jade();
frame.add( jade, "Center" );
frame.setVisible( true );
jade.init();
jade.start();
}
public void init ()
{
setBackground( Color.pink );
}
// Applet Info may be seen from the appletviewer's menu.
public String getAppletInfo ()
{
return title
+ " Version 1.0 \u00A9 1999 Wayne Pollock, "
+ "Tampa FL USA";
}
public void paint ( Graphics g )
{
// To use the new 2D Graphics API we must use a Graphics 2D object:
Graphics2D g2 = ( Graphics2D ) g;
// Set some special features on a Graphics2D, to impove the output:
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON );
// Set the font and color of the text:
g2.setFont( new Font( "Serif", Font.PLAIN, 80 ) );
g2.setColor( Color.green.darker().darker() );
// Transform the orgin to be centered near the bottom. This
// is tricky since we want to center the whole message. The
// height is no problem, just set it to 3/4 the height of the
// window. The width is 1/2 the free space, which is determined
// by subtracting the width of the message from the width of
// the frame. Note msg1 is drawn several times including nearly
// backwards, so it must be counted in the message width, but
// then the orgin must be put to the right of that extra.
// (The magic number 0.6 below is pulled from thin air, i.e.,
// determined by trial and error.):
int frameWidth = getSize().width, frameHeight = getSize().height;
FontMetrics fm = g2.getFontMetrics();
int msg1Width = fm.stringWidth( msg1 );
int msg2Width = fm.stringWidth( msg2 );
int msg1Extra = (int) ( 0.6 * msg1Width );
int leftPosition = msg1Extra + ( frameWidth -
( msg1Extra + msg1Width + msg2Width ) ) / 2;
// Translate the whole window so the 0,0 point (the orgin) refers
// to the point the text is centered around. (Later you can use
// the familar drawString method with coordinates of 0,0 and the
// text will apprear in the proper position.)
AffineTransform ct = AffineTransform.getTranslateInstance(
leftPosition, frameHeight * 3 / 4 );
g2.transform( ct );
// Save this original transformation:
AffineTransform old = g2.getTransform();
// Now draw some copies of msg1 using rotate transforms:
final int limit = 6;
for ( int i = 1; i <= limit; ++i )
{
// Draw the message limit times, evenly spaced around a half-circle.
// The ratio determines how far to rotate. Note in Java we don't
// draw text on some angle, we rotate the whole window and then
// draw text in a straight line:
float ratio = ( float ) i / ( float ) limit;
g2.transform( AffineTransform.getRotateInstance(
Math.PI * ( ratio - 1.0f ) ) );
float alpha = ( ( i == limit ) ? 1.0f : ratio / 3 );
// Set Compositing rul.: What should happen when drawing one pixel
// on top of another? Java 2 lets us pick a compositing rule:
g2.setComposite( AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha ) );
g2.drawString( msg1, 0, 0 ); // Finally, draw the text at the origin.
// Restore original (non-rotated) transformation:
g2.setTransform( old );
}
// Draw msg2. Its relative position is easy to determine:
g2.drawString( msg2, msg1Width, 0 );
} // End of paint method
}