0

I've seen a lot of people saying in order to draw multiple objects to the screen at once and overriding the EDT, you need something similar to below:

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);    // This will clear everything...

My question is, how do you call this method? and where do you get the Graphics for it's parameter?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ben Crazed Up Euden
  • 353
  • 2
  • 7
  • 18
  • You don't call it. Java calls it (from the EDT) when it decides something needs to be painted. – John3136 Nov 27 '13 at 01:50
  • 1
    AWT has no `paintComponent` method. You are thinking of Swing (not the top level containers, but extensions of `JComponent`). – Andrew Thompson Nov 27 '13 at 01:50
  • Read the section from the Swing tutorial on [Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for the basics of painting. – camickr Nov 27 '13 at 02:22

1 Answers1

1

how do you call this method? and where do you get the Graphics for it's parameter?

Call the component's repaint() method. It will call paintComponent() (as soon as the event dispatch thread is not busy). Actually you only need to do this call manually when conditions on which the painting depends change. Otherwise, your paint methods will be called automatically whenever the relevant part of the window needs painting, and that's all you need.

Boann
  • 48,794
  • 16
  • 117
  • 146