tags:

views:

560

answers:

1

When you call repaint(), for example, repaintComponent(Graphics) gets called, and then you can call it from outside the class without the parameter Graphics.

I'd like to have a function that takes more parameters I'm using, but I still want to be able to draw with it, so I need to be able to make a call like that (eg repaint() calls repaintComponent(Graphics)) or get access to Graphics to call the function, assuming it would get repainted by explicitly calling repaintComponent() anyway.

I've been thinking about putting the data in the object and then drawing, but I'm not sure if it'd work and it would be pretty complicated, I think.

+1  A: 

You cannot do any component painting to the GUI except and only while processing an O/S paint event, which triggers a Java paint event on the event dispatch thread. Repaint does not call paint() or any of it's derivatives - it requests a repaint of an area of your component, and that triggers a paint event.

You need to somehow set the information you need to paint into your component, whether that be by setting properties (e.g. Swing) or subclassing and adding the setter's to your subclassed component. Having put your data into an object, setting that object into your component should be trivial.

You can also create a separate object with a paint(Graphics) method, associate that object with your component, and invoke it's painting method(s) from the object paint - I have done that in the past in order to separate painting concerns out to a large number of objects.

Software Monkey
+1 Software MonkeyFYI, calling repaint() does not directly call paintComponent(...). You're essentially asking swing(awt) to repaint a component at the next possible time. The event thread will decided when that time is and eventually a paintComponent(...) and the proper graphics object will be provided by the top-level native code.The idea of passing paramters to a function with out specifically declaring them or using a surrounding object/static object/singleton is not possible in Java.
basszero