3. ทำซ้ำ ๆ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 3-9  โปรแกรมแสดงลูกบอลสีดำเคลื่อนที่ ซึ่งวนกลับซ้าย และวนขึ้นบนได้
import jlab.graphics.DWindow;
// โปรแกรมแสดงลูกบอลสีดำเคลื่อนที่ ถ้าตกขอบ จะวนกลับมาอีกด้านของวินโดว์ 
public class MovingBall {
  public static void main(String[] args) {
    DWindow w = new DWindow(200, 100);
    double x = 100, y = 0;
    double dx = 4, dy = 2;
    while (true) {
      x = (x + dx) % w.getWidth();
      y = (y + dy) % w.getHeight();
      w.fade(0.3);
      w.fillEllipse(w.BLACK, x, y, 20, 20);
      w.sleep(50);
    }
  }
}
©2009 S.Prasitjutrakul