Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 4:41 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: Area clipping
PostPosted: Tue Nov 11, 2008 5:26 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

Area clipping java code
Code:
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(1, 2));
    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(450, 400);
    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(0, 0, 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(0, 0, wi, ht);
    }
  }
}


Attachments:
File comment: Output will be displayed as:
clipArea1.gif
clipArea1.gif [ 6.08 KiB | Viewed 1273 times ]
File comment: On clicking clip2 button, a rectangle is shown inside the rectangular area:
clipArea2.gif
clipArea2.gif [ 6.24 KiB | Viewed 1273 times ]

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 compute area of the circle.
 Java- Copy text area into disabled text area
 java Clipping
 Calculate the area of a circle of given radius
 Cohen clipping on J2me

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 3 guests



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team