0

There are a number of similar posts, notably: Java Swing JLayeredPane not showing up and Top layer in JLayeredPane not displaying

But none of them seem to solve my specific problem.

I have the following code:

public class PlayScreen{
private JLayeredPane playScreen;
private Player player;
private JPanel towerButtons;
private JPanel detailPanel;
private JLabel map;
public PlayScreen(Player player){
    this.player = player;
    playScreen = new JLayeredPane();
    playScreen.setLayout(new BorderLayout());
    setTowerButtons();
    playScreen.add(towerButtons, BorderLayout.EAST);
    playScreen.setLayer(towerButtons, 0);        
    setDetailLabels();
    playScreen.add(detailPanel, BorderLayout.WEST);
    playScreen.setLayer(detailPanel, 0);        
    setMap(player.getNextMap());
    playScreen.add(map, BorderLayout.CENTER);
    playScreen.setLayer(map, 0);
    JButton playButton = new JButton("play");
    playScreen.add(playButton, BorderLayout.NORTH);
    playScreen.setLayer(playButton, 0);        
    playButton.addActionListener(new playListener());
    playScreen.setVisible(true);
}

where setTowerButtons(), setDetailLabels() and setMap() methods called in the constructor basically initialize the towerButtons, detailPanel and map fields. When the "play" button is pressed, the following code is called:

public class playListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Image image = null;
        try{
            image = ImageIO.read(new File("mob1.bmp"));
        }
        catch (Exception e1){
            System.out.println("image creation failed");
        }
        ImageIcon img = new ImageIcon(image);
        JLabel mob1Button = new JLabel(img);
        System.out.println("hi");            
        playScreen.add(mob1Button, BorderLayout.CENTER);
        playScreen.setLayer(mob1Button, 1);
        mob1Button.setVisible(true);
    }
}  

I know that this is definitely called, because "hi" appears on the output stream.

(The playScreen is a JLayeredPane that is constructed from this:)

public class View {
private JFrame frame;
private Container contentPane;
private Player player;
private LoadScreen loadScreen;
private PlayScreen playScreen;
public View(Player player){
    frame = new JFrame("Display");
    contentPane = frame.getContentPane();
    contentPane.setLayout(new BorderLayout());
    this.player = player;      
    frame.setLocationRelativeTo(null); //puts frame in middle of screen
    frame.pack();
    frame.setVisible(true);
}

public void makeLoadScreen(){
    loadScreen = new LoadScreen();
    contentPane.add(loadScreen.getLoadScreen(), BorderLayout.CENTER);
    frame.pack();
}

public void makePlayScreen(Player player){
    contentPane.remove(loadScreen.getLoadScreen());;
    playScreen = new PlayScreen(player);
    contentPane.add(playScreen.getPlayScreen());
    frame.pack();
}

public class LoadScreen{
    private JPanel loadScreen;
    public LoadScreen(){
        loadScreen = new JPanel();
        JButton play = new JButton("Play");
        JButton load = new JButton("Load");
        loadScreen.setLayout(new GridLayout(2,1,0,0));
        loadScreen.add(play);
        loadScreen.add(load);
        play.addActionListener(new OpenActionListener());
        load.addActionListener(new LoadActionListener());
    }

    public JPanel getLoadScreen(){
        return loadScreen;
    }

    public class OpenActionListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            makePlayScreen(player);//might not be receiving correct player variable?
        }
    }

    public class LoadActionListener implements ActionListener{
        public void actionPerformed(ActionEvent e){

        }
    }
}

}

public GameManager(){
    player = new Player();
    player.setNextMap("map.bmp");
    //Tower tower = new Tower1();
    //player.addUnlockedTower(tower);
    view = new View(player);
    view.makeLoadScreen();
}

A GameManager Class is created. This creates the loadScreen, which has two buttons, "play" and "load." Clicking on "play" creates the playScreen, which is governed by a separate class.

Sorry for all the code, but I have no idea anymore where it is going wrong, I thought it was to do with not setting the sizes of the playScreen, towerButtons etc, as in the similar post, but that didn't seem to work when I added preferred /min / max sizes.

Currently an image of the map comes up, but the image of the "mob1" does not appear at all, and I would expect it to overlay the map image.

Community
  • 1
  • 1
w1nter
  • 337
  • 4
  • 23
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson May 31 '15 at 15:21
  • Your concept of using a layered pane seems to be wrong. You don't set a layout manager for the layered pane and than add component to the NORTH, SOUTH, EAST, WEST etc. A JLayeredPane is for staking components in the 3rd dimension. Read the section from the Swing tutorial on [How to Use Layered Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html) for more information and working examples. – camickr May 31 '15 at 17:25
  • What would you suggest I used then? I just need a panel which can contain towerButtons, detailButtons, map and playButton, which won't be overlapping. Then I'll be adding various images that with sit on top of the map label. This seemed like a simple solution, or at least it would have been if it worked :D – w1nter May 31 '15 at 17:51

0 Answers0