import java.awt.*;import java.awt.event.*;import java.awt.color.*;class wuzi { public static void main(String[] args) { frame myFrame =new frame("五子棋");} } class frame extends Frame implements ActionListener{ MenuBar mbar=new MenuBar();Menu mgame=new Menu("选项");Men...
怎么Java实现鼠标点击的五子棋啊 ?各位大侠 帮帮忙
给你发了,记得给个答复啊。O(∩_∩)O~2010-06-24
import java.awt.*;
import java.awt.event.*;
import java.awt.color.*;
class wuzi {
public static void main(String[] args) {
frame myFrame =new frame("五子棋");
}
}
class frame extends Frame implements ActionListener{
MenuBar mbar=new MenuBar();
Menu mgame=new Menu("选项");
Menu mhelp=new Menu("帮助");
MenuItem mstart=new MenuItem("开始 ");
MenuItem mclose=new MenuItem("结束");
MenuItem mabout=new MenuItem("关于");
myCanvas canvas;
frame(String t) {
super(t);
canvas = new myCanvas(this);
mgame.add(mstart);
mgame.add(mclose);
mhelp.add(mabout);
mgame.addActionListener(this);
mstart.addActionListener(this);
mclose.addActionListener(this);
mhelp.addActionListener(this);
mabout.addActionListener(this);
mbar.add(mgame);
mbar.add(mhelp);
this.setMenuBar(mbar);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
this.add(canvas);
this.pack();
this.show();
this.setLocation(380,100);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mstart)
{
repaint();
}
else if(e.getSource()==mclose){
this.dispose();
System.exit(0);
}
if(e.getSource()==mabout){
about frm=new about();
}
pack();
}
}
class chessman{
private boolean isExist;
private boolean isColor;
public chessman(boolean isExist){
this.isExist = isExist ;
}
boolean getChessExist() {
return isExist;
}
boolean getChessColor() {
return isColor;
}
void setChessExist(boolean m){
isExist = m;
}
void setChessColor(boolean m) { //设置棋的颜色
isColor = m;
}
}
class myCanvas extends Canvas implements MouseListener {
frame myFrame;
//保存棋子
private chessman chessMan[][] = new chessman[19][19];
int mx,my;//获取位置
int x,y;
boolean state = true;
myCanvas(frame myFrame) {
this.myFrame = myFrame;
setBackground(Color.cyan);
addMouseListener(this);
for(int i=0;i<19;i++)
for(int j=0;j<19;j++)
chessMan[i][j] = new chessman(false);
}
public void start() {
for(int i=0;i<19;i++)
for(int j=0;j<19;j++)
chessMan[i][j].setChessExist(false);
}
public void paint(Graphics g) {//画线
Dimension size = this.getSize();
g.drawRect(0,0,size.width-1,size.height-1);
g.draw3DRect(1,1,size.width-3,size.height-3,true);
for(int i=0;i<19;i++)
g.drawLine(30,i*24+30,462,i*24+30);
for(int j=0;j<19;j++)
g.drawLine(j*24+30,30,j*24+30,462);
x=(int)((mx-20)/24);
y=(int)((my-20)/24); //计算落子位置
if(!chessMan[y][x].getChessExist()&&(mx>=18||my>=18)){
chessMan[y][x].setChessExist(true);
chessMan[y][x].setChessColor(state);
state = !state;
}
for(int i =0;i<19;i++)
for(int j=0;j<19;j++){
if(chessMan[i][j].getChessExist())
{
if(chessMan[i][j].getChessColor()==true){
g.setColor(Color.black); //默认黑棋先下
}
else{
g.setColor(Color.white);
}
g.fillOval(j*24+21,i*24+21,18,18);
// whowin(x, y) ;
}
}
}
/* public void whowin(int x, int y) {
int m1 = 0, m2 = 0, m3 = 0, m4 = 0;
for (int i = 1; i < 5; i++) {//判断-------向左-------
if (x - i > 0 && chessMan[x - i][y] == chessMan[x][y]) {
m1++;
}
}
for (int j = 1; j < 5; j++) {//判断-------向右-------
if (x + j < 20 && chessMan[x + j][y] == chessMan[x][y]) {
m1++;
}
}
//判断输赢------shu线
for (int i = 1; i < 5; i++) {//判断-------向下-----------
if (y + i < 20 && chessMan[x][y + i] == chessMan[x][y]) {
m2++;
}
}
for (int i = 1; i < 5; i++) {//判断-------向上------------
if (y - i > 0 && chessMan[x][y - i] == chessMan[x][y]) {
m2++;
}
}
//判断输赢------\线
for (int i = 1; i < 5; i++) {//判断-------向左上-------------
if (x - i > 0 && y - i > 0 && chessMan[x - i][y - i] == chessMan[x][y]) {
m3++;
}
}
for (int i = 1; i < 5; i++) {//判断-------向右下-------------
if (x + i < 20 && y + i < 20 && chessMan[x + i][y + i] == chessMan[x][y]) {
m3++;
}
}
//判断输赢------/线
for (int i = 1; i < 5; i++) {//判断-------向左下------------
if (x - i > 0 && y + i < 20 && chessMan[x - i][y + i] == chessMan[x][y]) {
m4++;
}
}
for (int i = 1; i < 5; i++) {//判断-------向右上--------------
if (y - i > 0 && x + i < 20 && chessMan[x + i][y - i] == chessMan[x][y]) {
m4++;
}
}
//***************最后判断输赢********************
if (m1 > 3 || m2 > 3 || m3 > 3 || m4 > 3) {
if (chessMan[x][y] == ) {//----因为下棋后将棋子颜色改为对方的颜色,所以这里互相调一下---
JOptionPane.showMessageDialog(this, "hei-----win");
isgamealive = 0;
}
if (chessMan[x][y] == false) {
JOptionPane.showMessageDialog(this, "bai-----win");
isgamealive = 0;
}
}
}*/
public Dimension getPreferredSize() {
return new Dimension(492,492); //返回图形大小
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){
mx = e.getX();
my = e.getY();
repaint();
}
}
class about extends Frame implements WindowListener //设置关于
{
about() {
this.setSize(300,170);
this.setTitle("关于五子棋游戏");
this.addWindowListener(this);
this.setVisible(true);
this.setLocation(400,200);
this.setResizable(false);
this.setLayout(new FlowLayout());
Label text1 =new Label();
Label text2 =new Label();
Label text3=new Label();
text1.setText("游戏介绍:");
text1.setFont(new Font("黑体",Font.ITALIC,25));
text1.setAlignment(text1.CENTER);
text1.setVisible(true);
text2.setText("横排,竖排,斜排,谁先放到5个就赢。");
text2.setFont(new Font("黑体",Font.PLAIN,15));
text2.setAlignment(text1.LEFT);
text2.setVisible(true);
text3.setText("制作者:0957207吴君");
text3.setFont(new Font("楷体",Font.PLAIN,15));
text3.setVisible(true);
this.add(text1,"North");
this.add(text2,"West");
this.add(text3,"South");
this.show(true);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){
}
public void windowClosing(WindowEvent e){
this.dispose();
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}2010-06-28