importjava.awt.*;
importjava.awt.event.*;
classChessPadextendsPanelimplementsMouseListener,ActionListener{
intx=-1,y=-1,chessColor=1;
Buttonbutton=newButton(重新开局);
TextFieldtext_1=newTextField(请下黑子棋),
text_2=newTextField();
ChessPad(){
setSize(440,440);
setLayout(null);setBackground(Color.orange);
addMouseListener(this);
add(button);button.setBounds(10,5,60,26);
button.addActionListener(this);
add(text_1);
text_1.setBounds(90,5,90,24);
add(text_2);
text_2.setBounds(290,5,90,24);
text_1.setEditable(false);
text_2.setEditable(false);
}
publicvoidpaint(Graphicsg){
for(inti=40;i<=380;i=i+20){
g.drawLine(40,i,400,i);
}
g.drawLine(40,400,400,400);
for(intj=40;j<=380;j=j+20){
g.drawLine(j,40,j,400);
}
g.drawLine(400,40,400,400);
g.fillOval(97,97,6,6);
g.fillOval(337,97,6,6);
g.fillOval(97,337,6,6);
g.fillOval(337,337,6,6);
g.fillOval(217,217,6,6);
}
publicvoidmousePressed(MouseEvente){
if(e.getModifiers()==InputEvent.BUTTON1_MASK){
x=(int)e.getX();
y=(int)e.getY();
ChessPoint_blackchesspoint_black=newChessPoint_black(this);
ChessPoint_whitechesspoint_white=newChessPoint_white(this);
inta=(x+10)/20,
b=(y+10)/20;
if(x/2019||y/20>19){}
else{
if(chessColor==1){
this.add(chesspoint_black);
chesspoint_black.setBounds(a*20-10,b*20-10,20,20);
chessColor=chessColor*(-1);
text_2.setText(请白棋下子);
text_1.setText();
}
elseif(chessColor==-1){
this.add(chesspoint_white);
chesspoint_white.setBounds(a*20-10,b*20-10,20,20);
chessColor=chessColor*(-1);
text_1.setText(请黑棋下子);
text_2.setText();
}
}
}
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){}
publicvoidactionPerformed(ActionEvente){
this.removeAll();chessColor=1;
add(button);button.setBounds(10,5,60,26);
add(text_1);text_1.setBounds(90,5,90,24);
text_2.setText();text_1.setText(请黑棋下子);
add(text_2);text_2.setBounds(290,5,90,24);
}
}
classChessPoint_blackextendsCanvasimplementsMouseListener{
ChessPadchesspad=null;
ChessPoint_black(ChessPadp){
setSize(20,20);chesspad=p;
addMouseListener(this);
}
publicvoidpaint(Graphicsg){
g.setColor(Color.black);
g.fillOval(0,0,20,20);
}
publicvoidmousePressed(MouseEvente){
if(e.getModifiers()==InputEvent.BUTTON3_MASK){
chesspad.remove(this);
chesspad.chessColor=1;
chesspad.text_2.setText();
chesspad.text_1.setText(请黑棋下子);
}
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){
if(e.getClickCount()>=2)chesspad.remove(this);
}
}
classChessPoint_whiteextendsCanvasimplementsMouseListener{
ChessPadchesspad=null;
ChessPoint_white(ChessPadp){
setSize(20,20);addMouseListener(this);
chesspad=p;
}
publicvoidpaint(Graphicsg){
g.setColor(Color.white);
g.fillOval(0,0,20,20);
}
publicvoidmousePressed(MouseEvente){
if(e.getModifiers()==InputEvent.BUTTON3_MASK){
chesspad.remove(this);
chesspad.chessColor=-1;
chesspad.text_2.setText(请白棋下子);
chesspad.text_1.setText();
}
}
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){
if(e.getClickCount()>=2)chesspad.remove(this);
}
}
publicclassChessextendsFrame{
ChessPadchesspad=newChessPad();
Chess(){
setVisible(true);
setLayout(null);
Labellabel=newLabel(单击左键下棋子,双击吃棋子,用右键单击棋子悔棋,Label.CENTER);
add(label);
label.setBounds(70,55,440,26);
label.setBackground(Color.orange);
add(chesspad);
chesspad.setBounds(70,90,440,440);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
pack();setSize(600,550);
}
publicstaticvoidmain(Stringargs[]){
Chesschess=newChess();
}
}