package redduke.game.j2me;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* 缓冲类
* @author redduke1202
*
*/
public class ReddukeBuffer {
private int width,height;
private Paintable p;
private Image buf;
private Graphics g;
//缓冲区左上角相对Layer的偏移
private int offx,offy;
//缓冲区当前偏移量
private int _offx,_offy;
//显示区相对缓冲区的偏移量
private int offx2,offy2;
/**
* 构造方法
* @param p 需要缓冲的绘制对象
* @param w 宽度
* @param h 高度
* @param w2 缓冲2边的宽度 缓冲宽度=宽度+w2*2
* @param h2 缓冲上下的高度 缓冲高度=高度+h2*2
*/
public ReddukeBuffer(Paintable p,int w,int h,int w2,int h2)
{
if(w2<0 || h2<0 || w<1 || h<1 || w+w2>p.getWidth() || h+h2>p.getHeight())
throw new IllegalArgumentException();
this.width=w;
this.height=h;
this.p=p;
buf=Image.createImage(w+w2,h+h2);
g=buf.getGraphics();
bufferImpl(0,0,buf.getWidth(),buf.getHeight());
}
/**
* 将此缓冲绘制到g
*/
public final void paint(Graphics g,int x,int y)
{
int tempx=offx%buf.getWidth();
int tempy=offy%buf.getHeight();
drawRegion(g,buf,tempx+offx2,tempy+offy2,buf.getWidth()-tempx-offx2,buf.getHeight()-tempy-offy2,0,x,y,0);
drawRegion(g,buf,0,tempy+offy2,width-(buf.getWidth()-tempx-offx2),buf.getHeight()-tempy-offy2,0,x+width,y,Graphics.RIGHT|Graphics.TOP);
drawRegion(g,buf,tempx+offx2,0,buf.getWidth()-tempx-offx2,height-(buf.getHeight()-tempy-offy2),0,x,y+height,Graphics.BOTTOM|Graphics.LEFT);
drawRegion(g,buf,0,0,width-(buf.getWidth()-tempx-offx2), height-(buf.getHeight()-tempy-offy2),0,x+width,y+height,Graphics.RIGHT|Graphics.BOTTOM);
}
private void drawRegion(Graphics g,Image img,int x_src,int y_src,int width,int height,int transform,int x_dest,int y_dest,int anchor)
{
if(width<=0 || height<=0) return;
if(width>this.width)
width=this.width;
if(height>this.height)
height=this.height;
g.drawRegion(img,x_src,y_src,width,height,transform,x_dest,y_dest,anchor);
}
/**
* 卷动绘制对象,根据需要进行缓冲
*/
public void scrollTo(int offx,int offy)
{
scroll(offx-this._offx,offy-this._offy);
}
/**
* 卷动绘制对象,根据需要进行缓冲
*/
public void scroll(int dx,int dy)
{
if(_offx+offx2+dx<0)
dx=-offx2-_offx;
else if(_offx+offx2+dx>p.getWidth()-width)
dx=p.getWidth()-width-offx2-_offx;
if(_offy+offy2+dy<0)
dy=-offy2-_offy;
else if(_offy+offy2+dy>p.getHeight()-height)
dy=p.getHeight()-height-offy2-_offy;
int w2=buf.getWidth()-width;
int h2=buf.getHeight()-height;
if(dx!=0)
{
int _offx2=offx2;
offx2+=dx;
if(offx2<0 || offx2>w2)
{
offx2=w2>>1;
dx=_offx2+dx-offx2;
if(_offx+dx<0)
{
offx2+=_offx+dx;
dx=-_offx;
}
else if(_offx+dx>p.getWidth()-buf.getWidth())
{
offx2-=p.getWidth()-buf.getWidth()-_offx-dx;
dx=p.getWidth()-buf.getWidth()-_offx;
}
}
else
dx=0;
}
if(dy!=0)
{
int _offy2=offy2;
offy2+=dy;
if(offy2<0 || offy2>h2)
{
offy2=h2>>1;
dy=_offy2+dy-offy2;
if(_offy+dy<0)
{
offy2+=_offy+dy;
dy=-_offy;
}
else if(_offy+dy>p.getHeight()-buf.getHeight())
{
offy2-=p.getHeight()-buf.getHeight()-_offy-dy;
dy=p.getHeight()-buf.getHeight()-_offy;
}
}
else
dy=0;
}
if(dx!=0 || dy!=0)
{
_offx+=dx;
_offy+=dy;
buffer();
}
}
private void bufferImpl(int x,int y,int w,int h)
{
int tempx=_offx%buf.getWidth();
int tempy=_offy%buf.getHeight();
// A
{
g.setClip(tempx,tempy,buf.getWidth()-tempx,buf.getHeight()-tempy);
p.paint(g,tempx-_offx,tempy-_offy);
}
// B
if(tempx>0)
{
g.setClip(0,tempy,tempx,buf.getHeight()-tempy);
p.paint(g,tempx-_offx-buf.getWidth(),tempy-_offy);
}
// C
if(tempy>0)
{
g.setClip(tempx,0,buf.getWidth()-tempx,tempy);
p.paint(g,tempx-_offx,tempy-_offy-buf.getHeight());
}
// D
if(tempx>0 && tempy>0)
{
g.setClip(0,0,tempx,tempy);
p.paint(g,tempx-_offx-buf.getWidth(),tempy-_offy-buf.getHeight());
}
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
private void buffer()
{
if(offx==_offx && offy==_offy) return;
if(Math.abs(offx-_offx)>=buf.getWidth() || Math.abs(offy-_offy)>=buf.getHeight())
bufferImpl(0,0,buf.getWidth(),buf.getHeight());
else
{
int minX=Math.min(_offx%buf.getWidth(),offx%buf.getWidth());
int maxX=Math.max(_offx%buf.getWidth(),offx%buf.getWidth());
int minY=Math.min(_offy%buf.getHeight(),offy%buf.getHeight());
int maxY=Math.max(_offy%buf.getHeight(),offy%buf.getHeight());
if(_offx/buf.getWidth()==offx/buf.getWidth() && _offy/buf.getHeight()==offy/buf.getHeight())
{
if(minX==maxX)
bufferImpl(0,minY,width,maxY-minY);
else if(minY==maxY)
bufferImpl(minX,0,maxX-minX,buf.getHeight());
else
{
bufferImpl(minX,0,maxX-minX,minY);
bufferImpl(0,minY,buf.getWidth(),maxY-minY);
bufferImpl(minX,maxY,maxX-minX,buf.getHeight()-maxY);
}
}
else if(_offx/buf.getWidth()==offx/buf.getWidth())
{
bufferImpl(0,0,buf.getWidth(),minY);
bufferImpl(minX,minY,maxX-minX,maxY-minY);
bufferImpl(0,maxY,buf.getWidth(),buf.getHeight()-maxY);
}
else if(_offy/buf.getHeight()==offy/buf.getHeight())
{
bufferImpl(0,0,minX,buf.getHeight());
bufferImpl(minX,minY,maxX-minX,maxY-minY);
bufferImpl(maxX,0,buf.getWidth()-maxX,buf.getHeight());
}
else
{
bufferImpl(0,0,buf.getWidth(),minY);
bufferImpl(0,minY,minX,maxY-minY);
bufferImpl(maxX,minY,buf.getWidth()-maxX,maxY-minY);
bufferImpl(0,maxY,buf.getWidth(),buf.getHeight()-maxY);
}
}
offx=_offx;
offy=_offy;
}
/**
* 返回偏移量横坐标
* @return
*/
public int getOffx()
{
return offx+offx2;
}
/**
* 返回偏移量纵坐标
* @return
*/
public int getOffy()
{
return offy+offy2;
}
//以下方法仅做测试用
/* 返回缓冲图片 */
public Image getBuffer()
{
return buf;
}
/* 返回缓冲区横坐标偏移量 */
public int getBufOffx()
{
return offx;
}
/* 返回缓冲区纵坐标偏移量 */
public int getBufOffy()
{
return offy;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
卡马克缓冲算法及演示代码.rar (7个子文件)
BufferExample.jar 19KB
src
redduke
game
j2me
Paintable.java 251B
ReddukeBuffer.java 6KB
example
PaintObject.java 586B
test.png 15KB
BufferExampleCanvas.java 2KB
BufferExampleMIDlet.java 357B
共 7 条
- 1
资源评论
- xinquan2011-11-08不错,但只是部分的,融合到项目中还要更多的。。。
- x31912013-09-12很不错,尤其是算法中对四个象限处理的核心思路解释的很到位
- wangjianwise2012-05-22很好。可以看下里面到底是怎么做的。可惜不能直接运行。
- h7674199052016-03-31只是部分的,
- dzxxgc2zyy2012-09-24说实话有点久了,不过依稀记得当时载的好几个资源都不是完整的。
wyb417
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功