Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  (Solved)I need some help with Loading Images outside Eclipse IDE  (Read 630 times)
0 Members and 1 Guest are viewing this topic.
Offline SeriousPeter

Senior Newbie


Medals: 1



« Posted 2013-05-25 16:16:19 »

Hello everyone! i would like help for a problem that i continuesly have... This is my first Post and sorry if my english or post is not perfect. I was working on a game and everytime i stuck on the same problem. While i try to load images, i succesded inside Eclipse IDE but when i export it i get error. I tryed to make a res folder and add it into my project libraries which used to solve this problem but at the moment again i have issues. I don't know what to do or how to fix it. Is a bug of Eclipse IDE ? or something i am doing wrong.


My Resources class which i use to load all my images is the following:

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  
public class Resources extends Component {
   private static final long serialVersionUID = -8912492884211057348L;
   public static BufferedImage bullet,craft,emeny,bg1,bg2;

     public Resources(){
     
         try {
             ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
             InputStream input = classLoader.getResourceAsStream("bullets.png");
             
            bullet = ImageIO.read(input);
           
             input = classLoader.getResourceAsStream("airplane.png");
            craft = ImageIO.read(input);
           
             input = classLoader.getResourceAsStream("background.png");
            bg1 = ImageIO.read(input);
         
             input = classLoader.getResourceAsStream("enemy.png");
            emeny = ImageIO.read(input);
         } catch (IOException e) {
         }
      }
   
}


After that in my Main Method which is plane.class where i create a JFRAME for my game and i add a new board() which is where i do everything else. Here is my Main Plane method:

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  
 extends JFrame {
   /**
    *
    */

   private static final long serialVersionUID = 8957704465368507754L;
   public static final int DISPLAY_HEIGHT = 800;
   public static final int DISPLAY_WIDTH = 600;


   plane() {
   
      super("Plane");// INITIALISE THE JFRAME

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      setSize(new Dimension(DISPLAY_HEIGHT, DISPLAY_WIDTH));
      setLocationRelativeTo(null);

      setResizable(false);
      Resources r = new Resources();
      add(new board());
      setVisible(true);
   }

   public static int getDISPLAY_HEIGHT() {
      return DISPLAY_HEIGHT;
   }

   public static int getDISPLAY_WIDTH() {
      return DISPLAY_WIDTH;
   }

   public static void main(String[] args) {
      new plane();
   }

}


I create an Resources object with the name r which loads all the Images. And now about my Board class which the paint method looks solike this

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
   public void paint(Graphics g) {
      paintComponent(g);
      g.setFont(new Font("Arial", 0, 25));
      g.setColor(new Color(255, 5, 128));

      g.drawImage(Resources.bg1, back1.getX(), back1.getY(), this);
      g.drawImage(Resources.bg1, back2.getX(), back2.getY(), this);

      craft.paint(g);
      for (int i = 0; i < weapon.size(); i++) {
         weapon.get(i).paint(g);
      }
      for (int i = 0; i < 5; i++) {
         enemyarray[i].paint(g);
      }


When i run it though Eclipse IDE works excelent but when i want to create a stand alone .jar it throws me

Exception in thread "main" java..lang.IllegalArgumentException: input == null! to my Resourse Class.

I have continuesly have problems with this issue and sometimes i solve it but always seems to come back. I would like if someone could help me with issue.

In addition , while i was doing some research on how to load and read images i created this class

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  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
package com.petrosarts.plane;


import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;

import javax.imageio.*;
import javax.swing.*;
 
/**
 * This class demonstrates how to load an Image from an external file
 */

public class LoadImageApp extends Component {
           
    public static BufferedImage img;
    public static  ImageIcon image;
    public void paint(Graphics g) {
        g.drawImage(LoadImageApp.img, 0, 0, null);
    }
 
    public LoadImageApp() {
       try {
          ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
          InputStream input = classLoader.getResourceAsStream("strawberry.jpg");
       
           img = ImageIO.read(input);

       } catch (IOException e) {
       }
 
    }
 
    public Dimension getPreferredSize() {
        if (img == null) {
             return new Dimension(100,100);
        } else {
           return new Dimension(img.getWidth(null), img.getHeight(null));
       }
    }
 
    public static void main(String[] args) {
 
        JFrame f = new JFrame("Load Image Sample");
             
        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
 
        f.add(new LoadImageApp());
        f.pack();
        f.setVisible(true);
    }
}


which runs perfectly and it works outside IDE which is wierd cause i tryed to do the same technique on mine but didn't work out.
Thank you in advance for your help.
Offline Andre Lopes
« Reply #1 - Posted 2013-05-25 16:19:23 »

ok, when you export the .jar, the images should be in same folder than the .jar

Unless it android, if it is, kill me , because i cant run libgdx on it. Except the example project meh.
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-05-25 16:20:13 »

If it runs in Eclipse I would assume its a problem with how your loading it. Make sure everything is inside of your project. After that make sure those are being exported when you create a runnable jar.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #3 - Posted 2013-05-25 16:24:31 »

If it runs in Eclipse I would assume its a problem with how your loading it. Make sure everything is inside of your project. After that make sure those are being exported when you create a runnable jar.

Oh yes, i forgot that Eclipse have that export option.
Offline alaslipknot
« Reply #4 - Posted 2013-05-25 16:36:17 »

this is how i load images from a resource folder :
1  
2  
3  
4  
5  
6  
       try {
         URL url = this.getClass().getResource("image.png");
         image = ImageIO.read(url);
      } catch (IOException e) {
         e.printStackTrace();
      }

am not sure if you need a classLoader

good luck

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline SeriousPeter

Senior Newbie


Medals: 1



« Reply #5 - Posted 2013-05-25 20:51:48 »

Yes the problem is the way i load images indeed.... I created a folder name "Res" and then i load it as Class Folder though Propertise for plane java build path / Libraries.

I used
1  
2  
URL url = this.getClass().getResource("bullets.png");
            System.out.println(" " + url);


to see what does it point too and its returns me null but if i put /bullets.png intended it point me to the file in my hard drive folder. I am confused Sad
Offline SeriousPeter

Senior Newbie


Medals: 1



« Reply #6 - Posted 2013-05-25 21:02:50 »

Fixed it... finaly... it was a mistake because i misspelled enemy.png its enemy.PNG .... Stupit me ^_^ thank you everyone for the help to load images i used URL Smiley as alaslipknot said thank you
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #7 - Posted 2013-05-26 03:39:33 »

I hate silly mistakes like that

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.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (45 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!