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-3  โปรแกรมนาฬิกาที่มีแต่เข็มวินาที
import jlab.graphics.DWindow;
// โปรแกรมนาฬิกาเข็มเดียว 
public class Clock {
  public static void main(String[] args) {
    int width = 160, height = width, len = width*4/10;
    DWindow w = new DWindow(width, height);
    double x0 = width/2, y0 = height/2;
    double x1, y1, angle = 90, delta = 6; // เริ่มที่ตำแหน่งเลข 12
    while (true) {
      x1 = x0 + len * Math.cos(Math.toRadians(angle)); //เหมือนใน 
      y1 = y0 - len * Math.sin(Math.toRadians(angle)); //รหัส 2-18
      w.clearBackground();         // ล้างวินโดว์ 
      w.drawLine(x0, y0, x1, y1);  // วาดเส้นใหม่ 
      w.sleep(1000);               // หยุดการทำงาน 1000 มิลลิวินาที 
      angle -= delta;              // ปรับมุม เพื่อวาดเส้นในรอบต่อไป 
    }
  }
}
©2009 S.Prasitjutrakul