Help with writing string vertically

O

oswaldt

I am trying to create a graph in Java and want to draw the x-axis
labels vertically. So far I have not been able to do this. I have
written a small test program for working though it. Here is my code.

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

public class test
{
public test()
{
BufferedImage bi = new
BufferedImage(20,200,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0,0,20,200);
g2.setColor(Color.BLACK);
g2.setFont(new Font("Arial",Font.PLAIN,10));
g2.rotate(Math.toRadians(-90));
g2.drawString("Testing this image",0,10);
try
{
File f = new File("test.png");
f.createNewFile();
ImageIO.write(bi,"png",f);
}
catch (IOException e) {}
}
public static void main(String[] args)
{
test t = new test();
}
}


It creates the image but the text does not appear, if I remove the
g2.rotate from the equation and increase the width of the image the
text appears. I am sure there is a simple solution I just don't have
that much experiece with Java let alone graphics programming. If
someone could point out the solution that would be great. I was
planning on taking this iage and then drawing that image on the graph
image. By the way I am using JDK 1.4.2.

thank you,
Tyson
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

I am trying to create a graph in Java and want to draw the x-axis
labels vertically. So far I have not been able to do this. I have
written a small test program for working though it. Here is my code.
Graphics2D g2 = bi.createGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0,0,20,200);
g2.setColor(Color.BLACK);
g2.setFont(new Font("Arial",Font.PLAIN,10));
g2.rotate(Math.toRadians(-90));
g2.drawString("Testing this image",0,10);
It creates the image but the text does not appear, if I remove the
g2.rotate from the equation and increase the width of the image the
text appears. I am sure there is a simple solution I just don't have
that much experiece with Java let alone graphics programming.

The problem here is that the Graphics2D.rotate(doulbe) method always
(QUOTE) Concatenates the current Graphics2D Transform with a rotation
transform. Subsequent rendering is rotated by the specified radians
relative to the previous origin.(UNQUOTE)

Unless you have applied other transforms before rotate, the previous
origin will be the point 0,0 in the upper left corner. All rotation is
then done around that point. In your case, that is not what you desire;
the rotation must be done around one of the corners of the 'rectangle'
that the String is drawn in. So you should use the rotate(double,
double, double) method in Graphics2D instead.

For example:

g2.rotate(Math.PI / 2, 10, 10);
g2.drawString("Testing this image (text goes down)", 10, 10);

This will rotate around the lower left corner of the text, with the text
going down. Another example:

// height should be height of image
g2.rotate(3 * Math.PI / 2, 10, height - 10);
g2.drawString("Testing this image (text goes up)", 10, height - 10);

Again, this is rotation around the lower left corner of the text, but
because the angle is changed, the text will now go up. Because we are
rotating around the lower left corner 90 degrees counter clockwise, we
must also make sure we draw the text in the lower half of the image.

I hope that gets you started. A thing you should also remember (as this
is far from obvious) is that different transforms stack. So doing one
rotation after another will not cancel the previous rotations.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top