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);
}
}
|