views:

712

answers:

4

Hi everyone,

How can I tell the paint method to draw background on JPanel only and not on the entire JFrame. My JFrame size is bigger than the JPanel. When I try to paint a grid background for the JPanel, the grid seems to be painted all over the JFrame instead of just the JPanel.

Here parts of the 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)

}
A: 
super.paintComponents(g);

I would suggest as your first point of investigation.

Pool
thanks,I have tried that, however since my program involved drawing using mouse drag, I need the super.paintComponents(s) to delete the trail of previous drawing when I drag my drawing to a new location.
Jessy
+4  A: 

If you want to do painting on the JPanel then override the JPanel, not the JFrame.

You should be overriding the paintComponent() method of JPanel. Read the section from the Swing tutorial on Custom Painting for a working example.

camickr
Thanks for the link
Jessy
+2  A: 

camickr is correct. 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 your drawing from different components. Swing is already managing subcomponents, so there is absolutely no need to implement drawings in your Panel in the Frame (calling paintComponents() is a severe error). And you should never override paint(), because only paintComponent() is used in Swing. Don't mix both until you absolutely know what you are doing.

Thorsten S.
Thanks Thorsten.
Jessy
A: 

The code you posted is not complete, it's missing how the panel is added to the JFrame and which LayoutManager is being used.
The code seams to be correct. Are you sure the JPanel is not occupying the whole JFrame? Add a System.out.println(drawingPanel.getSize()) to check this.
If you are using the BorderLayout, the default for JFrame, and has just added the panel without any constraint, the panel will use the whole area. The PreferredSize 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 this is not the best or correct way to do it. I would prefer to override the paintComponent() method from the JPanel, as suggested by Thorsten and camickr.
But it will still use the whole area of the JFrame until other Component is added to the JFrame or the LayoutManager changed.

Carlos Heuberger