[CODE]/*
* Test.java
*
* Created on 2006年12月28日, 下午2:17
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package testImage;
/**
*
* @author lbf
*/
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test extends JFrame implements ActionListener{
private JMenuItem open,save,saveAs,exit,about,rotate,scale,gc;
private JLabel content;
private Map<JRadioButtonMenuItem,String> map=new HashMap<JRadioButtonMenuItem,String>();
private File currentFile;
private BufferedImage source;
/** Creates a new instance of Test */
public Test() {
super("图像操作");
initWindow();
}
private void initWindow(){
open=new JMenuItem("打开");
save=new JMenuItem("保存");
saveAs=new JMenuItem("另存为");
exit=new JMenuItem("退出");
about=new JMenuItem("关于");
gc=new JMenuItem("GC");
gc.addActionListener(this);
rotate=new JMenuItem("旋转");
scale=new JMenuItem("缩放");
open.addActionListener(this);
save.addActionListener(this);
saveAs.addActionListener(this);
exit.addActionListener(this);
about.addActionListener(this);
rotate.addActionListener(this);
scale.addActionListener(this);
content=new JLabel("<html><Font face=\"宋体\" size=16 color=RED><B>请选择图像</B></Font></html>",JLabel.CENTER);
this.getContentPane().add(new JScrollPane(content),BorderLayout.CENTER);
JMenu file=new JMenu("文件(F)");
file.setMnemonic('F');
file.add(open);
file.add(save);
file.add(saveAs);
file.add(exit);
file.add(about);
file.add(gc);
gc.setAccelerator(KeyStroke.getKeyStroke('X',InputEvent.CTRL_DOWN_MASK));
open.setAccelerator(KeyStroke.getKeyStroke('O',InputEvent.CTRL_DOWN_MASK));
save.setAccelerator(KeyStroke.getKeyStroke('S',InputEvent.CTRL_DOWN_MASK));
saveAs.setAccelerator(KeyStroke.getKeyStroke('S',InputEvent.CTRL_DOWN_MASK|InputEvent.SHIFT_DOWN_MASK));
exit.setAccelerator(KeyStroke.getKeyStroke('X',InputEvent.ALT_DOWN_MASK));
JMenu edit=new JMenu("操作(E)");
edit.setMnemonic('E');
edit.add(rotate);
edit.add(scale);
rotate.setEnabled(false);
scale.setEnabled(false);
JMenu look=new JMenu("风格(L)");
look.setMnemonic('L');
buildLookAndFeelMenu(look);
JMenuBar jmb=new JMenuBar();
jmb.add(file);
jmb.add(edit);
jmb.add(look);
this.setJMenuBar(jmb);
this.setSize(500,500);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void buildLookAndFeelMenu(JMenu menu){
ButtonGroup bg=new ButtonGroup();
LookAndFeelInfo[] ls=UIManager.getInstalledLookAndFeels();
String current=UIManager.getLookAndFeel().getName();
for(LookAndFeelInfo li:ls){
String name=li.getName();
JRadioButtonMenuItem item=new JRadioButtonMenuItem(name,name.equals(current));
menu.add(item);
bg.add(item);
item.addActionListener(this);
map.put(item,li.getClassName());
}
}
public void actionPerformed(ActionEvent e) {
Object obj=e.getSource();
if(obj==exit){
System.exit(0);
}else if(obj==about){
JOptionPane.showMessageDialog(this,"一款简单的图像操作程序!","关于" +
"",JOptionPane.INFORMATION_MESSAGE);
}else if(obj==open){
doOpenFile();
}else if(obj instanceof JRadioButtonMenuItem){
try{
UIManager.setLookAndFeel(map.get(obj));
SwingUtilities.updateComponentTreeUI(this);
} catch(Exception exe){
exe.printStackTrace();
}
}else if(obj==gc){
System.out.println("调用了GC");
System.gc();
}else if(obj==scale){
doScale();
}else if(obj==rotate){
new Rotate();
}else if(obj==save){
doSaveFile();
}else if(obj==saveAs){
doSaveAs();
}
}
private void doScale(){
new Scale();
}
private void doOpenFile(){
JFileChooser jf=new JFileChooser(".");
jf.addChoosableFileFilter(new MyExtension(".gif","gif图像"));
jf.addChoosableFileFilter(new MyExtension(".png","png图像"));
jf.addChoosableFileFilter(new MyExtension(".jpg","JPG图像"));
int i=jf.showDialog(this,"打开");
if(i==JFileChooser.APPROVE_OPTION){
currentFile=jf.getSelectedFile();
showImage(currentFile);
rotate.setEnabled(true);
scale.setEnabled(true);
}
}
private class Rotate extends JDialog implements ChangeListener,ActionListener{
private JSlider js;
private BufferedImage temp;
private JButton ok,cancel;
public Rotate(){
super(Test.this,"旋转操作",true);
js=new JSlider(0,360);
js.setMajorTickSpacing(50);
js.setMinorTickSpacing(10);
js.setPaintLabels(true);
js.setPaintTicks(true);
js.setPaintTrack(true);
js.setValue(0);
js.addChangeListener(this);
this.getContentPane().add(js,BorderLayout.CENTER);
JPanel jp=new JPanel();
jp.add(ok=new JButton("确定"));
jp.add(cancel=new JButton("取消"));
ok.addActionListener(this);
cancel.addActionListener(this);
this.getContentPane().add(jp,BorderLayout.SOUTH);
this.setSize(300,300);
this.setLocationRelativeTo(Test.this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
Rotate.this.dispose();
temp=null;
content.setIcon(new ImageIcon(source));
System.gc();
}
});
this.setVisible(true);
}
public void stateChanged(ChangeEvent e) {
int value=js.getValue();
AffineTransform af=new AffineTransform();
af.rotate(value/180.0*Math.PI,source.getWidth()/2,source.getHeight()/2);
AffineTransformOp aop=new AffineTransformOp(af,null);
temp=aop.filter(source,new BufferedImage((int)(source.getWidth()),(int)(source.getWidth()),BufferedImage.TYPE_INT_ARGB));
content.setIcon(new ImageIcon(temp));
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ok){
source=temp;
this.dispose();
temp=null;
System.gc();
}else if(e.getSource()==cancel){
this.dispose();
temp=null;
content.setIcon(new ImageIcon(source));
System.gc();
}
}
}
private class Scale extends JDialog implements ChangeListener,ActionListener{
private JSlider js;
private BufferedImage temp;
private JButton ok,cancel;
public Scale(){
super(Test.this,"缩放操作",true);
js=new JSlider(-50,50);
js.setMajorTickSpacing(50);
js.setMinorTickSpacing(10);
js.setPaintLabels(true);
js.setPaintTicks(true);
js.setPaintTrack(true);
js.addChangeListener(this);
this.getContentPane().add(js,BorderLayout.CENTER);
JPanel jp=new JPanel();
jp.add(ok=new JButton("确定"));
jp.add(cancel=new JButton("取消"));
ok.addActionListener(this);
cancel.addActionListener(this);
this.getContentPane().add(jp,BorderLayout.SOUTH);
this.setSize(300,300);
this.setLocationRelativeTo(Test.this);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
temp=null;
content.setIcon(new ImageIcon(source));
System.gc();
Scale.this.dispose();
}
});
this.setVisible(true);
}
public void stateChanged(ChangeEvent e) {
int current=js.getValue();
double rate=1+current/50.0+0.1;
AffineTransform af=AffineTransform.getScaleInstance(rate,rate);
AffineTransformOp aop=new AffineTransformOp(af,null);
temp=aop.filter(source,new BufferedImage((int)(source.getWidth()*rate),(int)(source.getWidth()*rate),BufferedImage.TYPE_INT_ARGB));
content.setIcon(new ImageIcon(temp));
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ok){
source=temp;
this.dispose();
temp=null;
System.gc();
}else if(e.getSource()==cancel){
this.dispose();
temp=null;
content.setIcon(new ImageIcon(source));
System.gc();
}
}
}
private void doSaveAs(){
try{
JFileChooser jf=new JFileChooser();
int i=jf.showDialog(this,"保存图片");
if(i==jf.APPROVE_OPTION){
File to=jf.getSelectedFile();
ImageIO.write(source,"PNG",to);
JOptionPane.showMessageDialog(this,"保存成功至:\n"+to);
}
} catch(Exception exe){
exe.printStackTrace();
}
}
private void doSaveFile(){
try{
ImageIO.write(source,currentFile.toString().substring(currentFile.toString().lastIndexOf(".")+1),currentFile);
JOptionPane.showMessageDialog(this,"保存成功!!\n"+currentFile.toString());
} catch(Exception exe){
exe.printStackTrace();
}
}
private void showImage(File f){
try{
source=ImageIO.read(f);
content.setText("");
content.setIcon(new ImageIcon(source));
} catch(Exception exe){
exe.printStackTrace();
}
}
public static void main(String[] args) {
new Test();
}
}
class MyExtension extends javax.swing.filechooser.FileFilter{
private String ends,des;
public MyExtension(String ends,String des){
this.ends=ends.toLowerCase();
this.des=des;
}
public boolean accept(File f) {
if(f.isDirectory()){
return true;
}else{
return f.getName().toLowerCase().endsWith(ends);
}
}
public String getDescription() {
return des;
}
}[/CODE]
写了一个,可以实现旋转和缩放
当内存不够用时,请手动GC一下
还有很多地方不太完善,你可以自己修改一下