您的位置:首页 > 编程语言 > Java开发

java基础学习之面对对面的图形绘制 坦克

2016-08-19 15:34 417 查看
/**
*
*/
package com.javaTest;
import java.awt.*;

import javax.sw
ac06
ing.*;
/**
* @author Administrator
*
*/
public class MyTankGame1 extends JFrame{

/**
* @param args
*/
MyPanel mp=null;
public static void main(String[] args) {
// TODO 自动生成的方法存根
MyTankGame1 mtg =new MyTankGame1();

}
//构造函数
public MyTankGame1()
{
mp=new MyPanel();
this.add(mp);
this.setSize(400,300);
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);
}
//画出坦克的函数
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://上

//画出我的坦克
//画出左面的矩形
g.fill3DRect(x,y,5,30,true);
//画出右边的矩形
g.fill3DRect(x+15,y,5,30,true);
//画出中间矩形
g.fill3DRect(x+5,y+5,10,20,true);
//画出中间圆

g.fillOval(x+5,y+10,10,10);
//画线
g.drawLine(x+10,y+15, x+10, y-7);
break;

}

}
}

}

class Tank
{
int x;
int 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;
}

public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
}
//我的坦克
class Hero extends Tank
{
public Hero(int x,int y)
{
super(x, y);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: