Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Clip Area 
 

A Clip is a portion of a graphics or video or any picture. We are providing you an example which shows the clipping of area.

 

Clip Area

                         

This section illustrates you how to show the clipping of Area.

A Clip is a portion of a graphics or video or any picture. We are providing you an example which shows the clipping of area.

In the given example, we have defined two buttons clip1 and clip2 to perform two actions. On clicking clip1, a Rectangle2D class shows the clip of  rectangular area and on clicking clip2,  Rectangle class (subclass of Rectangle2D) shows a rectangle inside the rectangular area. The method setClip() sets the clipping area to clip the shape. 

Here is the code of ClipAreaExample.java

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.border.TitledBorder;

public class ClipAreaExample extends JFrame {
  Canvas1 canvas;
  JButton button1, button2;
    public ClipAreaExample() {
    super("Clip Area");
    Container contentPane = getContentPane();
    canvas = new Canvas1();
    contentPane.add(canvas);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(12));
    button1 = new JButton("Clip1");
    button1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        canvas.clip1 = true;
        canvas.clip2= false;
        canvas.repaint();
      }
    });
    button2 = new JButton("Clip2");
    button2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        canvas.clip2 = true;
        canvas.repaint();
      }
    });
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(button1);
    buttonGroup.add(button2);
    panel.add(button1);
    panel.add(button2);
    contentPane.add(BorderLayout.SOUTH, panel);
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent event) {
        System.exit(0);
      }
    });
    setVisible(true); 
  }
  public static void main(String arg[]) {
    new ClipAreaExample();
  }
}
class Canvas1 extends JPanel{
  boolean clip1 = true;
  boolean clip2 = false;
  Canvas1() {
    setSize(450400);
    setBackground(Color.white);
  }
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    int wi = getSize().width;
    int ht = getSize().height;
    if (clip1) {
      Rectangle2D rectangle = new Rectangle2D.Double(wi / 4.0f,
       ht / 
4.0f, wi / 2.0f,ht / 2.0f);
      g2d.setClip(rectangle);
      g2d.setColor(Color.cyan);
      g2d.fillRect(00, wi, ht);
    }
  if (clip2) {
      Rectangle rect = new Rectangle(wi / 2, ht / 2, wi / 2, ht / 2);
      g2d.clip(rect);
      g2d.setColor(Color.red);
      g2d.fillRect(00, wi, ht);
    }
  }
}

Output will be displayed as:

On clicking clip2 button, a rectangle is shown inside the rectangular area:

Download Source code

                         

» View all related tutorials
Related Tags: c string com class orm ant form object io sed format screen font display vi metrics id render oo define

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.