/*
*
* 功能:坦克大战1.0.版
*/
package com.haitao.tank;
import javax.swing.*;
import java.awt.*;
public class MyTankGame1 extends JFrame{
MyPanel mp=null;
public static void main(String[] args) {
MyTankGame1 mt=new MyTankGame1();
}
//构造函数
public MyTankGame1()
{
mp=new MyPanel();
this.add(mp);
this.setSize(400,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//我的面板
class MyPanel extends JPanel
{
//初始化一个我的坦克
Tank hero=null;
//构造函数
public MyPanel()
{
hero=new Tank (10,10);
}
//重写paint
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0,0,400,300);
this.drawTank(hero.getX(), hero.getY(), g, 0, 0);
}
//画出坦克函数
public void drawTank(int x,int y,Graphics g,int direct,int type)
{
//判断是什么类型的坦克
switch(type)
{
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
//判断方向
switch(direct)
{
//向上
case 0:
//画出我的坦克(到时再封装成一个函数)
//1、画出左边的矩形
g.fillRect(x, hero.getY(), 5, 30);
//画出右边的矩形
g.fillRect(x+15, hero.getY(), 5, 30);
//画出中间矩形
g.fill3DRect(x+5, hero.getY()+5, 10, 20, false);
//画出中间的圆形
g.fillOval(x+5, hero.getY()+10, 10, 10);
//画出炮筒
g.drawLine(x+10, hero.getY()+15, x+10, x);
break;
}
}
}
//定义一个坦克类
class Tank
{
//表示坦克的横坐标
int x=0;
//表示坦克纵坐标
int y=0;
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;
}
}
//定义我的坦克
class Hero extends Tank
{
public Hero(int x, int y) {
super(x, y);
}
}
java实现坦克大战1.0版 在面板上画出坦克
最新推荐文章于 2022-12-16 12:17:29 发布