Solved

Initializing a Graphics object?

Posted on 1997-04-16
1
574 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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now