bhuey_ling
asked on
simple: load pic file in applet
hello....
i copy some code from book....i have my jpg file same dir in my pc with my class file...but it can't load the picture
import java.awt.*;
import java.applet.*;
public class SimpleImage extends Applet{
Image image;
public void init(){
image = getImage(getDocumentBase() , "think.jpg");
}
public void paint(Graphics g){
g.drawImage(image,10,10, this);
}
}
here r runtime errror:java.security.Acces sControlEx ception: access denied (java.io.FilePermission \thi
nk.jpg read)
at java.security.AccessContro lContext.c heckPermis sion(Compi led Code)
at java.security.AccessContro ller.check Permission (Compiled Code)
at java.lang.SecurityManager. checkPermi ssion(Comp iled Code)
at java.lang.SecurityManager. checkRead( Compiled Code)
at sun.awt.image.URLImageSour ce.<init>( URLImageSo urce.java: 44)
at sun.applet.AppletImageRef. reconstitu te(AppletI mageRef.ja va:40)
at sun.misc.Ref.get(Ref.java: 53)
at sun.applet.AppletViewer.ge tCachedIma ge(AppletV iewer.java :272)
at sun.applet.AppletViewer.ge tImage(App letViewer. java:267)
at java.applet.Applet.getImag e(Applet.j ava:190)
at java.applet.Applet.getImag e(Applet.j ava:212)
at SimpleImage.init(SimpleIma ge.java:7)
at sun.applet.AppletPanel.run (Compiled Code)
at java.lang.Thread.run(Threa d.java:479 )
thnx for reply
i copy some code from book....i have my jpg file same dir in my pc with my class file...but it can't load the picture
import java.awt.*;
import java.applet.*;
public class SimpleImage extends Applet{
Image image;
public void init(){
image = getImage(getDocumentBase()
}
public void paint(Graphics g){
g.drawImage(image,10,10, this);
}
}
here r runtime errror:java.security.Acces
nk.jpg read)
at java.security.AccessContro
at java.security.AccessContro
at java.lang.SecurityManager.
at java.lang.SecurityManager.
at sun.awt.image.URLImageSour
at sun.applet.AppletImageRef.
at sun.misc.Ref.get(Ref.java:
at sun.applet.AppletViewer.ge
at sun.applet.AppletViewer.ge
at java.applet.Applet.getImag
at java.applet.Applet.getImag
at SimpleImage.init(SimpleIma
at sun.applet.AppletPanel.run
at java.lang.Thread.run(Threa
thnx for reply
do not load the document base. use getCodebase()
replace
image = getImage(getDocumentBase() , "think.jpg");
with
image = getImage(getCodeBase(), "think.jpg");
image = getImage(getDocumentBase()
with
image = getImage(getCodeBase(), "think.jpg");
cached page copy .. once again :(
ASKER
hello...
importjava.awt.*; import java.applet.*;
public class SimpleImage extends Applet{
Image image;
public void init(){
image = getImage(getCodeBase(), "think.jpg");
}
public void paint(Graphics g){
g.drawImage(image,10,10, this);
}
}
i change it oledi....not compile error .no run time error but pic din load......and i make sure all file r in same dir.....
thnx for reply...
importjava.awt.*; import java.applet.*;
public class SimpleImage extends Applet{
Image image;
public void init(){
image = getImage(getCodeBase(), "think.jpg");
}
public void paint(Graphics g){
g.drawImage(image,10,10, this);
}
}
i change it oledi....not compile error .no run time error but pic din load......and i make sure all file r in same dir.....
thnx for reply...
Now use the MediaTracker to track whether this 'image' object is actually loaded. adding it to a media tracker and wait()-ing for it to load would probably do. This all you have to add after the "image = ...".
Nik
Nik
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thnx.....