Multi Threading and graphics - java

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.

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.

Call repaint of an other Component

I'm doing some small Java program for college and i'm running into a problem to redraw some Graphics after an update. Here's the context:
I have a main Jframe which contains basically two things, a JComponent used to draw/paint some Graphics (Rectangles, Circle, etc) and a JMenu to give sort options to the user.
The JComponent overrides paintComponent so I can draw the Graphics upon request. The Graphics are customs classes with methods to define the type of Graphic and each will call their own drawRect() or drawOval(). That is working very well. If I call repaint() for the main JFrame it will call paintComponent of the JComponent() as expected.
One of the menus in the JMenu has options to sort the Graphics per different order. So in my ActionListner of each options I proceed with the sort of the LinkedList containing the Graphics.
The problem is when im done with the sort in the ActionListener, I would call repaint() to draw the Graphics in the JComponent but it will call the repaint() of the JMenu instead... So my question is how to somehow call the repaint of the JComponent from the JMenu?
Thanks.
One of the menus in the JMenu has options to sort the Graphics per different order. So in my ActionListner of each options I proceed with the sort of the LinkedList containing the Graphics.
Then that code should invoke repaint() on the component. That is LinkList should be part of the component that paints the Graphics. So you would have a method like sort(...) that you invoke on the component. The method would then do the sort and then repaint() the component.
This is the way all Swing methods work, like setText(...), setBackground(...). You invoke these methods on the component and then the component invokes repaint() on itself.
The key is to make sure that the calling code has a clean reference to the viewed object of the called code. In other words, the listener code for your menu needs a reference to the drawing JComponent, usually placed into a field of the listener's class. How you pass it in will depend on the structure of your program. This can be done via a constructor parameter, or if you want to be cleaner, via dependency injection.

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.

Difference between paint() and paintcomponent()?

I have tried tutorials on this but I still don't quite understand it. Basically my question is which method is better and why? Should I use paint or paintComponent?
Please try to keep the answer simple, thanks.
Quoting from documentation of paint() method
This method actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren.
...
A subclass that just wants to specialize the UI (look and feel) delegate's paint method should just override paintComponent.
It looks like the paint() method actually draws the component, including the border and children. If you only want to customize the component's appearance excluding the border and children, you use paintComponent().
http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#paint(java.awt.Graphics)
Generally speaking, when painting in Swing, it is recommended to override paintComponent.
There are a number of reasons why, one is paintComponent is painted at the bottom layer, meaning you won't accidentally wiping out any components that were rendered during the paint process - this happens a lot to people who post here.
There are a, very, few times you might need to override paint, but I would always encourage you to try making it work with paintComponent first.
Check out
Performing custom painting
Painting in AWT and Swing (+1 to trashgod)

Content of JDesktopPane dissappears when resized - Java Swing

I have been working on Java Swing for a while. I paint something(draw some basic shapes like circle,rectangle etc) on a JDesktopPane and once I resize the frame window that contains jDesktopPane or drag some other window over this frame, then the drawn shapes disappear. I use an object of the BufferedImage class so as to save the image. So Is there any way of preventing the shapes getting disappeared or repaint it when they disappear?
You need to make sure you are saving what you paint and repainting it each time in the paintComponent() method. This method is called automatically whenever a repaint is needed (for example: because of a resize).
I can only provide a guess since you've decided not to post the necessary code, but my suggestions are:
Don't get your Graphics object by calling getGraphics on a component. Instead,
Be sure to do your drawing in a class that subclasses a JComponent or one of its children (such as a JPanel).
draw your BufferedImage in the JComponent's paintComponent method immediately after calling super.paintComponent() in the same method. Use the Graphics object that is provided by the JVM and passed into the method's parameter.
To be sure that your paintComponent method's signature is correct, place an #Override annotation immediately before it. Otherwise if it is not correct, the JVM will probably not call it when you expect it to.
But also, please when asking a question here, try to give us enough information so we don't have to guess. Since your problem is graphics related, it would make sense to post your graphics-related code, right?

Categories

Resources