Commit 865bb1f2 authored by 刘家荣's avatar 刘家荣 💬
Browse files

feat(view: background button): implemented background switching

parent e23de279
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -5,10 +5,11 @@
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
      <sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" exported="" name="alibaba.fastjson2" level="project" />
    <orderEntry type="library" exported="" name="io.vavr" level="project" />
    <orderEntry type="library" name="alibaba.fastjson2" level="project" />
    <orderEntry type="library" name="io.vavr" level="project" />
  </component>
</module>
 No newline at end of file
+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+20 −0
Original line number Diff line number Diff line
package view;

public enum Background {
	BLACK(1, "res/CBP 1.0.png"),
	CHEMISTRY(2, "res/CBP 2.0.png");

	public final int id;
	public final String path;

	private static final Background[] values = values();

	public Background next() {
		return values[(this.ordinal() + 1) % values.length];
	}

	Background(int id, String path) {
		this.id = id;
		this.path = path;
	}
}
+1 −1
Original line number Diff line number Diff line
@@ -112,7 +112,7 @@ public class SquareCom extends JComponent {

    @Override
    protected void paintComponent(Graphics g) {
        System.out.printf("repaint chess [%d,%d]\n", this.site.getX(), this.site.getY());
//        System.out.printf("repaint chess [%d,%d]\n", this.site.getX(), this.site.getY());
        //填方格颜色
        if(this.site.plate == ChessColor.NONE) {
            g.setColor(squareColor);
+24 −13
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package view.Frame;
import controller.AppController;
import controller.AppStatus;
import model.game.Game;
import view.Background;
import view.Com.ChessboardCom;
import view.testBGM;

@@ -21,7 +22,8 @@ import java.awt.event.WindowEvent;
public class ChessGameFrame extends JFrame  {

    public static ChessGameFrame instance;
    private Image icon1 ;
    private Background currentBackground = Background.BLACK;
    private JLabel backgroundLabel;
	private final int WIDTH = 620;
    private final int HEIGHT = 900;
    public final int CHESSBOARD_SIZE;
@@ -39,15 +41,7 @@ public class ChessGameFrame extends JFrame {
        setTitle("2022 CS109 Project Demo"); //设置标题
        this.CHESSBOARD_SIZE = HEIGHT * 3 / 4;

        //TODO change background in BGPButton
        icon1 = new ImageIcon("res\\CBP 1.0.png").getImage().getScaledInstance(620,895,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 bgp = (JPanel) getContentPane();
        bgp.setOpaque(false);

        initBackground();
        setSize(WIDTH, HEIGHT);
        setLocationRelativeTo(null); // Center the window.
//        getContentPane().setBackground(Color.WHITE);
@@ -188,6 +182,22 @@ public class ChessGameFrame extends JFrame {
            }).start();
        });
    }

    private void initBackground(){
        backgroundLabel = new JLabel();
        this.getLayeredPane().add(backgroundLabel,Integer.valueOf(Integer.MIN_VALUE));
        JComponent bgp = (JComponent) getContentPane();
        bgp.setOpaque(false);
        setBackground(currentBackground);
    }

    private void setBackground(Background background){
        Image image = new ImageIcon(background.path).getImage().getScaledInstance(WIDTH, HEIGHT, Image.SCALE_SMOOTH);
        ImageIcon icon = new ImageIcon(image);
        backgroundLabel.setIcon(icon);
        backgroundLabel.setBounds(0,0, icon.getIconWidth(), icon.getIconHeight());
    }

    private void addBGPButton() {
        Image img = new ImageIcon("res\\BGP icon.png").getImage().getScaledInstance(30,30,Image.SCALE_SMOOTH);
        ImageIcon icon = new ImageIcon(img);
@@ -200,7 +210,8 @@ public class ChessGameFrame extends JFrame {

        // TODO 完成
        button.addActionListener(e -> {

            currentBackground = currentBackground.next();
            setBackground(currentBackground);
        });
    }