📄 currentframe.java
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CurrentFrame extends JFrame {
private Minesweeper ms = null;
private int gridWidth = 15;
private int gridHeight = 15;
// Resources
private Image blockImage = null;
private Image zeroImage = null;
private Image oneImage = null;
private Image twoImage = null;
private Image threeImage = null;
private Image fourImage = null;
private Image fiveImage = null;
private Image sixImage = null;
private Image sevenImage = null;
private Image eightImage = null;
private Image mineImage = null;
private Image redMineImage = null;
private Image flagImage = null;
// Menu
private JMenuBar jMsJMenuBar = null;
private JMenu jGameMenu = null;
private JMenu jHelpMenu = null;
private JMenuItem jNewGameMenuItem = null;
private JMenuItem jAboutMenuItem = null;
private JPanel jMinesPanel = null;
private JPanel jSettingsPanel = null;
private JPanel jStatusPanel = null;
private JLabel jSettingsLabel = null;
private JLabel jStatusLabel = null;
/**
* This method initializes jMinesPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJMinesPanel() {
if (jMinesPanel == null) {
jMinesPanel = new JPanel() {
@Override
public void paint(Graphics arg0) {
// TODO Auto-generated method stub
super.paint(arg0);
arg0.setColor(Color.GRAY);
int startx = (jMinesPanel.getWidth() - ms.getWidth() * gridWidth - (ms.getWidth() + 1)) / 2;
int starty = (jMinesPanel.getHeight() - ms.getHeight() * gridHeight - (ms.getHeight() + 1)) / 2;
for (int i = 0; i <= ms.getWidth(); i++) {
arg0.drawLine(startx, starty + i * gridWidth + i, startx + ms.getHeight() * gridHeight + ms.getHeight(), starty + i * gridWidth + i);
}
for (int j = 0; j <= ms.getHeight(); j++) {
arg0.drawLine(startx + j * gridHeight + j, starty, startx + j * gridHeight + j, starty + ms.getWidth() * gridWidth + ms.getWidth());
}
int[][] minesBoard = ms.getMinesBoard();
boolean[][] minesField = ms.getMinesField();
for (int i = 0; i < ms.getWidth(); i++) {
for (int j = 0; j < ms.getHeight(); j++) {
if (minesField[i][j] == true) {
if (minesBoard[i][j] == -1) {
arg0.drawImage(redMineImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else if (minesBoard[i][j] == 0) {
arg0.drawImage(zeroImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else if (minesBoard[i][j] == 1) {
arg0.drawImage(oneImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else if (minesBoard[i][j] == 2) {
arg0.drawImage(twoImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else if (minesBoard[i][j] == 3) {
arg0.drawImage(threeImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else if (minesBoard[i][j] == 4) {
arg0.drawImage(fourImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else if (minesBoard[i][j] == 5) {
arg0.drawImage(fiveImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else if (minesBoard[i][j] == 6) {
arg0.drawImage(sixImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else if (minesBoard[i][j] == 7) {
arg0.drawImage(sevenImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else if (minesBoard[i][j] == 8) {
arg0.drawImage(eightImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
else {
;
}
}
else {
arg0.drawImage(blockImage, startx + i * gridWidth + i + 1, starty + j * gridHeight + j + 1, null, null);
}
}
}
}
};
jMinesPanel.setPreferredSize(new java.awt.Dimension(300,264));
jMinesPanel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
System.out.println("mouseClicked()");
// TODO Auto-generated Event stub mouseClicked()
int startx = (jMinesPanel.getWidth() - ms.getWidth() * gridWidth - (ms.getWidth() + 1)) / 2;
int starty = (jMinesPanel.getHeight() - ms.getHeight() * gridHeight - (ms.getHeight() + 1)) / 2;
int endx = startx + ms.getWidth() * gridWidth + (ms.getWidth() + 1);
int endy = starty + ms.getHeight() * gridHeight + (ms.getHeight() + 1);
int pointx = e.getX();
int pointy = e.getY();
if (pointx < startx || pointx > endx || pointy < starty || pointy > endy) {
return;
}
int result = ms.sweep((pointx - startx) / (gridWidth + 1), (pointy - starty) / (gridHeight + 1));
if (result == -1) {
jStatusLabel.setText("You are lost. ");
}
else if (result == 1) {
jStatusLabel.setText("Congratulations. You win the game. ");
}
else {
jStatusLabel.setText("It's your turn. ");
}
jMinesPanel.updateUI();
jStatusPanel.updateUI();
}
});
}
return jMinesPanel;
}
/**
* This method initializes jSettingsPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJSettingsPanel() {
if (jSettingsPanel == null) {
jSettingsLabel = new JLabel();
jSettingsLabel.setText("Settings");
jSettingsPanel = new JPanel();
jSettingsPanel.setLayout(new BorderLayout());
jSettingsPanel.add(jSettingsLabel, java.awt.BorderLayout.NORTH);
}
return jSettingsPanel;
}
/**
* This method initializes jStatusPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJStatusPanel() {
if (jStatusPanel == null) {
jStatusLabel = new JLabel();
jStatusLabel.setText("Status");
jStatusPanel = new JPanel();
jStatusPanel.setLayout(new BorderLayout());
jStatusPanel.add(jStatusLabel, java.awt.BorderLayout.NORTH);
}
return jStatusPanel;
}
/**
* This method initializes jMsJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar createJMenuBar() {
if (jMsJMenuBar == null) {
jMsJMenuBar = new JMenuBar();
jGameMenu = new JMenu("Game");
jGameMenu.setMnemonic(KeyEvent.VK_G);
jNewGameMenuItem = new JMenuItem("New Game");
jNewGameMenuItem.setMnemonic(KeyEvent.VK_N);
jHelpMenu = new JMenu("Help");
jHelpMenu.setMnemonic(KeyEvent.VK_H);
jAboutMenuItem = new JMenuItem("About");
jAboutMenuItem.setMnemonic(KeyEvent.VK_A);
jGameMenu.add(jNewGameMenuItem);
jHelpMenu.add(jAboutMenuItem);
jMsJMenuBar.add(jGameMenu);
jMsJMenuBar.add(jHelpMenu);
}
return jMsJMenuBar;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
/**
* This is the default constructor
*/
public CurrentFrame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
// Initialize a Minesweeper instance.
ms = new Minesweeper();
ms.initialize();
// Initialize resources.
try {
blockImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\block.jpg"));
zeroImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\0.jpg"));
oneImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\1.jpg"));
twoImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\2.jpg"));
threeImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\3.jpg"));
fourImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\4.jpg"));
fiveImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\5.jpg"));
sixImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\6.jpg"));
sevenImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\7.jpg"));
eightImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\8.jpg"));
mineImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\mine.jpg"));
redMineImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\redmine.jpg"));
flagImage = ImageIO.read(new FileInputStream("E:\\Java\\workspace\\Java Programming Technique Practice\\[Minesweeper]\\resources\\flag.jpg"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setSize(160, 250);
this.setPreferredSize(new java.awt.Dimension(160,250));
this.setResizable(false);
this.setName("MainFrame");
this.setTitle("Minesweeper");
this.add(getJMinesPanel(), java.awt.BorderLayout.CENTER);
this.add(getJSettingsPanel(), java.awt.BorderLayout.NORTH);
this.add(getJStatusPanel(), java.awt.BorderLayout.SOUTH);
}
private static void createAndShowGUI() {
// Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
// Create and set up the window.
CurrentFrame mainFrame = new CurrentFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setJMenuBar(mainFrame.createJMenuBar());
// Display the window.
mainFrame.pack();
mainFrame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -