JPanel does not display drawing

I have a problem displaying a picture on a JPanel. I have created three classes that are related to each other as follows. I was wondering why this code is not displaying my drawing.

c.add(pDraw);
pDraw.add(draw);

      

1) BASIC

public class mainPage {
    public static void main(String[]args){
      JFrame appFrame = new Frame();
      appFrame.setVisible(true); 
      appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

      

2) JFRAME

 public class Frame extends JFrame implements ActionListener{

    private drawingBoard draw;  

    public Frame (){
         draw = new drawingBoard(); //generate pattern
         GridBagLayout m = new GridBagLayout();
         Container c = (Container)getContentPane();
         c.setLayout (m);
         GridBagConstraints con;
         .......

         JPanel pDraw = new JPanel();       
         pDraw.setPreferredSize(new Dimension(500,500));
             .....  
         c.add(pDraw);
         pDraw.add(draw); // Call other class for drawing

         .....
         setResizable(false); 
         pack();
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
         setVisible(true);
    }
}

      

3) JPANEL

public class drawingBoard extends JPanel {
      .....
      public void paint(Graphics g) {
      ......
      }
   }

      

+2


source to share


4 answers


Here is the answer, I forgot to set the size of the JPanel's drawing bar :-)



 public class drawingBoard extends JPanel {
        public drawingBoard(){
        setPreferredSize(new Dimension (500,500));

        }
    }

      

+3


source


Its line will change to your code.

All you have to do is read the Custom Painting Swing Tutorial to figure out what you are doing wrong.



This is the third question in a line that could be solved in a few minutes if you took the trouble to read the tutorial.

And you still haven't learned how to post SSCCE , so I'm not going to feed on you.

+1


source


While it's a bit tricky to debug your code without seeing the GridBagConstraints or the paint () method, I suggest you that it is usually better to override the paintComponent () method rather than the paint () method. In drawingBoard try this instead of overriding paint ():

public void paintComponent(Graphics g) {
    super.paintComponent(g); //optional
    ...
}

      

It can make a difference. For more information, check out this article from Java .

Also, as a freebie, you probably don't need to do setVisible () and setDefaultCloseOperation () in both the main () method and the Frame constructor.

0


source


I agree with all the points Ben Torell made in his post, plus some additional troubleshooting tips.

Try it -

public class DrawingBoardTest extends JFrame {
    public DrawingBoardTest() {
        getContentPane().add(new drawingBoard(), BorderLayout.CENTER);
    }
    public static void main(String[] args ) {
        JFrame f = new DrawingBoardTest();
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

      

If the drawing pane is showing, it is a problem with your GridBagLayout, or the preferred size set on the drawingPanel is not large enough to display the drawing, or adding drawBoard to pDraw (which, as far as I can see from the code, is not actually required ...) ...

The default layout for the JFrame content area is BorderLayout, which will give all the space to the center component where I place the drawBoard in the code I posted.

The default JPanel layout is a FlowLayout which will only provide the component it prefers to size. I can see that you are setting the preferred size on pDraw but not on the drawingBoard, in the source code - drawingBoard may have a preferred size that is too small to display the drawing.

If the drawing panel is not showing then it is a problem in your paintPanel paint () method.

0


source







All Articles