drawPattern.java

import java.awt.*;  // uses the abstract windowing toolkit
import javax.swing.*;

public class drawPattern extends JPanel {

    Graphics g;
    int squareWidth;
    int frameTop, frameLeft;  // platform-dependent width of frame's top and left edges
    Color C_color = new Color(250,250,250); // s=3 cooperator
    Color O_color = new Color(60,00,160);   // s=2 pool
    Color E_color = new Color(160,60,00);   // s=1 peer
    Color D_color = new Color(00,00,00);    // s=0 defector

    /* コンストラクタ */
    drawPattern(int maxSize, int size) {
      squareWidth = maxSize / size;

      // ゲームシミュレーション本体画面の初期化
      JFrame gWin = new JFrame("Public Goods Game Model");
      gWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      gWin.setLocation(20,150);  // screen coordinates of top left corner
      gWin.setResizable(false);
      gWin.setVisible(true);  // show it!
      Insets theInsets = gWin.getInsets();
      gWin.setSize(maxSize+theInsets.left+theInsets.right,maxSize+theInsets.top+theInsets.bottom);
      this.frameTop = theInsets.top;
      this.frameLeft = theInsets.left;
      this.g = gWin.getGraphics();  // stick graphics object into a global so we can draw from anywhere!
    }

    // given a lattice site, color that square according to the current s value:
    public void colorSquare(int i, int j,int s) {
      switch (s) {
        case 0: this.g.setColor(this.D_color); break;
        case 1: this.g.setColor(this.E_color); break;
        case 2: this.g.setColor(this.O_color); break;
        case 3: this.g.setColor(this.C_color); break;
    }
    this.g.fillRect(this.squareWidth*i + this.frameLeft, this.squareWidth*j + this.frameTop, this.squareWidth, this.squareWidth);
  }
}