• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

painting from user defined class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find your answer in this code somewhere.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic