/**
* 功能:画出坦克
*/
package com.tank;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.*;
public class myTankGame1 extends JFrame{
public static void main(String[] args){
myTankGame1 myt1=new myTankGame1();
}
public myTankGame1(){
MyPanel myp=new MyPanel();
this.add(myp);
this.setTitle("坦克大战!");
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel{
hero myhero;
public MyPanel(){
myhero=new hero(10, 10);
}
//重写paint()函数
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(myhero.getX(), myhero.getY(), g, 0, 0);
//this.drawTank(myhero.getX()+40, myhero.getY(), g, 0, 0);
}
//画出坦克
public void drawTank(int x,int y,Graphics g,int derect,int type){
switch (type){
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
switch(derect){
case 0:
//坦克方向向上
//画出我的坦克,到时再封装到函数中
//画出坦克左侧的方框
g.fill3DRect(x, y, 5, 30,false);
//画出
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20,false);
g.setColor(Color.gray);
g.fillRoundRect(x+6, y+11, 7, 7, 200, 200);
//画出线
g.drawLine(x+9, y-6, x+9, y+15);
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 this.x;
}
public int getY(){
return this.y;
}
}
//我的坦克
class hero extends Tank{
public hero(int x,int y){
super(x,y);
}
}
转载于:https://blog.51cto.com/6846041/1332164