• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class with Graphics instance

 
Chris Carney
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Carney
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic