Solved

threads & double-buffering

Posted on 1998-10-18
6
251 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 200 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

Industry Leaders: 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!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

690 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