Tyring to add image from PNG file to JApplet

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Tyring to add image from PNG file to JApplet

 
0
  #1
Aug 1st, 2008
O.K. I wrote my own version of the Addiction Solitaire game using a JFrame and it worked. I want to put it on the web, so I'm converting it to a JApplet. I have 52 PNG files, one for each card, that I need to be able to access for the game to work properly. I switched the JFrame over to a JApplet and previewed it using Java's appletviewer. That works fine, so it's clearly getting enough permission to read the PNG files and create BufferedImages from them. When I switch to a browser, though, it doesn't work, and I am assuming that is because, due to the fact that it is an applet, it can't read the files due to security/permissions considerations. Keep in mind that I have no need or desire to try to read files from the user's computer with this JApplet. I am supplying the 52 files and these are the files that I am trying to open in the program. Below is a very stripped-down version of the program. Can anyone confirm that this is the problem and suggest a solution please? Thanks.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.image.*;
  4. import java.util.*;
  5. import javax.imageio.*;
  6. import java.io.*;
  7.  
  8.  
  9. public class AppletWithPNG extends JApplet
  10. {
  11. MainPanel mp;
  12.  
  13. public void init ()
  14. {
  15. AppletWithPNG awp = new AppletWithPNG ();
  16. }
  17.  
  18.  
  19. public AppletWithPNG()
  20. {
  21. mp = new MainPanel ();
  22. this.add(mp);
  23. this.setVisible(true);
  24. this.setSize(150, 150);
  25. }
  26. }
  27.  
  28.  
  29. class MainPanel extends JPanel
  30. {
  31. BufferedImage bi = null;
  32. File file = null;
  33.  
  34. public MainPanel ()
  35. {
  36. String filename = "AceClubs.png";
  37. try
  38. {
  39. file = new File (filename);
  40. bi = ImageIO.read (file);
  41. }
  42. catch (Exception ex)
  43. {
  44. // problem. exit program
  45. System.exit (1);
  46. }
  47.  
  48. this.setVisible (true);
  49. this.setMinimumSize(new Dimension (150, 150));
  50. }
  51.  
  52.  
  53. public void paintComponent(Graphics g)
  54. {
  55. super.paintComponent (g);
  56. this.setBackground(Color.GREEN);
  57. g.drawImage (bi, 5, 5, null);
  58. }
  59. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Tyring to add image from PNG file to JApplet

 
0
  #2
Aug 1st, 2008
I know for a fact that there must be an easy way around this...

If not, you could consider setting up a server, send the files to the server from your computer, keep it up and running then read the files from the Server into the JApplet (once, to prevent concurrent and expensive I/O operations) then use the image references obtained.

I'm sure there's another way. I'm not sure if it's possible to set up a database that has this information on it then simply query it... but even so you'd have access permissions if you're using a restrictive OS like Windows Vista.

I remember someone else having this problem... but I don't recall how they solved the problem.

Edit: Actually this might not be a good solution, because you will still have access-privilege issues since the server exists separately from the location of the JApplet.
Last edited by Alex Edwards; Aug 1st, 2008 at 1:25 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Tyring to add image from PNG file to JApplet

 
1
  #3
Aug 1st, 2008
This is the error I'm getting when trying to load this using FireFox (after 3 attempts)

Java Plug-in 1.6.0_07
Using JRE version 1.6.0_07 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Mark


----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
p:   reload proxy configuration
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------

java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.1)
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkExit(Unknown Source)
	at java.lang.Runtime.exit(Unknown Source)
	at java.lang.System.exit(Unknown Source)
	at MainPanel.<init>(MainPanel.java:24)
	at AppletWithPNG.<init>(AppletWithPNG.java:20)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at java.lang.Class.newInstance0(Unknown Source)
	at java.lang.Class.newInstance(Unknown Source)
	at sun.applet.AppletPanel.createApplet(Unknown Source)
	at sun.plugin.AppletViewer.createApplet(Unknown Source)
	at sun.applet.AppletPanel.runLoader(Unknown Source)
	at sun.applet.AppletPanel.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

Edited*

Edit2: Ok I commented out the System call and now I'm getting just the background without the image (most likely your main issue).
Last edited by Alex Edwards; Aug 1st, 2008 at 1:42 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Tyring to add image from PNG file to JApplet

 
0
  #4
Aug 1st, 2008
package the images in a jar file with the classes and use getClass().getResource(path); to get a url for it and you can use the url directly as an argument to your ImageIO.read()
you won't get access denied errors because they will be in the jar
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Tyring to add image from PNG file to JApplet

 
0
  #5
Aug 1st, 2008
sciwizeh, thank you. I'll play around with that. Alex, thank you too. Hopefully this is the easier way we were thinking of.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Tyring to add image from PNG file to JApplet

 
0
  #6
Aug 1st, 2008
Originally Posted by VernonDozier View Post
sciwizeh, thank you. I'll play around with that. Alex, thank you too. Hopefully this is the easier way we were thinking of.
I don't doubt sciwizeh, especially when it comes to image-handling. Check out his site for example =).
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Tyring to add image from PNG file to JApplet

 
0
  #7
Aug 1st, 2008
thanks Alex Edwards

VernonDozier if you don't know how to add images to a jar there is a thread here if you search daniweb there are many image and jar related threads, most are problems accessing images within a jar, but they are usually told what i already said or something close to it.
Last edited by sciwizeh; Aug 1st, 2008 at 2:20 am.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Tyring to add image from PNG file to JApplet

 
0
  #8
Aug 1st, 2008
Originally Posted by sciwizeh View Post
thanks Alex Edwards

VernonDozier if you don't know how to add images to a jar there is a thread here if you search daniweb there are many image and jar related threads, most are problems accessing images within a jar, but they are usually told what i already said or something close to it.
Just saw your edit. I checked out the link and a few others. My first attempt was to make a jar file of just the PNG images (no code or classes), then try to access them. This doesn't seem to be what you are recommending. I think you're saying to compile the code, which will make the classes, then make one big jar file of all the classes and all the PNG files? And put into the code the name of a jar file that doesn't yet exist at the time I compile the code?

I'll experiment around with that idea, but if you could confirm that that IS your idea, I'd appreciate it. The experiment that I DID do (and which didn't work) was the following. Make a jar file called "CardPNGs.jar". The contents of that jar file is the 52 PNG files, one of which is called "AceClubs.png". I put that jar into my NetBeans Compile-time library list, then made the following attempt to load it in the program.

  1. try
  2. {
  3. URL url = getClass().getResource("CardPNGs.AceClubs.png");
  4. // file = new File (url);
  5. bi = ImageIO.read (url); // bi is type BufferedImage
  6. }
  7. catch (Exception ex)
  8. {
  9. System.out.println (ex.toString());
  10. System.exit (1);
  11. }

It didn't work and I didn't expect it to. Something has to go before getClass() , right? And it's not a class anyway. How far off am I?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 412
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Tyring to add image from PNG file to JApplet

 
1
  #9
Aug 1st, 2008
not very, you do need the class files in the jar, you don't need the name of the jar file at compile time. that last code should work fine except that you need the class files in the jar too, and the path should be slashes (/) and not periods (.)
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Tyring to add image from PNG file to JApplet

 
0
  #10
Aug 2nd, 2008
All right, that worked! Wasn't too bad at all. Thanks a bunch.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC