Search code examples
javaswinggraphicsjframeimageicon

Can't Get ImageIcon to Display


I've looked at a ton of other questions where people have similar issues to what I'm having here (most solutions found by simple mistakes) but I can't for the life of me figure out why my graphics won't display in my jframe. I'm pretty new to Java Graphics so I'd appreciate all the help that I can get. (If someone thinks this is a repeat question, all I ask is that you wait until I get an answer before you close it)

Oh, also, when I run the program, it tells me that the repaint method is called, if that helps in some way

package game.try5;

import java.awt.Dimension;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Window {

    public Window() {
        JFrame frame = new JFrame("Epic Game");
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GamePanel panel = new GamePanel();
        frame.add(panel);

        frame.setVisible(true);
    }

    public static void main(String[] args){
        Window window = new Window();
    }
}   

.

package game.try5;

import java.awt.Graphics;

import javax.swing.JPanel;

public class GamePanel extends JPanel{
    GameObject go = new GameObject(0,0,false,"Dog.jpg");

    public GamePanel(){
        repaint();
        System.out.println("Repaint method called");
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(go.getImg().getImage(), go.getxLoc(), go.getyLoc(), 50, 50, null);
        System.out.println("Graphics method called");
    }
}

.

package game.try5;

import java.awt.Dimension;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Window {

    public Window() {
        JFrame frame = new JFrame("Epic Game");
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GamePanel panel = new GamePanel();
        frame.add(panel);

        frame.setVisible(true);
    }

    public static void main(String[] args){
        Window window = new Window();
    }
}   

Solution

  • Comment out frame.setLayout(null); and you'll most likely see the game.

    1. The default layout of a JFrame is currently a BorderLayout
    2. An object added to a BorderLayout without a constraint defaults to the CENTER.
    3. An object in the CENTER of a BorderLayout is stretched to the available with and height.
    4. Since the code calls frame.setSize(800,600); that available width and height is 800x600 pixels less the frame decorations.

    A better all round approach is to:

    1. Have the custom painted component override getPreferredSize() to return a sensible value.
    2. Add the component to a container.
    3. Lastly, pack the top level window. This will make the frame the smallest size it needs to be in order to display the content.