




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Java實驗周報告 經(jīng)濟(jì)與管理學(xué)院信息管理與信息系統(tǒng)專業(yè) java實驗周報告 (2015/2016學(xué)年 第一學(xué)期) 學(xué)生姓名: 學(xué)生班級: 學(xué)生學(xué)號: 指導(dǎo)教師: 2015年12月25日11 實現(xiàn)屏幕截圖的小程序一、實驗題目實現(xiàn)屏幕截圖的小程序二、實驗要求編程一個應(yīng)用小程序,能夠具有屏幕截圖的功能,截圖的具體實現(xiàn)有:(1)顯示出工作區(qū)域,即能夠截屏的面積;(2)鼠標(biāo)可以隨意滑動進(jìn)行截圖;(3)將所截取的圖片保存在想要保存的位置;(4)程序結(jié)束后可以退出整個應(yīng)用。三、程序流程圖3.1 業(yè)務(wù)流程圖4、 技術(shù)原理程序的主類是cutScreen,繼承自無邊框的框架JWindow;cutScreen()
2、是一個定義屏幕尺寸的構(gòu)造方法;使用方法mousePressed(MouseEvent e)來監(jiān)聽當(dāng)前鼠標(biāo)點擊的動作;用方法mouseReleased(MouseEvent e)監(jiān)聽鼠標(biāo)松開時,顯示操作窗口;方法mouseDragged(MouseEvent e)監(jiān)聽拖動鼠標(biāo);paint(Graphics g)畫出指定的工作區(qū)域;saveImage()保存圖像。工具欄ToolsWindow類,繼承自有邊框的框架JFrame;方法init()用來設(shè)置布局方式為BorderLayout;run()捕捉屏幕截圖。五、附實驗代碼import java.awt.*;import java.awt.even
3、t.*;import java.awt.image.BufferedImage;import java.awt.image.RescaleOp;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import javax.imageio.ImageIO;import javax.swing.*;import javax.swing.filechooser.FileNameExtensionFilter;import javax.swing.f
4、ilechooser.FileSystemView;/Jwindow 是一個無邊框的框架 public class cutScreen extends JWindow /beginX 開始的橫坐標(biāo); beginY開始的縱坐標(biāo)private int beginX, beginY, endX, endY;private BufferedImage image = null;private BufferedImage tempImage = null;private BufferedImage saveImage = null;private ToolsWindow tools = null; /構(gòu)
5、造方法public cutScreen() throws AWTException, IOException / 獲取屏幕尺寸寬和高int width = Toolkit.getDefaultToolkit().getScreenSize().width;int height = Toolkit.getDefaultToolkit().getScreenSize().height;/ 設(shè)置窗口大小/(0, 0, width, height)第一個0代表橫坐標(biāo) ,第二個代表縱坐標(biāo)this.setBounds(0, 0, width, height);/ 截取屏幕Robot robot = new
6、 Robot();/參數(shù)Rectangle是代表工作區(qū)域image = robot.createScreenCapture(new Rectangle(0, 0, width, height);ImageIO.write(image, "jpg", new File("d:/1");/ 本窗口添加監(jiān)聽(適配器)this.addMouseListener(new MouseAdapter() Override/當(dāng)前鼠標(biāo)點擊動作public void mousePressed(MouseEvent e) beginX = e.getX();beginY =
7、e.getY();if (tools != null) tools.setVisible(false);Overridepublic void mouseReleased(MouseEvent e) / 鼠標(biāo)松開時,顯示操作窗口if (tools = null) tools = new ToolsWindow(cutScreen.this, e.getX(), e.getY(); else tools.setLocation(e.getX(), e.getY();tools.setVisible(true);/ 將此窗口置于前端,并可以將其設(shè)為焦點 Windowtools.toFront();
8、);/ 監(jiān)聽拖動鼠標(biāo)this.addMouseMotionListener(new MouseMotionAdapter() Overridepublic void mouseDragged(MouseEvent e) / 鼠標(biāo)拖動時,記錄坐標(biāo)并重繪窗口endX = e.getX();endY = e.getY();/ 臨時圖像,用于緩沖屏幕區(qū)域放置屏幕閃爍Image tempImage2 = createImage(cutScreen.this.getWidth(),cutScreen.this.getHeight();Graphics g = tempImage2.getGraphics(
9、);g.drawImage(tempImage, 0, 0, null);int x = Math.min(beginX, endX);int y = Math.min(beginY, endY);int width2 = Math.abs(endX - beginX) + 1;int height2 = Math.abs(endY - beginY) + 1;g.drawRect(x - 1, y - 1, width2 + 1, height2 + 1);/ 生成子區(qū)域流圖片saveImage = image.getSubimage(x, y, width2, height2);/畫出圖片
10、g.drawImage(saveImage, x, y, null); /繪制當(dāng)前指定的區(qū)域的圖片cutScreen.this.getGraphics().drawImage(tempImage2, 0, 0,cutScreen.this);); /Override/畫出指定的工作區(qū)域public void paint(Graphics g) /進(jìn)行逐像素重縮放RescaleOp ro = new RescaleOp(0.8f, 0, null);tempImage = ro.filter(image, null);g.drawImage(tempImage, 0, 0, this);/ 保存
11、圖像到文件public void saveImage() throws IOException JFileChooser jfc = new JFileChooser();jfc.setDialogTitle("保存");/ 文件過濾器,用戶過濾可選擇文件FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG","jpg");jfc.setFileFilter(filter);/ 初始化一個默認(rèn)文件(此文件會生成到桌面上)/ 生成時間SimpleDateFor
12、mat sdf = new SimpleDateFormat("yyyymmddHHmmss");String fileName = sdf.format(new Date();File filePath = FileSystemView.getFileSystemView().getHomeDirectory();File defaultFile = new File(filePath + File.separator + fileName+ ".jpg");jfc.setSelectedFile(defaultFile);int flag = jfc
13、.showSaveDialog(this);if (flag = JFileChooser.APPROVE_OPTION) File file = jfc.getSelectedFile();String path = file.getPath();/System.out.println(path);/ 檢查文件后綴,放置用戶忘記輸入后綴或者輸入不正確的后綴if (!(path.endsWith(".jpg") | path.endsWith(".JPG") path += ".jpg"/ 寫入文件ImageIO.write(save
14、Image, "jpg", new File(path);System.exit(0);/* * 操作窗口 */ToolsWindow 內(nèi)部類class ToolsWindow extends JFrame private cutScreen parent; /構(gòu)造函數(shù)(cutScreen,int x, int y)x代表鼠標(biāo)釋放位置的橫坐標(biāo),public ToolsWindow(cutScreen parent, int x, int y) this.parent = parent;this.init();this.setLocation(x, y);/讓窗口里面的組建確
15、定為最佳大小this.pack();this.setVisible(true);private void init() /設(shè)置布局方式為BorderLayoutthis.setLayout(new BorderLayout();/工具欄JToolBar toolBar = new JToolBar();/ 保存按鈕JButton saveButton = new JButton("保存");/匿名內(nèi)部類添加監(jiān)聽saveButton.addActionListener(new ActionListener() Overridepublic void actionPerform
16、ed(ActionEvent e) try parent.saveImage(); catch (IOException e1) e1.printStackTrace(););toolBar.add(saveButton);/ 關(guān)閉按鈕JButton closeButton = new JButton("關(guān)閉");closeButton.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) System.exit(0););toolBar.add(c
17、loseButton);this.add(toolBar, BorderLayout.CENTER);public static void main(String args) EventQueue.invokeLater(new Runnable() Overridepublic void run() try cutScreen cutScreen = new cutScreen();cutScreen.setVisible(true); catch (Exception e) e.printStackTrace(););六、實驗結(jié)果圖6.1 截圖圖6.2 保存七、個人總結(jié)一周的課程設(shè)計結(jié)束了,剛開始對所要設(shè)計的課題完全沒有頭緒
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 冷庫拆遷合同范例
- 鄉(xiāng)鎮(zhèn)門店送貨合同范例
- 事故車車輛維修合同范例
- 農(nóng)村荒地合作合同范本
- 做蔬菜大棚合同范例
- 中藥設(shè)備租賃合同范例
- 傳統(tǒng)年畫元素在雕塑創(chuàng)作中的運用
- 無碰撞磁場重聯(lián)中能量轉(zhuǎn)換和靜電孤立波的粒子模擬研究
- 買賣定金合同范例
- 鋼筋石籠施工方案
- 2024年廣西農(nóng)村投資集團(tuán)應(yīng)屆生校園招考聘用85人(高頻重點復(fù)習(xí)提升訓(xùn)練)共500題附帶答案詳解
- 《建筑門窗玻璃幕墻熱工計算規(guī)程》JGJ@T151
- 老年人智能手機(jī)使用教程含內(nèi)容課件
- DZ∕T 0219-2006 滑坡防治工程設(shè)計與施工技術(shù)規(guī)范(正式版)
- 家族族譜資料收集表
- 混凝土實測實量記錄表
- 2024年人力資源管理師三級考試真題及答案
- 2024年中國遠(yuǎn)洋海運集團(tuán)有限公司招聘筆試沖刺題(帶答案解析)
- 高等職業(yè)學(xué)校電梯工程技術(shù)專業(yè)實訓(xùn)教學(xué)條件建設(shè)標(biāo)準(zhǔn)(征求意見稿)
- 2024年錦州師范高等??茖W(xué)校單招職業(yè)技能測試題庫及答案解析
- 商品房施工組織設(shè)計
評論
0/150
提交評論