首页 > 代码库 > Java TEST
Java TEST
package com.sunzhiyan04;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
import java.util.*;
public class Tank_1 extends JFrame{
Mypanel_1 jp1 = null;
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
// TODO Auto-generated method stub
Tank_1 tank = new Tank_1();
}
public Tank_1() {
// TODO Auto-generated constructor stub
jp1 = new Mypanel_1();
this.add(jp1);
this.addKeyListener(jp1);
this.setTitle("Tank_1");
this.setSize(300,300);
this.setLocation(200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Mypanel_1 extends JPanel implements KeyListener{
//实例化一个我的坦克
Hero hero = null;
Vector<EnemtyTank> ets = new Vector<>();
int ensize = 3;
public Mypanel_1(){
hero = new Hero(10,10);
for(int i=0;i<ensize;i++ ){
EnemtyTank et = new EnemtyTank((i+1)*50,0);
ets.add(et);
}
//this.addKeyListener(this);
}
public void paint(Graphics g){
super.paint(g);
//设置背景为一块黑色的矩形运动
g.fillRect(0, 0, 400, 300);
//调用画坦克的方法
this.drawTank(hero.getX(), hero.getY(), g, hero.direct, hero.type);
//画子弹
if(hero.shot !=null){
g.draw3DRect(hero.getX(), hero.getY(), 10, 10, false);
}
for(int i=0;i<ets.size();i++){
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).direct, ets.get(i).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.BLUE);
break;
}
switch(direct)
{
case 0:
case 2:
//画出我的坦克(届时封装成成一个画坦克的方法)
//1.左边的矩形
g.fill3DRect(x,y, 5, 30, false);
//2.右边的矩形
g.fill3DRect(x+15,y, 5, 30, false);
//2.中间的矩形
g.fill3DRect(x+5,y+5, 10, 20, false);
//3.画出圆形
g.fillOval(x+5,y+10, 10, 10);
if(direct == 0){
//3.画出线
g.drawLine(x+10,y+15, x+10, y);
}else if(direct == 2){
//3.画出线
g.drawLine(x+10,y+15, x+10, y+30);
}
break;
case 1:
case 3:
//画出我的坦克(届时封装成成一个画坦克的方法)
//1.左边的矩形
g.fill3DRect(x,y,30, 5, false);
//2.右边的矩形
g.fill3DRect(x,y+15, 30, 5, false);
//2.中间的矩形
g.fill3DRect(x+5,y+5, 20, 10, false);
//3.画出圆形
g.fillOval(x+10,y+5, 10, 10);
if(direct == 1){
//3.画出线
g.drawLine(x+15,y+10, x+30, y+10);
}else if(direct == 3){
//3.画出线
g.drawLine(x+15,y+10, x, y+10);
}
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
//设置我坦克的方向
if(e.getKeyCode() == KeyEvent.VK_W){
//上
this.hero.direct = 0;
this.hero.moveUP();
}else if(e.getKeyCode() == KeyEvent.VK_D){
this.hero.direct = 1;
this.hero.moveRIGHT();
//右
}else if(e.getKeyCode() == KeyEvent.VK_S){
//下
this.hero.direct = 2;
this.hero.moveDOWN();
}else if(e.getKeyCode() == KeyEvent.VK_A){
//左
this.hero.direct = 3;
this.hero.moveLEFT();
}
if(e.getKeyCode() == KeyEvent.VK_J){
//判断玩家是否按下J键
this.hero.shotEnemy();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
//子弹类
class Shot implements Runnable
{
int x ;
int y;
int direct;
public Shot(int x,int y){
this.x = x;
this.y = y;
}
public void run(){
}
}
//坦克类
class Tank{
int x;
int y;
int direct;
int type;
int speed = 2;
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
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 void moveUP(){
this.y -= this.speed;
}
public void moveRIGHT(){
this.x += this.speed;
}
public void moveDOWN(){
this.y += this.speed;
}
public void moveLEFT(){
this.x -= this.speed;
}
public Tank(int x,int y){
this.x = x;
this.y = y;
}
}
//定义我的坦克
class Hero extends Tank{
//子弹是坦克的一个属性
Shot shot = null;
public Hero(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
//子弹发射方法
public void shotEnemy(){
switch(this.direct){
case 0:
shot = new Shot(this.x+10,this.y);
break;
case 1:
shot = new Shot(this.x+30,this.y+10);
break;
case 2:
shot = new Shot(this.x+10,this.y+30);
break;
case 3:
shot = new Shot(this.x,this.y+10);
break;
}
}
}
//定义我的坦克
class EnemtyTank extends Tank{
public EnemtyTank(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
}