demoimage.java
来自「精通NetBeans光盘源代码,很好很好的资料」· Java 代码 · 共 199 行
JAVA
199 行
/*
* DemoImage.java
*
* Created on 2006年4月2日, 上午1:32
*/
package org.netbeans.swing.image;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
/**
*
* @author boyingking
*/
public class DemoImage extends javax.swing.JFrame {
/** Creates new form DemoImage */
public DemoImage() {
initComponents();
}
private Image createImage() {
try {//截图代码开始
Image tempLocalImage=null;
Robot robot=new Robot();
Dimension dimension= Toolkit.getDefaultToolkit().getScreenSize();
Rectangle scrRect=new Rectangle(0,0,dimension.width,dimension.height);
tempLocalImage=robot.createScreenCapture(scrRect);
//截图代码结束
return tempLocalImage;
} catch (AWTException ex) {
ex.printStackTrace();
}
return null;
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPaneGlobal = new javax.swing.JScrollPane();
jLabelShowIcon = new javax.swing.JLabel();
jMenuBar = new javax.swing.JMenuBar();
jMenuAction = new javax.swing.JMenu();
jMenuItemCapture = new javax.swing.JMenuItem();
jMenuItemSaveAs = new javax.swing.JMenuItem();
jMenuItemNoIcon = new javax.swing.JMenuItem();
jMenuItemExit = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("\u6293\u53d6\u5c4f\u5e55\u7a0b\u5e8f");
jScrollPaneGlobal.setViewportView(jLabelShowIcon);
jMenuAction.setText("\u64cd\u4f5c");
jMenuItemCapture.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
jMenuItemCapture.setText("\u5f00\u59cb\u622a\u56fe");
jMenuItemCapture.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemCaptureActionPerformed(evt);
}
});
jMenuAction.add(jMenuItemCapture);
jMenuItemSaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
jMenuItemSaveAs.setText("\u6587\u4ef6\u53e6\u5b58\u4e3a");
jMenuItemSaveAs.setEnabled(false);
jMenuItemSaveAs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemSaveAsActionPerformed(evt);
}
});
jMenuAction.add(jMenuItemSaveAs);
jMenuItemNoIcon.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
jMenuItemNoIcon.setText("\u6e05\u7a7a\u5185\u5bb9");
jMenuItemNoIcon.setEnabled(false);
jMenuItemNoIcon.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemNoIconActionPerformed(evt);
}
});
jMenuAction.add(jMenuItemNoIcon);
jMenuItemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
jMenuItemExit.setText("\u9000\u51fa");
jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemExitActionPerformed(evt);
}
});
jMenuAction.add(jMenuItemExit);
jMenuBar.add(jMenuAction);
setJMenuBar(jMenuBar);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPaneGlobal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPaneGlobal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
// TODO add your handling code here:
System.exit(0);
}//GEN-LAST:event_jMenuItemExitActionPerformed
private void jMenuItemNoIconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNoIconActionPerformed
// TODO add your handling code here:
this.jLabelShowIcon.setIcon(null);
this.jMenuItemSaveAs.setEnabled(false);
this.jMenuItemNoIcon.setEnabled(false);
this.jMenuItemCapture.setEnabled(true);
}//GEN-LAST:event_jMenuItemNoIconActionPerformed
private void jMenuItemSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveAsActionPerformed
// TODO add your handling code here:
this.saveImage();
}//GEN-LAST:event_jMenuItemSaveAsActionPerformed
private void jMenuItemCaptureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCaptureActionPerformed
// TODO add your handling code here:
jLabelShowIcon.setIcon(new ImageIcon(tempImage=this.createImage()));
this.jMenuItemSaveAs.setEnabled(true);
this.jMenuItemNoIcon.setEnabled(true);
this.jMenuItemCapture.setEnabled(false);
}//GEN-LAST:event_jMenuItemCaptureActionPerformed
/**
* @param args the command line arguments
*/
public void saveImage()
{
try
{
int saved=fileChooser.showSaveDialog(this);
if(saved==JFileChooser.APPROVE_OPTION)
{
//获取要保存
File saveFileName=fileChooser.getSelectedFile();
//获取要保存文件的名字
String fileName=new String(saveFileName.getPath()+".jpg");
//1.首先创建一个输出流
FileOutputStream fileOutPut=new FileOutputStream(fileName);
//2.然后把输出流用JPEG编码器进行包裹,其实就是把输出流连接到编码器
encoder=JPEGCodec.createJPEGEncoder(fileOutPut);
//把BufferedImage对象进行编码
encoder.encode((BufferedImage) tempImage);
fileOutPut.flush();
fileOutPut.close();
}
}
catch(Exception ea)
{
ea.printStackTrace();
}
}
private Image tempImage;
private JPEGImageEncoder encoder;
private JFileChooser fileChooser=new JFileChooser();
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabelShowIcon;
private javax.swing.JMenu jMenuAction;
private javax.swing.JMenuBar jMenuBar;
private javax.swing.JMenuItem jMenuItemCapture;
private javax.swing.JMenuItem jMenuItemExit;
private javax.swing.JMenuItem jMenuItemNoIcon;
private javax.swing.JMenuItem jMenuItemSaveAs;
private javax.swing.JScrollPane jScrollPaneGlobal;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?