• 【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


  • 相关阅读:
    EasyExcel表头校验方法
    【牛客网-公司真题-前端入门篇】——2021牛客模考-卷1
    鸿蒙原生应用开发-DevEco Studio中HarmonyOS与OpenHarmony项目的切换
    TCP连接保活机制
    电磁场几何和衍射理论的统一
    Diango项目-简易个人博客项目
    创建你的第一个Vue项目(小白专享版本)
    MySQL基础-多表查询
    USB HID在系统下通信的一些总结
    (选专业)什么性格的人适合医学类专业?
  • 原文地址:https://blog.csdn.net/qq_51916086/article/details/134494685