Chinaunix首页 | 论坛 | 博客
  • 博客访问: 213234
  • 博文数量: 33
  • 博客积分: 1690
  • 博客等级: 上尉
  • 技术积分: 381
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-27 18:57
个人简介

吟游天地间,隐没市井中..

文章分类
文章存档

2014年(1)

2009年(6)

2008年(26)

我的朋友

分类: Java

2008-07-24 10:24:33

  这是以前写的一个用来测试的小应用程序,主要功能是用小应用程序实现打开图片,保存图片功能。在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);
    }
}

阅读(2368) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~