import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import com.sun.awt.AWTUtilities;
public class SplitFrame extends JFrame {
public SplitFrame() {
super("SplitFrame");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
@Override
public void dispose() {
split();
super.dispose();
}
private void split() {
Image frameImage = getFrameImage();
AWTUtilities.setWindowOpacity(SplitFrame.this,0f);
Image image = getScreenshot();
if (image!=null) {
new SplitAnimation(image,frameImage).start();
}
}
private Image getFrameImage() {
try {
Robot rob = new Robot();
Rectangle rect = new Rectangle(getX(),getY(),getWidth(),getHeight());
return rob.createScreenCapture(rect);
} catch (AWTException e) {
e.printStackTrace();
return null;
}
}
private Image getScreenshot() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
try {
Robot rob = new Robot();
Rectangle screen = new Rectangle(0,0,dim.width,dim.height);
return rob.createScreenCapture(screen);
} catch (AWTException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new SplitFrame();
frame.setLayout(new FlowLayout());
for (int i=0;i<16;i++)
frame.add(new JButton("Button "+i));
frame.setSize(600,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private class SplitAnimation extends JPanel {
private static final int DURATION = 1000;
private static final int DELTATIME = 30;
private JFrame frame;
private Image background,image;
private float progress;
private int scrW, scrH;
private int frmW, frmH;
private SplitAnimation(Image bg, Image image) {
this.background = bg;
this.image = image;
this.scrW = bg.getWidth(null);
this.scrH = bg.getHeight(null);
this.frmW = image.getWidth(null);
this.frmH = image.getHeight(null);
frame = new JFrame();
frame.setUndecorated(true);
frame.setSize(scrW,scrH);
frame.add(this);
}
private void start() {
frame.setVisible(true);
new Thread() {
public void run() {
animate();
}
}.start();
}
private void animate() {
progress = 0f;
try {
for (int i=0;i<DURATION;i+=DELTATIME) {
float linprog = (float) i/DURATION;
// für den Beschleunigungseffekt ^4
progress = (float)Math.pow(linprog,4);
repaint();
Thread.sleep(DELTATIME);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
frame.dispose();
}
public void paintComponent(Graphics g) {
int cx = scrW/2; // center x
int cy = scrH/2;
int dx = (int) (progress*cx); // delty x
int dy = (int) (progress*cy);
g.drawImage(background,0,0,null);
g.drawImage(image,cx-dx-frmW/2,cy-dy-frmH/2,cx-dx,cy-dy,0,0,frmW/2,frmH/2,null);
g.drawImage(image,cx+dx,cy-dy-frmH/2,cx+dx+frmW/2,cy-dy,frmW/2,0,frmW,frmH/2,null);
g.drawImage(image,cx+dx,cy+dy,cx+dx+frmW/2,cy+dy+frmH/2,frmW/2,frmH/2,frmW,frmH,null);
g.drawImage(image,cx-dx-frmW/2,cy+dy,cx-dx,cy+dy+frmH/2,0,frmH/2,frmW/2,frmH,null);
}
}
}