0

I am reading swing tutorial. From what is mentioned there:

The paintComponent method of JComponent is automatically called 
when a frame is drawn/re-drawn. 

But, what I do not understand is what is the Graphics Object that is passed to it. I do not see any new object of Graphics type being instanstiated and passed. so how does all this happen?

public void paintComponent(Graphics g){
              Graphics2D g2 = (Graphics2D)g;
              g2.drawimage(image,x,y,null);
              //
            }

I guess it is a similar situation with actionlisteners. But in this case, The actionPerformed is called automatically when event occurs such as button click and the events gets passed to the actionPerformed. There is no need to separately call this method and pass the Actionevent object. But I do not understand how it happens with paintComponent method.

button.addActionListener(new ActionListener()
   {
     public void actionPerformed(ActionEvent event){
              //do something
     } 
   });

Thanks

brain storm
  • 30,124
  • 69
  • 225
  • 393
  • 1
    See also [*Painting in AWT and Swing*](http://www.oracle.com/technetwork/java/painting-140037.html). – trashgod Jan 14 '14 at 02:04

2 Answers2

4

The Graphics object is created by the paint sub system.

You should never be calling paintComponent yourself and should simply allow the paint system deal with it. Even if you wanted to capture or print the component using your Graphics context (from something like BufferedImage), you should be using print or printAll

Take a look at Painting in AWT and Swing

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I will read that link, but is that each time `paintComponent` method is called, a new `Graphics` object is created. That would be waste of resources correct? – brain storm Jan 14 '14 at 02:19
  • Generally, you shouldn't care, but in practice, no. I believe the `Graphics` object is changed/created when the window is attached to a native peer. Then, basically, all the components share the same `Graphics` context until the window is closed. This is why you should NEVER call `dispose` on a `Graphcis` context you did not create – MadProgrammer Jan 14 '14 at 02:22
  • may be not very relevant here, but `repaint()` needs to be explicitly called, whereas `paint()` not. If I have, `paintComponent` how does this work? – brain storm Jan 14 '14 at 02:42
  • Calling `repaint` puts a `paint` event onto the Event Queue, which is passed to the `RepaintManager` which makes decisions about what and when something should be painted. `paint` is the action, `repaint` is the request. – MadProgrammer Jan 14 '14 at 02:44
  • so does the `paintComponent` has any relation to them? The `paintComponent` gets called when Frame is drawn/re-drawn. That means I don't have to `repaint` when I use `paintComponent`? – brain storm Jan 14 '14 at 02:50
  • so a `repaint()` has be to called explicitly, if I want my frame to be `redrawn`.? – brain storm Jan 14 '14 at 02:56
  • 1
    Sometimes. Some components are bound, so they provide the container with some idea that something has changed and needs to be revalidated and repainted... – MadProgrammer Jan 14 '14 at 02:57
  • I have a question that is very similar to this here: http://stackoverflow.com/questions/21106626/how-are-the-appropriate-methods-of-mousemotionlistener-in-java-swing – brain storm Jan 14 '14 at 05:44
3

They are similar issues. The Graphics object is created by the Swing library at the request o the JVM and passed into the paintComponent(Graphics g) method when the JVM makes this method call. Because you yourself are not directly calling this method, you never see the object being created.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373