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]
# 1
Id advise splitting the String into a character array then displaying each letter with drawString... Not that efficient, but I tried... :)
ArikArikArika at 2007-7-8 1:25:45 > top of Java-index,Security,Cryptography...
# 2

Create your own method to do it.

public void drawVerticleString(Graphics g, String line)

{

//you do the rest

}

You can use a for loop.

CaptainMorgan08a at 2007-7-8 1:25:45 > top of Java-index,Security,Cryptography...
# 3

consider the following example:

if the orignal string is:

abcdef

hijklm

I would draw the string like:

ah

bi

cj

dk

el

fm

I think out an method:

reordering the charactres in the destination format and draw them horizontal.

No other good method?

Message was edited by:

Zjg

Zjga at 2007-7-8 1:25:45 > top of Java-index,Security,Cryptography...
# 4
but if blank occurs in the orignal string. things become diffcult......Message was edited by: Zjg
Zjga at 2007-7-8 1:25:45 > top of Java-index,Security,Cryptography...
# 5
You can use AffineTransform, but then you will get also the letters rotated not like you drew.for example I will become a horizonal line:I->_
Rodney_McKaya at 2007-7-8 1:25:45 > top of Java-index,Security,Cryptography...
# 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);

}

}

crwooda at 2007-7-8 1:25:45 > top of Java-index,Security,Cryptography...