001/*
002 * Copyright 2010, 2011 Institut Pasteur.
003 * 
004 * This file is part of ICY.
005 * 
006 * ICY is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * ICY is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
018 */
019package plugins.tutorial.painter;
020
021import icy.canvas.IcyCanvas;
022import icy.canvas.IcyCanvas2D;
023import icy.painter.Overlay;
024import icy.sequence.Sequence;
025import icy.type.point.Point5D;
026
027import java.awt.AlphaComposite;
028import java.awt.BasicStroke;
029import java.awt.Color;
030import java.awt.Graphics2D;
031import java.awt.RenderingHints;
032import java.awt.event.ActionEvent;
033import java.awt.event.ActionListener;
034import java.awt.event.MouseEvent;
035import java.awt.geom.Arc2D;
036import java.awt.geom.Ellipse2D;
037import java.awt.geom.Point2D;
038
039import javax.swing.Timer;
040
041/**
042 * Fancy mark to demonstrate the capabilities of painters.
043 * 
044 * @author Fabrice de Chaumont
045 * @author Stephane Dallongeville
046 */
047public class AnimatedOverlay extends Overlay implements ActionListener
048{
049    Timer timer = new Timer(10, this);
050    float current = 0;
051    Point2D center;
052    Point2D mouse;
053    Color color;
054    Color color2;
055    double currentRotation = 0;
056
057    public AnimatedOverlay(Point2D imagePoint)
058    {
059        super("Animated mark");
060
061        // this.sequence = sequence;
062        center = (Point2D) imagePoint.clone();
063        mouse = new Point2D.Double(0, 0);
064        float randomH = (float) Math.random();
065        color = Color.getHSBColor(randomH, 1f, 1f);
066        color2 = Color.getHSBColor(randomH + 0.5f, 1f, 1f);
067        timer.start();
068    }
069
070    @Override
071    public void paint(Graphics2D g, Sequence sequence, IcyCanvas canvas)
072    {
073        // check if we are dealing with a canvas 2D and we have a valid Graphics object
074        if ((canvas instanceof IcyCanvas2D) && (g != null))
075        {
076            Ellipse2D ellipse;
077            Arc2D arc;
078            String text;
079            float ray;
080
081            final Graphics2D g2 = (Graphics2D) g.create();
082
083            g2.rotate(currentRotation, center.getX(), center.getY());
084            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
085
086            g2.setColor(color);
087            ray = current * 30f;
088            ellipse = new Ellipse2D.Double(center.getX() - ray, center.getY() - ray, ray * 2, ray * 2);
089
090            g2.setStroke(new BasicStroke(5));
091            g2.draw(ellipse);
092
093            g2.setColor(color2);
094            ray = current * 30f;
095            arc = new Arc2D.Double(Arc2D.PIE);
096            arc.setFrame(center.getX() - ray * 1.5f, center.getY() - ray * 1.5f, ray * 3, ray * 3);
097            arc.setAngleStart(0);
098            arc.setAngleExtent(45);
099
100            g2.setStroke(new BasicStroke(5 * current));
101            g2.draw(arc);
102
103            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, current));
104
105            g2.setColor(color2);
106            ray = 62f - current * 30f;
107            ellipse = new Ellipse2D.Double(center.getX() - ray, center.getY() - ray, ray * 2, ray * 2);
108            g2.setStroke(new BasicStroke(5 * (1 - current / 2)));
109            g2.draw(ellipse);
110
111            if (mouse.distance(center) < 30)
112                text = "Mouse position X:" + (int) mouse.getX() + " Y:" + (int) mouse.getY();
113            else
114                text = "This is a painter example";
115
116            g2.rotate(1.6, center.getX(), center.getY());
117
118            for (int i = 0; i < text.length(); i++)
119            {
120                final String t = text.substring(i, i + 1);
121
122                g2.rotate(0.2, center.getX(), center.getY());
123                // This lead to a memory leak, due to a bug in the JVM 1.6
124                // cache of transformed fonts (bug reported).
125                // we chose not to care about it for this example.
126                g2.drawString(t, (int) (center.getX()), (int) (center.getY() - 40));
127            }
128
129            g2.dispose();
130        }
131    }
132
133    @Override
134    public void mouseMove(MouseEvent e, Point5D.Double imagePoint, IcyCanvas canvas)
135    {
136        // check if we are dealing with a 2D canvas and we have a valid image position
137        if ((canvas instanceof IcyCanvas2D) && (imagePoint != null))
138        {
139            mouse = imagePoint.toPoint2D();
140        }
141    }
142
143    @Override
144    public void actionPerformed(ActionEvent e)
145    {
146        if (current < 0.90)
147            current += 0.05f;
148
149        currentRotation += 0.02d;
150
151        painterChanged();
152    }
153
154}