by Dinesh Thakur

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) {
  public void paint(Graphics g) {
    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("", 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();
               Shape shp = TextLay.getOutline(AffineTrans);
               G2D.setStroke(new BasicStroke(2.0f));
               for ( double j = shp.getBounds().getY();
               j=j+4) {
              Line2D L2D = new Line2D.Double(0.0,j,w,j);

Filling a clipping path with different objects