Commit 9c3c4693 authored by lynn's avatar lynn
Browse files

尝试写 drawBullet 和 RoomFrame ;

添加 message 的写入方法 writeMessage 和 writePlayer
parent 8d0c778a
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
    </content>
+24 −0
Original line number Diff line number Diff line
@@ -3,6 +3,10 @@ package controller;
import model.game.Game;
import model.game.Msg;

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

public class BulletAnim extends Thread{
    @Override
    public void run() {
@@ -21,5 +25,25 @@ public class BulletAnim extends Thread{

    public void drawBullet(Msg msg){
        //TODO: drawBullet

        JPanel bullet = new JPanel();
        JTextArea bulletText = new JTextArea();
        bullet.add(bulletText);

        bullet.setOpaque(false);
        bulletText.setSize(50,20);
        bulletText.setFont(new Font("Rockwell", Font.BOLD, 20));
        bullet.setVisible(true);
        Timer draw = new Timer(100, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int x = 400;
                bulletText.setText(String.valueOf(msg));
                while (x >= 0) {
                    x -= 20;
                    bullet.setLocation(x, 200);
                }
            }
        });
    }
}
+7 −1
Original line number Diff line number Diff line
@@ -13,4 +13,10 @@ public class Room {
    
    public Account host;

    public static List<Room> getRoomList() {
        return roomList;
    }
    public String toString(){
        return this.name + this.host + this.url;
    }
}
+75 −40
Original line number Diff line number Diff line
@@ -2,12 +2,9 @@ package view;

