drawString(): specify the position of the text on the window area : Text « 2D Graphics GUI « Java






drawString(): specify the position of the text on the window area

drawString(): specify the position of the text on the window area
   

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 Text 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);
    Font font = new Font("New Roma", Font.BOLD, 40);
    g2d.setFont(font);
    g2d.drawString("text", 20, 30);
  }
  public static void main(String[] args) {
    Text text = new Text();
    JFrame frame = new JFrame("Sonnet 55");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(text);
    frame.setSize(500, 470);
    frame.setLocationRelativeTo(null);
    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 some lyrics on the panel.
13.Display unicode text
14.Rotate a line of character (String)
15.Generate Shape From Text