Total members 11696 |It is currently Sat Feb 24, 2018 12:16 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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 5383 times ]
Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Clip of image in java
 how to compress Bmp image in java...     -  
 Image Scale in java     -  
 flip image in java     -  
 java image effects     -  
 image binarization in java     -  
 image processing in java     -  
 crop image in java     -  
 Image Noise Filter in Java     -  
 Image Custom Filter In Java     -  
 Image Colors Frequencies Histogram in Java     -  



Topic Tags

Java Image, Java Graphics
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