I can want a ball is moving in the window, but it doesn't work. the idea is : draw a ball in red color,then draw the same ball in the ground color to cover the previous ball, the then draw another ball beside the previous one ,the cover .. so it looks like the ball is moving. but the result is at beginning time, where is no ball at the window, at last the ball appear at the final position , there is no movement. here is the program: //import java.awt.BorderLayout; import java.awt.Point; import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class operation extends JFrame{
Panel graph;
public operation(String name) { super(name); setSize(400,600); graph = new Panel(); getContentPane().setLayout(new BorderLayout()); getContentPane().add(graph,BorderLayout.CENTER);
the reason i thought is the first the computer calculate the things of the panel, then put the panel on the frame, so we can only see the final result. i am right? how to realize what i want .
The text area in which you write place your posting has instructions to your right on how to format your code. Please repost your code whilst following these instructions i.e your code must be encampused in Java tags. You see the reality is that on Artima there is a diverse amount of skill in a whole wack of areas. For instance if you're asking about Swing, I would have probably responded to your post in a matter of hours from when you posted it depending on your geographical location (I deal with Swing 9 hrs a day at work and 3 hours at home on personal projects). Matthias also seems to be extremely competent in Swing.
That said to me, its extremely discouraging to look at untidy code so there are lazy days like today being a Friday when I'd just say, forget that.
For you'r own good please follow the instructions you will find that you will get an extremely fast response rate on artima relative to any other time (especially if its a Swing based question) I spend my coffee breaks on this site.
Animation in Java requires that you decide on two distinct issues. Resolution of the speed and smoothness of the motion. If your motion needs to update 30 times per second, and it needs to paint all frames, then you can use a loop with a sleep and call JComponent.paintImediately(). If the resolution is looser, then you can call repaint() inside of a SwingUtilities.invokeLater() with a delay between calls. Then, in the paintComponent, you use the current time, or a frame number to paint the current, correct rendering. By doing invoke later, you allow slow computers to still update in real time, but draw fewer frames/sec.
You can't call sleep in a paintComponent() call and expect to see anything on the display. Your paint routing is painting into a double buffering buffer, which is composited onto the screen after you return from paintComponent().