Original Post
I have been learning java from some tutorials and I do not understand why the ball didn't move faster whenever I increase the value of speed. Any help will be appreciated.
import java.applet.*;import java.awt.*;public class Bounce extends Applet implements Runnable{ //variables int x_pos = 10; int y_pos = 100; int radius = 20; int speed = 4; int appx = 300; private Image dbImage; private Graphics dbg; public void init() { setBackground(Color.blue); } public void start() { Thread th = new Thread(this); th.start(); } public void stop() {} public void destroy() {} public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while(true) { if(x_pos > appx - radius) { speed = -4; } else if(x_pos < radius) { speed = +4; } x_pos += speed; repaint(); try { Thread.sleep(10); } catch(InterruptedException ex) { } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void update(Graphics g) { if (dbImage == null) { dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics(); } dbg.setColor(getBackground()); dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); dbg.setColor(getForeground()); paint(dbg); g.drawImage(dbImage, 0, 0, this); } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); } }