java [25] 图形化 坦克项目之事件处理

成员类:

package tank1;




//坦克类  
class Tank{
	//坦克的横坐标
	int x =0;
	//坦克的纵坐标 
	int y =0;
	
	//坦克方向
	//0表示上,1表示右,2表示下,3表示左
	
	//坦克速度;
	int speed = 10;
	
	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}


	int dir = 0;
	public int getDir() {
		return dir;
	}

	public void setDir(int dir) {
		this.dir = dir;
	}

	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 Helo extends Tank{
	
	
	
	public Helo(int x,int y) {
		super(x,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;
		
	}
	
	
}

panel  及调用:

package tank1;

import javax.swing.JFrame;

/*
 * 
 * 
 * 坦克游戏1.0 
画出坦克
*/

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class tank_1 extends JFrame{
	mypane mp = null;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		tank_1 tk = new tank_1();
		System.out.println();
		
	}
	public tank_1(){
		mp = new mypane();
		
		
		this.add(mp);
		this.addKeyListener(mp);
		
		this.setSize(400,300);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}


// 我的面板  
class mypane extends JPanel implements KeyListener{
	
	//定义一个我的坦克
	Helo hero = null;
	
	//构造函数 
	public mypane() {
		hero = new Helo(100, 100);
		
	}
		
	//重新paint
	public void paint(Graphics g) {
		super.paint(g);
		
		//填充背景
		g.fillRect(0, 0, 400, 300);
		this.drawTank(hero.getX(), hero.getY(),g, 0, 0);
		/*g.setColor(Color.CYAN);
		// 画出我的坦克;(可以封装成一个函数)
		//左边矩形
		//g.fillRect(hero.getX(), y, 5, 30);
		g.fill3DRect(hero.getX(), y, 5, 30,false);
		
		// 画出右边的矩形
		//g.fillRect(hero.getX()+15, y, 5, 30);
		g.fill3DRect(hero.getX()+15, y, 5, 30,false);
		
		g.setColor(Color.white);
		//画出中间矩形 
		//g.fillRect(hero.getX()+5, y+5, 10, 20);
		g.fill3DRect(hero.getX()+5, y+5, 10, 20,false);
		
		//画出圆形
		g.fillOval(hero.getX()+5, y+10,10,10);
		
		//画出线
		g.drawLine(hero.getX()+10, y+10,hero.getX()+10,10);*/
		
	}
	
	//画坦克的函数 
	//x,y定义坦克的横坐标和纵坐标。
	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);
		default:
			break;
		}
		
		//判断方向
		switch (direct) {
		//向上
		case 0:
			// 画出我的坦克;(可以封装成一个函数)
			//左边矩形
			//g.fillRect(x, y, 5, 30);
			g.fill3DRect(hero.getX(),hero.getY(), 5, 30,false);
			
			// 画出右边的矩形
			//g.fillRect(x+15, y, 5, 30);
			g.fill3DRect(hero.getX()+15, hero.getY(), 5, 30,false);
			
			//画出中间矩形 
			//g.fillRect(x+5, y+5, 10, 20);
			g.fill3DRect(hero.getX()+5, hero.getY()+5, 10, 20,false);
			
			//画出圆形
			g.fillOval(hero.getX()+5, hero.getY()+10,10,10);
			
			//画出线
			g.drawLine(hero.getX()+10, hero.getY()+10,hero.getX()+10,hero.getY()-2);

			break;
		}
		
		
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	
    //对asdw做处理w向上
	//s向下
	//a向左d
	//d向右
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		System.out.println("pk!!");
		if (e.getKeyCode() == KeyEvent.VK_W) {
			this.hero.setDir(0);
			System.out.println("up");
			this.hero.moveUP();
		} else if (e.getKeyCode() == KeyEvent.VK_D) {
			System.out.println("right");
			this.hero.setDir(1);
			this.hero.moveRIGHT();
		} else if (e.getKeyCode() == KeyEvent.VK_S) {
			System.out.println("down");
			this.hero.setDir(2);
			this.hero.moveDOWN();
		} else if (e.getKeyCode() == KeyEvent.VK_A) {
			System.out.println("left");
			this.hero.setDir(3);
			this.hero.moveLEFT();
		}
		
		//必须重新绘制panel
		this.repaint();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_38125626/article/details/81410975