Commit 969b25f2 authored by lynn's avatar lynn
Browse files

finish parts of basic function

parent fdc79157
Loading
Loading
Loading
Loading
+410 −22
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ import model.game.Chessboard;

import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;

/**
 * 这个类表示游戏窗体,窗体上包含:
@@ -16,8 +18,11 @@ public class ChessGameFrame extends JFrame {
    private final int WIDTH ;
    private final int HEIGHT ;
    public final int CHESSBOARD_SIZE;
    private AppController appController;
    private AppController appComtroller;
    private static JLabel statusLabel;
    private boolean flag = true;
    private testBGM bgm = new testBGM();


    public ChessGameFrame(int width, int height) {
        setTitle("2022 CS109 Project Demo"); //设置标题
@@ -25,35 +30,267 @@ public class ChessGameFrame extends JFrame {
        this.HEIGHT = height;
        this.CHESSBOARD_SIZE = HEIGHT * 4 / 5;

        //TODO use gridLayout
        Image icon1 = new ImageIcon("res\\ChessBoardBGP.png").getImage().getScaledInstance(620,895,Image.SCALE_SMOOTH);
        ImageIcon icon = new ImageIcon(icon1);
        JLabel uprightPanel = new JLabel(icon);
        uprightPanel.setBounds(0,0, icon.getIconWidth(), icon.getIconHeight());
        this.getLayeredPane().add(uprightPanel,Integer.valueOf(Integer.MIN_VALUE));
        JPanel bgp = (JPanel) getContentPane();
        bgp.setOpaque(false);

        setSize(WIDTH, HEIGHT);
        setLocationRelativeTo(null); // Center the window.
        getContentPane().setBackground(Color.WHITE);
//        getContentPane().setBackground(Color.WHITE);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置程序关闭按键,如果点击右上方的叉就游戏全部关闭了
        setLayout(null);

        Chessboard.setInstance(new Chessboard());
        new Thread(() -> {
            while (true) {
                bgm.playMusic(flag);
            }
        }).start();

        //todo 用两个 label 分别挡住 red black(曲线救国)
        //      要图层的效果得写三个 panel ( technically )
        addChessboard();
        addLabel();
        addHelloButton();
//        addLabel();
//        addHelloButton();
        addExitButton();
        addLoadButton();
        appController = new AppController(Chessboard.getInstance());
        appController.loadGameFromFile("res/save.txt");
        addBGMButton();
        addWithdrawButton();
        addSendButton();

        // 加入 textArea
        addText();
        addPlayerList();
        addActionHistory();
        addRoomList();
        addMessages();

        //加入 labels
        addLabels("RED",440,Color.red);
        addLabels("BLACK",510,Color.black);
        addLabels("Player List",340);
        addLabels("Action History",435);
        addLabels("Room List",603);
        addLabels("Messages",688);

//        JPanel paintPanel = new JPanel();
        paint(new Graphics() {
            @Override
            public Graphics create() {
                return null;
            }

            @Override
            public void translate(int x, int y) {

            }

            @Override
            public Color getColor() {
                return null;
            }

            @Override
            public void setColor(Color c) {

            }

            @Override
            public void setPaintMode() {

            }

            @Override
            public void setXORMode(Color c1) {

            }

            @Override
            public Font getFont() {
                return null;
            }

            @Override
            public void setFont(Font font) {

            }

            @Override
            public FontMetrics getFontMetrics(Font f) {
                return null;
            }

            @Override
            public Rectangle getClipBounds() {
                return null;
            }

            @Override
            public void clipRect(int x, int y, int width, int height) {

            }

            @Override
            public void setClip(int x, int y, int width, int height) {

            }

            @Override
            public Shape getClip() {
                return null;
            }

            @Override
            public void setClip(Shape clip) {

            }

            @Override
            public void copyArea(int x, int y, int width, int height, int dx, int dy) {

            }

            @Override
            public void drawLine(int x1, int y1, int x2, int y2) {

            }

            @Override
            public void fillRect(int x, int y, int width, int height) {

            }

            @Override
            public void clearRect(int x, int y, int width, int height) {

            }

            @Override
            public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {

            }

            @Override
            public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {

            }

            @Override
            public void drawOval(int x, int y, int width, int height) {

            }

            @Override
            public void fillOval(int x, int y, int width, int height) {

            }

            @Override
            public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {

            }

            @Override
            public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {

            }

            @Override
            public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {

            }

            @Override
            public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {

            }

            @Override
            public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {

            }

            @Override
            public void drawString(String str, int x, int y) {

            }

            @Override
            public void drawString(AttributedCharacterIterator iterator, int x, int y) {

            }

            @Override
            public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
                return false;
            }

            @Override
            public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
                return false;
            }

            @Override
            public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
                return false;
            }

            @Override
            public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
                return false;
            }

            @Override
            public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
                return false;
            }

            @Override
            public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
                return false;
            }

            @Override
            public void dispose() {

            }
        });


    }

    /**
     * 在游戏窗体中添加棋盘
     */
    private void addChessboard() {
        ChessboardCom.setInstance(new ChessboardCom(CHESSBOARD_SIZE / 2 + 6, CHESSBOARD_SIZE / 3, CHESSBOARD_SIZE + 6));
        appController = new AppController(Chessboard.getInstance());
        ChessboardCom.getInstance().setLocation(HEIGHT / 10, HEIGHT / 10);
        add(ChessboardCom.getInstance());
        // TODO : check chessboard's size
        //        adding chessboard
        //
        Chessboard chessboard = new Chessboard();
        appComtroller = new AppController(chessboard);
//        chessboard.setLocation(30, HEIGHT / 10);
//        add(chessboard);
    }

    public void paint(Graphics g){
        super.paint(g);
//        JLayeredPane jLayeredPane = new JLayeredPane();
        g.setColor(Color.ORANGE);
        // todo 设置粗细
        g.drawRect(440, 75, 80, 45);
//        g2.fillRect(510, 75, 100, 45);
//        jLayeredPane.setVisible(true);
//        add(jLayeredPane,0);
    }

    /**
     * 在游戏窗体中添加标签
     */
    // todo 对不同伦茨显示不同 label
    private void addLabel() {
        statusLabel = new JLabel("BLACK's TURN");
        statusLabel.setLocation(WIDTH * 3 / 5, HEIGHT / 10);
@@ -61,16 +298,32 @@ public class ChessGameFrame extends JFrame {
        statusLabel.setFont(new Font("Rockwell", Font.BOLD, 20));
        add(statusLabel);
    }

    public static JLabel getStatusLabel() {
        return statusLabel;
    }
    public void addLabels(String title,int y ){
        JLabel label = new JLabel(title);
        label.setSize(150,20);
        label.setLocation(450,y);
        label.setFont(new Font("Rockwell", Font.BOLD, 20));
        add(label);
    }
    public void addLabels(String title,int x,Color color){
        JLabel label = new JLabel(title);
        label.setSize(120,30);
        label.setLocation(x,50);
        label.setFont(new Font("Rockwell", Font.BOLD, 30));
        label.setForeground(color);
        add(label,1);
    }


    /**
     * 在游戏窗体中增加一个按钮,如果按下的话就会显示Hello, world!
     */

    private void addHelloButton() {
        JButton button = new JButton("Hello");
        JButton button = new JButton("Show Hello Here");
        button.addActionListener((e) -> JOptionPane.showMessageDialog(this, "Hello, world!"));
        button.setLocation(WIDTH * 3 / 5, HEIGHT / 10 + 120);
        button.setSize(180, 60);
@@ -78,20 +331,155 @@ public class ChessGameFrame extends JFrame {
        add(button);
    }


    //新建棋盘才能 load
    private void addLoadButton() {
        JButton button = new JButton("Load");
        button.setLocation(WIDTH * 3 / 5, HEIGHT / 10 + 240);
        button.setSize(180, 60);
        button.setFont(new Font("Rockwell", Font.BOLD, 20));
        button.setBackground(Color.LIGHT_GRAY);
        button.setLocation(450,280);
        button.setSize(80, 30);
        button.setFont(new Font("Rockwell", Font.BOLD, 10));
//        button.setBackground(Color.);
        add(button);

        button.addActionListener(e -> {
            System.out.println("Click load");
            String path = JOptionPane.showInputDialog(this, "Input Path here");
            appController.loadGameFromFile(path);
            appComtroller.loadGameFromFile(path);
        });
    }
    private void addExitButton() {
        JButton button = new JButton("Exit");
        button.setLocation(450,240);
        button.setSize(80, 30);
        button.setFont(new Font("Rockwell", Font.BOLD, 10));
        add(button);

        // TODO-- finish exit action
        //        frame 的 关闭和重调( =? 初始化 ?)
        button.addActionListener(e -> {
            System.out.println("Click exit");
            GameStartFrame g = new GameStartFrame();
            g.setVisible(true);
        });
    }
    private void addBGMButton() {
        JButton button = new JButton();
        button.setLocation(550,200);
        button.setSize(30, 30);
//        button.setBackground(Color.green);
        setVisible(false); //  whether visible
        add(button);

        button.addActionListener(e -> {
            System.out.println("Click BGM");
            flag = !flag;
            new Thread(() -> {
                while (true) {
                    bgm.playMusic(flag);
                }
            }).start();
        });
    }

    private void addWithdrawButton() {
        JButton button = new JButton("Withdraw");
        button.setLocation(445,560);
        button.setSize(150, 20);
        button.setFont(new Font("Rockwell", Font.BOLD, 20));
        button.setForeground(Color.white);
        // todo 设置字体为白色
        button.setBackground(Color.green);
        setVisible(false);
        add(button);

        // TODO-- finish withdraw action
        button.addActionListener(e -> {
            System.out.println("Click withdraw");

        });
    }
    private void addSendButton() {
        JButton button = new JButton("Send");
        button.setLocation(550,790);
        button.setSize(60, 20);
        button.setFont(new Font("Rockwell", Font.BOLD, 10));
        button.setBackground(Color.CYAN);
        button.setForeground(Color.white);
        setVisible(true);
        add(button);

        // TODO-- finish send action
        button.addActionListener(e -> {
            System.out.println("Click send");

        });
    }
    public void addText(){
        JTextArea textArea = new JTextArea("Enter message");
        textArea.setSize(120,20);
        textArea.setLocation(440,790);
        textArea.setLineWrap(true);
        textArea.setForeground(Color.BLACK);
        textArea.setFont(new Font("Rockwell",Font.BOLD,10));
        textArea.setBackground(Color.lightGray);
//        JScrollPane scrollPane = new JScrollPane(textArea);
//        Dimension size = textArea.getPreferredSize();
//        textArea.setBounds(0,0,100,100);
        add(textArea);
    }
    public void addPlayerList(){
        JTextArea playerListArea = new JTextArea();
        playerListArea.setSize(155,65);
        playerListArea.setLocation(443,365);
        playerListArea.setLineWrap(true);
        playerListArea.setForeground(Color.RED);
        playerListArea.setFont(new Font("Rockwell",Font.BOLD,10));
        playerListArea.setBackground(Color.WHITE);
//        playerListArea.setEnabled(false);
        playerListArea.setOpaque(false);
        add(playerListArea);
    }
    public void addActionHistory(){
        JTextArea actionHistoryArea = new JTextArea();
        actionHistoryArea.setSize(160,100);
        actionHistoryArea.setLocation(440,460);
        actionHistoryArea.setLineWrap(true);
        actionHistoryArea.setForeground(Color.RED);
        actionHistoryArea.setFont(new Font("Rockwell",Font.BOLD,10));
        actionHistoryArea.setBackground(Color.lightGray);
//        playerListArea.setEnabled(false);
        add(actionHistoryArea);
    }
    public void addRoomList(){
        JTextArea roomListArea = new JTextArea();
        roomListArea.setSize(155,65);
        roomListArea.setLocation(445,625);
        roomListArea.setLineWrap(true);
        roomListArea.setForeground(Color.BLACK);
        roomListArea.setFont(new Font("Rockwell",Font.BOLD,10));
        roomListArea.setBackground(Color.WHITE);
        // todo 后端可 print 但 user 不能编辑
//        playerListArea.setEnabled(false);
        add(roomListArea);
    }

    public void addMessages(){
        JTextArea messageArea = new JTextArea();
        messageArea.setSize(155,65);
        messageArea.setLocation(445,718);
        messageArea.setLineWrap(true);
        messageArea.setForeground(Color.BLUE);
        messageArea.setFont(new Font("Rockwell",Font.BOLD,10));
        messageArea.setBackground(Color.WHITE);
        // todo 后端可 print 但 user 不能编辑
//        playerListArea.setEnabled(false);
        add(messageArea);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ChessGameFrame mainFrame = new ChessGameFrame(650,900);
            mainFrame.setVisible(true);
        });
    }
}
+83 −0
Original line number Diff line number Diff line
package view;
import javax.swing.*;
import java.awt.*;

