Animated clipping of an image and shapes with alpha : BufferedImage « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » BufferedImageScreenshots 
Animated clipping of an image and shapes with alpha
Animated clipping of an image and shapes with alpha
      
/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JFrame;

/**
 * Animated clipping of an image & shapes with alpha.
 */
public class ClipImage extends JApplet implements Runnable {

  private Image img;
  private final double OINC[] 5.03.0 };
  private final double SINC[] 5.05.0 };
  private double x, y;
  private double ix = OINC[0];
  private double iy = OINC[1];
  private double iw = SINC[0];
  private double ih = SINC[1];
  private double ew, eh; // ellipse width & height
  private GeneralPath p = new GeneralPath();
  private AffineTransform at = new AffineTransform();
  private BasicStroke bs = new BasicStroke(20.0f);
  private Arc2D arc = new Arc2D.Float();
  private Ellipse2D ellipse = new Ellipse2D.Float();
  private RoundRectangle2D roundRect = new RoundRectangle2D.Float();
  private Rectangle2D rect = new Rectangle2D.Float();
  private Color redBlend = new Color(25500120);
  private Color greenBlend = new Color(02550120);
  private Thread thread;
  private BufferedImage offImg;
  private int w, h;
  private boolean newBufferedImage;

  public void init() {
    setBackground(Color.white);
    img = getImage(getURL("images/clouds.jpg"));
    try {
      MediaTracker tracker = new MediaTracker(this);
      tracker.addImage(img, 0);
      tracker.waitForID(0);
    catch (Exception e) {
    }
  }

  public void drawDemo(Graphics2D g2) {

    if (newBufferedImage) {
      x = Math.random() * w;
      y = Math.random() * h;
      ew = (Math.random() * w2;
      eh = (Math.random() * h2;
    }
    x += ix;
    y += iy;
    ew += iw;
    eh += ih;
    if (ew > w / 2) {
      ew = w / 2;
      iw = Math.random() * -w / 16 1;
    }
    if (ew < w / 8) {
      ew = w / 8;
      iw = Math.random() * w / 16 1;
    }
    if (eh > h / 2) {
      eh = h / 2;
      ih = Math.random() * -h / 16 1;
    }
    if (eh < h / 8) {
      eh = h / 8;
      ih = Math.random() * h / 16 1;
    }
    if ((x + ew> w) {
      x = (w - ew1;
      ix = Math.random() * -w / 32 1;
    }
    if (x < 0) {
      x = 2;
      ix = Math.random() * w / 32 1;
    }
    if ((y + eh> h) {
      y = (h - eh2;
      iy = Math.random() * -h / 32 1;
    }
    if (y < 0) {
      y = 2;
      iy = Math.random() * h / 32 1;
    }

    ellipse.setFrame(x, y, ew, eh);
    g2.setClip(ellipse);

    rect.setRect(x + 5, y + 5, ew - 10, eh - 10);
    g2.clip(rect);

    g2.drawImage(img, 00, w, h, this);

    p.reset();
    p.moveTo(-w / 2.0f, -h / 8.0f);
    p.lineTo(+w / 2.0f, -h / 8.0f);
    p.lineTo(-w / 4.0f, +h / 2.0f);
    p.lineTo(+0.0f, -h / 2.0f);
    p.lineTo(+w / 4.0f, +h / 2.0f);
    p.closePath();

    at.setToIdentity();
    at.translate(w * .5f, h * .5f);
    g2.transform(at);
    g2.setStroke(bs);
    g2.setPaint(redBlend);
    g2.draw(p);

    at.setToIdentity();
    g2.setTransform(at);

    g2.setPaint(greenBlend);

    for (int yy = 0; yy < h; yy += 50) {
      for (int xx = 0, i = 0; xx < w; i++, xx += 50) {
        switch (i) {
        case 0:
          arc.setArc(xx, yy, 252545270, Arc2D.PIE);
          g2.fill(arc);
          break;
        case 1:
          ellipse.setFrame(xx, yy, 2525);
          g2.fill(ellipse);
          break;
        case 2:
          roundRect.setRoundRect(xx, yy, 252544);
          g2.fill(roundRect);
          break;
        case 3:
          rect.setRect(xx, yy, 2525);
          g2.fill(rect);
          i = -1;
        }

      }
    }
  }

  public Graphics2D createDemoGraphics2D(Graphics g) {
    Graphics2D g2 = null;

    if (offImg == null || offImg.getWidth() != w || offImg.getHeight() != h) {
      offImg = (BufferedImagecreateImage(w, h);
      newBufferedImage = true;
    }

    if (offImg != null) {
      g2 = offImg.createGraphics();
      g2.setBackground(getBackground());
    }

    // .. set attributes ..
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_QUALITY);

    // .. clear canvas ..
    g2.clearRect(00, w, h);

    return g2;
  }

  public void paint(Graphics g) {

    w = getWidth();
    h = getHeight();

    if (w <= || h <= 0)
      return;

    Graphics2D g2 = createDemoGraphics2D(g);
    drawDemo(g2);
    g2.dispose();

    if (offImg != null && isShowing()) {
      g.drawImage(offImg, 00this);
    }

    newBufferedImage = false;
  }

  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();
    }
    thread = null;
  }

  protected URL getURL(String filename) {
    URL codeBase = this.getCodeBase();
    URL url = null;

    try {
      url = new URL(codeBase, filename);
    catch (java.net.MalformedURLException e) {
      System.out.println("Couldn't create image: " "badly specified URL");
      return null;
    }

    return url;
  }

  public static void main(String s[]) {
    final ClipImage demo = new ClipImage();
    WindowListener l = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }

      public void windowDeiconified(WindowEvent e) {
        demo.start();
      }

      public void windowIconified(WindowEvent e) {
        demo.stop();
      }
    };
    JFrame f = new JFrame("Java 2D Demo - ClipImage");
    f.addWindowListener(l);
    f.getContentPane().add("Center", demo);
    f.setSize(new Dimension(400300));
    f.setVisible(true);
    demo.start();
  }
}

   
    
    
    
    
    
  
Related examples in the same category
1.BufferImage based Label
2.Renders an ellipse overlapping a rectangle with the compositing rule and alpha value selected by the userRenders an ellipse overlapping a rectangle with the compositing rule and alpha value selected by the user
3.Image operationsImage operations
4.BufferedImage with Jumbled images
5.Save an image after operations
6.Create buffered image that does not support transparency
7.Create a buffered image that supports transparency
8.Display image after operation
9.Compositing is the combining of elements from separate sources into single images
10.Convert a non-RGB image to an RGB buffered image
11.Blurring a Buffered Image
12.Sharpening a Buffered Image
13.Converting a Colored Buffered Image to Gray
14.Get average of a set of images with WritableRaster
15.Creating a BufferedImage from an Image object
16.Create Headless BufferedImage
17.Constructs a BufferedImage with a linear sRGB colorModel, and alpha
18.Creates and returns a buffered version of the specified image.
19.Scale, convert images
20.Make Linear Buffered Image
21.Renders multiple paragraphs of text in an array to an image (created and returned).
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.