实现屏幕截图的小程序java课程设计.docx

上传人:b****2 文档编号:1687308 上传时间:2022-10-23 格式:DOCX 页数:10 大小:95.45KB
下载 相关 举报
实现屏幕截图的小程序java课程设计.docx_第1页
第1页 / 共10页
实现屏幕截图的小程序java课程设计.docx_第2页
第2页 / 共10页
实现屏幕截图的小程序java课程设计.docx_第3页
第3页 / 共10页
实现屏幕截图的小程序java课程设计.docx_第4页
第4页 / 共10页
实现屏幕截图的小程序java课程设计.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

实现屏幕截图的小程序java课程设计.docx

《实现屏幕截图的小程序java课程设计.docx》由会员分享,可在线阅读,更多相关《实现屏幕截图的小程序java课程设计.docx(10页珍藏版)》请在冰豆网上搜索。

实现屏幕截图的小程序java课程设计.docx

实现屏幕截图的小程序java课程设计

经济与管理学院信息管理与信息系统专业

《java实验周》报告

(2015/2016学年第一学期)

 

学生姓名:

学生班级:

学生学号:

指导教师:

 

2015年12月25日

实现屏幕截图的小程序

一、实验题目

实现屏幕截图的小程序

二、实验要求

编程一个应用小程序,能够具有屏幕截图的功能,截图的具体实现有:

(1)显示出工作区域,即能够截屏的面积;

(2)鼠标可以随意滑动进行截图;

(3)将所截取的图片保存在想要保存的位置;

(4)程序结束后可以退出整个应用。

三、程序流程

图3.1业务流程图

 

4、技术原理

程序的主类是cutScreen,继承自无边框的框架JWindow;cutScreen()是一个定义屏幕尺寸的构造方法;使用方法mousePressed(MouseEvente)来监听当前鼠标点击的动作;用方法mouseReleased(MouseEvente)监听鼠标松开时,显示操作窗口;方法mouseDragged(MouseEvente)监听拖动鼠标;paint(Graphicsg)画出指定的工作区域;saveImage()保存图像。

工具栏ToolsWindow类,继承自有边框的框架JFrame;方法init()用来设置布局方式为BorderLayout;run()捕捉屏幕截图。

五、附实验代码

importjava.awt.*;

importjava.awt.event.*;

importjava.awt.image.BufferedImage;

importjava.awt.image.RescaleOp;

importjava.io.File;

importjava.io.IOException;

importjava.text.SimpleDateFormat;

importjava.util.Date;

importjavax.imageio.ImageIO;

importjavax.swing.*;

importjavax.swing.filechooser.FileNameExtensionFilter;

importjavax.swing.filechooser.FileSystemView;

//Jwindow是一个无边框的框架

publicclasscutScreenextendsJWindow{

//beginX开始的横坐标;beginY开始的纵坐标

privateintbeginX,beginY,endX,endY;

privateBufferedImageimage=null;

privateBufferedImagetempImage=null;

privateBufferedImagesaveImage=null;

privateToolsWindowtools=null;

//构造方法

publiccutScreen()throwsAWTException,IOException{

//获取屏幕尺寸宽和高

intwidth=Toolkit.getDefaultToolkit().getScreenSize().width;

intheight=Toolkit.getDefaultToolkit().getScreenSize().height;

//设置窗口大小

//(0,0,width,height)第一个0代表横坐标,第二个代表纵坐标

this.setBounds(0,0,width,height);

//截取屏幕

Robotrobot=newRobot();

//参数Rectangle是代表工作区域

image=robot.createScreenCapture(newRectangle(0,0,width,height));

ImageIO.write(image,"jpg",newFile("d:

/1"));

//本窗口添加监听(适配器)

this.addMouseListener(newMouseAdapter(){

@Override

//当前鼠标点击动作

publicvoidmousePressed(MouseEvente){

beginX=e.getX();

beginY=e.getY();

if(tools!

=null){

tools.setVisible(false);

}

}

@Override

publicvoidmouseReleased(MouseEvente){

//鼠标松开时,显示操作窗口

if(tools==null){

tools=newToolsWindow(cutScreen.this,e.getX(),e.getY());

}else{

tools.setLocation(e.getX(),e.getY());

}

tools.setVisible(true);

//将此窗口置于前端,并可以将其设为焦点Window

tools.toFront();

}

});

//监听拖动鼠标

this.addMouseMotionListener(newMouseMotionAdapter(){

@Override

publicvoidmouseDragged(MouseEvente){

//鼠标拖动时,记录坐标并重绘窗口

endX=e.getX();

endY=e.getY();

//临时图像,用于缓冲屏幕区域放置屏幕闪烁

ImagetempImage2=createImage(cutScreen.this.getWidth(),

cutScreen.this.getHeight());

Graphicsg=tempImage2.getGraphics();

g.drawImage(tempImage,0,0,null);

intx=Math.min(beginX,endX);

inty=Math.min(beginY,endY);

intwidth2=Math.abs(endX-beginX)+1;

intheight2=Math.abs(endY-beginY)+1;

g.drawRect(x-1,y-1,width2+1,height2+1);

//生成子区域流图片

saveImage=image.getSubimage(x,y,width2,height2);

//画出图片

g.drawImage(saveImage,x,y,null);

//绘制当前指定的区域的图片

cutScreen.this.getGraphics().drawImage(tempImage2,0,0,

cutScreen.this);

}

});

}

//@Override

//画出指定的工作区域

publicvoidpaint(Graphicsg){

//进行逐像素重缩放

RescaleOpro=newRescaleOp(0.8f,0,null);

tempImage=ro.filter(image,null);

g.drawImage(tempImage,0,0,this);

}

//保存图像到文件

publicvoidsaveImage()throwsIOException{

JFileChooserjfc=newJFileChooser();

jfc.setDialogTitle("保存");

//文件过滤器,用户过滤可选择文件

FileNameExtensionFilterfilter=newFileNameExtensionFilter("JPG",

"jpg");

jfc.setFileFilter(filter);

//初始化一个默认文件(此文件会生成到桌面上)

//生成时间

SimpleDateFormatsdf=newSimpleDateFormat("yyyymmddHHmmss");

StringfileName=sdf.format(newDate());

FilefilePath=FileSystemView.getFileSystemView().getHomeDirectory();

FiledefaultFile=newFile(filePath+File.separator+fileName

+".jpg");

jfc.setSelectedFile(defaultFile);

intflag=jfc.showSaveDialog(this);

if(flag==JFileChooser.APPROVE_OPTION){

Filefile=jfc.getSelectedFile();

Stringpath=file.getPath();

//System.out.println(path);

//检查文件后缀,放置用户忘记输入后缀或者输入不正确的后缀

if(!

(path.endsWith(".jpg")||path.endsWith(".JPG"))){

path+=".jpg";

}

//写入文件

ImageIO.write(saveImage,"jpg",newFile(path));

System.exit(0);

}

}

/*

*操作窗口

*/

//ToolsWindow内部类

classToolsWindowextendsJFrame{

privatecutScreenparent;

//构造函数(cutScreen,intx,inty)x代表鼠标释放位置的横坐标,

publicToolsWindow(cutScreenparent,intx,inty){

this.parent=parent;

this.init();

this.setLocation(x,y);

//让窗口里面的组建确定为最佳大小

this.pack();

this.setVisible(true);

}

privatevoidinit(){

//设置布局方式为BorderLayout

this.setLayout(newBorderLayout());

//工具栏

JTool

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1