Java Tutorial/2D Graphics/Draw Text — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:25, 31 мая 2010

Center text based on font metrics

// This example is from the book _Java AWT Reference_ by John Zukowski.
// Written by John Zukowski.  Copyright (c) 1997 O"Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Center extends JPanel {
    static String text[] = new String[]{"asdf","asdfasdf"};
    private Dimension dim;
    public void addNotify() {
        super.addNotify();
        int maxWidth = 0;
        FontMetrics fm = getFontMetrics(getFont());
        for (int i=0;i<text.length;i++) {
            maxWidth = Math.max (maxWidth, fm.stringWidth(text[i]));
        }
        Insets inset = getInsets();
        dim = new Dimension (maxWidth + inset.left + inset.right,
            text.length*fm.getHeight() + inset.top + inset.bottom);
        setSize (dim);
    }
    public void paint (Graphics g) {
        g.translate(100, 100);
        FontMetrics fm = g.getFontMetrics();
        for (int i=0;i<text.length;i++) {
            int x,y;
            x = (getWidth() - fm.stringWidth(text[i]))/2;
            y = (i+1)*fm.getHeight()-1;
            g.drawString (text[i], x, y);
        }
  
    }
    public static void main(String[] a){
      JFrame f = new JFrame();
      f.add(new Center());
      f.setSize(300,300);
      f.setVisible(true);
    } 
}





Create a shadowed text

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.TextLayout;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ShadowText extends JLabel {
  public ShadowText() {
  }
  public void paint(Graphics g) {
    String text = "Hello World";
    int x = 10;
    int y = 100;
    Font font = new Font("Georgia", Font.ITALIC, 50);
    Graphics2D g1 = (Graphics2D) g;
    TextLayout textLayout = new TextLayout(text, font, g1.getFontRenderContext());
    g1.setPaint(new Color(150, 150, 150));
    textLayout.draw(g1, x + 3, y + 3);
    g1.setPaint(Color.BLACK);
    textLayout.draw(g1, x, y);
  }
  public static void main(String[] args) throws IOException {
    JFrame f = new JFrame();
    f.add(new ShadowText());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(450, 150);
    f.setVisible(true);
  }
}





Display some lyrics on the panel.

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PrintLineByLine extends JPanel {
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHints(rh);
    g2d.setFont(new Font("Purisa", Font.PLAIN, 13));
    g2d.drawString("Line 1", 20, 30);
    g2d.drawString("Line 2", 20, 60);
    g2d.drawString("Line 3", 20, 90);
    g2d.drawString("Line 4", 20, 120);
    g2d.drawString("Line 5", 20, 150);
    g2d.drawString("Line 6", 20, 180);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.add(new PrintLineByLine());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(420, 250);
    frame.setVisible(true);
  }
}





Display underlined text

import java.awt.Graphics;
import javax.swing.JPanel;
public class Main extends JPanel{
  String s = "Underlined text";
  int x = 10;
  int y = 10;
  public void init() {
  }
  public void paint(Graphics g) {
    g.drawString(s, x, y);
    g.drawLine(x, y + 2, x + getFontMetrics(getFont()).stringWidth(s), y + 2);
  }
}





