FileDocCategorySizeDatePackage
ClipImage.javaAPI DocExample8285Tue Dec 12 19:00:00 GMT 2000None

ClipImage

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

Fields Summary
private Image
img
private final double[]
OINC
private final double[]
SINC
private double
x
private double
y
private double
ix
private double
iy
private double
iw
private double
ih
private double
ew
private double
eh
private GeneralPath
p
private AffineTransform
at
private BasicStroke
bs
private Arc2D
arc
private Ellipse2D
ellipse
private RoundRectangle2D
roundRect
private Rectangle2D
rect
private Color
redBlend
private Color
greenBlend
private Thread
thread
private BufferedImage
offImg
private int
w
private int
h
private boolean
newBufferedImage
Constructors Summary
Methods Summary
public java.awt.Graphics2DcreateDemoGraphics2D(java.awt.Graphics g)

        Graphics2D g2 = null;

        if ( offImg == null || offImg.getWidth() != w ||
             offImg.getHeight() != h ) {
            offImg = (BufferedImage) createImage(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(0, 0, w, h);

        return g2;
    
public voiddrawDemo(java.awt.Graphics2D g2)


        if ( newBufferedImage ) {
            x = Math.random()*w;
            y = Math.random()*h;
            ew = (Math.random()*w)/2;
            eh = (Math.random()*h)/2;
        }
        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 - ew)-1;
            ix = Math.random() * -w/32 - 1;
        }
        if ( x < 0 ) {
            x = 2;
            ix = Math.random() * w/32 + 1;
        }
        if ( (y+eh) > h ) {
            y = (h - eh)-2;
            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, 0, 0, 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, 25, 25, 45, 270, Arc2D.PIE);
                    g2.fill(arc); break;
                case 1 : ellipse.setFrame(xx, yy, 25, 25);
                    g2.fill(ellipse); break;
                case 2 : roundRect.setRoundRect(xx, yy, 25, 25, 4, 4);
                    g2.fill(roundRect); break;
                case 3 : rect.setRect(xx, yy, 25, 25);
                    g2.fill(rect);
                    i = -1;
                }

            }
        }
    
protected java.net.URLgetURL(java.lang.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 voidinit()


       
        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 static voidmain(java.lang.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(400,300));
        f.show();
        demo.start();
    
public voidpaint(java.awt.Graphics g)


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

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

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

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

        newBufferedImage = false;
    
public voidrun()


        Thread me = Thread.currentThread();

        while ( thread == me && isShowing() ) {
            Graphics g = getGraphics();
            paint(g);
            g.dispose();
            thread.yield();
        }
        thread = null;
    
public voidstart()

        thread = new Thread(this);
        thread.start();
    
public synchronized voidstop()

        thread = null;