Solved

simple: load pic file in applet

Posted on 2000-02-17
7
273 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
MySQL  on Tomcat 8 69
hibernate jars 4 45
hashmap order 17 37
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

829 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