Java-Java基础05之坦克大战v1.0到v1.2
主要内容有:使用Java的绘图原理绘直线、矩形、文字、圆及孤形曲线,一步步从v1.0到v2.0开发坦克大战小游戏,使用java事件处理机制,监听敌方坦克与对子弹的控制,处理键盘事件控制坦克的移动等。
java绘图原理Demo9_1.java
/*
* 功能:java绘图原理
*/
package com.test1;
import java.awt.*;
import javax.swing.*;
public class Demo9_1 extends JFrame{
/**
* @param args
*/
public static void main(String[] args) {
Demo9_1 demo9_1=new Demo9_1();
}
public Demo9_1()
{
MyPanel1 mp=new MyPanel1();
this.add(mp);
this.setSize(400,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//定义一个MyPanel(自定义的面板,用于绘图和现实绘图的区域)
class MyPanel1 extends JPanel
{
//覆盖JPanel的paint方法
public void paint(Graphics g) //Graphics是绘图的重要类,画笔类
{ //paint(Graphic g)绘制组件的外观,repaint()刷新组件的外观
//1.调用父类函数完成初始化
//这句话,不能少
super.paint(g);
System.out.println("paint被调用");
//以下情况paint()会被调用
//1.窗口最小化,再最大化。
//2.窗口的大小发生变化
//3.repaint函数被调用
//先画出一个圆
g.drawOval(10, 10, 30, 30);
//画出直线
g.drawLine(80, 10, 40, 40);
//画出矩形边框
g.drawRect(10, 50, 40, 60);
//设置颜色
g.setColor(Color.blue);
//填充矩形
g.fillRect(10,50,40,60);
g.setColor(Color.red);
g.fillRect(70,70,40,40); //填充矩形
g.fillOval(70, 10, 60, 40); //填充椭圆
//在面板上画出图片,记住这种在面板上画图像的方法
Image im=Toolkit.getDefaultToolkit().getImage
(Panel.class.getResource("/刘亦菲2.jpg"));
//实现
g.drawImage(im, 140, 10, 200, 300, this);
//画出文字
g.setColor(Color.red); //设置颜色
g.setFont(new Font("微软简行楷",Font.BOLD,30)); //设置字体
g.drawString("祖国万岁", 5, 150); //画文字
//画孤形
g.drawArc(10, 180, 120, 300, 50, 100);
}
}
坦克大战v1.0_Demo9_2
/*
* 功能:坦克大战v1.0
* 1.画出坦克
*/
package com.test2;
import java.awt.*;
import javax.swing.*;
public class MyTankGame1 extends JFrame{
MyPanel mp=null;
public MyTankGame1()
{
mp=new MyPanel();
this.add(mp);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
MyTankGame1 mtg=new MyTankGame1();
}
}
//我的面板
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: //向上
//画出我的坦克(到时封装成一个函数)
//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+5, y+10, 10, 10);
//5.画出线
g.drawLine(x+10, y+15, x+10, y);
//画齿轮
g.setColor(Color.darkGray);
g.drawLine(x+1, y+2, x+4, y+2);
g.drawLine(x+1, y+5, x+4, y+5);
g.drawLine(x+1, y+8, x+4, y+8);
g.drawLine(x+1, y+11, x+4, y+11);
g.drawLine(x+1, y+14, x+4, y+14);
g.drawLine(x+1, y+17, x+4, y+17);
g.drawLine(x+1, y+20, x+4, y+20);
g.drawLine(x+1, y+23, x+4, y+23);
g.drawLine(x+1, y+27, x+4, y+27);
g.drawLine(x+16, y+2, x+19, y+2);
g.drawLine(x+16, y+5, x+19, y+5);
g.drawLine(x+16, y+8, x+19, y+8);
g.drawLine(x+16, y+11, x+19, y+11);
g.drawLine(x+16, y+14, x+19, y+14);
g.drawLine(x+16, y+17, x+19, y+17);
g.drawLine(x+16, y+20, x+19, y+20);
g.drawLine(x+16, y+23, x+19, y+23);
g.drawLine(x+16, y+27, x+19, y+27);
break;
}
}
}
//坦克类
class Tank
{
//表示坦克的横坐标
int x=0;
//坦克纵坐标
int y=0;
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); //用父类的构造函数初始化子类的成员变量
}
}
java事件处理机制_Demo9_3.java
/*
* 功能:java事件处理机制
*
*/
/*
* 1.一个类要实现监听的步骤
* a.实现相应的接口[KeyListener,MouseListener,ActionListener,WindowListener,]
* b.把接口的处理方法根据需要重新编写(override)
* c.在事件源注册监听
* d.事件传递是靠事件对象
*/
package com.test3;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Demo9_3 extends JFrame implements ActionListener //让窗体对象实现对事件的监听
{
//定义一个panel
JPanel mp=null;
JButton jb1=null;
JButton jb2=null;
public static void main(String[] args) {
Demo9_3 demo9_3=new Demo9_3();
}
public Demo9_3()
{
//初始化一个面板和两个按钮
mp=new JPanel();
jb1=new JButton("黑色");
jb2=new JButton("红色");
Cat myCat1=new Cat();
//注册监听
jb1.addActionListener(this); //让窗体对象监听
jb1.addActionListener(myCat1); //让猫对象监听
//指定action命令
jb1.setActionCommand("blackbtn");
jb2.addActionListener(this); //添加事件监听,让窗体对象监听
jb2.addActionListener(myCat1); //让猫对象监听
jb2.setActionCommand("redbtn");
this.add(jb1,BorderLayout.NORTH); //将按钮jb1添加到窗体北边
mp.setBackground(Color.black); //设置面板颜色
this.add(mp);
this.add(jb2,BorderLayout.SOUTH); //将按钮jb2添加到窗体南边
this.setSize(200,150); //设置窗体大小
this.setLocation(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//对事件处理的方法
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("OK");
//判断是哪个按钮被点击
if(e.getActionCommand().equals("blackbtn"))
{
System.out.println("你点击黑色按钮了!");
mp.setBackground(Color.BLACK);
}
else if(e.getActionCommand().equals("redbtn"))
{
System.out.println("你点击红色按钮了!");
mp.setBackground(Color.RED);
}
else
{
System.out.println("不知道!");
}
}
}
//任何一个类,只要他实现了相应的接口,就可以去临界听某个事件源
class Cat implements ActionListener //让猫也现实对事件的监听
{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("blackbtn"))
{
System.out.println("猫监听到你按下了黑色按钮!");
}
else if(e.getActionCommand().equals("redbtn"))
{
System.out.println("猫监听到你按下了红色按钮!");
}
else
{
System.out.println("猫没有监听到按钮消息!");
}
}
}
加深对事件处理机制的理解Demo9_4.java
/*
* 功能:加深对事件处理机制的理解
* 1.通过上下左右键,来控制一个小球的位置
*/
package com.test4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.KeyListener;
public class Demo9_4 extends JFrame{
/**
* @param args
*/
public static void main(String[] args) {
Demo9_4 demo9_4=new Demo9_4();
}
//构造函数
public Demo9_4()
{
MyPanel mp=new MyPanel();
//mp加入到JFrame
this.add(mp);
//实现监听
this.addKeyListener(mp);
this.setSize(400,300);
this.setLocation(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//定义自己的面板
class MyPanel extends JPanel implements KeyListener
{
int x=10;
int y=10;
public void paint(Graphics g)
{
super.paint(g);
g.fillOval(x, y, 20, 20);
}
@Override
public void keyPressed(KeyEvent e) //某一个键被压下
{
//System.out.println("键被按下!"+(char)e.getKeyCode());
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
if(y<248)
y+=2;
else
return;
}
else if(e.getKeyCode()==KeyEvent.VK_UP)
{
if(y>0)
y-=2;
else
return;
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
if(x>0)
x-=2;
else
return;
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
if(x<368)
x+=2;
else
return ;
}
else
return;
//调用repaint(),重绘窗口
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) //某一个键被释放
{
System.out.println("键被释放!");
}
@Override
public void keyTyped(KeyEvent e) //键的值被输出
{
System.out.println("被按下的键是:"+(char)e.getKeyChar());
}
}
监听器Demo9_5.java
/*
* 功能:让一个监听器监听多个不同的事件
*/
package com.test5;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo9_5 extends JFrame{
MyPanel mp=null;
public static void main(String[] args) {
Demo9_5 demo9_5=new Demo9_5();
}
//构造函数
public Demo9_5()
{
mp=new MyPanel();
this.add(mp);
//注册监听
this.addMouseListener(mp);
this.addKeyListener(mp);
this.addMouseMotionListener(mp);
this.addWindowListener(mp);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//1.让MyPanel知道鼠标按下的消息,并且知道点击的位置(x,y)
//2.让MyPanel知道哪个键按下
//3.让MyPanel知道鼠标移动、拖拽
//4.让MyPanel知道窗口的变化(关闭,最小化,最大化)
class MyPanel extends JPanel implements
MouseListener,KeyListener,MouseMotionListener,WindowListener
{
public void paint(Graphics g)
{
super.paint(g);
}
@Override
public void mouseClicked(MouseEvent e) //鼠标点击
{
System.out.println("鼠标点击了 x="+e.getX()+" y="+e.getY());
}
@Override
public void mouseEntered(MouseEvent e) //鼠标移入
{
System.out.println("鼠标来了");
}
@Override
public void mouseExited(MouseEvent e) //鼠标离开MyPanel
{
System.out.println("鼠标走了");
}
@Override
public void mousePressed(MouseEvent e) //鼠标按下
{
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) //鼠标释放
{
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) //键按下
{
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) //键松开
{
System.out.println("按下的键是:"+e.getKeyChar());
}
@Override
public void keyTyped(KeyEvent e) //打印键的值
{
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) //鼠标拖拽
{
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) //鼠标移动
{
System.out.println("鼠标当前坐标是:x="+e.getX()+"y="+e.getY());
}
@Override
public void windowActivated(WindowEvent e) //窗口变为活动窗口
{
System.out.println("windowActivated");
}
@Override
public void windowClosed(WindowEvent e) //窗口关闭了
{
System.out.println("windowClosed");
}
@Override
public void windowClosing(WindowEvent e) //窗口正在关闭
{
System.out.println("windowClosing");
}
@Override
public void windowDeactivated(WindowEvent e) //窗口变为非活动窗口
{
System.out.println("windowDeactivated");
}
@Override
public void windowDeiconified(WindowEvent e) //窗口变到最大图标区域
{
System.out.println("windowDeiconified");
}
@Override
public void windowIconified(WindowEvent e) //窗口变到最小图标区域
{
System.out.println("windowIconified");
}
@Override
public void windowOpened(WindowEvent e) //窗口打开了
{
System.out.println("windowOpened");
}
}
坦克大战v2.0_ MyTankGame2.java
/*
* 功能:坦克大战v2.0
* 1.画出坦克
* 2.我的坦克可以上下左右移动
*/
package com.test6;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class MyTankGame2 extends JFrame{
MyPanel mp=null;
//构造函数
public MyTankGame2()
{
mp=new MyPanel();
this.add(mp);
//注册监听
this.addKeyListener(mp);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
MyTankGame2 mtg=new MyTankGame2();
}
}
//我的面板
class MyPanel extends JPanel implements KeyListener
{
//定义一个我的坦克
Hero hero=null;
//定义敌人的坦克组
Vector<EnemyTank>ets=new Vector<EnemyTank>();
int enSize=3;
//构造函数
public MyPanel()
{
hero=new Hero(100,200);
//初始化敌人的坦克
for(int i=0;i<enSize;i++)
{
//创建一个辆敌人的坦克对象
EnemyTank et=new EnemyTank((i+1)*50,0);
et.setColor(0); //设置颜色
et.setDirect(2);
//加入
ets.add(et);
}
}
//重写paint函数
public void paint(Graphics g)
{
super.paint(g); //这句不能少
g.fillRect(0,0,400,300); //设置游戏面板背景
//画出自己的坦克
this.drawTank(hero.getX(), hero.getY(), g, this.hero.direct, 1);
//画出敌人的坦克
for(int i=0;i<ets.size();i++)
{
this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 0);
}
}
//画出坦克的函数(扩展)
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, 30, 5, false);
//画出下面的矩形
g.fill3DRect(x, y+15, 30, 5, false);
//画出中间的矩形
g.fill3DRect(x+5, y+5, 20, 10, false);
//画出圆形
g.fillOval(x+10, y+5, 10, 10);
//画出线
g.drawLine(x+15, y+10, x+30, y+10);
//画齿轮
g.setColor(Color.darkGray);
g.drawLine(x+2, y+1, x+2, y+4);
g.drawLine(x+5, y+1, x+5, y+4);
g.drawLine(x+8, y+1, x+8, y+4);
g.drawLine(x+11, y+1, x+11, y+4);
g.drawLine(x+14, y+1, x+14, y+4);
g.drawLine(x+17, y+1, x+17, y+4);
g.drawLine(x+20, y+1, x+20, y+4);
g.drawLine(x+23, y+1, x+23, y+4);
g.drawLine(x+26, y+1, x+26, y+4);
g.drawLine(x+2, y+16, x+2, y+19);
g.drawLine(x+5, y+16, x+5, y+19);
g.drawLine(x+8, y+16, x+8, y+19);
g.drawLine(x+11, y+16, x+11, y+19);
g.drawLine(x+14, y+16, x+14, y+19);
g.drawLine(x+17, y+16, x+17, y+19);
g.drawLine(x+20, y+16, x+20, y+19);
g.drawLine(x+23, y+16, x+23, y+19);
g.drawLine(x+26, y+16, x+27, y+19);
break;
case 1: //向左
//画出上面的矩形
g.fill3DRect(x, y, 30, 5, false);
//画出下面的矩形
g.fill3DRect(x, y+15, 30, 5, false);
//画出中间的矩形
g.fill3DRect(x+5, y+5, 20, 10, false);
//画出圆形
g.fillOval(x+10, y+5, 10, 10);
//画出线
g.drawLine(x+15, y+10, x, y+10);
//画齿轮
g.setColor(Color.darkGray);
g.drawLine(x+2, y+1, x+2, y+4);
g.drawLine(x+5, y+1, x+5, y+4);
g.drawLine(x+8, y+1, x+8, y+4);
g.drawLine(x+11, y+1, x+11, y+4);
g.drawLine(x+14, y+1, x+14, y+4);
g.drawLine(x+17, y+1, x+17, y+4);
g.drawLine(x+20, y+1, x+20, y+4);
g.drawLine(x+23, y+1, x+23, y+4);
g.drawLine(x+26, y+1, x+26, y+4);
g.drawLine(x+2, y+16, x+2, y+19);
g.drawLine(x+5, y+16, x+5, y+19);
g.drawLine(x+8, y+16, x+8, y+19);
g.drawLine(x+11, y+16, x+11, y+19);
g.drawLine(x+14, y+16, x+14, y+19);
g.drawLine(x+17, y+16, x+17, y+19);
g.drawLine(x+20, y+16, x+20, y+19);
g.drawLine(x+23, y+16, x+23, y+19);
g.drawLine(x+26, y+16, x+27, y+19);
break;
case 2: //向下
//画出我的坦克(到时封装成一个函数)
//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+5, y+10, 10, 10);
//5.画出线
g.drawLine(x+10, y+15, x+10, y+30);
//画齿轮
g.setColor(Color.darkGray);
g.drawLine(x+1, y+2, x+4, y+2);
g.drawLine(x+1, y+5, x+4, y+5);
g.drawLine(x+1, y+8, x+4, y+8);
g.drawLine(x+1, y+11, x+4, y+11);
g.drawLine(x+1, y+14, x+4, y+14);
g.drawLine(x+1, y+17, x+4, y+17);
g.drawLine(x+1, y+20, x+4, y+20);
g.drawLine(x+1, y+23, x+4, y+23);
g.drawLine(x+1, y+27, x+4, y+27);
g.drawLine(x+16, y+2, x+19, y+2);
g.drawLine(x+16, y+5, x+19, y+5);
g.drawLine(x+16, y+8, x+19, y+8);
g.drawLine(x+16, y+11, x+19, y+11);
g.drawLine(x+16, y+14, x+19, y+14);
g.drawLine(x+16, y+17, x+19, y+17);
g.drawLine(x+16, y+20, x+19, y+20);
g.drawLine(x+16, y+23, x+19, y+23);
g.drawLine(x+16, y+27, x+19, y+27);
break;
case 3: //向上
//画出我的坦克(到时封装成一个函数)
//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+5, y+10, 10, 10);
//5.画出线
g.drawLine(x+10, y+15, x+10, y);
//画齿轮
g.setColor(Color.darkGray);
g.drawLine(x+1, y+2, x+4, y+2);
g.drawLine(x+1, y+5, x+4, y+5);
g.drawLine(x+1, y+8, x+4, y+8);
g.drawLine(x+1, y+11, x+4, y+11);
g.drawLine(x+1, y+14, x+4, y+14);
g.drawLine(x+1, y+17, x+4, y+17);
g.drawLine(x+1, y+20, x+4, y+20);
g.drawLine(x+1, y+23, x+4, y+23);
g.drawLine(x+1, y+27, x+4, y+27);
g.drawLine(x+16, y+2, x+19, y+2);
g.drawLine(x+16, y+5, x+19, y+5);
g.drawLine(x+16, y+8, x+19, y+8);
g.drawLine(x+16, y+11, x+19, y+11);
g.drawLine(x+16, y+14, x+19, y+14);
g.drawLine(x+16, y+17, x+19, y+17);
g.drawLine(x+16, y+20, x+19, y+20);
g.drawLine(x+16, y+23, x+19, y+23);
g.drawLine(x+16, y+27, x+19, y+27);
break;
}
}
@Override
public void keyPressed(KeyEvent e) //键按下处理
{
//a表示向左,s表示向上,w表示向上,d表示向右
if(e.getKeyCode()==KeyEvent.VK_D)
{
this.hero.setDirect(0); //设置我的坦克的方向,向右
this.hero.moveRight();
}
else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.hero.setDirect(1); //向左
this.hero.moveLeft();
}
else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.hero.setDirect(2); //向下
this.hero.moveDown();
}
else if(e.getKeyCode()==KeyEvent.VK_W)
{
this.hero.setDirect(3); //向上
this.hero.moveUp();
}
//重绘Panel
this.repaint();
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void keyTyped(KeyEvent e)
{
}
}
坦克大战v2.0_ Members.java
package com.test6;
//坦克类
class Tank
{
//表示坦克的横坐标
int x=0;
//坦克纵坐标
int y=0;
//坦克方向
int direct=0; //0表示右,1表示左,2表示下,3表示上
//坦克的速度
int speed=1;
//坦克的颜色
int color;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
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 EnemyTank extends Tank
{
public EnemyTank(int x,int y)
{
super(x,y);
}
}
//我的坦克
class Hero extends Tank
{
public Hero(int x,int y)
{
super(x,y); //用父类的构造函数初始化子类的成员变量
}
//坦克向上移动
public void moveUp()
{
y-=speed;
}
//坦克向右移动
public void moveRight()
{
x+=speed;
}
//坦克向下移动
public void moveDown()
{
y+=speed;
}
//坦克向左移动
public void moveLeft()
{
x-=speed;
}
}