Solved

simple: load pic file in applet

Posted on 2000-02-17
7
245 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IT Company 5 69
strCopies  challenge 17 74
groovy example issue 10 70
GUI builder for Eclipse? 8 14
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 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

10 Experts available now in Live!

Get 1:1 Help Now