Matthew
Matthew

Reputation: 867

Using JButtons with Canvas

I'm currently trying to create a primitive HUD inside a JFramewindow for a test game I am working on. In the course of doing this, all my attempts have been thwarted. Either the panel which contains all the buttons that are meant to represent the HUD does not display, or, the Canvas I attempt to draw to appears to not exist as no graphics I draw to it appear.

What I believe the problem is, is that the layers are not 'overlapping', and thus one is blocking out the other. However, I tried using a Flowlayout()(just for testing purposes) & that still did not fix my issue. So right now, I am at a loss for trying to have a HUD of buttons/Labels.

For visual purposes, I am trying to do something like this:

Example image

But I am actually getting this (just a test drawn tile, no HUD): enter image description here Here is the code that is involved:

public static void createDrawFrame(int width, int height) {


    f = new JFrame();
    c = new Canvas();

    c.setPreferredSize(new Dimension(width, height));
    c.setFocusable(false);

    f.add(c);
    f.pack();

    f.setSize(width, height);

    f.setLocationRelativeTo(null);

And

public static JFrame getDrawFrame() {

    f.setVisible(true);

    return f;
}

And

public static JFrame createHUD(JFrame f, Game game) {

    Panel p = new Panel();

    p.setLayout(new FlowLayout());
    p.setSize(100, 100);
    p.setLocation(300, 0);

    Button b0 = new Button("Settings");
    Button b1 = new Button("Exit");

    MenuListeners mListeners = new MenuListeners(game);

    b0.addActionListener(mListeners);
    b1.addActionListener(mListeners);

    p.add(b0);
    p.add(b1);
    f.add(p);


    return f;
}

are called here:

private void init() {

    Display.createDrawFrame(width, height);
    Display.createMenuFrame(width, height);

    this.f = Display.getDrawFrame();
    this.c = Display.getCanvas();
    this.f = HUDDisplay.createHUD(this.f, this);

    MapAssets.init();

}

And are rendered here:

private void render() {

    b = c.getBufferStrategy();
    if(b == null) {
        c.createBufferStrategy(3);
        return;
    }


    do {
        do {

            g = b.getDrawGraphics();

            g.clearRect(0, 0, width, height);
            state.render(g);

            b.show();

        }while(b.contentsLost());

        g.dispose();

    }while(b.contentsRestored());

}

Hopefully I illustrated this problem correctly. To recap, I just want to know why my buttons are not displaying within the JFrame as they should be. Thanks for any help.

Upvotes: 1

Views: 424

Answers (1)

prasad_
prasad_

Reputation: 14287

I think you can use JFrame's add method with BorderLayout to position the canvas and the buttons. I changed your code to show (demonstrate) it can be done. I hope it is what you are looking for.

I added the components (Canvas and Panel with buttons) to the JFrame using code like this to make them visible:

frame.add(panel, BorderLayout.NORTH); // panel with buttons
frame.add(canvas, BorderLayout.CENTER);

Here are links to Oracle's Java tutorials on Swing for further details; see the sections on Swing Components and Laying Out Components Within a Container.

Example code:

import java.awt.*;
import javax.swing.*;

public class FrameLayoutTest {

    private static JFrame f;
    private static Canvas c;

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

    private static void init() {
        //Display.createDrawFrame(width, height);
        createDrawFrame(400, 400);
        //Display.createMenuFrame(width, height);
        createHUD();
        //this.f = Display.getDrawFrame();
        //getDrawFrame();
        //this.c = Display.getCanvas();
        //this.f = createHUD(this.f, this);
        //MapAssets.init();
    }

    public static void createDrawFrame(int width, int height) {
        f = new JFrame();
        c = new Canvas();
        c.setBackground(Color.blue);
        c.setPreferredSize(new Dimension(width, height));
        c.setFocusable(false);
        f.add(c, BorderLayout.CENTER); //f.add(c);
        f.pack();
        f.setVisible(true); // getDrawFrame()
        f.setSize(width, height);
        f.setLocationRelativeTo(null);
    }

    public static void createHUD() {
        Panel p = new Panel();
        p.setLayout(new FlowLayout());
        p.setSize(100, 100);
        p.setLocation(300, 0);
        Button b0 = new Button("Settings");
        Button b1 = new Button("Exit");
        //MenuListeners mListeners = new MenuListeners(game);
        //b0.addActionListener(mListeners);
        //b1.addActionListener(mListeners);
        p.add(b0);
        p.add(b1);
        f.add(p, BorderLayout.NORTH); //f.add(p);
    }
}

Upvotes: 2

Related Questions