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

📄 reddukebuffer.java

📁 java卡马克屏幕缓冲算法演示
💻 JAVA
字号:
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(_offx/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;
	}
}

⌨️ 快捷键说明

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