Created
April 19, 2015 17:02
-
-
Save l4p4/96d9eb0fdd4ed7885e6b to your computer and use it in GitHub Desktop.
Usa a classe Robot para fazer printscreens a cada 15s, e a cada 5s mover o mouse aleatóriamente e dar dois clicks.
This file contains 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
package brinks; | |
import java.awt.AWTException; | |
import java.awt.Dimension; | |
import java.awt.Rectangle; | |
import java.awt.Robot; | |
import java.awt.Toolkit; | |
import java.awt.event.InputEvent; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Path; | |
import java.util.Random; | |
import javax.imageio.ImageIO; | |
public class Brinks { | |
public static void main(String[] args) { | |
try { | |
final Robot r = new Robot(); | |
r.delay(10000); // 10s | |
Runnable prog = new Runnable() { | |
@Override | |
public void run() { | |
new Thread() { | |
@Override | |
public void run() { | |
try { | |
while (true) { | |
printScreen(r); | |
Thread.sleep(15000); // 15s | |
} | |
} catch (IOException | InterruptedException e) { | |
} | |
}; | |
}.start(); | |
while (true) { | |
moveMouse(r); | |
doubleClick(r); | |
try { | |
Thread.sleep(5000); // 5s | |
} catch (InterruptedException e) { | |
} | |
} | |
} | |
}; | |
new Thread(prog).start(); | |
} catch (AWTException e) { | |
} | |
} | |
/** | |
* @param r | |
* Instance of Robot. | |
* @throws IOException | |
* Exception. | |
* | |
*/ | |
protected static void printScreen(Robot r) throws IOException { | |
BufferedImage bi = r.createScreenCapture(new Rectangle(0, 0, getScreenSize().width, | |
getScreenSize().height)); | |
File outputfile = new File(getDirSaveFile() + (getPlatform() == 1 ? "\\" : "/") | |
+ getRandomInt(1000000000) + ".jpg"); | |
ImageIO.write(bi, "jpg", outputfile); | |
} | |
/** | |
* Mouse left click. | |
* | |
* @param r | |
* Instance of Robot | |
* | |
*/ | |
protected static void click(Robot r) { | |
r.mousePress(InputEvent.BUTTON1_MASK); | |
r.mouseRelease(InputEvent.BUTTON1_MASK); | |
} | |
/** | |
* Mouse Double left click | |
* | |
* @param r | |
* Instance of Robot. | |
* | |
*/ | |
protected static void doubleClick(Robot r) { | |
click(r); | |
click(r); | |
} | |
/** | |
* @param r | |
* Instance of Robot. | |
* | |
*/ | |
protected static void moveMouse(Robot r) { | |
Dimension d = getScreenSize(); | |
r.mouseMove(d.width - getRandomInt(d.width), d.height - getRandomInt(d.height)); | |
} | |
/** | |
* | |
* @param v | |
* Value to generate random. | |
* @return Random between 0 and v. | |
*/ | |
private static Integer getRandomInt(int v) { | |
return new Random().nextInt(v); | |
} | |
/** | |
* | |
* @return Dimension of screen. | |
*/ | |
private static Dimension getScreenSize() { | |
return Toolkit.getDefaultToolkit().getScreenSize(); | |
} | |
/** | |
* | |
* @return Path to paste. | |
*/ | |
protected static Path getDirSaveFile() { | |
Path p = null; | |
File f0 = new File("printscreen"); | |
File f = new File(f0.getAbsolutePath()); | |
f.mkdirs(); | |
p = f.toPath(); | |
return p; | |
} | |
/** | |
* | |
* @return 1 if windows. 0 others platform. | |
*/ | |
public static int getPlatform() { | |
return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 ? 1 : 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment