Link to home
Start Free TrialLog in
Avatar of bhuey_ling
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.AccessControlException: access denied (java.io.FilePermission \thi
nk.jpg read)
        at java.security.AccessControlContext.checkPermission(Compiled Code)
        at java.security.AccessController.checkPermission(Compiled Code)
        at java.lang.SecurityManager.checkPermission(Compiled Code)
        at java.lang.SecurityManager.checkRead(Compiled Code)
        at sun.awt.image.URLImageSource.<init>(URLImageSource.java:44)
        at sun.applet.AppletImageRef.reconstitute(AppletImageRef.java:40)
        at sun.misc.Ref.get(Ref.java:53)
        at sun.applet.AppletViewer.getCachedImage(AppletViewer.java:272)
        at sun.applet.AppletViewer.getImage(AppletViewer.java:267)
        at java.applet.Applet.getImage(Applet.java:190)
        at java.applet.Applet.getImage(Applet.java:212)
        at SimpleImage.init(SimpleImage.java:7)
        at sun.applet.AppletPanel.run(Compiled Code)
        at java.lang.Thread.run(Thread.java:479)

thnx for reply

Avatar of diakov
diakov

do not load the document base. use getCodebase()
replace
  image = getImage(getDocumentBase(), "think.jpg");

with
  image = getImage(getCodeBase(), "think.jpg");
cached page copy .. once again :(
Avatar of bhuey_ling

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...

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
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thnx.....