Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

threads & double-buffering

Posted on 1998-10-18
6
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1225920
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
ID: 1225921
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
ID: 1225922
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
Technology Partners: 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!

 

Author Comment

by:kevinhol
ID: 1225923
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
ID: 1225924
Adjusted points to 200
0
 
LVL 8

Accepted Solution

by:
diakov earned 600 total points
ID: 1225925
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

618 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