luluValley
luluValley

Reputation: 21

How to draw on subPanels created by .form?

I would like to draw on sub-panels, which are created by the .form. There is one "mainPanel", which contains three subPanels named panel1(top), panel2(bottom-left) and panel3(bottom-right).

Now I would like to draw something on the subPanel "panel1", but it does not work. When I run the program, it only shows the three subPanels there, but without my drawings of the "paintComponent()" method.

I pasted my code here, could anyone help me to check what is the problem? Thanks a lot.

public class PanelDrawTest extends JFrame {
    private JPanel mainPanel;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;

    public PanelDrawTest(){
       getContentPane().add(mainPanel);
       setPanel1(new MyPanel1());
    }

    public JPanel getMainPanel() {
       return mainPanel;
    }

    public JPanel getPanel1() {
       return panel1;
    }

    public void setPanel1(JPanel panel1) {
       this.panel1 = panel1;

    }

    private class MyPanel1 extends JPanel {

       public MyPanel1(){

       }
       @Override
       public void paintComponent(Graphics g){
           g.drawString("This is Panel 1",20,20);
           g.drawRect(0,200,100,200);

       }
    }

    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable() {
           @Override
           public void run() {
               createAndShowGUI();
           }
        });
    }

    private static void createAndShowGUI() {
        PanelDrawTest frame = new PanelDrawTest();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(50, 50, 1200, 700);
        frame.setPreferredSize(new Dimension(1200,800));
        frame.pack();
        frame.setVisible(true);
    }

 }

Upvotes: 1

Views: 182

Answers (2)

MyPasswordIsLasercats
MyPasswordIsLasercats

Reputation: 1630

Your version wasn't runnable for me, but this works:

public class PanelDrawTest extends JFrame {
  private JPanel mainPanel = new JPanel(); // I added a constructor to avoid NullPointerexception
  private JPanel panel1;
  private JPanel panel2;
  private JPanel panel3;

  public PanelDrawTest(){
    getContentPane().add(mainPanel);
    setPanel1(new MyPanel1());
    add(panel1); // and I added the panel
  }

  public JPanel getMainPanel() {
     return mainPanel;
  }

  public JPanel getPanel1() {
    return panel1;
  }

  public void setPanel1(JPanel panel1) {
    this.panel1 = panel1;

  }

  private class MyPanel1 extends JPanel {

    public MyPanel1(){

    }
    @Override
    public void paintComponent(Graphics g){
      g.drawString("This is Panel 1",20,20);
      g.drawRect(0,200,100,200);

    }
  }

  public static void main(String args[]){
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        createAndShowGUI();
      }
    });
  }

  private static void createAndShowGUI() {
    PanelDrawTest frame = new PanelDrawTest();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(50, 50, 1200, 700);
    frame.setPreferredSize(new Dimension(1200,800));
    frame.pack();
    frame.setVisible(true);
  }
}

Upvotes: 1

Thorn
Thorn

Reputation: 4057

Note: The code posted throws a NullPointerException because mainPanel was never initialized.

The constructor is not adding your custom panel to the layout and this is why it cannot be seen. Try this and you'll see your drawing:

public PanelDrawTest() {
   mainPanel = new JPanel();
   getContentPane().add(mainPanel,BorderLayout.NORTH);
   panel1 = new MyPanel1();
   getContentPane().add(panel1,BorderLayout.CENTER);      
}

Upvotes: 1

Related Questions