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

    Default Trouble understanding how paint is called...?

    hey guys, I've been looking through tutorials, but I really can't find the answer to this... I know that I am calling the constructor, but don't understand how the "paint" method is called. Could someone help me out on this? Thanks! heres the code:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    class drawin extends JFrame {
    drawin() {
    super("My Prog!");
    public void paint(Graphics mw) {
    mw.setFont(new Font("Verdana", Font.BOLD, 16));
    mw.drawString("Hey, this is my first program!", 30, 50);
    mw.drawString("……………." , 30, 70);
    mw.drawString("Goodbye!", 30, 90);
    public static void main(String[] args) {
    new drawin();

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Trouble understanding how paint is called...?

    As per this tutorial that I linked to you earlier:
    Regardless of how a paint request is triggered, the AWT uses a "callback" mechanism for painting, and this mechanism is the same for both heavyweight and lightweight components. This means that a program should place the component's rendering code inside a particular overridden method, and the toolkit will invoke this method when it's time to paint.
    So the toolkit, meaning AWT (which stands for Abstract Windowing Toolkit) itself, will call the paint(...) method when it is time to paint.

    If your application things that it needs to be repainted, say you've written an animation program and have changed the x and y position of some sprite, then the tutorial suggests:
    The program determines that either part or all of a component needs to be repainted in response to some internal state change.
    The program invokes repaint() on the component, which registers an asynchronous request to the AWT that this component needs to be repainted.
    In other words and again, you never call the paint method directly but rather call repaint() which tells the AWT that you want paint to be called, and AWT will ultimately call paint(...) if there are no contraindications (such as "stacking" of repaint() calls, meaning a bunch of repaint() calls have been made in a row and paint can not keep up with the calls).
    Please re-read the tutorial as it explains it well. But first, please re-read the basic Swing graphics tutorial again, because I see that you're still overriding a JFrame's paint method, and this will (again) tell you why you shouldn't do this and what you should do instead.

Similar Threads

  1. Replies: 5
    Last Post: 07-24-2012, 09:35 AM
  2. Help - Yet Another Paint() Called Twice thread
    By devdon in forum AWT / Swing
    Replies: 13
    Last Post: 04-11-2012, 01:30 PM
  3. Replies: 2
    Last Post: 08-23-2011, 11:04 PM
  4. Replies: 5
    Last Post: 10-22-2010, 01:55 PM
  5. Why the paint() method is called two times ?
    By supremo in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 06:21 PM

Tags for this Thread

Posting Permissions

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