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-7  คลาส Ball แสดงตัวแปรและเมท็อดประจำอ็อบเจกต์ลูกบอล
import jlab.graphics.DWindow;
public class Ball {
  double x, y;
  double dx, dy;
  double r;
  //---------------------------
  public void move(DWindow w) {
    x += dx; y += dy;
    if (x <= r || x >= w.getWidth() - r) dx = -dx;  
    if (y <= r || y >= w.getHeight()- r) dy = -dy;  
    x = Math.min(Math.max(x, r), w.getWidth()  - r);
    y = Math.min(Math.max(y, r), w.getHeight() - r);
  }
  public void draw(DWindow w) {
    w.fillEllipse(DWindow.BLACK, x, y, 2*r, 2*r);
  }     
}
©2009 S.Prasitjutrakul