坦克大战-第一天(画出坦克)

/**
 * 作者:@author ZhangShaoWen
 * 日期:2013-12-04
 * 功能:坦克1.0
 */
package com.tank;

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

public class MyTankGame1 extends JFrame{

	MyPanel mp = null;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyTankGame1();
	}
	
	//构造函数
	public MyTankGame1(){
		mp = new MyPanel();
		
		this.add(mp);
		this.setSize(400,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}

//我的面板
class MyPanel extends JPanel{
	
	//定义一个坦克
	Hero hero = null;
	
	//构造函数
	public MyPanel(){
		
		hero = new Hero(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, 1);
	}
	
	/**
	 * //画出坦克的函数
	 * @param x 起始位置x
	 * @param y 起始位置y
	 * @param g 画笔
	 * @param direct 坦克的方向
	 * @param type 坦克的类型
	 */
	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);
		}
		
		switch(direct){//判断坦克的方向
			
			case 0:
				//画出我的坦克
				//1.画出左边的矩形
				g.fill3DRect(x, y, 5, 30, false);
				//2.画出右边的矩形
				g.fill3DRect(x+15, y, 5, 30, false);
				//3.画出中间矩形
				g.fill3DRect(x+5, y+5, 10, 20, false);
				//4.画出圆形
				g.fillOval(x+4, y+10, 10, 10);
				//5.画出线
				g.drawLine(x+10, y, x+10, y+5);
				break;
		}
	}
}

//坦克类
class Tank{

	//x表示坦克的横坐标
	int x=0;
	//y表示坦克的纵坐标
	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);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹道人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值