This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

getImageIcon() : how does it work?

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: https://coderanch.com/t/331731/GUI/java/Resize-ImageIcon. It works perfect.

 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic