Results 1 to 3 of 3
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Why doesn't the paintComponent method need to be called?

    So, two questions about the paintComponent:
    First of all, why does it take an object
    Java Code:
    Graphics g
    as its parameter? What does that mean? It hurts my brain and I have never had anyone explain why. Now, the big question is: when you write in the method, you could do things like
    Java Code:
    g.drawRectangle(blah,blah)
    and
    Java Code:
    g.drawString(blah,blah)
    I understand that you are calling these methods within the method that you are overriding, the paintComponent. Now, what I don't understand, is why you can do all of this and then all of it will just be painted, without you ever having to do paintComponent(); aka calling the paintComponent method. Why does it just do all of the things you ask without being called? To my understanding of methods, you write them and then call them. Here we are just overriding the paintComponent method yet it is not being called :( , and it is really annoying to me (I like to understand all my Java code)

    I know it's a hard topic to explain. Thanks to all of those who try!

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Why doesn't the paintComponent method need to be called?

    First, the paintComponent() method takes a Graphics g object in its constructor because it needs to use the Graphics class in order to draw things... the methods inside allow it to draw.

    Secondly, the paintComponent() method is called when you first start up your program and the window appears. You can also recall it to update using repaint();

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Why doesn't the paintComponent method need to be called?

    Long story short, the paintComponent() method is called automatically by Java. You can also call repaint() to tell Java to call it again. Java takes care of creating the instance of Graphics that gets passed into the paintComponent() method.

    Think about the main() method. You don't have to call that, do you?

    Recommended reading: A Closer Look at the Paint Mechanism (The Java™ Tutorials > Creating a GUI With JFC/Swing > Performing Custom Painting)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2013, 04:09 AM
  2. Replies: 11
    Last Post: 04-26-2012, 10:46 PM
  3. paintComponent() Method straitjacket
    By oldalistair in forum New To Java
    Replies: 5
    Last Post: 09-11-2010, 12:06 AM
  4. Replies: 10
    Last Post: 02-28-2010, 01:52 PM
  5. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 10:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •