⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ui.java

📁 扫雷完整的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on Mar 24, 2005
 *
 * TODO the view of MVC structure. to create user interface and show result
 * automatically when model data are changed
 */
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.HashMap;
import java.util.Observable;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import javax.swing.JSeparator;
import javax.swing.JCheckBoxMenuItem;

import javax.swing.UIManager;


import model.CellNotifingObject;
import model.Location;
import model.Model;
import model.NotifingObject;
import model.SaveAndLoad;

import control.CoreAction;
import control.MenuAction;

import java.util.Observer;

/**
 * @author mqqqvpppm
 * 
 * TODO the view of MVC structure. to create user interface and show result
 * automatically when model data are changed
 */
public class UI implements Observer {

	public UI(Model model) {
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//			UIManager
//					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			
		}
		this.model = model;

		componentsInstantiation();
		initComponents();
		//		mainFrame.setVisible(true);
	}

	//	public MyButton[][] getButtons() {
	//		return jLabelButtons;
	//	}

	public JFrame getMainFrame() {
		return mainFrame;
	}

	public JMenuItem getMenuItem(String name) {
		if (menuItemMap == null)
			return null;
		return (JMenuItem) menuItemMap.get(name);
	}

	public String[] getUserNames() {
		return userNames;
	}

	public int getButtonSize() {
		return buttonSize;
	}

	public void drawImage(Image image, int x, int y) {
		g2.drawImage(image, x * buttonSize, y * buttonSize, null);
		//		mainFrame.getContentPane().getGraphics().drawImage(imageBoard,15,head.getHeight()+15,null);
	}

	public void drawImage(Image image, Location location) {
		g2.drawImage(image, location.x * buttonSize, location.y * buttonSize,
				null);
		//		mainFrame.getContentPane().getGraphics().drawImage(imageBoard,15,head.getHeight()+15,null);
	}

	public void updateImage() {
		gameImagePanel.repaint();
	}

	public int getImageX() {
		return margin;
	}

	public int getImageY() {
		return headHeight;
	}

	public int[] getScore() {
		return score;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 */
	private void initComponents() {
		mainFrame
				.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		//		mainFrame.setVisible(true);
		mainFrame.setResizable(false);
		gameImagePanel.setLayout(null);
		gameImagePanel.setBackground(new Color(236, 233, 216));

		mainFrame.setContentPane(gameImagePanel);
		gameImagePanel.add(start);
		gameImagePanel.add(mineNumberLabel);
		gameImagePanel.add(time);
		
		gameImagePanel.addMouseListener(coreListener);

		//build menu bar
		group.add(easy);
		group.add(hard);
		group.add(hell);
		group.add(custom);

		game.setText("Game");
		startItem.setText("Start");
		game.add(startItem);
		startItem.addActionListener(menuListener);
		menuItemMap.put("start", startItem);
		game.add(jSeparator1);

		easy.setText("Easy");
		game.add(easy);
		easy.addActionListener(menuListener);
		menuItemMap.put("easy", easy);

		hard.setText("Hard");
		hard.addActionListener(menuListener);
		game.add(hard);
		menuItemMap.put("hard", hard);

		hell.setText("Hell");
		hell.addActionListener(menuListener);
		game.add(hell);
		menuItemMap.put("hell", hell);

		custom.setText("Custom");
		custom.addActionListener(menuListener);
		game.add(custom);
		menuItemMap.put("custom", custom);

		game.add(jSeparator2);

		//		music.setText("Music");
		//		music.setEnabled(false);
		//		game.add(music);
		//		menuItemMap.put("music", music);
		
		mark.setText("Mark");
		game.add(mark);
		menuItemMap.put("mark",mark);
		mark.addActionListener(menuListener);

		record.setText("Record");
		game.add(record);
		menuItemMap.put("record", record);
		record.addActionListener(menuListener);

		game.add(jSeparator3);

//		language.setText("Language");
//		english.setText("English");
//		english.setEnabled(false);
//		language.add(english);
//		menuItemMap.put("english", english);
//
//		chinese.setText("Chinese");
//		chinese.setEnabled(false);
//		language.add(chinese);
//		menuItemMap.put("chinese", chinese);
//
//		game.add(language);

		exit.setText("Exit");
		exit.addActionListener(menuListener);
		game.add(exit);
		menuItemMap.put("exit", exit);

		jMenuBar.add(game);

		saveAndLoad.setText("Save/Load");

		save.setText("Save");
		save.addActionListener(menuListener);
		saveAndLoad.add(save);
		menuItemMap.put("save", save);

		load.setText("Load");
		load.addActionListener(menuListener);
		saveAndLoad.add(load);
		menuItemMap.put("load", load);

		jMenuBar.add(saveAndLoad);

		help.setText("help");
		//		contents.setText("Contents");
		//		contents.setEnabled(false);
		//		help.add(contents);
		//		menuItemMap.put("contents", contents);

		about.setText("About");
		about.addActionListener(menuListener);
		help.add(about);
		menuItemMap.put("about", about);

		jMenuBar.add(help);
		mainFrame.setJMenuBar(jMenuBar);
		//build menu bar end

//		mainFrame.getContentPane().setLayout(null);
//		mainFrame.addMouseListener(coreListener);

		//build head panel
		//		head.setBorder(new javax.swing.border.TitledBorder(null, "",
		//				javax.swing.border.TitledBorder.CENTER,
		//				javax.swing.border.TitledBorder.DEFAULT_POSITION));
		//		head.setLayout(null);

		start.setIcon(Images.START_BEGIN);
		start.addActionListener(coreListener);

		Font font = new Font("Serif", Font.BOLD, 12);
		//		mineNumberLabel.setFont(font);
		//		time.setFont(font);
		//
		//		mineNumberLabel.setHorizontalAlignment(JLabel.CENTER);
		//		time.setHorizontalAlignment(JLabel.CENTER);

		mineNumberLabel.setFontAndBoard(new Font("SansSerif", Font.BOLD, 14),
				3, 2);
		time.setFontAndBoard(new Font("SansSerif", Font.BOLD, 14), 3, 2);

		mineNumberLabel.setNumber(String.valueOf(model.getMineNumber()));
		time.setNumber("0");

		mineNumberLabel.setBackground(Color.BLACK);
		time.setBackground(Color.BLACK);

		mineNumberLabel.setFontColor(Color.RED);
		time.setFontColor(Color.RED);

		//		head.add(mineNumberLabel);
		//		head.add(start);
		//		head.add(time);

		//		mainFrame.getContentPane().add(head);
		//build head panel end

		//build body panel
		//		mainFrame.getContentPane().add(body);
		//build body panel end
		mainFrame.setTitle("JMineSweeper");
		//		Image mainFrameIcon = Images.createImage("images/mainFrameIcon.gif");
		//		if (mainFrameIcon != null)
		mainFrame.setIconImage(Images.FRAME_IMAGE);

		//paint GUI in detail
		restart(model.getWidth(), model.getHeight(), model.getMineNumber(),
				model.getType());

		mainFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				try {
					io.saveType();
					//					clearRecord();
					io.saveRecord(userNames, score);
					io.closeStream();
				} catch (IOException e1) {
				}
			}
		});

		try {
			io.loadRecord(userNames, score);
		} catch (IOException e1) {
			int i;
			int length = userNames.length;
			for (i = 0; i != length; i++) {
				userNames[i] = "Unknow Name";
				score[i] = 999;
			}
		}
	}

	//Instantiation of components
	private void componentsInstantiation() {
		mainFrame = new JFrame();

		gameImagePanel = new GameImagePanel();

		//		head = new JPanel();
		//		body = new JPanel();
		//		mineNumberPanel = new JPanel();
		mineNumberLabel = new NumberLabel();
		//		startPanel = new JPanel();
		start = new JButton();
		//		timePanel = new JPanel();
		time = new NumberLabel();

		jMenuBar = new JMenuBar();
		game = new JMenu();
		startItem = new JMenuItem();
		jSeparator3 = new JSeparator();
		//        easy = new JRadioButtonMenuItem();
		easy = new JCheckBoxMenuItem();
		hard = new JCheckBoxMenuItem();
		hell = new JCheckBoxMenuItem();
		custom = new JCheckBoxMenuItem();

		music = new JCheckBoxMenuItem();
		record = new JMenuItem();
		mark = new JCheckBoxMenuItem();

		language = new JMenu();
		english = new JMenuItem();
		chinese = new JMenuItem();

		exit = new JMenuItem();
		saveAndLoad = new JMenu();
		save = new JMenuItem();
		jSeparator1 = new JSeparator();
		load = new JMenuItem();
		help = new JMenu();
		contents = new JMenuItem();
		jSeparator2 = new JSeparator();
		about = new JMenuItem();

		menuItemMap = new HashMap(15);
		coreListener = new CoreAction(model, this);
		menuListener = new MenuAction(model, this);

		group = new ButtonGroup();

		try {
			io = new SaveAndLoad(model);
		} catch (IOException e) {
			System.out
					.println("can not find the save file, game will use default setting");
		}

		userNames = new String[3];
		score = new int[3];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -