public boolean handleEvent(Event evt) { switch (evt.id) { case Event.WINDOW_DESTROY: return true; case Event.KEY_PRESS: if (evt.key=='\n'){ mp_game.name=name.getText(); if (!mp_game.name.equals("")){ ok=true; hide(); } name.setText(""); } default: return super.handleEvent(evt); } } }
import java.awt.*; import java.io.*; import java.applet.*; import java.applet.Applet; import java.net.URL; import java.net.Socket; import java.net.InetAddress; import java.net.MalformedURLException;public class netgame extends Applet implements Runnable{ ControlPanel Control; GridBagLayout gridbag = new GridBagLayout(); String name=""; int otherId; public static int travel[]; int final_die[]=new int[21]; public final int r = 34; // 半径 int turn=1; // 1: blue, 2:red int myturn=1; // 1 or 2; int mode=0; static int play=1; static int calling=-2; static int idle=0; static int called=-1; public int height=0; public int width=0; Image offscreen; Graphics offGraph; Image pic; public static Point center[]=new Point[21];
// 0-nothing 1-blue 2-red public static int state[]=new int[21]; public static int neighbor[][]=new int[21][4]; Point now_point=null; int now_id=-1; Socket sock; DataInputStream datain; DataOutputStream dataout; Thread m_mygame = null; boolean m_fAllLoaded=false; AudioClip a_one,a_back,a_eat,a_eatted,a_win,a_start,a_your_turn,a_lose; public void ini_global(){ myturn=1; mode=idle; otherId=-1; turn=1; for (int i=0;i<21;i++){ center[i]=new Point(0,0);} center[0].x=190; center[0].y=21;
public void run(){ while (true){ try{ String msg=null;
msg=datain.readLine(); // 接收welcome msg
if (msg.charAt(0)=='$') // 如果接收到命令 { switch (msg.charAt(1)){
// 接收到移动棋子 case 'm': receive_state(msg.substring(2)); break;
case 'w': Control.do_who(msg.substring(2)); break;
// 有人 call case 'r': Control.u_button(0,1,1,0,0); otherId=Integer.parseInt(msg.substring(2)); mode=called; break;
// 别人接受 case 'y': Control.u_button(1,0,0,1,1); Control.Message.appendText("** Your partner accepted your request.\r\n"); Control.Message.appendText("** Starting the game.\r\n"); Control.Message.appendText("** You are blue (the first player).\r\n"); Control.Message.appendText("** It is your turn now.\r\n"); mode=play; break;
// 别人不接受 case 'u': Control.u_button(1,0,0,0,0); Control.Message.appendText("** Your partner rejected your request.\r\n"); mode=idle; break;
// user not available case 's': Control.u_button(1,0,0,0,0); Control.Message.appendText("** Your partner is busy.\r\n"); mode=idle; break;
// 对方要 restart case 'a': Control.u_button(1,0,0,1,1); Control.Message.appendText("** Your partner requested to restart.\r\n"); init_circle(); break;
// 离开棋桌 case 'b': Control.u_button(1,0,0,0,0); myturn=1; Control.Message.appendText("** Your partner quitted.\r\n"); init_circle(); mode=idle; break; default: break; } } else { Control.Message.appendText(msg+"\r\n"); } } catch (Exception e){}; } } public void receive_state(String x){ int i; //a_one.play(); // 对家下子了 for (i=0;i<21;i++){ if (x.charAt(i)=='1') state[20-i]=1; else if (x.charAt(i)=='0') state[20-i]=0; else if (x.charAt(i)=='2') state[20-i]=2; } repaint(); Control.Message.appendText("** Your Turn.\r\n"); turn=turn%2+1; } public void message(String msg){ try{ m_mygame.suspend(); dataout.writeBytes(msg); dataout.flush(); m_mygame.resume(); } catch(Exception e){ System.out.println("Send Message error : "+msg); } } // mydraw Paint Handler public void paint_circle(Graphics g,int x,int y,int color,int move){ if (move==0){ g.setColor(new Color(171,118,46)); //画阴影 g.fillOval(x-2,y+8,24,10); } color=Math.abs(color); if (color==1) g.setColor(Color.blue); else if (color==2) g.setColor(Color.red); int a,b,c,d,e; a=x-r/2;b=y-r/2; c=x-2*r/5;d=y-2*r/5; e=4*r/5; g.fillOval(a,b,r,r); g.setColor(Color.white); g.drawArc(c,d,e,e,100,70); g.drawArc(c,d,e+1,e+1,100,70); g.drawArc(c,d,e-1,e-1,100,70); g.drawArc(c,d,e+2,e+2,100,70); g.setColor(Color.lightGray); g.drawArc(c,d,e,e,286,35); g.drawArc(c,d,e-1,e-1,286,35); g.drawArc(c,d,e-2,e-2,286,33); } public void paint(Graphics g){ offGraph.drawImage(pic,0,0,this); // 画背景 int now=-1; for (int i=0;i<21;i++){ if (state[i]>0){ paint_circle(offGraph,center[i].x,center[i].y,state[i],0); } else if (state[i]<0){ now=i; //paint_circle(g,now_point.x,now_point.y,-state[i],1); } } // now moving if (now!=-1){ paint_circle(offGraph,now_point.x,now_point.y,-state[now],1); } g.drawImage(offscreen,250,5,this); } public void update(Graphics g){ paint(g); } public int find_which(int x,int y){ // return -1 not found int i; // return i 为 棋子之号码 if (y<100){ for (i=0;i<=3;i++){ if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2) return i; } return -1; } else if (y<150){ for (i=4;i<=6;i++){ if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2) return i; } return -1; } else if (y<210){ for (i=7;i<=13;i++){ if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2) return i; } return -1; } else if (y<270){ for (i=14;i<=16;i++){ if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2) return i; } return -1; } else{ for (i=17;i<=20;i++){ if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2) return i; } } return -1; } public int is_neighbor(int x,int y){ int i; for (i=0;i<4;i++){ if (neighbor[x][i]==y) return 1; } return 0; } public boolean consider(int color,int id){ int die_list[]=new int[21]; boolean game_over=false; int i,j; travel=new int[21]; for (i=0;i<21;i++){ travel[i]=-1; // -1 not travel die_list[i]=0; final_die[i]=0; } // 0 no chess 1 color 1 , 2 color 2 travel[id]=state[id]; boolean has_die=false; for (i=3;i>=0;i--){ if(die(color,neighbor[id][i],die_list)==1){ for (j=0;j<21;j++){ if (die_list[j]==1) final_die[j]=1; } has_die=true; } } int alive_sum=0;
for (i=0;i<21;i++){ if (final_die[i]==1){ state[i]=0; } if (state[i]==(color%2+1)) alive_sum++; } if (alive_sum==0) { Control.Message.appendText("\n** Game Over.\n"); Control.Message.appendText("\n** You win.\n"); game_over=true; } if (game_over) a_win.play(); else if (has_die) a_eat.play(); return game_over; }
public int die(int color,int id,int[] d_list){
if (id==-1||state[id]==color||travel[id]==-2) return 0; if (state[id]==0||travel[id]==-3) return -1;
int i,j; int bak_list[]=new int[21]; for (i=0;i<21;i++){ bak_list[i]=0; } if (travel[id]==-1) travel[id]=-2;
for (i=0;i<4;i++){ if (neighbor[id][i]<0) break;
if (travel[neighbor[id][i]]==-1||travel[neighbor[id][i]]!=-2){ int re=die(color,neighbor[id][i],d_list); if (re==-1){ for (j=0;j<21;j++){ d_list[j]=0; } travel[id]=-3; return -1; } else if (re==1){ for (j=0;j<21;j++){ if (d_list[j]!=0){ bak_list[j]=1; } } } } } for (i=0;i<21;i++){ d_list[i]=bak_list[i]; } d_list[id]=1; travel[id]=-1; return 1; } public boolean mouseDown(Event evt, int x, int y){ x-=250;y-=5; if (now_point==null&&turn==myturn&&mode==play){ int which=find_which(x,y); if (which==-1) return true; else if (state[which]==0||state[which]!=myturn){ if (state[which]!=myturn) a_back.play(); return true; } else { now_point=new Point(x,y); now_id=which; state[which]=-state[which]; } } else { } return true; } public boolean mouseUp(Event evt, int x, int y){ x-=250;y-=5; int oo; if (now_point!=null){ oo=find_which(x,y); if (oo!=-1){ if (state[oo]==0&&(is_neighbor(oo,now_id)==1)){ a_one.play(); state[oo]=-state[now_id]; state[now_id]=0; now_point=null;now_id=-1; if (!consider(turn,oo)){ turn=(turn%2)+1; } else { turn=0; } repaint(); send_state(); return true; } } state[now_id]=-state[now_id]; now_point=null; now_id=-1; a_one.play(); repaint(); } return true; } public void send_state(){ try{ StringBuffer sss=new StringBuffer(); sss.append("$m"); for (int i=0;i<21;i++){ if (state[i]==1) sss.append('1'); else if (state[i]==2) sss.append('2'); else if (state[i]==0) sss.append('0'); } sss.append("\n"); message(sss.toString()); Control.Message.appendText("** The other's Turn **\r\n"); }catch (Exception e){}; } public boolean mouseDrag(Event evt, int x, int y){ x-=250;y-=5; if (now_point!=null){ now_point.x=x;now_point.y=y; repaint(); } return true; }
javac package_name1/package_name2/*.java
如果没有定义包,则必须在同一目录中,javac *.java
要不,搜索论坛看看,应该有好多~~
{
// mydrawFrame constructor
//--------------------------------------------------------------------------
public gameframe(String str)
{
super (str);
} public static void constrain(Container container,Component component,
int grid_x,int grid_y,int grid_width,int grid_height,
int fill,int anchor,double weight_x,double weight_y,
int top,int left,int bottom,int right){
GridBagConstraints c=new GridBagConstraints();
c.gridx=grid_x;c.gridy=grid_y;
c.gridwidth=grid_width;c.gridheight=grid_height;
c.fill=fill;c.anchor=anchor;
c.weightx=weight_x;c.weighty=weight_y;
if (top+bottom+left+right>0){
c.insets=new Insets(top,left,bottom,right);
}
((GridBagLayout)container.getLayout()).setConstraints(component,c);
container.add(component); } public static void constrain(Container container,Component component,
int grid_x,int grid_y,int grid_width,int grid_height){ constrain(container,component,grid_x,grid_y,
grid_width,grid_height,GridBagConstraints.NONE,
GridBagConstraints.NORTHWEST,
0.0,0.0,0,0,0,0);
} public static void constrain(Container container,Component component,
int grid_x,int grid_y,int grid_width,int grid_height,
int top,int left,int bottom,int right){
constrain(container,component,grid_x,grid_y,
grid_width,grid_height,GridBagConstraints.NONE,
GridBagConstraints.NORTHWEST,
0.0,0.0,top,left,bottom,right);
} public boolean handleEvent(Event evt)
{
switch (evt.id)
{
case Event.WINDOW_DESTROY:
dispose();
System.exit(0);
return true; default:
return super.handleEvent(evt);
}
}
}
import netgame;
import gameframe;class loginframe extends gameframe {
boolean ok=false;
netgame mp_game;
Label msg;
Button b_ok;
TextField name;
GridBagLayout gridbag=new GridBagLayout(); public loginframe(netgame mp,String title) {
super(title);
msg = new Label("Please enter user name!", Label.CENTER);
b_ok=new Button("OK");
name=new TextField(10);
mp_game=mp; setLayout(gridbag); setBackground(new Color(172,148,136));
constrain(this,msg,0,0,10,1,GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTHWEST,1.0,0.0,5,3,2,3);
constrain(this,name,0,10,8,1,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,1.0,0.0,0,3,2,3);
constrain(this,b_ok,4,11,4,1,GridBagConstraints.NONE,
GridBagConstraints.CENTER,1.0,0.0,0,3,5,3); name.setBackground(Color.white);
name.setForeground(Color.black);
name.setFont(new Font("System",Font.BOLD,13));
//Label.setFont(new Font("System",Font.BOLD,12)); pack();
show();
} public boolean action(Event evt,Object what){
if (evt.target instanceof Button){
mp_game.name=name.getText();
if (!mp_game.name.equals("")){
ok=true;
hide();
}
}
return false;
}
public boolean handleEvent(Event evt) {
switch (evt.id) {
case Event.WINDOW_DESTROY:
return true;
case Event.KEY_PRESS:
if (evt.key=='\n'){
mp_game.name=name.getText();
if (!mp_game.name.equals("")){
ok=true;
hide();
}
name.setText("");
}
default:
return super.handleEvent(evt);
}
}
}
import java.io.*;
import java.applet.*;
import java.applet.Applet;
import java.net.URL;
import java.net.Socket;
import java.net.InetAddress;
import java.net.MalformedURLException;public class netgame extends Applet implements Runnable{ ControlPanel Control;
GridBagLayout gridbag = new GridBagLayout(); String name="";
int otherId; public static int travel[];
int final_die[]=new int[21]; public final int r = 34; // 半径
int turn=1; // 1: blue, 2:red
int myturn=1; // 1 or 2;
int mode=0;
static int play=1;
static int calling=-2;
static int idle=0;
static int called=-1;
public int height=0;
public int width=0; Image offscreen;
Graphics offGraph;
Image pic; public static Point center[]=new Point[21];
// 0-nothing 1-blue 2-red
public static int state[]=new int[21];
public static int neighbor[][]=new int[21][4];
Point now_point=null;
int now_id=-1; Socket sock;
DataInputStream datain;
DataOutputStream dataout;
Thread m_mygame = null;
boolean m_fAllLoaded=false; AudioClip a_one,a_back,a_eat,a_eatted,a_win,a_start,a_your_turn,a_lose; public void ini_global(){
myturn=1;
mode=idle;
otherId=-1;
turn=1;
for (int i=0;i<21;i++){ center[i]=new Point(0,0);} center[0].x=190;
center[0].y=21;
center[1].x=131;
center[1].y=32;
center[2].x=254;
center[2].y=35;
center[3].x=190;
center[3].y=81;
center[4].x=32;
center[4].y=129;
center[5].x=190;
center[5].y=127;
center[6].x=346;
center[6].y=129;
center[7].x=22;
center[7].y=190;
center[8].x=85;
center[8].y=190;
center[9].x=130;
center[9].y=190;
center[10].x=190;
center[10].y=190;
center[11].x=255;
center[11].y=190;
center[12].x=300;
center[12].y=190;
center[13].x=358;
center[13].y=190;
center[14].x=35;
center[14].y=252;
center[15].x=190;
center[15].y=250;
center[16].x=345;
center[16].y=250;
center[17].x=190;
center[17].y=295;
center[18].x=130;
center[18].y=345;
center[19].x=253;
center[19].y=343;
center[20].x=190;
center[20].y=355;
neighbor[0][0]=1;
neighbor[0][1]=2;
neighbor[0][2]=3;
neighbor[0][3]=-1;
neighbor[1][0]=0;
neighbor[1][1]=3;
neighbor[1][2]=4;
neighbor[1][3]=-1;
neighbor[2][0]=0;
neighbor[2][1]=3;
neighbor[2][2]=6;
neighbor[2][3]=-1;
neighbor[3][0]=0;
neighbor[3][1]=1;
neighbor[3][2]=2;
neighbor[3][3]=5;
neighbor[4][0]=1;
neighbor[4][1]=7;
neighbor[4][2]=8;
neighbor[4][3]=-1;
neighbor[5][0]=3;
neighbor[5][1]=9;
neighbor[5][2]=10;
neighbor[5][3]=11;
neighbor[6][0]=2;
neighbor[6][1]=12;
neighbor[6][2]=13;
neighbor[6][3]=-1;
neighbor[7][0]=4;
neighbor[7][1]=8;
neighbor[7][2]=14;
neighbor[7][3]=-1;
neighbor[8][0]=4;
neighbor[8][1]=7;
neighbor[8][2]=9;
neighbor[8][3]=14;
neighbor[9][0]=5;
neighbor[9][1]=8;
neighbor[9][2]=10;
neighbor[9][3]=15;
neighbor[10][0]=5;
neighbor[10][1]=9;
neighbor[10][2]=11;
neighbor[10][3]=15;
neighbor[11][0]=5;
neighbor[11][1]=10;
neighbor[11][2]=12;
neighbor[11][3]=15;
neighbor[12][0]=6;
neighbor[12][1]=11;
neighbor[12][2]=13;
neighbor[12][3]=16;
neighbor[13][0]=6;
neighbor[13][1]=12;
neighbor[13][2]=16;
neighbor[13][3]=-1;
neighbor[14][0]=7;
neighbor[14][1]=8;
neighbor[14][2]=18;
neighbor[14][3]=-1;
neighbor[15][0]=9;
neighbor[15][1]=10;
neighbor[15][2]=11;
neighbor[15][3]=17;
neighbor[16][0]=12;
neighbor[16][1]=13;
neighbor[16][2]=19;
neighbor[16][3]=-1;
neighbor[17][0]=15;
neighbor[17][1]=18;
neighbor[17][2]=19;
neighbor[17][3]=20;
neighbor[18][0]=14;
neighbor[18][1]=17;
neighbor[18][2]=20;
neighbor[18][3]=-1;
neighbor[19][0]=16;
neighbor[19][1]=17;
neighbor[19][2]=20;
neighbor[19][3]=-1;
neighbor[20][0]=17;
neighbor[20][1]=18;
neighbor[20][2]=19;
neighbor[20][3]=-1;
init_circle();
} public void init_circle(){
int i;
turn=1;
for (i=0;i<21;i++){
state[i]=0;
}
for (i=0;i<=6;i++){
state[i]=myturn%2+1;
}
for (i=14;i<=20;i++){
state[i]=myturn;
}
state[5]=0;
state[15]=0;
repaint();
} public void destroy(){
message("$b\n");
stop();
} loginframe namedlg; public void init(){
height=380;
width=380; namedlg = new loginframe(this,"User Name"); // Loading Sound a_start = getAudioClip(getCodeBase(),"start.au");
a_one = getAudioClip(getCodeBase(),"one.au");
a_back = getAudioClip(getCodeBase(),"back.au");
a_your_turn = getAudioClip(getCodeBase(),"your_turn.au");
a_eat = getAudioClip(getCodeBase(),"eat.au");
a_eatted = getAudioClip(getCodeBase(),"eatted.au");
a_win = getAudioClip(getCodeBase(),"win.au");
a_lose = getAudioClip(getCodeBase(),"lose.au"); // Loading 棋盘图
pic = getImage(getCodeBase(),"ft.jpg");
offscreen = createImage(width,height);
offGraph = offscreen.getGraphics(); MediaTracker tracker = new MediaTracker(this);
tracker.addImage(pic, 0);
try{
tracker.waitForAll();
m_fAllLoaded = !tracker.isErrorAny();
}
catch (InterruptedException e){
}
} public void start() {
ini_global(); now_id=-1;
//turn=1;
height=380;
width=380;
try {
while (name.equals("")&&!namedlg.ok){
Thread.sleep(2000);
};
}
catch (InterruptedException e){
System.out.println(e.toString());
} setLayout(new BorderLayout(0,0));
Control=new ControlPanel(this);
setBackground(new Color(172,148,136));
add(Control,"west");
Control.reshape(0,5,250,384); if (a_start==null){
play(getCodeBase(),"start.au");
}
else a_start.play(); try{
sock=new Socket(InetAddress.getLocalHost(),4444); //********************************************************************
//* // Change to your own host name
sock=new Socket(InetAddress.getByName("192.168.1.212"),4444);
datain = new DataInputStream(sock.getInputStream());
dataout = new DataOutputStream(sock.getOutputStream());
if (m_mygame == null){
m_mygame=new Thread(this);
m_mygame.start();
} message("$n"+name+"\n");
message("$w\n");
}
catch (Exception e){};
}
public void stop() {
if (m_mygame != null){
m_mygame.stop();
m_mygame = null;
}
Control.Message=null;
Control.Outgoing=null;
Control.who=null;
Control=null;
try{
dataout.close();
datain.close();
sock.close();
}
catch (IOException e){};
}
while (true){
try{
String msg=null;
msg=datain.readLine(); // 接收welcome msg
if (msg.charAt(0)=='$') // 如果接收到命令
{
switch (msg.charAt(1)){
// 接收到移动棋子
case 'm':
receive_state(msg.substring(2));
break;
case 'w':
Control.do_who(msg.substring(2));
break;
// 有人 call
case 'r':
Control.u_button(0,1,1,0,0);
otherId=Integer.parseInt(msg.substring(2));
mode=called;
break;
// 别人接受
case 'y':
Control.u_button(1,0,0,1,1);
Control.Message.appendText("** Your partner accepted your request.\r\n");
Control.Message.appendText("** Starting the game.\r\n");
Control.Message.appendText("** You are blue (the first player).\r\n");
Control.Message.appendText("** It is your turn now.\r\n");
mode=play;
break;
// 别人不接受
case 'u':
Control.u_button(1,0,0,0,0);
Control.Message.appendText("** Your partner rejected your request.\r\n");
mode=idle;
break;
// user not available
case 's':
Control.u_button(1,0,0,0,0);
Control.Message.appendText("** Your partner is busy.\r\n");
mode=idle;
break;
// 对方要 restart
case 'a':
Control.u_button(1,0,0,1,1);
Control.Message.appendText("** Your partner requested to restart.\r\n");
init_circle();
break;
// 离开棋桌
case 'b':
Control.u_button(1,0,0,0,0);
myturn=1;
Control.Message.appendText("** Your partner quitted.\r\n");
init_circle();
mode=idle;
break;
default:
break;
}
}
else {
Control.Message.appendText(msg+"\r\n");
}
}
catch (Exception e){};
}
} public void receive_state(String x){
int i;
//a_one.play(); // 对家下子了
for (i=0;i<21;i++){
if (x.charAt(i)=='1') state[20-i]=1;
else if (x.charAt(i)=='0') state[20-i]=0;
else if (x.charAt(i)=='2') state[20-i]=2;
}
repaint();
Control.Message.appendText("** Your Turn.\r\n");
turn=turn%2+1;
} public void message(String msg){
try{
m_mygame.suspend();
dataout.writeBytes(msg);
dataout.flush();
m_mygame.resume();
}
catch(Exception e){
System.out.println("Send Message error : "+msg);
}
} // mydraw Paint Handler
public void paint_circle(Graphics g,int x,int y,int color,int move){
if (move==0){
g.setColor(new Color(171,118,46)); //画阴影
g.fillOval(x-2,y+8,24,10);
}
color=Math.abs(color);
if (color==1) g.setColor(Color.blue);
else if (color==2) g.setColor(Color.red);
int a,b,c,d,e;
a=x-r/2;b=y-r/2;
c=x-2*r/5;d=y-2*r/5;
e=4*r/5;
g.fillOval(a,b,r,r);
g.setColor(Color.white);
g.drawArc(c,d,e,e,100,70);
g.drawArc(c,d,e+1,e+1,100,70);
g.drawArc(c,d,e-1,e-1,100,70);
g.drawArc(c,d,e+2,e+2,100,70);
g.setColor(Color.lightGray);
g.drawArc(c,d,e,e,286,35);
g.drawArc(c,d,e-1,e-1,286,35);
g.drawArc(c,d,e-2,e-2,286,33);
} public void paint(Graphics g){ offGraph.drawImage(pic,0,0,this); // 画背景 int now=-1;
for (int i=0;i<21;i++){
if (state[i]>0){
paint_circle(offGraph,center[i].x,center[i].y,state[i],0);
}
else if (state[i]<0){
now=i;
//paint_circle(g,now_point.x,now_point.y,-state[i],1);
}
}
// now moving
if (now!=-1){
paint_circle(offGraph,now_point.x,now_point.y,-state[now],1);
}
g.drawImage(offscreen,250,5,this);
} public void update(Graphics g){
paint(g);
} public int find_which(int x,int y){ // return -1 not found
int i; // return i 为 棋子之号码
if (y<100){
for (i=0;i<=3;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
return -1;
}
else if (y<150){
for (i=4;i<=6;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
return -1;
}
else if (y<210){
for (i=7;i<=13;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
return -1;
}
else if (y<270){
for (i=14;i<=16;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
return -1;
}
else{
for (i=17;i<=20;i++){
if (Math.abs(center[i].x-x)<r/2&&Math.abs(center[i].y-y)<r/2)
return i;
}
}
return -1;
}
public int is_neighbor(int x,int y){
int i;
for (i=0;i<4;i++){
if (neighbor[x][i]==y) return 1;
}
return 0;
} public boolean consider(int color,int id){
int die_list[]=new int[21];
boolean game_over=false;
int i,j;
travel=new int[21];
for (i=0;i<21;i++){
travel[i]=-1; // -1 not travel
die_list[i]=0;
final_die[i]=0;
} // 0 no chess 1 color 1 , 2 color 2
travel[id]=state[id];
boolean has_die=false;
for (i=3;i>=0;i--){
if(die(color,neighbor[id][i],die_list)==1){
for (j=0;j<21;j++){
if (die_list[j]==1) final_die[j]=1;
}
has_die=true;
}
}
int alive_sum=0;
for (i=0;i<21;i++){
if (final_die[i]==1){
state[i]=0;
}
if (state[i]==(color%2+1)) alive_sum++;
} if (alive_sum==0) {
Control.Message.appendText("\n** Game Over.\n");
Control.Message.appendText("\n** You win.\n"); game_over=true;
}
if (game_over) a_win.play();
else if (has_die) a_eat.play();
return game_over;
}
if (id==-1||state[id]==color||travel[id]==-2) return 0;
if (state[id]==0||travel[id]==-3) return -1;
int i,j;
int bak_list[]=new int[21];
for (i=0;i<21;i++){
bak_list[i]=0;
}
if (travel[id]==-1) travel[id]=-2;
for (i=0;i<4;i++){
if (neighbor[id][i]<0) break;
if (travel[neighbor[id][i]]==-1||travel[neighbor[id][i]]!=-2){
int re=die(color,neighbor[id][i],d_list);
if (re==-1){
for (j=0;j<21;j++){
d_list[j]=0;
}
travel[id]=-3;
return -1;
}
else if (re==1){
for (j=0;j<21;j++){
if (d_list[j]!=0){
bak_list[j]=1;
}
}
}
}
}
for (i=0;i<21;i++){
d_list[i]=bak_list[i];
}
d_list[id]=1;
travel[id]=-1;
return 1;
} public boolean mouseDown(Event evt, int x, int y){
x-=250;y-=5; if (now_point==null&&turn==myturn&&mode==play){
int which=find_which(x,y); if (which==-1) return true;
else if (state[which]==0||state[which]!=myturn){
if (state[which]!=myturn) a_back.play();
return true;
}
else {
now_point=new Point(x,y);
now_id=which;
state[which]=-state[which];
}
}
else {
}
return true;
} public boolean mouseUp(Event evt, int x, int y){
x-=250;y-=5;
int oo;
if (now_point!=null){
oo=find_which(x,y);
if (oo!=-1){
if (state[oo]==0&&(is_neighbor(oo,now_id)==1)){
a_one.play();
state[oo]=-state[now_id];
state[now_id]=0;
now_point=null;now_id=-1;
if (!consider(turn,oo)){
turn=(turn%2)+1;
}
else {
turn=0;
}
repaint();
send_state();
return true;
}
}
state[now_id]=-state[now_id];
now_point=null;
now_id=-1;
a_one.play();
repaint();
}
return true;
} public void send_state(){
try{
StringBuffer sss=new StringBuffer();
sss.append("$m");
for (int i=0;i<21;i++){
if (state[i]==1) sss.append('1');
else if (state[i]==2) sss.append('2');
else if (state[i]==0) sss.append('0');
}
sss.append("\n");
message(sss.toString());
Control.Message.appendText("** The other's Turn **\r\n");
}catch (Exception e){};
} public boolean mouseDrag(Event evt, int x, int y){
x-=250;y-=5;
if (now_point!=null){
now_point.x=x;now_point.y=y;
repaint();
}
return true;
}
}
class ControlPanel extends Panel{
public netgame mp_game = null;
GridBagLayout bridbag = new GridBagLayout();
TextArea Message;
TextField Outgoing;
Button b_call,b_accept,b_reject,b_exit;
List who;
public Font cf; public ControlPanel(netgame mp){
setBackground(new Color(172,148,136));
mp_game=mp;
setLayout(bridbag);
Message=new TextArea("** Message Panel **\r\n",8,30);
Message.appendText("Welcome " + mp_game.name +"!\r\n");
//Message.appendText("Use $n[name] to Change Your name!!\r\n");
Message.setEditable(false);
Message.setBackground(Color.black);
Message.setForeground(Color.green);
Message.setFont(new Font("System",Font.PLAIN,13));
Outgoing=new TextField(30);
Outgoing.setBackground(Color.darkGray);
Outgoing.setForeground(Color.white);
Outgoing.setFont(new Font("System",Font.PLAIN,13)); b_call=new Button(" Call ");
b_call.setBackground(new Color(172,148,136));
b_call.setForeground(new Color(240,168,56));
b_accept=new Button("Accept");
b_reject=new Button("Reject");
b_exit=new Button(" Exit "); b_call.setFont(new Font("System",Font.PLAIN,13));
b_accept.setFont(new Font("System",Font.PLAIN,13));
b_reject.setFont(new Font("System",Font.PLAIN,13));
b_exit.setFont(new Font("System",Font.PLAIN,13)); who=new List(4,false);
who.setBackground(new Color(220,32,48));
who.setForeground(Color.white);
who.setFont(new Font("System",Font.PLAIN,13));
gameframe.constrain(this,Message, 0, 0,4,10,GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTHWEST,1.0,0.0,0,5,4,5);
gameframe.constrain(this,Outgoing, 0,11,4, 1,GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTHWEST,1.0,0.0,0,5,4,5);
gameframe.constrain(this,b_call, 0,12,1, 1,GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTHWEST,1.0,0.0,0,5,0,0);
gameframe.constrain(this,b_accept, 1,12,1, 1,GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTHWEST,1.0,0.0,0,5,0,0);
gameframe.constrain(this,b_reject,2,12,1, 1,GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTHWEST,1.0,0.0,0,5,0,0);
gameframe.constrain(this,b_exit, 3,12,1, 1,GridBagConstraints.HORIZONTAL,
GridBagConstraints.NORTHWEST,1.0,0.0,0,5,0,5);
gameframe.constrain(this,who, 0,13,4, 4,GridBagConstraints.BOTH,
GridBagConstraints.SOUTH,1.0,1.0,4,5,0,5);
u_button(1,0,0,0,0);
} public void u_button(int a,int b,int c,int d,int e){
if (e==1) b_call.setLabel("Restart"); else b_call.setLabel(" Call ");
if (a==1) b_call.enable(); else b_call.disable();
if (b==1) b_accept.enable(); else b_accept.disable();
if (c==1) b_reject.enable(); else b_reject.disable();
if (d==1) b_exit.enable(); else b_exit.disable();
}
if (evt.target instanceof Button){
String tar=(String)what;
if (tar.equals("Accept")){
do_accept();
} if (tar.equals("Reject")){
do_reject();
}
if (tar.equals(" Call ")){
do_call();
}
if (tar.equals("Restart")){
do_restart();
} if (tar.equals(" Exit ")){
do_byebye();
}
return true;
}
if (evt.target instanceof List){
return true;
}
return false;
} public void do_restart(){
u_button(1,0,0,1,1);
mp_game.message("$a\n");
mp_game.init_circle();
Message.appendText("** You restart the game.\r\n");
} public void do_reject(){
mp_game.mode=mp_game.idle;
mp_game.message("$u"+mp_game.otherId+"\n");
u_button(1,0,0,0,0);
Message.appendText("** You reject the call.\r\n");
} public void do_call(){
if (who.getSelectedIndex()!=-1&&!who.getSelectedItem().equals(mp_game.name)){
try{
u_button(0,0,0,0,0);
mp_game.mode=mp_game.calling;
String www=who.getSelectedItem();
mp_game.message("$r"+www+"\n");
Message.appendText("** Calling "+www+".\r\n");
}catch(Exception e){};
}
} public void do_byebye(){
try {
mp_game.message("$b\n");
u_button(1,0,0,0,0);
mp_game.myturn=1;
mp_game.init_circle();
mp_game.mode=mp_game.idle;
Message.appendText("** You have left the game.\r\n");
}catch (Exception e){};
} public void do_accept(){
mp_game.myturn=2;
mp_game.turn=1;
mp_game.init_circle();
mp_game.repaint();
mp_game.message("$y"+mp_game.otherId+"\n");
Message.appendText("** Starting the game.\r\n");
Message.appendText("** You are red (the second player).\r\n");
u_button(1,0,0,1,1);
mp_game.mode=mp_game.play;
} public void do_who(String msg){
int i,j=0,k;
String name=null;
int lg=msg.length(); who.clear();
for (i=0;i<lg;i++){
k=msg.indexOf('+');
if (k!=-1){
name=msg.substring(0,k);
who.addItem(name);
msg=msg.substring(k+1);
i=k;
}
else {
name=msg;
who.addItem(name);
break;
}
}
} public boolean handleEvent(Event evt){
switch(evt.id){
case Event.KEY_PRESS:
if (evt.key=='\n'){
try{
if (!Outgoing.getText().equals("")){
mp_game.message(Outgoing.getText()+"\n");
if (Outgoing.getText().indexOf("$n")!=-1){
mp_game.name=Outgoing.getText().substring(2);
mp_game.message("$w\n");
}
Outgoing.setText("");
}
}catch (Exception e){};
}
return super.handleEvent(evt);
case Event.LIST_SELECT:
return true;
default:
return super.handleEvent(evt);
}
}}
不过先要但当前目录下建立classes目录
源码已发给你了
请问能用DataInputStream和DataOutputStream显示中文吗?
可用bin\native2ascii.exe 获得,
不知其他大虾有无更好办法