Make text under line in Java

Description

The following code shows how to make text under line.

Example


import java.awt.Graphics;
import java.awt.font.TextAttribute;
import java.util.Hashtable;
import java.awt.Font;
import javax.swing.JComponent;
import javax.swing.JFrame;
/* w  w w. j  ava  2s .c om*/
class MyCanvas extends JComponent {
  public void paint(Graphics g) {
    Hashtable<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
    Font font = new Font(Font.SERIF, Font.PLAIN, 24);

    /* Underlining is easy */
    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
    font = font.deriveFont(map);
    g.setFont(font);
    g.drawString("java2s.com", 45, 90);
  }
}

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

The code above generates the following result.

Make text under line in Java




















Home »
  Java Tutorial »
    Graphics »




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