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");
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.*;
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.