0

Thank you for the suggestions as to why the Frame was not displaying. I have content displaying now with the following code (in a subclass in a main method with nothing being extended, though the class which has the main method is extending JPanel), but not correctly:

case start:
    JPanel mapFrame = new JPanel();
    mapFrame.setPreferredSize(new Dimension(950, 950));
    mapFrame.setBackground(Color.CYAN);
    mapFrame.setVisible(true);
    mapFrame.add(new Main());

    JPanel statBar = new JPanel();
    statBar.setBackground(Color.BLACK);
    statBar.setPreferredSize(new Dimension(400, 950));

    JFrame fullBox = new JFrame();
    fullBox.getContentPane().setLayout(new BorderLayout());
    fullBox.setPreferredSize(new Dimension(1350, 950));

    fullBox.getContentPane().add(statBar, BorderLayout.EAST);
    fullBox.getContentPane().add(mapFrame, BorderLayout.WEST);
    fullBox.setResizable(true);
    fullBox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fullBox.setVisible(true);
    fullBox.pack();

Previously, I had the following code instead of what I included above (in a subclass in the main method), which was working perfectly in displaying a background image and a player, whose position is updating with key input:

case start:
    JFrame mapFrame = new JFrame();
    mapFrame.pack();
    mapFrame.setSize(1350, 1000);
    mapFrame.setResizable(false);
    mapFrame.setLocationRelativeTo(null);
    mapFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    mapFrame.add(new Main());
    mapFrame.setVisible(true);

My paint method for the background and player is as follows (in the class extending JPanel):

public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(getBackgroundImage(), 0, 0, this);
    ((Penguin)player).draw(g2d);

public BufferedImage getBackgroundImage(){
    ImageIcon i = new ImageIcon(getClass().getResource(background));
    Image image = i.getImage();
    BufferedImage scaledImage = new BufferedImage(950, 950, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = scaledImage.createGraphics();
    g2.drawImage(image, 0, 0, 950, 950, null);
    g2.dispose();
    return scaledImage;
}

The fullBox Frame needs to be repainted continuously, as the mapFrame contains the player character and points to be collected and the statBar will later contain text that will also be updating the time, and points. I tried removing the background colour of the mapFrame and it is just the default colour of the JPanel. When I include the colour, there is a small white box that is fixed in position above the cyan - kind of strange.

screenshot of execution of current code

Community
  • 1
  • 1
gkdub
  • 1
  • 2
  • 1
    Start by calling setVisible last, after you've added all the components to the frame – MadProgrammer Mar 17 '17 at 23:47
  • Also, no need to extend `JFrame` as you're not using that `JFrame` anywhere (at least in the code shown). Also see [The use of multiple JFrames, Good / Bad practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) The general consensus says it's bad. For better help sooner please post a valid [mcve] – Frakcool Mar 17 '17 at 23:48
  • On JFrames, when you add visible components to them, you should do it via the content pane e.g. `fullBox.getContentPane().add(mapFrame, BorderLayout.WEST);` – bcr666 Mar 18 '17 at 00:02
  • @Brianbcr666Ray Not "required" since Java, what 5 or was it 6? `JFrame#add` automatically forwards the call to the content pane, it won't make much a difference either way, but I'm lazy and the less typing I need to do, the better :P – MadProgrammer Mar 18 '17 at 00:05
  • Based on what I can see of your code, I suggest also have a read of [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), it will eliminate the need for multiple frames and help make things a little easier with updating – MadProgrammer Mar 18 '17 at 00:07
  • @MadProgrammer Thanks for the suggestion, I will look into that. I'm wondering if you may be able to take a look at the edit I made to the question? – gkdub Mar 18 '17 at 18:39

1 Answers1

0
class MenuActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {

        switch (e.getActionCommand()) {
            case startGame:
                JFrame fullBox = new JFrame();
                fullBox.getContentPane().setLayout(new BorderLayout());
                JPanel mapFrame = new JPanel();
                mapFrame.setSize(950, 950);
                mapFrame.setVisible(true);
                mapFrame.add(new Main());
                fullBox.getContentPane().add(mapFrame, BorderLayout.WEST);
                JPanel statBar = new JPanel();
                statBar.setSize(400, 950);

                fullBox.getContentPane().add(statBar, BorderLayout.EAST);

                fullBox.setResizable(false);
                fullBox.setLocationRelativeTo(null);
                fullBox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                fullBox.pack();
                fullBox.setVisible(true);

                break;
            case loadLog:
                System.out.println("load game");
                break;
            case aboutGame:
                System.out.println("about game");
                break;
            case exitGame:
                System.out.println("exit game");
                System.exit(0);
bcr666
  • 2,157
  • 1
  • 12
  • 23