my dog learned polymorphism
The moose likes Beginning Java and the fly likes painting from user defined class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "painting from user defined class" Watch "painting from user defined class" New topic

painting from user defined class

Ed Langley

Joined: Feb 10, 2001
Posts: 4
Hello everybody
I am trying to use painting methods from inside my own class in an application:

The drawstring call in update() generates a NullPointerException during execution. I have tried various other ways but can't get the text drawn in the frame.
What is wrong with the above code?
What class should I be extend to make picSegArray and is there a better way to paint from inside it?
Thanks in advance.
Michael Hildner
Ranch Hand

Joined: Oct 13, 2000
Posts: 297
From the API on getGraphics():
Creates a graphics context for this component. This method will return null if this component is currently not displayable.
You are getting a NullPointerExcpetion because g is null, as the Panel is not currently disaplyed.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

your main problem is there is you are defining your own update() method. The update method of Component class is update(Graphics g)
Panel is ok as far as I know but Canvas is perhaps used more often.
I believe this is all you really need:

The component should get painted automatically. If you do need a constructor this is probably all it needs to be:
public picSegArray() {
super() is automatically called.
repaint() calls update(Graphics g), supplying the Graphics object. update(Graphics g) clears the component then calls paint(Graphics g)
I dont think you need a constructor but I wanted to explain about super() and repaint()
Dont blindly believe everything I say.

Visit my download page
Ed Langley

Joined: Feb 10, 2001
Posts: 4
Thanks a lot for that.
I discovered that when i did it other ways the panel wasn't showing because i had a button in the frame as well and was using a flowLayout. I was adding the button first though so I don't know why it showed and not picSegArray.
The class actually does a whole lot more than that but I simplified it for posting.
Anyway I wanted to use a lot of painting code in other functions and the double buffering technique seems very handy for this, but I can't get the image created properly:

the getGraphics line in this function inside picSegArray generates another nullPointerException, so my new question is how do I properly initialise offScreenImage?
resetOffScreen() isn't called until the component is displayed.
[This message has been edited by Ed Langley (edited February 12, 2001).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

You might find your answer in this code somewhere.
I agree. Here's the link:
subject: painting from user defined class
It's not a secret anymore!