Solved

threads & double-buffering

Posted on 1998-10-18
6
239 Views
Last Modified: 2010-03-30
My main thread has no problem double-buffering, however when I spawn another thread it returns a null pointer exception at the line offscreenGraphics = offscreenImage.getGraphics();. Both variables are static. What is going on and how do I get a seperate thread to double-buffer?
0
Comment
Question by:kevinhol
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:diakov
Comment Utility
I suggest you declare access methods for the offscreen variable and declare them synchronized. This way you will make the access to them thread safe, and no competition will occur for setting and reading the shared variables.
0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
Sound advice in general, however it doesn't appear, off hand, to deal with a null pointer exception at the line (though it could be related to a null pointer exception somewhere within the getgraphics call).
Perhaps you could post the relevant code here for a more precise diagnosis.

0
 

Author Comment

by:kevinhol
Comment Utility
I didn't mention that the spawned thread was in a class of it's own. I don't know if that will help or not.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:kevinhol
Comment Utility
Here is the source of my madness. It should paint two words to the screen.


import java.awt.*;


class cry extends angry implements Runnable {
  public void run() {
    mad(40);
}
}

public class angry extends java.applet.Applet implements Runnable {
 static Image currentimg = null;
 static Thread runner;
 static Thread tears;
 static Graphics currentGraphics;
 static Image offscreenimage;
 static Graphics offscreengraphics;
 static boolean draw = false;


public void start() {
        if (runner == null) {
            runner = new Thread(this);
            runner.start();
        }
    }
    public void stop() {
        if (runner != null) {
            runner.stop();
            runner = null;
        }
    }

public void mad(int line){
  offscreenimage = createImage(100,60);
  offscreengraphics = offscreenimage.getGraphics();
  offscreengraphics.setColor(Color.red);
  Font font = new Font("TimesRoman", Font.BOLD,11);
  offscreengraphics.setFont(font);
  offscreengraphics.drawString("MADNESS!!!!!",0,line);
  currentimg = offscreenimage;
  paint(currentGraphics);
}

public void run() {
  draw = true;
  mad(25);
  cry cy = new cry();
  tears = new Thread(cy);
  tears.start();
}

public void init() {
 setBackground(Color.white);
 currentGraphics = getGraphics();
}

public void paint(Graphics g) {
  if (draw == true){
  g.drawImage(currentimg,0,0,this);
  }
}
}
0
 

Author Comment

by:kevinhol
Comment Utility
Adjusted points to 200
0
 
LVL 8

Accepted Solution

by:
diakov earned 200 total points
Comment Utility
Hi Kevin,

I hapens that it is not a good idea to make two instances of an applet within the same code. One instance the browser makes out of your angry class, and another you make manually by creating a cry object which derives the angry class. Anyway, the second instance of the applet is not provided with the right Applet context and I suspect this is the reason the getGraphics fails.
I modified the code so the second thread calls the only instance of the applet with its mad() method which I also made synchronized, in case you want to run some more Threads to use it.

Hope this helps,
 Nik

//file angry.java
import java.awt.*;


class cry implements Runnable
{
 angry p;
 cry (angry p)
 {
   this.p = p;
 }
 public void run()
 {
   p.mad(40);
 }
}

public class angry extends java.applet.Applet implements Runnable
{
      static Image currentimg = null;
      static Thread runner;
      static Thread tears;
      static Graphics currentGraphics;
      static Image offscreenimage;
      static Graphics offscreengraphics;
      static boolean draw = false;


      public void start() {
               if (runner == null) {
                     runner = new Thread(this);
                     runner.start();
               }
         }
         public void stop() {
               if (runner != null) {
                     runner.stop();
                     runner = null;
               }
         }

      public synchronized void mad(int line)
      {
       try
       {
             offscreenimage = createImage(100, 60);
             offscreengraphics = offscreenimage.getGraphics();
             offscreengraphics.setColor(Color.red);
             Font font = new Font("TimesRoman", Font.BOLD,11);
             offscreengraphics.setFont(font);
             offscreengraphics.drawString("MADNESS!!!!!",0,line);
             currentimg = offscreenimage;
             repaint();
      }
      catch (Exception e)
      {
            e.printStackTrace();
      }
      }

      public void run()
      {
       draw = true;
       mad(25);
       cry cy = new cry(this);
       tears = new Thread(cy);
       tears.start();
      }

      public void init()
      {
        setBackground(Color.white);
      }

      public void paint(Graphics g)
      {
       if (draw == true)
       {
             g.drawImage(currentimg, 0, 0, this);
       }
      }
}
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now