Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

simple: load pic file in applet

Posted on 2000-02-17
7
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

715 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