8. วัตถุสิ่งของ

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.

 
รหัสที่ 8-4  โปรแกรมแสดงลูกบอล แบบใช้อ็อบเจกต์ลูกบอล
import jlab.graphics.DWindow;
public class TestBall {
  public static void main(String[] args) {
    Ball b = new Ball();
    b.x = 100; b.y = 100;
    b.dx = 3; b.dy = -2;
    b.r = 5;
    DWindow w = new DWindow(200, 200);
    while (true) {
      w.clearBackground();
      b.x += b.dx; b.y += b.dy;
      if (b.x <= b.r || b.x >= w.getWidth() - b.r) b.dx = -b.dx;  
      if (b.y <= b.r || b.y >= w.getHeight() - b.r) b.dy = -b.dy;  
      b.x = Math.min(Math.max(b.x, b.r), w.getWidth() - b.r);
      b.y = Math.min(Math.max(b.y, b.r), w.getHeight() - b.r);        
      w.fillEllipse(DWindow.BLACK, b.x, b.y, 2*b.r, 2*b.r);
      w.sleep(50);
    }
  }
}
©2009 S.Prasitjutrakul