Paint Background JPanel

How can I say that the drawing method draws the background only on the JPanel, and not on the entire JFrame. My JFrame is bigger than JPanel. When I try to draw a grid background for JPanel, the grid seems to be drawn all over the JFrame, not just the JPanel.

Here is the piece of code:

public class Drawing extends JFrame { JPanel drawingPanel; ........... public Drawing (){ drawingPanel = new JPanel(); drawingPanel.setPreferredSize(new Dimension(600,600)); } public void paint(Graphics g) { super.paintComponents(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); paintBackground(g2); //call a METHOD to paint the for JPANEL } private void paintBackground(Graphics2D g2) { g2.setPaint(Color.GRAY); for (int i = 0; i < drawingPanel.getSize().width; i += 300) { Shape line = new Line2D.Float(i, 0, i, drawingPanel.getSize().height); g2.draw(line); } for (int i = 0; i < drawingPanel.getSize().height; i += 300) { Shape line = new Line2D.Float(0, i, drawingPanel.getSize().width, i); g2.draw(line); } } //END private void paintBackground(Graphics2D g2) } 
+4
source share
5 answers

camickr is true. So:

 public class Drawing extends JFrame { JPanel drawingPanel; ........... public Drawing (){ drawingPanel = new MyPanel(); drawingPanel.setPreferredSize(new Dimension(600,600)); add(drawingPanel); } } public class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); myBackgroundRoutine(g2); } } 

You need to strictly separate the drawing from the different components. Swing is already managing subcomponents, so there is absolutely no need to implement drawings in your Panel in a frame (calling paintComponents () is a serious mistake). And you should never override paint (), because only paintComponent () is used in Swing. Do not mix them until you know what you are doing.

+3
source

If you want to paint on a JPanel, then override the JPanel, not the JFrame.

You must override the paintComponent () JPanel method. Read the section in the Swing tutorial on Custom Painting for a working example.

+5
source
 super.paintComponents(g); 

I would suggest as your first point of inquiry.

0
source

The code you sent is not complete, it lacks how the panel is added to the JFrame and which LayoutManager is used.
Code seams must be correct. Are you sure JPanel does not take up the whole JFrame? Add System.out.println(drawingPanel.getSize()) to verify this. If you use BorderLayout, by default for JFrame and just added a panel without any restrictions, the panel will use the entire area. Preferred size is ignored.
Try this, just for testing:

 public Drawing (){ drawingPanel = new JPanel(); drawingPanel.setPreferredSize(new Dimension(600,600)); // ignored drawingPanel.setBounds(0, 0, 600, 600); // location and size setLayout(null); add(drawingPanel); } 

but IMO is not the best or right way to do it. I would rather override the paintComponent() method from JPanel, as suggested by Torsten and Camicre.
But it will still use the entire JFrame area until another component is added to the JFrame or the LayoutManager is modified.

0
source

You should override JPanel, not JFrame for drawing. You can override the paintComponent () JPanel method

0
source

All Articles