KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > slides > ScaleFx


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.view.ui.slides;
21
22 import java.awt.AlphaComposite JavaDoc;
23 import java.awt.BorderLayout JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.Component JavaDoc;
26 import java.awt.Composite JavaDoc;
27 import java.awt.Container JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Graphics JavaDoc;
30 import java.awt.Graphics2D JavaDoc;
31 import java.awt.GraphicsEnvironment JavaDoc;
32 import java.awt.Image JavaDoc;
33 import java.awt.Rectangle JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.geom.AffineTransform JavaDoc;
37 import java.awt.image.BufferedImage JavaDoc;
38 import javax.swing.CellRendererPane JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JComponent JavaDoc;
41 import javax.swing.JLayeredPane JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.SwingUtilities JavaDoc;
44 import javax.swing.Timer JavaDoc;
45 import javax.swing.event.ChangeListener JavaDoc;
46
47 /**
48  * Scale effect with customizable alpha and effect speed.
49  *
50  * @author Dafe Simonek
51  */

52 final class ScaleFx implements SlidingFx, ActionListener JavaDoc {
53     
54     private float initialAlpha = 0.1f;
55     private float finishAlpha = 0.9f;
56     
57     private int iterCount = 9;
58     
59     private int curIter = 0;
60     
61     private static final float DIVIDING_FACTOR = 2.0f;
62     
63     private static final int FRAME_DELAY = 20;
64     
65     private Rectangle JavaDoc middle = new Rectangle JavaDoc();
66     private Rectangle JavaDoc current = new Rectangle JavaDoc();
67     
68     private Timer JavaDoc timer = null;
69     
70     private StretchedImageComp stretchedImage = new StretchedImageComp();
71     
72     private Rectangle JavaDoc[] path;
73     
74     private JLayeredPane JavaDoc pane;
75     
76     private SlideOperation operation;
77     
78     private Image JavaDoc preparedImage;
79     
80     private ChangeListener JavaDoc finishL;
81     
82     private final boolean shouldOperationWait;
83     
84     public ScaleFx(float initialAlpha, float finishAlpha, boolean shouldOperationWait) {
85         setTransparency(initialAlpha, finishAlpha);
86         this.shouldOperationWait = shouldOperationWait;
87     }
88     
89     public void prepareEffect(SlideOperation operation) {
90         Component JavaDoc comp = operation.getComponent();
91         preparedImage = createCompImage(operation.getComponent(), operation.getComponent().getSize());
92     }
93     
94     public void showEffect(JLayeredPane JavaDoc pane, Integer JavaDoc layer, SlideOperation operation) {
95         this.pane = pane;
96         this.operation = operation;
97         Component JavaDoc comp = operation.getComponent();
98         Graphics2D JavaDoc gr2d = (Graphics2D JavaDoc)pane.getGraphics();
99         Rectangle JavaDoc start = operation.getStartBounds();
100         Rectangle JavaDoc finish = operation.getFinishBounds();
101         Dimension JavaDoc finishSize = finish.getSize();
102         Dimension JavaDoc startSize = start.getSize();
103         Rectangle JavaDoc current = start;
104         Image JavaDoc compImage = preparedImage;
105
106        /* if (compImage == null) {
107             if (finishSize.width * finishSize.height > startSize.width * startSize.height) {
108                 compImage = renderCompIntoImage(comp, finishSize, pane);
109             } else {
110                 compImage = renderCompIntoImage(comp, startSize, pane);
111             }
112         }*/

113         pane.add(stretchedImage, layer);
114         
115         path = computePath(start, finish);
116         
117         curIter = 1;
118         if (compImage != null) {
119             stretchedImage.setOrigImage(compImage);
120         } else {
121             if (finishSize.width * finishSize.height > startSize.width * startSize.height) {
122                 stretchedImage.setComp(comp, finishSize);
123             } else {
124                 stretchedImage.setComp(comp, startSize);
125             }
126         }
127         stretchedImage.setBoundsAndAlpha(start, initialAlpha);
128         
129         getTimer().start();
130     }
131     
132     public void actionPerformed(ActionEvent JavaDoc e) {
133         float coef = (float)curIter / (float)(iterCount - 1);
134         float curAlpha = (1 - coef) * initialAlpha + coef * finishAlpha;
135         
136         stretchedImage.setBoundsAndAlpha(path[curIter], curAlpha);
137         
138         curIter++;
139         
140         if (curIter >= iterCount) {
141             getTimer().stop();
142             finish();
143         }
144     }
145     
146     private void finish () {
147         pane.remove(stretchedImage);
148         stretchedImage.cleanup();
149         // notify about end of effect
150
if (finishL != null) {
151             finishL.stateChanged(null);
152         }
153     }
154     
155     public void setTransparency(float initialAlpha, float finishAlpha) {
156         this.initialAlpha = initialAlpha;
157         this.finishAlpha = finishAlpha;
158     }
159     
160     private void setSuggestedIterations(int count) {
161         if (count < 3) {
162             count = 3;
163         }
164         // make iterations odd number for easier path computing, see computePath
165
this.iterCount = count % 2 == 0 ? count + 1 : count;
166     }
167     
168     private Rectangle JavaDoc[] computePath(Rectangle JavaDoc start, Rectangle JavaDoc finish) {
169         Rectangle JavaDoc[] path = new Rectangle JavaDoc[iterCount];
170         
171         middle.x = Math.abs((finish.x + start.x) / 2);
172         middle.y = Math.abs((finish.y + start.y) / 2);
173         middle.width = Math.abs((finish.width + start.width) / 2);
174         middle.height = Math.abs((finish.height + start.height) / 2);
175         
176         current = new Rectangle JavaDoc(middle);
177         for (int i = iterCount / 2 - 1; i >= 0; i--) {
178             current.x = (int)Math.abs((current.x + start.x) / DIVIDING_FACTOR);
179             current.y = (int)Math.abs((current.y + start.y) / DIVIDING_FACTOR);
180             current.width = (int)Math.abs((current.width + start.width) / DIVIDING_FACTOR);
181             current.height = (int)Math.abs((current.height + start.height) / DIVIDING_FACTOR);
182             path[i] = new Rectangle JavaDoc(current);
183         }
184         path[iterCount / 2] = new Rectangle JavaDoc(middle);
185         current = middle;
186         for (int i = iterCount / 2 + 1; i < iterCount; i++) {
187             current.x = (int)Math.abs((current.x + finish.x) / DIVIDING_FACTOR);
188             current.y = (int)Math.abs((current.y + finish.y) / DIVIDING_FACTOR);
189             current.width = (int)Math.abs((current.width + finish.width) / DIVIDING_FACTOR);
190             current.height = (int)Math.abs((current.height + finish.height) / DIVIDING_FACTOR);
191             path[i] = new Rectangle JavaDoc(current);
192         }
193         
194         return path;
195     }
196     
197     private Image JavaDoc createCompImage(Component JavaDoc comp, Dimension JavaDoc targetSize) {
198         // component won't paint if not showing anyway, so don't create
199
// empty image but honestly return null
200
if (!comp.isShowing()) {
201             return null;
202         }
203         
204         Image JavaDoc image = comp.createImage(comp.getWidth(), comp.getHeight());
205         
206         /*BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().
207                 getDefaultScreenDevice().getDefaultConfiguration().
208                 createCompatibleImage(comp.getWidth(), comp.getHeight());*/

209         //BufferedImage image = new BufferedImage (targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
210

211         Graphics2D JavaDoc gr2d = (Graphics2D JavaDoc)image.getGraphics();
212         
213         comp.paint(gr2d);
214         
215         gr2d.dispose();
216         
217         return image;
218     }
219     
220     /*private Image renderCompIntoImage(Component comp, Dimension targetSize, Container parent) {
221         BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().
222                 getDefaultScreenDevice().getDefaultConfiguration().
223                 createCompatibleImage(targetSize.width, targetSize.height);
224         
225         if (comp.isShowing()) {
226             // just paint component directly without double buffering into image
227             boolean wasDoubleBuffered = false;
228             if ((comp instanceof JComponent) && ((JComponent)comp).isDoubleBuffered()) {
229                 wasDoubleBuffered = true;
230                 ((JComponent)comp).setDoubleBuffered(false);
231             }
232
233             comp.setBounds(0, 0, targetSize.height, targetSize.height);
234             
235             Graphics2D gr2d = image.createGraphics();
236             try {
237                 comp.paint(gr2d);
238             } finally {
239                 gr2d.dispose();
240             }
241             
242             if (wasDoubleBuffered && (comp instanceof JComponent)) {
243                 ((JComponent)comp).setDoubleBuffered(true);
244             }
245         } else {
246             // use renderer to make component believe that it's showing and
247             // paint itself correctly
248 System.out.println("Rendering using comp renderer...");
249             compRenderer.setRenderee(comp);
250             parent.add(compRenderer);
251             Graphics2D gr2d = image.createGraphics();
252             rendererPane.paintComponent(gr2d, compRenderer, parent, 0, 0, targetSize.width, targetSize.height, false);
253             parent.remove(compRenderer);
254             gr2d.dispose();
255         }
256         
257         return image;
258     }*/

259     
260     private Timer JavaDoc getTimer () {
261         if (timer == null) {
262             timer = new Timer JavaDoc (FRAME_DELAY, this);
263             timer.setRepeats(true);
264         }
265         return timer;
266     }
267     
268     public void setFinishListener(ChangeListener JavaDoc finishL) {
269         this.finishL = finishL;
270     }
271     
272     public boolean shouldOperationWait() {
273         return shouldOperationWait;
274     }
275     
276     
277     // XXX - TBD - this component should be changed to glass pane to not interfere
278
// with layered pane real components in any situation
279
private class StretchedImageComp extends JComponent JavaDoc {
280
281         private Image JavaDoc origImage;
282         
283         private float alpha = 1.0f;
284         
285         private Component JavaDoc comp;
286         
287         private Dimension JavaDoc scaleSource, targetSize;
288         
289         public void setComp(Component JavaDoc comp, Dimension JavaDoc targetSize) {
290             this.comp = comp;
291             this.targetSize = targetSize;
292         }
293         
294         public void setOrigImage(Image JavaDoc origImage) {
295             this.origImage = origImage;
296         }
297         
298         public void setScaleSource(Dimension JavaDoc scaleSource) {
299             this.scaleSource = scaleSource;
300         }
301         
302         public void setBoundsAndAlpha(Rectangle JavaDoc bounds, float alpha) {
303             this.alpha = alpha;
304             setBounds(bounds);
305             if (origImage == null) {
306                 origImage = tryCreateImage();
307             }
308         }
309         
310         private Image JavaDoc tryCreateImage () {
311             Image JavaDoc result = null;
312             if (comp != null && isDisplayable()) {
313                 comp.setSize(targetSize);
314                 add(comp);
315                 result = createCompImage(comp, targetSize);
316                 remove(comp);
317             }
318             return result;
319         }
320         
321         public void cleanup() {
322             comp = null;
323             origImage = null;
324             scaleSource = null;
325             targetSize = null;
326         }
327
328         public void paint(Graphics JavaDoc g) {
329             Rectangle JavaDoc bounds = getBounds();
330             
331             if (origImage == null) {
332                 if (comp == null) {
333                     return;
334                 }
335                 origImage = tryCreateImage();
336                 if (origImage == null) {
337                     return;
338                 }
339             }
340             Image JavaDoc img = origImage;
341             Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) g;
342             Composite JavaDoc origComposite = g2d.getComposite();
343             g2d.setComposite (AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
344             /*AffineTransform at = AffineTransform.getScaleInstance(
345                 (double)bounds.width / (double)scaleSource.width,
346                 (double)bounds.height / (double)scaleSource.height);
347             g2d.setTransform(at);*/

348             g2d.drawImage(img, 0, 0, bounds.width, bounds.height, null);
349             //SwingUtilities.paintComponent(g, getComponent(0), this, 0, 0, bounds.width, bounds.height);
350
//super.paint(g2d);
351

352             if (origComposite != null) {
353                 g2d.setComposite(origComposite);
354             }
355         }
356         
357
358     } // StretchedImageComp
359

360     
361 }
362
Popular Tags