坦克大战的学习

发布于:2025-09-14 ⋅ 阅读:(18) ⋅ 点赞:(0)

1.绘制入门和机制

package com.hspedu.draw;


import javax.swing.*;
import java.awt.*;

@SuppressWarnings({"all"})
// 演示如何在面板上画出圆形
public class DrawCircle extends JFrame{ // JFrame 对应窗口,可以理解成是一个画框
    //定义一个面板
    private MyPanel mp = null;
    public static void main(String[] args) {
        new DrawCircle();
        System.out.println("退出程序~");

    }

    public DrawCircle() { // 构造器
        //初始化面板
        mp = new MyPanel();
        //把面板放入到窗口(画框)
        this.add(mp);
        //设置窗口的大小
        this.setSize(400, 300);
        // 当点击窗口的小 x ,程序就会完全退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true); // 可以显示
    }
}

//1.先定义一个 MyPanel(),继承 JPanel 类,画图形,就在面板上画
class MyPanel extends JPanel {

    //说明
    //1. Mypanel 对象就是一个画板
    //2. Graphics g 把 g 理解成一支画笔
    //3. Graphics 提供了很多绘图方法
    // Graphics g
    @Override
    public void paint(Graphics g) { // 绘图方法
        super.paint(g); // 调用父类的方法完成初始化
//        System.out.println("paint 方法被调用");
        // 画出一个圆形
        g.drawOval(10, 10, 100, 100);
    }
}

2.绘图方法

package com.hspedu.draw;


import javax.swing.*;
import java.awt.*;

@SuppressWarnings({"all"})
// 演示如何在面板上画出圆形
public class DrawCircle extends JFrame{ // JFrame 对应窗口,可以理解成是一个画框
    //定义一个面板
    private MyPanel mp = null;
    public static void main(String[] args) {
        new DrawCircle();
        System.out.println("退出程序~");

    }

    public DrawCircle() { // 构造器
        //初始化面板
        mp = new MyPanel();
        //把面板放入到窗口(画框)
        this.add(mp);
        //设置窗口的大小
        this.setSize(400, 300);
        // 当点击窗口的小 x ,程序就会完全退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true); // 可以显示
    }
}

//1.先定义一个 MyPanel(),继承 JPanel 类,画图形,就在面板上画
class MyPanel extends JPanel {

    //说明
    //1. Mypanel 对象就是一个画板
    //2. Graphics g 把 g 理解成一支画笔
    //3. Graphics 提供了很多绘图方法
    // Graphics g
    @Override
    public void paint(Graphics g) { // 绘图方法
        super.paint(g); // 调用父类的方法完成初始化
        System.out.println("paint 方法被调用");
        // 画出一个圆形
        //g.drawOval(10, 10, 100, 100);

        //演示绘制不同的图形...
        //画直线 drawLine(int x1, int y1, int x2, int y2)
        //g.drawLine(10, 10, 100, 100);

        //画矩形边框 drawRect(int x, int y, int width, int height)
//        g.drawRect(10, 10, 100, 100);

        //画椭圆边框 drawOval(int x, int y, int width, int height)

        //填充矩形 fillRect(int x, int y, int width, int height)
        //设置画笔的颜色
//        g.setColor(Color.blue);
//        g.fillRect(10, 10, 100, 100);

        //填充椭圆 fillOval(int x, int y, int width, int height)
//        g.setColor(Color.red);
//        g.fillOval(10, 10, 100, 100);

        //画图片 drawImage(Image img, int x, int y, ...)
        //1.获取图片资源  /pic.jpg 表示在该项目的根目录去获取 pic.jpg 图片资源
//        Image image = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/pic.jpg"));
//        g.drawImage(image, 10, 10, 200, 200, this);


        //画字符串 drawString(String str, int x, int y)
        //给画笔设置颜色和字体
        g.setColor(Color.red);
        g.setFont(new Font("隶书", Font.BOLD, 50));
        //这里设置的 100 100 是 "孙悟空" 的左下角
        g.drawString("孙悟空", 100, 100);

        //设置画笔的字体 setFont(Font font)

        //设置画笔的颜色 setColor(Color c)
    }
}

3.绘制坦克游戏区域

package com.hspedu.tankgame;

import javax.swing.*;

public class HspTankGame01 extends JFrame {
    //定义 MyPanel
    MyPanel mp = null;
    public static void main(String[] args) {
        HspTankGame01 hspTankGame01 = new HspTankGame01();
        
    }

    public HspTankGame01() {
        mp = new MyPanel();
        this.add(mp); // 把面板(就是游戏的绘图区域)
        this.setSize(1000, 750);;
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}
package com.hspedu.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.hspedu.tankgame;

public class Hero extends Tank{
    public Hero(int x, int y) {
        super(x, y);
    }
}
package com.hspedu.tankgame;

import javax.swing.*;
import java.awt.*;

//坦克大战绘图区域
public class MyPanel extends JPanel {
    //定义我的坦克
    Hero hero = null;

    public MyPanel() {
        hero = new Hero(100, 100); // 初始化自己的坦克
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750); // 填充矩形,默认是黑色

    }
}

  


网站公告

今日签到

点亮在社区的每一天
去签到