drawPattern.javaを2-punishments model とdouble-moral modelと共通のものとし、それぞれのモデル用のdrawPatern用の描画クラスはそれをextendして使う
2つのモデルを同時に作ったので、punish と moralが行動している部分があるかもしれない。チェックして使うこと.
import java.awt.*; // uses the abstract windowing toolkit public class drawPattern2Punishments extends drawPattern { /* 色のオーバーライド */ drawPattern2Punishments(int maxSize, int size) { super(maxSize, size); color3 = new Color(250, 250, 250); // s=3 cooperator color2 = new Color(60, 00, 160); // s=2 pool color1 = new Color(160, 60, 00); // s=1 peer color0 = new Color(00, 00, 00); // s=0 defector bg_color = new Color(255,255,255); fore_color = new Color(0,0,0); } }
import java.awt.*; // uses the abstract windowing toolkit public class drawPattern2morals extends drawPattern { /* コンストラクタ */ drawPattern2morals(int maxSize, int size) { super(maxSize, size); color3 = new Color(0, 0, 250); // s=3 cooperator color2 = new Color(0, 255, 0); // s=2 moralist (green) color1 = new Color(150, 150, 0); // s=1 immoralist (dark yellow) color0 = new Color(255, 0, 0); // s=0 defector (red) bg_color = new Color(255,255,255); fore_color = new Color(0,0,0); } }
MCステップを表示するためのshowStringメソッドを追加したもの
import java.io.*; import java.awt.*; // uses the abstract windowing toolkit import javax.swing.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class drawPattern extends JPanel { Graphics g; JFrame gWin; int squareWidth; int frameTop, frameLeft; // platform-dependent width of frame's top and left // edges protected Color color3 = new Color(0, 0, 255); // s=3 cooperator (blue) protected Color color2 = new Color(0, 255, 0); // s=2 moralist (green) protected Color color1 = new Color(150, 150, 0); // s=1 immoralist (dark yellow) protected Color color0 = new Color(255, 0, 0); // s=0 defector (red) protected Color bg_color = new Color(255,255,255); protected Color fore_color = new Color(0,0,0); protected int showStringHeight; /* コンストラクタ */ drawPattern(int maxSize, int size) { squareWidth = maxSize / size; // ゲームシミュレーション本体画面の初期化 gWin = new JFrame("Public Goods Game Model"); 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+30); /* 下30は文字出力用*/ this.frameTop = theInsets.top; this.frameLeft = theInsets.left; this.g = gWin.getGraphics(); // stick graphics object into a global so // we can draw from anywhere! /* set the property of strings to show status */ Font font = new Font("Arial", Font.BOLD, 24); this.g.setFont(font); this.showStringHeight=30; } // 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.color0); break; case 1: this.g.setColor(this.color1); break; case 2: this.g.setColor(this.color2); break; case 3: this.g.setColor(this.color3); break; } this.g.fillRect(this.squareWidth * i + this.frameLeft, this.squareWidth * j + this.frameTop, this.squareWidth, this.squareWidth); } public void showString(String str) { g.setColor(this.bg_color); g.fillRect(0, gWin.getHeight() - showStringHeight,gWin.getWidth(), showStringHeight); g.setColor(this.fore_color); g.drawString(str, 10,gWin.getHeight()-5); } /* saveImage doesn't work! (save a snapshot to a file) */ public boolean saveImage(String fileName,String format){ BufferedImage img = new BufferedImage(gWin.getWidth(), gWin.getHeight(), BufferedImage.TYPE_INT_RGB); gWin.paint(img.getGraphics()); try { ImageIO.write(img, format, new File(fileName)); System.out.println("panel saved as image"); } catch (IOException e) { e.printStackTrace(); System.out.println("panel not saved" + e.getMessage()); } return true; } }