Java saolei.txt
上传用户:fuelmate
上传日期:2021-07-26
资源大小:3k
文件大小:13k
- //package com.makoto.MineSweeping;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.geom.*;
- import java.awt.image.*;
- import java.util.*;
- import javax.swing.*;
- import java.io.*;
- import javax.imageio.*;
- import javax.swing.border.*;
- class MineSweeping extends JFrame{
- private ResourceBundle bundle;
- private Hashtable numIcons;
- private Hashtable statusIcons;
- private Hashtable bomIcons;
- private String numIconResource = "numIcons";
- private String statusIconResource = "statusIcons";
- private String bomIconResource = "bomIcons";
- private String menuResource = "menu";
- private String toolBarResource = "toolBar";
- private String menuSuffix = "Menu";
- private String labelSuffix = "Label";
- private String imageSuffix = "Image";
- private String resetActionName = "reset";
- private String optionsActionName = "options";
- private Action[] defaultActions = {
- new ResetAction(),
- new OptionsAction(),
- new AboutAction()
- };
- private Hashtable ActionTable = new Hashtable();
- private Hashtable menuItems = new Hashtable();
- private Hashtable toolBarButtons = new Hashtable();
- private Hashtable regionsTable;
- private ArrayList regionsList;
- private JPanel mineArea = new JPanel();
- private JDialog aboutBox;
- private Options options = new Options(this);
- private int XSize = 20;
- private int YSize = 20;
- private int bom = 50;
- private int currentBom = 0;
- private int flag = 0;
- public int MAX_XSIZE = 30;
- public int MAX_YSIZE = 30;
- public MineSweeping(){
- bundle = getResourceBundle();
- numIcons = createIcons(numIconResource);
- statusIcons = createIcons(statusIconResource);
- bomIcons = createIcons(bomIconResource);
- for(int i = 0;i < defaultActions.length;i++){
- Action a = defaultActions[i];
- ActionTable.put(a.getValue(Action.NAME), a);
- }
- setJMenuBar(createMenuBar(menuResource));
- add(mineArea,BorderLayout.CENTER);
- add(createToolBar(toolBarResource),BorderLayout.NORTH);
- createArea();
- setTitle(getString("title"));
- pack();
- addWindowListener(new Closer());
- middle(this);
- setVisible(true);
- setResizable(false);
- }
- public static void main(String[] args){
- new MineSweeping();
- }
- // *******************************************************
- // ******************** Creates ***********************
- // *******************************************************
- public JPanel createToolBar(String resource){
- JPanel toolBar = new JPanel(new FlowLayout(FlowLayout.CENTER));
- String resources = getString(resource);
- String[] buttons = resources.split("\s");
- for(int i = 0;i < buttons.length;i++){
- JButton button = createToolBarButton(buttons[i]);
- if(button != null)
- toolBar.add(button);
- }
- return toolBar;
- }
-
- public JButton createToolBarButton(String buttonName){
- JButton button = new JButton(new ImageIcon(getString(buttonName + imageSuffix)));
- button.setFocusPainted(false);
- //button.setBorderPainted(false);
- button.setContentAreaFilled(false);
- if(buttonName.equals("reset")){
- button.setIcon((Icon)bomIcons.get("bomStart"));
- }
- Action action = getAction(buttonName);
- if(action == null){
- button.setEnabled(false);
- }else{
- button.addActionListener(action);
- }
- toolBarButtons.put(buttonName,button);
- return button;
- }
-
- public void createArea(){
- ((AbstractButton)toolBarButtons.get("reset")).setIcon((Icon)bomIcons.get("bomStart"));
- if(XSize > MAX_XSIZE)
- XSize = MAX_XSIZE;
- if(YSize > MAX_YSIZE)
- YSize = MAX_YSIZE;
- mineArea.removeAll();
- regionsTable = new Hashtable();
- regionsList = new ArrayList();
- mineArea.setLayout(new GridLayout(YSize,XSize));
- for(int i = 0;i < YSize;i++){
- for(int j = 0;j < XSize;j++){
- Region a = new Region(j,i);
- regionsTable.put(j + ":" + i,a);
- regionsList.add(a);
- mineArea.add(a);
- }
- }
- mineArea.revalidate();
- pack();
- createBoms();
- }
- public void createBoms(){
- int[] boms = new int[bom];
- int m = bom;
- boolean repeat = false;
- int num = 0;
- int index = 0;
- currentBom = bom;
- while(m > 0){
- num = (int)(Math.random() * (XSize * YSize));
- for(int i = 0;i < bom - m;i++){
- if(num == boms[i]) repeat = true;
- }
- if(!repeat){
- boms[bom - m] = num;
- m--;
- }
- repeat = false;
- }
- for(int i = 0;i < boms.length;i++){
- ((Region)regionsList.get(boms[i])).isBom = true;
- }
- for(int i = 0;i < YSize;i++){
- for(int j = 0;j < XSize;j++){
- Region r = (Region)regionsTable.get(j + ":" + i);
- ArrayList surround = getSurroundRegions(j,i);
- int surroundNum = 0;
- for(int n = 0;n < surround.size();n++){
- if(surround.get(n) != null)
- surroundNum++;
- }
- int bnum =getNumOfBoms(surround);
- r.round = bnum;
- if(r.round == surroundNum&&r.isBom){
- r.isBom = false;
- currentBom--;
- }
- }
- }
- flag = currentBom;
- }
- public Hashtable createIcons(String resource){
- String resources = getString(resource);
- String[] icons=resources.split("\s");
- Hashtable table = new Hashtable();
- for(int i = 0;i < icons.length;i++){
- table.put(icons[i],new ImageIcon(getString(icons[i])));
- }
- return table;
- }
- public JMenuBar createMenuBar(String resource){
- JMenuBar menuBar = new JMenuBar();
- String resources = getString(resource);
- String[] menus=resources.split("\s");
- for(int i = 0;i < menus.length;i++){
- JMenu menu = createMenu(menus[i]);
- if(menu != null)
- menuBar.add(menu);
- }
- return menuBar;
- }
- public JMenu createMenu(String menuName){
- JMenu menu = new JMenu(getString(menuName + menuSuffix + labelSuffix));
- String resources = getString(menuName + menuSuffix);
- String[] items=resources.split("\s");
- for(int i = 0;i < items.length;i++){
- JMenuItem item = createMenuItem(items[i]);
- if(item != null)
- menu.add(item);
- }
- return menu;
- }
- public JMenuItem createMenuItem(String itemName){
- JMenuItem item = new JMenuItem(getString(itemName + labelSuffix));
- Action action = getAction(itemName);
- if(action == null){
- item.setEnabled(false);
- }else{
- item.addActionListener(action);
- }
- menuItems.put(itemName,item);
- return item;
- }
- // *******************************************************
- // ******************** Gets ***********************
- // *******************************************************
-
- public Action getAction(String name){
- return (Action)ActionTable.get(name);
- }
- public ArrayList getSurroundRegions(int x,int y){
- ArrayList surroundRegions = new ArrayList();
- surroundRegions.add(regionsTable.get(x + ":" + (y + 1)));
- surroundRegions.add(regionsTable.get(x + ":" + (y - 1)));
- surroundRegions.add(regionsTable.get((x + 1) + ":" + (y + 1)));
- surroundRegions.add(regionsTable.get((x + 1) + ":" + y));
- surroundRegions.add(regionsTable.get((x + 1) + ":" + (y - 1)));
- surroundRegions.add(regionsTable.get((x - 1) + ":" + (y + 1)));
- surroundRegions.add(regionsTable.get((x - 1) + ":" + y));
- surroundRegions.add(regionsTable.get((x - 1) + ":" + (y - 1)));
- return surroundRegions;
- }
- public int getNumOfBoms(ArrayList regions){
- int boms = 0;
- for(int i = 0;i < regions.size();i++){
- Region region = (Region)regions.get(i);
- if(region != null){
- if(region.isBom){
- boms++;
- }
- }
- }
- return boms;
- }
- /**
- * Returns the resource bundle associated with this program. Used
- * to get accessable and internationalized strings.
- */
- public ResourceBundle getResourceBundle() {
- if(bundle == null) {
- bundle = ResourceBundle.getBundle("resources.minesweeping");
- }
- return bundle;
- }
- /**
- * This method returns a string from the demo's resource bundle.
- */
- public String getString(String key) {
- String value = null;
- try {
- value = getResourceBundle().getString(key);
- } catch (MissingResourceException e) {
- System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);
- }
- return value;
- }
-
- protected Frame getFrame() {
- for (Container p = getParent(); p != null; p = p.getParent()) {
- if (p instanceof Frame) {
- return (Frame) p;
- }
- }
- return null;
- }
-
- public int getXSize(){
- return XSize;
- }
-
- public int getYSize(){
- return YSize;
- }
-
- public int getBom(){
- return bom;
- }
-
- public void middle(Component component){
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- Dimension frameSize = component.getSize();
- component.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
- }
-
- public void openSurround(Region region){
- ArrayList stack = new ArrayList();
- stack.add(region);
- while(stack.size() > 0){
- Region sr = (Region)stack.get(stack.size() - 1);
- stack.remove(stack.size() - 1);
- ArrayList surroundRegions = getSurroundRegions(sr.x,sr.y);
- for(int i = 0;i < surroundRegions.size();i++){
- Region rr = (Region)surroundRegions.get(i);
- if(rr != null){
- if(!rr.isOpened){
- if(rr.isTaked)
- flag++;
- rr.open();
- if(rr.round == 0)
- stack.add(rr);
- }
- }
- }
- }
- }
-
- public void bomWin(){
- ((AbstractButton)toolBarButtons.get("reset")).setIcon((Icon)bomIcons.get("bomWin"));
- for(int i = 0;i < regionsList.size();i++){
- ((Region)regionsList.get(i)).open();
- }
- }
-
- public void bomLoss(){
- ((AbstractButton)toolBarButtons.get("reset")).setIcon((Icon)bomIcons.get("bomLoss"));
- for(int i = 0;i < regionsList.size();i++){
- ((Region)regionsList.get(i)).open();
- }
- }
-
- // *******************************************************
- // ******************** Sets ***********************
- // *******************************************************
-
- public void setXSize(int XSize){
- this.XSize = XSize;
- }
-
- public void setYSize(int YSize){
- this.YSize = YSize;
- }
-
- public void setBom(int bom){
- this.bom = bom;
- }
-
- // *******************************************************
- // ******************** Actions ***********************
- // *******************************************************
-
- class ResetAction extends AbstractAction {
- ResetAction() {
- super(resetActionName);
- }
- public void actionPerformed(ActionEvent e) {
- ((AbstractButton)toolBarButtons.get("reset")).setIcon((Icon)bomIcons.get("bomStart"));
- createArea();
- }
- }
-
- class OptionsAction extends AbstractAction {
- OptionsAction() {
- super(optionsActionName);
- }
- public void actionPerformed(ActionEvent e) {
- options.setLocationRelativeTo(MineSweeping.this);
- options.show();
- }
- }
- private class AboutAction extends AbstractAction {
- AboutAction() {
- super("about");
- }
- public void actionPerformed(ActionEvent e) {
- if(aboutBox == null){
- AboutPanel panel = new AboutPanel(getString("AboutBox.icon"));
- panel.setLayout(new BorderLayout());
- aboutBox = new JDialog(getFrame(), getString("AboutBox.title"), false);
- aboutBox.setResizable(false);
- aboutBox.getContentPane().add(panel, BorderLayout.CENTER);
- JPanel buttonpanel = new JPanel();
- buttonpanel.setBorder(new javax.swing.border.EmptyBorder(0, 0, 3, 0));
- buttonpanel.setOpaque(false);
- JButton button = (JButton) buttonpanel.add(new JButton(getString("AboutBox.ok_button_text")));
- panel.add(buttonpanel, BorderLayout.SOUTH);
- button.addActionListener(new OkAction(aboutBox));
- }
- aboutBox.pack();
- aboutBox.setLocationRelativeTo(MineSweeping.this);
-
-
-
- aboutBox.show();
- }
- }
- private class OkAction extends AbstractAction {
- JDialog aboutBox;
- protected OkAction(JDialog aboutBox) {
- super("OkAction");
- this.aboutBox = aboutBox;
- }
- public void actionPerformed(ActionEvent e) {
- aboutBox.setVisible(false);
- }
- }
- protected static final class Closer extends WindowAdapter {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- }
- // *******************************************************
- // ******************** InnerClasses ******************
- // *******************************************************
- class Region extends JLabel{
- int x;
- int y;
- int round = 0;
- boolean isBom = false;
- boolean isTaked = false;
- boolean isOpened = false;
- MineSweeping mineSweeping;
- public Region(int x,int y){
- this.x = x;
- this.y = y;
- this.mineSweeping = mineSweeping;
- setIcon((Icon)statusIcons.get("status1Icon"));
- addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e){
- if(!isOpened){
- setIcon((Icon)statusIcons.get("status2Icon"));
- }
- }
- public void mouseReleased(MouseEvent e){
- if(!isOpened){
- if(e.getButton() == 3){
- setIcon((Icon)statusIcons.get("status1Icon"));
- sign();
- }
- else{
- if(isBom){
- bomWin();
- }
- else{
- open();
- if(round == 0){
- openSurround(Region.this);
- }
- }
- }
- }
- }
- });
- }
- public void open(){
- if(!isOpened){
- isOpened = true;
- if(isBom){
- setIcon((Icon)statusIcons.get("status4Icon"));
- if(isTaked){
- setIcon((Icon)statusIcons.get("status5Icon"));
- }
- }
- else{
- setIcon((Icon)numIcons.get("num" + round + "Icon"));
- }
- }
- }
- public void sign(){
- if(!isTaked){
- if(flag > 0){
- setIcon((Icon)statusIcons.get("status3Icon"));
- flag--;
- isTaked = true;
- if(isBom){
- currentBom--;
- }
- if(currentBom < 1)
- bomLoss();
- }
- }
- else{
- setIcon((Icon)statusIcons.get("status1Icon"));
- isTaked = false;
- flag++;
- if(isBom){
- currentBom++;
- }
- }
- }
- }
- }
-