9. สร้างใหม่จากเก่า

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.

 
รหัสที่ 9-5  คลาส DotMatrixLED
import jlab.graphics.DWindow;

public class DotMatrixLED {
  private boolean[][] dots;
  private double width;
  private int color;
   
  public DotMatrixLED(int w, int h) {
    dots = new boolean[h][w];
    color = DWindow.GREEN;
    width = 50;
  }
  public void setDots(boolean[][] s) {
    if (s.length == dots.length && s[0].length == dots[0].length) {
      for (int i = 0; i < s.length; i++)
        for (int j = 0; j < s[0].length; j++)
          dots[i][j] = s[i][j];
    }
  }
  public void setWidth(double w) {
    if (w >= dots[0].length) width = w;
  }
  public int getWidth() {
    return (int) Math.round(width);
  }
  public void setColor(int c) {
    color = c;
  }
  public int getColor() {
    return color;
  }
  public void draw(DWindow w, double x, double y) {
    double wd = width / dots[0].length;
    double xc = x + wd / 2;
    double yc = y + wd / 2;
    for (int i = 0; i < dots.length; i++)
      for (int j = 0; j < dots[0].length; j++)
        if (dots[i][j])
          w.fillEllipse(color, xc + j*wd, yc + i*wd, wd, wd);
  }
}
©2009 S.Prasitjutrakul