插入源代碼:(以下代碼只是我用來測試VEVb推薦的客戶端中添加源代碼的方法,效果還是比在線差了點(diǎn),內(nèi)容請忽略!!!!!)
/** To change this license header, choose License Headers in PRoject Properties.* To change this template file, choose Tools | Templates* and open the template in the editor.*/package com.silianbo;
/**** @author silianbo*/import java.awt.AWTException;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionAdapter;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.ImageIcon;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JToolBar;import javax.swing.JWindow;import javax.swing.filechooser.FileNameExtensionFilter;import javax.swing.filechooser.FileSystemView;public class ScreenShot {public static void main(String[] args) {
EventQueue.invokeLater(() -> { try { ScreenShotWindow ssw=new ScreenShotWindow(); ssw.setVisible(true); } catch (AWTException e) { } });}}/** 截圖窗口*/class ScreenShotWindow extends JWindow{ private int orgx, orgy, endx, endy; private BufferedImage image=null; private BufferedImage tempImage=null; private BufferedImage saveImage=null;
private ToolsWindow tools=null;
public ScreenShotWindow() throws AWTException{ //獲取屏幕尺寸 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); this.setBounds(0, 0, d.width, d.height);
//截取屏幕 Robot robot = new Robot(); image = robot.createScreenCapture(new Rectangle(0, 0, d.width,d.height));
this.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { //鼠標(biāo)松開時(shí)記錄結(jié)束點(diǎn)坐標(biāo),并隱藏操作窗口 orgx = e.getX(); orgy = e.getY();
if(tools!=null){ tools.setVisible(false); } } @Override public void mouseReleased(MouseEvent e) { //鼠標(biāo)松開時(shí),顯示操作窗口 if(tools==null){ tools=new ToolsWindow(ScreenShotWindow.this,e.getX(),e.getY()); }else{ tools.setLocation(e.getX(),e.getY()); } tools.setVisible(true); tools.toFront(); } });
this.addMouseMotionListener(new MouseMotionAdapter() {
@Override public void mouseDragged(MouseEvent e) { //鼠標(biāo)拖動(dòng)時(shí),記錄坐標(biāo)并重繪窗口 endx = e.getX(); endy = e.getY();
//臨時(shí)圖像,用于緩沖屏幕區(qū)域放置屏幕閃爍 Image tempImage2=createImage(ScreenShotWindow.this.getWidth(),ScreenShotWindow.this.getHeight()); Graphics g =tempImage2.getGraphics(); g.drawImage(tempImage, 0, 0, null); int x = Math.min(orgx, endx); int y = Math.min(orgy, endy); int width = Math.abs(endx - orgx)+1; int height = Math.abs(endy - orgy)+1; // 加上1防止width或height0 g.setColor(Color.BLUE); g.drawRect(x-1, y-1, width+1, height+1); //減1加1都了防止圖片矩形框覆蓋掉 saveImage = image.getSubimage(x, y, width, height); g.drawImage(saveImage, x, y, null);
ScreenShotWindow.this.getGraphics().drawImage(tempImage2,0,0,ScreenShotWindow.this); } });}
@Override public void paint(Graphics g) { RescaleOp ro = new RescaleOp(0.8f, 0, null); tempImage = ro.filter(image, null); g.drawImage(tempImage, 0, 0, this); } //保存圖像到文件public void saveImage() throws IOException { JFileChooser jfc=new JFileChooser(); jfc.setDialogTitle("保存");
//文件過濾器,用戶過濾可選擇文件 FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG", "jpg"); jfc.setFileFilter(filter);
//初始化一個(gè)默認(rèn)文件(此文件會生成到桌面上) SimpleDateFormat 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.showSaveDialog(this); if(flag==JFileChooser.APPROVE_OPTION){ File file=jfc.getSelectedFile(); String path=file.getPath(); //檢查文件后綴,放置用戶忘記輸入后綴或者輸入不正確的后綴 if(!(path.endsWith(".jpg")||path.endsWith(".JPG"))){ path+=".jpg"; } //寫入文件 ImageIO.write(saveImage,"jpg",new File(path)); System.exit(0); }}}/** 操作窗口*/class ToolsWindow extends JWindow{private final ScreenShotWindow parent;
public ToolsWindow(ScreenShotWindow parent,int x,int y) { this.parent=parent; this.init(); this.setLocation(x, y); this.pack(); this.setVisible(true);}
private void init(){
this.setLayout(new BorderLayout()); JToolBar toolBar=new JToolBar("Java 截圖");
//保存按鈕 JButton saveButton=new JButton(new ImageIcon("images/save.gif")); saveButton.addActionListener((ActionEvent e) -> { try { parent.saveImage(); } catch (IOException e1) { e1.printStackTrace(); } }); toolBar.add(saveButton);
//關(guān)閉按鈕 JButton closeButton=new JButton(new ImageIcon("images/close.gif")); closeButton.addActionListener((ActionEvent e) -> { System.exit(0); }); toolBar.add(closeButton);
this.add(toolBar,BorderLayout.NORTH);}}
新聞熱點(diǎn)
疑難解答
圖片精選