Results 1 to 5 of 5
  1. #1
    lightstream is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default paint vs paintComponent for a JPanel

    Hi. In my Java applet, I have a class which extends JPanel in order to display 2D graphics.

    In this class I can override paint(), or paintComponent(), and both appear to work exactly the same. Obviously there is some difference, but I don't know what it is -- can anyone help with a brief explanation?

    thanks in advance!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    Look up the paint method in the JComponent class api and read the description of it in the Method Detail.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default

    Also, read the Sun Swing tutorials section on Swing graphics for more in depth coverage of this very question. Look at especially this page: A Closer Look at the Paint Mechanism (The Java™ Tutorials > Creating a GUI with JFC/Swing > Performing Custom Painting)

    Once you'll read this, you'll know exactly why you should use paintComponent. Please let me know if it answers your question adequately or not. Good luck.

  4. #4
    lightstream is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    thanks for the pointer hardwired, and the link Fubarable - both very helpful indeed - one more murky corner of Java UI stuff is enlightened :)

    (They also provided useful explanation for why you should call super.paintComponent(g))

    thanks guys!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

Similar Threads

  1. help?! paint on top of swing components on JPanel
    By beam2008 in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2008, 05:15 AM
  2. Working around paintcomponent
    By sahhhm in forum New To Java
    Replies: 2
    Last Post: 05-16-2008, 02:43 AM
  3. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 05:59 PM
  4. Drawing outside paintComponent()
    By DarkSide1 in forum Java 2D
    Replies: 2
    Last Post: 11-08-2007, 11:36 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 03:43 AM

Posting Permissions

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