[4389] in java-interest

home help back first fref pref prev next nref lref last post

Re: NullPointerException from drawLine() inside a thread in paint() ??

daemon@ATHENA.MIT.EDU (Pradeep Badri)
Thu Dec 21 15:40:30 1995

From: Pradeep Badri <pbadri@cisco.com>
To: eric@internet.sbi.com (Eric Ho)
Date: Thu, 21 Dec 95 10:58:53 PST
Cc: java-interest@java.sun.com (Java Interest Group)
In-Reply-To: <9512210504.AA07447@picard.sbi.com>; from "Eric Ho" at Dec 21, 95 12:04 am


Hello,

Get the graphic context in your paint or update method
and not in your constructor.  That should get you going.
The context resource gets created when the shows up on screen.

cheers,
-pradeep.
Pradeep Badri.
Cisco Systems.

> 
> Greetings Everyone,
> 
> I'm not sure if this is a bug or not in the appletviewer (JDK b1).
> 
> I've a canvas and in its overriden paint() method, I tried to do some simple
> drawings in a separate thread.  Calling Graphics' setColor() is fine but
> calling drawLine() greeted me with a NullPointerException.
> 
> Am I missing something ??
> 
> Any pointers/info much appreciated.
> 
> Here is the sample code :-
> 
> ------------8<------- CUT HERE -----------8<-----------
> 
> import java.applet.*;
> import java.lang.*;
> 
> public class Simple extends java.applet.Applet {
>   public void init() {
>     setLayout(new BorderLayout());
>     add("Center", new MyCanvas());
>   }
>   public static void main(String args[]) {
>     Frame f = new Frame("Simple");
>     Simple t = new Simple();
> 
>     t.init();
>     t.start();
>     f.add("Center", t);
>     f.resize(400, 400);
>     f.show();
>   }
> }
> 
> class MyCanvas extends java.awt.Canvas {
>   public void paint(Graphics g) {
>     CreativeBrush cb = new CreativeBrush(g);
>     new Thread(cb).start();
>     System.out.println("MyCanvas.paint(): Done.");  System.out.flush();
>   }
> }
> 
> class CreativeBrush implements Runnable {
>   Graphics gcontext;
>   public CreativeBrush(Graphics g) { 
>     this.gcontext = g; 
>   }
>   public void run() {
>     System.out.println("run(): starts.");  System.out.flush();
>     CrazyDrawings();
>     System.out.println("run(): done.");  System.out.flush();
>   }
>   void CrazyDrawings() {
>     gcontext.setColor(Color.orange);
>     System.out.println("CrazyDrawings(): starts.");  System.out.flush();
>     gcontext.drawLine(10, 10, 90, 90);  // <<=== *** PROBLEM  HERE *****
>     System.out.println("CrazyDrawings(): Done.");  System.out.flush();
>   }
> }
> ------------8<------- CUT HERE -----------8<-----------
> 
> -Eric Ho-  eric@sbi.com
> 
> -
> This message was sent to the java-interest mailing list
> Info: send 'help' to java-interest-request@java.sun.com
> 

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post