Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help loading images in an Applet  (Read 1200 times)
0 Members and 1 Guest are viewing this topic.
Offline uraknai

Senior Newbie





« Posted 2006-06-19 12:57:19 »

hi,

I'm writing a simple applet (JApplet actualy) that needs to display images but I'm finding it very difficult.

My code is as follows:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
import java.awt.*;  
import javax.swing.*;
import java.applet.*;
import javax.imageio.*;
import java.io.*;
/**
 *
 * @author Nick
 */

public class TestJApplet extends JApplet {
   
    /** Creates a new instance of TestJApplet */
    public TestJApplet() {
    }
   
    public void init()
    {
       
    }
   
    public void paint(Graphics g)
    {
        Image image = null;
       
        try
        {
                       
            InputStream is = new BufferedInputStream(new FileInputStream("a.gif"));
            image = ImageIO.read(is);
        }
        catch(Exception e)
        {
            System.out.println("Exception : " + e);
        }
         
       
        g.drawImage(image, 100,100,this);
        g.drawRect(30,30,30,30);
    }
       
   
}


and get the exception :

1  
java.security.AccessControlException: access denied (java.io.FilePermission a.gif read)


I've also tried:

1  
2  
File file = new File("a.gif");
image = ImageIO.read(file);


and

1  
image = getImage(getDocumentBase(), "a.gif");


and get the same access denied message.

Is there a simple way to get an image to load in an Applet (or JApplet)

Thanks in advance Wink
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #1 - Posted 2006-06-19 13:57:14 »

if the file is in the applets jar (presuming you have jarred up your applet?), you should use this.getClass().getResource[asStream](...)
Offline uraknai

Senior Newbie





« Reply #2 - Posted 2006-06-19 14:25:31 »

if the file is in the applets jar (presuming you have jarred up your applet?), you should use this.getClass().getResource[asStream](...)

I dont have too much experience working with Jar files. Am I right in thinking that the .jar file will contain .class files and not .java files? If so, how can I modify the applet after the Jar has been created? Also, do I store the images in the Jar file?

cheers
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #3 - Posted 2006-06-19 16:17:54 »

yes, a jar file is an encapsulated distribution of your application.

getResource[AsStream]() simply finds the first file that is present on the classpath, that matches the specified name.

If you are running your application unjarred, the current directory will be considered in the classpath too. (along with anything else you have specified)
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #4 - Posted 2006-06-19 18:51:16 »

>Am I right in thinking that the .jar file will contain .class files and not .java files?

Yes.

>If so, how can I modify the applet after the Jar has been created?

You like... keep the source for that case Wink

>Also, do I store the images in the Jar file?

With applets its optional, with webstart its a must. Btw it doesnt has to be the same jar. You can use one for code, one for images, one for sounds... etc. The benefit of this approach is that you can change them individually, which means that users dont need to reload that much again (only the changed jars).

If you want to create jars which arent double clickable (or change the classpath or something like that) you can just zip the files with some everyday zip application and change the extension to jar.

Some simple image loading example can be found here:
http://kaioa.com/k/jarimage.jar (doubleclickable/src included... usually you dont include the source... mind you)

弾幕 ☆ @mahonnaiseblog
Offline bert

Junior Member




Miles of road and miles of code


« Reply #5 - Posted 2006-06-20 20:35:36 »

Try using an ImageIcon and then calling getImage() on the icon. It works! (at least for me...)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
import java.awt.*;  
import javax.swing.*;
import java.applet.*;
import javax.imageio.*;
import java.io.*;
/**
*
* @author Nick
*/

public class TestJApplet extends JApplet {
    ImageIcon icon=new ImageIcon("a.gif");
    /** Creates a new instance of TestJApplet */
    public TestJApplet() {
    }
   
    public void init()
    {
       
    }
   
    public void paint(Graphics g)
    {
        Image image = icon.getImage();
        g.drawImage(image, 100,100,this);
        g.drawRect(30,30,30,30);
    }
       
   
}

That's pretty much as close to your original code as you will get. However, (and this is just me) wouldn't it run a little bit faster if you declared your Image outside of any methods, then called getImage() within the init() method? This way the computer doesn't have to copy your image over and over every frame.

w00t, i'm winning
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (13 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!