美文网首页
JAVA实现坦克大战小游戏——绘制坦克游戏区域

JAVA实现坦克大战小游戏——绘制坦克游戏区域

作者: 让你变好的过程从来都不会很舒服 | 来源:发表于2021-08-14 15:29 被阅读0次

    定义坦克实体

    package com.tank.tankgame;
    
    /**
     * 坦克实体类
     */
    public class Tank {
    
        private int x;
        private int y;
    
        public Tank(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        public int getX() {
            return x;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    }
    
    

    定义自己坦克实体

    package com.tank.tankgame;
    
    /**
     * 自己的坦克
     */
    public class Hero extends Tank {
    
    
        public Hero(int x, int y) {
            super(x, y);
        }
    }
    
    

    定义面板

    package com.tank.tankgame;
    
    import javafx.embed.swing.JFXPanel;
    
    import java.awt.*;
    
    /**
     * 坦克大战的绘图区域
     */
    public class MyPanel extends JFXPanel {
    
        Hero hero = null;
    
        public MyPanel(){
            new Hero(100,100); // 初始化自己的坦克
    
        }
    
        @Override
        public void  paint (Graphics g){
            super.paint(g);
    
            g.fillRect(0,0,1000,750); // 填充矩形,默认黑色
        }
    
    }
    
    

    定义画框

    package com.tank.tankgame;
    
    import javafx.application.Application;
    import javafx.stage.Stage;
    
    import javax.swing.*;
    
    public class JqTankGame01 extends JFrame {
    
        // 定义Mypanel
        MyPanel mp = null;
        public static void main(String[] args) {
            JqTankGame01 jqTankGame01 = new JqTankGame01(); // 游戏区域生成
    
        }
        public JqTankGame01(){
            mp = new MyPanel();
            this.add(mp);
            this.setSize(1000,750);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
    }
    
    

    结果展示

    相关文章

      网友评论

          本文标题:JAVA实现坦克大战小游戏——绘制坦克游戏区域

          本文链接:https://www.haomeiwen.com/subject/xfcovltx.html