java 五子棋游戏
大家好,我是java新手,今天写五子棋游戏,遇上点小麻烦,希望大家给解决一下!
我用Arraylist装棋子,然后,不知道怎么判定坐标处已有棋子!代码如下:[code=Java][/code]
package com.chess.game;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyJFrame extends JFrame
{
boolean isback =true;//判断棋子颜色
MyJFrame mf ;
/**
* 声明窗体的宽
*/
private static final int WIDTH = 500;
/**
* 声明窗体的长
*/
private static final int HEIGHT = 500;
public MyJFrame()
{
//设置窗体标题
this.setTitle("五子棋对弈");
this.setSize(WIDTH,HEIGHT);
int wideth = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation((wideth-WIDTH)/2,(height-HEIGHT)/2 );
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
addMouseListener(new MyMouseMonitor());
this.setVisible(true);
new Thread(new MyRunnable()).start();
/**
* 通过标签加入背景图片
*/
ImageIcon backIcon = new ImageIcon("D:/yzy1/Image/fiveChess.jpg");
JLabel label = new JLabel(backIcon);
label.setOpaque(true);
label.setBounds(0, 0, backIcon.getIconWidth(),backIcon.getIconHeight() );
this.add(label);
}
List <Chessman> chessmanList =new ArrayList <Chessman>(); //装棋子
/**
* 重写paint方法,画出棋盘方格
*/
@Override
public void paint(Graphics g)
{
super.paint(g);
Color c = g.getColor();
g.setColor(Color.BLACK);
for(int i = 0;i <= 360;i = i+20)
{
g.drawLine(10, 70 + i,370,70 + i );
g.drawLine(10 + i, 70, 10+i, 430);
}
Font f = g.getFont();
g.setColor(Color.red);
g.setFont(new Font("宋体",0,18));
g.drawString("五子棋对弈:", 60, 60);
g.setColor(Color.BLACK);
g.drawString("黑方时间:",40, 470);
g.drawString("白方时间:", 260,470 );
g.setFont(f);
g.setColor(c);
/**
* 在棋盘中画棋子
*/
for(int i =0;i < chessmanList.size();i++ )
{
Chessman chessman = chessmanList.get(i);
chessman.paint(g);
}
}
/**
* 鼠标监听事件
* @author Administrator
*
*/
class MyMouseMonitor extends MouseAdapter
{
@Override
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
//System.out.println(x);
//System.out.println(y);
Chessman chessman;
if(isback)
{
chessman = new Chessman(x, y, mf, isback);
chessmanList.add(chessman);
isback = false;
}
else
{
chessman = new Chessman(x, y, mf, isback);
chessmanList.add(chessman);
isback = true;
}
}
}
class MyRunnable implements Runnable
{
@Override
public void run()
{
while(true)
{
repaint();
try
{
Thread.sleep(50);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.chess.game;
import java.awt.Color;
/**
* 棋子类
* @author Administrator
*
*/
public class Chessman
{
MyJFrame mf;
int x,y;
boolean isback;
/**
* 确定棋子的大小