java学习笔记-窗口绘图(坦克大战)

坦克大战,金典小霸王游戏。


主框架的搭建,,刚开始使用的是awt下的类来创建窗口,但在图片刷新的时候会卡,不知道怎么解决,然后就换了swing.Jframe来进行创建.

AWT 是抽象窗口组件工具包,是 java 最早的用于编写图形节目应用程序的开发包,提供了一套与本地图形界面进行交互的接口,利用 AWT 来构件图形用户界面的时候,我们实际上是在利用操作系统所提供的图形库。
Swing 是为了解决 AWT 存在的问题而新开发的包,它是在 AWT 为基础上构建的一套新的图形界面系统,它提供了AWT 所能够提供的所有功能,并且用纯粹的Java代码对AWT 的功能进行了大幅度的扩充。
个人理解的话,就是swing比较灵活,在各个平台上都可以保持一个风格,而awt的话则是根据本地资源来创建,所以在不同平台上,风格可能会不同,另外就是运行速度,awt运行速度更快,他是基于C/C++的程序。
目前完成的有我方坦克,敌方坦克,老家,自动路径,开火,子弹碰撞,坦克击杀,,未完成的地形隔离,老家销毁等。

package com.javaTank;



import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.ArrayList;
import java.util.List;
   

public class TankWindow {
	
	
	public static final int window_Width=1000;
	public static final int window_Height=500;
	
	private int dc_H=window_Width/2-100,dc_V=window_Height-90;
	private TimeThred ds;
	private Direction dt=Direction.STOP;
	
	private Win ck=null;
	public Tankkk tank_H=new Tankkk(dc_H, dc_V, dt,true);;
	
    static List bullet = new ArrayList();
	static List tanks=new ArrayList();              //对象容器
	static List hometank=new ArrayList();
	
	public TankWindow(){
		
		ck=new Win();
	   
		ck.setVisible(true);                            //窗口显示

		ds=new TimeThred(50);
		 
	
	}
	/**
	 窗口搭建
	 * */
	class Win extends JFrame
	{
		Mbar cd=null;     
		PaintWin jp=null;                         
		WinClose wc=null;                      
		KeyWalk kw=null;                       
		public Win(){
			cd=new Mbar();                       //菜单
			this.setJMenuBar(cd);
			
			this.setLocation(280,50);             //显示位置
			this.setSize(window_Width,window_Height);//窗口大小
			this.setBackground(new Color(160,220,220));//背景色
			
			this.setTitle("坦克大战");	              //窗口标题		
			this.setResizable(false);             //窗口大小不可改
			
			jp=new PaintWin();                    //面板(可绘图 )
			this.add(jp);
			
			wc=new WinClose();                     //窗口关闭监听
			this.addWindowListener(wc);
			
			kw=new KeyWalk();                       //键盘监听
			this.addKeyListener(kw);
			
			this.repaint();
			

		}
		class Mbar extends JMenuBar                //菜单
		{
			                   
			JMenu bar1,bar2,bar3;                //选项
			JMenuItem bat1,bat2,bat3;            //选项中的子按钮
			LisWin ckj=null;                    //按钮监听
			
			public Mbar(){
					bar1=new JMenu("Start");
					bar2=new JMenu("Exit");
					bar3=new JMenu("sd");
					bar1.setFont(new Font("TimesRoman", Font.BOLD, 15));
					bar2.setFont(new Font("TimesRoman", Font.BOLD, 15));
					bar3.setFont(new Font("TimesRoman", Font.BOLD, 15));

					bat1=new JMenuItem("Start");
					bat2=new JMenuItem("Exit");
					bat3=new JMenuItem("sadf");
			        bat1.setFont(new Font("TimesRoman",Font.BOLD,15));
			        bat2.setFont(new Font("TimesRoman",Font.BOLD,15));
			        bat3.setFont(new Font("TimesRoman",Font.BOLD,15));
			        
			        bar1.add(bat1);
			        bar2.add(bat2);
			        bar3.add(bat3);
			        
			        this.add(bar1);
			        this.add(bar2);
			        this.add(bar3);
			        
					ckj=new LisWin();
					
				
			        
			        bat1.addActionListener(ckj);              //添加按钮监听
					bat1.setActionCommand("Start");
					bat2.addActionListener(ckj); 
					bat2.setActionCommand("Exit");
					bat3.addActionListener(ckj);
					bat3.setActionCommand("fuck");

                     for(int i=0;i<10;i++){
			    		
			    		tanks.add(new Tankkk(100*i,20*i,dt,false,TankWindow.this));	    		
			    	}
                     hometank.add(tank_H);
			}
			
			class LisWin implements ActionListener{
				public void actionPerformed(ActionEvent e)
				{
					
					if(e.getActionCommand().equals("Start")){
//						Object[] options = { "确定", "取消" };
//						int response = JOptionPane.showOptionDialog(this, "您确认要开始新游戏!", "",
//								JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,
//								options, options[0]);
//					}else if(e.getActionCommand().equals("Exit")){
//						Object[] options = { "确定", "取消", "qwe"};
//						Icon icon=new ImageIcon( "com.javaTank.Images/9.gif ");
//						int response = JOptionPane.showOptionDialog(this, "您确认退出游戏!", "提示框名",
//								JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, icon,
//								options, options[2]);
//						if (response == 1) {
//							System.out.println("退出");
//							System.exit(0);
//							}
					}
				}
				
			}
	     }
		/**
		 窗口监听 
		 */
		class WinClose extends WindowAdapter                
		{
			public void windowClosing(WindowEvent e)
			{
//				ds1.xc.stop();
				System.exit(0);
			}
		}

		/**键盘监听
		 */
		class  KeyWalk implements KeyListener  
		{
			
			public void keyPressed(KeyEvent e){
				tank_H.keyPressed(e);
//				
			}  
			public void keyReleased(KeyEvent e)
			{
				tank_H.keyReleased(e);
			}

			public void keyTyped(KeyEvent e)
			{
			}
//		
		}
    
		
		/**面板
		 */
		class PaintWin extends JPanel{
			
//			class DrawCanvas extends Canvas{ //新建画布类 
		    @Override//重写方法 
		        public void paint(Graphics g){ 
//		            g.setFont(new Font("楷体",Font.PLAIN,20));//设置字体 
//		            g.setColor(Color.RED);//设置颜色 
//		            g.drawString("绘图测试",20,20);//画出字符串 
//		            g.drawLine(20,30,60,30);//画出一条直线 
//		            g.setColor(Color.BLUE);//设置颜色 
//		            g.draw3DRect(20,40,50,30,false);//画出一矩形 
//		            g.fillArc(20,80,65,65,0,90);//画出一扇形 
//		            g.setFont(new Font("华文彩云",Font.PLAIN,30));//设置字体 
//		            g.setColor(Color.PINK);//设置颜色 
//		            g.drawString("彩云之南",40,150);//画出字符串
//		  
		    	if(tanks.size()==0){
		    		Font f=g.getFont();
		    		g.setFont(new Font("TimesRoman",Font.BOLD,60));
		    		g.drawString("YOU WIN", 300, 200);
		    		g.setFont(f);
		    	}
		    	
		    	if(hometank.size()==0){
		    		Font f=g.getFont();
		    		g.setFont(new Font("TimesRoman",Font.BOLD,60));
		    		g.drawString("YOU LOSE", 300, 200);
		    		g.setFont(f);
		    	}
		    	for(int i=0;i


 
  

子弹类

package com.javaTank;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

import java.awt.Image;


public class Bullet  {
	private int x=0;
	private int y=0;
	private Direction bdt;
	private Image zd;
	TankWindow tw;
	boolean good=true;
	boolean firend;
	
	public Bullet(int x,int y,Direction bdt ,boolean f,TankWindow tw){
		this.x=x+13;
		this.y=y+13;
		this.bdt=bdt;
		this.firend=f;
		this.tw=tw;
		
	}
	public void draw(Graphics g){
		if(!good){
			tw.bullet.remove(this);
		}
		switch(bdt){
		case U:
			 zd=(new ImageIcon("src/Images/bulletU.gif")).getImage();
			break;
		case D:
			 zd=(new ImageIcon("src/Images/bulletD.gif")).getImage();

			break;
		case R:			
			 zd=(new ImageIcon("src/Images/bulletR.gif")).getImage();

			break;
		case L:
			 zd=(new ImageIcon("src/Images/bulletL.gif")).getImage();

			break;		
		}
		g.drawImage(zd, x, y, null);
		switch(bdt){
		case U:
			y-=10;
			break;
		case D:
			y+=10;
			break;
		case L:
			x-=10;
			break;
		case R:
			x+=10;
			break;
		}
		if(x<0||x>1000||y<0||y>440){
			tw.bullet.remove(this);
			
		}
		
	}

	public Rectangle getRect(){
		return new Rectangle(x,y,10,10);
	}
	public void tankhit(List ts){
		for(int i=0;i h){
		for(int i=0;i b){
		for(int i=0;i

 坦克类 
  

package com.javaTank;


import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import java.util.Random;

public class Tankkk {
	private Direction tdt=Direction.STOP;
	Direction tand=Direction.U;
	private int x;
	private int y;
	private Image tank;
	private boolean firend =true;
	private Random r=new Random();
	private int step=r.nextInt(10)+5;
	private boolean live=true;
	TankWindow tw;
	private int oldx;
	private int oldy;
	
	
	
	public Tankkk(int x,int y, Direction tdt,boolean firend){
		this.x=x;
		this.y=y;
		this.tdt=tdt;
		this.firend=firend;
		
	}
	public Tankkk(int x,int y , Direction tdt,boolean firend,TankWindow tw){
		this(x,y,tdt,firend);
		this.tw=tw;
	
		
	}
	public void draw(Graphics g){
		if(!live){
			
			tw.tanks.remove(this);
			if(firend){
			
			tw.hometank.remove(this);
			System.out.println(tw.hometank.size());}
		}
	
		
		switch(tand){
		case U:
	    	 tank=(new ImageIcon("src/Images/tankU.gif")).getImage();
			break;
		case D:	    
			tank=(new ImageIcon("src/Images/tankD.gif")).getImage();
			break;
		case R:	    
			tank=(new ImageIcon("src/Images/tankR.gif")).getImage();
			break;
		case L:	    
			tank=(new ImageIcon("src/Images/tankL.gif")).getImage();
			break;
		
		}
		g.drawImage(tank, x, y, null);
		if(firend==false){
			if(step==0){
				Direction[] dc=Direction.values();
				step = r.nextInt(12) + 3;  //产生随机路径
				int rn = r.nextInt(dc.length);
				tdt = dc[rn];      //产生随机方向
			
			}
			step--;
			if (r.nextInt(40) > 38)//产生随机数,控制敌人开火
				this.fire(false);
		}
		move();
	
	}
	public void fire(boolean f){
		Bullet z =new Bullet(x,y,tand,f,this.tw);
		tw.bullet.add(z);
		
	}
	public void move(){
		this.oldx=x;
		this.oldy=y;
		switch(tdt){
		case U:
			y-=6;
			break;
		case D:
			y+=6;
			break;
		case L:
			x-=6;
			break;
		case R:
			x+=6;
			break;
		}
		if(tdt!=Direction.STOP){
			tand=tdt;
		}
		if(x>960||x<0||y<0||y>410){
			x=x<0?0:x;
			x=x>960?960:x;
			y=y<0?0:y;
			y=y>410?410:y;
		}
		
	}
	
	public void setLive(boolean l){
		this.live=l;
	}
	public boolean isFirend(){
		return firend;
	}
	public Rectangle getRect(){
		return new Rectangle(x,y,35,35);
	}
	
    public void changDic(){    	
	    x=oldx;
	    y=oldy;	
    }
	
	public void tankWithtank(List tank){
		for(int i=0;i

 
  

方向枚举

package com.javaTank;

public enum Direction {  
	L, U,  R,  D,  STOP
}



你可能感兴趣的:(java)