Solved

Initializing a Graphics object?

Posted on 1997-04-16
1
577 Views
Last Modified: 2006-11-17
I need to draw some graphics into a frame(drawRect, drawLine, drawPolygon).  I am doing this in an APPLICATION.  This requires a Graphics object.  I have tried Graphics g= someComponent.getGraphics(); but this does not work.  I get a nullPointerException when I run the program.  The window frame appears, but I get no graphics.  I need to know how to initialize the Graphics object.  I have had several people tell me to use a paint method, but this does not instantiate the Graphics object.
0
Comment
Question by:FRAYSER
1 Comment
 

Accepted Solution

by:
Ted Hopp earned 100 total points
ID: 1219589
If you getGraphics() before the component's addNotify() method has been called, it will return null. This function generally does not get called until the component's show() method is called. Things will probably work fine if you delay calling getGraphics() on your component until after the component is visible.

You can be fancy, and override someComponent's addNotify() method as follows:

public synchronized void addNotify() {
      super.addNotify();            // Creates peer
      // ... inform the app that getGraphics() will work.
}

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 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