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-24  คลาส Card แทนบัตรตัวเลขของเกมจับคู่ทดสอบความจำ
import jlab.graphics.DWindow;
public class Card {
  public static final int WIDTH = 60, HEIGHT = 60;
  private int number;           // หมายเลขของบัตรใบนี้ 
  private int row, col;         // ตำแหน่ง (หมายเลขแถว หมายเลขสดมภ์)
  private boolean matched;      // จับคู่ไปแล้วหรือยัง 
  private DWindow window;       // วินโดว์ที่แสดงบัตรใบนี้ 

  public Card(DWindow w, int r, int c, int n) {
    window = w;
    row = r; col = c;
    number = n;
  }
  public int number() {                  // คืนหมายเลขของบัตรใบนี้ 
    return number;
  }
  public void show() {
    draw(DWindow.WHITE, true); // บัตรสีขาว แสดงหมายเลขของบัตร 
  }
  public void hide() {
    draw(DWindow.GRAY, false); // บัตรสีเทา ไม่แสดงหมายเลข 
  }
  public void matched() {
    matched = true;             //  บอกให้รู้ว่าบัตรใบนี้ถูกจับคู่แล้ว  
    draw(DWindow.PINK, true);  // แสดงหมายเลข บนบัตรสีชมพู 
  }
  public boolean isMatched() {  // คืนสภาวะว่า บัตรใบนี้จับคู่แล้วหรือยัง 
    return matched;
  }
  private void draw(int color, boolean showNumber) {
    int x = col * WIDTH, y = row * HEIGHT;
    window.fillRect(color, x, y, WIDTH, HEIGHT);
    window.drawRect(DWindow.BLACK, x, y, WIDTH, HEIGHT);
    if (showNumber) window.drawString(""+number, 30, x+5, y+5);
  }
}  
©2009 S.Prasitjutrakul