patternjavaMinor
Java screengrab
Viewed 0 times
screengrabjavastackoverflow
Problem
This is a short class to take screenshots on mutltiple monitor systems, I made some modifications to the solution at https://stackoverflow.com/questions/10042086/screen-capture-in-java-not-capturing-whole-screen so that the code coped with monitors at different hights and so on. Any and all suggestions welcome :)
```
/**
* Code modified from code given in http://whileonefork.blogspot.co.uk/2011/02/java-multi-monitor-screenshots.html following a SE question at
* https://stackoverflow.com/questions/10042086/screen-capture-in-java-not-capturing-whole-screen
*/
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
class ScreenCapture {
static int numberOfMinutesToSleepBetweenScreenshots = 4;
public static void main(String args[]) throws Exception {
int i = 1000;
while (true) {
takeScreenshot("ScreenCapture" + i++);
try {
Thread.sleep(60 numberOfMinutesToSleepBetweenScreenshots 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void takeScreenshot(String filename) throws Exception {
// Okay so all we have to do here is find the screen with the lowest x,
// the screen with the lowest y, the screen with the higtest value of X+
// width and the screen with the highest value of Y+height
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();
Rectangle allScreenBounds = new Rectangle();
int farx = 0;
int fary = 0;
for (GraphicsDevice screen : screens) {
Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
//finding the one corner
if (allScreenBounds.x > screenBounds.x) {
```
/**
* Code modified from code given in http://whileonefork.blogspot.co.uk/2011/02/java-multi-monitor-screenshots.html following a SE question at
* https://stackoverflow.com/questions/10042086/screen-capture-in-java-not-capturing-whole-screen
*/
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
class ScreenCapture {
static int numberOfMinutesToSleepBetweenScreenshots = 4;
public static void main(String args[]) throws Exception {
int i = 1000;
while (true) {
takeScreenshot("ScreenCapture" + i++);
try {
Thread.sleep(60 numberOfMinutesToSleepBetweenScreenshots 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void takeScreenshot(String filename) throws Exception {
// Okay so all we have to do here is find the screen with the lowest x,
// the screen with the lowest y, the screen with the higtest value of X+
// width and the screen with the highest value of Y+height
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();
Rectangle allScreenBounds = new Rectangle();
int farx = 0;
int fary = 0;
for (GraphicsDevice screen : screens) {
Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
//finding the one corner
if (allScreenBounds.x > screenBounds.x) {
Solution
public static void takeScreenshot(String filename) throws Exception {Do not catch or throw generic Exceptions whenever possible. Be explicit about the possible problems.
public static void main(String args[]) throws Exception {That's bad. Your main function should not throw exceptions, but rather handle them gracefully. I assume this is a off-the-shelf template, you should stop doing that (just adding
throws Exception and being done with it).int i = 1000;
while (true) {
takeScreenshot("ScreenCapture" + i++);I know it's minor, but variables with names like
i, j or arr give me the creeps. This variable might be better named counter.Code Snippets
public static void takeScreenshot(String filename) throws Exception {public static void main(String args[]) throws Exception {int i = 1000;
while (true) {
takeScreenshot("ScreenCapture" + i++);Context
StackExchange Code Review Q#10783, answer score: 5
Revisions (0)
No revisions yet.