Draw vertical text in Java

Description

The following code shows how to draw vertical text.

Example


import java.awt.Font;
import java.awt.Graphics;
/*from w ww. j a v  a2 s.  c om*/
import javax.swing.JComponent;
import javax.swing.JFrame;

class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    String s = "java2s.com";

    Font font = new Font("Georgia", Font.ITALIC, 50);
    g.setFont(font);
    int v = g.getFontMetrics(g.getFont()).getHeight() + 1;
    int j = 0;
    int k = s.length();
    while (j < k + 1) {
      if (j == k)
        g.drawString(s.substring(j), 10, 10 + (j * v));
      else
        g.drawString(s.substring(j, j + 1), 10, 10 + (j * v));
      j++;
    }
  }
}

public class Main {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 450, 450);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}

The code above generates the following result.

Draw vertical text in Java




















Home »
  Java Tutorial »
    Graphics »




Animation
BufferedImage
Color
Font
Gradient
Graphics Settings
Image
Mouse Draw
Print
Shape
Text
Transform