by Dinesh Thakur Category: Java 2D Example

This code can be seen as built a clipping path with the content of the text area of the word "Java " located in the middle of the window.In the drawn area can be seen as the image is painted behind the chain. As seen, the effect is very colorful and easy to perform.

import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.Shape;
import javax.swing.*;
public class DrawTextEffectExample extends Applet {
public static void main(String[] args) {
Frame DrawTextEffect  = new Frame("Draw TextEffect  Example");
DrawTextEffect .setSize(350, 250);
Applet DrawTextEffectExample = new DrawTextEffectExample();
DrawTextEffect .add(DrawTextEffectExample);
DrawTextEffect .setVisible(true);
DrawTextEffect .addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Text Effect in Java Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                int w = getSize().width;
                int h = getSize().height;
                Image image = (new ImageIcon("Canada.jpg")).getImage();
                FontRenderContext FontRC = G2D.getFontRenderContext();
                Font font = new Font("Times",Font.BOLD,100);
                TextLayout TextLay = new TextLayout("Java",font,FontRC);
                float sw = (float)TextLay.getBounds().getWidth();
                AffineTransform AffineTran = new AffineTransform();
                AffineTran.setToTranslation(w/2-sw/2,h*5/8);
                Shape s = TextLay.getOutline(AffineTran);
               G2D.setClip(s);
               G2D.drawImage(image,50,60,this);
               G2D.setColor(Color.green);
               G2D.draw(s);
  }
}

Text Effect in Java Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.