Results 1 to 9 of 9
  1. #1
    flok is offline Member
    Join Date
    Sep 2009
    Location
    Gouda, the Netherlands
    Posts
    24
    Rep Power
    0

    Default update() gets never called

    Hi,

    From what I understand at java.sun.com/products/jfc/tsc/articles/painting/ , repaint() lets awt call update(). But what you can see with the following code, what gets called is paint()! Am I doing something wrong? What?
    Thanks.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class pu extends JFrame
    {
    	public void paint(Graphics g)
    	{
    		System.out.println("paint");
    	}
    
    	public void update(Graphics g)
    	{
    		System.out.println("update");
    	}
    
    	public static void main(String[] arg)
    	{
    		try
    		{
    			/* retrieve max window size */
    			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    			GraphicsDevice[] gs = ge.getScreenDevices();
    			GraphicsConfiguration [] gc = gs[0].getConfigurations();
    			Rectangle r = gc[0].getBounds();
    
    			/* create frame to draw in */
    			pu frame = new pu();
    			frame.setSize(r.width, r.height);
    
    			System.out.println("set visible");
    			frame.setVisible(true);
    
    			for(;;)
    			{
    				System.out.println("Invoke repaint");
    
    				frame.repaint();
    
    				System.out.println("Sleep");
    
    				Thread.sleep(1000);
    			}
    		}
    		catch(Exception e)
    		{
    			System.out.println("exception " + e);
    		}
    	}
    }

  2. #2
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    I think if you want to draw a string on a frame by using paint() method, you must use the drawString() method of class Graphics.
    Write a program to achieve anything you want in your life!

  3. #3
    flok is offline Member
    Join Date
    Sep 2009
    Location
    Gouda, the Netherlands
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Willi View Post
    I think if you want to draw a string on a frame by using paint() method, you must use the drawString() method of class Graphics.
    No. That is also not the point of my question.

    What I mean is: repaint() should let AWT call update() but in reality it calls paint(). Why is that? The System.out.println()'s are only debugging calls to show what is going on and wrong.

  4. #4
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    the method repaint() will call paint() method because you are using a lightweight (Swing) component. Try a heavyweight component (AWT) instead. I think it works!
    Write a program to achieve anything you want in your life!

  5. #5
    flok is offline Member
    Join Date
    Sep 2009
    Location
    Gouda, the Netherlands
    Posts
    24
    Rep Power
    0

    Default [SOLVED] JFrame -> Frame

    Quote Originally Posted by Willi View Post
    the method repaint() will call paint() method because you are using a lightweight (Swing) component. Try a heavyweight component (AWT) instead. I think it works!
    Yes it does! Thanks a lot!

    I replaced JFrame by Frame and now I get updates.

  6. #6
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    You are welcome!

    Anyway, do you have the JDK Documentation with you? If not, download one here. It is the best reference for all programmers.
    Write a program to achieve anything you want in your life!

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

    Default

    But I don't understand why it is important that update gets called here. All update does in swing is call paint(), something which repaint already does (indirectly). I will posit that regardless you are far better off using Swing and its lightweight components, that there is a better way of solving whatever underlying problem you are having that makes you feel impelled to override update().

  8. #8
    flok is offline Member
    Join Date
    Sep 2009
    Location
    Gouda, the Netherlands
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    But I don't understand why it is important that update gets called here. All update does in swing is call paint(), something which repaint already does (indirectly). I will posit that regardless you are far better off using Swing and its lightweight components, that there is a better way of solving whatever underlying problem you are having that makes you feel impelled to override update().
    My paint() function handles redraws invoked by the system.
    Update() does my repaint() calls.
    In update() I only draw a counter, in paint() the whole screen().

    It is for vanheusden.com/java/CoffeeSaint/

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    It appears you don't understand how painting works. Remember, your original question was about JFrame, which indicates you are attempting to create a Swing application.

    In Swing custom painting is done by overriding the paintComponent() method of a component like JPanel.

    When using AWT custom painting was done by overriding paint() and update(). I find people generally use this approach because they found some 10 year old code lying around.

    People generally use Swing over AWT because it is far more flexible, more current and advanced than AWT.

Similar Threads

  1. doPost() is not called in servlet logic
    By bubulina in forum Java Servlet
    Replies: 6
    Last Post: 10-28-2009, 11:19 AM
  2. Replies: 0
    Last Post: 09-22-2009, 08:15 PM
  3. My constructor not called
    By rdtindsm in forum New To Java
    Replies: 2
    Last Post: 09-20-2009, 02:38 AM
  4. Return objects called
    By MV1 in forum New To Java
    Replies: 7
    Last Post: 03-11-2009, 08:16 AM
  5. javascipt function is nt getting called
    By pankaj_salwan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-20-2008, 09:13 AM

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
  •