wood burning stoves 2.0*
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/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "painting from user defined class" Watch "painting from user defined class" New topic
Author

painting from user defined class

Ed Langley
Greenhorn

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: 4347
    
    2

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() {
repaint();
}
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.


SCJP
Visit my download page
Ed Langley
Greenhorn

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: 4347
    
    2

You might find your answer in this code somewhere.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: painting from user defined class