資源簡介
JavaSE 實現的簡單版五子棋
使用JPanel的畫板畫棋盤跟棋子
可以存盤和復盤(文件讀寫)
代碼片段和文件信息
package?application;
import?java.awt.Color;
import?java.awt.Dimension;
import?java.awt.Graphics;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.MouseAdapter;
import?java.awt.event.MouseEvent;
import?java.io.BufferedInputStream;
import?java.io.BufferedOutputStream;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?javax.swing.JFileChooser;
import?javax.swing.JLabel;
import?javax.swing.JMenu;
import?javax.swing.JMenuBar;
import?javax.swing.JMenuItem;
import?javax.swing.JOptionPane;
import?javax.swing.WindowConstants;
import?javax.swing.Jframe;
/**
?*?@author?Kevin
?*?
?*/
public?class?FiveStep?extends?javax.swing.JPanel?implements?ActionListener?{
private?static?final?long?serialVersionUID?=?7315011316877886035L;
public?static?final?int?size?=?15;
public?static?final?int?chessPieceSize?=?40;
public?static?final?int?boardBound?=?10;
private?int?labelHeight?=?20;
private?int?width;
private?int?length;
private?JLabel?promptLab;
private?int[][]?pieces;
private?int?currentPieceX;
private?int?currentPieceY;
private?boolean?blackOrWhite;
private?Jframe?frame;
//?menu?bar?menus?and?menu?items
private?JMenuBar?menuBar;
private?JMenu[]?menus;
private?JMenuItem[][]?menuItems;
public?static?void?main(String[]?args)?{
Jframe?frame?=?new?Jframe();
frame.getContentPane().add(new?FiveStep(frame));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public?FiveStep()?{
super();
initGUI();
}
public?FiveStep(Jframe?j)?{
this();
this.frame?=?j;
this.frame.setJMenuBar(this.menuBar);
}
private?void?initGUI()?{
//?generate?the?width?and?length
this.length?=?FiveStep.size?*?FiveStep.chessPieceSize
+?(FiveStep.boardBound?*?2)?+?this.labelHeight;
this.width?=?FiveStep.size?*?FiveStep.chessPieceSize
+?(FiveStep.boardBound?*?2);
try?{
setPreferredSize(new?Dimension(this.width?this.length));
}?catch?(Exception?e)?{
e.printStackTrace();
}
this.promptLab?=?new?JLabel();
this.add(this.promptLab);
//?build?pieces
this.pieces?=?new?int[FiveStep.size][FiveStep.size];
for?(int?i?=?0;?i? for?(int?j?=?0;?j? this.pieces[i][j]?=?0;
}
}
//?initial?the?current?x?and?y
this.currentPieceX?=?0;
this.currentPieceY?=?0;
//?the?black?first
this.blackOrWhite?=?true;
//?add?event?handle
this.eventHandle();
//?set?the?board?color
this.setBackground(Color.orange);
//?set?the?label?prompt?the?black?first
this.promptLab.setText(“The?black?first“);
//?build?menu?bar
this.menuBar?=?new?JMenuBar();
this.menus?=?new?JMenu[1];
this.menus[0]?=?new?JMenu(“Game“);
this.menuItems?=?new?JMenuItem[1][];
this.menuItems[0]?=?new?JMenuItem[4];
this.menuItems[0][0]?=?new?JMenuItem(“New?Game“);
this.menuItems[0][1]?=?new?JMenuItem(“Save...“);
- 上一篇:遺傳算法 tsp java
- 下一篇:Java實現的打字游戲
評論
共有 條評論