📄 netfive1.java
字号:
package NetFive;import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JOptionPane;import javax.swing.WindowConstants;import javax.swing.JFrame;import javax.swing.JLabel;import sun.print.BackgroundLookupListener;/*** This code was edited or generated using CloudGarden's Jigloo* SWT/Swing GUI Builder, which is free for non-commercial* use. If Jigloo is being used commercially (ie, by a corporation,* company or business for any purpose whatever) then you* should purchase a license for each developer using Jigloo.* Please visit www.cloudgarden.com for details.* Use of Jigloo implies acceptance of these licensing terms.* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.*/public class NetFive1 extends javax.swing.JPanel { Graphics2D gs=null; int t=1; int x=0; int y=0; int a[][]=new int[100][100]; int b[][]=new int[100][100]; Image chessBoard; Image blackStone; Image whiteStone; String path=System.getProperty("user.dir")+"\\1.gif"; String path1=System.getProperty("user.dir")+"\\2.gif"; String path2=System.getProperty("user.dir")+"\\3.gif"; public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new NetFive1()); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.pack(); frame.setVisible(true); } public NetFive1() { super(); initGUI(); } public void paint(Graphics g){ gs=(Graphics2D)g; gs.drawImage(chessBoard, 0, 0, null); for(int i=0;i<15;i++) { for(int j=0;j<15;j++) { if(a[i][j]==1) { gs.drawImage(blackStone, i*40, j*40, null); } else if(a[i][j]==0) { gs.drawImage(whiteStone, i*40,j*40, null); } } } } private void initGUI() { try { setPreferredSize(new Dimension(600, 600)); this.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { thisMouseClicked(evt); } }); } catch (Exception e) { e.printStackTrace(); } for(int i=0;i<15;i++) { for(int j=0;j<15;j++) a[i][j]=2; } initImages(); } public void initImages() { try { chessBoard = ImageIO.read(new File(path)); blackStone = ImageIO.read(new File(path1)); whiteStone = ImageIO.read(new File(path2)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void thisMouseClicked(MouseEvent evt) { //鼠标事件,获取鼠标的x轴和y轴 x=evt.getX(); y=evt.getY(); x=x/40*40; y=y/40*40; add(x,y); } public void add(int x,int y){ //更新数组内容 switch(t){ case 1: if(a[x/40][y/40]==2) { a[x/40][y/40]=1; t=2; } else { t=1; break; } if(judgeY(1,y/40)||judgeX(1,x/40)||judgeXY1(1,x/40,y/40)) JOptionPane.showMessageDialog(null, "恭喜!黑棋获胜!"); break; case 2: if(a[x/40][y/40]==2) { a[x/40][y/40]=0; t=1; } else { t=2; break; } if(judgeY(0,y/40)||judgeX(0,x/40)||judgeXY1(0,x/40,y/40)) JOptionPane.showMessageDialog(null, "恭喜!白棋获胜!"); break; } repaint(); System.out.println(x/40+" "+y/40); System.out.println(x+" "+y); for(int i=0;i<15;i++) { for(int j=0;j<15;j++) System.out.print(a[i][j]); System.out.println(); } } public boolean judgeY(int state,int y){ //判断横坐标 int num=0; for(int i=0;i<15;i++) { if(a[i][y]==state) { num++; if(num==5) return true; } else num=0; } return false; } public boolean judgeX(int state,int x){ //判断纵坐标 int num=0; for(int i=0;i<15;i++) { if(a[x][i]==state) { num++; if(num==5) return true; } else num=0; } return false; } public boolean judgeXY1(int state,int x,int y) //判断左斜和右斜 { boolean t=false; for(int i=2;i<13;i++) { for(int j=2;j<13;j++) if(a[i][j]==state&&a[i-1][j-1]==state&&a[i-2][j-2]==state&&a[i+1][j+1]==state&&a[i+2][j+2]==state) { t=true; } else if(a[i][j]==state&&a[i-1][j+1]==state&&a[i-2][j+2]==state&&a[i+1][j-1]==state&&a[i+2][j-2]==state) { t=true; } } return t; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -