📄 demoimage.java
字号:
/*
* DemoImage.java
*
* Created on 2007年8月5日, 下午10:29
*/
package org.Adam;
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 Administrator
*/
public class DemoImage extends javax.swing.JFrame {
/** Creates new form DemoImage */
public DemoImage() {
initComponents();
this.setLocationRelativeTo(this);
}
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=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPaneGlobal = new javax.swing.JScrollPane();
jLabelShowIcon = new javax.swing.JLabel();
jMenuBarAll = new javax.swing.JMenuBar();
jMenuAction = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jScrollPaneGlobal.setViewportView(jLabelShowIcon);
jMenuAction.setText("\u64cd\u4f5c");
jMenuItem1.setText("\u5f00\u59cb\u622a\u56fe");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenuAction.add(jMenuItem1);
jMenuItem2.setText("\u6587\u4ef6\u53e6\u5b58\u4e3a");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenuAction.add(jMenuItem2);
jMenuItem3.setText("\u6e05\u7a7a\u5185\u5bb9");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenuAction.add(jMenuItem3);
jMenuItem4.setText("\u9000\u51fa");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenuAction.add(jMenuItem4);
jMenuBarAll.add(jMenuAction);
setJMenuBar(jMenuBarAll);
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, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jScrollPaneGlobal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
// TODO 将在此处添加您的处理代码:
System.exit(0);
}//GEN-LAST:event_jMenuItem4ActionPerformed
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
// TODO 将在此处添加您的处理代码:
this.jLabelShowIcon.setIcon(null);
this.jMenuItem2.setEnabled(false);
this.jMenuItem3.setEnabled(false);
this.jMenuItem1.setEnabled(true);
}//GEN-LAST:event_jMenuItem3ActionPerformed
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
// TODO 将在此处添加您的处理代码:
this.saveImage();
}//GEN-LAST:event_jMenuItem2ActionPerformed
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO 将在此处添加您的处理代码:
jLabelShowIcon.setIcon(new ImageIcon(tempImage=this.createImage()));
this.jMenuItem2.setEnabled(true);
this.jMenuItem3.setEnabled(true);
this.jMenuItem1.setEnabled(false);
}//GEN-LAST:event_jMenuItem1ActionPerformed
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();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DemoImage().setVisible(true);
}
});
}
/*
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jMenuItemExitActionPerformed
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNoIconActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jMenuItemNoIconActionPerformed
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveAsActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jMenuItemSaveAsActionPerformed
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCaptureActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jMenuItemCaptureActionPerformed
*/
private Image tempImage;
private JPEGImageEncoder encoder;
private JFileChooser fileChooser=new JFileChooser();
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JLabel jLabelShowIcon;
private javax.swing.JMenu jMenuAction;
private javax.swing.JMenuBar jMenuBarAll;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JScrollPane jScrollPaneGlobal;
// 变量声明结束//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -