Created
September 22, 2012 06:08
-
-
Save TonyWhite/3765315 to your computer and use it in GitHub Desktop.
Inerzia
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
| /** | |
| * Il mouse slitta come se non avesse inerzia | |
| * | |
| * Autore: Antonio Bianco | |
| * Versione: |0| | |
| */ | |
| import java.awt.Dimension; | |
| import java.awt.Toolkit; | |
| import java.awt.Robot; // Controlla il mouse | |
| import java.awt.MouseInfo; // Leggi le informazioni del mouse | |
| import java.awt.PointerInfo; | |
| import java.awt.Point; | |
| public class Inerzia extends Thread | |
| { | |
| // instance variables - replace the example below with your own | |
| private int prevX; | |
| private int prevY; | |
| private int maxX; | |
| private int maxY; | |
| private int differenzaX; | |
| private int differenzaY; | |
| private Robot r; | |
| private boolean scassaLePalle; | |
| private JClipboard clipboard; | |
| public static void main(String args[]) | |
| { | |
| new Inerzia(); | |
| } | |
| /** | |
| * Constructor for objects of class Inerzia | |
| */ | |
| public Inerzia() | |
| { | |
| super("Inerzia"); | |
| try | |
| { | |
| r = new Robot(); | |
| scassaLePalle = true; | |
| clipboard = new JClipboard(); | |
| clipboard.toClipboard(""); | |
| Dimension dimensioniSchermo = Toolkit.getDefaultToolkit().getScreenSize(); // Legge le dimensioni dello schermo | |
| maxX = dimensioniSchermo.width - 1; | |
| maxY = dimensioniSchermo.height - 1; | |
| } | |
| catch (Exception e) | |
| { | |
| System.out.println("Impossibile inizializzare la classe Robot.\nProkramma kapùtt"); | |
| System.exit(1); | |
| } | |
| PointerInfo info = MouseInfo.getPointerInfo(); | |
| Point punto = info.getLocation(); | |
| prevX = (int)punto.getX(); | |
| prevY = (int)punto.getY(); | |
| this.start(); | |
| } | |
| public void run() | |
| { | |
| while(scassaLePalle) | |
| { | |
| try | |
| { | |
| // Legge la posizione attuale del mouse | |
| PointerInfo info = MouseInfo.getPointerInfo(); | |
| Point punto = info.getLocation(); | |
| int x = (int)punto.getX(); | |
| int y = (int)punto.getY(); | |
| // Scivola... | |
| differenzaX = x-prevX; | |
| differenzaY = y-prevY; | |
| prevX = x; | |
| prevY = y; | |
| x = x+differenzaX; | |
| y = y+differenzaY; | |
| // Non bisogna avvicinarsi ai bordi (necessario solo per Windows) | |
| if (x<1) x=1; | |
| else if (x>maxX-1) x=maxX-1; | |
| if (y<1) y=1; | |
| else if (y>maxY-1) y=maxY-1; | |
| r.mouseMove(x, y); | |
| // Esce dall'applicazione | |
| if (clipboard.fromClipboard().equals("Geek rulez")) | |
| { | |
| this.interrupt(); | |
| System.exit(0); | |
| } | |
| Thread.sleep(25); | |
| } | |
| catch(Exception e) | |
| { | |
| // Nope | |
| } | |
| } | |
| } | |
| } |
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 java.awt.datatransfer.*; | |
| import java.awt.*; | |
| public class JClipboard implements ClipboardOwner | |
| { | |
| /** | |
| * Incolla nella Clipboard | |
| */ | |
| public void toClipboard(String contenuto) | |
| { | |
| StringSelection st = new StringSelection(contenuto); | |
| Clipboard cp = Toolkit.getDefaultToolkit().getSystemClipboard(); | |
| cp.setContents(st, this); | |
| } | |
| /** | |
| * Legge il testo dalla clipboard di sistema | |
| */ | |
| public String fromClipboard() | |
| { | |
| String contenuto = null; | |
| Clipboard cp = Toolkit.getDefaultToolkit().getSystemClipboard(); | |
| Transferable contents = cp.getContents(null); | |
| boolean hasTransferableText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor); //Se c'è il contenuto ed è una stringa | |
| if (hasTransferableText) | |
| { | |
| try | |
| { | |
| contenuto = (String)contents.getTransferData(DataFlavor.stringFlavor); | |
| } | |
| catch(Exception e){} | |
| } | |
| return contenuto; | |
| } | |
| public void lostOwnership(Clipboard clip, Transferable tr){} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment