Joern
06-23-2007, 02:57 PM
Hello
I'm a freebie to Java and to this forum, so have mercy with me.
I want to draw rotated text in a canvas, but I do not succeed.
I only get the original (not rotated text) in the output (Mozilla 2.0, but it doesn't do it in the IE6 too).
Can anyone help me, and tell me what I'm doing wrong in the given example:
.............................. java code .........................................
/*
* @(#)RotatedStringApplet.java 0.1 2007-06-23
*/
import java.awt.*;
import java.text.*;
import java.util.Locale;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
public class RotatedStringApplet extends java.applet.Applet
{
private String title;
private Font titleFont;
private FontMetrics titleMetrics;
private int titleHeight, titleWidth;
private int globalSizeX, globalSizeY;
private BufferedImage bImg;
private Graphics2D g2;
public void init()
{
globalSizeY = getSize().height;
globalSizeX = getSize().width;
title = "It's just a Test";
titleFont = new java.awt.Font( "Monospaced", Font.BOLD, 18 );
titleMetrics = getFontMetrics( titleFont );
titleHeight = titleMetrics.getHeight();
titleWidth = titleMetrics.stringWidth( title );
}
public void paint( Graphics g )
{
setBackground( Color.lightGray );
g.setColor( Color.black );
if ( bImg == null || bImg.getWidth() != 2 * titleWidth )
{
bImg = (BufferedImage) createImage( 2 * titleWidth, 2 * titleWidth );
g2 = bImg.createGraphics();
g2.setBackground( Color.gray );
g2.clearRect( 0, 0, 2 * titleWidth, 2 * titleWidth );
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
}
AffineTransform at = new AffineTransform();
//AffineTransform at = AffineTransform.getTranslateInstance( 10.0, 0.0 );
new TextLayout( title, titleFont, g2.getFontRenderContext()).draw( g2, (float)titleWidth, (float)titleWidth );
at.setToRotation( Math.PI / 2.0 );
//at.translate( 10.0, 0.0 );
//at.rotate( Math.PI / 2.0, 0, 0 );
g2.transform( at );
//g2.translate( 10.0, 0.0 );
g2.dispose();
g.drawImage( bImg, globalSizeX / 2 - titleWidth, globalSizeY / 2 - titleWidth, this );
}
}
.............................. java code end .........................................
.............................. RotatedString.html ...................................
<html>
<head>
<title>Rotated String</title>
</head>
<body>
<h1>RotatedString</h1>
<hr>
<applet code=RotatedStringApplet.class width=800 height=500>
alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."
Your browser is completely ignoring the <APPLET> tag!
</applet>
<hr>
<a href="RotatedStringApplet.java">The source</a>.
</body>
</html>
.............................. RotatedString.html ...................................
I'm a freebie to Java and to this forum, so have mercy with me.
I want to draw rotated text in a canvas, but I do not succeed.
I only get the original (not rotated text) in the output (Mozilla 2.0, but it doesn't do it in the IE6 too).
Can anyone help me, and tell me what I'm doing wrong in the given example:
.............................. java code .........................................
/*
* @(#)RotatedStringApplet.java 0.1 2007-06-23
*/
import java.awt.*;
import java.text.*;
import java.util.Locale;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
public class RotatedStringApplet extends java.applet.Applet
{
private String title;
private Font titleFont;
private FontMetrics titleMetrics;
private int titleHeight, titleWidth;
private int globalSizeX, globalSizeY;
private BufferedImage bImg;
private Graphics2D g2;
public void init()
{
globalSizeY = getSize().height;
globalSizeX = getSize().width;
title = "It's just a Test";
titleFont = new java.awt.Font( "Monospaced", Font.BOLD, 18 );
titleMetrics = getFontMetrics( titleFont );
titleHeight = titleMetrics.getHeight();
titleWidth = titleMetrics.stringWidth( title );
}
public void paint( Graphics g )
{
setBackground( Color.lightGray );
g.setColor( Color.black );
if ( bImg == null || bImg.getWidth() != 2 * titleWidth )
{
bImg = (BufferedImage) createImage( 2 * titleWidth, 2 * titleWidth );
g2 = bImg.createGraphics();
g2.setBackground( Color.gray );
g2.clearRect( 0, 0, 2 * titleWidth, 2 * titleWidth );
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
}
AffineTransform at = new AffineTransform();
//AffineTransform at = AffineTransform.getTranslateInstance( 10.0, 0.0 );
new TextLayout( title, titleFont, g2.getFontRenderContext()).draw( g2, (float)titleWidth, (float)titleWidth );
at.setToRotation( Math.PI / 2.0 );
//at.translate( 10.0, 0.0 );
//at.rotate( Math.PI / 2.0, 0, 0 );
g2.transform( at );
//g2.translate( 10.0, 0.0 );
g2.dispose();
g.drawImage( bImg, globalSizeX / 2 - titleWidth, globalSizeY / 2 - titleWidth, this );
}
}
.............................. java code end .........................................
.............................. RotatedString.html ...................................
<html>
<head>
<title>Rotated String</title>
</head>
<body>
<h1>RotatedString</h1>
<hr>
<applet code=RotatedStringApplet.class width=800 height=500>
alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."
Your browser is completely ignoring the <APPLET> tag!
</applet>
<hr>
<a href="RotatedStringApplet.java">The source</a>.
</body>
</html>
.............................. RotatedString.html ...................................