Display some lyrics on the panel. : Text « 2D Graphics GUI « Java






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);
  }
}

   
    
    
  








Related examples in the same category

1.Create a shadowed text
2.Draw 2D Text
3.Drawing Simple Text
4.Drawing Rotated Text
5.Draw string rotated clockwise 45 degrees
6.Draw string rotated counter-clockwise 45 degrees
7.Getting the Dimensions of Text
8.Display underlined text
9.Display vertical text
10.Use AffineTransform to draw vertical text
11.Have a Label with underlined text
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