Total members 11893 |It is currently Tue Nov 05, 2024 6:33 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





java Clipping
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.font.TextLayout;
import java.awt.font.FontRenderContext;

public class 
ClipExample {
  public static 
void main(String[] args) {
    
JFrame f = new Clipping();
    
f.show();
  }
}
class 
Clipping extends JFrame implements ActionListener {
  public 
Clipping() {
    
setTitle("Show clippings");
    
setSize(350300);
    
addWindowListener(new WindowAdapter() {
      public 
void windowClosing(WindowEvent event) {
        
System.exit(0);
      }
    });
    
Container contentPane getContentPane();
    
panel = new ClipPanel();
    
contentPane.add(panel"Center");
    
JPanel buttonPanel = new JPanel();
    
ButtonGroup group = new ButtonGroup();
    
button1 = new JButton("No Clip");
    
buttonPanel.add(button1);
    
group.add(button1);
    
button1.addActionListener(this);
    
button2 = new JButton("Show Clip");
    
buttonPanel.add(button2);
    
group.add(button2);
    
button2.addActionListener(this);
    
contentPane.add(buttonPanel"North");
  }
 public 
void actionPerformed(ActionEvent e) {
    
Object object e.getSource();
    if (
object == button2)
      
panel.setClip(true);
    else if (
object == button1)
      
panel.setClip(false);
  }
  private 
JButton button1;
  private 
JButton button2;
  private 
ClipPanel panel;
}
class 
ClipPanel extends JPanel {
  public 
void paint(Graphics graphics) {
    
super.paint(graphics);
    
Graphics2D g2 = (Graphics2Dgraphics;
    if (
clip) {
      
FontRenderContext fontRendContext g2.getFontRenderContext();
      
Font font = new Font("Monotype Corsiva"Font.BOLD100);
      
GeneralPath path = new GeneralPath();

      
TextLayout layout = new TextLayout("Welcome"font
         
fontRendContext);
      
AffineTransform affineTransform AffineTransform.
      
getTranslateInstance(0,90);
      
Shape outline layout.getOutline(affineTransform);
      
path.append(outlinefalse);
      
g2.draw(path);
      
g2.clip(path);
    }
  final 
int LINES 90;
    
Point2D point1 = new Point2D.Double(22);
    for (
int k 0LINESk++) {
      
double x = (getWidth() * k) / LINES;
      
double y = (getHeight() * (LINES k)) / LINES;
      
Point2D point2 = new Point2D.Double(xy);
      
g2.draw(new Line2D.Double(point1point2));
    }
  }
   public 
void setClip(boolean c1) {
    
clip c1;
    
repaint();
  }
  private 
boolean clip;





Attachments:
File comment: Output will be displayed as
clip1.gif
clip1.gif [ 12.8 KiB | Viewed 7867 times ]
File comment: After clicking the button show clip, the clip will be shown as
clip2.gif
clip2.gif [ 8.38 KiB | Viewed 7873 times ]

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : java Clipping
 Area clipping     -  
 Cohen clipping on J2me     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 Java course     -  
 need help in java     -  
 What is Java API?!!!     -  
 java or .net     -  
 Using FTP in java     -  
 what is java     -  
 firewall using java     -  



Topic Tags

Java Graphics, Java AWT
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com