我想编一个围棋程序,在paint中where="init"处画了个棋盘,可是单击一下它并不画棋子,用另一个窗口覆盖一次后就出现了棋子,可棋盘又没了,我原以为是update问题,可我跟踪调试发现repaint好像根本没到update,怎么回事?怎么解决棋盘不见的问题,谢谢了
import java .awt .*; public class Igo extends Frame { public void Put (int x,int y){ //单击下子 IgoPanel.set("put"); IgoPanel.set(x,y); repaint(); } public Igo() { //初始化 setLayout(new BorderLayout()); IgoPanel display = new IgoPanel(); add("Center",display); } public boolean handleEvent(Event e){ switch(e.id){ case Event.WINDOW_DESTROY:System.exit(0);return true; case Event.MOUSE_DOWN :Put(e.x,e.y);return true; default:return false; } } public static void main(String args[]) { //初始化 IgoPanel.set("init"); Igo igo = new Igo(); igo.setTitle ("Igo"); igo.setSize(560,560); igo.show(); } }
class IgoPanel extends Panel { public static String where; //用来判断是画子阶段,还是初始化阶段 private boolean BoW=false; //画黑白子标子 public static int x,y; //棋子位置 public static void set(String w1) { where=w1; } public static void set(int x1,int y1) { //设置放子位置 x=x1-26; y=y1-48; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (where == "init") { setBackground(new Color(235,199,88)); for(int i=1;i<=9;i++) { g.drawLine((550/10)*i,(550/10),(550/10)*i,(550/10)*9); g.drawLine((550/10),(550/10)*i,(550/10)*9,(550/10)*i); } //IgoPanel.set("NULL"); } if ( where == "put" ) { //画棋子 if (BoW) { g.setColor(new Color(255,255,255)); g.fillOval(x,y,50,50); BoW=!BoW; IgoPanel.set("NULL"); } else{ g.setColor(new Color(0,0,0)); g.fillOval(x,y,50,50); BoW=!BoW; IgoPanel.set("NULL"); } } if ( where == "NULL") {} } }