?
Solved

Initializing a Graphics object?

Posted on 1997-04-16
1
Medium Priority
?
581 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
[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
1 Comment
 

Accepted Solution

by:
Ted Hopp earned 300 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

719 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