I'm experimenting with graphics in Java, creating a domino game. However, I have run into another mysterious little "challenge"... I think I really ticked off the IDE this time
Anyway here is my code:
// In the main class
import java.awt.Color;
import javax.swing.JFrame;
public class GameBoard extends JFrame {
public static void main(String[] args) {
JFrame game = new JFrame();
game.setTitle("Domino");
game.setSize(800, 600);
game.setDefaultCloseOperation(EXIT_ON_CLOSE);
game.setBackground(Color.GREEN);
Domino double6 = new Domino("images/double_6.png",16,16,'H',6,6);
game.add(double6);
// Create pieces
game.setVisible(true);
}
}
// The game piece class
package domino;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Domino extends JPanel {
// Instance variables for each piece
public int n1, n2, x, y;
public char position;
// n1 is the first number, n2 is the second number, x and y are coordinates, position refers to horizontal or vertical (ideally would be a different image for each direction)
public BufferedImage img = null;
public String fileName;
// Constructor
public Domino(String fileName, int x, int y, char position, int n1, int n2) {
this.fileName = fileName;
this.x = x;
this.y = y;
this.n1 = n1;
this.n2 = n2;
this.position = position;
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.position == 'H') {
try {
// Here's where the trouble appears to rear its ugly head...
img = ImageIO.read(new File(fileName));
g.drawImage(img, this.x, this.y, null);
} catch (IOException ex) {
// This code was generated by the system - it wouldn't even let me do the drawImage code about without a try/catch.
Logger.getLogger(Domino.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
// If all else fails I can at least draw the game pieces manually.
g.setColor(Color.WHITE);
g.fillRect(this.x, this.y, 32, 64);
// etc.
}
}
}
Anyway, when I run it, I get this:
run:
Jul 04, 2013 11:33:13 PM domino.Domino paintComponent
SEVERE: null
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at domino.Domino.paintComponent(Domino.java:43)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
at javax.swing.JComponent.paint(JComponent.java:1040)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.awt.Container.paint(Container.java:1967)
at java.awt.Window.paint(Window.java:3877)
at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Jul 04, 2013 11:33:14 PM domino.Domino paintComponent
SEVERE: null
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at domino.Domino.paintComponent(Domino.java:43)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
at javax.swing.JComponent.paint(JComponent.java:1040)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.awt.Container.paint(Container.java:1967)
at java.awt.Window.paint(Window.java:3877)
at javax.swing.RepaintManager$3.run(RepaintManager.java:807)
at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 6 seconds)
So I did some research. First, I went to the Java/Oracle website and found their tutorial on drawing images (and in fact my code very closely based on theirs, at least the try/catch part). Then I tried to remove the try/catch thing to see what the exception would be (I was 99% sure there would be one, lol). Well my IDE (NetBeans) responded by bugging out and basically forcing me to use one. So then I went into the project folder and checked to make sure the file was in the right place (it was). So just for the heck of it, I copied it into every other folder in the project to see if that would help. It didn't.
So it's painfully obvious I've done something wrong, and for whatever reason Java can't or won't read the file, even though it exists and is in correct location (and now every location), but I have no idea what else to do about it.
So, as always, I'm open to any ideas, suggestions or other information. Thanks in advance. : )
What happens if you take the images/ part out of the path, and make sure the file is in the same directory as your exe? So, instead of
Domino double6 = new Domino("images/double_6.png",16,16,'H',6,6);
try
Domino double6 = new Domino("double_6.png",16,16,'H',6,6);
That should check the same directory as where you are running your program from.