I'm pretty sure it's not a file location issue since the image file is in the resource folder and I don't get a missing input file error when I run the code. I have been trying to teach myself how to code a very simple 2D game using tutorials online and I haven't found the answer as to why the image won't show up.
Main class
import javax.swing.JFrame;
public class main
{
public static void main(String[]args)
{
JFrame frame = new JFrame("First Game");
BlockBreakerPanel panel = new BlockBreakerPanel();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(800, 800);
frame.setResizable(false);
}
}
Block.java
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.awt.Component;
public class Block extends Rectangle
{
Image pic;
boolean destroyed = false;
int posX,posY;
int width,height;
Block(int x, int y, int w, int h, String s)
{
this.posX = x;
this.posY = y;
this.width = w;
this.height = h;
try
{
pic = ImageIO.read(new File("Resources/"+s));
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void draw(Graphics g, Component c)
{
g.drawImage(pic, posX, posY, width, height, c);
}
}
BlockBreakerPanel.java
import javax.swing.JPanel;
import java.util.ArrayList;
public class BlockBreakerPanel extends JPanel
{
ArrayList<Block> blocks = new ArrayList<Block>();
Block cobble = new Block(1,1, 160, 160, "Cobble.png");
BlockBreakerPanel()
{
}
}