Commit 3d425067 authored by YuFan Jia's avatar YuFan Jia 💤
Browse files

feat(menu): add menu

to chessgameui and add restart
parent 1ecfc386
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -37,12 +37,24 @@ public class GameCore {
    }

    private void intializeChess() {// 初始化放棋

        gamePanel.setChess(3, 3, ChessPiece.BLACK);
        gamePanel.setChess(4, 4, ChessPiece.BLACK);
        gamePanel.setChess(3, 4, ChessPiece.WHITE);
        gamePanel.setChess(4, 3, ChessPiece.WHITE);
    }

    public void restart() {
        for (int i = 0; i != 8; ++i) {
            for (int j = 0; j != 8; ++j) {
                gamePanel.setChess(i, j, ChessPiece.EMPTY);
            }
        }
        intializeChess();
        currentPlayer = ChessPiece.BLACK;
        countScore();
    }

    public void swapPlayer() {// 交换玩家
        countScore();
        if (this.currentPlayer == ChessPiece.BLACK) {
+13 −12
Original line number Diff line number Diff line
@@ -6,36 +6,37 @@ import javax.swing.*;
/**
 * @author uint44t
 */
public class BasicBackground extends JFrame {
public class BasicBackground {
	public static ChessGameUI thisChessGameUI;
	public static StartMenuUI thisStartMenuUI;
	public static JFrame backFrame;

	public BasicBackground() {
		initComponents();
		thisChessGameUI = new ChessGameUI();
		thisStartMenuUI = new StartMenuUI();
		changePanel(thisChessGameUI);
		changePanel(thisStartMenuUI);
	}

	public void changePanel(JPanel o) {
		this.add(o);
	public static void changePanel(JPanel o) {
		backFrame.add(o);
		o.updateUI();
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents

		backFrame = new JFrame();
		// ======== basic ========
		{
			this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			this.setVisible(true);
			this.setTitle("Othello");
			this.setName("backFrame");
			Container basicContentPane = this.getContentPane();
			backFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			backFrame.setVisible(true);
			backFrame.setTitle("Othello");
			backFrame.setName("backFrame");
			Container basicContentPane = backFrame.getContentPane();
			basicContentPane.setPreferredSize(new Dimension(700, 500));
			this.pack();
			this.setLocationRelativeTo(null);
			backFrame.pack();
			backFrame.setLocationRelativeTo(null);
		}
		// JFormDesigner - End of component initialization //GEN-END:initComponents
	}
+11 −0
Original line number Diff line number Diff line
@@ -65,6 +65,11 @@ public class ChessGameUI extends JPanel {
		}
	}

	private void menuBUttonActionPerformed() {
		BasicBackground.backFrame.remove(BasicBackground.thisChessGameUI);
		BasicBackground.changePanel(BasicBackground.thisStartMenuUI);
	}

	// private void cheatListener(ItemEvent e) {// 上面出发后弹出的选项卡的触发事件
	// String select = e.getItem().toString();
	// if (cheatOnOff.isSelected()) {
@@ -87,6 +92,7 @@ public class ChessGameUI extends JPanel {
	JComboBox cheat = new JComboBox();
	JRadioButton cheatOnOff = new JRadioButton();
	JPanel center;
	JButton menuButton = new JButton();

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
@@ -143,6 +149,11 @@ public class ChessGameUI extends JPanel {
				undoButton.addActionListener(e -> undoButtonActionPerformed());
				bottomButtons.add(undoButton);

				// menubutton
				menuButton.setText("Menu");
				menuButton.addActionListener(e -> menuBUttonActionPerformed());
				bottomButtons.add(menuButton);

				// ---- cheatOnOff ----
				cheatOnOff.setText("Cheat");
				cheatOnOff.addActionListener(e -> cheatOnOffButtonActionPerformed());
+21 −19
Original line number Diff line number Diff line

package GameUI;

import java.awt.*;
import java.util.prefs.BackingStoreException;

import javax.swing.*;
/*
 * Created by JFormDesigner on Fri Dec 03 22:18:40 CST 2021
 */
import javax.swing.text.Position.Bias;
import javax.swing.plaf.basic.BasicHTML;

/**
 * @author uint44t
@@ -22,15 +16,18 @@ public class StartMenuUI extends JPanel {
	}

	private void newGaemButtonActionPerformed() {
		// .changePanel(BasicBackground.thisChessGameUI);
		BasicBackground.backFrame.remove(BasicBackground.thisStartMenuUI);
		BasicBackground.changePanel(BasicBackground.thisChessGameUI);
		BasicBackground.thisChessGameUI.thisGameCore.restart();
	}

	private void loadButtonActionPerformed() {
		// TODO add your code here
		new FileChooser(true);
		// todo: add import methord
	}

	private void exitButtonActionPerformed() {
		// TODO add your code here
		BasicBackground.backFrame.dispose();
	}

	private void initComponents() {
@@ -40,7 +37,7 @@ public class StartMenuUI extends JPanel {
		label1 = new JLabel();
		buttons = new JPanel();
		panel2 = new JPanel();
		label6 = new JLabel();
		label2 = new JLabel();
		panel5 = new JPanel();
		newGaemButton = new JButton();
		label5 = new JLabel();
@@ -55,11 +52,11 @@ public class StartMenuUI extends JPanel {

		// ======== title ========
		{
			title.setLayout(new CardLayout(20, 40));
			title.setLayout(new CardLayout());

			// ---- label1 ----
			label1.setText("Othello");
			label1.setFont(new Font("Sitka Banner", Font.BOLD, 48));
			label1.setFont(new Font("Sitka Banner", Font.BOLD, 60));
			label1.setHorizontalAlignment(SwingConstants.CENTER);
			label1.setEnabled(false);
			label1.setVisible(false);
@@ -73,11 +70,11 @@ public class StartMenuUI extends JPanel {

			// ======== panel2 ========
			{
				panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
				panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));

				// ---- label6 ----
				label6.setText("  ");
				panel2.add(label6);
				// ---- label2 ----
				label2.setText(" ");
				panel2.add(label2);

				// ======== panel5 ========
				{
@@ -85,6 +82,7 @@ public class StartMenuUI extends JPanel {

					// ---- newGaemButton ----
					newGaemButton.setText("New Game");
					newGaemButton.setFont(buttonsStandard);
					newGaemButton.addActionListener(e -> newGaemButtonActionPerformed());
					panel5.add(newGaemButton);
				}
@@ -92,6 +90,7 @@ public class StartMenuUI extends JPanel {

				// ---- label5 ----
				label5.setText(" ");
				label5.setFont(buttonsStandard);
				panel2.add(label5);

				// ======== panel4 ========
@@ -100,6 +99,7 @@ public class StartMenuUI extends JPanel {

					// ---- loadButton ----
					loadButton.setText("Load");
					loadButton.setFont(buttonsStandard);
					loadButton.addActionListener(e -> loadButtonActionPerformed());
					panel4.add(loadButton);
				}
@@ -115,6 +115,7 @@ public class StartMenuUI extends JPanel {

					// ---- exitButton ----
					exitButton.setText("Exit");
					exitButton.setFont(buttonsStandard);
					exitButton.addActionListener(e -> exitButtonActionPerformed());
					panel6.add(exitButton);
				}
@@ -122,7 +123,7 @@ public class StartMenuUI extends JPanel {
			}
			buttons.add(panel2);
		}
		add(buttons, BorderLayout.CENTER);
		add(buttons, BorderLayout.SOUTH);
		// JFormDesigner - End of component initialization //GEN-END:initComponents
	}

@@ -131,7 +132,7 @@ public class StartMenuUI extends JPanel {
	private JLabel label1;
	private JPanel buttons;
	private JPanel panel2;
	private JLabel label6;
	private JLabel label2;
	private JPanel panel5;
	private JButton newGaemButton;
	private JLabel label5;
@@ -140,5 +141,6 @@ public class StartMenuUI extends JPanel {
	private JLabel label4;
	private JPanel panel6;
	private JButton exitButton;
	Font buttonsStandard = new Font("\u6977\u4f53", Font.PLAIN, 20);
	// JFormDesigner - End of variables declaration //GEN-END:variables
}
+10 −11
Original line number Diff line number Diff line

JFDML JFormDesigner: "7.0.4.0.360" Java: "15.0.1" encoding: "UTF-8"

new FormModel {
	contentType: "form/swing"
	root: new FormRoot {
		auxiliary() {
			"JavaCodeGenerator.i18nInitMethod": true
		}
		add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.BorderLayout ) ) {
			name: "this"
			add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.CardLayout ) {
				"hgap": 20
				"vgap": 40
			} ) {
			add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.CardLayout ) ) {
				name: "title"
				add( new FormComponent( "javax.swing.JLabel" ) {
					name: "label1"
@@ -27,11 +26,11 @@ new FormModel {
			add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.FlowLayout ) ) {
				name: "buttons"
				add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class javax.swing.BoxLayout ) {
					"axis": 1
					"axis": 3
				} ) {
					name: "panel2"
					add( new FormComponent( "javax.swing.JLabel" ) {
						name: "label6"
						name: "label2"
						"text": " "
					} )
					add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.FlowLayout ) ) {
@@ -39,6 +38,7 @@ new FormModel {
						add( new FormComponent( "javax.swing.JButton" ) {
							name: "newGaemButton"
							"text": "New Game"
							"font": new java.awt.Font( "楷体", 0, 20 )
							addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "newGaemButtonActionPerformed", false ) )
						} )
					} )
@@ -68,12 +68,11 @@ new FormModel {
					} )
				} )
			}, new FormLayoutConstraints( class java.lang.String ) {
				"value": "Center"
				"value": "South"
			} )
		}, new FormLayoutConstraints( null ) {
			"location": new java.awt.Point( 210, 70 )
			"location": new java.awt.Point( 230, 85 )
			"size": new java.awt.Dimension( 400, 300 )
		} )
	}
}