public class GameStartFrame extends JFrame {

    public GameStartFrame() {
        //在 label 中放置背景图片
        Image icon1 = new ImageIcon("res\\background.png").getImage().getScaledInstance(630,864,Image.SCALE_SMOOTH);
        ImageIcon icon = new ImageIcon(icon1);
        JLabel label = new JLabel(icon);
        label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
        this.getLayeredPane().add(label,Integer.valueOf(Integer.MIN_VALUE));
        //顶层容器
        JPanel imPanel = (JPanel)getContentPane();
        imPanel.setOpaque(false);

        // Frame 位置
        setLocation(400,0);
        int WIDTH = 630;
        int HEIGHT = 864;
        setSize(WIDTH, HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //添加 buttons
        addStart();
        addOnline();

    }
    private void addStart(){
        JButton start = new JButton("START");
        start.setLocation(270, 490);
//        start.setLocation(WIDTH / 2, HEIGHT * 3/ 5);
        start.setSize(130,50);
        start.setFont(new Font("Rockwell", Font.BOLD, 20));
        start.setBackground(Color.getHSBColor(255,212,60));
        add(start);

        // todo 跳转到 modes 界面
        //      退回?
        //      单例?
        start.addActionListener( e ->{
//            System.out.println("Click localMode");
            SwingUtilities.invokeLater(() -> {
                ChessGameFrame mainFrame = new ChessGameFrame(620, 895);
                mainFrame.setVisible(true);
            });
            setVisible(false);
        });
    }
    private void addOnline(){
        JButton online = new JButton("Online");
        online.setLocation(270, 550);
        online.setSize(130,50);
        online.setFont(new Font("Rockwell", Font.BOLD, 20));
        online.setBackground(Color.getHSBColor(255,212,60));
        add(online);
// todo 为什么调出的 modes Panel 这么怪?
        online.addActionListener( e ->{
            SwingUtilities.invokeLater(ModesPanel::new);
            setVisible(false);

        });
    }
    private void addContinue(){
        JButton start = new JButton("Continue");
        start.setLocation(270, 600);
        start.setSize(130,50);
        start.setFont(new Font("Rockwell", Font.BOLD, 20));
        // todo finish local file loading and set Color (exist ? yellow : dark)
        start.setBackground(Color.getHSBColor(255,212,60));
        add(start);

        // todo local file loading
        start.addActionListener( e ->{

        });
    }
//    public static void main (String[] args){
//        new GameStartFrame();
//    }
}
+93 −0
Original line number Diff line number Diff line
package view;

import javax.swing.*;
import java.awt.*;

public class ModesPanel extends JFrame {

