This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Class with Graphics instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Class with Graphics instance" Watch "Class with Graphics instance" New topic
Author

Class with Graphics instance

Chris Carney
Greenhorn

Joined: Sep 28, 2004
Posts: 7
I am looking to draw a rectangle with multiple fill3DRect(), status boxes inside, which, represent network status of each host. I would like to create a class instantiated from my:
public class MapDsp extends JPanel implements MouseListener

that would handle all the drawing. I have a two part question, do I have to pass in the Graphics instance from the paintComponent(Graphics g) method , or can I create an instance in the class I want to do all my drawing in. Once I get the graphics instance, and make a draw call how do I get the content so it is Visible on the screen?

I wrote A quick test class that was instantiated in the paintComponent(Graphics g) of public class MapDsp extends JPanel implements MouseListener and used called
public void test() {
g.setColor(FuncStatTypes.FailedColor);
g.fill3DRect(25,25,500,500, true);
}
But nothing was visible on the screen. Is there a simple call to make the drawing visible?

Thanks.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I think we need to see more of your code to understand exactly what's going on...

Why not use separate JPanels for each of the network status "boxes"? It is way more easier to handle all the sizing issues, and if changes are needed - it's a lot easier to just add a component to a panel than figure out how to draw new stuff yourself with graphics primitives.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Chris Carney
Greenhorn

Joined: Sep 28, 2004
Posts: 7
I have anywhere from 2 to a dozen "boxes" to draw all with a different number of "status boxes" in them. I need more control of placement that I can get from a layout manager. I want to have location and size control to the pixel.


I am wondering if I am getting a stale reference to the graphics instance when I call another class passing in the reference. Can you pass a graphics reference from public void paintComponent(Graphics g) to another class?
Chris Carney
Greenhorn

Joined: Sep 28, 2004
Posts: 7
Don't know what I am doing wrong yet, but I wrote a simple app to simulate this and I can draw boxes in both classes.
Thanks for your help, looks like I have a little debuging to do.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

OK - just wanting to make sure.

Yes - you can pass the Graphics reference to other classes from inside the paintComponent() method.

This sounds similiar to what I do when I want to do some Java game programming rather than straight "GUI" programming. Make one big JPanel for the entire "Game" content - then make separate classes for each "Sprite" type of thing. Each "Sprite" handles all the drawing information for itself, but it's not a "Component" - I just pass the Graphics reference from my Game JPanel into the drawing method of each "Sprite" as it needs to be drawn.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Nathan Pruett:

Yes - you can pass the Graphics reference to other classes from inside the paintComponent() method.


In fact that is what you *need* to do. You can't create those graphics instances yourself - that's the responsibility of the platform specific AWT implementation.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class with Graphics instance
 
Similar Threads
repainting JLabels on a JPanel
Urgent: Glass Pane is Opaque!!
JApplets, can't seem to draw anything
JPanel painComponent not working
MousePressed