JFrame Repaint() Individual Component - java

Is it possible to repaint only a specific component in a JFrame, rather than the entire thing? If this is possible, how would you do this?

Yes, you can suggest the repainting of a single component by calling repaint(); on that component only. Per the Component API:
public void repaint()
Repaints this component.
If this component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible.

Related

What is the need of recursive update or paint in order to paint lightweight component?

I am reading this article on Painting in AWT and Swing which have following paragraph in section "How lightweights get painted"
It's worth noting that the default implementation of Container.update() does not use recursion to invoke update() or paint() on lightweight descendents. This means that any heavyweight Container subclass that uses update() to do incremental painting must ensure that lightweight descendents are recursively repainted if necessary. Fortunately, few heavyweight container components need incremental painting, so this issue doesn't affect most programs.
Please tell me, in which case do I need to have recursive calls to update() or paint() and why?
You don't need recursive calls to update() or paint().
If you want to paint a component all you do is:
component.repaint();
This will add a paint request to the RepaintManager to paint the component and Swing will make sure that the components child components get painted when the paintChildren(...) method is invoked. See A Closer Look at the Paint Mechanism for more information.

paintComponent called continuously

Similar problem here but didn't find an answer: Why is paintComponent() continuously and asynchronously being called without explicit repaint() call?
I have a JPanel that I am drawing onto
class DrawPanel extends JPanel {
final void paintComponent(Graphics g) {
super.paintComponent(g);
// some graphics drawing stuff
} }
and then adding this to a JScrollPane. However if I put a system.out.println() in the paintComponent method I can see its continuously being called. Any way to stop this? According to the link its possible due to the jpanel being covered
Generally paintComponent() does not paint continually. It get invoked occasionally when Swing determines it needs be painted.
If your method is being invoked continually then I can think of a couple of possible problems. You are:
manually invoking repaint()
changing a property of the component in the paintComponent() method which then automatically invokes repaint()
The paintComponent calls come from Swing's Event Dispatch Thread. It gets called everytime the component needs to be repainted.
If you resize the component or bring it back from minimized state, then it's repainted. Of course if you cover it with another component then the repainting will be called less. The other component will have a paintComponent method too though.
Nothing to worry about.

Using swing components with an applets paint method

I have a functioning Java applet which can be embedded into a web page and wish to now add some swing components for additional functions. However whenever I add a component like a JLabel, it simply does not appear on the viewport/canvas unless I remove my entire paint method. The latter option allows me to add swing components but naturally I then cannot render any shapes. It appears to resemble an eXclusive OR (XOR), either one but not the other.
Is there anyway in a native Java applet to add swing components and still maintain the paint(Graphics g) method. Please note that I am inheriting from Applet and not JApplet.
If you override paint method in applet then there is no simple way.
What you could do instead of overriding paint in applet.
Extend JComponent instead and do the custom drawing there.
create JPanel that contains all the needed swing components including the component from earlier step.
Add that panel to applet which is using default paint method.
I suggest that you add the components to a separate panel that doesn't override paint. By overridding paint, you are customizing the way the component is drawn, so the layout manager and the components it has to manage do not count, it's only the implementation of paint you wrote that dictates how stuff is rendered.
So, your applet would contain a panel with the components and a panel that does the custom painting.
At the end of your paint method, call super.paint(g).
I was able to visualise the components over the applet canvas by invoking
paintComponents(g);
Thus the components are thereafter painted AFTER the actual paint(Graphics g) method completes its epoch.

Multi Threading and graphics

My project is based on multi threading and graphics. The problem is in calling repaint() method. I am trying to call repaint method from class second which implements runnable and paint() method is in class first extending Canvas. But repaint not working.
What should I do?
Thnx
It would be much wise, that instead of using Canvas, if you override the paintComponent(...) method of a JComponent and paint on it, instead of overriding paint(...) method.
The Component.repaint() method, under Swing in particular, only marks the component as needing a repaint, it doesn't actually trigger the repaint directly.
Make sure you allow other threads to run by calling Thread.yield(), as it's the main graphics thread (which launched your other thread(s)) that actually checks the components to see what needs to be repainted.

Java repaint not working correctly

i use the java repaint method , it repaints, but the update is only seen when I either click on the canvas or resize the panel. How can I fix this ? What causes it?
You need to call the method revalidate(). This forces the layout manager to update / repaint all its components.
repaint() isn't actually repainting, it's just requesting a repaint of the component.
It may be helpful to simply grab the Graphics object from the component you wish to paint. Then just invoke a paint method on the Graphics object. For example:
g = component.getGraphics();
draw(g);

Categories

Resources