• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

painting from user defined class

 
Ed Langley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ed Langley
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find your answer in this code somewhere.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic