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);
Related
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.
I'm trying to implement a simple window scale in java's swing library. The goal is simply to double the window height and width, and paint the window and each of its components in scale.
Here's an example of the code I'm using:
public class MyWindow extends JFrame {
...
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.scale(2,2);
super.paint(g);
}
}
The position and size for each my components in this window is set manually using setBounds with a null layout for the window.
When I actually run the program, what happens is that the first paint for the window seems successful-- everything is sized appropriately. Each subsequent repaint by the components, however, is neither twice the size, nor in the proper location. Here's what I mean:
As you can see, portions of the screen which have components that call repaint manually (the animating bits), don't seem to be using the Graphics2D scale of the JFrame. I looked in the source code, and tried overloading a few other methods (update and repaint, mostly), but all of them seemed to produce the same result. I further looked at the paint and repaint methods of the component and container classes, but they all seem to call a specified repaint of their parent. Shouldn't my Window be the "biggest" parent? If so, why haven't these repaint calls reached my Window?
My big question to you is, therefore: what repaint methods of the parent component do the child components call? Why aren't the calls properly routed to my JFrame's paint call? Is there any other (better) way that I can scale my window? Any and all help is appreciated!
As discussed in Painting in AWT and Swing: The Paint Methods, "Swing programs should override paintComponent() instead of overriding paint()." A common approach is to create a view by overriding paintComponent() in a JComponent (or subclass), as shown here. Let your view listen for changes to the game's model, as discussed here.
SwingUtilities.updateComponentTreeUI() should be used to change the Look & Feel, not update the view.
Use
javax.swing.SwingUtilities.updateComponentTreeUI(parentComponent);
when you need to make sure all of the parentComponents child component's look and feel are updated properly.
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.
Frequently when I call getGraphics() it returns null, even if I set the xxx.getGraphics(); xxx to be visible (as a Google search shows...)
But this doesn't work, and this frustrates me as it is easy and simple to do in C-Sharp.
Does anyone know of a better way of doing this instead of using getGraphics()??
You usually don't want to use getGraphics on a Java Swing component since this will be null if the component has not been rendered yet, and even if the component has been rendered and the Graphics object returned is not null, it will usually be a short-lived object and will not work properly if the component gets repainted (a process that is out of your control).
A better alternative is to draw in a JComponent's paintComponent method and using the Graphics object passed into this method as its parameter. If you need to draw something that is to be a background image, also look into drawing on a BufferedImage. When you do that, here you will call getGraphics() on the image (or createGraphics() if you need a Graphics2D object), and here the object returned will be stable. You'll still need to display this image somehow, either as an ImageIcon displayed by a JLabel or as an Image displayed in a JComponent's paintComponent method, by calling Graphics#drawImage(....) on the paintComponent's Graphics parameter.
Don't use getGraphics(). Any painting you do will be temporary and will be lost the next time Swing determines a component needs to be repainted.
Instead override the paintComponent() method of a JComponent or JPanel to do your custom painting. See Custom Painting for more details and examples.
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?