Results 1 to 17 of 17

Thread: ImageIO Error

  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default ImageIO Error

    I this error is related to reading an image, but I'm unsure, the code:

    Java Code:
    package pack.script.game;
    
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    //import java.awt.image.DataBufferInt;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    
    public class Game extends Canvas implements Runnable, KeyListener {
    	private static final long serialVersionUID = 1L;
    	
    	public boolean iRUNNING = false;
    	//public int cTICKCOUNT = 0;
    	
    	public static final String sNAME = "Java Game";
    	public static final String sVER = "v1.00";
    	public static final int sSIZE = 40;
    	public static final int sSCALE = 16;
    	public static int iSCALE = 1;
    	
    	public static final String sDISPLAYNAME = sNAME + " " + sVER;
    	public static final int sWIDTH = sSIZE * sSCALE;
    	public static final int sHEIGHT = (int) ((double)sWIDTH / 12D * 9D);
    	public static int iWIDTH = sWIDTH * iSCALE;
    	public static int iHEIGHT = sHEIGHT * iSCALE;
    	public static Dimension sDIMENSION = new Dimension(iWIDTH,iHEIGHT);
    	
    	private BufferedImage iIMAGE = new BufferedImage (iWIDTH, iHEIGHT, BufferedImage.TYPE_INT_RGB);
    	//private int[] pixels = ((DataBufferInt) iIMAGE.getRaster().getDataBuffer()).getData();
    	private JFrame sFRAME;
    	
    	public Game() {
    		setMinimumSize(new Dimension(iWIDTH,iHEIGHT));
    		setMaximumSize(new Dimension(iWIDTH,iHEIGHT));
    		setPreferredSize(new Dimension(iWIDTH,iHEIGHT));
    		
    		sFRAME = new JFrame(sDISPLAYNAME);
    		sFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		sFRAME.setLayout(new BorderLayout());
    		sFRAME.add(this, BorderLayout.CENTER);
    		sFRAME.setSize(iWIDTH, iHEIGHT);
    		sFRAME.pack();
    		
    		sFRAME.setResizable(false);
    		sFRAME.setLocationRelativeTo(null);
    		sFRAME.setVisible(true);
    	}
    	
    	public synchronized void start() {
    		iRUNNING = true;
    		new Thread(this).start();
    	}
    	
    	public synchronized void stop() {
    		iRUNNING = false;
    	}
    	
    	public void run() {
    		long cLASTTIME = System.nanoTime();
    		double sNSPERTICK = 1000000000D / 60D;
    		
    		int cFRAMES = 0;
    		int cTICKS = 0;
    		
    		long cLASTTIMER = System.currentTimeMillis();
    		double delta = 0;
    		
    		while (iRUNNING) {
    			long cNOWTIME = System.nanoTime();
    			delta += (cNOWTIME - cLASTTIME) / sNSPERTICK;
    			cLASTTIME = cNOWTIME;
    			boolean shouldRender = true;
    			
    			while (delta >= 1) { cTICKS++; tick(); delta--; shouldRender = true; }
    			
    			try { Thread.sleep(2); } catch (InterruptedException e)
    			{ e.printStackTrace(); }
    			
    			if (shouldRender) { cFRAMES++; }
    			if (System.currentTimeMillis() - cLASTTIMER >= 1000) {
    				cLASTTIMER += 1000;
    				System.out.println(cFRAMES + " frames, " + cTICKS + " ticks");
    				cFRAMES = 0; cTICKS = 0;
    			}
    		}
    	}
    	
    	public void tick() {
    		//cTICKCOUNT++;
    		/*for (int i = 0; i < pixels.length; i++) {
    			pixels[i] = i + cTICKCOUNT;
    		}*/
    		BufferStrategy sBUFFERSTRATEGY = getBufferStrategy();
    		if (sBUFFERSTRATEGY == null) {
    			createBufferStrategy(3);
    			return;
    		}
    		
    		Graphics sGRAPHICS = sBUFFERSTRATEGY.getDrawGraphics();
    		sGRAPHICS.drawImage(iIMAGE, 0, 0, getWidth(), getHeight(), null);
    		
    		rPREINIT(sGRAPHICS, sBUFFERSTRATEGY);
    	}
    	
    	public void rPREINIT(Graphics g, BufferStrategy bs) {
    		
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		
    		g.setColor(Color.WHITE);
    		gRECT(g,5,5,645,485,true);
    		
    		g.dispose();
    		bs.show();
    		
    		g.drawImage(tile("example_tile_grass_01.png"), 10, 10, null);
    	}
    	
    	public BufferedImage tile(String path) {
    		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    		try { image = ImageIO.read(new File(path)); }
    		catch (IOException e) { e.printStackTrace(); }
    		
    		return image;
    	}
    	
    	public void gRECT(Graphics g, int x1, int y1, int x2, int y2, boolean fill) {
    		int x[] = {x1,x2,x2,x1,x1};
    		int y[] = {y1,y1,y2,y2,y1};
    		if (fill == false) {
    			g.drawPolygon(x, y, 5);
    		} else {
    			g.fillPolygon(x, y, 5);
    		}
    	}
    	
    	public void gPIX(Graphics g, int x, int y) {
    		g.drawLine(x, y, x, y);
    	}
    	
    	public static void main(String[] args) {
    		new Game().start();
    	}
    
    	public void keyPressed(KeyEvent arg0) {
    		
    	}
    
    	public void keyReleased(KeyEvent arg0) {
    		
    	}
    
    	public void keyTyped(KeyEvent arg0) {
    		
    	}
    }
    And The Error:

    Java Code:
    javax.imageio.IIOException: Can't read input file!
    	at javax.imageio.ImageIO.read(Unknown Source)
    	at pack.script.game.Game.tile(Game.java:131)
    	at pack.script.game.Game.rPREINIT(Game.java:126)
    	at pack.script.game.Game.tick(Game.java:112)
    	at pack.script.game.Game.run(Game.java:84)
    	at java.lang.Thread.run(Unknown Source)
    I have created the Game Folder within which there is the source folder, and in the same game Folder there's a resources folder, I've put the image in the folder, and I went to build path properties of the game folder, and added the Resources folder as a (class folder), my image is .PNG, my image has the same name as mention.

    If you can help Thanks
    Augustas

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: ImageIO Error

    Check that the file is where the program is trying to read it and spelled correctly. One way to see is create a File object for the file and print its absolute path.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: ImageIO Error

    I've tried many things, still not working... I think it's related to these lines in specific:

    Java Code:
    		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    		try { image = ImageIO.read(new File(path)); }
    		catch (IOException e) { e.printStackTrace(); }
    or even deeper this specific section:
    Java Code:
    new File(path)
    But I don't know, just can't find the issues, help

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: ImageIO Error

    Did you do what I suggested? What printed out for the path to the file? Was the file at that location?

    BTW The assignment statement on line 2 will replace the value assigned on line 1.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: ImageIO Error

    I'm not sure exactly what you mean, and I've tried going to debug mode, it allowed to try and change the source folder and resume, I've tried changing it to all res files or folders that existed, it still gave me the same error. Also I've watched this tutorial video, instead of
    new File(path)
    he used
    Game.class.getResourceAsStream(path)
    where the Game is replaced with his Sprites class, but I'm making everything in one class.

    Kinda frustrating, just a simple error and delays the whole project. xD

    EDIT: Also instead of using BufferedImage image = new BufferedImage(args) he used just BufferedImage image = null;

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: ImageIO Error

    1) create an instance of a File object using the path
    2) print out that File object's absolute path (sse the API doc for full method name)
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: ImageIO Error

    I'm sure it's not the path, literally look here, in debug mode:
    ImageIO Error-hfgvrpe.png

    I click OK, it still gives me Source not found. I manually go to that same folder, the image is there, spelling is good, I copy paste, see no difference through my eyes. PNG format, as so mentioned in DOWN-CASE letters in my code.

  8. #8
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: ImageIO Error

    HEY, so, I managed to get it work, but the image isn't there, I just added res/ to the path, and it worked, I've tried making it without /res and putting the image in the game folder. It worked! So I guess it looked in the game folder. But the image is like not there, I play the game I have my 5 pixel thick borders, no image on the corner?

  9. #9
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: ImageIO Error

    Sorry again, fixed that aswell, I put the drawImage after disposing graphics and showing the strategy, whereas I put it back now.
    FINALLY!!! Got mah image to work, can I just ask, is this how layers work? Just print the image after you print the lower layer image? Because my image overlays the black borders I've written to appear before I've written for the image to appear.
    Last edited by augustas656; 04-08-2014 at 03:40 AM.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: ImageIO Error

    What printed out when you printed the File's absolute path? Was the image file at the location that was shown?

    Does the code read the image now?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: ImageIO Error

    I didn't read the absolute path, because I used debug mode to change it, but it still didn't work, I then just experimented and added res/ before the image name and it worked! Guess I'll just leave it like that. Thanks though...

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: ImageIO Error

    I guess making suggestions is a waste of time.
    I take it that you still don't know where the program was looking for the file.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: ImageIO Error

    Quote Originally Posted by Norm View Post
    I guess making suggestions is a waste of time.
    If the person you are making suggestions to is in the mode of making a problem go away in stead of learning what causes it so it may be properly solved, then it is indeed a fruitless effort.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: ImageIO Error

    To the OP: Norm has asked you FOUR times what is printed when you print the absolute path of your File object, but you never tried and showed the result! People here are eager to help, but they need that info. It also teaches you basic debugging skills.

    It's like going to the doctor and refusing to say where it hurts:
    - "Doctor, it hurts."
    - "Where?"
    - "It just hurts"
    - "But where?"
    - "It doesn't hurt always"
    - "BUT WHERE DOES IT HURT?"
    - "... It just does..."
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  15. #15
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    120
    Rep Power
    0

    Default Re: ImageIO Error

    Quote Originally Posted by Norm View Post
    I guess making suggestions is a waste of time.
    I take it that you still don't know where the program was looking for the file.
    It was looking for the file in the Game folder, if I know that if typing res/file.png will fix it, and if you remove the res/ and put the file into the game folder also makes it work, it's a lack of common sense for me not to understand that it is originally looking for the file in the Game folder. Which has the resources folder in it.

    And, as I said, I thought checking the absolute path isn't worth it because in debug mode I can literally change it, but it still didn't work, and then it actually WAS the issue that it was the wrong path, it's either that Eclipse is faulty or I'm missing out on something. Either way, knowing that I can't fix it that way, I will use absolute path next time.

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: ImageIO Error

    Things to learn: new File("blah.txt") will represent a file (existing or not), relative to the current directory. You can find out what that is by examining System.getProperty("user.dir"). If you start your application in C:\MyProjects\Game then System.getProperty("user.dir") will contain "C:\MyProjects\Game" and the File will be initialized to "C:\MyProjects\Game\blah.txt". If you have a subfolder Resources there, you need to add that to the path: new File("Resources/blah.txt");

    Edit: using absolute paths is generally a bad idea. It will almost certainly never work on other computers, unless installed in EXACTLY the same directory structure. For standalone applications/games, make it a habit to either use packaged resources (this.getClass().getResourceAsStream()) or use relative paths (System.getProperty("user.dir"))
    Last edited by SurfMan; 04-08-2014 at 04:16 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: ImageIO Error

    Quote Originally Posted by SurfMan View Post
    new File("blah.txt") will represent a file (existing or not), relative to the current directory.
    I feel a need to clarify because I don't 100% agree with your explanation although I know the intention is correct. The current WORKING directory is what is referred to here.

    Q: And what directory is that?
    A: the directory where the java command was invoked ('application' = Java)

    It becomes interesting when you run an executable jar by double clicking on it because then you don't really 'run' java', rather the OS runs it for you. The current working directory is then the directory where the jar lives.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. ImageIO question
    By lordsurya08 in forum New To Java
    Replies: 1
    Last Post: 12-23-2011, 07:17 AM
  2. JPEGCodec and ImageIO
    By Lacrim in forum Advanced Java
    Replies: 0
    Last Post: 12-14-2011, 02:57 PM
  3. Error with ImageIO
    By stegano in forum New To Java
    Replies: 4
    Last Post: 03-31-2011, 03:59 PM
  4. Trouble with ImageIO
    By sari in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 09:42 PM
  5. Help with ImageIO
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •