Reputation:
I have just started learning java GUI and I have a lot of problems with the images. I looked through multiple topics on this site and others too, but for some reason I cannot get this to work (though I am probably making a lot of mistakes and I just don't realise it). I just want to start with showing an image on the screen. To add some information - I am using IntelliJ; the image is stored in a resource folder that I have marked as a "library root" (also, the image is pretty small - 16x16, but I have also tried with a bigger image and it doesn't help me).
import javax.swing.*;
import java.awt.*;
public class Frame {
public static final int WIDTH = 1024;
public static final int HEIGHT = 768;
public Frame()
{
JFrame frame = new JFrame();
frame.setTitle("Shady Path");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.pack();
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.getContentPane().setBackground(Color.BLACK);
frame.setResizable(false);
//Font font = new Font(Font.MONOSPACED, Font.PLAIN, 10);
JLabel human = new JLabel(new ImageIcon(getClass().getResource("/human.jpg")));
Dimension humanDimension = new Dimension(150, 150);
human.setMinimumSize(humanDimension);
human.setPreferredSize(humanDimension);
human.setMaximumSize(humanDimension);
human.setLocation(100, 100);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(human);
frame.add(panel);
frame.setVisible(true);
}
}
Upvotes: 0
Views: 341