⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 screenshotframe.java

📁 rmi C/S 客 户 端 与 服 务 端 连 接
💻 JAVA
字号:
package com.topking.rt.ui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;

/*
 * ScreenShotFrame.java
 *
 * Created on 2009年2月10日, 下午2:19
 */



/**
 *
 * @author  lzkj
 */
public class ScreenShotFrame extends javax.swing.JFrame {

    /** Creates new form ScreenShotFrame */
    public ScreenShotFrame(BufferedImage image) {
    	this.image = image;
    	this.setTitle("Screen Shot Image");    	
        initComponents();
        panel_shot.setImage(image);
        panel_shot.updateUI();
        panel_shot.repaint();
        setFrameLocation();
    }

    public void setFrameLocation(){
    	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    	int Width = (int) dim.getWidth();
    	int Height = (int) dim.getHeight();
    	int fWidth = this.getWidth();
    	int fHeight = this.getHeight();
    	
    	this.setLocation((Width-fWidth)/2, (Height-fHeight)/2);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    	panel_shot = new MyPanel(null);
        jMenuBar1 = new javax.swing.JMenuBar();
        menu_file = new javax.swing.JMenu();
        item_save = new javax.swing.JMenuItem();

        scrollPane = new JScrollPane(panel_shot,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        
        item_save.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				saveImage();
			}
        	
        });
        

        javax.swing.GroupLayout panel_shotLayout = new javax.swing.GroupLayout(panel_shot);
        panel_shot.setLayout(panel_shotLayout);
        panel_shotLayout.setHorizontalGroup(
            panel_shotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 817, Short.MAX_VALUE)
        );
        panel_shotLayout.setVerticalGroup(
            panel_shotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 527, Short.MAX_VALUE)
        );

        menu_file.setText("File");

        item_save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        item_save.setText("Save");
        menu_file.add(item_save);

        jMenuBar1.add(menu_file);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>

    public void saveImage(){
    	if(image==null){
    		JOptionPane.showMessageDialog(this, "Save File Fail,There is No Image","Success",JOptionPane.DEFAULT_OPTION);
    		return;
    	}
    		
    	JFileChooser jf = new JFileChooser();    	
    	jf.setFileFilter(new FileFilter(){

			@Override
			public boolean accept(File f) {
				// TODO Auto-generated method stub
				if(f.getName().endsWith(".jpg"))
					return true;
				return false;
			}

			@Override
			public String getDescription() {
				// TODO Auto-generated method stub
				return "Image File *.jpg";
			}
    		
    	});
    	jf.showSaveDialog(this);
    	if(jf.getCurrentDirectory()!=null){
    		String fileName = jf.getCurrentDirectory().getAbsolutePath()+"\\"+jf.getSelectedFile().getName()+"."+IMAGE_SUFFIX;
        	try {
        		OutputStream out = new FileOutputStream(new File(fileName));
				ImageIO.write(image,IMAGE_SUFFIX,out);
				out.flush();
				out.close();
				JOptionPane.showMessageDialog(this, "Save File Success","Success",JOptionPane.DEFAULT_OPTION);
				this.dispose();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				JOptionPane.showMessageDialog(this, "Exception:"+e.getMessage(),"Save Image Fail",JOptionPane.ERROR_MESSAGE);
			}
    		System.err.println(fileName);
    	}    	
    }

    // Variables declaration - do not modify
    private javax.swing.JMenuItem item_save;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu menu_file;
    private MyPanel panel_shot;
    private JScrollPane scrollPane;
    private BufferedImage image;
    private static final String IMAGE_SUFFIX = "jpg";
    // End of variables declaration

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -