MyJFrame.java package chi1.JFrame; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JOptionPane; public class MyJFrame extends JFrame implements MouseListener { private static final long serialVersionUID = -46847020668994298L; BufferedImage buff = null; // 声明对象,为获取图片 int x , y ; // 保存所有的点多的坐标 int[][] allChess = new int[15][15] ; // 用一个二维数组保存黑白子信息。 String show1 = "" ; // 默认黑方下棋 //默认下黑子 , 判断下什么棋子。bool=true 下黑子 ; bool=false 下白子 // boolean bool = true ; // 判断是否赢了比赛, 如果赢了比赛, 就不能再继续下棋了, boolean win =true ; public MyJFrame(){ int width = Toolkit.getDefaultToolkit().getScreenSize().width ; int height = Toolkit.getDefaultToolkit().getScreenSize().height ; this.setTitle("五子棋"); this.setResizable(false); this.setSize(500,550 ); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocation((width-500)/2 , (height-550)/2); this.repaint(); this.addMouseListener(this); try { buff = ImageIO.read(new File("e:/wzq.jpg" )); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.setVisible(true); }; // boolean rekai = true ; int select = 0 ; boolean bool2=true ; @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub // System.out.println(e.getX()); // System.out.println(e.getY()); } @Override public void mousePressed(MouseEvent e) { boolean bool = true ; for(int x = 0 ; x<15 ; x++){ for(int y = 0 ; y < 15 ; y++){ if(allChess[x][y]==0){ if(select==2){ bool = true ; }else if(select==1){ bool = false; } } } } if(win==true){ // TODO Auto-generated method stub x = e.getX(); y = e.getY(); if(x>=65&&x<=418&&y>=70&&y<=430){ x = (x-60)/25; //得到距离附近的交叉点X轴坐标 y = (y-70)/25; if(allChess[x][y]==0){ if(bool==true){ //默认下黑棋子先 allChess[x][y] =1; show1 = "白方下棋"; // 提示下步棋下白色 bool = false ; // 修改标记, 下步棋应该由白方下 select = 1 ; }else{ allChess[x][y]= 2 ; show1 = "黑方下棋"; bool = true; select = 2 ; } this.repaint(); int zong = 0 ; for(int i = 0 ; i < 15 ; i++ ){ for(int k = 0 ; k < 15 ; k++){ if(allChess[i][k]!=0){ zong++ ; } } } if(zong==225){ JOptionPane.showMessageDialog(this, "游戏平局,请点击“重新开始”重 新比过!"); } boolean heng1 = this.myCheck1() ; //横 false boolean heng2 = this.myCheck2() ; boolean heng3 = this.myCheck3() ; boolean heng4 = this.myCheck4() ; if(heng1==true||heng2==true||heng3==true||heng4==true){ JOptionPane.showMessageDialog(this,"游戏结束:" + (allChess[x][y]==1?"黑方":"白方")+"胜利"); win = false ; } } } } if(e.getX()>=427&&e.getX()<=495&&e.getY()>193&&e.getY()<228){ // 关于设计 JOptionPane.showMessageDialog(this, "本程序用JAVA设计") ; } if(e.getX()>=301&&e.getX()<=407&&e.getY()>466&&e.getY()<532){ // 重新开始 JOptionPane.showMessageDialog(this, "重新游戏") ; for(int i = 0 ; i < 15 ; i++ ){ for(int k = 0 ; k < 15 ; k++){ allChess[i][k] = 0 ; } } show1 = "" ; bool = true ; win =true ; select = 0 ; bool2 = true ; this.repaint() ; } if(e.getX()>=425&&e.getX()<=491&&e.getY()>101&&e.getY()<140){ // 设置设计 for(int x = 0 ; x<15 ; x++){ for(int y = 0 ; y < 15 ; y++){ if(allChess[x][y]==0&&select!=0){ bool2 = false ; } } } if(bool2==true){ String input = JOptionPane.showInputDialog( "选择谁先下棋:白方输入“1” ; 黑方输入 “2” ") ; try{ select = Integer.parseInt(input); if(select!=1&&select!=2){ JOptionPane.showMessageDialog(this, "您输入的不是1或2,请重新设置!"); select = 0; } }catch(Exception e1){ JOptionPane.showMessageDialog(this, "输入有误,请重新输入!"); } }else{ JOptionPane.showMessageDialog(this, "游戏比赛中,不能设置此项目!!!"); } } if(e.getX()>=426&&e.getX()<=495&&e.getY()>382&&e.getY()<425){ // 结束设计 int a = JOptionPane.showConfirmDialog(this, "游戏是否结束?") ; if(a==0){ System.exit(0); } if(a==1){ JOptionPane.showMessageDialog(this, "欢迎您回到游戏来!"); } if(a==2){ JOptionPane.showMessageDialog(this, "小样, 请别再耍我!"); } } if(e.getX()>=433&&e.getX()<=495&&e.getY()>289&&e.getY()<334){ //游戏说明 JOptionPane.showMessageDialog(this, "五子棋游戏规则: 谁先把5颗棋子相连,谁 就是赢家") ; } } public void paint(Graphics g){ BufferedImage B1 = new BufferedImage(500 , 550 ,BufferedImage.TYPE_INT_ARGB); // 定义一个新的缓冲图片 Graphics g2 = B1.createGraphics(); g2.drawImage(buff, 0, 20, this); // 将图片显示在窗体上 g2.setColor(Color.BLACK); // 设置画笔颜色为黑色 g2.fillOval(135, 144, 6, 6); g2.fillOval(334, 144, 6, 6); // 画几个定点 g2.fillOval(138, 342, 6, 6); g2.fillOval(334, 342, 6, 6); g2.fillOval(236, 242, 6, 6); g2.setFont(new Font("黑体" , Font.BOLD , 20)); g2.drawString("游戏信息:"+show1, 83, 49); //显示当前由哪一方下棋 g2.drawString("黑方:", 65, 473); g2.drawString("白方:", 62, 518); for(int i = 0 ; i < 15 ; i++ ){ // 循环判断 for(int j = 0 ; j < 15 ; j++){ if(allChess[i][j]==1){ //黑子 int tempX = i*25 + 63 ; int tempY = j*25 + 70 ; g2.fillOval(tempX-8, tempY-8, 16,16); } if(allChess[i][j]==2){ //白子 int tempX = i*25 + 63 ; int tempY = j*25 + 70 ; g2.setColor(Color.WHITE); g2.fillOval(tempX-8, tempY-8, 16,16); g2.setColor(Color.BLACK); g2.drawOval(tempX-8, tempY-8, 16, 16); } } } g.drawImage(B1, 0, 5, this); // 把B1这张图片在窗体显示出来 } public boolean myCheck1() { // 判断横向是否有五子相连 int count = 1; boolean heng = false ; // int color = allChess[x][y] ; // 判断颜色 // TODO Auto-generated method stub int i = 1 ; while(x+i<=14&&color == allChess[x+i][y]){ i++ ; count++ ; // System.out.println(count); //System.out.println(i); } i = 1 ; while(x-i>=0&&color == allChess[x-i][y]){ i++; count++ ; } if(count>=5){ heng = true ; } return heng ; } public boolean myCheck2() { // 判断纵向是否有五子相连 int count = 1; boolean zhong = false ; int color = allChess[x][y] ; // TODO Auto-generated method stub int i = 1 ; while((y+i)<=14&&color == allChess[x][y+i]){ i++ ; count++ ; } i = 1 ; while((y-i)>=0&&color == allChess[x][y-i]){ i++; count++ ; } if(count>=5){ zhong = true ; } return zhong ; } public boolean myCheck3() { // 判断右斜向是否有五子相连 int count = 1; boolean zhong = false ; int color = allChess[x][y] ; // TODO Auto-generated method stub int i = 1 ; while((x+i)<=14&&(y-i)>=0&&color == allChess[x+i][y-i]){ i++ ; count++ ; } i = 1 ; while((x-i)>=0&&(y+i)<=14&&color == allChess[x-i][y+i]){ i++; count++ ; } if(count>=5){ zhong = true ; } return zhong ; } public boolean myCheck4() { // 判左斜向是否有五子相连 int count = 1; boolean zhong = false ; int color = allChess[x][y] ; // TODO Auto-generated method stub int i = 1 ; while((x-i)>=0&&(y-i)>=0&&color == allChess[x-i][y-i]){ i++ ; count++ ; } i = 1 ; while((x+i)<=14&&(y+i)<=14&&color == allChess[x+i][y+i]){ i++; count++ ; } if(count>=5){ zhong = true ; } return zhong ; } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } } Text01.java package chi1.Text; import chi1.JFrame.MyJFrame; public class Text01 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MyJFrame jf = new MyJFrame() ; // String b1 = JOptionPane.showInputDialog("请输入你的名字:") ; //int b = JOptionPane.showConfirmDialog(jf, "我的信息:" + b1); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10175.html