This code can be seen as built a clipping path with the text area of the word ” Dinesh Thakur ” 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 java.awt.font.*;
public class DrawClippingPathExample extends Applet {
public static void main(String[] args) {
Frame DrawClippingPath = new Frame("Draw ClippingPath Example");
DrawClippingPath .setSize(350, 250);
Applet DrawClippingPathExample = new DrawClippingPathExample();
DrawClippingPath .add(DrawClippingPathExample);
DrawClippingPath .setVisible(true);
DrawClippingPath .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("Filling a Clipping Path 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;
FontRenderContext FontRC = G2D.getFontRenderContext();
Font font = new Font("Times",Font.BOLD,w/15);
String str = new String ("Dinesh Thakur");
TextLayout TextLay = new TextLayout(str,font,FontRC);
float sw = (float)TextLay.getBounds().getWidth();
AffineTransform AffineTrans = new AffineTransform();
AffineTrans.setToTranslation(w/2-sw/2,h/1.5);
Shape shp = TextLay.getOutline(AffineTrans);
G2D.setClip(shp);
G2D.setColor(Color.red);
G2D.fill(shp.getBounds2D());
G2D.setColor(Color.white);
G2D.setStroke(new BasicStroke(2.0f));
for ( double j = shp.getBounds().getY();
j<shp.getBounds2D().getY()+shp.getBounds2D().getHeight();
j=j+4) {
Line2D L2D = new Line2D.Double(0.0,j,w,j);
G2D.draw(L2D);
}
}
}