Java实现俄罗斯方块-初始化游戏界面的JPanel

目录

技术实现:

2.初始化游戏界面; 

2.1 什么是游戏界面

2.2 JPanel面板

 2.3 流布局管理器【FlowLayout】

2.4 网络布局管理器 【GridLayout】

2.5 边界布局管理器 


技术实现:


1.初始化游戏窗口;

2.初始化游戏界面;

3.初始化游戏的说明面板;

4.随机生成下落方块;

5.绘制方块;

6.清除方块;

7.清楚某一行方块,上方方块掉落;

8.刷新清除某一行方块后的界面;

9.键盘控制下落方块的移动方向,形状;

10.判断方块能否下落;

11.实现方块下落速度的变化;

12.游戏的暂停;

2.初始化游戏界面; 

2.1 什么是游戏界面

        通俗的说就是你进入一款游戏,在屏幕上看到的都属于游戏界面;

 

2.2 JPanel面板

        JPanel也是一个容器类(非顶层容器),主要用于界面布局;

        一个界面只能有一个JFrame,但可以有多个JPanel,在JPanel的基础上还可以增加其他组件;

另外,多个JPanel相互独立,互不影响;

 

 温馨提示:

         在早期的 Java 版本中,getContentPane() 方法是必要的步骤,因为 Swing 组件不能直接添加到 JFrame 上。但随着 Java SE 5.0 的发布,可以直接在 JFrame 上使用 add() 方法添加组件,使得 getContentPane() 方法的使用变得可选,但仍然推荐使用该方法以确保最佳实践

        在使用 JFrame 创建窗口时,一般不要直接将组件添加到 JFrame 上,应该用 getContentPane() 方法获取内容面板对象后,再对其进行适当的操作;

  //将按钮,文本域等组件添加到面板上
        jpanel.add(jb);
        jpanel.add(jl);
        //使用getContentPane()获取内容面板;
        jframe.getContentPane().add(jpanel);

 2.3 流布局管理器【FlowLayout】

        这是最基本的布局方式;

class csdn {
    public void test() {
        //创建窗体
        JFrame frame=new JFrame("这是窗体的名字");
        //建个按钮
        JButton button=new JButton("登录");
        //建个Lable标签
        JLabel lable=new JLabel("这是一个JFrame窗口");
        //设置窗体可见
        frame.setVisible(true);
        frame.setBounds(0,0,600,900);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //frame.pack();//不固定窗口大小
        frame.setResizable(false);//固定窗口大小

        //来个面板
        JPanel panel=new JPanel();	
        panel.add(lable);		//面板里添加Lable
        panel.add(button);		//面板里添加按钮

     	//将窗体转换为容器再添加上面板
        frame.getContentPane().add(panel);
        frame.setLayout(new FlowLayout(1));//调用流布局,设为0时,每一行的组件将被指定按照左对齐排列
        //1是中间布局,2是右对齐排列
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        csdn c= new csdn();
        c.test();
    }
}

         

 

2.4 网络布局管理器 【GridLayout】

        1.该布局中每个组件大小相同;

        2.组件按照从左到右,从上到下的的顺序排列;

        3.改变窗体大小,组件大小也会随之改变;

class Demo2{
    public static void main(String [] args){
        JFrame jframe=new JFrame("神秘无敌宇宙力量");
        jframe.setVisible(true);
        jframe.setBounds(0,0,600,900);
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //jframe.setResizable(false);
        //jframe.pack();

        //创建按钮
        JButton jbutton1=new JButton("青龙");
        JButton jbutton2=new JButton("白虎");
        JButton jbutton3=new JButton("朱雀");
        JButton jbutton4=new JButton("玄武");
        
        //直接添加到界面上
        jframe.add(jbutton1);
        jframe.add(jbutton2);
        jframe.add(jbutton3);
        jframe.add(jbutton4);
        //分布式管理器
        jframe.setLayout(new GridLayout(3,2,10,10));
    }
}

 

