/* 游戏面板
* 鼠标监听
* 画棋子、棋盘
*/
//导入区
import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
class MainPanel extends JPanel implements MouseListener{
//private成员区
private int width;
private int height;
private Chessboard cm;
private WinTable wt = new WinTable();
//构造器
MainPanel( Chessboard cm){
this.cm = cm;
this.width = cm.getWidth();
this.height = cm.getHeight();
addMouseListener(this);
}
//设置模式
public void setModel( Chessboard cm){
this.cm = cm;
this.width = cm.getWidth();
this.height = cm.getHeight();
}
//paint区域
public void paintComponent( Graphics g){
String temp_str = "";
int temp_int = 0;
super.paintComponent(g);
g.setColor(Color.black);
//画棋盘、标数字
for(int y = 1; y <= this.height; y++){
g.drawLine( 20 ,y*20 ,this.width*20, y*20);
temp_str=String.valueOf(y);
g.drawString( temp_str, this.width*20+5, y*20+5);
}
for(int x = 1; x <= this.width; x++){
g.drawLine( x*20, 20, x*20, 20*this.height);
temp_str = String.valueOf(x);
g.drawString( temp_str, x*20-5, this.height*20+15);
}
//画光标
g.setColor(Color.white);
g.drawRect((WinTable.a+1)*20-8 , (WinTable.b+1)*20-8 , 16 , 16 );
//画棋子
for(int j = 0; j < this.height; j++){
for(int i = 0; i < this.width; i++){
temp_int = cm.getGrid()[i][j];
if(temp_int == 1 || temp_int == 2)
draw( g, i, j, temp_int);
}
}
}
//画棋子
public void draw( Graphics g, int i, int j, int v){
//计算棋子的实际坐标
int x = 20 * i + 20;
int y = 20 * j + 20;
//draw the black chess
if( v == 1){
g.setColor( Color.black);
g.fillOval( x-8, y-8, 16, 16);
}
//draw the white chess
if( v == 2){
g.setColor( Color.red);
g.fillOval( x-8, y-8, 16, 16);
}
}
//调用cm的悔棋方法
public void regret(){
cm.regret();
repaint();
}
/*public JPanel getJP(){
return this;
}
*/
//鼠标事件
public void mousePressed( MouseEvent evt){
//控制区域
if(evt.getX() < width * 20 + 10 && evt.getY()< height * 20 + 10){
int x = (evt.getX() - 10) / 20;
int y = (evt.getY() - 10) / 20;
if( evt.getModifiers() == MouseEvent.BUTTON1_MASK && !wt.isWin()){
cm.play(x,y);
repaint();
//这是玩家或对战模式下获胜
if( wt.isWin()){
cm.showMsg(this);
evt.consume();//取消鼠标事件
repaint();
}
}
}
}
public void mouseClicked(MouseEvent evt){}
public void mouseReleased(MouseEvent evt){}
public void mouseEntered(MouseEvent mouseevt){}
public void mouseExited(MouseEvent mouseevent){}
}