| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 881 人关注过本帖
标题:[求助]图片浏览程序中添加东西
只看楼主 加入收藏
tigershi10
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2006-10-12
收藏
 问题点数:0 回复次数:11 
[求助]图片浏览程序中添加东西

我想在这个程序中加入实现图片旋转,放大,缩小,我搞了半天搞不出来哦
//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编辑过]

搜索更多相关主题的帖子: 浏览 
2006-12-27 23:03
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

[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一下
还有很多地方不太完善,你可以自己修改一下


可惜不是你,陪我到最后
2006-12-28 15:07
tigershi10
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2006-10-12
收藏
得分:0 

谢谢了,我看看学习一下


专业源于品质
2006-12-28 15:51
tigershi10
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2006-10-12
收藏
得分:0 
哦,我还想问一下,想学3D方面的话,得先学写什么啊

专业源于品质
2006-12-28 16:03
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

先把基础的2D学好

然后下载J3D的包,慢慢学


可惜不是你,陪我到最后
2006-12-28 16:26
tigershi10
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2006-10-12
收藏
得分:0 
到SUN网站下吗?

专业源于品质
2006-12-28 16:44
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
当然

可惜不是你,陪我到最后
2006-12-28 16:54
tigershi10
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2006-10-12
收藏
得分:0 
问一下setAccelerator()方法是干什么的?

专业源于品质
2006-12-29 12:51
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
设置快捷键,比如,你打开图片用"Ctrol+O就可以打开了

可惜不是你,陪我到最后
2006-12-29 13:54
tigershi10
Rank: 1
等 级:新手上路
帖 子:84
专家分:0
注 册:2006-10-12
收藏
得分:0 

锋哥,想问你一下
图象缩放的原理是什么


专业源于品质
2006-12-29 15:21
快速回复:[求助]图片浏览程序中添加东西
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028114 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved