Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a graphics object?

Status
Not open for further replies.

tommyboyau

Programmer
Feb 7, 2005
47
AU
Hi Guys,
I am looking for some help creating a graphics object. At least thats what I think I want to do. Here is my scenario:

- thePainter.java - does the painting to the JPanel
- otherFile.java - does some other processing things not directly related to the applet however I would like it to generate graphics objects say a label or rectangle which can be passed to thePainter.java

Can someone give me some advise on this?

I have a method in otherFile.java which would return this object but i'm not sure how to setup a Graphics object if you get my drift. This is what I have:
public Graphics getSquare()
{
Graphics g = null;
g.create(15,15,40,40);
g.setColor(Color.green);
g.fillRect(15,15,40,40);

return g;
}
I am calling it using this in thePainter.java - paintComponent(currentGrid.getTom()); - that is within public void paintComponent(Graphics g).

I would have thought that would work ok but it keeps saying I am returning a Null Pointer exception or something when I run it?

Thanks,

Tom
 
I was approaching this from the wrong way. However I would be interested to see if this could be done :)

Thanks,

Tom
 
Code:
Graphics g = null;
g.create(15,15,40,40);

This will obviously cause a null pointer exception. You've got a null object reference (g) which you're trying to call a method on (create).

You need to do some reading around the 'paint' methods of the Component class. The AWT Gui system supplies you with a graphics context (Graphics) when 'paint' is called on a Component, upon which you can draw stuff.

Have a read of


Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top