Display unicode text

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RussiaUnicode extends JPanel {
  public void paint(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawString("\u0424\u0451\u0434\u043e\u0440.", 20, 30);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Unicode");
    frame.add(new RussiaUnicode());
    frame.setSize(520, 220);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}





Display vertical text

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel{
  String s = "Vertical text";
  int v;
  public void paint(Graphics g) {
    v = g.getFontMetrics(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 static void main(String[] a){
    JFrame f = new JFrame();
    f.add(new Main());
    f.setSize(300,300);
    f.setVisible(true);
  }
}





draw Bytes

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setSize(400, 400);
    f.add(new MainClass());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
  public void paint(Graphics g) {
    byte[] b = { 0x65, (byte) "B", 0x66 };
    g.drawBytes(b, 0, b.length, 10, 30);
  }
}





Draw Chars

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GraphicsDrawChars extends JPanel {
  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setSize(400, 400);
    f.add(new GraphicsDrawChars());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
  public void paint(Graphics g) {
    char[] c = { "W", "a", "t", "c", "h", " ", "i", "t", " ", "D", "u", "k", "e", "!" };
    g.drawChars(c, 0, c.length, 10, 30);
  }
}





Drawing Rotated Text

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BasicDraw {
  public static void main(String[] args) {
    new BasicDraw();
  }
  BasicDraw() {
    JFrame frame = new JFrame();
    frame.add(new MyComponent());
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}
class MyComponent extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    
    g2d.drawString("aString", 100, 100);
    AffineTransform at = new AffineTransform();
    at.setToRotation(Math.PI / 4.0);
    g2d.setTransform(at);
    g2d.drawString("aString", 100, 100);
  }
}





Drawing Simple Text

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BasicDraw {
  public static void main(String[] args) {
    new BasicDraw();
  }
  BasicDraw() {
    JFrame frame = new JFrame();
    frame.add(new MyComponent());
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}
class MyComponent extends JComponent {
  public void paint(Graphics g) {
    Font font = new Font("Serif", Font.PLAIN, 12);
    g.setFont(font);
    g.drawString("a String", 10, 10);
    FontMetrics fontMetrics = g.getFontMetrics();
    g.drawString("aString", 10, 10 + fontMetrics.getAscent());
  }
}





Draw string rotated clockwise 45 degrees

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BasicDraw {
  public static void main(String[] args) {
    new BasicDraw();
  }
  BasicDraw() {
    JFrame frame = new JFrame();
    frame.add(new MyComponent());
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}
class MyComponent extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    
    g2d.drawString("aString", 100, 100);
    AffineTransform at = new AffineTransform();
    at.setToRotation(Math.PI / 4.0);
    g2d.setTransform(at);
    g2d.drawString("aString", 100, 100);
  }
}





Draw string rotated counter-clockwise 45 degrees

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BasicDraw {
  public static void main(String[] args) {
    new BasicDraw();
  }
  BasicDraw() {
    JFrame frame = new JFrame();
    frame.add(new MyComponent());
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}
class MyComponent extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    
    AffineTransform at = new AffineTransform();
    at.setToRotation(Math.PI/4.0);
    g2d.setTransform(at);
    g2d.drawString("aString", 200, 100);
  }
}





Draw unicode with Graphics2D

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
public class DrawStringI18N extends JFrame {
  public void paint(Graphics g) {
    Graphics2D graphics2D = (Graphics2D) g;
    GraphicsEnvironment.getLocalGraphicsEnvironment();
    graphics2D.setFont(new Font("LucidaSans", Font.PLAIN, 40));
    graphics2D.drawString("\u05E9\u05DC\u05D5\u05DD \u05E2\u05D5\u05DC\u05DD", 50, 75);
  }
  public static void main(String[] args) {
    JFrame frame = new DrawStringI18N();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200,200);
    frame.setVisible(true);
  }
}





