//import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ChessPad extends Panel implements MouseListener,ActionListener
{int x=-1,y=-1,chesscolor=1;
Button button=new Button("重新开局");
TextField text_1=new TextField("请黑棋下子"),
text_2=new TextField();
ChessPad()
{setSize(440,440);
setLayout(null);
setBackground(Color.pink);
addMouseListener(this);
add(button);
button.setBounds(10, 5, 670, 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);
}
public void paint(Graphics g)
{for(int i=40;i<=380;i=i+20)
{g.drawLine(40,i, 400, i);}
g.drawLine(40, 400, 400, 400);
for(int j=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);
}
public void mousePressed(MouseEvent e)
{if(e.getModifiers()==InputEvent.BUTTON1_MASK)
{x=(int)e.getX();
y=(int)e.getY();
ChessPoint_black chesspoint_black=new ChessPoint_black(this);
ChessPoint_white chesspoint_white=new ChessPoint_white(this);
int a=(x+10)/20,b=(y+10)/20;
if(x/20<2||y/20<2||x/20>19||y/20>19)
{}
else{if(chesscolor==1)
{this.add(chesspoint_black);
chesspoint_black.setBounds(a*20-7,b*20-7,16,16);
chesscolor=chesscolor*(-1);
text_2.setText("请白棋下子");
text_1.setText("");
}
else if(chesscolor==-1)
{this.add(chesspoint_white);
chesspoint_white.setBounds(a*20-7,b*20-7,16,16);
chesscolor=chesscolor*(-1);
text_1.setText("请黑棋下子");
text_2.setText("");
}
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{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);
}
}
class ChessPoint_black extends Canvas implements MouseListener
{ChessPad chesspad=null;
ChessPoint_black(ChessPad p)
{setSize(20,20);
chesspad=p;
addMouseListener(this);
}
public void paint(Graphics g)
{g.setColor(Color.black);
g.fillOval(0,0,14,14);
}
public void mousePressed(MouseEvent e)
{if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{chesspad.remove(this);
chesspad.chesscolor=1;
chesspad.text_2.setText("");
chesspad.text_1.setText("请黑棋下子");
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
class ChessPoint_white extends Canvas implements MouseListener
{ChessPad chesspad=null;
ChessPoint_white(ChessPad p)
{setSize(20,20);
chesspad=p;
addMouseListener(this);
}
public void paint(Graphics g)
{g.setColor(Color.white);
g.fillOval(0,0,14,14);
}
public void mousePressed(MouseEvent e)
{if(e.getModifiers()==InputEvent.BUTTON3_MASK)
{chesspad.remove(this);
chesspad.chesscolor=-1;
chesspad.text_2.setText("请白棋下子");
chesspad.text_1.setText("");
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
public class Chess extends Frame
{ChessPad chesspad=new ChessPad();
Chess()
{setSize(600,600);
setVisible(true);
setLayout(null);
Label label=new Label("单击下棋子,右击棋子悔棋",Label.CENTER);
add(label);
label.setBounds(70,55, 440, 26);
label.setBackground(Color.orange);
add(chesspad);
chesspad.setBounds(70, 90, 440, 440);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public static void main(String args[])
{Chess chess=new Chess();
}
}
周楷雯
- 粉丝: 93
- 资源: 1万+
最新资源
- VMware 是一款功能强大的虚拟化软件,它允许用户在一台物理计算机上同时运行多个操作系统
- 31万条全国医药价格与采购数据.xlsx
- SQL注入详解,SQL 注入是一种常见的网络安全漏洞,攻击者通过在输入数据中插入恶意的 SQL 语句,欺骗应用程序执行这些恶意语句,从而获取、修改或删除数据库中的数据,甚至控制数据库服务器
- 用C语言实现哈夫曼编码:从原理到实现的详细解析
- py爱心代码高级粒子!!
- 爱心代码高级,拿去博得喜欢的人的欢心吧
- DZ-ID005-V1.0-20240911-原理图.zip
- 用C语言实现字符串去重功能
- java实现对ZKFBioFS200半导体指纹采集器对接
- NO.3学习样本,请参考第3章的内容配合学习使用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