import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JFrame;
public class Zittermaus extends JFrame {
public static void main(String[] args) {
new Zittermaus();
}
private int iWidth = 400, iHeight = 3*iWidth/4;
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
MouseEvent mEvent = null;
private Zittermaus() {
super("Zittermaus");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds((d.width-iWidth)/2, (d.height-iHeight)/2, iWidth, iHeight);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
System.exit(0);
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent me) {
mEvent = me;
}
});
addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent me) {
mEvent = null;
}
});
setResizable(false);
setVisible(true);
startZitterThread();
}
private void startZitterThread() {
new Thread(new Runnable() {
public void run() {
try {
Robot rob = new Robot();
Random rnd = new Random();
int iSprungweite = 10; //maximale Sprungweite der Originalposition
while(true) {
if(mEvent != null) {
int newX = mEvent.getXOnScreen() - iSprungweite + rnd.nextInt(iSprungweite*2+1);
int newY = mEvent.getYOnScreen() - iSprungweite + rnd.nextInt(iSprungweite*2+1);
rob.mouseMove(newX, newY);
}
Thread.sleep(10);
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}).start();;
}
}