There is a robot class in the Java standard API that implements the screen and simulates the functions of the mouse keyboard operation. This shows only its screen.
The key method of Createscreencapture (Rectangle rect), which requires a Rectangle object, Rectangle is a rectangular area defining the screen, and constructing Rectangle is fairly easy:
New Rectangle (int x, int y, int width, int height), four parameters are the x-coordinate of the upper-left corner of the rectangle, the y-coordinate of the upper-left corner of the rectangle, the rectangle width, and the rectangle height. The BufferedImage method returns the object as a sample code:
1 /**2 * Specify the screen area, return the BufferedImage object3 * @paramx4 * @paramy5 * @paramwidth6 * @paramHeight7 * @return 8 */9 PublicBufferedImage Getscreenshot (intXintYintWidthintheight) {TenBufferedImage Bfimage =NULL; One Try { ARobot Robot =NewRobot (); -Bfimage = Robot.createscreencapture (NewRectangle (x, y, width, height)); -}Catch(awtexception e) { the e.printstacktrace (); - } - returnBfimage; -}
If you need to keep the file, use Imageio.write (RenderedImage im, String formatname, file output), sample code:
1 /**2 * Specify screen area, save to specified directory3 * @paramx4 * @paramy5 * @paramwidth6 * @paramHeight7 * @paramSavepath-File Save path8 * @paramfilename-File Save name9 * @paramFormat -file formatTen */ One Public voidScreenshotasfile (intXintYintWidthintheight, string savepath, String fileName, string format) { A Try { -Robot Robot =NewRobot (); -BufferedImage bfimage = Robot.createscreencapture (NewRectangle (x, y, width, height)); theFile Path =NewFile (savepath); -File File =NewFile (Path, filename+ "." +format); - imageio.write (bfimage, format, file); -}Catch(awtexception e) { + E.printstacktrace (); -}Catch(IOException e) { + e.printstacktrace (); A } at}
Java screen and clipping