用java编写个五子棋的程序

要能运行的,
2024年11月20日 00:49
有2个网友回答
网友(1):

很大,需要的话,加我的5 - 6 - 4 - 7 - 2 - 2 - 7 - 2 - 7

网友(2):

import java.awt.*;
import java.awt.event.*;
import java.awt.color.*;
public class wuzi
{
public static void main(string[] args)
{
frame myframe =new frame("五子棋");
}
}

class frame extends frame implements actionlistener
{
menubar menubar;
menu menugame;

menuitem menugamestart;
menuitem menugameclose;

mycanvas canvas;

frame(string t)
{
super(t);

canvas = new mycanvas(this);

menubar = new menubar();
menugame = new menu("游戏");
menugamestart = new menuitem("开始");
menugameclose = new menuitem("结束");

menugame.add(menugamestart);
menugame.add(menugameclose);

menugame.addactionlistener(this);
menugamestart.addactionlistener(this);
menugameclose.addactionlistener(this);

menubar.add(menugame);

this.setmenubar(menubar);
this.add(canvas);

addwindowlistener(new windowadapter()
{
public void windowclosing(windowevent e)
{
system.exit(0);
}
}
);
this.pack();
this.show();
}
public void actionperformed(actionevent e)

{

if(e.getsource() == menugamestart)
{

}
else if(e.getsource()==menugameclose)
{
this.dispose();
}

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.white);
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.red);
}
g.filloval(j*24+21,i*24+21,18,18);
}

}

}

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();

}
}