943,862 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2864
  • Java RSS
Aug 1st, 2008
0

Tyring to add image from PNG file to JApplet

Expand Post »
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.

JAVA Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Aug 1st, 2008
0

Re: Tyring to add image from PNG file to JApplet

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 1st, 2008
1

Re: Tyring to add image from PNG file to JApplet

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 1st, 2008
0

Re: Tyring to add image from PNG file to JApplet

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
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 1st, 2008
0

Re: Tyring to add image from PNG file to JApplet

sciwizeh, thank you. I'll play around with that. Alex, thank you too. Hopefully this is the easier way we were thinking of.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Aug 1st, 2008
0

Re: Tyring to add image from PNG file to JApplet

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 =).
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 1st, 2008
0

Re: Tyring to add image from PNG file to JApplet

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.
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 1st, 2008
0

Re: Tyring to add image from PNG file to JApplet

Click to Expand / Collapse  Quote originally posted by sciwizeh ...
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.

JAVA Syntax (Toggle Plain Text)
  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?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Aug 1st, 2008
1

Re: Tyring to add image from PNG file to JApplet

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 (.)
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 2nd, 2008
0

Re: Tyring to add image from PNG file to JApplet

All right, that worked! Wasn't too bad at all. Thanks a bunch.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: any one know to make interfaces in java
Next Thread in Java Forum Timeline: Needing Java Advice





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC