This is the code i tried to use to paint an image on screen (location of the image is correct), the code runs but nothing is drawn on the screen. My program's getFile() and paintComponent() method:
BufferedImage image;
public void getFile() throws IOException{
image = ImageIO.read(new File("grass.png"));
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
You do realise that you never create an instance of DrawImage
nor do you actually add it to anything that could display it
public class DrawImage extends JPanel
{
BufferedImage image;
public DrawImage() throws IOException {
getFile();
}
public void getFile() throws IOException
{
image = ImageIO.read(new File("grass.png"));
}
//...
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DrawImage pane = new DrawImage();
JFrame frame = new JFrame();
frame.add(pane);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
}