Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Thread in applet using Graphics object

5 views
Skip to first unread message

Chris

unread,
Aug 28, 2001, 4:28:52 PM8/28/01
to
Hi all,
Having a bit of a problem with a graphics thread in an applet.
Hoping that it's something stupid, and one of you might know.

I've put a thread in my applet that does something interesting while the
aplet is displayed. All works correctly until I try to use the applet's
Graphics object. E.g. when my thread tries g.drawString I get:

java.lang.NullPointerException: arguments || graphics

at sun.awt.windows.WGraphics.devFillPolygon(Native Method)

at sun.awt.windows.WGraphics.fillPolys(WGraphics.java:695)

at
sun.java2d.pipe.ShapeToPolyConverter.sendPoly(ShapeToPolyConverter.java:79)

at
sun.java2d.pipe.ShapeToPolyConverter.doDraw(ShapeToPolyConverter.java:129)

at
sun.java2d.pipe.ShapeToPolyConverter.fill(ShapeToPolyConverter.java:48)

at
sun.java2d.loops.RasterOutputManager.drawTextOutline(RasterOutputManager.java:2653)

at
sun.java2d.loops.RasterOutputManager.drawString(RasterOutputManager.java:2890)

at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2245)

at app2d$1.run(app2d.java:61)

at java.lang.Thread.run(Thread.java:484)

I've created the thread in the paint() method because then the Graphics
object is available. Didn't see a way to put this in init().
The code is basically like this:

public void paint (final Graphics g) {
if ( th == null ) { // First time only, create and
start thread
th = new Thread(new Runnable () {
public void run () {
while (true) {
g.drawString("Count:"+counter++, 10, 10);
}
}
});
th.start();
}
}

Why does the drawString() not work ? Am I overlooking something ?
TIA for any ideas.

Chris

Paul Keeble

unread,
Aug 28, 2001, 5:07:47 PM8/28/01
to
> public void paint (final Graphics g) {
> if ( th == null ) { // First time only, create and
> start thread
> th = new Thread(new Runnable () {
> public void run () {
> while (true) {
> g.drawString("Count:"+counter++, 10, 10);
> }
> }
> });
> th.start();
> }
> }
>
> Why does the drawString() not work ? Am I overlooking something ?
> TIA for any ideas.
>
> Chris

When I look at this code I consider it quite strange. To be honest I'm not sure
you can do what your doing at all.

A way to fix it is to make your applet implement Runnable and then run the
thread on "this" where the run method contains a call to paint and have the
contents of the paint method as g.drawString("Count:"+counter++, 10, 10);

Then when you first run it you create the thread and the first thread does the
counter and then from then on the second thread does the job and its entirely
contained within the same class.

But like I say I've got no real reason why this doesn't work properly, or fails
in the way you describe but I'm sure that not calling update to clear the screen
or using any buffering is a bad plan anyhow.


Lee Fesperman

unread,
Aug 28, 2001, 5:15:09 PM8/28/01
to
Chris wrote:
>
> Hi all,
> Having a bit of a problem with a graphics thread in an applet.
> Hoping that it's something stupid, and one of you might know.
>
> I've created the thread in the paint() method because then the Graphics
> object is available. Didn't see a way to put this in init().
> The code is basically like this:
>
> public void paint (final Graphics g) {
> if ( th == null ) { // First time only, create and
> start thread
> th = new Thread(new Runnable () {
> public void run () {
> while (true) {
> g.drawString("Count:"+counter++, 10, 10);
> }
> }
> });
> th.start();
> }
> }
>
> Why does the drawString() not work ? Am I overlooking something ?
> TIA for any ideas.

The Graphics object passed to paint() is only valid until paint() returns. By passing it
to another thread, you end up using it after paint() has returned. At that point (in so
many words), the Graphics object is no longer valid.

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
===================================================================
* Check out Database Debunkings (http://www.firstsql.com/dbdebunk/)
* "Where Persistent Prevailing Database Fallacies Are Dispelled"

Chris Breemer

unread,
Aug 29, 2001, 6:53:07 AM8/29/01
to

"Paul Keeble" <P.Ke...@ntlworld.com> wrote in message
news:1JTi7.74366$Qh2.6...@news11-gui.server.ntli.net...

> When I look at this code I consider it quite strange. To be honest I'm not
sure
> you can do what your doing at all.
>

Yes, it's weird isn't it. Real newbie stuff. Dunno what came over me ;-)

> A way to fix it is to make your applet implement Runnable and then run the
> thread on "this" where the run method contains a call to paint and have
the
> contents of the paint method as g.drawString("Count:"+counter++, 10, 10);
>

Indeed that's what I should have done. And it works, too. repaint() is the
key.

> Then when you first run it you create the thread and the first thread
does the
> counter and then from then on the second thread does the job and its
entirely
> contained within the same class.
>
> But like I say I've got no real reason why this doesn't work properly, or
fails
> in the way you describe but I'm sure that not calling update to clear the
screen
> or using any buffering is a bad plan anyhow.
>

Probably it doesn't work because the Graphics context is only valid within
the
paint() method, as suggested by Lee. A moot point now, as I've seen the
light ;-)

Thanks for helping me out.
Cheers,

Chris
>


0 new messages