免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1316 | 回复: 0
打印 上一主题 下一主题

JAVA的Applet实现图片的打开保存功能 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-24 10:24 |只看该作者 |倒序浏览

                  这是以前写的一个用来测试的小应用程序,主要功能是用小应用程序实现打开图片,保存图片功能。在Eclipse中可以运行,打包数字签名后也可以在网页中运行,但是有几个问题,第一次打开图片的时候是个小图标,看不清楚,非要最大化一次才能看清;还有就是图片由于我是以流的形式读入,所以图片的大小是自己设定的,我这里初始设定为8K。当时网上找了很久也没找到答案,后来由于后继工作的进行也就放下这两个问题了。
  由于本人不是专业的Java程序员,所以代码写得实在简陋,不过作为鄙人的一点经验,所以就贴上来,希望大侠见了不要笑话。如果大侠能够帮我指出如何解决上面的问题。小弟当感激不尽,谢谢了!
               
               
                import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
public class applet_t extends Applet implements ActionListener{
    Button b1 = new Button("   open   ");
    Button b2 = new Button("   save   ");
    JLabel l1 = new JLabel("Picture");
    FileWriter fw;
    BufferedWriter out;
   
    byte tom[]=new byte[100000];
    public void init() {
        setLayout(new BorderLayout());
        Panel c = new Panel();
        Panel s = new Panel();
        Label l = new Label("JPEG    Viewer",Label.CENTER);
        c.setBackground(Color.pink);
        c.add(BorderLayout.CENTER,l1);
        s.setLayout(new BorderLayout());
        s.add(BorderLayout.WEST,b1);
        s.add(BorderLayout.EAST,b2);
        s.add(BorderLayout.CENTER,l);
        add(BorderLayout.CENTER,c);
        add(BorderLayout.SOUTH,s);
        b1.addActionListener(this);
        b2.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == b1){
            Frame f=new fileDialog("File Dialog Demo!");
            f.setVisible(false);
            f.setSize(100,100);
            FileDialog fd1=new FileDialog(f,"File Dialog",FileDialog.LOAD);
            fd1.setVisible(true);
        try{
            File f2 = new File(fd1.getDirectory(),fd1.getFile());
            int len=(int)(f2.length());
            
            FileInputStream readfile = new FileInputStream(f2);
            readfile.read(tom,0,len);
            
            BufferedImage image = ImageIO.read(new ByteArrayInputStream(tom));
            ImageIcon ii = new ImageIcon(image);
            l1.setText(null);
            l1.setIcon(ii);
            
        }catch(IOException e1){
            
        }
        }
        else if (e.getSource() == b2){
            Frame f=new fileDialog("File Dialog Demo!");
            f.setVisible(false);
            f.setSize(100,100);
            FileDialog fd=new FileDialog(f,"File Dialog",FileDialog.SAVE);
            fd.setVisible(true);
            try{
                File f1 = new File(fd.getDirectory(),fd.getFile());
                FileOutputStream writefile = new FileOutputStream(f1);
                writefile.write(tom,0,8000);         
            }catch(IOException e2) {
            }
        }
    }
}
class fileDialog extends Frame
{
    fileDialog(String title)
    {
        super(title);
        MyWindowAdapter adapter = new MyWindowAdapter(this);
        addWindowListener(adapter);
    }
}
class MyWindowAdapter extends WindowAdapter
{
    fileDialog sf;
    public MyWindowAdapter(fileDialog sfr)
    {
        this.sf=sfr;
    }
    public void windowClosing(WindowEvent we)
    {
        sf.setVisible(false);
    }
}
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/60509/showart_1090435.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP