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

📄 mui.java

📁 一个Java写的数独游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * This file is part of MUSoSu. * * MUSoSu is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3 of the License. * * MUSoSu is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MUSoSu.  If not, see <http://www.gnu.org/licenses/>. */package musUI;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Image;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.io.File;import java.io.InputStream;import java.util.Vector;import javax.imageio.ImageIO;import javax.swing.JButton;import javax.swing.JCheckBoxMenuItem;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import sudoku.Sudoku;import sudoku.SudokuEvent;import sudoku.SudokuListener;import sudokuSolver.SolvedSquare;import sudokuSolver.Solver;/** * <p>class <code>Mui</code> does most of the GUI work and implements the * main frame of the application.<br> * Creates the menu and hanldes it's events (implementing  * <code>ActionListener</code> interface).<br>Initialises all the components</p> *  * @see GamePanel * @see MStatusPanel * @author Visvardis Marios *  */@SuppressWarnings("serial")public class Mui extends JFrame implements ActionListener {	// Visual Components	private JMenuBar menuBar = new JMenuBar();	private JLabel label;	private JPanel superPanel;	private GamePanel panel;	private MStatusPanel statusPanel;	private JPanel newCustomPanel;	private JPanel southPanel;	private JPanel titlePanel;	private JPanel middle;	private MSolvePanel msp;		private JButton save;	private JButton savenopen;	private JButton cancel;	private JButton getStr;		// game represents the game currently processed and displayed	private Sudoku game;	private Sudoku virgin;  // A untouched copy of the original game		// Solver	private Solver solver;		// Holding the path of the last saved file or null if not defined	private String saveName;		// preloaded	private Vector<String> preloaded;	private Vector<String> preloadedNames;		private Settings settings;		private String version;		// For the Menu	private JMenu fileMenu;     private JMenu viewMenu;    private JMenu solverMenu;    private JMenu aboutMenu;    private JMenu newMenu;        private JMenuItem newCustomM;    private JMenuItem newM;    private JMenuItem openM;    private JMenu openPresetM;    JMenuItem[] preMenuItems;    private JMenuItem saveAsM;    private JMenuItem restartM;    private JMenuItem closeM;    private JMenuItem printM;    private JMenuItem exitM;    private JCheckBoxMenuItem showSolvePanelM;    private JCheckBoxMenuItem showTraceM;    private JCheckBoxMenuItem showTTM;    private JMenuItem clearMarksM;    private JMenuItem saveThemeM;     private JMenuItem loadThemeM;    private JMenuItem loadDefaultThemeM;    private JMenuItem saveDefaultM;    private JMenuItem prefsM;    private JMenuItem undoM;    private JMenuItem redoM;    private JMenuItem hintM;    private JMenuItem solveAtOnceM;    private JMenuItem solveM;    private JMenuItem aboutM;    	/**	 * Mui - Constructor	 * All the work is done in initComponents, createMenu, initSettings	 * is loaded or created.	 *	 */	public Mui(){		setIcon();		initSettings();		createMenu();		initComponents();		loadSettings(settings);		setVisible(false);	}		public void setVersion(String version) {		this.version = version;	}		private void initSettings(){	    settings = new Settings();		//获取用户的主目录	    String home = System.getProperty("user.home");	    System.out.println(home);	    String fileDIR = new String(home + File.separator +"MUSoSu");	    File f = new File(fileDIR);	    if(f.exists()){	      String fileURI = new String(fileDIR + File.separator + "settings.xml");	      if(new File(fileURI).exists()){	        settings.loadFromFile(fileURI);	      }	    }else{ 	      ClassLoader cl = this.getClass().getClassLoader();	      InputStream in = cl.getResourceAsStream("default.xml");	      settings.loadFromFile(in);	    }	  }		private void loadSettings(Settings s){		panel.setFixedBackColor(s.getFixedBackColor());		panel.setHighlightColor(s.getHighlightColor());		panel.setSquareBackColor(s.getButtonBackColor());		panel.setErrorBackColor(s.getErrorBackColor());		panel.setPanelBackColor(s.getPanelBackColor());		panel.setShowTrace(s.getShowTrace());		panel.setShowToolTip(s.getShowToolTip());		panel.setMarkColorA(s.getMarkColorA());		panel.setMarkColorB(s.getMarkColorB());		panel.setMarkColorC(s.getMarkColorC());		panel.setMarkColorD(s.getMarkColorD());		showTraceM.setState(s.getShowTrace());		showTTM.setState(s.getShowToolTip());	}		private void setIcon(){		try{			ClassLoader loader = this.getClass().getClassLoader();			InputStream imStream = loader.getResourceAsStream("musosu.png");			Image img = ImageIO.read(imStream);			setIconImage(img);		}catch(Exception e){			System.out.println("Could not load icon");		}	}		private void registerGameListener(Sudoku s){		s.addSudokuListener(new SudokuListener(){			public void sudokuChanged(SudokuEvent e){				sudokuChangedEvent(e);			}		});	}		private void initSudoku(){		virgin = new Sudoku(game);		solver = new Solver();		registerGameListener(game);		game.fireSudokuEvent(SudokuEvent.SUDOKU_CHANGED);		statusPanel.startTimer();		panel.loadBoard(game);		solver.setGame(game);		game.setDifficulty(solver.estimateDifficulty());		statusPanel.setDifficulty(game.getDifficulty());				newCustomPanel.setVisible(false);		restartM.setEnabled(true);		closeM.setEnabled(true);		saveAsM.setEnabled(true);		printM.setEnabled(true);		showSolvePanelM.setEnabled(true);		hintM.setEnabled(true);		solveM.setEnabled(true);		solveAtOnceM.setEnabled(true);		if(msp.isVisible()){			msp.setSudoku(game);		}		panel.panelResized();	}			// EVENT HANDLING METHODS		public void actionPerformed(ActionEvent evt){		if(evt.getSource() == newM) newMenuEvent(evt);		else if(evt.getSource() == newCustomM) newCustomMenuEvent(evt);		else if(evt.getSource() == openM) openMenuEvent(evt);		else if(evt.getSource() == restartM) restartMenuEvent(evt);		else if(evt.getSource() == closeM) closeMenuEvent(evt);		else if(evt.getSource() == saveAsM) saveAsMenuEvent(evt);		else if(evt.getSource() == printM) printMenuEvent(evt);		else if(evt.getSource() == exitM) exitMenuEvent(evt);		else if(evt.getSource() == clearMarksM) panel.unmarkAll();		else if(evt.getSource() == loadThemeM) loadThemeMenuEvent(evt);		else if(evt.getSource() == loadDefaultThemeM) loadDefaultThemeMenuEvent(evt);		else if(evt.getSource() == saveThemeM) saveThemeMenuEvent(evt);		else if(evt.getSource() == saveDefaultM) saveDefaultMenuEvent(evt);		else if(evt.getSource() == prefsM) prefsMenuEvent(evt);		else if(evt.getSource() == undoM) panel.undo();		else if(evt.getSource() == redoM) panel.redo();		else if(evt.getSource() == hintM) hintMenuEvent(evt);		else if(evt.getSource() == solveAtOnceM) solveAtOnceMenuEvent(evt);		else if(evt.getSource() == solveM) solveMenuEvent(evt); 		else if(evt.getSource() == aboutM) aboutMenuEvent(evt); 		// Events from newCustomPanel Buttons 		else if(evt.getSource() == getStr) getStrEvent(); 		else if(evt.getSource() == cancel) closeMenuEvent(evt); 		else if(evt.getSource() == save) saveAsMenuEvent(evt); 		else if(evt.getSource() == savenopen){ 			saveAsMenuEvent(evt); 			if(this.saveName != null) 				openFromFile(saveName); 		} 		else System.out.println("Action not defined...");	}		private void sudokuChangedEvent(SudokuEvent e){		Sudoku s = (Sudoku) e.getSource();		statusPanel.gameUpdatedEvent(s);	}		private void newMenuEvent(ActionEvent e){		GenerateDialog generateDialog;		generateDialog = new GenerateDialog(this, "New Sudoku");		generateDialog.setVisible(true);		game = generateDialog.getGame();		if(game != null){			setStatus("New Game Generated!");			this.initSudoku();		}			}		private void newCustomMenuEvent(ActionEvent e){		newCustomPanel.setVisible(true);		if(msp.isVisible()) 			hideSolverPanel();		closeM.setEnabled(true);		saveAsM.setEnabled(true);		printM.setEnabled(true);		this.showSolvePanelM.setEnabled(false);		this.solveM.setEnabled(false);				this.game = new Sudoku();		panel.loadBoard(game);		panel.setVisible(true);				statusPanel.setStopped();		setStatus("Creating custom game.");				this.validateTree();	}		/**	 * Handles the event tha occurs when the "Open Game..." 	 * MenuItem is pressed.	 * Creates an OpenDialog, stores the desired filename,	 * Creates an object of class Sudoku for this file and	 * finaly calls loadBoard to display the board.	 */	private void openMenuEvent(ActionEvent evt){		String fname;		setStatus("Open Menu selected");		final JFileChooser fc = new JFileChooser();		fc.setFileFilter(new ExtensionFileFilter("sud", "Sudoku file"));		int returnVal = fc.showOpenDialog(this);		if(returnVal == JFileChooser.APPROVE_OPTION){			fname = new String(fc.getSelectedFile().getAbsolutePath());			openFromFile(fname);		}	}		private void openFromFile(String fname){		setStatus("File choosed: " + fname);		game = new Sudoku(fname);		initSudoku();	}		private void preloadMenuEvent(ActionEvent evt){		JMenuItem ob = (JMenuItem) evt.getSource();		setStatus("Loaded preloaded game");		game = new Sudoku(ob.getName(), 4734);				initSudoku();	}		private void saveAsMenuEvent(ActionEvent evt){		String ext = "sud";		final JFileChooser fc = new JFileChooser();		fc.setFileFilter(new ExtensionFileFilter(ext, "Sudoku file"));		int returnVal = fc.showSaveDialog(this);		if(returnVal == JFileChooser.APPROVE_OPTION){			saveName = new String(fc.getSelectedFile().getAbsolutePath());			if(!saveName.endsWith(ext))				saveName += ".sud";						if(fc.getSelectedFile().exists()){				if(!(JOptionPane.NO_OPTION 				   == JOptionPane.showConfirmDialog(this, saveName +" exists. Overwrite?",				      "Confirm Save As", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE))){					game.saveToFile(saveName);					setStatus("File saved: " + saveName);				}			}else{				game.saveToFile(saveName);				setStatus("File saved: " + saveName);							}		}			}		private void restartMenuEvent(ActionEvent evt){		game = new Sudoku(virgin);		initSudoku();	}		private void closeMenuEvent(ActionEvent evt){		panel.setVisible(false);		setStatus("Does the close function have sense?");		statusPanel.stopTimer();		if(msp.isVisible())			this.hideSolverPanel();		newCustomPanel.setVisible(false);		restartM.setEnabled(false);		saveAsM.setEnabled(false);		printM.setEnabled(false);		closeM.setEnabled(false);		showSolvePanelM.setEnabled(false);		showSolvePanelM.setState(false);		solveM.setEnabled(false);		solveAtOnceM.setEnabled(false);		hintM.setEnabled(false);		statusPanel.setStopped();	}		private void printMenuEvent(ActionEvent evt){		PrintUtilities.printComponent(panel);		// TODO		// 1) add functionality to print more than one Sudokus		//    with Print Preview		// 2) print a label		// 3) to do the above, panel must be replaced		//    with anoter printable	}		private void exitMenuEvent(ActionEvent evt){	     processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));	}			private void showTraceMenuEvent(ChangeEvent evt){		JCheckBoxMenuItem tmp = (JCheckBoxMenuItem) evt.getSource();		boolean b = tmp.getState();		panel.setShowTrace(b);		settings.setShowTrace(b);	}		private void showTTMenuEvent(ChangeEvent evt){		JCheckBoxMenuItem tmp = (JCheckBoxMenuItem) evt.getSource();		boolean b = tmp.getState();		panel.setShowToolTip(b);		settings.setShowToolTip(b);	}		private void saveThemeMenuEvent(ActionEvent evt){		String saveName;		String ext = "xml";		final JFileChooser fc = new JFileChooser();		fc.setFileFilter(new ExtensionFileFilter(ext, "XML Theme File"));		int returnVal = fc.showSaveDialog(this);		if(returnVal == JFileChooser.APPROVE_OPTION){			saveName = new String(fc.getSelectedFile().getAbsolutePath());			if(!saveName.endsWith(ext))				saveName += ".xml";						if(fc.getSelectedFile().exists()){				if(!(JOptionPane.NO_OPTION 				   == JOptionPane.showConfirmDialog(this, saveName +" exists. Overwrite?",				      "Confirm Save As", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE))){

⌨️ 快捷键说明

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