import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class StartFrame extends JFrame implements ActionListener {
<span style="white-space:pre"> </span>private JMenuBar jmb=null;
<span style="white-space:pre"> </span>private JMenu sysMenu,helpMenu;
<span style="white-space:pre"> </span>private JMenuItem startMenuItem,exitMenuItem;
<span style="white-space:pre"> </span>public StartFrame(){
<span style="white-space:pre"> </span>//Main Frame
<span style="white-space:pre"> </span> ChessBoard chessBoard=new ChessBoard();
<span style="white-space:pre"> </span>setTitle("five-chess");
<span style="white-space:pre"> </span>Container c=getContentPane();
<span style="white-space:pre"> </span>jmb=new JMenuBar();
<span style="white-space:pre"> </span>sysMenu =new JMenu("menu");
<span style="white-space:pre"> </span>helpMenu =new JMenu("help");
<span style="white-space:pre"> </span>startMenuItem =new JMenuItem("begin");
<span style="white-space:pre"> </span> exitMenuItem =new JMenuItem("exit");
<span style="white-space:pre"> </span>startMenuItem.addActionListener(this);
<span style="white-space:pre"> </span> sysMenu.add(startMenuItem);
<span style="white-space:pre"> </span> sysMenu.add(exitMenuItem);
<span style="white-space:pre"> </span> jmb.add(sysMenu);
<span style="white-space:pre"> </span> jmb.add(helpMenu);
<span style="white-space:pre"> </span> //setJMenuBar(jmb);
<span style="white-space:pre"> </span> //c.setLayout(new FlowLayout(FlowLayout.LEFT));
<span style="white-space:pre"> </span> //c.add(jmb);
<span style="white-space:pre"> </span> //c.add(chessBoard,BorderLayout.CENTER);
<span style="white-space:pre"> </span> //c.add(chessBoard);
<span style="white-space:pre"> </span> chessBoard.setOpaque(true);
<span style="white-space:pre"> </span> add(jmb,BorderLayout.NORTH);
<span style="white-space:pre"> </span> add(chessBoard,BorderLayout.CENTER);
<span style="white-space:pre"> </span> this.setSize(700, 700);
<span style="white-space:pre"> </span> //this.pack();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void actionPerformed(ActionEvent e) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>if(e.getSource().equals(startMenuItem)){
<span style="white-space:pre"> </span>//JOptionPane.showMessageDialog(null, "begin");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void main(String[] args){
<span style="white-space:pre"> </span>StartFrame sf=new StartFrame();
<span style="white-space:pre"> </span>sf.setVisible(true);
<span style="white-space:pre"> </span>}
}
public class Point {
int x,y;
String color="";
public Point(){
<span style="white-space:pre"> </span>
}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public int hashCode() {
<span style="white-space:pre"> </span>final int prime = 31;
<span style="white-space:pre"> </span>int result = 1;
<span style="white-space:pre"> </span>result = prime * result + x;
<span style="white-space:pre"> </span>result = prime * result + y;
<span style="white-space:pre"> </span>return result;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public boolean equals(Object obj) {
<span style="white-space:pre"> </span>if (this == obj)
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>if (obj == null)
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>if (getClass() != obj.getClass())
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>Point other = (Point) obj;
<span style="white-space:pre"> </span>if (x != other.x)
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>if (y != other.y)
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public Point(int x, int y, String color) {
<span style="white-space:pre"> </span>super();
<span style="white-space:pre"> </span>this.x = x;
<span style="white-space:pre"> </span>this.y = y;
<span style="white-space:pre"> </span>this.color = color;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String getColor() {
<span style="white-space:pre"> </span>return color;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setColor(String color) {
<span style="white-space:pre"> </span>this.color = color;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public int getX() {
<span style="white-space:pre"> </span>return x;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setX(int x) {
<span style="white-space:pre"> </span>this.x = x;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public int getY() {
<span style="white-space:pre"> </span>return y;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void setY(int y) {
<span style="white-space:pre"> </span>this.y = y;
<span style="white-space:pre"> </span>}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class ChessBoard extends JPanel implements MouseListener{
<span style="white-space:pre"> </span>public static final int MARGIN=30;
<span style="white-space:pre"> </span>public static final int GRID_SPAN=35;
<span style="white-space:pre"> </span>public static final int ROWS=15;
<span style="white-space:pre"> </span>public static final int COLS=15;
<span style="white-space:pre"> </span>public static String chessColor="black"; //chess role
<span style="white-space:pre"> </span>Point[] chessList=new Point[ROWS*COLS]; //store chess
<span style="white-space:pre"> </span>int chessCount=0; //chess number
<span style="white-space:pre"> </span>public boolean gameOver=false;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public ChessBoard(){
<span style="white-space:pre"> </span> //chessList=new Point[(ROWS+1)*(COLS+1)]; //create chessBoard
<span style="white-space:pre"> </span> addMouseListener(this);
<span style="white-space:pre"> </span> addMouseMotionListener(new MouseMotionListener(){
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mouseDragged(MouseEvent e) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mouseMoved(MouseEvent e) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> });
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mouseClicked(MouseEvent e) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>//JOptionPane.showMessageDialog(null, "ok");
<span style="white-space:pre"> </span>if(gameOver){
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>int x=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
<span style="white-space:pre"> </span>int y=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
<span style="white-space:pre"> </span>Point p=new Point(x,y,chessColor);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(x<0||x>ROWS||y<0||y>COLS){
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(findChess(p)){ //check chess whether exist before choose
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>chessList[chessCount]=p;
<span style="white-space:pre"> </span>chessCount++;
<span style="white-space:pre"> </span>repaint();
<span style="white-space:pre"> </span>if(isWin(p)){
<span style="white-space:pre"> </span>String message="";
<span style="white-space:pre"> </span>gameOver=true;
<span style="white-space:pre"> </span>if("black".equals(chessColor)){
<span style="white-space:pre"> </span>message="Black Win";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else if ("white".equals(chessColor)){
<span style="white-space:pre"> </span>message="White Win";
<span style="white-space:pre"> </span>}else if(chessCount==(ROWS*COLS)){
<span style="white-space:pre"> </span>message="Draw";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>JOptionPane.showMessageDialog(null, message );
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if("white".equals(chessColor)){
<span style="white-space:pre"> </span>chessColor="black";
<span style="white-space:pre"> </span>}else{
<span style="white-space:pre"> </span> chessColor="white";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public boolean isWin(Point p){ //judge win or not
<span style="white-space:pre"> </span>int sum=1;
<span style="white-space:pre"> </span>int x=p.getX();
int y=p.getY();
String color=p.getColor();
<span style="white-space:pre"> </span>//West-->East
<span style="white-space:pre"> </span>for(int i=1;i<=4;i++){
<span style="white-space:pre"> </span>if(sameColor(x-i,y,color)){
<span style="white-space:pre"> </span>sum++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(sameColor(x+i,y,color)){
<span style="white-space:pre"> </span>sum++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(sum==5){
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>//North -->South
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(int i=1;i<=4;i++){
<span style="white-space:pre"> </span>if(sameColor(x,y-i,color)){
<span style="white-space:pre"> </span>sum++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(sameColor(x,y+i,color)){
<span style="white-space:pre"> </span>sum++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(sum==5){
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>//North-West -->South-East
<span style="white-space:pre"> </span>for(int i=1;i<=4;i++){
<span style="white-space:pre"> </span>if(sameColor(x-i,y+i,color)){
<span style="white-space:pre"> </span>sum++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(sameColor(x+i,y-i,color)){
<span style="white-space:pre"> </span>sum++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(sum==5){
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>//North-East-->South-East
<span style="white-space:pre"> </span>for(int i=1;i<=4;i++){
<span style="white-space:pre"> </span>if(sameColor(x-i,y-i,color)){
<span style="white-space:pre"> </span>sum++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(sameColor(x+i,y+i,color)){
<span style="white-space:pre"> </span>sum++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(sum==5){
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public boolean sameColor(int x,int y,String color){
<span style="white-space:pre"> </span>//Point point=new Point();
<span style="white-space:pre"> </span>for(Point point:chessList){
<span style="white-space:pre"> </span>if(point==null){
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(point.x==x && point.y==y && color.equals(point.color)){
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public boolean findChess(Point p){
<span style="white-space:pre"> </span>for(Point point:chessList){
<span style="white-space:pre"> </span>if(p.equals(point)){
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mouseEntered(MouseEvent arg0) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mouseExited(MouseEvent arg0) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mousePressed(MouseEvent arg0) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void mouseReleased(MouseEvent arg0) {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void paintComponent(Graphics g){
<span style="white-space:pre"> </span>super.paintComponent(g);
<span style="white-space:pre"> </span>for(int i=0;i<=ROWS;i++){ //draw chessBoead row Lines
<span style="white-space:pre"> </span>g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>for(int j=0;j<=COLS;j++){ //draw col lines
<span style="white-space:pre"> </span>g.drawLine(MARGIN+j*GRID_SPAN, MARGIN, MARGIN+j*GRID_SPAN, MARGIN+ROWS*GRID_SPAN);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>String color="";
<span style="white-space:pre"> </span>int x, y;
Point point=new Point();
<span style="white-space:pre"> </span>for (int i = 0; i < chessCount; i++) {
<span style="white-space:pre"> </span>point=chessList[i];
<span style="white-space:pre"> </span>color = point.getColor();
<span style="white-space:pre"> </span>x = point.getX();
<span style="white-space:pre"> </span>y = point.getY();
<span style="white-space:pre"> </span>//System.out.println("x :"+x+"y: "+y);
<span style="white-space:pre"> </span>if (color.equals("black")) {
<span style="white-space:pre"> </span>g.setColor(Color.black); //draw black chess
<span style="white-space:pre"> </span>g.fillOval(MARGIN + x * GRID_SPAN + x - 15, MARGIN + y
<span style="white-space:pre"> </span>* GRID_SPAN + y - 15, 30, 30);
<span style="white-space:pre"> </span>} else if (color.equals("white")) {
<span style="white-space:pre"> </span>g.setColor(Color.white);
<span style="white-space:pre"> </span>g.fillOval(MARGIN + x * GRID_SPAN + x - 15, MARGIN + y
<span style="white-space:pre"> </span>* GRID_SPAN + y - 15, 30, 30);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>g.dispose();
<span style="white-space:pre"> </span>}
}