为什么把APPLET 存为 a.jpeg,a.jpeg什么都没有???急!!!!
为什么把APPLET 存为 a.jpeg 时 打开a.jpeg什么都没有,还有我该怎么样设置时applet有权限许可才能写文件
import java.applet.Applet;
import java.awt.image.BufferedImage;
import java.io.*;
import com.sun.media.jai.codec.*;
import com.sun.media.jai.codecimpl.*;
import java.awt.*;
import java.awt.event.*;
public class HelloWorldApplet extends Applet {
Button button1 = new Button();
BufferedImage bi=new BufferedImage(200,100,BufferedImage.TYPE_INT_RGB);
public void paint(Graphics g) {
Graphics bg=bi.createGraphics();
System.out.print("op");
bg.drawString("Hello world!", 50, 25);
g.drawImage(bi,0,0,this);
}
public static void saveImage(BufferedImage img, File f) throws Exception{
ImageEncoder enc = getImageEncoder(f);
if(enc == null) {
return; //ups...
}
enc.encode(img.getData(), img.getColorModel());
enc.getOutputStream().close();
}
public static ImageEncoder getImageEncoder(File f)
throws Exception {
ImageEncodeParam encodeParam = null;
String type = null;
String name = f.getName();
System.out.println(name);
String ext;
int dotIndex = name.lastIndexOf('.');
if ( dotIndex == -1 ¦¦ dotIndex == name.length()-1 ) ext = "";
else ext = name.substring(dotIndex+1).toLowerCase();
System.out.println(ext);
if(ext.equals("jpeg") ¦¦ ext.equals("jpg")) {
type = "JPEG";
encodeParam = new JPEGEncodeParam();
((JPEGEncodeParam)encodeParam).setQuality(1F);
}
else if (ext.equals("tiff") ¦¦ ext.equals("tif")) {
type = "TIFF";
encodeParam = new TIFFEncodeParam();
((TIFFEncodeParam)encodeParam).setCompression(TIFFEncodeParam.COMPRESSION_NONE);
}
else if(ext.equals("bmp")) {
type = "BMP";
encodeParam = new BMPEncodeParam();
}
else if(ext.equals("png")) {
type = "PNG";
encodeParam = new PNGEncodeParam.Palette();
((PNGEncodeParam.Palette)encodeParam).setBitDepth(2);
}
else if(ext.equals("gif")) {
System.out.println("oK");
throw new Exception( "Gif file format is not yet supported !");
}
else {
throw new Exception( "Unknown extension: " + ext);
}
return ImageCodec.createImageEncoder(type, new FileOutputStream(f),encodeParam);
}
public HelloWorldApplet() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
button1.setLabel("button1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
this.add(button1, null);
}
void button1_actionPerformed(ActionEvent e) {
String s="classes/a.jpeg";
File f=new File(s);
HelloWorldApplet hwa=new HelloWorldApplet();
System.out.print(s);
try
{
hwa.saveImage(bi, f);
}
catch(Exception ee){
System.out.println(e.toString());}
}
}