该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
http://tieba.baidu.com/p/3718757434?pid=67400973593&cid=0#67400973593
写完了第一个程序球运动后,写一个同样是java初级的程序---五子棋联机班
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.omg.CORBA.PRIVATE_MEMBER;
public class Fivechess extends JFrame{
private JPanel jPanel;
private Image image;
private int i=0;
private int count=0;
private byte[][] chess =new byte[21][21];
//private int model=0;
private boolean ischessed=false;
private boolean isyouturn=false;
private byte color=0;
private byte[]lastchess=new byte[2];
public Fivechess(){
this.setTitle("五子棋游戏");
this.setSize(700, 750);
int x =Toolkit.getDefaultToolkit().getScreenSize().width;
int y = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation((x-700)/2, (y-750)/2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar =new JMenuBar();
JMenu game =new JMenu("游戏");
JMenu help =new JMenu("帮助");
JMenuItem lianjie = new JMenuItem("连接主机");
JMenuItemjianli = new JMenuItem("建立主机");
JMenuItem guanyu = new JMenuItem("关于游戏");
JMenuItem tuichu= new JMenuItem("退出游戏");
tuichu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i=JOptionPane.showConfirmDialog(Fivechess.this, "您确定要退出游戏吗?");
if(i==0){
System.exit(0);
}
}
});
guanyu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showConfirmDialog(Fivechess.this, "游戏制作--炜神,公元2015年,联机版五子棋游戏--单机任意按钮回到游戏");
}
});
jianli.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
new server().start();
}
});
lianjie.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new client().start();
}
});
game.add(tuichu);
game.add(jianli);
game.add(lianjie);
help.add(guanyu);
bar.add(game);
bar.add(help);
jPanel =new JPanel();
this.add(jPanel);
this.setJMenuBar(bar);
try {
image =ImageIO.read(new File("1.png"));
} catch (Exception e) {
// TODO: handle exception
}
JFrameMouseeListenter jml= new JFrameMouseeListenter();
this.addMouseListener(jml);
this.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
Graphics gr = jPanel.getGraphics();
gr.drawImage(image, 0, 0, 700, 750, null);
for(int i =0;i<21;i++){
gr.drawString(i+"",30,55+30*i);
gr.drawLine(50, 50+i*30, 650, 50+30*i);
}
for(int i =0;i<21;i++){
gr.drawString((char)(i+'A')+"",45+i*30,35);
gr.drawLine(50+i*30,50, 50+30*i,650);
}
gr.fillOval(498, 198, 7, 7);
gr.fillOval(498, 498, 7, 7);
gr.fillOval(198, 498, 7, 7);
gr.fillOval(198, 198, 7, 7);
gr.fillOval(48, 646, 7, 7);
gr.fillOval(646, 646, 7, 7);
gr.fillOval(646, 48, 7, 7);
gr.fillOval(48, 48, 7, 7);
for(int i =0;i
for(int j=0;j
if(chess[i][j]==1){
gr.setColor(Color.black);
gr.fillOval(i*30+38, j*30+38, 25, 25);
}
else if(chess[i][j]==-1){
gr.setColor(Color.black);
gr.drawOval(i*30+38,j*30+38, 25,25);
gr.setColor(Color.white);
gr.fillOval(i*30+39,j*30+39, 24,24);
}
}
}
}
public boolean isover(int x ,int y){
int samecolor=0;
for(int i=x-4;i<=x+4;i++){
if(i>=0 && i<=20){
if(chess[i][y]==chess[x][y]){
samecolor++;
if(samecolor==5){
return true;
}
}
else{
samecolor=0;
}
}
}
for(int i=y-4;i
if(i>=0 && i<=20){
if(chess[x][i]==chess[x][y]){
samecolor++;
if(samecolor==5){
return true;
}
}
else{
samecolor=0;
}
}
}
for(int i=x-4,j=y+4;i<=x+4 && j>=y-4;i++,j--){
if(i>=0 && i=0 && j<=20){
if(chess[i][j]==chess[x][y]){
samecolor++;
if(samecolor==5){
return true;
}
}
else{
samecolor=0;
}
}
}
for(int i=x-4, j=y-4;i<=x+4 && j<=y+4;i++,j++){
if(i>=0 && i=0 && j<=20){
if(chess[i][j]==chess[x][y]){
samecolor++;
if(samecolor==5){
return true;
}
}
else{
samecolor=0;
}
}
}
return false;
}
public static void main(String[] args) {
new Fivechess();
}
class JFrameMouseeListenter implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println(e.getX()+":"+e.getY());
/*Graphics gr = jPanel.getGraphics();
i++;
if(i%2==0){
gr.fillOval(e.getX()-18, e.getY()-36, 25, 25);
}
else {
gr.drawOval(e.getX()-18, e.getY()-36, 25, 25);
}*/
//System.out.println(e.getX()+" "+e.getY());
if(isyouturn && !ischessed){
int x = e.getX();
int y =e.getY();
System.out.println(x+" "+y);
if(x<47 || x>653 || y<97| y>703){
return;
}
else{
x = (int)Math.round((x-50)/30.0);
y= (int)Math.round((y-100)/30.0);
if(chess[x][y]==0){
count++;
if(count%2==1){
chess[x][y]=1;
}
else{
chess[x][y]=-1;
}
chess[x][y]=color;
ischessed =true;
lastchess[0]=(byte)x;
lastchess[1]=(byte)y;
Fivechess.this.repaint();
if(Fivechess.this.isover(x, y)){
JOptionPane.showConfirmDialog(Fivechess.this, (chess[x][y]==1)?"黑棋获胜":"白棋获胜");
}
}
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("mouseEntered");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("mouseExited");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
class server extends Thread{
public void run(){
try {
ServerSocket ss =new ServerSocket(10000);
JOptionPane.showConfirmDialog(Fivechess.this, "建立主机成功,等待玩家连入");
Socket socket =ss.accept();
String address =socket.getInetAddress().getHostAddress();
JOptionPane.showConfirmDialog(Fivechess.this, address+"已经连入主机,等待游戏开始");
isyouturn=true;
ischessed=false;
color=1;
InputStream is =socket.getInputStream();
OutputStream os =socket.getOutputStream();
while(true){
while(isyouturn && !ischessed){
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
}
}
os.write(lastchess);
os.flush();
isyouturn=false;
byte []b=new byte[2];
is.read(b);
byte x=b[0];
byte y=b[1];
Fivechess.this.chess[x][y]=-1;
Fivechess.this.repaint();
if(Fivechess.this.isover(x, y)){
JOptionPane.showConfirmDialog(Fivechess.this, (chess[x][y]==1)?"黑棋获胜":"白棋获胜");
}
isyouturn=true;
ischessed=false;
}
} catch (BindException e) {
// TODO: handle exception
JOptionPane.showConfirmDialog(Fivechess.this, "端口被占用,主机建立失败");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class client extends Thread{
public void run(){
String address =JOptionPane.showInputDialog("请输入主机IP地址");
try {
Socket socket=new Socket(address,10000);
JOptionPane.showConfirmDialog(Fivechess.this, "已经连入主机,等待游戏开始");
isyouturn=false;
ischessed=false;
color=-1;
InputStream is =socket.getInputStream();
OutputStream os = socket.getOutputStream();
while(true){
byte []b=new byte[2];
is.read(b);
byte x=b[0];
byte y=b[1];
Fivechess.this.chess[x][y]=1;
Fivechess.this.repaint();
if(Fivechess.this.isover(x, y)){
JOptionPane.showConfirmDialog(Fivechess.this,(chess[x][y]==1)?"黑棋获胜":"白棋获胜");
}
isyouturn=true;
ischessed=false;
while(isyouturn && !ischessed){
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
}
}
os.write(lastchess);
System.out.println("发送"+lastchess[0]+":"+lastchess[1]);
os.flush();
isyouturn=false;
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
程序到这里就结束了,下面是本人打开两个五子棋程序,一个作为主机,一个用来连接主机,可以做到自己检测调试的目的。
同样,另一个电脑获取主机的IP地址,在主机建立好后,即可达到联机的目的。
这是主机建立好之后,等待玩家连入。