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){
viewer.repaint();
}
drawBox(getGraphics());
return false;
}
Box b=box.copy();
b.setY(b.getY()+1);
if(!canAdd(b)){
setNoMove(box);
Box next=box.willNext();
next.setPoint(startPoint.x,startPoint.y-box.getBoxHeight());
if(!canAdd(next)){//Game Over
if(listener!=null)
listener.doGameOver(new BoxEvent(box.copy(),0));
clearBoxs(getGraphics());
init();
return false;
}
box.next();
box.setPoint(startPoint
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
BoxGame_2.0.zip (90个子文件)
BoxGame 2.0
src
com
bokee
nicend
boxgame
gui
GamePanel.java 14KB
game
BoxEvent.java 266B
Box.java 6KB
BoxEventListener.java 412B
main
MyRobot.java 3KB
Robot.java 3KB
Main.java 5KB
robot
Step.java 3KB
AbstractGamePanel.java 5KB
bin
com
bokee
nicend
boxgame
gui
GamePanel.class 12KB
game
Box.class 5KB
BoxEvent.class 673B
BoxEventListener.class 249B
main
Robot.class 3KB
Main.class 5KB
Main$2.class 1KB
MyRobot.class 3KB
Main$3.class 1KB
Robot$1.class 1KB
Main$5.class 1KB
Main$4.class 985B
Main$1.class 943B
robot
Step.class 3KB
AbstractGamePanel.class 4KB
.classpath 232B
BoxGame 2.0_fat.jar 25KB
doc
overview-frame.html 1KB
constant-values.html 5KB
com
bokee
nicend
boxgame
gui
package-tree.html 7KB
package-frame.html 931B
package-summary.html 6KB
class-use
GamePanel.html 10KB
package-use.html 8KB
GamePanel.html 42KB
game
package-tree.html 6KB
BoxEvent.html 11KB
package-frame.html 1KB
package-summary.html 7KB
class-use
BoxEvent.html 12KB
BoxEventListener.html 10KB
Box.html 17KB
package-use.html 10KB
BoxEventListener.html 10KB
Box.html 19KB
main
package-tree.html 7KB
package-frame.html 1KB
package-summary.html 7KB
MyRobot.html 13KB
class-use
MyRobot.html 6KB
Robot.html 8KB
Main.html 6KB
Robot.html 17KB
package-use.html 7KB
Main.html 14KB
robot
package-tree.html 6KB
package-frame.html 1KB
AbstractGamePanel.html 16KB
package-summary.html 6KB
class-use
AbstractGamePanel.html 6KB
Step.html 8KB
package-use.html 7KB
Step.html 18KB
deprecated-list.html 5KB
resources
inherit.gif 57B
overview-tree.html 8KB
package-list 124B
index-files
index-12.html 15KB
index-13.html 6KB
index-11.html 7KB
index-9.html 6KB
index-5.html 16KB
index-8.html 9KB
index-1.html 7KB
index-2.html 7KB
index-4.html 9KB
index-10.html 6KB
index-3.html 8KB
index-14.html 6KB
index-6.html 7KB
index-7.html 7KB
allclasses-noframe.html 2KB
help-doc.html 8KB
index.html 1KB
overview-summary.html 6KB
allclasses-frame.html 2KB
serialized-form.html 8KB
stylesheet.css 1KB
.settings
org.eclipse.jdt.ui.prefs 99B
.fatjar 360B
.project 387B
共 90 条
- 1
seyoudong
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页