Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  A noobish question about loading BufferedImage  (Read 1110 times)
0 Members and 1 Guest are viewing this topic.
Avenger
Guest
« Posted 2006-02-10 15:46:36 »

Hi all just a quick questiong Smiley

I understood that we can only load BufferedImages with ImageIO.read( url ) ;   , right?

Thing is I have no idea about URL so I took only the example from the Planetalua space invaders tutorial and made the URL like this :
url = getClass().getClassLoader().getResource(filename);

Thins is , ok it works .. but what If I wan tto have my images in another place and not in the classloader?How do I form my own URL to tell it where to load the images from?

I tried URL url = new URL( String string) ;

But it said something about malfunctioned URL and about a protocol.Please excuse my noobiness Sad , and any help is appreciated ! Cheesy
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2006-02-10 17:07:10 »

ImageIO's read() method is also available as a version which takes an InputStream.

>but what If I wan tto have my images in another place and not in the classloader?

Why?

弾幕 ☆ @mahonnaiseblog
Avenger
Guest
« Reply #2 - Posted 2006-02-10 17:53:05 »

hmm don't know why , I just want toexplore all my options I guess.Hm did not know it can take InputStream too ... need to studdy the API's better  I guess Lips Sealed


EDIT: Another question arose .. I reread the tutorial to help me divide the code in classes and I made the SpriteCache class like in the tutorial :

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  
43  
44  
package Version01;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.net.URL;
import javax.imageio.ImageIO;

public class SpriteCache
{
    private HashMap sprites;
    private URL url;
   
    public SpriteCache()
    {
        sprites = new HashMap();
    }
   
    private  BufferedImage loadImage(String filename)
        {
           
            try
            {
                url = getClass().getClassLoader().getResource(filename);
                return ImageIO.read(url);
            }
             catch(Exception e)
             {
                 e.printStackTrace();
                 return null;
             }
        }
   
   
    public BufferedImage getSprite(String name)
    {
        BufferedImage img = (BufferedImage)sprites.get(name);
        if(img == null)
        {
            img = loadImage("images/"+name);
            sprites.put(name , img);
        }
        return img;
    }
}


Thing is in my main class .. in the paintWorld() method I try to load an image in a random position of the screen
 
1  
2  
3  
4  
5  
6  
7  
8  
public void paintWorld()
    {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setColor(getBackground());
        testImage = spriteCache.getSprite("test.gif");
        g.drawImage(testImage , 80 , 90 , this);
        strategy.show();
    }


When I run it it throws a NullPointerException .... Sad    seems it can not find the image ... I am sure the image has the right name though and is at the right place.So what is wrong with the code?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Avenger
Guest
« Reply #3 - Posted 2006-02-11 00:35:00 »

Sorry about double posting , but I bump my thread cause I edited the above reply of mine with another question
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #4 - Posted 2006-02-11 00:40:24 »

Uhm yea... use leading slashing in the path for making it relative to the roots in the classpath.

say your jar contains:
omg/wtf/Foo.class
omg/Bar.class
gfx/foobar.png

and another jar in your cp contains:
gfx/barfoo.png

Then you can access the foobar.png with "/gfx/foobar.png" and barfoo.png with "/gfx/barfoo.png" from Foo.class or Bar.class.

Thats the whole magic Smiley

edit: See details here:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResource%28java.lang.String%29

弾幕 ☆ @mahonnaiseblog
Avenger
Guest
« Reply #5 - Posted 2006-02-11 01:13:15 »

Hmmmm If I understood correctly all I had to do was add a   /   at the "images/"+name     ?

I did that but it did not work Sad

Or again I might have completely misunderstood :p , it is 2:00 here in Greece and I am kind of sleepy ; (
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #6 - Posted 2006-02-11 01:36:16 »

Well, whats the location of that test.gif? (Note that the whole thingy inclusive the extension is case sensitive)

弾幕 ☆ @mahonnaiseblog
Avenger
Guest
« Reply #7 - Posted 2006-02-11 01:39:40 »

C:\Java2\Game\build\classes\images\test.gif

That is where it is ... but I don't think it is that kind of mistake because I move the images folder and recompiled and ran many many times.
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #8 - Posted 2006-02-11 02:12:53 »

http://kaioa.com/k/jarimage.jar

Comes with source. Its double clickable and it also works in extracted form.

弾幕 ☆ @mahonnaiseblog
Avenger
Guest
« Reply #9 - Posted 2006-02-11 11:57:48 »

Thanks for even going through the trouble of doing that  Grin

I have no idea what iw wrong with my code so .. all I can do is rewrite it now that it is morning ... the sun is shining outside .. the coffe is plenty e.t.c.  Wink

Also thanks again again and again for striving to answer every single question Cheesy
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.

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

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

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

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (53 views)
2014-07-14 18:02:53
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!