读书人

Swing 兑现截图小软件 (二)

发布时间: 2012-12-28 10:29:04 作者: rapoo

Swing 实现截图小软件 (二)

?

刚才写了 Swing 实现截图小软件(一)后,点击“保存草稿”,回来再修改,点“编辑”在“可视化编辑器”中就出现了一堆的HTML代码了。 写不下去了,就直接转到(二)好了。

?

接上一节

?

第三步:为截取图像时,鼠标所标示的截取区域用矩形表示出来。

要实现根据鼠标的拖动,实时画矩形,可以采用

1. 取得鼠标的按下点和移动当前点坐标

2. 创建一个缓冲图形对象(BufferedImage) bi

3. 将原始图形画到 bi 中

4. 根据取得的坐标画一个矩形到 bi 中

5. 将 bi 画到屏幕上

?

?

/** * 截图屏幕中制定区域的图片 * @param x * @param y * @param w * @param h * @return 被截部分的BufferedImage对象 * @throws AWTException * @throws InterruptedException */public static BufferedImage getScreenImage(int x, int y, int w, int h) throws AWTException, InterruptedException {Robot robot = new Robot();w = w > 0 ? w : 1;h = h > 0 ? h : 1;BufferedImage screen = robot.createScreenCapture(new Rectangle(x, y, w, h));return screen;}
5 楼 沙舟狼客 2011-03-15 写的挺好的,就是有一点不足,文件保存的格式没有
[code language="Java"]
file = chooser.getSelectedFile();
System.out.println(file.getAbsolutePath());
file = new File(file.getAbsoluteFile()+".jpg");
6 楼 如果风…… 2011-05-13 我写了一下,怎么就不能运行的啊, 7 楼 如果风…… 2011-05-13 我检查了一下我写的代码,没有错啊, 8 楼 如果风…… 2011-05-13 呵呵,是我的错啊,最后居然忘记调事件了。晕

读书人网 >编程

热点推荐