2.5 边界布局管理器 

         1.不指定布局管理器时,Swing使用的就是边界布局管理器;

          2.窗体被划分为东,西,南,北,中五个区域;

class Demo4{
    public static void main(String[] args) {
        JFrame jframe=new JFrame("欢迎入坑");
        jframe.setBounds(0,0,600,900);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //创建按钮
        JButton button1=new JButton("AA");
        JButton button2=new JButton("BB");
        JButton button3=new JButton("CC");
        JButton button4=new JButton("DD");
        JButton button5=new JButton("EE");
        //边界布局管理
        jframe.add(button1,BorderLayout.EAST);
        jframe.add(button2,BorderLayout.CENTER);
        jframe.add(button3,BorderLayout.WEST);
        jframe.add(button4,BorderLayout.NORTH);
        jframe.add(button5,BorderLayout.SOUTH);
    }
}

  • 24
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现俄罗斯方块的基本步骤和代码示例: 1.定义方块类,包括方块的形状、颜色、位置等属性,以及方块的移动、旋转等方法。 ```java public class Block { private int[][] shape; // 方块的形状 private int color; // 方块的颜色 private int x, y; // 方块的位置 public Block(int[][] shape, int color, int x, int y) { this.shape = shape; this.color = color; this.x = x; this.y = y; } public void moveLeft() { x--; } public void moveRight() { x++; } public void moveDown() { y++; } public void rotate() { int[][] newShape = new int[shape[0].length][shape.length]; for (int i = 0; i < shape.length; i++) { for (int j = 0; j < shape[0].length; j++) { newShape[j][shape.length - 1 - i] = shape[i][j]; } } shape = newShape; } // 省略getter和setter方法 } ``` 2.定义游戏区域类,包括游戏区域的大小、当前方块、下一个方块等属性,以及游戏区域的初始化、更新等方法。 ```java public class GameArea { private int[][] area; // 游戏区域 private Block currentBlock; // 当前方块 private Block nextBlock; // 下一个方块 public GameArea(int width, int height) { area = new int[height][width]; init(); } public void init() { // 初始化游戏区域 for (int i = 0; i < area.length; i++) { for (int j = 0; j < area[0].length; j++) { area[i][j] = 0; } } // 初始化当前方块和下一个方块 currentBlock = createBlock(); nextBlock = createBlock(); } public void update() { // 更新当前方块的位置 currentBlock.moveDown(); // 如果当前方块已经到达底部或者与其他方块重叠,则将其固定在游戏区域中,并生成下一个方块 if (isOverlap() || isBottom()) { fixBlock(); currentBlock = nextBlock; nextBlock = createBlock(); } } public boolean isOverlap() { // 判断当前方块是否与其他方块重叠 for (int i = 0; i < currentBlock.getShape().length; i++) { for (int j = 0; j < currentBlock.getShape()[0].length; j++) { if (currentBlock.getShape()[i][j] != 0 && area[currentBlock.getY() + i][currentBlock.getX() + j] != 0) { return true; } } } return false; } public boolean isBottom() { // 判断当前方块是否到达底部 for (int i = 0; i < currentBlock.getShape().length; i++) { for (int j = 0; j < currentBlock.getShape()[0].length; j++) { if (currentBlock.getShape()[i][j] != 0 && currentBlock.getY() + i == area.length - 1) { return true; } } } return false; } public void fixBlock() { // 将当前方块固定在游戏区域中 for (int i = 0; i < currentBlock.getShape().length; i++) { for (int j = 0; j < currentBlock.getShape()[0].length; j++) { if (currentBlock.getShape()[i][j] != 0) { area[currentBlock.getY() + i][currentBlock.getX() + j] = currentBlock.getColor(); } } } } public Block createBlock() { // 随机生成一个方块 int[][][] shapes = { {{1, 1, 1, 1}}, // I型方块 {{1, 1, 0}, {0, 1, 1}}, // Z型方块 {{0, 1, 1}, {1, 1, 0}}, // S型方块 {{1, 1}, {1, 1}}, // O型方块 {{1, 1, 1}, {0, 1, 0}}, // T型方块 {{1, 1, 1}, {0, 0, 1}}, // L型方块 {{1, 1, 1}, {1, 0, 0}} // J型方块 }; int[][] shape = shapes[(int) (Math.random() * shapes.length)]; int color = (int) (Math.random() * 7) + 1; int x = area[0].length / 2 - shape[0].length / 2; int y = 0; return new Block(shape, color, x, y); } // 省略getter和setter方法 } ``` 3.定义游戏界面类,包括游戏界面的大小、游戏区域、分数等属性,以及游戏界面初始化、更新等方法。 ```java public class GamePanel extends JPanel implements ActionListener { private static final int WIDTH = 300; // 游戏界面的宽度 private static final int HEIGHT = 600; // 游戏界面的高度 private static final int BLOCK_SIZE = 30; // 方块的大小 private static final int AREA_WIDTH = 10; // 游戏区域的宽度 private static final int AREA_HEIGHT = 20; // 游戏区域的高度 private static final int DELAY = 500; // 游戏更新的延迟时间 private GameArea gameArea; // 游戏区域 private Timer timer; // 游戏更新的定时器 private int score; // 分数 public GamePanel() { setPreferredSize(new Dimension(WIDTH, HEIGHT)); setBackground(Color.WHITE); setFocusable(true); addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: gameArea.getCurrentBlock().moveLeft(); break; case KeyEvent.VK_RIGHT: gameArea.getCurrentBlock().moveRight(); break; case KeyEvent.VK_DOWN: gameArea.getCurrentBlock().moveDown(); break; case KeyEvent.VK_UP: gameArea.getCurrentBlock().rotate(); break; } } }); gameArea = new GameArea(AREA_WIDTH, AREA_HEIGHT); timer = new Timer(DELAY, this); timer.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // 绘制游戏区域 for (int i = 0; i < gameArea.getArea().length; i++) { for (int j = 0; j < gameArea.getArea()[0].length; j++) { if (gameArea.getArea()[i][j] != 0) { g.setColor(getColor(gameArea.getArea()[i][j])); g.fillRect(j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } // 绘制当前方块 Block currentBlock = gameArea.getCurrentBlock(); for (int i = 0; i < currentBlock.getShape().length; i++) { for (int j = 0; j < currentBlock.getShape()[0].length; j++) { if (currentBlock.getShape()[i][j] != 0) { g.setColor(getColor(currentBlock.getColor())); g.fillRect((currentBlock.getX() + j) * BLOCK_SIZE, (currentBlock.getY() + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } // 绘制下一个方块 Block nextBlock = gameArea.getNextBlock(); for (int i = 0; i < nextBlock.getShape().length; i++) { for (int j = 0; j < nextBlock.getShape()[0].length; j++) { if (nextBlock.getShape()[i][j] != 0) { g.setColor(getColor(nextBlock.getColor())); g.fillRect((j + AREA_WIDTH + 1) * BLOCK_SIZE, (i + 1) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE); } } } // 绘制分数 g.setColor(Color.BLACK); g.drawString("Score: " + score, (AREA_WIDTH + 1) * BLOCK_SIZE, (AREA_HEIGHT / 2) * BLOCK_SIZE); } @Override public void actionPerformed(ActionEvent e) { gameArea.update(); score += 10; repaint(); } private Color getColor(int color) { switch (color) { case 1: return Color.RED; case 2: return Color.ORANGE; case 3: return Color.YELLOW; case 4: return Color.GREEN; case 5: return Color.CYAN; case 6: return Color.BLUE; case 7: return Color.MAGENTA; default: return Color.WHITE; } } } ``` 4.在主函数中创建游戏界面并显示。 ```java public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Tetris"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.add(new GamePanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值