001    /**
002     * Copyright (c) 2008-2009, Piet Blok
003     * All rights reserved.
004     *
005     * Redistribution and use in source and binary forms, with or without
006     * modification, are permitted provided that the following conditions
007     * are met:
008     *
009     *   * Redistributions of source code must retain the above copyright
010     *     notice, this list of conditions and the following disclaimer.
011     *   * Redistributions in binary form must reproduce the above
012     *     copyright notice, this list of conditions and the following
013     *     disclaimer in the documentation and/or other materials provided
014     *     with the distribution.
015     *   * Neither the name of the copyright holder nor the names of the
016     *     contributors may be used to endorse or promote products derived
017     *     from this software without specific prior written permission.
018     *
019     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
020     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
021     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
022     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
023     * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
024     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
025     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
029     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030     */
031    
032    package org.pbjar.jxlayer.demo;
033    
034    import java.awt.Component;
035    import java.awt.Graphics;
036    import java.awt.Graphics2D;
037    import java.awt.RenderingHints;
038    import java.awt.event.ActionEvent;
039    import java.awt.geom.Ellipse2D;
040    import java.util.Iterator;
041    import java.util.Map;
042    import java.util.WeakHashMap;
043    
044    import javax.swing.AbstractAction;
045    import javax.swing.Icon;
046    import javax.swing.Timer;
047    
048    /**
049     * A very simple animated icon to see what happens in the Transform demo.
050     * 
051     * @author Piet Blok
052     */
053    public class AnimatedIcon implements Icon {
054    
055        private static final int maxRadius = 4;
056    
057        private Map<Component, Object> componentMap = new WeakHashMap<Component, Object>();
058    
059        private double radius = 0;
060    
061        private Ellipse2D dot = new Ellipse2D.Float();
062    
063        public AnimatedIcon() {
064    
065            Timer timer = new Timer(100, new AbstractAction() {
066    
067                private static final long serialVersionUID = 1L;
068    
069                @Override
070                public void actionPerformed(ActionEvent e) {
071                    radius += 0.1;
072                    if (radius > maxRadius) {
073                        radius = 0.1;
074                    }
075                    dot.setFrameFromCenter(maxRadius, maxRadius,
076                            maxRadius - radius, maxRadius - radius);
077                    // For all registered components
078                    Iterator<Component> iterator = componentMap.keySet().iterator();
079                    while (iterator.hasNext()) {
080                        iterator.next().repaint();
081                        // Remove, because the component will be re-registered
082                        // when painting, or not when not painting.
083                        iterator.remove();
084                    }
085                }
086            });
087            timer.start();
088        }
089    
090        @Override
091        public int getIconHeight() {
092            return 2 * maxRadius;
093        }
094    
095        @Override
096        public int getIconWidth() {
097            return 2 * maxRadius;
098        }
099    
100        @Override
101        public void paintIcon(Component c, Graphics g, int x, int y) {
102            // Remember the component for a repaint
103            componentMap.put(c, null);
104            // Paint the icon
105            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
106                    RenderingHints.VALUE_ANTIALIAS_ON);
107            g.translate(x, y);
108            ((Graphics2D) g).fill(dot);
109        }
110    
111    }