|
| 1 | +package cn.xeblog.plugin.game.game2048; |
| 2 | + |
| 3 | +import cn.xeblog.commons.enums.Game; |
| 4 | +import cn.xeblog.plugin.annotation.DoGame; |
| 5 | +import cn.xeblog.plugin.game.AbstractGame; |
| 6 | + |
| 7 | +import javax.swing.*; |
| 8 | +import java.awt.*; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author 浓睡不消残酒 |
| 12 | + * @date 2022/8/11 15:11 PM |
| 13 | + */ |
| 14 | +@DoGame(Game.GAME_2048) |
| 15 | +public class Game2048 extends AbstractGame { |
| 16 | + |
| 17 | + @Override |
| 18 | + protected void init() { |
| 19 | + initMainPanel(); |
| 20 | + mainPanel.setMinimumSize(new Dimension(150, 200)); |
| 21 | + JPanel startPanel = new JPanel(); |
| 22 | + startPanel.setBounds(10, 10, 120, 200); |
| 23 | + mainPanel.add(startPanel); |
| 24 | + |
| 25 | + JLabel title = new JLabel("2048小游戏"); |
| 26 | + title.setFont(new Font("", 1, 14)); |
| 27 | + startPanel.add(title); |
| 28 | + |
| 29 | + Box vBox = Box.createVerticalBox(); |
| 30 | + startPanel.add(vBox); |
| 31 | + vBox.add(Box.createVerticalStrut(5)); |
| 32 | + vBox.add(getStartGameButton()); |
| 33 | + vBox.add(getExitButton()); |
| 34 | + mainPanel.updateUI(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + protected void start() { |
| 39 | + initMainPanel(); |
| 40 | + int width = 240; |
| 41 | + int height = 300; |
| 42 | + mainPanel.setMinimumSize(new Dimension(width + 10, height + 10)); |
| 43 | + mainPanel.setLayout(new BorderLayout()); |
| 44 | + mainPanel.add(Box.createVerticalStrut(10), BorderLayout.NORTH); |
| 45 | + GameUI gameUI = new GameUI(); |
| 46 | + mainPanel.add(gameUI, BorderLayout.CENTER); |
| 47 | + |
| 48 | + JPanel buttonPanel = new JPanel(); |
| 49 | + buttonPanel.add(getBackGameButton()); |
| 50 | + buttonPanel.add(getExitButton()); |
| 51 | + mainPanel.add(buttonPanel, BorderLayout.SOUTH); |
| 52 | + mainPanel.updateUI(); |
| 53 | + |
| 54 | + gameUI.requestFocusInWindow(); |
| 55 | + } |
| 56 | + |
| 57 | + private JButton getStartGameButton() { |
| 58 | + JButton button = new JButton("开始游戏"); |
| 59 | + button.addActionListener(e -> start()); |
| 60 | + return button; |
| 61 | + } |
| 62 | + |
| 63 | + private JButton getBackGameButton() { |
| 64 | + JButton button = new JButton("返回游戏"); |
| 65 | + button.addActionListener(e -> init()); |
| 66 | + return button; |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments