⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamepanel.java

📁 俄罗斯方块,机器人,华容道破解,文件格式(PE,BMP),邮箱硬盘,日历图片
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.bokee.nicend.boxgame.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;

import javax.swing.Timer;

import javax.swing.JPanel;

import com.bokee.nicend.boxgame.game.Box;
import com.bokee.nicend.boxgame.game.BoxEvent;
import com.bokee.nicend.boxgame.game.BoxEventListener;

/**
 * 游戏面板
 * @author Butnet
 */
public class GamePanel extends JPanel implements java.awt.event.KeyListener,java.awt.event.ActionListener{
	private Color lineColor;//线条颜色
	private Color boxColor;//方块颜色
	private Point point;//左上角起点  //  @jve:decl-index=0:
	private int boxWidth=-1;//方块像素宽度
	private int boxHeight=-1;//方块像素高度
	private int boxRow=-1;//方块行数
	private int boxCol=-1;//方块列数
	private boolean space[][]=null;//方块是否为空
	private static final long serialVersionUID = 1L;
	private int gameLine=0;//消去方块行数
	private Box box;//方块系统  
	private Point startPoint;//方块初始起点
	private boolean view=false;//是否为预览面板标记
	private GamePanel viewer;//预览面板
	private BoxEventListener listener;//事件监听
	private Timer timer;//定时器
	private int delay=-1;//定时时间间隔
	
