1

I have a MyFrame class which extends JFrame . I added components(Controls i.e buttons) to this frame using the design options in NET BEANS. I have a MyCanvas class which extends JPanel with an overridden paintComponent() method . I am trying to add this component to MyFrame class. But when i add it and make it visible the canvas(JPanel) doesn't show itself on the JFrame. (First I was trying to add Mycanvas class extended by Canvas. But then i read a thread here to try and change it to JPanel. I didn't work either. And for canvas i obviously use paint not paintcomponent()) My code is here below

MyCanvas Class

 public class MyCanvas extends javax.swing.JPanel{

 MyCanvas()
 {
      super();
 }

 @Override
 public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D graphics2D=(Graphics2D)g;

    graphics2D.drawRect(10, 10, 10, 10);

    graphics2D.drawOval(0, 0, 100, 100);
}

}

MyFrame

public class Myframe extends javax.swing.JFrame {

public Myframe() {

    initComponents();
  @SuppressWarnings("unchecked")
  +generatedcode by the designer

 private void RectangleActionPerformed(java.awt.event.ActionEvent evt) {
}

private void SquareActionPerformed(java.awt.event.ActionEvent evt) {
}

private void CircleActionPerformed(java.awt.event.ActionEvent evt) {
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
 java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Myframe().setVisible(true);
        }
    });
}
public void run() {
        new Myframe().setVisible(true);
    }
// Variables declaration - do not modify
private javax.swing.JButton Circle;
private javax.swing.JButton Rectangle;
private javax.swing.JButton Square;
// End of variables declaration
}

My Main Program

public static void main(String[] args) {

    MyCanvas c = new MyCanvas();
    Myframe f= new Myframe();//Works if used JFrame instead of MyFrame

    f.add(c);
    f.setVisible(true);
}

Basically i want to create a GUI in which i want Buttons which can trigger events and change what is drawn on a canvas. I tried it with an empty jframe. added the canvas/panel to the JFrame . It worked. Also I changed my Panel/Canvas and refresehed the frame. That also worked fine. But i am unable to do it like this.

Muhammad Taha
  • 15
  • 1
  • 6

3 Answers3

2

The is that you are mixing creating JFrame with IDE and creating JPanel yourself, remember the IDE adds all components to JFrame in initComponents() which is ideally where you'd want to have your Canvas added.

either create the JFrame and JPanel by yourself (without use of Netbeans GUI Builder)

or

You can use the Palette Manager of Netbeans to add your Component to the palette, then you can use it in the GUI builder as you would any other class:

(Simply drag the Canvas class from the projects tree on to the JFrame in the GUI designer)

To make sure your custom Canvas functions:

  • override getPrefferedSize(..) and return a size which fits

Reference:

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • Okay so now i have edited according to you instructions and my canvas shows itself . But as the initcomponents method has been removed it does not show the buttons that i added. I want the buttons and the canvas should show itself .Was this the expected result .Any way to do it according to my requirements? – Muhammad Taha Oct 26 '12 at 19:04
  • My buttons should be as I have placed them in the designer. The canvas should at least show itself along with the button.(I didnt go as far considering the position of the canvas). Where should i call the initComponents methods.?I should get some positive result that canvas and button both are being shown.If you can please guide me how i can set the canvas postion. – Muhammad Taha Oct 26 '12 at 19:08
  • 1
    That is what I am trying to do . I will study thoroughly it and try to implement. Thankyou @David Kroukamp – Muhammad Taha Oct 26 '12 at 19:24
0

Try changing it to:

public static void main(String[] args) {

    MyCanvas c = new MyCanvas();
    Myframe f= new Myframe();//Works if used JFrame instead of MyFrame

    f.add(c);
    c.setVisible(true);
    f.setVisible(true);
}

Also, the code in your MyFrame class:

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
 java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Myframe().setVisible(true);
        }
    });
}

won't be executed unless you are running MyFrame instead of your program main.

Also check that you have a super() call in your MyFrame constructor.

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
0
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(Color.BLACK);

    Graphics2D graphics2D=(Graphics2D)g;

    graphics2D.drawRect(10, 10, 10, 10);

    graphics2D.drawOval(0, 0, 100, 100);
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
JCN
  • 11
  • 2