中易网

编程求问,JAVA写了一个五子棋,编译通过,但无法运行,请问是异常吗?怎么解决

答案:4  悬赏:0  
解决时间 2021-03-14 09:33
import javax.imageio.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;

public class Gobang
{
BufferedImage table;
BufferedImage black;
BufferedImage white;
BufferedImage selected;

private static int BOARD_SIZE=15;
private final int TABLE_WIDTH=535;
private final int TABLE_HEIGHT=536;
private final int RATE=TABLE_WIDTH/BOARD_SIZE;
private final int X_OFFSET=5;
private final int Y_OFFSET=6;

private String[][] board=new String[BOARD_SIZE][BOARD_SIZE];
JFrame f=new JFrame("五子棋");
ChessBoard chessBoard=new ChessBoard();
private int selectedX=-1;
private int selectedY=-1;

public void init()throws Exception
{
table=ImageIO.read(new File("image2/board.jpg"));
black=ImageIO.read(new File("image2/black.gif"));
white=ImageIO.read(new File("image2/white.gif"));
selected=ImageIO.read(new File("image2/selected.gif"));
for(int i=0;i<=BOARD_SIZE;i++)
{
for(int j=0;i<=BOARD_SIZE;j++)
{
board[i][j]="*";
}
}
chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));
chessBoard.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int xPos=(int)((e.getX()-X_OFFSET)/RATE);
int yPos=(int)((e.getY()-Y_OFFSET)/RATE);
board[xPos][yPos]="黑";
chessBoard.repaint();
}
});
chessBoard.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e)
{
selectedX=(int)((e.getX()-X_OFFSET)/RATE);
selectedY=(int)((e.getY()-Y_OFFSET)/RATE);
chessBoard.repaint();
}

});
f.add(chessBoard);
f.pack();
f.setVisible(true);
}

public static void main(String[] args)throws Exception
{
Gobang gb=new Gobang();
gb.init();
}

class ChessBoard extends JPanel
{
public void paint(Graphics g)
{
g.drawImage(table,0,0,null);
if(selectedX>=0&&selectedY>=0)
g.drawImage(selected,selectedX*RATE+X_OFFSET,selectedY*RATE+Y_OFFSET,null);
for(int i=0;i {
for(int j=0;j {
if(board[i][j].equals("黑"))
{
g.drawImage(black,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);
}
if(board[i][j].equals("白"))
{
g.drawImage(white,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);
}
}
}
}
}
}
最佳答案
编译通过是说明程序的语法没有问题,异常的话在运行程序的时候就会提示的,异常为什么会产生?它是为了减少程序出错的一种产物,它把程序中一般性错误都给与定义,当出现某种异常的时候就会提示,这样程序员就可以按照提示去修改程序,减少了很多程序出错的概率,但是尽管是这样,面对世界上的多种多样,错误也是多种多样的,自己的逻辑错误是检查不出来的,例如你的程序中有一个死循环,编译器是无法发现的,因为事实上有很多程序的循环条件都是永真的,但是里面会有一些判断条件来跳出循环,执行break语句,这样就跳出来循环,而且死循环如果没有提示信息的话,就好像是不再运行,可是事实上它是运行的。
全部回答
跟个断点 走一遍 先看看变量和逻辑问题啊
java网络五子棋 下面的源代码分为4个文件; chessclient.java:客户端主程序。 chessinterface.java:客户端的界面。 chesspad.java:棋盘的绘制。 chessserver.java:服务器端。 可同时容纳50个人同时在线下棋,聊天。 没有加上详细注释,不过绝对可以运行,j2sdk1.4下通过。 import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; class clientthread extends thread { chessclient chessclient; clientthread(chessclient chessclient) { this.chessclient=chessclient; } public void acceptmessage(string recmessage) { if(recmessage.startswith("/userlist ")) { stringtokenizer usertoken=new stringtokenizer(recmessage," "); int usernumber=0; chessclient.userpad.userlist.removeall(); chessclient.inputpad.userchoice.removeall(); chessclient.inputpad.userchoice.additem("所有人"); while(usertoken.hasmoretokens()) { string user=(string)usertoken.nexttoken(" "); if(usernumber>0 && !user.startswith("[inchess]")) { chessclient.userpad.userlist.add(user); chessclient.inputpad.userchoice.additem(user); } usernumber++; } chessclient.inputpad.userchoice.select("所有人"); } else if(recmessage.startswith("/yourname ")) { chessclient.chessclientname=recmessage.substring(10); chessclient.settitle("java五子棋客户端 "+"用户名:"+chessclient.chessclientname); } else if(recmessage.equals("/reject")) { try { chessclient.chesspad.statustext.settext("不能加入游戏"); chessclient.controlpad.cancelgamebutton.setenabled(false); chessclient.controlpad.joingamebutton.setenabled(true); chessclient.controlpad.creatgamebutton.setenabled(true); } catch(exception ef) { chessclient.chatpad.chatlinearea.settext("chessclient.chesspad.chesssocket.close无法关闭"); } chessclient.controlpad.joingamebutton.setenabled(true); } else if(recmessage.startswith("/peer ")) { chessclient.chesspad.chesspeername=recmessage.substring(6); if(chessclient.isserver) { chessclient.chesspad.chesscolor=1; chessclient.chesspad.ismouseenabled=true; chessclient.chesspad.statustext.settext("请黑棋下子"); } else if(chessclient.isclient) { chessclient.chesspad.chesscolor=-1; chessclient.chesspad.statustext.settext("已加入游戏,等待对方下子..."); } } else if(recmessage.equals("/youwin")) { chessclient.isonchess=false; chessclient.chesspad.chessvictory(chessclient.chesspad.chesscolor); chessclient.chesspad.statustext.settext("对方退出,请点放弃游戏退出连接"); chessclient.chesspad.ismouseenabled=false; } else if(recmessage.equals("/ok")) { chessclient.chesspad.statustext.settext("创建游戏成功,等待别人加入..."); } else if(recmessage.equals("/error")) { chessclient.chatpad.chatlinearea.append("传输错误:请退出程序,重新加入 \n"); } else { chessclient.chatpad.chatlinearea.append(recmessage+"\n"); chessclient.chatpad.chatlinearea.setcaretposition( chessclient.chatpad.chatlinearea.gettext().length()); } } public void run() { string message=""; try { while(true) { message=chessclient.in.readutf(); acceptmessage(message); } } catch(ioexception es) { } } } public class chessclient extends frame implements actionlistener,keylistener { userpad userpad=new userpad(); chatpad chatpad=new chatpad(); controlpad controlpad=new controlpad(); chesspad chesspad=new chesspad(); inputpad inputpad=new inputpad(); socket chatsocket; datainputstream in; dataoutputstream out; string chessclientname=null; string host=null; int port=4331; boolean isonchat=false; //在聊天? boolean isonchess=false; //在下棋? boolean isgameconnected=false; //下棋的客户端连接? boolean isserver=false; //如果是下棋的主机 boolean isclient=false; //如果是下棋的客户端 panel southpanel=new panel(); panel northpanel=new panel(); panel centerpanel=new panel(); panel westpanel=new panel(); panel eastpanel=new panel(); chessclient() { super("java五子棋客户端"); setlayout(new borderlayout()); host=controlpad.inputip.gettext(); westpanel.setlayout(new borderlayout()); westpanel.add(userpad,borderlayout.north); westpanel.add(chatpad,borderlayout.center); westpanel.setbackground(color.pink); inputpad.inputwords.addkeylistener(this); chesspad.host=controlpad.inputip.gettext(); centerpanel.add(chesspad,borderlayout.center); centerpanel.add(inputpad,borderlayout.south); centerpanel.setbackground(color.pink); controlpad.connectbutton.addactionlistener(this); controlpad.creatgamebutton.addactionlistener(this); controlpad.joingamebutton.addactionlistener(this); controlpad.cancelgamebutton.addactionlistener(this); controlpad.exitgamebutton.addactionlistener(this); controlpad.creatgamebutton.setenabled(false); controlpad.joingamebutton.setenabled(false); controlpad.cancelgamebutton.setenabled(false); southpanel.add(controlpad,borderlayout.center); southpanel.setbackground(color.pink); addwindowlistener(new windowadapter() { public void windowclosing(windowevent e) { if(isonchat) { try { chatsocket.close(); } catch(exception ed) { } } if(isonchess || isgameconnected) { try { chesspad.chesssocket.close(); } catch(exception ee) { } } system.exit(0); } public void windowactivated(windowevent ea) { } }); add(westpanel,borderlayout.west); add(centerpanel,borderlayout.center); add(southpanel,borderlayout.south); pack(); setsize(670,548); setvisible(true); setresizable(false); validate(); } public boolean connectserver(string serverip,int serverport) throws exception { try { chatsocket=new socket(serverip,serverport); in=new datainputstream(chatsocket.getinputstream()); out=new dataoutputstream(chatsocket.getoutputstream()); clientthread clientthread=new clientthread(this); clientthread.start(); isonchat=true; return true; } catch(ioexception ex) { chatpad.chatlinearea.settext("chessclient:connectserver:无法连接,建议重新启动程序 \n"); } return false; } public void actionperformed(actionevent e) { if(e.getsource()==controlpad.connectbutton) { host=chesspad.host=controlpad.inputip.gettext(); try { if(connectserver(host,port)) { chatpad.chatlinearea.settext(""); controlpad.connectbutton.setenabled(false); controlpad.creatgamebutton.setenabled(true); controlpad.joingamebutton.setenabled(true); chesspad.statustext.settext("连接成功,请创建游戏或加入游戏"); } } catch(exception ei) { chatpad.chatlinearea.settext("controlpad.connectbutton:无法连接,建议重新启动程序 \n"); } } if(e.getsource()==controlpad.exitgamebutton) { if(isonchat) { try { chatsocket.close(); } catch(exception ed) { } } if(isonchess || isgameconnected) { try { chesspad.chesssocket.close(); } catch(exception ee) { } } system.exit(0); } if(e.getsource()==controlpad.joingamebutton) { string selecteduser=userpad.userlist.getselecteditem(); if(selecteduser==null || selecteduser.startswith("[inchess]") || selecteduser.equals(chessclientname)) { chesspad.statustext.settext("必须先选定一个有效用户"); } else { try { if(!isgameconnected) { if(chesspad.connectserver(chesspad.host,chesspad.port)) { isgameconnected=true; isonchess=true; isclient=true; controlpad.creatgamebutton.setenabled(false); controlpad.joingamebutton.setenabled(false); controlpad.cancelgamebutton.setenabled(true); chesspad.chessthread.sendmessage("/joingame "+userpad.userlist.getselecteditem()+" "+chessclientname); } } else { isonchess=true; isclient=true; controlpad.creatgamebutton.setenabled(false); controlpad.joingamebutton.setenabled(false); controlpad.cancelgamebutton.setenabled(true); chesspad.chessthread.sendmessage("/joingame "+userpad.userlist.getselecteditem()+" "+chessclientname); } } catch(exception ee) { isgameconnected=false; isonchess=false; isclient=false; controlpad.creatgamebutton.setenabled(true); controlpad.joingamebutton.setenabled(true); controlpad.cancelgamebutton.setenabled(false); chatpad.chatlinearea.settext("chesspad.connectserver无法连接 \n"+ee); } } } if(e.getsource()==controlpad.creatgamebutton) { try { if(!isgameconnected) { if(chesspad.connectserver(chesspad.host,chesspad.port)) { isgameconnected=true; isonchess=true; isserver=true; controlpad.creatgamebutton.setenabled(false); controlpad.joingamebutton.setenabled(false); controlpad.cancelgamebutton.setenabled(true); chesspad.chessthread.sendmessage("/creatgame "+"[inchess]"+chessclientname); } } else { isonchess=true; isserver=true; controlpad.creatgamebutton.setenabled(false); controlpad.joingamebutton.setenabled(false); controlpad.cancelgamebutton.setenabled(true); chesspad.chessthread.sendmessage("/creatgame "+"[inchess]"+chessclientname); } } catch(exception ec) { isgameconnected=false; isonchess=false; isserver=false; controlpad.creatgamebutton.setenabled(true); controlpad.joingamebutton.setenabled(true); controlpad.cancelgamebutton.setenabled(false); ec.printstacktrace(); chatpad.chatlinearea.settext("chesspad.connectserver无法连接 \n"+ec); } } if(e.getsource()==controlpad.cancelgamebutton) { if(isonchess) { chesspad.chessthread.sendmessage("/giveup "+chessclientname); chesspad.chessvictory(-1*chesspad.chesscolor); controlpad.creatgamebutton.setenabled(true); controlpad.joingamebutton.setenabled(true); controlpad.cancelgamebutton.setenabled(false); chesspad.statustext.settext("请建立游戏或者加入游戏"); } if(!isonchess) { controlpad.creatgamebutton.setenabled(true); controlpad.joingamebutton.setenabled(true); controlpad.cancelgamebutton.setenabled(false); chesspad.statustext.settext("请建立游戏或者加入游戏"); } isclient=isserver=false; } } public void keypressed(keyevent e) { textfield inputwords=(textfield)e.getsource(); if(e.getkeycode()==keyevent.vk_enter) { if(inputpad.userchoice.getselecteditem().equals("所有人")) { try { out.writeutf(inputwords.gettext()); inputwords.settext(""); } catch(exception ea) { chatpad.chatlinearea.settext("chessclient:keypressed无法连接,建议重新连接 \n"); userpad.userlist.removeall(); inputpad.userchoice.removeall(); inputwords.settext(""); controlpad.connectbutton.setenabled(true); } } else { try { out.writeutf("/"+inputpad.userchoice.getselecteditem()+" "+inputwords.gettext()); inputwords.settext(""); } catch(exception ea) { chatpad.chatlinearea.settext("chessclient:keypressed无法连接,建议重新连接 \n"); userpad.userlist.removeall(); inputpad.userchoice.removeall(); inputwords.settext(""); controlpad.connectbutton.setenabled(true); } } } } public void keytyped(keyevent e) { } public void keyreleased(keyevent e) { } public static void main(string args[]) { chessclient chessclient=new chessclient(); } } import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; class userpad extends panel { list userlist=new list(10); userpad() { setlayout(new borderlayout()); for(int i=0;i<50;i++) { userlist.add(i+"."+"没有用户"); } add(userlist,borderlayout.center); } } class chatpad extends panel { textarea chatlinearea=new textarea("",18,30,textarea.scrollbars_vertical_only); chatpad() { setlayout(new borderlayout()); add(chatlinearea,borderlayout.center); } } class controlpad extends panel { label iplabel=new label("ip",label.left); textfield inputip=new textfield("localhost",10); button connectbutton=new button("连接主机"); button creatgamebutton=new button("建立游戏"); button joingamebutton=new button("加入游戏"); button cancelgamebutton=new button("放弃游戏"); button exitgamebutton=new button("关闭程序"); controlpad() { setlayout(new flowlayout(flowlayout.left)); setbackground(color.pink); add(iplabel); add(inputip); add(connectbutton); add(creatgamebutton); add(joingamebutton); add(cancelgamebutton); add(exitgamebutton); } } class inputpad extends panel { textfield inputwords=new textfield("",40); choice userchoice=new choice(); inputpad() { setlayout(new flowlayout(flowlayout.left)); for(int i=0;i<50;i++) { userchoice.additem(i+"."+"没有用户"); } userchoice.setsize(60,24); add(userchoice); add(inputwords); } } import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; class chessthread extends thread { chesspad chesspad; chessthread(chesspad chesspad) { this.chesspad=chesspad; } public void sendmessage(string sndmessage) { try { chesspad.outdata.writeutf(sndmessage); } catch(exception ea) { system.out.println("chessthread.sendmessage:"+ea); } } public void acceptmessage(string recmessage) { if(recmessage.startswith("/chess ")) { stringtokenizer usertoken=new stringtokenizer(recmessage," "); string chesstoken; string[] chessopt={"-1","-1","0"}; int chessoptnum=0; while(usertoken.hasmoretokens()) { chesstoken=(string)usertoken.nexttoken(" "); if(chessoptnum>=1 && chessoptnum<=3) { chessopt[chessoptnum-1]=chesstoken; } chessoptnum++; } chesspad.netchesspaint(integer.parseint(chessopt[0]),integer.parseint(chessopt[1]),integer.parseint(chessopt[2])); } else if(recmessage.startswith("/yourname ")) { chesspad.chessselfname=recmessage.substring(10); } else if(recmessage.equals("/error")) { chesspad.statustext.settext("错误:没有这个用户,请退出程序,重新加入"); } else { //system.out.println(recmessage); } } public void run() { string message=""; try { while(true) { message=chesspad.indata.readutf(); acceptmessage(message); } } catch(ioexception es) { } } } class chesspad extends panel implements mouselistener,actionlistener { int chesspoint_x=-1,chesspoint_y=-1,chesscolor=1; int chessblack_x[]=new int[200]; int chessblack_y[]=new int[200]; int chesswhite_x[]=new int[200]; int chesswhite_y[]=new int[200]; int chessblackcount=0,chesswhitecount=0; int chessblackwin=0,chesswhitewin=0; boolean ismouseenabled=false,iswin=false,isingame=false; textfield statustext=new textfield("请先连接服务器"); socket chesssocket; datainputstream indata; dataoutputstream outdata; string chessselfname=null; string chesspeername=null; string host=null; int port=4331; chessthread chessthread=new chessthread(this); chesspad() { setsize(440,440); setlayout(null); setbackground(color.pink); addmouselistener(this); add(statustext); statustext.setbounds(40,5,360,24); statustext.seteditable(false); } public boolean connectserver(string serverip,int serverport) throws exception { try { chesssocket=new socket(serverip,serverport); indata=new datainputstream(chesssocket.getinputstream()); outdata=new dataoutputstream(chesssocket.getoutputstream()); chessthread.start(); return true; } catch(ioexception ex) { statustext.settext("chesspad:connectserver:无法连接 \n"); } return false; } public void chessvictory(int chesscolorwin) { this.removeall(); for(int i=0;i<=chessblackcount;i++) { chessblack_x[i]=0; chessblack_y[i]=0; } for(int i=0;i<=chesswhitecount;i++) { chesswhite_x[i]=0; chesswhite_y[i]=0; } chessblackcount=0; chesswhitecount=0; add(statustext); statustext.setbounds(40,5,360,24); if(chesscolorwin==1) { chessblackwin++; statustext.settext("黑棋胜,黑:白为"+chessblackwin+":"+chesswhitewin+",重新开局,等待白棋下子..."); } else if(chesscolorwin==-1) { chesswhitewin++; statustext.settext("白棋胜,黑:白为"+chessblackwin+":"+chesswhitewin+",重新开局,等待黑棋下子..."); } } public void getlocation(int a,int b,int color) { if(color==1) { chessblack_x[chessblackcount]=a*20; chessblack_y[chessblackcount]=b*20; chessblackcount++; } else if(color==-1) { chesswhite_x[chesswhitecount]=a*20; chesswhite_y[chesswhitecount]=b*20; chesswhitecount++; } } public boolean checkwin(int a,int b,int checkcolor) { int step=1,chesslink=1,chesslinktest=1,chesscompare=0; if(checkcolor==1) { chesslink=1; for(step=1;step<=4;step++) { for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) { if(((a+step)*20==chessblack_x[chesscompare]) && ((b*20)==chessblack_y[chesscompare])) { chesslink=chesslink+1; if(chesslink==5) { return(true); } } } if(chesslink==(chesslinktest+1)) chesslinktest++; else break; } for(step=1;step<=4;step++) { for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) { if(((a-step)*20==chessblack_x[chesscompare]) && (b*20==chessblack_y[chesscompare])) { chesslink++; if(chesslink==5) { return(true); } } } if(chesslink==(chesslinktest+1)) chesslinktest++; else break; } chesslink=1; chesslinktest=1; for(step=1;step<=4;step++) { for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) { if((a*20==chessblack_x[chesscompare]) && ((b+step)*20==chessblack_y[chesscompare])) { chesslink++; if(chesslink==5) { return(true); } } } if(chesslink==(chesslinktest+1)) chesslinktest++; else break; } for(step=1;step<=4;step++) { for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) { if((a*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare])) { chesslink++; if(chesslink==5) { return(true); } } } if(chesslink==(chesslinktest+1)) chesslinktest++; else break; } chesslink=1; chesslinktest=1; for(step=1;step<=4;step++) { for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) { if(((a-step)*20==chessblack_x[chesscompare]) && ((b+step)*20==chessblack_y[chesscompare])) { chesslink++; if(chesslink==5) { return(true); } } } if(chesslink==(chesslinktest+1)) chesslinktest++; else break; } for(step=1;step<=4;step++) { for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) { if(((a+step)*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare])) { chesslink++; if(chesslink==5) { return(true); } } } if(chesslink==(chesslinktest+1)) chesslinktest++; else break; } chesslink=1; chesslinktest=1; for(step=1;step<=4;step++) { for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) { if(((a+step)*20==chessblack_x[chesscompare]) && ((b+step)*20==chessblack_y[chesscompare])) { chesslink++; if(chesslink==5) { return(true); } } } if(chesslink==(chesslinktest+1)) chesslinktest++; else break; } for(step=1;step<=4;step++) { for(chesscompare=0;chesscompare<=chessblackcount;chesscompare++) { if(((a-step)*20==chessblack_x[chesscompare]) && ((b-step)*20==chessblack_y[chesscompare])) { chesslink++; if(chesslink==5) { return(true); } }
我来帮你调试。你回复我把你自己的qq号留下,我加你。你把代码发给我。我帮你调试一下,告诉你哪里错了。 ...我快成了百度知道专业调试程序员了
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
为什么2017 教育教学思想大讨论
合同快到期
苹果电脑系统,我看有很多版本,这个通用吗?
成分相同的材料,性能是否一定相同?举例说明.
到了湖南吉首的火车站,去湖南吉首大学怎么走
几分甜烘焙工坊怎么去啊,有知道地址的么
请问八段锦、六字决、易筋经、五禽戏四套健身
枇杷果吃有什么好处?
1993年正月初四23点35出生
念及你,既无风雪也无晴。什么意思求详解
韩膳馆韩国料理NO.2我想知道这个在什么地方
imba 命令
前辈,我收到中国核电工程深圳设计院的面试了
柳絮的成分也是纤维素,为什么没有被发现任何
汽车导航wifi总是连接不好是怎么回事
推荐资讯
荣威350减震器换了得多少钱
和尚打伞.……无法无天!打三个数;0……9
看《名侦探柯南》有什么好处?
商品房可以做存放白酒仓库吗
棉城卫生院东山服务站这个地址在什么地方,我
关于摩托车蓄电池和排气管的问题
生峰平价超市在什么地方啊,我要过去处理事情
求高手解答:关于古代音韵学 双声连绵字和叠
这个是什么龟?听说是麒麟龟,还是国家二级保
桂林市科技进修学院中专部怎么去啊,有知道地
死人客栈有多少部
谁有草麻袋围堰平面布置cad图,围堰断面施工ca
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?