Create a shadowed text : Text « 2D Graphics GUI « Java






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

  }
}

   
    
    
  








Related examples in the same category

1.Draw 2D 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