import controller.AppController;
import controller.AppStatus;
import model.game.Chessboard;

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

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


    public ChessGameFrame(int width, int height) {
    //message 要在多个 method 中调用
    private JTextArea playerListArea;
    private JTextArea messageArea;
    private JTextArea textArea;
    private JTextArea actionHistoryArea ;
    public ChessGameFrame() {
        setTitle("2022 CS109 Project Demo"); //设置标题
        this.WIDTH = width;
        this.HEIGHT = height;
        this.CHESSBOARD_SIZE = HEIGHT * 4 / 5;

        //TODO use gridLayout
@@ -50,35 +48,37 @@ public class ChessGameFrame extends JFrame {

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

        //todo 用两个 label 分别挡住 red black(曲线救国)
        //      要图层的效果得写三个 panel ( technically )
        addChessboard();
//        addLabel();
//        addHelloButton();
        addExitButton();
        addLoadButton();
        addBGMButton();
        addWithdrawButton();
        addSendButton();

        // 加入 textArea
        addText();
        addPlayerList();
        addActionHistory();
//        addActionHistory();
        addRoomList();
        addMessages();
        addScore(0,440);
        addScore(1,535);


        addExitButton();
        addLoadButton();
        addBGMButton();
        addWithdrawButton();
        addSendButton();

        //加入 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);
//        addLabels("Action History",435);
        addLabels("Room List",435); //603
        addLabels("Messages",603); //688

//        JPanel paintPanel = new JPanel();
        /*
@@ -311,6 +311,7 @@ public class ChessGameFrame extends JFrame {
        label.setFont(new Font("Rockwell", Font.BOLD, 20));
        add(label);
    }
    // red & black labels
    public void addLabels(String title,int x,Color color){
        JLabel label = new JLabel(title);
        label.setSize(120,30);
@@ -337,7 +338,7 @@ public class ChessGameFrame extends JFrame {
    //新建棋盘才能 load
    private void addLoadButton() {
        JButton button = new JButton("Load");
        button.setLocation(450,280);
        button.setLocation(450,240);
        button.setSize(80, 30);
        button.setFont(new Font("Rockwell", Font.BOLD, 10));
//        button.setBackground(Color.);
@@ -351,7 +352,7 @@ public class ChessGameFrame extends JFrame {
    }
    private void addExitButton() {
        JButton button = new JButton("Exit");
        button.setLocation(450,240);
        button.setLocation(450,200);
        button.setSize(80, 30);
        button.setFont(new Font("Rockwell", Font.BOLD, 10));
        add(button);
@@ -363,7 +364,7 @@ public class ChessGameFrame extends JFrame {
        });
    }
    private void addBGMButton() {
        JButton button = new JButton();
        JButton button = new JButton("bgm");
        button.setLocation(550,200);
        button.setSize(30, 30);
//        button.setBackground(Color.green);
@@ -383,8 +384,8 @@ public class ChessGameFrame extends JFrame {

    private void addWithdrawButton() {
        JButton button = new JButton("Withdraw");
        button.setLocation(445,560);
        button.setSize(150, 20);
        button.setLocation(450, 280);
        button.setSize(120, 30);
        button.setFont(new Font("Rockwell", Font.BOLD, 20));
        button.setForeground(Color.white);
        // todo 设置字体为白色
@@ -398,7 +399,8 @@ public class ChessGameFrame extends JFrame {

        });
    }
    private void addSendButton() {

    public void addSendButton() {
        JButton button = new JButton("Send");
        button.setLocation(550,790);
        button.setSize(60, 20);
@@ -407,15 +409,30 @@ public class ChessGameFrame extends JFrame {
        button.setForeground(Color.white);
        setVisible(true);
        add(button);

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

            actionHistoryArea.setText("");
            writeMessage(textArea.getText());
            textArea.setText("");
        });
    }

    // chessType : 0 for red, 1 for black
    public void addScore(int chessType,int x){
        JTextArea score = new JTextArea();
        score.setLocation(x,110);
        score.setSize(65,40);
        score.setBackground(Color.black);
        score.setVisible(true);

        // todo instance is null
//        int[] getScore = Game.instance.getScoreboard();
//        score.setText(String.valueOf(getScore[chessType]));
        add(score);
    }
    public void addText(){
        JTextArea textArea = new JTextArea("Enter message");
        textArea = new JTextArea("Enter message");
        textArea.setSize(120,20);
        textArea.setLocation(440,790);
        textArea.setLineWrap(true);
@@ -428,7 +445,7 @@ public class ChessGameFrame extends JFrame {
        add(textArea);
    }
    public void addPlayerList(){
        JTextArea playerListArea = new JTextArea();
        playerListArea = new JTextArea();
        playerListArea.setSize(155,65);
        playerListArea.setLocation(443,365);
        playerListArea.setLineWrap(true);
@@ -440,7 +457,7 @@ public class ChessGameFrame extends JFrame {
        add(playerListArea);
    }
    public void addActionHistory(){
        JTextArea actionHistoryArea = new JTextArea();
        actionHistoryArea = new JTextArea();
        actionHistoryArea.setSize(160,100);
        actionHistoryArea.setLocation(440,460);
        actionHistoryArea.setLineWrap(true);
@@ -453,7 +470,8 @@ public class ChessGameFrame extends JFrame {
    public void addRoomList(){
        JTextArea roomListArea = new JTextArea();
        roomListArea.setSize(155,65);
        roomListArea.setLocation(445,625);
        roomListArea.setLocation(450,460);
        // y 625
        roomListArea.setLineWrap(true);
        roomListArea.setForeground(Color.BLACK);
        roomListArea.setFont(new Font("Rockwell",Font.BOLD,10));
@@ -463,22 +481,39 @@ public class ChessGameFrame extends JFrame {
        add(roomListArea);
    }

    //为了实现历史消息可见的效果,该方法最终的操作是在主 frame 上添加只有 massage 文本框的 ScrollPane
    // todo 完成设置
    public void addMessages(){
        JTextArea messageArea = new JTextArea();
        messageArea.setSize(155,65);
        messageArea.setLocation(445,718);

        messageArea = new JTextArea();
//        JScrollPane jScrollPane = new JScrollPane(messageArea);
//        jScrollPane.setSize(messageArea.getPreferredSize());
//        jScrollPane.setVisible(true);
        messageArea.setSize(155,150); //65

        messageArea.setLineWrap(true);
        messageArea.setForeground(Color.BLUE);
        messageArea.setFont(new Font("Rockwell",Font.BOLD,10));
        messageArea.setBackground(Color.WHITE);
        messageArea.setBackground(Color.green);
//        jScrollPane.setLocation(445,718);
        // todo 后端可 print 但 user 不能编辑
//        playerListArea.setEnabled(false);
//        messageArea.setEnabled(false);
        messageArea.setLocation(445,625);
//        add(jScrollPane);
        add(messageArea);
    }

    // 实现对 messageArea 的写入
    public void writeMessage(String s){
        messageArea.append(s + "\r\n");
    }
    public void writePlayer(String s){
        playerListArea.append(s + "\r\n");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ChessGameFrame mainFrame = new ChessGameFrame(650,900);
            ChessGameFrame mainFrame = new ChessGameFrame();
            mainFrame.setVisible(true);
        });
    }
+1 −2
Original line number Diff line number Diff line
@@ -59,9 +59,8 @@ public class GameStartFrame extends JFrame {
        add(online);
// todo 为什么调出的 modes Panel 这么怪?
        online.addActionListener( e ->{
            SwingUtilities.invokeLater(ModesPanel::new);
            new RoomFrame();
            setVisible(false);

        });
    }
    private void addContinue(){
Loading