When I run the code below with the "thread.sleep", the ball runs from left corner to right corner, leaving a trail behind it as expected.
but when I remove the thread.sleep part, the balls appears directly at the right side corner, without a trail (similar to the behavior after adding a "fill.rec", but there is no such thing is this code".)
So I ran the code without "thread.sleep" in DEBUG mode. Then it left a trail as expected. The same code, but in debug mode. What is going on here?
package mypong; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author Administrator */ public class MyPong extends JFrame { int x=0; //ball X coordinate at the beginning int y=0; //ball's Y coordinate at the beginning int xPos=100; int yPos=100; int circ=20; //width height of the ball //initialize the frame public void go() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyDrawPanel drawPanel = new MyDrawPanel(); frame.getContentPane().add(drawPanel); frame.setSize(480,500); frame.setVisible(true); while (true) { if (x<440) { x+=1; y+=1; } drawPanel.repaint(); // try // { // Thread.sleep(10); // } // catch(Exception x) // { // x.printStackTrace(); // } }//end for }//end go public static void main(String[] args) { MyPong mypong=new MyPong(); mypong.go(); } //inner class drawpanel class MyDrawPanel extends JPanel { @Override public void paintComponent(Graphics g) { g.setColor(Color.black); g.fillOval(x, y, circ, circ); } } }