	/**
	 * 设置下降速度,deplay毫秒下降一个高度
	 * @param delay 毫秒
	 */
	public void setDelay(int delay){
		this.delay=delay;
		if(timer!=null)
			timer.setDelay(delay);
	}
	/**
	 * @return 取得下降速度
	 */
	public int getDelay(){return delay;}
	/**
	 * 设置方块事件监听器
	 * @param listener
	 */
	public void setBoxListener(BoxEventListener listener){
		this.listener=listener;
	}
	/**
	 * @param box 设置当前方块
	 */
	public void setBox(Box box){this.box=box;}
	/**
	 * 设置下一个方块的预览面板
	 * @param viewer
	 */
	public void setViewer(GamePanel viewer){
		this.viewer=viewer;
		viewer.setBox(box);
	}
	/**
	 * 设置是当前面板是否是预览面板
	 * @param view 
	 */
	public void setView(boolean view){this.view=view;}
	/**
	 * @return 判断是否是预览面板
	 */
	public boolean isView(){return view;}
	/**
	 * @param point 设置方块起点
	 */
	public void setBoxStartPoint(Point point){
		this.startPoint=point;
	}
	/**
	 * 设置方块起点
	 * @param x X轴位置
	 * @param y Y轴位置
	 */
	public void setBoxStartPoint(int x,int y){
		startPoint=new Point(x,y);
	}
	/**
	 * This is the default constructor
	 */
	public GamePanel() {
		super();
		this.setDoubleBuffered(true);
		initialize();
	}
	/**
	 * @return 取得方块的颜色
	 */
	public Color getBoxColor(){
		return boxColor;
	}
	/**
	 * @param color 设置方块颜色
	 */
	public void setBoxColor(Color color){
		boxColor=color;
	}
	/**
	 * @return 取得线条颜色
	 */
	public Color getLineColor(){
		return lineColor;
	}
	/**
	 * @param color 设置线条颜色
	 */
	public void setLineColor(Color color){
		lineColor=color;
	}
	/**
	 * 初始化没有设置的属性
	 */
	public void init(){
		//当数据未设置时,以默认数据 进行设置
		if(boxWidth==-1)boxWidth=30;
		if(boxHeight==-1)boxHeight=30;
		if(boxRow==-1)boxRow=15;
		if(boxCol==-1)boxCol=10;
		if(delay==-1)delay=800;
		if(lineColor==null)lineColor=Color.BLACK;
		if(boxColor==null)boxColor=Color.BLUE;
		//初始化游戏方块
		space=new boolean[boxRow][boxCol];
		for(int i=0;i<boxRow;i++)
			for(int j=0;j<boxCol;j++)
				space[i][j]=true;//全为空
		//初始化大小
		if((getWidth()-boxWidth*boxCol)<0){this.setSize(boxWidth*(boxCol+1),getHeight());}
		if((getHeight()-boxHeight*boxRow)<0){this.setSize(getWidth(),boxHeight*(boxRow+2));}
		setPreferredSize(new Dimension(getWidth(),getHeight()));
		//计算左上角的点
		point=new Point((getWidth()-boxWidth*boxCol)/2,(getHeight()-boxHeight*boxRow)/2);
		//初始化移动方块
		box=Box.getBox();
		if(startPoint==null)
			startPoint=new Point(boxCol/2,0);
		box.setPoint(startPoint);
		if(listener!=null)
			listener.doNewBox(new BoxEvent(box.copy(),0));
		if(viewer!=null){
			viewer.setBox(box);
			viewer.repaint();
		}
		if(timer==null)timer=new Timer(delay,this);
	}
	//
	/**
	 * 判断方块b区域可否使用 
	 * @param b 要判断的方块
	 * @return 如果可用返回true,否则返回false
	 */
	private boolean canAdd(Box b){
		if(b.getBoxWidth()+b.getX()>boxCol)return false;
		if(b.getX()<0)return false;
		if(b.getY()+b.getBoxHeight()>boxRow)return false;
		int[][] d=b.getPoints();
		for(int i=0;i<d.length;i++){
			if(b.getY()+d[i][1]<0||b.getX()+d[i][0]<0)continue;
			if(!space[b.getY()+d[i][1]][b.getX()+d[i][0]])return false;
		}
		return true;
	}
	/**
	 * @return 取得游戏中消去的行数
	 */
	public int getGameLine(){
		return gameLine;
	}
	/**
	 * 处理向上变形
	 */
	private void actionUP(){
		Box b=box.getWillChang();
		if(!canAdd(b))return;
		clearBox(getGraphics());
		box.chang();
		drawBox(getGraphics());
	}
	/**
	 * 处理向左
	 */
	private void actionLEFT(){
		Box b=box.copy();
		b.setX(b.getX()-1);
		if(!canAdd(b))return;
		clearBox(getGraphics());
		box.setX(box.getX()-1);
		drawBox(getGraphics());
	}
	/**
	 * 处理向右
	 */
	private void actionRIGHT(){
		Box b=box.copy();
		b.setX(b.getX()+1);
		if(!canAdd(b))return;
		clearBox(getGraphics());
		box.setX(box.getX()+1);
		drawBox(getGraphics());
	}
	/**
	 * 检测是否有行可被消去.如果有则消去
	 * @param b
	 */
	private void disponeLine(Box b){
		int[][] d=b.getPoints();
		int[] lines=new int[d.length];
		for(int i=0;i<lines.length;i++){
			lines[i]=d[i][1];
			for(int j=0;j<i;j++){
				if(lines[i]==lines[j])lines[i]=-1;
				else if(lines[j]<lines[i]){
					int t=lines[j];
					lines[j]=lines[i];
					lines[i]=t;
				}
			}
		}
		int point=b.getY();//原来方块的位置
		int cutLines=0;
		//boolean clear=false;
		for(int i=0;i<lines.length;i++){
			if(lines[i]==-1)break;
			if(!lineOver(point+lines[i]))continue;
			//if(!clear)
				clearBoxs(getGraphics());
			//if(!clear)
				//clear=true;
			for(int k=point+lines[i]-1;k>0;k--)
				System.arraycopy(space[k],0,space[k+1],0,space[0].length);
			for(int tempi=0;tempi<space[0].length;tempi++)
				space[0][tempi]=true;
			gameLine++;
			cutLines++;
			point++;
			box.setY(point);
		}
		if(cutLines!=0)
			if(listener!=null)
				listener.doDispone(new BoxEvent(box.copy(),cutLines));
		drawBoxs(getGraphics());
	}
	/**
	 * 判断第line行 是否全满
	 * @param line 行索引
	 * @return 全满返回true,否则返回false
	 */
	private boolean lineOver(int line){
		for(int i=0;i<boxCol;i++){
			if(line<0)return false;
			if(space[line][i])return false;
		}
		return true;
	}
	/**
	 * @param b 将方块b设置为不可移动
	 */
	private void setNoMove(Box b){
		if(b.getY()+b.getBoxHeight()>boxRow)return;
		int[][] d=b.getPoints();
		for(int i=0;i<d.length;i++){
			if(b.getY()+d[i][1]<0||b.getX()+d[i][0]<0)continue;
			space[b.getY()+d[i][1]][b.getX()+d[i][0]]=false;
		}
		disponeLine(b);
	}
	/**
	 * @return 处理向下移动
	 */
	private boolean actionDOWN(){
		if(box.getY()+box.getBoxHeight()==boxRow){
			setNoMove(box);
			Box b=box.willNext();
			b.setPoint(startPoint.x,startPoint.y-b.getBoxHeight());
			if(!canAdd(b)){//Game Over
				if(listener!=null)
					listener.doGameOver(new BoxEvent(box.copy(),0));
				clearBoxs(getGraphics());
				init();
				return false;
			}
			box.next();
			box.setPoint(startPoint.x,startPoint.y-box.getBoxHeight());
			if(listener!=null)
				listener.doNewBox(new BoxEvent(box.copy(),0));
			if(viewer!=null){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -