• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getImageIcon() : how does it work?

 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I'm trying to get an imageicon into BorderLayout and I'm getting this error message from the java console:
...
java.security.AccessControlException: access denied (java.io.FilePermission bottlenose.jpg read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
...
This seems strange because following code works well:

but there seems to be no javax.swing equivalent for getImageIcon.
Of course I'm assuming I need to use either getDocumentBase() or getCodeBase() to get pass the "access denied" problem. Following code should work, but it doesn't

TIA :-)
I think the question should be "how does one use getDocumentBase() win getImageIcon()" ?
Without getDocumentBase(), it seems I cannot get the image.

Okay, this code works and fetch the "icon" back.
But resize() didn't iconify my picture ie. it didn't shrink it so that it becomes an icon. I was hoping to put the iconified barn owl next to the label text...

By the way, it doesn't seem to matter whether I use SWING's super.paintComponent(g) or not ...
[ November 10, 2003: Message edited by: achana chan ]
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception you are getting is because you are in a Java applet and Java applets aren't allowed to access other files. The method getImage() in the Applet/JApplet class is a way around this (I'm not entirely sure how it works, but it does).
Creating an ImageIcon using "new ImageIcon()" is not possible because it relies on normal I/O. So you get an access permission exception. Instead, use getImage() from the Applet class to create you're image. Then create a new ImageIcon using that Image object. This should get rid of that exception.
If you are doing anything in Swing you should use paintComponent() and NOT paint(). This stuffs up the way Swing does back buffering.
When you are setting the size of a JPanel, you should use setPreferredSize() instead of resize(). You will probably have more luck this way.
Hope that helps.
 
achana chan
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Modifying the code has no visible effect. I can still get the image back. The problem seems not to be with getDocumentBase() or getCodeBase().
I just cannot iconify the image, when I use setPreferredSize(imageWidth,imageHeight), its more like a little viewport into a bigger picture.
 
Santiago Escobar Robledo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's so late to answer, but to someone this could be useful ...

Daniel Searson wrote:The exception you are getting is because you are in a Java applet and Java applets aren't allowed to access other files. The method getImage() in the Applet/JApplet class is a way around this (I'm not entirely sure how it works, but it does). Creating an ImageIcon using "new ImageIcon()" is not possible because it relies on normal I/O. So you get an access permission exception.


That's true.

So, with ImageIcon you could do two things to avoid the AccessControlException because java.io.FilePermission:

1. To grant explicit permission to access local system resources ... resources that you will use in the applet.


You could see the next link about "Security and Permissions": http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html


2. To use ClassLoader

For example:


Everything is in the same path:

src/
- ImageIcon.java
- image.jpg
- index.html -> To run the applet

You could see the next link: http://www.leepoint.net/notes-java/GUI-lowlevel/graphics/45imageicon.html


By the way, ImageIcon doesn't allow to resize the image. You could see this post: http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon. It works perfect.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic