Solved

simple: load pic file in applet

Posted on 2000-02-17
7
253 Views
Last Modified: 2008-03-10
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

0
Comment
Question by:bhuey_ling
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 2534045
do not load the document base. use getCodebase()
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2534120
replace
  image = getImage(getDocumentBase(), "think.jpg");

with
  image = getImage(getCodeBase(), "think.jpg");
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2534126
cached page copy .. once again :(
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:bhuey_ling
ID: 2534384
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...

0
 
LVL 8

Expert Comment

by:diakov
ID: 2534641
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
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 30 total points
ID: 2534675
image = getImage(getCodeBase(), "think.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 11);
    try
    {
      mt.waitForID(11);
    }
    catch (Exception x)  
    {      
      x.printStackTrace();
      return null;
    }
    mt.removeImage(image);
    if (image.getWidth(null) == -1)
System.out.println("hmm ... wrong image URL ??? ");
0
 

Author Comment

by:bhuey_ling
ID: 2535405
thnx.....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 123
Java Options in Zimbra Server 2 44
web services creation SOAP vs REST 5 17
object oriented programming comparison 5 52
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now