Solved

Initializing a Graphics object?

Posted on 1997-04-16
1
576 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

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 69
Spring Framework HTTPSession management 1 36
web services creation SOAP vs REST 5 41
sql import cannot be resolved jsp 3 23
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

809 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