Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Why doesn't my JApplet repaint itself automatically?

Thread view: 
Z - 16 Feb 2007 06:14 GMT
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>
Knute Johnson - 16 Feb 2007 07:40 GMT
> The following simple JApplet doesn't repaint itself when the window is
> occluded and then exposed or when it's minimized and then maximized.
[quoted text clipped - 20 lines]
> </applet>
> </html>

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

Signature

Knute Johnson
email s/nospam/knute/

Z. - 18 Feb 2007 03:09 GMT
> 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?

Signature

Real Estate in '07: "It's dead, Jim."

Knute Johnson - 18 Feb 2007 06:17 GMT
>> I'm not sure why but take out the super.paint() and it will work fine.
>
[quoted text clipped - 10 lines]
>
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Z. - 18 Feb 2007 19:19 GMT
> 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?

Signature

Real Estate in '07: "It's dead, Jim."

Knute Johnson - 18 Feb 2007 21:00 GMT
>> 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
[quoted text clipped - 3 lines]
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Z - 18 Feb 2007 22:44 GMT
>> 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.

> 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?
Knute Johnson - 19 Feb 2007 00:00 GMT
>>> 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
[quoted text clipped - 35 lines]
>
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Andrew Thompson - 16 Feb 2007 09:05 GMT
> 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.
Knute Johnson - 16 Feb 2007 17:50 GMT
>> The following simple JApplet doesn't repaint itself
> ...
[quoted text clipped - 5 lines]
>
> Andrew T.

There is no paintComponent() for JApplet.

Signature

Knute Johnson
email s/nospam/knute/

Andrew Thompson - 16 Feb 2007 18:42 GMT
On Feb 17, 4:50 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> >> The following simple JApplet doesn't repaint itself
> > ...
[quoted text clipped - 3 lines]
>
> > Swing components should overidr painComponent()
..
> 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.
Chris Uppal - 16 Feb 2007 18:22 GMT
> 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
Andrew Thompson - 18 Feb 2007 20:32 GMT
On Feb 17, 5:22 am, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
THIS.org> wrote:
> > Swing components should overidr painComponent()
>
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.