• 【GUI】-- 10 贪吃蛇小游戏之静态面板绘制


    GUI编程

    04 贪吃蛇小游戏

    4.1 第一步:先绘制一个静态的面板

    首先,需要新建两个类,一个StartGame类作为游戏的主启动类;一个GamePanel类作为游戏的面板类。此外,再新建一个Data类作为数据中心(存放了小蛇各部分图像的URL及ImageIcon)。代码如下:

    StartGame:

    package com.duo.snake;
    
    import javax.swing.*;
    
    //游戏的主启动类
    public class StartGame {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
    
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setTitle("贪吃蛇-2023");
            frame.setBounds(5, 10, 915, 740);
            frame.setLocationRelativeTo(null);
    
            frame.add(new GamePanel());
    
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    GamePanel:

    package com.duo.snake;
    
    import javax.swing.*;
    import java.awt.*;
    
    //游戏的面板
    public class GamePanel extends JPanel {
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);  //起到清屏的作用
    
            //先绘制一个静态的面板
            Data.header.paintIcon(this, g, 25, 11);
            g.fillRect(25, 75, 850, 600);
    
            this.setBackground(Color.white);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Data:

    package com.duo.snake;
    
    import javax.swing.*;
    import java.net.URL;
    
    //数据中心
    public class Data {
    
        //相对路径  tx.jpg
        //绝对路径  /:相当于当前的项目
        public static URL headerURL = Data.class.getResource("static/header.png");
        public static URL upURL = Data.class.getResource("static/up.png");
        public static URL downURL = Data.class.getResource("static/down.png");
        public static URL leftURL = Data.class.getResource("static/left.png");
        public static URL rightURL = Data.class.getResource("static/right.png");
        public static URL bodyURL = Data.class.getResource("static/body.png");
        public static URL foodURL = Data.class.getResource("static/food.png");
    
        public static ImageIcon header = new ImageIcon(headerURL);
        public static ImageIcon up = new ImageIcon(upURL);
        public static ImageIcon down = new ImageIcon(downURL);
        public static ImageIcon left = new ImageIcon(leftURL);
        public static ImageIcon right = new ImageIcon(rightURL);
        public static ImageIcon body = new ImageIcon(bodyURL);
        public static ImageIcon food = new ImageIcon(foodURL);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    最后,通过StartGame中main方法,显示当前绘制的静态窗口如下:

    图1


  • 相关阅读:
    Vue3详解
    pycharm如何安装pygame库
    自定义注解
    ES6 入门教程 28 异步遍历器 28.4 异步 Generator 函数 & 28.5 yield星号 语句
    Hadoop面试重点
    算法通关村第五关-二叉树遍历(深度优先)之经典问题: 递归/迭代实现二叉树前、中、后序遍历
    LeetCode题解:7. 整数反转,迭代,JavaScript,详细注释
    mfc140u.dll丢失的解决方法,以及mfc140u.dll解决方法的优缺点
    时间序列算法总结:多变量模型
    第151篇 Solidity Example(1)
  • 原文地址:https://blog.csdn.net/qq_51916086/article/details/134494685