Thursday, December 11, 2014

How to Fake Adding Piccolo's PCanvas to a JPanel

One issue with Piccolo is that typically the PCanvas, which is the main canvas used to draw items on the screen must be attached to the top level JFrame. This might be problematic if you want to have your top level JFrame include several Panels. For instance, you might want a JPanel on the left to be a control panel and the PCanvas to be to the right of that. This is like the popular Paint program in Windows.

Here's how to fake it:
  1. Add the PCanvas to the top level JFrame.
  2. Set the Bounds of the PCanvas to shadow that of a JPanel.
private MainFrame() {    
        JButton myButton = new JButton("My Button");
        JPanel panel2 = new JPanel();
        this.add(myButton);
        this.add(panel2);
        this.add(canvas);
        
        this.setLayout(new BorderLayout());
        this.add(myButton, BorderLayout.NORTH);
        this.add(panel2, BorderLayout.CENTER);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        setVisible(true);

        canvas.setBounds(panel2.getX(), panel2.getY(), panel2.getWidth(), panel2.getHeight());
    }


This post was reposted from http://scottizu.wordpress.com/2013/08/20/how-to-fake-adding-piccolos-pcanvas-to-a-jpanel/, originally written on August 20th, 2010.

No comments:

Post a Comment