When is the paint()-method called?

J

Jesper Sahner

Hi!

A beginner's question: When is the paint()-method EXACTLY called in
the following little example?

import java.awt.*;

public class GWindow extends Frame
{
public void paint(Graphics g)
{
g.drawLine(0,0,50,50);
g.fillOval(5,20,300,30);
g.setColor(Color.green);
g.drawString("Hello",100,40);
}
}

public class ShowGWindow
{
public static void main(String[] arg)
{
GWindow w = new GWindow();
w.setSize(350,60);
w.setTitle("GWindow");
w.setVisible(true);
}
}

Regards,
Jesper
 
P

Paul H. van Rossem

Hi!

A beginner's question: When is the paint()-method EXACTLY called in
the following little example?

import java.awt.*;

public class GWindow extends Frame
{
public void paint(Graphics g)
{
g.drawLine(0,0,50,50);
g.fillOval(5,20,300,30);
g.setColor(Color.green);
g.drawString("Hello",100,40);
}
}

public class ShowGWindow
{
public static void main(String[] arg)
{
GWindow w = new GWindow();
w.setSize(350,60);
w.setTitle("GWindow");
w.setVisible(true);
}
}

Regards,
Jesper
after setVisible().

You can find out these things by yourself as follows:

public static void main(String[] arg)
{
GWindow w = new GWindow();
System.out.println("calling setSize");
w.setSize(350,60);
System.out.println("calling setTitle");
w.setTitle("GWindow");
System.out.println("calling setVisible");
w.setVisible(true);
System.out.println("main done");
}

public void paint(Graphics g)
{
System.out.println("paint called");
super.paint(g);
g.drawLine(0,0,50,50);
...
}

PS don't forget to call super.paint()

Success, Paul.
 
T

Thomas Weidenfeller

Jesper said:
Hi!

A beginner's question:

comp.lang.java.help is next door.

When is the paint()-method EXACTLY called in
the following little example?

Short answer: Whenever it is needed.

Long answer: Indeed whenever it is needed. Which means, it can, and
usually will, be called multiple times. Usually as an reaction (via some
intermediate steps) to an internal or external event. E.g. like when the
window becomes visible for the first time, or when parts of the window
have been obscured by another window, and these parts now become visible
again.

If you count on paint() being called only once, or paint() being called
only at a particular time then you are about to add a few bugs to your
application. It is the GUI main loop (event loop) which decides when to
call what, and you have little or no control over it.

/Thomas
 
B

bugbear

Example snipped.
You can find out these things by yourself as follows:

reverse engineering technique snipped.

Attempting to "find out" how a complex system works by
example based reverse engineering is poor development technique.

paint() is called in extremely well documented
circumstances; it's dramatically better to look up this
documentation than reverse engineer. Reverse enghineering
done in a half-arsed way may not reveal all possible circumstances
and permutations. Thorough reverse engineering may reveal
details of the implementation that may be subject to change.

Oh, and finally, there's a dedicated comp.lang.java.gui

BugBear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top