Assume you have a xxx.html, which uses an applet called abc.class. .../ xxx.html abc.class bbb.gifYou can use getImage(getDocumentBase(),"bbb.gif"); to get the image if you double click the xxx.html from IE.
xxx.html abc.class(abc是一个extends Japplet的类,既abc.java) bbb.gif 若我用try.java如下:import javax.Swing.*; public class try { public static void main(Sting[] args) { abc t v=new abc(); JFrame r= new JFrame(); r,getContentPane.add(t); t.init(); t.start(); r.setSize(345,456); r,setVisible(true); } } 这些文件在同一个文件夹下,可还是不行!!!
If you use "java try" to run your program( I still doubt if you can use "try" as your class name because it is a keyword in java), getDocumentBase() won't work.Why don't you use Image img = new ImageIcon("c:/xxx/bbb.gif").getImage(); to run it?BTW, it's not a good idea to discuss this under wudi002 (吴迪)'s thread. You can open another thread so that other people can help you also.
sorry,我既用html打开过,根本打不开.也用try打开过,用try打开时,若把有关生成图片的getCodeBase()或getDocumentBase()去掉,则JFrame中的JApplet可正常生成,但是也没了图片了,否则就告诉我空指针异常.不知为什么?? 不过还是要谢谢你 thank you and sorry to 吴迪)'s thread!!!
你的问题在于当缩小图像时,一些细节已经失去,再放大时,只好用插值做,当然就模糊了.
我把你的程序改了一下,好像没有这个问题了.可以试一试.改动较大,一些不要的类我已经去掉了.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;import javax.swing.*;
import javax.swing.event.*;public class image extends JApplet
{
boolean isStandalone = false;
JSlider slid;
static ImageProcessingPanel panel;
BufferedImage image,changedImage;
public static void main(String[] args) {
image applet = new image();
applet.isStandalone = true;
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
public void processZoom(ChangeEvent e) {
JSlider sld = (JSlider)e.getSource();
float factor = (float)sld.getValue();
if(factor < 0)
factor = -1/factor;
System.out.println(""+factor);
changedImage = zoom(image,factor);
panel.redraw(changedImage);
}
public static BufferedImage zoom(BufferedImage image,float factor) {
AffineTransform transform=AffineTransform.getScaleInstance(factor,factor);
BufferedImageOp op=new AffineTransformOp(transform,AffineTransformOp.TYPE_BILINEAR);
BufferedImage filteredImage=new BufferedImage(
(int)(image.getWidth()*factor),
(int)(image.getHeight()*factor),
image.getType());
op.filter(image, filteredImage);
return filteredImage;
} public void init()
{
Container contentPane=getContentPane();
panel=new ImageProcessingPanel();//定义用于显示和处理图像的面板
contentPane.add(panel,"Center");
JSlider slid = new JSlider(JSlider.HORIZONTAL,-10,10,0);
slid.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
processZoom(e);
}
});
contentPane.add(slid,"South"); String name="moon.gif";
try{
Image loadedImage;
if(isStandalone)
loadedImage= new ImageIcon("c:/"+name).getImage();
else
loadedImage=getImage(getCodeBase(),name);
image=load(panel,loadedImage);//加载图像并写入缓冲中
panel.redraw(image);//显示缓冲图像
//init()方法结束
}catch(Exception e){e.printStackTrace();}
} public static BufferedImage load(Component panel, Image loadedImage)
{//下面四行语句加载图像
MediaTracker tracker=new MediaTracker(panel);
tracker.addImage(loadedImage,0);
try{ tracker.waitForID(0);}
catch(InterruptedException e){}
//下面五行与将加载图像写入缓冲图像image中
BufferedImage image=new BufferedImage(loadedImage.getWidth(null),loadedImage.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics2D g2=image.createGraphics();
g2.drawImage(loadedImage,0,0,null);
return image;
}}//image类定义结束class ImageProcessingPanel extends JPanel
{
private BufferedImage image; public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (image!=null)
g.drawImage(image,5,5,null);
} public void redraw(BufferedImage image)
{
this.image=image;
repaint();
}}//本类定义结束
.../
xxx.html
abc.class
bbb.gifYou can use getImage(getDocumentBase(),"bbb.gif"); to get the image if you double click the xxx.html from IE.
abc.class(abc是一个extends Japplet的类,既abc.java)
bbb.gif
若我用try.java如下:import javax.Swing.*;
public class try
{
public static void main(Sting[] args)
{
abc t v=new abc();
JFrame r= new JFrame();
r,getContentPane.add(t);
t.init();
t.start();
r.setSize(345,456);
r,setVisible(true);
}
}
这些文件在同一个文件夹下,可还是不行!!!
Image img = new ImageIcon("c:/xxx/bbb.gif").getImage();
to run it?BTW, it's not a good idea to discuss this under wudi002 (吴迪)'s thread. You can open another thread so that other people can help you also.
不过还是要谢谢你 thank you and sorry to 吴迪)'s thread!!!