Vertical draw text not working in Graphics2D?

K

Kevin

Hello All,
It supposed to be a simple problem but I just can't get it working:

The Mission:
to draw a text vertically on a JPanel.

The Code:
// in paintComponent or any other places such as in response to a mouse
click.
Graphics2D g2d = (Graphics2D) g;
AffineTransform at = new AffineTransform();
at.setToRotation(-Math.PI/2.0);
g2d.setTransform(at);
g2d.drawString("tttttt", 10, 10);

////
:-( it does NOT work. No text is drawn.
I tried and if I remove the line g2d.setTransform(at), then the text
will be drawn as expected (non vertical). But if I add the
setTransform, nothing shows up.

Also, I have tried the rotate functions, still not work.
Any one know any possible reasons?
Thanks a lot.

PS: my code is taken from
http://javaalmanac.com/egs/java.awt/RotateText.html
 
B

Boudewijn Dijkstra

Kevin said:
Hello All,
It supposed to be a simple problem but I just can't get it working:

The Mission:
to draw a text vertically on a JPanel.

The Code:
// in paintComponent or any other places such as in response to a mouse
click.
Graphics2D g2d = (Graphics2D) g;
AffineTransform at = new AffineTransform();
at.setToRotation(-Math.PI/2.0);
g2d.setTransform(at);

read the documentation for setTransform: "WARNING: This method should never be
used to apply a new coordinate transform on top of an existing transform
because the Graphics2D might already have a transform that is needed for other
purposes, such as rendering Swing components or applying a scaling
transformation to adjust for the resolution of a printer."
g2d.drawString("tttttt", 10, 10);

////
:-( it does NOT work. No text is drawn.

Maybe the text is drawn somewhere where you can't see it. You see, the
coordinates passed to the drawString method are also affected by the rotate
transform. To draw vertical text at (10, 10), you have to do this:

g2d.translate(10, 10);
g2d.rotate(-Math.PI/2.0);
g2d.drawString("tttttt", 0, 0);
 
C

Chris Uppal

Kevin said:
AffineTransform at = new AffineTransform();
at.setToRotation(-Math.PI/2.0);

My visualisation of these things never been good, but I believe that this is
defining a rotation by 90 degrees anti-clockwise around the top left corner.

-- chris
 
K

Kevin

Thanks a lot for the hint. Yes, I found out the rotate and transform
are applied to the whole coordinates.
In an other thread,
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#03f8badca78539b0
I found this way by Richard Freedman works nice, hope it help. See
below:

What I did instead was to derive a font with the desired transform, and
then use
that font in a drawText call, thusly:


private void label_line(Graphics g, double x, double y, double theta,
String label) {


Graphics2D g2D = (Graphics2D)g;


// Create a rotation transformation for the font.
AffineTransform fontAT = new AffineTransform();


// get the current font
Font theFont = g2D.getFont();


// Derive a new font using a rotatation transform
fontAT.rotate(theta);
Font theDerivedFont = theFont.deriveFont(fontAT);


// set the derived font in the Graphics2D context
g2D.setFont(theDerivedFont);


// Render a string using the derived font
g2D.drawString(label, (int)x1, (int)y1);
// put the original font back
g2D.setFont(theFont);



}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top