1   package org.wcb.gui.effect;
2   
3   import javax.swing.*;
4   import java.awt.*;
5   import java.awt.image.BufferedImage;
6   import java.util.logging.Logger;
7   import java.util.logging.Level;
8   
9   /**
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23   */
24  
25  public class Dissolver extends JComponent implements Runnable {
26  
27      protected Frame frame;
28      protected Window fullscreen;
29      protected int count;
30      protected BufferedImage frame_buffer;
31      protected BufferedImage screen_buffer;
32      private Logger LOG = Logger.getLogger(Dissolver.class.getName());
33  
34      public void dissolveExit(JFrame frame) {
35          try
36          {
37              this.frame = frame;
38              Robot robot = new Robot();
39              //capture screen with frame to frame buffer
40              Rectangle frame_rectangle = frame.getBounds();
41              frame_buffer = robot.createScreenCapture(frame_rectangle);
42  
43              //Hide frame
44              frame.setVisible(false);
45  
46              //capture screen without frame
47              Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
48              Rectangle screen_rect = new Rectangle(0, 0, screensize.width, screensize.height);
49              screen_buffer = robot.createScreenCapture(screen_rect);
50  
51              // create big window without decorations
52              fullscreen = new Window(new JFrame());
53              fullscreen.setSize(screensize);
54              fullscreen.add(this);
55              this.setSize(screensize);
56              fullscreen.setVisible(true);
57  
58              // start animation
59              new Thread(this).start();
60          } catch(AWTException awt) {
61              LOG.log(Level.WARNING, "Dissolve problem ", awt);
62          }
63      }
64  
65      public void run() {
66          count = 0;
67          try
68          {
69              Thread.currentThread().sleep(100);
70              for (int i = 0; i < 20; i ++) {
71                  count = i;
72                  fullscreen.repaint();
73                  Thread.currentThread().sleep(100);
74              }
75          } catch (InterruptedException iex) {
76              iex.printStackTrace();
77          }
78          System.exit(0);
79      }
80  
81      public void paint(Graphics g) {
82          Graphics2D g2 = (Graphics2D) g;
83          // draw the screen, offset in case the window isn't at 0,0
84          g.drawImage(screen_buffer, -fullscreen.getX(), -fullscreen.getY(), null);
85  
86          // draw the frame
87          Composite old_comp = g2.getComposite();
88          Composite fade = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f-((float)count)/20f);
89          g2.setComposite(fade);
90          g2.drawImage(frame_buffer, frame.getX(), frame.getY(), null);
91          g2.setComposite(old_comp);
92      }
93  }