Generate Shape From Text

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
/*
 * $Id: ShapeUtils.java,v 1.4 2008/10/14 22:31:46 rah003 Exp $
 *
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
public class Utils {
  public static Shape generateShapeFromText(Font font, char ch) {
    return generateShapeFromText(font, String.valueOf(ch));
  }
  public static Shape generateShapeFromText(Font font, String string) {
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = img.createGraphics();
    try {
      GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string);
      Shape shape = vect.getOutline(0f, (float) -vect.getVisualBounds().getY());
      return shape;
    } finally {
      g2.dispose();
    }
  }
}





Getting the Dimensions of Text

import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class BasicDraw {
  public static void main(String[] args) {
    new BasicDraw();
  }
  BasicDraw() {
    JFrame frame = new JFrame();
    frame.add(new MyComponent());
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}
class MyComponent extends JComponent {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    FontMetrics fontMetrics = g2d.getFontMetrics();
    int width = fontMetrics.stringWidth("aString");
    int height = fontMetrics.getHeight();
    System.out.println(width);
    System.out.println(height);
  }
}





How do I...Draw text?

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    g.setColor(Color.RED);
    g.drawString ("text", 20, 30);  
  }
}
public class DrawText {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}





Paint a calendar

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
  SimpleDateFormat month = new SimpleDateFormat("MMMM");
  SimpleDateFormat year = new SimpleDateFormat("yyyy");
  SimpleDateFormat day = new SimpleDateFormat("d");
  Date date = new Date();
  public void setDate(Date date) {
    this.date = date;
  }
  public void paintComponent(Graphics g) {
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.black);
    g.drawString(month.format(date), 34, 36);
    g.setColor(Color.white);
    g.drawString(year.format(date), 235, 36);
    Calendar today = Calendar.getInstance();
    today.setTime(date);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DATE, 1);
    cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 1);
    for (int week = 0; week < 6; week++) {
      for (int d = 0; d < 7; d++) {
        Color col = Color.black;
          g.drawString(day.format(cal.getTime()), d * 30 + 46 + 4,
              week * 29 + 81 + 20);
        cal.add(Calendar.DATE, +1);
      }
    }
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setPreferredSize(new Dimension(300, 280));
    Main ch = new Main();
    ch.setDate(new Date());
    frame.getContentPane().add(ch);
    frame.setUndecorated(true);
    frame.pack();
    frame.setVisible(true);
  }
}





TextAttribute.BACKGROUND: Set background for AttributedString

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TextAttributesBackground extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.BACKGROUND, Color.LIGHT_GRAY, 2, 9);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new TextAttributesBackground());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.FONT: Set font for AttributedString

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TextAttributesFont extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 40);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.FONT, font);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new TextAttributesFont());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.FOREGROUND: Set color for AttributedString

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TextAttributesColor extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.FOREGROUND, Color.red, 0, 2);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new TextAttributesColor());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.SIZE: Set SIZE for AttributedString

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TextAttributesSize extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.SIZE, 40);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new TextAttributesSize());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.STRIKETHROUGH: Set STRIKETHROUGH for AttributedString

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TextAttributesStrikeThrough extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 2, 8);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new TextAttributesStrikeThrough());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.SUPERSCRIPT: Set SUPERSCRIPT for AttributedString

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TextAttributesSuperscript extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 5, 7);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new TextAttributesSuperscript());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





TextAttribute.UNDERLINE: Set underline for AttributedString

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TextAttributesUnderline extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    AttributedString as1 = new AttributedString("1234567890");
    as1.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 3, 4);
    g2d.drawString(as1.getIterator(), 15, 60);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text attributes");
    frame.add(new TextAttributesUnderline());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(620, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





Use AffineTransform to draw vertical text

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform at = new AffineTransform();
    at.setToRotation(-Math.PI / 2.0, getWidth() / 2.0, getHeight() / 2.0);
    g2d.setTransform(at);
    g2d.drawString("Vertical text", 10, 10);
  }
  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.add(new Main());
    f.setSize(300, 300);
    f.setVisible(true);
  }
}





use Text Attributes to style text

/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.TextAttribute;
import java.util.Hashtable;
/**
 * This class demonstrates how to use Text Attributes to style text.
 */
public class AttributedText extends Applet {
  public void paint(Graphics g) {
    Font font = new Font(Font.SERIF, Font.PLAIN, 24);
    g.setFont(font);
    String text = "Too WAVY";
    g.drawString(text, 45, 30);
    Hashtable<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
    /* Kerning makes the text spacing more natural */
    map.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    font = font.deriveFont(map);
    g.setFont(font);
    g.drawString(text, 45, 60);
    /* Underlining is easy */
    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
    font = font.deriveFont(map);
    g.setFont(font);
    g.drawString(text, 45, 90);
    /* Strikethrouh is easy */
    map.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
    font = font.deriveFont(map);
    g.setFont(font);
    g.drawString(text, 45, 120);
    /* This colour applies just to the font, not other rendering */
    map.put(TextAttribute.FOREGROUND, Color.BLUE);
    font = font.deriveFont(map);
    g.setFont(font);
    g.drawString(text, 45, 150);
  }
  public static void main(String[] args) {
    Frame f = new Frame("Attributed Text Sample");
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    f.add("Center", new AttributedText());
    f.setSize(new Dimension(250, 200));
    f.setVisible(true);
  }
}