Draw 2D Text : Text « 2D Graphics GUI « Java

Draw 2D Text


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {
  public Main() {

  public void paint(Graphics g) {
    Graphics2D g2D;
    g2D = (Graphics2D) g;
    FontRenderContext frc = g2D.getFontRenderContext();
    Font font1 = new Font("Courier", Font.BOLD, 24);
    String str1 = new String("Java");
    TextLayout tl = new TextLayout(str1, font1, frc);
    tl.draw(g2D, 70, 150);

  public static void main(String s[]) {
    JFrame frame1 = new JFrame("2D Text");

    frame1.getContentPane().add("Center", new Main());
    frame1.setSize(new Dimension(500, 300));


Related examples in the same category

1.Create a shadowed text
2.Drawing Simple Text
3.Drawing Rotated Text
4.Draw string rotated clockwise 45 degrees
5.Draw string rotated counter-clockwise 45 degrees
6.Getting the Dimensions of Text
7.Display underlined text
8.Display vertical text
9.Use AffineTransform to draw vertical text
10.Have a Label with underlined text
11.Display some lyrics on the panel.
12.Display unicode text
13.drawString(): specify the position of the text on the window areadrawString(): specify the position of the text on the window area
14.Rotate a line of character (String)
15.Generate Shape From Text