    public ModesPanel() {

        Image icon1 = new ImageIcon("res\\modePanelBGP.jpg").getImage().getScaledInstance(640, 480, Image.SCALE_SMOOTH);
        ImageIcon icon = new ImageIcon(icon1);
        JLabel label = new JLabel(icon);
        label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
        this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));

        JPanel imPanel = new JPanel();
        imPanel.setOpaque(false);
        setLocation(500, 200);
        int WIDTH = 640;
        int HEIGHT = 480;
        setSize(WIDTH, HEIGHT);
        setVisible(true);
        // todo 做到退出并返回上级界面?
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // add buttons
//        addLocal();
        addRoom();
        addVisit();
    }

    // 新建local game
    private void addLocal() {
        JButton start = new JButton("Local");
//        start.setLocation(150, 100);
        start.setLocation(220 , 100);
        start.setSize(130, 50);
        start.setFont(new Font("Rockwell", Font.BOLD, 20));
        start.setBackground(Color.getHSBColor(255, 212, 60));
        add(start);

        start.addActionListener(e -> {
            System.out.println("Click localMode");
            // todo 跳转到游戏界面
            SwingUtilities.invokeLater(() -> {
                ChessGameFrame mainFrame = new ChessGameFrame(620, 895);
                mainFrame.setVisible(true);
            });
            setVisible(false);
        });
    }


    // 选择 room 加入游戏
    private void addRoom() {
        JButton room = new JButton("Rooms");
        // button 位置
        room.setLocation(220,180);
        // button size
        room.setSize(130, 50);
        room.setFont(new Font("Rockwell", Font.BOLD, 20));
        room.setBackground(Color.LIGHT_GRAY);
        add(room);

        room.addActionListener(e -> {
            System.out.println("Click rooms");
            // todo 跳转到 room 选择界面

        });
    }

    // 选择 room 观战
    private void addVisit() {
        JButton room = new JButton("Visit");
        // button 位置
        room.setLocation(220,250);
        // button size
        room.setSize(130, 50);
        room.setFont(new Font("Rockwell", Font.BOLD, 20));
        room.setBackground(Color.LIGHT_GRAY);
        add(room);

        room.addActionListener(e -> {
            System.out.println("Click Room");
            // todo 跳转到 room 选择界面

        });
    }

//    public static void main (String[] args){
//        new ModesPanel();
//    }
}
+2 −2
Original line number Diff line number Diff line
@@ -91,7 +91,7 @@ public class SquareCom extends JComponent {
        return switch (chessColor) {
            case RED -> switch (chessType) {
                case CANNON -> "炮";
                case GENERAL -> "";
                case GENERAL -> "";
                case ADVISOR -> "仕";
                case MINISTER -> "相";
                case CHARIOT -> "俥";
@@ -100,7 +100,7 @@ public class SquareCom extends JComponent {
            };
            case BLACK -> switch (chessType) {
                case CANNON -> "砲";
                case GENERAL -> "";
                case GENERAL -> "";
                case ADVISOR -> "士";
                case MINISTER -> "象";
                case CHARIOT -> "車";

src/view/testBGM.java

0 → 100644
+52 −0
Original line number Diff line number Diff line
package view;

import javax.sound.sampled.*;
import java.io.File;

public class testBGM {
    static boolean flag ;

    public static void playMusic(boolean f){
        flag = f;
        try{
            AudioInputStream ais = AudioSystem.getAudioInputStream(new File("D:\\download\\普通disco的伴奏.wav"));
            AudioFormat aif = ais.getFormat();
            final SourceDataLine sdl;
            DataLine.Info info = new DataLine.Info(SourceDataLine.class,aif);
            sdl = (SourceDataLine)AudioSystem.getLine(info);
            sdl.open(aif);
            sdl.start();
            FloatControl fc = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
            double value = 2;
            float dB = (float)(Math.log(value == 0.0 ? 0.0001 : value)/Math.log(10.0) * 20.0);
            fc.setValue(dB);
            int nByte = 0;
            int writeByte = 0 ;
            final int SIZE = 1024 * 64;
            byte[] buffer = new byte[SIZE];
            while (nByte != -1 ) {
                if (flag) {

                    nByte = ais.read(buffer, 0, SIZE);
                    sdl.write(buffer, 0, nByte);

                }else {

                    nByte = ais.read(buffer,0,0);

                }
            }
            sdl.stop();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
//    public static void main(String[] args){
//        new Thread(() -> {
//            while (true) {
//                playMusic();
//            }
//        }).start();
//    }
}