0

I'm working on implementing a game in layered panes and i'm running into some layout issues. For some reason, nothing lines up correctly. I end up with a white space at the top of my frame and the frame never extends out to the correct size to fit everything. I've tried frame.pack() because I've done that before (without layeredpanes) but that doesn't seem to work either.

If anybody could help me out, I'd appreciate it. I have a feeling there is something small that I'm missing.

public class gameGUI extends JFrame{

//set up all the variables that will be used in the gameGUI
static int skyWidth = 1024;
static int skyHeight = 120;
static int sidebarWidth = 120;
static int sidebarHeight = 480;
static int boardWidth = 904;
static int boardHeight = 480;
private JLayeredPane pane = new JLayeredPane();
public static YardDriver thisDriver;
public static int getScreenWidth(){return boardWidth;}
public static int getScreenHeight(){return boardHeight;}

public JLayeredPane getPane(){
    return pane;
}


//Convert the width of the screen to work with the gui
public int convertX(int rainLoc,BufferedImage img){
    int x = rainLoc % 30;
    return ((boardWidth-img.getWidth())*x) / YardDriver.getScreenWidth();
}

//Convert the height of the screen to work with the gui
public int convertY(int rainLoc, BufferedImage img){
    int y = (rainLoc - (rainLoc % 30)) / 30;    
    return ((boardHeight-img.getHeight())*y) / YardDriver.getScreenWidth();
}   

//Convert the screen to the board
public static int convertStoB(Point screenLoc){
    int y = ((screenLoc.y - skyHeight) * YardDriver.getScreenHeight())/boardHeight;
    int x = ((screenLoc.x - sidebarWidth) * YardDriver.getScreenWidth())/boardWidth;
    return x + y*YardDriver.getScreenWidth();
}

private void loadAll() {

}

public gameGUI() {
    loadAll();

    JPanel timer = new JPanel();
    //place the image in a jlabel and set bounds
    timer.add(sky);
    //Set preferredsize, border, and set opaque
    timer.setPreferredSize(new Dimension(skyWidth,skyHeight));
    timer.setBounds(0, 0, skyWidth, skyHeight);
    timer.setOpaque(true);
    timer.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));

    JPanel sidebar = new JPanel();
    sidebar.setBounds(0, skyHeight, sidebarWidth, sidebarHeight);
    JButton nativeOne = new JButton();
    JButton nativeTwo = new JButton();
    JButton nativeThree = new JButton();
    JButton exoticOne = new JButton();
    JButton exoticTwo = new JButton();
    JButton exoticThree = new JButton();
    //Set the preferredsize and set a border
    sidebar.setLayout(new GridLayout(6, 1));
    sidebar.setPreferredSize(new Dimension(sidebarWidth, sidebarHeight));
    //Add each button to the sidebar
    sidebar.add(nativeOne);
    sidebar.add(nativeTwo);
    sidebar.add(nativeThree);
    sidebar.add(exoticOne);
    sidebar.add(exoticTwo);
    sidebar.add(exoticThree);
    sidebar.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));

    JPanel board = new gamePanel(this);
    board.setBounds(sidebarWidth, skyHeight, boardWidth, boardHeight);
    board.setPreferredSize(new Dimension(904, 480));
    board.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));

    timer.setVisible(true);
    sidebar.setVisible(true);
    board.setVisible(true);

    pane.add(timer, new Integer(1));
    pane.add(sidebar, new Integer(1));
    pane.add(board, new Integer(1));
    pane.setBounds(0,0,1024, 600);

    setTitle("Storm Watch");
    setSize(1024, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Pack the frame and set it to visible
    setVisible(true);
    setContentPane(pane);

}
    public static void main(String[] args){

        //Create a new frame with the createAndShowGUI 
        JFrame frame = new gameGUI();
    }
  • 2
    Ahh! Too much code! Could you provide a SSCCE? http://www.sscce.org – tckmn Nov 15 '12 at 02:26
  • 1
    @PicklishDoorknob The images are also a problem for making an [SSCCE](http://sscce.org/). The OP will need to do something like a) Hot-link to small (in bytes) images. b) Generate them in code. c) Substitute in J2SE components instead, that show the same layout problem. -- That being said, I suspect the problems are due to using a `JLayeredPane` which, from memory, ignores all content size hints (or at least has no layout-manager by default). – Andrew Thompson Nov 15 '12 at 02:30
  • 1
    @AndrewThompson haha, everyone always tells me to use one but when I try to tell other people it doesn't work :P – tckmn Nov 15 '12 at 02:36
  • 1
    `ImageIO.read(new File("images/Raindrop.png"));` Not related to the immediate problem, but at time of deployment, these will likely be an [tag:embedded-resource] that will only be accessible via. URL. See the [embedded-resource tag Wiki](http://stackoverflow.com/tags/embedded-resource/info) for how to form an `URL`. – Andrew Thompson Nov 15 '12 at 02:38
  • @PicklishDoorknob No, you're comment was spot on. If anything, it is the document that is lacking. I wrote it, yet am often adding those same types of caveats to advice to post an SSCCE of code that deals in images. Please keep up advising an SSCCE. If it has to do with images, and you can find the motivation, add the tips on how to rearrange where they are loaded from. :) – Andrew Thompson Nov 15 '12 at 02:41
  • I can't see any reason why your main panel needs a layered pane. The only panels that I can that might benifit from begin layered panes are the board and sky, the rest could be laid out with a layout manager. Also, I would be making a lot more use of preferred size - IMHO – MadProgrammer Nov 15 '12 at 02:55
  • I'm using layeredpanes so that when i implement an intro screen and a scoreboard screen, i can just bring those panes to the front or push them back when they are needed. –  Nov 15 '12 at 02:56
  • 1
    @Requiem Then use a `CardLayout` to switch between the groups. The `LayeredPane` is over kill - IMHO - It's a good choice for the sky and board, but not for the rest of game... – MadProgrammer Nov 15 '12 at 03:21
  • So the whole game should be based on a card layout then to switch between the various scenes I guess you could call them? Like menu, to game, to scoreboard? I'm not familiar with card layout at all. Are there any tutorials you would suggest? –  Nov 15 '12 at 20:41

1 Answers1

1

You forgot to set the LayoutManager, do something like this:

this.pane.setLayout(new BorderLayout());
this.pane.add(timer, BorderLayout.PAGE_START);
this.pane.add(sidebar, BorderLayout.LINE_START);
this.pane.add(board, BorderLayout.CENTER);
OblongZebra
  • 466
  • 5
  • 16