How to draw string vertically?
Hi,
The java drawString() method draws string horizontally by default, but I want to draw the string vertically, see an example below: I want to draw the string "abcdef" as below format:
a
b
c
d
e
f
so, I thought of the AffineTransform, but I can not work it out.
Who can help me? If you have other ways, they are welcome too.
Thanks. Robin
[409 byte] By [
Zjga] at [2007-11-26 13:49:19]

# 6
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class VText {
public static void main(String[] args) {
int w = 200, h = 300;
BufferedImage image = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setBackground(UIManager.getColor("OptionPane.background"));
g2.clearRect(0,0,w,h);
g2.setPaint(Color.black);
Font font = g2.getFont().deriveFont(18f);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
String text = "Hello World";
g2.drawString(text, 50, h - 25);
String[] s = text.split("(?<=[\\w\\s])");
float y = 25f;
for(int j = 0; j < s.length; j++) {
float width = (float)font.getStringBounds(s[j], frc).getWidth();
LineMetrics lm = font.getLineMetrics(s[j], frc);
float x = (w - width)/2;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
g2.setFont(font.deriveFont(at));
g2.drawString(s[j], 0, 0);
y += lm.getAscent();// - lm.getDescent();
}
g2.dispose();
JOptionPane.showMessageDialog(null, new ImageIcon(image), "",
JOptionPane.PLAIN_MESSAGE);
}
}