| | |
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:
Solved Threads: 501
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)
import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.util.*; import javax.imageio.*; import java.io.*; public class AppletWithPNG extends JApplet { MainPanel mp; public void init () { AppletWithPNG awp = new AppletWithPNG (); } public AppletWithPNG() { mp = new MainPanel (); this.add(mp); this.setVisible(true); this.setSize(150, 150); } } class MainPanel extends JPanel { BufferedImage bi = null; File file = null; public MainPanel () { String filename = "AceClubs.png"; try { file = new File (filename); bi = ImageIO.read (file); } catch (Exception ex) { // problem. exit program System.exit (1); } this.setVisible (true); this.setMinimumSize(new Dimension (150, 150)); } public void paintComponent(Graphics g) { super.paintComponent (g); this.setBackground(Color.GREEN); g.drawImage (bi, 5, 5, null); } }
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.
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.
This is the error I'm getting when trying to load this using FireFox (after 3 attempts)
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).
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.
package the images in a jar file with the classes and use
you won't get access denied errors because they will be in the jar
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
"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
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.
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
"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
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
•
•
•
•
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.
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)
try { URL url = getClass().getResource("CardPNGs.AceClubs.png"); // file = new File (url); bi = ImageIO.read (url); // bi is type BufferedImage } catch (Exception ex) { System.out.println (ex.toString()); System.exit (1); }
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? 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
"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
![]() |
Other Threads in the Java Forum
- Previous Thread: any one know to make interfaces in java
- Next Thread: Needing Java Advice
| Thread Tools | Search this Thread |
addball android api applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css csv database eclipse ee error event exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm key linux list loan loop machine map method methods migrate mobile netbeans newbie nls oracle output phone physics print problem program programming project radio recursion reporting scanner screen server service set size sms socket software sort sql string swing textfield threads time transfer tree trolltech utility windows






