parent
d1941b5eeb
commit
fcd9dada56
@ -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<String> 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()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue