From f04da33974146d34bc18d7ce0872ec9388b5106a Mon Sep 17 00:00:00 2001 From: pgb58ns4h <1639613619@qq.com> Date: Sat, 19 Apr 2025 23:48:31 +0800 Subject: [PATCH] ADD file via upload --- scr/tetris/NextBlockPanel.java | 96 ++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 scr/tetris/NextBlockPanel.java diff --git a/scr/tetris/NextBlockPanel.java b/scr/tetris/NextBlockPanel.java new file mode 100644 index 0000000..6f1cbc3 --- /dev/null +++ b/scr/tetris/NextBlockPanel.java @@ -0,0 +1,96 @@ +package tetris; + +import javax.swing.*; +import java.awt.*; + +public class NextBlockPanel extends JPanel { + private Block nextBlock; + private GamePanel gamePanel; + private JLabel scoreLabel; + private final ResourceManager resManager = ResourceManager.getInstance(); + + public NextBlockPanel() { + setPreferredSize(new Dimension(200, 600)); + setLayout(new BorderLayout()); + initUI(); + } + + public void setGamePanel(GamePanel gamePanel) { + this.gamePanel = gamePanel; + } + + private static final int[] SPEED_LEVELS = {800, 400, 200}; + + private void initUI() { + JPanel previewPanel = new JPanel() { + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + if (nextBlock == null) return; + + int[][] shape = nextBlock.getShape(); + int startX = (getWidth() - shape[0].length * 30) / 2; + int startY = (getHeight() - shape.length * 30) / 2; + + for (int i = 0; i < shape.length; i++) { + for (int j = 0; j < shape[i].length; j++) { + if (shape[i][j] == 1) { + int type = nextBlock.getType(); + Image blockImage = resManager.getBlockImage(type); + if (blockImage != null) { + g.drawImage(blockImage, startX + j * 30, startY + i * 30, 29, 29, this); + } + } + } + } + } + + @Override + public Dimension getPreferredSize() { + return new Dimension(150, 150); + } + }; + previewPanel.setBorder(BorderFactory.createTitledBorder("下一个方块")); + + scoreLabel = new JLabel("得分: 0", SwingConstants.CENTER); + scoreLabel.setFont(new Font("微软雅黑", Font.BOLD, 16)); + + JPanel controlPanel = new JPanel(new GridLayout(4, 1, 0, 10)); + controlPanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10)); + + JComboBox combo = new JComboBox<>(new String[]{"简单", "中等", "困难"}); + JButton restartBtn = new JButton("重新开始"); + + combo.addActionListener(e -> { + int index = combo.getSelectedIndex(); + gamePanel.setFallSpeed((index >= 0 && index < SPEED_LEVELS.length) ? SPEED_LEVELS[index] : 500); + gamePanel.requestFocusInWindow(); + }); + + restartBtn.addActionListener(e -> { + gamePanel.newGame(); + updateScore(); + gamePanel.requestFocusInWindow(); + }); + + controlPanel.add(scoreLabel); + controlPanel.add(new JLabel("难度设置:")); + controlPanel.add(combo); + controlPanel.add(restartBtn); + + add(previewPanel, BorderLayout.CENTER); + add(controlPanel, BorderLayout.SOUTH); + + setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + revalidate(); + } + + public void setNextBlock(Block block) { + nextBlock = block; + repaint(); + } + + public void updateScore() { + SwingUtilities.invokeLater(() -> scoreLabel.setText("得分: " + gamePanel.getScore())); + } +} \ No newline at end of file