Solved

threads & double-buffering

Posted on 1998-10-18
6
241 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
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

777 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