Java2D provides an enormous range of character display capabilities.
At one end of the range, the glyphs, i.e. the actual shapes of particular
characters in particular fonts, can be manipulated.
At the other end, text layout tools allow one to control such features
as cursors and highlighting so that custom editors can be created.
In the two demos below, we show some of these text techniques.
The first applet illustrates how individual characters can be rendered
with particular attributes - font, size, color, etc. The AttributedString
object holds the string and you can use its methods to set the attributes
for for each character.
IteratorTextApplet.java
|
import
javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.text.*;
import java.awt.font.*;
/** Demonstration of text interator in Java2D. **/
public class IteratorTextApplet extends JApplet
{
public void init() {
Container content_pane = getContentPane();
// Create an instance of DrawingPanel
IteratorTextPanel iterator_text_panel
=
new IteratorTextPanel();
// Add the DrawingPanel to the
content pane.
content_pane.add(iterator_text_panel);
} //init
} // class IteratorTextApplet
class IteratorTextPanel extends JPanel
{
public void paintComponent(Graphics g) {
// First paint background
super.paintComponent(g);
// The context that is passed
is actually
// the Graphics2D sub-class of
Graphics. So
// we can cast it to Graphics2D
and take advantage
// of the additional tools that
it has.
Graphics2D g2 = (Graphics2D)g;
// Options on the rendering, i.e.
the actual pixel settings
// of the drawing, can be chosen
via RenderingHints
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Get two different font objects
Font serifFont = new Font("Serif",
Font.PLAIN, 56);
Font sansSerifFont = new Font("Monospaced",
Font.ITALIC, 56);
String s = "Java2D Does Text!";
// An AttributeString allows each
element of the string
// to be given its own font, color,
etc.
// Create an AttributeString from
our String
AttributedString attStr = new
AttributedString(s);
// Set the font for the whole
string
attStr.addAttribute(TextAttribute.FONT,
serifFont);
// Make the 4th & 5th
characters blue
attStr.addAttribute(TextAttribute.FOREGROUND,
Color.blue, 4, 6);
// Make characters
7 to 10 with a different font.
attStr.addAttribute(TextAttribute.FONT,
sansSerifFont, 7, 11);
// and also make them
red.
attStr.addAttribute(TextAttribute.FOREGROUND,
Color.red, 7, 11);
// drawString in Java2D knows
how to step through the
// AttributeString character by
character and render each
// one according to its attribute
settings.
g2.drawString(attStr.getIterator(),
15, 65);
} // paintComponent
} // class IteratorTextPanel
|
In the next example, we show how to draw text
with an image tiled to provide a texture for the filling to
the glyph segments. (See also the previous texture
example.)
TexturedTextApplet.java |
import
javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.text.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
/** Demonstration of drawing text with textured in Java2D.**/
public class TexturedTextApplet extends JApplet
{
public void init () {
Container content_pane = getContentPane
();
// Create an instance of DrawingPanel
TexturedTextPanel textured_text_panel
=
new TexturedTextPanel
(getBufImage ());
// Add the DrawingPanel to the
contentPane.
content_pane.add (textured_text_panel);
} // init
BufferedImage getBufImage () {
// Get the image for the texture.
java.net.URL url=null;
try {
url = new java.net.URL
(getCodeBase (),
"Apollo16Lander.jpg");
} catch (java.net.MalformedURLException
e) {
String
msg = "Error loading image Apollo16Lander.gif";
System.err.println
(msg);
showStatus
(msg);
System.exit
(0);
}
// Grab an image using this trick
with ImageIcon which
// doesn't return until the image
is loaded.
Image img = new javax.swing.ImageIcon
(url).getImage ();
// Create a BufferedImage object
for the texture class.
// Should be same size as the
Image
BufferedImage buf_image =
new BufferedImage (img.getWidth
(null),
img.getHeight
(null),
BufferedImage.TYPE_INT_RGB);
// Get the (offscreen)
graphics context for this image
Graphics2D g2 = buf_image.createGraphics
();
// Draw the image onto the context (remember
that this is
// all offscreen.
g2.drawImage (img,null,null);
return buf_image;
} // getBufImage
} // class TexturedTextApplet
/** Panel on which to draw the textured text. **/
class TexturedTextPanel extends JPanel
{
BufferedImage fBufImage;
TexturedTextPanel (BufferedImage bImg) {
fBufImage = bImg;
} // ctor
public void paintComponent (Graphics g) {
// First paint background
super.paintComponent (g);
// The context that is passed
is actually
// the Graphics2D sub-class of
Graphics. So
// we can cast it to Graphics2D
and take advantage
// of the additional tools that
it has.
Graphics2D g2 = (Graphics2D)g;
// Options on the rendering, i.e.
the actual pixel settings
// of the drawing, can be chosen
via RenderingHints
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Set the Graphics context font
to the usual Times New Roman
// with a big size.
Font font = new Font ("Times New
Roman", Font.PLAIN, 100);
g2.setFont (font);
// Create a texture rectangle
the same size
// as the texture image.
Rectangle2D tr = new Rectangle2D.Double
(
0, 0,
fBufImage.getWidth (),
fBufImage.getHeight ()
);
// Create the TexturePaint. Need
a BufferedImage and a rectangle
// to create the "tile" that will
be used for the texture.
TexturePaint tp = new TexturePaint
(fBufImage, tr);
// Set out paint to this texture
and then draw the string.
g2.setPaint (tp);
String s = "Java2D";
g2.drawString (s, 20, 110);
} // paintComponent
} // class TexturedTextPanel
|
Latest update: Oct. 27, 2004
|