Solved

Initializing a Graphics object?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

739 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