/**
*
*/
package de.tutorials;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
/**
* @author Thomas.Darimont
*
*/
public class MovingFrameExample extends JFrame {
public MovingFrameExample() {
super("MovingFrameExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(120, 90);
setVisible(true);
Executors.newSingleThreadExecutor().execute(new Runnable() {
public void run() {
DisplayMode currentDisplayMode = GraphicsEnvironment
.getLocalGraphicsEnvironment().getScreenDevices()[0]
.getDisplayMode();
int dx = 5;
int dy = 5;
double scaleFactor = 1.05;
Point currentLocation = getLocation();
double currentScaleFactor = scaleFactor;
while (true) {
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (getWidth() > 360) {
currentScaleFactor = 1 / scaleFactor;
}
if (getWidth() < 120) {
currentScaleFactor = scaleFactor;
}
setSize((int) (getWidth() * currentScaleFactor),
(int) (getHeight() * currentScaleFactor));
getLocation(currentLocation);
if (currentLocation.x < 0) {
dx *= -1;
}
if (currentLocation.x + getHeight() > currentDisplayMode
.getWidth()) {
dx *= -1;
}
if (currentLocation.y < 0) {
dy *= -1;
}
if (currentLocation.y + getHeight() > currentDisplayMode
.getHeight()) {
dy *= -1;
}
currentLocation.translate(dx, dy);
setLocation(currentLocation);
}
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
new MovingFrameExample();
}
}