Clipping in Java Swing

By setting a clipping shape in the graphics(graphics means to the creation, storage and manipulation of pictures and drawings using a digital computer) context, you constrain all drawing operations to the interior of that clipping shape. The clip method intersects the existing clipping shape with the new one .

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.awt.geom.*;

import java.awt.font.*;

public class ClipTestt

{

public static void main(String aa[])

{

JFrame frame=new ClipTestFramee();

frame.show();

}

}

class ClipTestFramee extends JFrame implements ActionListener

{

public ClipTestFramee()

{

setTitle("ClipTestt");

setSize(500,500);

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent wwevent)

{

System.exit(0);

}

});

Container contentPane=getContentPane();

canvas=new ClipPanel();

contentPane.add(canvas,"Center");

JPanel buttonPanel=new JPanel();

ButtonGroup group=new ButtonGroup();

noClipButton=new JRadioButton("Nooo Clipping",true);

buttonPanel.add(noClipButton);

group.add(noClipButton);

noClipButton.addActionListener(this);

clipButton=new JRadioButton("Clipping",false);

buttonPanel.add(clipButton);

group.add(clipButton);

clipButton.addActionListener(this);

contentPane.add(buttonPanel,"North");

}

public void actionPerformed(ActionEvent aaevt)

{

Object source=aaevt.getSource();

if(source==clipButton)

canvas.setClip(true);

else if(source==noClipButton)

canvas.setClip(false);

}

private JRadioButton clipButton;

private JRadioButton noClipButton;

private ClipPanel canvas;

}

class ClipPanel extends JPanel

{

public void paintComponent(Graphics grp)

{

super.paintComponent(grp);

Graphics2D g2=(Graphics2D) grp;

if(clip)

{

FontRenderContext context=g2.getFontRenderContext();

Font font=new Font("Seriff",Font.PLAIN,100);

GeneralPath clipShapee=new GeneralPath();

                                       /*This TextLayout object describes the layout of a sequence of  characters(word or string), as rendered by a particular font(like Arial,Seriff) render context(describe horizontal and vertical extent of the characters)*/

TextLayout layout=new TextLayout("XCnotes.",font,context);

AffineTransform transform=AffineTransform.getTranslateInstance(0,100);

                                    /*getOutline() returns a Shape object that describes the shape of the outline of the characters in the text layout*/

Shape outline=layout.getOutline(transform);

clipShapee.append(outline,false);

layout=new TextLayout("Com ",font,context);

transform=AffineTransform.getTranslateInstance(0,200);

outline=layout.getOutline(transform);

clipShapee.append(outline,false);

g2.draw(clipShapee);//draws only the part that falls inside the clipping shape

g2.clip(clipShapee);//clip method intersects the existing clipping shape with the new one

}

final int NLINES=50;

                                    /*Finally we set the clipping shape and draw a set of lines(more than one lines). The lines appear only inside the character(every character in the string) boundaries*/

Point2D p=new Point2D.Double(0,0);

for(int i=0;i<NLINES;i++)

{

double x=(2*getWidth()*i)/NLINES;

double y=(2*getHeight()*(NLINES-1-1))/NLINES;

Point2D q=new Point2D.Double(x,y);

g2.draw(new Line2D.Double(p,q));//lines are clipped

}

}

public void setClip(boolean c)

{

clip=c;

repaint();

}

private boolean clip;

}

No Clipping

 

Clipping

Explanation…

FONT RENDER CONTEXT is used to obtain character outlines.

The outline shape starts at the origin(0,0 in every character) which is not suitable for most drawing (like line ellipse,oval)operations.Therefore, you need to supply an affine transform (transform= AffineTransform .get TranslateInstance(0,200)) to the getOutline operation that specifies where you would like the outline to appear.

Void setClip()-sets the current clipping shape to the Boolean c.

FontRenderContext context=g2.getFontRenderContext();---returns a font render context that is necessary for constructing TextLayout objects.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @