I am trying to create a simple base game for my class in Game Engine Architecture. But my JFrame just doesn't show anything.
My code is currently structured as follows:
.Java implementation (This is some arbitrary implementation of the Engine package that I create)
public class Implementation { public static void main(String[] args){ World w = new World("Hej", "M:\\workspace\\SP6\\pics\\tulips.jpg",1024,768); } }
World.java
public class World extends JFrame{ private static final long serialVersionUID = 1L; private SpritePanel spritePanel; private JPanel bottom; private int width; private int height; public World(String windowCaption, String bgPath, int width, int height){ super(windowCaption); spritePanel = new SpritePanel(bgPath); add(spritePanel, BorderLayout.CENTER); System.out.println(spritePanel); bottom = new JPanel(); bottom.add(new JLabel("Hej")); add(bottom, BorderLayout.SOUTH); Dimension size = new Dimension(width,height); setSize(size); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); validate(); repaint(); }
SpritePanel.java
public class SpritePanel extends JPanel { private static final long serialVersionUID = 1L; private ImageIcon background; private ArrayList<Sprite> sprites = new ArrayList<Sprite>(); public SpritePanel(String bgPath){ background = new ImageIcon(bgPath); setLayout(null); Dimension size = new Dimension(background.getIconWidth(), background.getIconHeight()); setPreferredSize(size); setMaximumSize(size); setMinimumSize(size); } @Override protected void paintComponent(Graphics g){ super.paintComponent(g); g.drawImage(background.getImage(), 0, 0, this); System.out.println("painted panel"); } }
So, the main flow of actions at the moment (from what I see):
- I create a new World object in implementation
- World-constructor is called with the given parameters
- Set window title (this works)
- I am creating a new SpritePanel object with this bgPath
- The SpritePanel constructor is called and installs ImageIcon in the image that exists in this path
- SpritePanel is added to the frame in BorderLayout.CENTER
- I am adding a new JPanel to the frame in BorderLayout.CENTER
- I set the size and stuff
- I pack and set the frame to visible
- I check and repaint
The thing is, the paintComponent methods in JPanel and SpritePanel do not seem to be called. As you can see, I added System.out.println to the paintComponent for SpritePanel, and this line is never executed.
Another thing I noticed is that the package seems to know that there are components. Because if I comment on three lines
spritePanel = new SpritePanel(bgPath); add(spritePanel, BorderLayout.CENTER); System.out.println(spritePanel);
The window size at program startup decreases to the size of the "bottom" -JPanel. I do not see the JLabel that I added to the panel, but the window size is the size. So the pack () method seems to find the sizes of my components.
If I comment on the three lines that add the bottom panel, and the window size is reduced to the height and width obtained from the standard icons.
I tried different things to make it work, but to no avail. I programmed with Swing before and made working programs, but I just can't find the problem here.
Help really appreciate.