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


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: Clip of image in java
PostPosted: Tue Nov 11, 2008 6:41 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

Clip of image in java
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.net.URL;
import java.awt.image.BufferedImage;

public class ClipImageExample extends JApplet implements Runnable {
private final double I[] = { 6.0,2.0 };
  private final double J[] = { 6.0,5.0 };
  private double p, q;
  private double ip = I[0];
  private double iq = J[1];
  private double iw = I[0];
  private double ih = J[1];
  private double width, height;
  private GeneralPath path = new GeneralPath();
  private AffineTransform affineTransform = new AffineTransform();
  private BasicStroke stroke = new BasicStroke(20.0f);
  private Ellipse2D ellipse1 = new Ellipse2D.Float();
  private Ellipse2D ellipse2 = new Ellipse2D.Float();
  private Thread thread;
  private BufferedImage bufferedImg;
  private int w, h;
 
  public void drawImage(Graphics2D g2) {
    p += ip;
    q += iq;
    width += iw;
    height += ih;
    if (width > w / 3) {
      width = w / 3;
      iw = Math.random() * -w / 16 - 1;
    }
    if (width < w / 9) {
      width = w / 9;
      iw = Math.random() * w / 16 + 1;
    }
    if (height > h / 3) {
      height = h / 3;
      ih = Math.random() * -h / 16 - 1;
    }
    if (height < h / 9) {
      height = h / 9;
      ih = Math.random() * h / 16 + 1;
    }
    if ((p + width) > w) {
      p = (w - width) - 1;
      ip = Math.random() * -w / 32 - 1;
    }
    if (p < 0) {
      p = 3;
      ip = Math.random() * w / 32 + 1;
    }
    if ((q + height) > h) {
      q = (h - height) - 2;
      iq = Math.random() * -h / 32 - 1;
    }
    if (q < 0) {
      q = 3;
      iq = Math.random() * h / 32 + 1;
    }
    ellipse1.setFrame(p, q, width, height);
    g2.setClip(ellipse1);
    ellipse2.setFrame(p + 6, q + 5, width - 10, height - 10);
    g2.clip(ellipse2);

    path.reset();
    path.moveTo(-w / 3.0f, -h / 9.0f);
    path.lineTo(+w / 3.0f, -h / 9.0f);
    path.lineTo(-w / 5.0f, +h / 3.0f);
    path.lineTo(+0.0f, -h / 3.0f);
    path.lineTo(+w / 5.0f, +h / 3.0f);
    path.closePath();

    affineTransform.setToIdentity();
    affineTransform.translate(w * .5f, h * .5f);
    g2.transform(affineTransform);
    g2.setStroke(stroke);
    g2.setPaint(Color.cyan);
    g2.draw(path);
    affineTransform.setToIdentity();
  g2.setTransform(affineTransform);
    g2.setPaint(Color.red);

   for (int qq = 0; qq < h; qq += 40) {
      for (int pp = 0, k = 0; pp < w; k++, pp += 40) {
        switch (k) {
        case 0:
          ellipse1.setFrame(pp, qq, 20, 20);
          g2.fill(ellipse1);
          break;
        case 1:
          ellipse2.setFrame(pp, qq, 20, 20);
          g2.fill(ellipse2);
          k = -1;
        }
      }
    }
  }
  public Graphics2D createDemoGraphics2D(Graphics g) {
    Graphics2D g2 = null;
    if (bufferedImg == null || bufferedImg.getWidth() != w || bufferedImg.
         getHeight() != h) {
      bufferedImg = (BufferedImage) createImage(w, h);
      }
    if (bufferedImg != null) {
      g2 = bufferedImg.createGraphics();
      g2.setBackground(getBackground());
    }
    g2.clearRect(0, 0, w, h);
    return g2;
  }
  public void paint(Graphics g) {
    w = getWidth();
    h = getHeight();
    if (w <= 0 || h <= 0)
      return;
    Graphics2D g2 = createDemoGraphics2D(g);
    drawImage(g2);
    g2.dispose();
     if (bufferedImg != null && isShowing()) {
      g.drawImage(bufferedImg, 0, 0, this);
    }
}
  public void start() {
    thread = new Thread(this);
    thread.start();
  }
  public synchronized void stop() {
    thread = null;
  }
  public void run() {
    Thread me = Thread.currentThread();
    while (thread == me && isShowing()) {
      Graphics g = getGraphics();
      paint(g);
      g.dispose();
      thread.yield();
    }
  }
  public static void main(String s[]) {
    final ClipImageExample clip = new ClipImageExample();
    JFrame frame = new JFrame("Clip Image Example");
    frame.getContentPane().add("Center", demo);
    frame.setSize(new Dimension(450, 300));
    frame.show();
    clip.start();
  }
}


Attachments:
File comment: the output
clipImage.gif
clipImage.gif [ 6.47 KiB | Viewed 2586 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
 Image Insert in Excel File Using POI
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 image processing
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: Google Adsense [Bot] and 2 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