Merge pull request #74 from Eric-Turner/Screenshot

[MERGE] Eric-Turner/Screenshot into Parabot/Master
This commit is contained in:
Jeroen Ketelaar
2016-05-22 15:23:30 +02:00
2 changed files with 43 additions and 3 deletions
@@ -40,6 +40,7 @@ public class Directories {
cached.put("Settings", new File(cached.get("Root"), "/Parabot/settings/"));
cached.put("Servers", new File(cached.get("Root"), "/Parabot/servers/"));
cached.put("Cache", new File(cached.get("Root"), "/Parabot/cache/"));
cached.put("Screenshots", new File(cached.get("Root"), "/Parabot/screenshots/"));
Core.verbose("Directories cached.");
clearCache(259200);
@@ -192,6 +193,16 @@ public class Directories {
return cached.get("Home");
}
/**
* Returns the screenshot folder.
*
* @return
*/
public static File getScreenshotDir() {
return cached.get("Screenshots");
}
/**
* Validates all directories and makes them if necessary
*/
+32 -3
View File
@@ -7,13 +7,17 @@ import org.parabot.core.ui.components.VerboseLoader;
import org.parabot.core.ui.images.Images;
import org.parabot.core.ui.utils.SwingUtil;
import org.parabot.environment.OperatingSystem;
import org.parabot.environment.api.utils.StringUtils;
import org.parabot.environment.scripts.Script;
import org.parabot.environment.scripts.randoms.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
@@ -67,7 +71,6 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
}
private void createMenu() {
JMenuBar menuBar = new JMenuBar();
@@ -143,7 +146,33 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
switch (command) {
case "Create screenshot":
JOptionPane.showMessageDialog(this, "We are still working on this...");
try {
Robot robot = new Robot();
Rectangle parabotScreen = new Rectangle((int) getLocation().getX(), (int) getLocation().getY(), getWidth(), getHeight());
BufferedImage image = robot.createScreenCapture(parabotScreen);
String randString = StringUtils.randomString(10);
boolean search = true;
boolean duplicate = false;
while (search == true) {
for (File f : Directories.getScreenshotDir().listFiles()) {
if (f.getAbsoluteFile().getName().contains(randString)) {
duplicate = true;
break;
}
}
if (!duplicate) {
search = false;
} else {
randString = StringUtils.randomString(10);
duplicate = false;
}
}
File file = new File(Directories.getScreenshotDir().getPath() + "/" + randString + ".png");
ImageIO.write(image, "png", file);
} catch (IOException | AWTException k) {
k.printStackTrace();
}
break;
case "Exit":
System.exit(0);
@@ -291,4 +320,4 @@ public class BotUI extends JFrame implements ActionListener, ComponentListener,
public void windowOpened(WindowEvent arg0) {
}
}
}