• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

graphics

 
ShawnNev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void board{
Graphics g = this.getGraphics();
g.setColor(Color.green);
}
if I make a call to this method from start() or init() I get a null pointer exception. BUT if I put it inside the
public boolean mouseDown() method then it draws it currectly.
I'm guessing it has to do with the focus. But I'm on sure, the description of getGraphics says that
:Creates a graphics context for drawing to an off-screen image. This method can only be called for off-screen images
What exactly does that mean? what exactly is the off screen image?
Shane
p.s. I want this to paint without having to use the paint(Graphics g) method.
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is due to the fact that you asking for the Graphics object before the component has had a chance to make a connection to it peer (a native graphics object, for instance). Basically, you shouldn't try to get the Graphics object until the component has called it's first repaint() (when it's all hooked up and ready to go). You could try to call repaint() and then the board() method in your init() method. But the safest thing to do might be to use a boolean flag and call the board() method the first time repaint() is called. For example

There are ways of doing this by comunicatinig with the peer directly to make sure that it is ready, but I can't recall the specifics at the momment. Hope this helps.
Sean
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also try doing something like this:


------------------
- Daniel
[This message has been edited by Daniel Searson (edited December 10, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic