File IO


run
Please select square image file!

---(Source List)---

//Example0D
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;
public class Example0D extends Frame implements ActionListener{
    Image img01,img02;
    int w,h;
    //Main
    public static void main(String ar[]){
        Frame f=new Example0D();
        f.setTitle("Image File IO");
        f.setSize(new Dimension(640,400));
        f.setVisible(true);
    }
    //Button
    Example0D(){
        setLayout(new FlowLayout());
        Button b01=new Button("Open");
        Button b02=new Button("Save");
        b01.addActionListener(this);
        b02.addActionListener(this);
        add(b01);
        add(b02);
        addWindowListener(new WinAdapter());
    }
    //Close
    class WinAdapter extends WindowAdapter{
        public void windowClosing(WindowEvent we){System.exit(0);}
    }
    public void actionPerformed(ActionEvent ae){
        if(ae.getActionCommand()=="Open"){
            try{
                String Filename = openfile();
                if(Filename!=null) img01 = getToolkit().getImage(Filename);
                repaint();
            }catch(Exception ex){}
        }
        if(ae.getActionCommand()=="Save"){
            if(img02!=null){
                try{
                    String Filename = writefile();
                    if(Filename!=null){
                        FileOutputStream fo = new FileOutputStream(Filename);
                        BufferedImage bi = new BufferedImage(w,w,BufferedImage.TYPE_INT_RGB);
                        Graphics gc = bi.getGraphics();
                        gc.drawImage(img02,0,0,this);
                        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fo);
                        encoder.encode(bi);
                        fo.close();
                    }
                }
                catch(Exception ex){}
            }
        }
    }
    public void update(Graphics g){
        paint(g);
    }
    public void paint(Graphics g){
        if(img01!=null){
            PixelGrabber pg=new PixelGrabber(img01,0,0,-1,-1,true);
            try{
                if(pg.grabPixels()){
                    w=pg.getWidth();
                    int[] op=(int[]) pg.getPixels();
                    int[] np=(int[]) new int[w*w];
                    g.drawImage(img01,10,100,this);
                    for(int y=0;y<w;y++){
                        for(int x=0;x<w;x++) np[(w-1-y)+x*w]=op[x+y*w];
                    }
                    img02=createImage(new MemoryImageSource(w,w,np,0,w));
                    g.drawImage(img02,20+w,100,this);
                }
            }catch(InterruptedException ie){}
        }
    }
    String openfile(){
        FileDialog fd=new FileDialog(new Frame(),"File Select");
        fd.show();
        String fullpath=fd.getDirectory()+fd.getFile();
        fd.dispose();
        return fullpath;
    }
    String writefile(){
        FileDialog fd=new FileDialog(new Frame(),"File Save",FileDialog.SAVE);
        fd.show();
        String fullpath=fd.getDirectory()+fd.getFile();
        fd.dispose();
        return fullpath;
    }
}