我想在这个程序中加入实现图片旋转,放大,缩小,我搞了半天搞不出来哦
//SwingPic.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingPic extends JFrame {
protected String filename = "", lastDir;
protected ImageIcon image;
Image im;
int height, width, count = 0;
JLabel labeledPic;
JScrollPane scroller;
Container content;
public SwingPic() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch (Exception e)
{
}
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(0);}
}
);
setTitle("小虎的图片浏览器");
MenuBar mbar = new MenuBar();
Menu m = new Menu("文件");
m.add(new MenuItem("打开"));
m.add(new MenuItem("退出"));
mbar.add(m);
setMenuBar(mbar);
setSize(800, 600);
setVisible(true);
}
public void swingdraw() {
if(count==1) {
labeledPic = new JLabel(image, JLabel.CENTER);
labeledPic.setText(filename);
scroller = new JScrollPane(labeledPic);
scroller.setPreferredSize(new Dimension(height, width));
content = getContentPane();
content.add(scroller);
this.show();
}
else if(count>1) {
labeledPic = new JLabel(image, JLabel.CENTER);
labeledPic.setText(filename);
System.out.println("new jlabel ");
scroller = new JScrollPane(labeledPic);
scroller.setPreferredSize(new Dimension(height, width));
scroller.revalidate();
scroller.repaint();
}
}
public boolean action(Event evt, Object arg) {
if(arg.equals("打开")) {
FileDialog d =
new FileDialog(this, "Open image file", FileDialog.LOAD);
d.setFile("*.gif;*.jpg");
d.setDirectory(lastDir);
d.show();
String f = d.getFile();
lastDir = d.getDirectory();
if(f != null) {
im = Toolkit.getDefaultToolkit ().getImage (lastDir + f);
if(im==null) return false;
image=new ImageIcon(lastDir+f);
height = image.getIconHeight();
width = image.getIconWidth();
System.out.println("new image icon"+height+" "+width);
}
System.out.println("new file"+lastDir+f);
count++;
swingdraw();
}
else if(arg.equals("退出"))
System.exit(0);
else
return false;
return true;
}
public static void main(String[]args) {
SwingPic sp = new SwingPic();
}
}
大家帮我写写哦,谢谢了,呵呵
[此贴子已经被作者于2006-12-28 11:44:47编辑过]
----------------解决方案--------------------------------------------------------
[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一下
还有很多地方不太完善,你可以自己修改一下
----------------解决方案--------------------------------------------------------
谢谢了,我看看学习一下
----------------解决方案--------------------------------------------------------
哦,我还想问一下,想学3D方面的话,得先学写什么啊
----------------解决方案--------------------------------------------------------
先把基础的2D学好
然后下载J3D的包,慢慢学
----------------解决方案--------------------------------------------------------
到SUN网站下吗?
----------------解决方案--------------------------------------------------------
当然
----------------解决方案--------------------------------------------------------
问一下setAccelerator()方法是干什么的?
----------------解决方案--------------------------------------------------------
设置快捷键,比如,你打开图片用"Ctrol+O就可以打开了
----------------解决方案--------------------------------------------------------
锋哥,想问你一下
图象缩放的原理是什么
----------------解决方案--------------------------------------------------------