PDA

View Full Version : Rotating String/Text


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 &lt;APPLET&gt; tag but isn't running the applet, for some reason."
Your browser is completely ignoring the &lt;APPLET&gt; tag!
</applet>
<hr>
<a href="RotatedStringApplet.java">The source</a>.
</body>
</html>
.............................. RotatedString.html ...................................

Aradon
06-24-2007, 05:54 PM
I'm confused about what you mean when you say rotated text. Do you mean text at 90 degree's? Backwards?

Joern
06-24-2007, 11:11 PM
Hello Aragon

Yes - I mean rotated text - by 90°.
In the mean time, I had a look in other forums as well and there I fond the solution for the problem: I have to do first the transformation and then the drawing of the text. This is just the other way round as I think it should happen.

So the last block of my code has to be changed to:

AffineTransform at = new AffineTransform();
at.rotate( Math.PI / 2.0, (titleWidth + titleHeight * 2 /3) / 2, (titleWidth - titleHeight * 2 / 3 )/2 );
g2.transform( at );
new TextLayout( title, titleFont, g2.getFontRenderContext()).draw( g2, 0, 0 );
g2.dispose();
g.drawImage( bImg, globalSizeX / 2 - titleWidth, globalSizeY / 2 - titleWidth, this );