by Dinesh Thakur Category: Java 2D Example

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

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, 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.