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.

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.

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).

commented: Thanks for spending time to work on this! +5

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

sciwizeh, thank you. I'll play around with that. Alex, thank you too. Hopefully this is the easier way we were thinking of.

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 =).

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.

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.

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 (.)

commented: Thanks for the help. +5

All right, that worked! Wasn't too bad at all. Thanks a bunch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.