Why doesn't my JApplet repaint itself automatically?


Z

Z

The following simple JApplet doesn't repaint itself when the window is
occluded and then exposed or when it's minimized and then maximized.

I get the same results whether I use appletviewer or my browser.

What am I doing wrong?


JAppletTest.java :

public class JAppletTest extends javax.swing.JApplet
{
public void paint(java.awt.Graphics g)
{
super.paint(g);
g.drawString("Hello world!", 50, 50);
}
}


JAppletTest.html :

<html>
<applet code="JAppletTest" width="200" height="100">
</applet>
</html>
 
Ad

Advertisements

K

Knute Johnson

Z said:
The following simple JApplet doesn't repaint itself when the window is
occluded and then exposed or when it's minimized and then maximized.

I get the same results whether I use appletviewer or my browser.

What am I doing wrong?


JAppletTest.java :

public class JAppletTest extends javax.swing.JApplet
{
public void paint(java.awt.Graphics g)
{
super.paint(g);
g.drawString("Hello world!", 50, 50);
}
}


JAppletTest.html :

<html>
<applet code="JAppletTest" width="200" height="100">
</applet>
</html>

I'm not sure why but take out the super.paint() and it will work fine.
 
A

Andrew Thompson

The following simple JApplet doesn't repaint itself ....

public class JAppletTest extends javax.swing.JApplet
{
public void paint(java.awt.Graphics g)

Swing components should overidr painComponent()


Andrew T.
 
C

Chris Uppal

Andrew said:
Swing components should overidr painComponent()

That is almost as revealing as the recent "Google is my fiend", or "Java is not
a god choice" ;-)

-- chris
 
A

Andrew Thompson

...
There is no paintComponent() for JApplet.

Huh! Good point.

I am so used to putting everything
in a JPanel (even animated rendering)
before it goes into a ..
JApplet/Frame/Dialog/OptionPane/Internal..
...you get the picture, that I never
noticed that!

( I should have checked the JDocs,
before I opened my big mouth ;)

Andrew T.
 
Ad

Advertisements

Z

Z.

Knute said:
I'm not sure why but take out the super.paint() and it will work fine.

Hmm. Very odd.

My text says :

"This statement [super.paint(g);] should be the first statement in every
applet's paint method. Omitting it can cause subtle drawing errors in
applets that combine drawing and GUI components."

I must be making some fundamental mistake here. The JApplet and Applet
docs at sun.com have offered no help or hints.

Does anyone have any other ideas or suggestions?
 
K

Knute Johnson

Z. said:
Knute said:
I'm not sure why but take out the super.paint() and it will work fine.

Hmm. Very odd.

My text says :

"This statement [super.paint(g);] should be the first statement in every
applet's paint method. Omitting it can cause subtle drawing errors in
applets that combine drawing and GUI components."

I must be making some fundamental mistake here. The JApplet and Applet
docs at sun.com have offered no help or hints.

Does anyone have any other ideas or suggestions?

The problem is that paint() only gets called once on a JApplet. I don't
think it was ever intended that you draw on the JApplet and have
components on it at the same time. If you want to do both, use a plain
Applet. It will work just fine.
 
Z

Z.

Knute said:
The problem is that paint() only gets called once on a JApplet. I don't
think it was ever intended that you draw on the JApplet and have
components on it at the same time. If you want to do both, use a plain
Applet. It will work just fine.

Neing new to Java, I want to make sure I understand ... on repaint, the
superclass (applet's) paint() gets called, not the JApplet's paint().

Is that right?

That would explain my problem.

Do you have a URL or some other reference that explains what method gets
called to repaint a JApplet?
 
A

Andrew Thompson

That is almost as revealing as the recent "Google is my fiend", or "Java is not
a god choice" ;-)

LOL! I'll upgrade my earlier
'should have checked the JDocs' to
'should have *copy/pasted* from the
JDocs'!

Andrew T.
 
K

Knute Johnson

Z. said:
Neing new to Java, I want to make sure I understand ... on repaint, the
superclass (applet's) paint() gets called, not the JApplet's paint().

I don't know if that is true. I don't think it is.
Is that right?

That would explain my problem.

Do you have a URL or some other reference that explains what method gets
called to repaint a JApplet?

No I don't. I can tell you though that mixing painting and components
on a JApplet will be problematic. If you want to draw on something just
put a JPanel in to draw on. I believe that the JApplet is just there to
have a lightweight container so that you can use swing components in an
applet.
 
Ad

Advertisements

Z

Z

Being new to Java, I want to make sure I understand ... on repaint, the
superclass (applet's) paint() gets called, not the JApplet's paint().
Is that right?
That would explain my problem.
Do you have a URL or some other reference that explains what method gets
called to repaint a JApplet?

http://java.sun.com/docs/books/tutorial/uiswing/painting/problems.html

Ok, I think I've got it now... the "Painting Problems" page reads:


Problem: The background of my applet shows up, but the foreground stuff
doesn't show up.

Did you make the mistake of performing painting directly in a JApplet
subclass? If so, then your contents will be covered by the content pane
that is automatically created for every JApplet instance. Instead,
create another class that performs the painting and then add that class
to the JApplet's content pane.

So ...

1. I removed the paint() method from my JApplet class

2. I created a new class (Repainter), a subclass of JComponent, with a
paintComponent() method and put my g.drawString() calls in that method

3. in my JApplet's init(), I instantiated a Repainter and used a call to
this.getContentPane().add() to add the Repainter to my JApplet's Content
Pane


It seems to be repainting correctly now, under all circumstances.

Did I do this correctly?
 
Ad

Advertisements

K

Knute Johnson

Z said:
http://java.sun.com/docs/books/tutorial/uiswing/painting/problems.html

Ok, I think I've got it now... the "Painting Problems" page reads:


Problem: The background of my applet shows up, but the foreground stuff
doesn't show up.

Did you make the mistake of performing painting directly in a JApplet
subclass? If so, then your contents will be covered by the content pane
that is automatically created for every JApplet instance. Instead,
create another class that performs the painting and then add that class
to the JApplet's content pane.

So ...

1. I removed the paint() method from my JApplet class

2. I created a new class (Repainter), a subclass of JComponent, with a
paintComponent() method and put my g.drawString() calls in that method

3. in my JApplet's init(), I instantiated a Repainter and used a call to
this.getContentPane().add() to add the Repainter to my JApplet's Content
Pane


It seems to be repainting correctly now, under all circumstances.

Did I do this correctly?

Sounds good to me. I normally just use a JPanel because the constructor
has parameters for LayoutManager and double buffering but to each his own.
 

Top