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

📄 board.java

📁 Sudoku游戏的原代码,以aop方式进行修改
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
* Copyright 2005 Victor Ferrer
* 
* This file is part of FreeSudoku.
* 
* FreeSudoku 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; either version 2 of the License, or
* (at your option) any later version.
* 
* FreeSudoku 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 FreeSudoku; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA*
*/
package org.gos.freesudoku.view;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import org.gos.freesudoku.*;
import java.io.*;
import java.util.*;

/**
 *
 */
public class Board extends JFrame
{
	// Variables declaration
	protected FreeSudoku 	cParent;					// To call action for some events.
	
	private int 			cAmple 		= 322;
	private int 			cAlt 		= 465;
	
	private JPanel[][] 		cPanel 		= new JPanel[3][3];
	private Cell[][] 		cPos 		= new Cell[9][9];

	// Menu
	private JMenuItem aboutMenuItem;
	private JMenuItem contentsMenuItem;
	private JMenuItem optionsMenuItem;
	private JMenu optionsMenu;
	private JMenuItem exitMenuItem;
	private JMenu fileMenu;
	private JMenu helpMenu;
    private JMenu langMenu;
	private JMenuBar menuBar;
	private JMenuItem newMenuItem;
	private JMenuItem openMenuItem;
	private JMenuItem saveMenuItem;
    private JMenuItem[] langItem;

	// Buttons
	private JButton jButton1;
	private JButton jButton2;

	private JPanel cPanelButtons;
	private JPanel cPanelOptions;
	
	// Messages
	private JPanel cPanelMessages;
	private JTextField cTextFieldMessages;
	// End of variables declaration

    private JSlider         diffSlider;
//	private JRadioButton cRadioButtonPlay;
//	private JRadioButton cRadioButtonSolver;
	
	private static final long serialVersionUID = 1L;
	protected boolean disabled = true;
    
    private OptionsDialog optionsDialog = null;

	/**
	 * Creates new form Board 
	 */
	public Board()
	{
		initComponents();
		disableAll();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 */
	private void initComponents()
	{
		setTitle("Free Sudoku");
		getContentPane().setLayout(new FlowLayout());
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setResizable(false);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screenSize.width - cAmple) / 2, (screenSize.height - cAlt) / 2,
				cAmple, cAlt);

		// Buttons & Messages
		cPanelButtons = new JPanel();
		cPanelOptions = new JPanel();
		jButton1 = new JButton();
		jButton2 = new JButton();
		cPanelMessages = new JPanel();
		cTextFieldMessages = new JTextField();

		// Menus
		menuBar = new JMenuBar();
		fileMenu = new JMenu();
		newMenuItem = new JMenuItem();
		openMenuItem = new JMenuItem();
		saveMenuItem = new JMenuItem();
		exitMenuItem = new JMenuItem();
		optionsMenu = new JMenu();
		optionsMenuItem = new JMenuItem();
        langMenu = new JMenu();
        helpMenu = new JMenu();
        contentsMenuItem = new JMenuItem();
        aboutMenuItem = new JMenuItem();

		cPanelButtons.setLayout(new FlowLayout(FlowLayout.LEFT));
		cPanelOptions.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		cPanelButtons.setPreferredSize(new Dimension(143, 50));
		cPanelOptions.setPreferredSize(new Dimension(142, 60));
		jButton1.setFont(new Font(CONSTS.FONT_NAME, Font.BOLD, 12));
		jButton1.setForeground( CONSTS.GREEN_DARK);
		//jButton1.setPreferredSize(new Dimension(35, 35));
		cPanelButtons.add(jButton1);
		jButton2.setForeground( CONSTS.RED_DARK);
//		jButton2.setPreferredSize(new Dimension(35, 35));
		cPanelButtons.add(jButton2);
//		jButton3.setText("B");
//		jButton3.setPreferredSize(new Dimension(35, 35));
//		cPanelButtons.add(jButton3);
//		jButton4.setText("C");
//		jButton4.setPreferredSize(new Dimension(35, 35));
//		cPanelButtons.add(jButton4);

        diffSlider = new JSlider(JSlider.HORIZONTAL, 1, 4, 2);
        diffSlider.setMajorTickSpacing(1);
        diffSlider.setMinorTickSpacing(1);
        diffSlider.setPaintTicks(true);
        diffSlider.setPaintLabels(true);
        diffSlider.setSnapToTicks( true);
        diffSlider.setPreferredSize( new Dimension(100, 45));

		cPanelOptions.add( diffSlider);
		getContentPane().add(cPanelButtons);
		getContentPane().add(cPanelOptions);
		
		jButton1.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent evt) {
                cParent.restart();
			}
		});
		jButton2.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent evt) {
                disableAll();
			}
		});

        Cell.board = this;      // Inicialitza referencia a les caselles.
		// Init each 3x3 panel
		for (int px = 0; px < 3; px++)
		{
			for (int py = 0; py < 3; py++)
			{
				cPanel[px][py] = new JPanel();
				initPanel( cPanel[px][py]);

				// Init each position from panel
				for( int cx = 0; cx < 3; cx++)
				{
					for( int cy = 0; cy < 3; cy++)
					{
						int i = px*3 + cx;
						int j = py*3 + cy;
						cPos[i][j] = new Cell( i, j);
						cPanel[px][py].add( cPos[i][j]);
					}
				}

				// Add this 3x3 panel
				getContentPane().add( cPanel[px][py]);
			}
		}

		// Messages
		cPanelMessages.setPreferredSize(new Dimension(290, 50));
		cTextFieldMessages.setBackground(new Color(204, 204, 204));
		cTextFieldMessages.setPreferredSize(new Dimension(290, 40));
		cTextFieldMessages.setHorizontalAlignment(JTextField.CENTER);
		cTextFieldMessages.setFont(new Font( CONSTS.FONT_NAME, Font.BOLD, 14));
		cTextFieldMessages.setForeground( Color.BLUE);
		cPanelMessages.add(cTextFieldMessages);

		getContentPane().add(cPanelMessages);

		// Menus
		fileMenu.add(newMenuItem);
		fileMenu.add(openMenuItem);
		fileMenu.add(saveMenuItem);
		fileMenu.add(exitMenuItem);
		menuBar.add(fileMenu);
		optionsMenu.add(optionsMenuItem);
		menuBar.add(optionsMenu);
        langItem = new JMenuItem[Trans.langDesc.length];
        for (int i = 0; i < Trans.langDesc.length; i++)
        {
            final int langIdx = i;
            langItem[i] = new JMenuItem();
            langItem[i].setText( Trans.langDesc[i]);
            langItem[i].addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent evt)
                        {
                            Trans.get().setLang( Trans.langIds[langIdx]);
                            setCompsText();
                        }
                    });

            langMenu.add( langItem[i]);
        }
        menuBar.add( langMenu);
        
		helpMenu.add(contentsMenuItem);
		helpMenu.add(aboutMenuItem);
		menuBar.add(helpMenu);

        newMenuItem.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent evt)
                    {
                        cParent.restart();
                    }
                });
        openMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				loadGame();
			}

		});
		saveMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				saveGame();
			}

		});
        exitMenuItem.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent evt)
                    {
                        exitMenuItemActionPerformed(evt);
                    }
                });
        optionsMenuItem.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent evt)
                    {
                        if( optionsDialog == null)
                            optionsDialog = new OptionsDialog();
                        optionsDialog.show();
                    }
                });
        aboutMenuItem.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent evt)
                    {
                        JOptionPane.showMessageDialog( 
                                getContentPane(), 
                                "FreeSudoku Project\n" +
                                "version " + CONSTS.version + "\n" +
                                "freesudoku.sourceforge.net\n\n" +
                                "Victorf2 (at) users.sourceforge.net\n" +
                                "GNU GPL License",
                                "About",
                                JOptionPane.PLAIN_MESSAGE                                
                                );
                    }
                });
        contentsMenuItem.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent evt)
                    {
                        JOptionPane.showMessageDialog( 
                                getContentPane(), 
                                "Free Sudoku\n\n" +
                                "Doble-click: Enter more than one digit.\n" +
                                "CTRL-click: Display posibles values.\n" +
                                "\nGame Info:\n" + 
                                " English -> http://en.wikipedia.org/wiki/Sudoku\n" +
                                " Spanish -> http://es.wikipedia.org/wiki/Sudoku\n"
                                ,
                                
                                "Help",
                                JOptionPane.PLAIN_MESSAGE                                
                                );

⌨️ 快捷键说明

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