0

First of all: sorry, if this question was asked before, but I cannot seem to find an answer anywhere, so here we go:

I am trying to get a canvas element to show while it being added to a panel with a titled border around the panel. Here is my code.

public class TestClass extends JFrame{

    private TestClass() {
        GuiCanvas canvas = new GuiCanvas();

        setTitle("TestClass");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1300, 800);

        Border menuBorder = BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.LIGHT_GRAY), "Overview");

        JPanel controlpanel = new JPanel();
        JPanel panelCanvas = new JPanel();

        panelCanvas.setBorder(menuBorder);
        panelCanvas.add(canvas);

        controlpanel.setLayout(new GridLayout(3, 1));
        controlpanel.add(panelCanvas);

        add(controlpanel);
        setLocationRelativeTo(null);
        setVisible(true);

        System.out.println(canvas.getBounds());

    }

    private class GuiCanvas extends Canvas {

        GuiCanvas() {
            setBackground(Color.LIGHT_GRAY);
        }

        @Override
        public void paint(Graphics g) {
            g.drawLine(20, 20, 20, 200);
        }
    }

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

The above code results in an empty panel with a titled border when it should show the defined line I draw in the GuiCanvas-Class. Am I missing something here? Is it even possible to add a canvas-element to a panel? Thanks for your help in advance :)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
D.Path
  • 3
  • 3
  • 1
    Avoid mixing heavyweight and lightweight containers, they don't play well with each other. Unless you're playing around with a `BufferStrategy` there is no need to use `Canvas` – MadProgrammer May 13 '16 at 13:14

2 Answers2

2

If you want the canvas to stretch to the size of the panel, change:

JPanel panelCanvas = new JPanel();

To:

JPanel panelCanvas = new JPanel(new GridLayout());

See also this answer:

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
-1

It is indeed possible to add a Canvas object to a JPanel.

Your problem lies in the fact that your Canvas has no defined size. What you need are the two following lines

    canvas.setPreferredSize(new Dimension(1300,300));
    /*
     *
     */
    this.pack();

This will place your canvas inside the panelCanvas border, displaying a black vertical line on a light gray background.

Wisph
  • 204
  • 2
  • 6
  • Thank you very much for you quick answer, really appreciated.just for me to understand that correctly: every canvas-element needs to have a preferred size to be added to a panel? – D.Path May 13 '16 at 11:03
  • 1
    See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) Layouts are probably the best approach here. – Andrew Thompson May 13 '16 at 13:28