Last active
August 29, 2015 14:16
-
-
Save gervaisb/158ac89c57e1988b81b4 to your computer and use it in GitHub Desktop.
An effect who reduce a JFrame to the lower right corner of the screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
public abstract class Effects { | |
public static void run(Effects effect, Runnable onComplete) { | |
run(effect, 500, onComplete); | |
} | |
public static void run(final Effects effect, final long durationInMillis, final Runnable onComplete) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
int steps = effect.getSteps(); | |
long delay = durationInMillis/steps; | |
effect.start(); | |
while ( effect.next() ) { | |
try { | |
Thread.sleep(delay); | |
} catch (InterruptedException e) {/**/} | |
} | |
onComplete.run(); | |
} | |
}).start(); | |
} | |
protected abstract int getSteps(); | |
protected abstract void start(); | |
protected abstract boolean next(); | |
protected abstract void done(); | |
public static class Reduction extends Effects { | |
private static final int VELOCITY = 80; | |
private final Ghost ghost; | |
private final JFrame frame; | |
private int translationY; | |
private int translationX; | |
private int shrinkHeight; | |
private int shrinkWIdth; | |
private Rectangle screen; | |
public Reduction(JFrame frame) { | |
this.frame = frame; | |
ghost = new Ghost(frame); | |
} | |
@Override | |
protected int getSteps() { | |
screen = new Rectangle(new Point(0, 0), Toolkit.getDefaultToolkit().getScreenSize()); | |
final double distance = ghost.getLocation().distance(screen.getWidth(), screen.getHeight()); | |
final int steps = (int) (distance / VELOCITY); | |
translationY = (int) ((screen.getHeight() - ghost.getY()) / steps); | |
translationX = (int) ((screen.getWidth() - ghost.getX()) / steps); | |
shrinkHeight = ghost.getHeight() / steps; | |
shrinkWIdth = ghost.getWidth() / steps; | |
return steps; | |
} | |
@Override | |
protected void start() { | |
ghost.setVisible(true); | |
frame.setVisible(false); | |
} | |
@Override | |
protected boolean next() { | |
ghost.setLocation(ghost.getX() + translationX, ghost.getY() + translationY); | |
ghost.setSize(ghost.getWidth() - shrinkWIdth, ghost.getHeight() - shrinkHeight); | |
return screen.contains(ghost.getLocation()); | |
} | |
@Override | |
protected void done() { | |
ghost.dispose(); | |
} | |
private class Ghost extends JDialog { | |
private final BufferedImage image; | |
public Ghost(JFrame frame) { | |
image = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_ARGB); | |
paint(image.getGraphics()); | |
setLocation(frame.getLocation()); | |
setSize(frame.getSize()); | |
setUndecorated(true); | |
} | |
@Override | |
public void paint(Graphics g) { | |
g.drawImage(image, 0, 0, getWidth(), getHeight(), null); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment