Commit e3c1b333 authored by YuFan Jia's avatar YuFan Jia 💤
Browse files

feat(static): UI

parent 9e8247a5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
package components;
package Components;

import javax.swing.*;
import java.awt.event.MouseAdapter;
+75 −0
Original line number Diff line number Diff line
package components;

import controller.GameController;
import model.*;
import view.GameFrame;
package Components;

import java.awt.*;

import GameUI.ChessGameUI;
import Model.ChessPiece;
import Progressing.ChessPanel;

public class ChessGridComponent extends BasicComponent {
    public static int chessSize;
    public static int gridSize;
    public static Color gridColor = new Color(255, 150, 50);

    private ChessPiece chessPiece;
    private ChessPanel chess;
    private int row;
    private int col;

    public ChessGridComponent(int row, int col) {
        this.setSize(gridSize, gridSize);
        // this.setSize(gridSize, gridSize);

        this.row = row;
        this.col = col;
@@ -24,24 +25,24 @@ public class ChessGridComponent extends BasicComponent {

    @Override
    public void onMouseClicked() {
        System.out.printf("%s clicked (%d, %d)\n", GameFrame.controller.getCurrentPlayer(), row, col);
        // System.out.printf("%s clicked (%d, %d)\n", GameCore.getCurrentPlayer(), row,
        // col);
        // todo: complete mouse click method
        if (GameFrame.controller.canClick(row, col)) {
        if (ChessGameUI.thisGameCore.canClick(row, col)) {
            if (this.chessPiece == null) {
                this.chessPiece = GameFrame.controller.getCurrentPlayer();
                GameFrame.controller.swapPlayer();
                this.chessPiece = ChessGameUI.thisGameCore.getCurrentPlayer();
                ChessGameUI.thisGameCore.swapPlayer();
            }
            repaint();
        }
    }


    public ChessPiece getChessPiece() {
        return chessPiece;
    }

    public void setChessPiece(ChessPiece chessPiece) {
        this.chessPiece = chessPiece;
    public void setChessPiece(ChessPiece playersChessPiece) {
        this.chessPiece = playersChessPiece;
    }

    public int getRow() {
@@ -53,14 +54,17 @@ public class ChessGridComponent extends BasicComponent {
    }

    public void drawPiece(Graphics g) {
        g.setColor(gridColor);
        g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);//fill the rect with chessboard color
        if (this.chessPiece != null) {
            g.setColor(chessPiece.getColor());
            g.fillOval((gridSize - chessSize) / 2, (gridSize - chessSize) / 2, chessSize, chessSize);//draw a point
        // g.setColor(gridColor);
        // g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);// fill the rect
        // with chessboard color
        // if (this.chessPiece != null) {
        // g.setColor(chessPiece.getColor());
        // g.fillOval((gridSize - chessSize) / 2, (gridSize - chessSize) / 2, chessSize,
        // chessSize);// draw a point
        // }

        // todo:complete the grapg methords or use other ways to draw the line
    }
    }


    @Override
    public void paintComponent(Graphics g) {
@@ -68,5 +72,4 @@ public class ChessGridComponent extends BasicComponent {
        drawPiece(g);
    }


}
+160 −0
Original line number Diff line number Diff line
package GameUI;

import java.awt.*;
import java.awt.event.*;

import javax.sound.midi.SysexMessage;
import javax.swing.*;
/*
 * Created by JFormDesigner on Tue Nov 30 22:41:46 CST 2021
 */

import Model.CheatStatus;
import Model.ChessPiece;
import Progressing.GameCore;

/**
 * @author uint44t
 */
public class ChessGameUI {

	public ChessGameUI() {
		initComponents();

	}

	private int rows = 8;
	private int cols = 8;

	public static GameCore thisGameCore;
	private ChessPiece player;
	public static GameCore contorl;
	public static StatusUI thisStatus;
	private CheatStatus thisCheatModeStatus = CheatStatus.Off;

	public CheatStatus getCheatStatus() {
		return thisCheatModeStatus;
	}

	private void importButtonActionPerformed(ActionEvent e) {
		// TODO add your code here
	}

	private void exportButtonActionPerformed(ActionEvent e) {
		// TODO add your code here
	}

	private void restartButtonActionPerformed(ActionEvent e) {
		// TODO add your code here
	}

	private void undoButtonActionPerformed() {
		// TODO add your code here
	}

	private void cheatOnOffButtonActionPerformed() {
		if (cheatOnOff.isSelected()) {
			cheat.setVisible(true);
			thisCheatModeStatus = CheatStatus.Black;
		} else {
			cheat.setVisible(false);
			thisCheatModeStatus = CheatStatus.Off;
			System.out.println("cheatoff");
		}
	}

	private void cheatListener(ItemEvent e) {
		String select = e.getItem().toString();
		if (cheatOnOff.isSelected()) {
			switch (select) {
				case "Black" -> thisCheatModeStatus = CheatStatus.Black;
				case "White" -> thisCheatModeStatus = CheatStatus.White;
			}
		} else {
			thisCheatModeStatus = CheatStatus.Off;
		}
		System.out.println(select);
	}

	JFrame background = new JFrame();
	JPanel bottomButtons = new JPanel();
	JButton importButton = new JButton();
	JButton exportButton = new JButton();
	JButton restartButton = new JButton();
	JButton undoButton = new JButton();
	JComboBox cheat = new JComboBox();
	JRadioButton cheatOnOff = new JRadioButton();

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		thisStatus = new StatusUI("blackPlayer", "whitePlayer");
		thisGameCore = new GameCore(rows, cols);

		// ======== background ========
		{
			background.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			background.setVisible(true);
			background.setTitle("Othello");
			background.setName("backFrame");
			Container bcakgroundContentPane = background.getContentPane();
			bcakgroundContentPane.setLayout(new BorderLayout(10, 10));
			background.addComponentListener(new ComponentAdapter() {
				@Override
				public void componentResized(ComponentEvent e) {
					thisGameCore.zoom();
					System.out.println("zooming");
				}
			});

			bcakgroundContentPane.add(thisGameCore.getGamePanel(), BorderLayout.CENTER);
			bcakgroundContentPane.add(thisStatus, BorderLayout.WEST);

			// ======== bottomButtons ========
			{
				bottomButtons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
				((FlowLayout) bottomButtons.getLayout()).setAlignOnBaseline(true);

				// ---- importButton ----
				importButton.setText("Import");
				importButton.addActionListener(e -> importButtonActionPerformed(e));
				bottomButtons.add(importButton);

				// ---- exportButton ----
				exportButton.setText("Export");
				exportButton.addActionListener(e -> exportButtonActionPerformed(e));
				bottomButtons.add(exportButton);

				// ---- restartButton ----
				restartButton.setText("Restart");
				restartButton.addActionListener(e -> restartButtonActionPerformed(e));
				bottomButtons.add(restartButton);

				// ---- undoButton ----
				undoButton.setText("Undo");
				undoButton.addActionListener(e -> undoButtonActionPerformed());
				bottomButtons.add(undoButton);

				// ---- cheatOnOff ----
				cheatOnOff.setText("Cheat");
				cheatOnOff.addActionListener(e -> cheatOnOffButtonActionPerformed());
				bottomButtons.add(cheatOnOff);

				// cheat
				cheat.addItem("Black");
				cheat.addItem("White");
				cheat.setVisible(false);
				cheat.addItemListener(e -> cheatListener(e));
				bottomButtons.add(cheat);

			}
			bcakgroundContentPane.add(bottomButtons, BorderLayout.SOUTH);
		}

		background.pack();
		background.setLocationRelativeTo(null);
		// JFormDesigner - End of component initialization //GEN-END:initComponents
	}
	// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
	// JFormDesigner - End of variables declaration //GEN-END:variables
}

GameUI/ChessGameUI.jfd

0 → 100644
+60 −0
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.memberVariableThis": true
			"JavaCodeGenerator.defaultVariableLocal": true
			"JavaCodeGenerator.i18nInitMethod": true
		}
		add( new FormWindow( "javax.swing.JFrame", new FormLayoutManager( class java.awt.BorderLayout ) {
			"hgap": 500
			"vgap": 450
		} ) {
			name: "bcakground"
			"defaultCloseOperation": 3
			"$locationPolicy": 1
			"visible": true
			"title": "Othello"
			"name": "backFrame"
			add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.FlowLayout ) {
				"hgap": 20
				"vgap": 10
				"alignOnBaseline": true
			} ) {
				name: "bottomButtons"
				add( new FormComponent( "javax.swing.JButton" ) {
					name: "importButton"
					"text": "Import"
					addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "importButtonActionPerformed", true ) )
				} )
				add( new FormComponent( "javax.swing.JButton" ) {
					name: "exportButton"
					"text": "Export"
					addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "exportButtonActionPerformed", true ) )
				} )
				add( new FormComponent( "javax.swing.JButton" ) {
					name: "restartButton"
					"text": "Restart"
					addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "restartButtonActionPerformed", true ) )
				} )
				add( new FormComponent( "javax.swing.JButton" ) {
					name: "undoButton"
					"text": "Undo"
					addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "undoButtonActionPerformed", false ) )
				} )
				add( new FormComponent( "javax.swing.JButton" ) {
					name: "moreSettingButton"
					"text": "More..."
					addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "moreSettingButtonActionPerformed", false ) )
				} )
			}, new FormLayoutConstraints( class java.lang.String ) {
				"value": "South"
			} )
		}, new FormLayoutConstraints( null ) {
			"location": new java.awt.Point( 220, 25 )
			"size": new java.awt.Dimension( 500, 500 )
		} )
	}
}
+54 −0
Original line number Diff line number Diff line
package GameUI;

import java.awt.*;
import javax.swing.*;
/*
 * Created by JFormDesigner on Fri Dec 03 22:18:40 CST 2021
 */

/**
 * @author uint44t
 */
public class StartMenuUI extends JFrame {
	public StartMenuUI() {
		initComponents();
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		panel1 = new JPanel();
		label1 = new JLabel();

		// ======== this ========
		setEnabled(false);
		setFont(new Font("Sitka Banner", Font.BOLD, 36));
		setTitle("Othello");
		setVisible(true);
		setMinimumSize(new Dimension(40, 40));
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

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

			// ---- label1 ----
			label1.setText("Othello");
			label1.setFont(new Font("Sitka Banner", Font.BOLD, 48));
			label1.setHorizontalAlignment(SwingConstants.CENTER);
			label1.setEnabled(false);
			label1.setVisible(false);
			panel1.add(label1, "card1");
		}
		contentPane.add(panel1, BorderLayout.NORTH);
		pack();
		setLocationRelativeTo(null);
		// JFormDesigner - End of component initialization //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
	private JPanel panel1;
	private JLabel label1;
	// JFormDesigner - End of variables declaration //GEN-END:variables
}
Loading