• 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

Class with Graphics instance

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It was the best of times. It was the worst of times. It was a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic