该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.util.*;
//棋子类class PieceChess{public static int getDIAMETER(){ return DIAMETER;} public static void setDIAMETER(int aRADIUS){ DIAMETER = aRADIUS;}private Color color;private int XIndex;private int YIndex;private static int DIAMETER = 30;public PieceChess(Color color,int x,int y){ this.color = color; this.XIndex = x; this.YIndex = y;}public Color getColor(){ return color;}public void serColor(Color color){ this.color = color;}public int getXIndex(){ return XIndex;}public void setXIndex(int XIndex){ this.XIndex = XIndex;}public int getYIndex(){ return YIndex;}public void setYIndex(int YIndex){ this.YIndex = YIndex;}@Override public String toString(){ return this.color+"_"+this.XIndex+","+this.YIndex; }
} class ChessBoard1 extends JPanel implements MouseMotionListener{ private static final int SIDE = 36;//宽 private static final int MARGIN = 30;//边距 private static final int XROWS = 15;//行数 private static final int YROWS = 15;//列数 private Color Chesscolor = Color.BLACK; private Vector chess_list = new Vector(); private String str = ""; boolean isFinished=true; public ChessBoard1(){ //背景 setBackground(Color.ORANGE); this.addMouseMotionListener(this); this.addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent me){ System.out.println(Thread.currentThread()); int x = me.getX(); int y = me.getY(); int xIndex = -10; int yIndex = -10; //根据鼠标坐标取得棋子索引 if (x>SIDE/2&&y>SIDE/2&&x
}
//重新设置棋盘 void StartChess(){ Chesscolor = Color.BLACK; this.isFinished = false; str = "黑棋先行"; repaint(); } //悔棋 void goBack(){ if(isFinished){ JOptionpane.showMessageDialog(this,"棋局已经结束!"); return; } else { chess_list.remove(chess_list.size()-1);Chesscolor = Chesscolor == Color.BLACK ? Color.WHITE : Color.BLACK;}if (chess_list.size() !=0) str = chess_list.elementAt(chess_list.size()-1).getColor() == Color.BLACK ? "白棋请走":"黑棋请走";else str = "黑棋先行";this.repaint(); } //重新开始void reStart(){ Chesscolor = Color.BLACK;chess_list.removeAllElements();this.isFinished = false;str = "黑棋先行";this.repaint(); }
public void paintComponent(Graphics g){ super.paintComponent(g); //画棋盘 for(int i = 0;i it = chess_list.iterator(); while(it.hasNext()){ PieceChess pc = it.next(); int Ox = (pc.getYIndex()-1)*SIDE+MARGIN; int Oy = (pc.getXIndex()-1)*SIDE+MARGIN; g.setColor(pc.getColor()); g.fillOval(Ox-PieceChess.getDIAMETER()/2,Oy-PieceChess.getDIAMETER()/2,PieceChess.getDIAMETER(),PieceChess.getDIAMETER());
} }
public Dimension getPreferredSize() {return new Dimension(MARGIN*2+(XROWS-1)*SIDE,MARGIN*2+(YROWS-1)*SIDE); }
public void mouseMoved(MouseEvent me){ //获取鼠标坐标 int x = me.getX(); int y = me.getY(); int xIndex = ((y-MARGIN)+SIDE/2)/SIDE+1; int yIndex = ((x-MARGIN)+SIDE/2)/SIDE+1; //当鼠标在规定范围内设置默认形状或手型 if (xXROWS||yIndex>YROWS) { setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } else setCursor(new Cursor(Cursor.HAND_CURSOR)); } public void mouseDragged(MouseEvent me){
}
boolean isChess(int xIndex,int yIndex){
boolean findchess = false;Iterator it = chess_list.iterator();if (!it.hasNext())return false;while(it.hasNext()){PieceChess pc = it.next();if (pc.getXIndex() == xIndex&&pc.getYIndex() == yIndex){
findchess = true;break;}
}
return findchess;}
private boolean isOver(PieceChess pc){Color color = pc.getColor();int xIndex = pc.getIndex();int yIndex = pc.getIndex();int ChessCount = 1 ; //设置计数器//向东寻找for (int i = 1;i<=4 ;i++ ){if(!isSameChess(color,xIndex,yIndex+i))break;elseChessCount++;}//如果计数器达到5if (ChessCount ==5)return true;//向西寻找for (int i=1;i<=4 ;i++ ){if(!isSameChess(color,xIndex,yIndex-i))break;elseChessCount++;}if(ChessCount ==5) return true; ChessCount = 1; //向北寻找 for (int i=1;i<=4 ;i++ ) {if(!isSameChess(color,xIndex-i,yIndex))break;elseChessCount++;}if(ChessCount ==5) return true;//向南寻找for (int i=1;i<=4 ;i++ ){if(!isSameChess(color,xIndex+i,yIndex))break;elseChessCount++;}if(ChessCount ==5) return true; ChessCount = 1; //向东北方寻找for (int i=1;i<=4 ;i++ ) {if(!isSameChess(color,xIndex-i,yIndex+i))break;elseChessCount++; } //向西南方寻找 if(ChessCount ==5) return true; for (int i=1;i<=4 ;i++ ) {if(!isSameChess(color,xIndex+i,yIndex-i))break;elseChessCount++;}if(ChessCount ==5) return true; ChessCount = 1; //向西北方寻找 for (int i=1;i<=4 ;i++ ){if(!isSameChess(color,xIndex-i,yIndex-i))break;elseChessCount++;}//向东南方寻找 if(ChessCount ==5) return true; for (int i=1;i<=4 ;i++ ) {if(!isSameChess(color,xIndex+i,yIndex+i))break;elseChessCount++;}if(ChessCount ==5) return true; else return false;}
public boolean isSameChess(Color color,int xIndex,int yIndex){
boolean isSame = false; Iterator it = chess_list.iteratir(); if (!it.hasNext()) return false; while (it.hasNext()) { PieceChess pc = it.next(); if(pc.getXIndex() == xIndex&&pc.getYIndex() == yIndex&&pc.getColor() == color){ idSame = true; break; }}return isSame;}
public void mousePressed (MouseEvent me){ System.out.println(Thread.currentThread());int x = me.getX();int y = me.getY();int xIndex = -10;int yIndex = -10;if (isFinished){return;}//根据鼠标坐标取得棋子索引if(x>SIDE/2&&y>SIDE/2&&x
class Five_in_a_row1 extends JFrame{ private JMenuBar jmb; private JMenu jm_sys; private ChessBoard1 chessBoard; private JMenuItem jmi_start;private JMenuItem jmi_back;private JMenuItem jmi_restart;private JMenuItem jmi_over;private JMenuItem jmi_exit; //为菜单按钮添加事件 private void jmi_start_action(){ chessBoard.StartChess(); } private void jmi_back_action(){ chessBoard.goBack(); } private void jmi_restart_action(){ chessBoard.reStart(); } private void jmi_over_action(){ chessBoard.Over(); } private void jmi_exit_action(){ System.exit(0); }
public Five_in_a_row1(String name){ super(name); jmb = new JMenuBar(); jmb.setLayout(new FlowLayout(FlowLayout.LEFT)); jm_sys = new JMenu("系统"); jmi_start = new JMenuItem("开局"); jmi_back = new JMenuItem("悔棋"); jmi_restart = new JMenuItem("重新开局"); jmi_over = new JMenuItem("结束回合"); jmi_exit = new JMenuItem("退出系统");
chessBoard = new ChessBoard1(); jmb.add(jm_sys); //添加菜单按钮 jm_sys.add(jmi_start);jm_sys.add(jmi_back);jm_sys.add(jmi_restart);jm_sys.add(jmi_over);jm_sys.add(jmi_exit);jmb.add(jm_sys);
this.add(jmb,BorderLayout.NORTH); this.add(chessBoard); this.setLocation(410,75); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); //开始按钮事件 jmi_start.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_start_action(); } }); //悔棋按钮事件 jmi_.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_back_action(); } }); //重新开局事件 jmi_restart.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_restart_action(); } }); //结束按钮事件 jmi_over.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_back_action(); } }); //退出系统事件 jmi_exit.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ jmi_exit_action(); } });
}public static void main(String[] arge){ JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try{ UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); }catch (Exception e){ e.printStackTrace(); }Five_in_a_row1 fiar = new Five_in_a_row1("制作——五子棋——蛋鸡班");fiar.setVisible(true);
}
}