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


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: java Clipping
PostPosted: Tue Nov 11, 2008 5:16 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

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(350, 300);
    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 = (Graphics2D) graphics;
    if (clip) {
      FontRenderContext fontRendContext = g2.getFontRenderContext();
      Font font = new Font("Monotype Corsiva", Font.BOLD, 100);
      GeneralPath path = new GeneralPath();

      TextLayout layout = new TextLayout("Welcome", font,
         fontRendContext);
      AffineTransform affineTransform = AffineTransform.
      getTranslateInstance(0,90);
      Shape outline = layout.getOutline(affineTransform);
      path.append(outline, false);
      g2.draw(path);
      g2.clip(path);
    }
  final int LINES = 90;
    Point2D point1 = new Point2D.Double(2, 2);
    for (int k = 0; k < LINES; k++) {
      double x = (2 * getWidth() * k) / LINES;
      double y = (2 * getHeight() * (LINES - 1 - k)) / LINES;
      Point2D point2 = new Point2D.Double(x, y);
      g2.draw(new Line2D.Double(point1, point2));
    }
  }
   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 2076 times ]
File comment: After clicking the button show clip, the clip will be shown as
clip2.gif
clip2.gif [ 8.38 KiB | Viewed 2082 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
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java
 java code for listing folder contents from remote folder
 java

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: Google Adsense [Bot] and 1 guest



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