[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

threads & double-buffering

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
kevinhol
Asked:
kevinhol
  • 3
  • 2
1 Solution
 
diakovCommented:
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
 
imladrisCommented:
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
 
kevinholAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kevinholAuthor Commented:
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
 
kevinholAuthor Commented:
Adjusted points to 200
0
 
diakovCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now