Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why doesn't my JApplet repaint itself automatically?

Reply
Thread Tools

Why doesn't my JApplet repaint itself automatically?

 
 
Knute Johnson
Guest
Posts: n/a
 
      02-18-2007
Z. wrote:
> Knute Johnson wrote:
>> 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().


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.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
Z
Guest
Posts: n/a
 
      02-18-2007
Z. wrote:
>> 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/tutor.../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?


 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      02-19-2007
Z wrote:
> Z. wrote:
>>> 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/tutor.../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.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery Jeff Kish XML 4 10-30-2008 05:47 PM
Best way to force a JComponent to repaint itself zerg Java 125 09-09-2008 01:14 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why does screen repaint? Karl ASP .Net 6 08-16-2005 12:36 PM
Why does JApplet not need an instance? Jenny Java 2 09-01-2004 05:22 PM



Advertisments