JFrame paint

T

Thomas Richter

Hi folks,

according to the Java API docs, a component should call paint() whenever
it needs refresh, and a swing component should call paintComponent() for
this purpose. I now need to render a tiny graphic (manually) into a
JFrame and to this end overrode paint() in JFrame. Interestingly, this
does not work correctly, the JFrame doesn't seem to call this custom
hook correctly. Sometimes my graphics is redrawn, sometimes it isn't.
The same effect happens when overriding paintComponent(): Doesn't work
reliable.

It *does* work reliable if I install a JPanel or a Canvas into the
content pane of the JFrame and override paint() there. (Thus, problem
solved on my side so far)

However, concerning the JFrame paint() method, am I missing something or
is this a "feature" (not to call it "bug")?

So long,
Thomas
 
S

SadRed

Hi folks,

according to the Java API docs, a component should call paint() whenever
it needs refresh, and a swing component should call paintComponent() for
this purpose. I now need to render a tiny graphic (manually) into a
JFrame and to this end overrode paint() in JFrame. Interestingly, this
does not work correctly, the JFrame doesn't seem to call this custom
hook correctly. Sometimes my graphics is redrawn, sometimes it isn't.
The same effect happens when overriding paintComponent(): Doesn't work
reliable.

It *does* work reliable if I install a JPanel or a Canvas into the
content pane of the JFrame and override paint() there. (Thus, problem
solved on my side so far)

However, concerning the JFrame paint() method, am I missing something or
is this a "feature" (not to call it "bug")?

So long,
Thomas

Do like this:
myJframe.getContentPane().add(new DrawPanel(), BorderLayout.center);
--------
class DrawPanel extends JPanel{
...
public void paintComponent(Graphics g){
...
// you do drawing here
}
...
}
 
T

Thomas Richter

SadRed said:
Do like this:
myJframe.getContentPane().add(new DrawPanel(), BorderLayout.center);
--------
class DrawPanel extends JPanel{
...
public void paintComponent(Graphics g){
...
// you do drawing here
}
...
}

Yes, thanks, I know that this works. The question is rather, why doesn't
JFrame.paint() work? Is this a *bug* or am I missing something?

So long,
Thomas
 
L

Lew

Thomas said:
Yes, thanks, I know that this works. The question is rather, why doesn't
JFrame.paint() work? Is this a *bug* or am I missing something?

It might be a bug, but likely not in Swing.

Have you checked the Java bugbase?

According to the Javadocs, the main purpose of JFrame's paint() is to forward
paint() to contained children. Does your override call super.paint(g)?

The usual approach is to put "tiny graphics" in subcomponents, not directly in
the JFrame.
 
T

Thomas Richter

Lew said:
It might be a bug, but likely not in Swing.

Have you checked the Java bugbase?

According to the Javadocs, the main purpose of JFrame's paint() is to
forward paint() to contained children. Does your override call
super.paint(g)?

Yup, it does. Of course *before* drawing my graphics into the frame.
The usual approach is to put "tiny graphics" in subcomponents, not
directly in the JFrame.

Clearly. It just was the simplest way for this specific problem at hand...

So long,
Thomas
 
K

Knute Johnson

Thomas said:
Yup, it does. Of course *before* drawing my graphics into the frame.


Clearly. It just was the simplest way for this specific problem at hand...

So long,
Thomas

A JFrame is not a subclass of JComponent. It doesn't have a
paintComponent() method to override. You and the others figured out the
solution.

knute...
 
R

Roedy Green

according to the Java API docs, a component should call paint() whenever
it needs refresh, and a swing component should call paintComponent() for

not quite. You need to compose a paintComponent method to override
the usual one. However you don't call it directly. Normally it gets
called as a side effect of a validate, setVisible etc etc. However,
if it needs a manual prod because data on which the rendering is based
have changed, you can call repaint to enqueue an event to get your
paintComponent called.

See http://mindprod.com/jgloss/repaint.html
http://mindprod.com/jgloss/paintcomponent.html
 

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

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top