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-7  โปรแกรมแสดงงูเลื้อยตามเมาส์ (ใช้กลวิธีการทำภาพจาง)
import jlab.graphics.DWindow;
// โปรแกรมงูเลื้อยตามเมาส์ 
public class Snake {
  public static void main(String[] args) {
    int width = 160, height = width;
    DWindow w = new DWindow(width, height);
    double x0 = 0, y0 = 0, x1, y1;
    while (true) {
      x1 = w.getMouse().getX();
      y1 = w.getMouse().getY();
      w.fade(0.3);     // ทำภาพให้จางลงนิดหน่อยสัก 30%
      w.drawLine(x0, y0, x1, y1); //เส้นล่าสุด เข้มสุด 
      w.sleep(100);    // หยุดพักสักครู่ให้ผู้ใช้เห็นภาพ 
      x0 = x1; y0 = y1;
    }
  }
}
©2009 S.Prasitjutrakul