📄 waverenderer.java
字号:
package org.gameeden.micro.effect;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* 水波渲染。
* @author Sol
* @since CLDC 1.1
*/
public final class WaveRenderer
{
private long time;
/**
* 绘制波动的图像。
* @param g 指定的Graphics上下文
* @param img 图像源
* @param inverted 是否颠倒图像
* @param power 波能(0.0-1.0)
* @param x_src 图像拷贝起始象素X坐标
* @param y_src 图像拷贝起始象素Y坐标
* @param width 图像拷贝宽
* @param height 图像拷贝高
* @param x_dest 锚点的象素X坐标
* @param y_dest 锚点的象素Y坐标
* @param anchor 图像锚点
* @throws IllegalArgumentException 如果参数错误则抛出此异常
*/
public void drawImage(Graphics g,Image img,boolean inverted,double power,int x_src,int y_src,int width,int height,int x_dest,int y_dest,int anchor) throws IllegalArgumentException
{
switch(anchor)
{
case Graphics.TOP|Graphics.LEFT:
case Graphics.TOP|Graphics.RIGHT:
case Graphics.TOP|Graphics.HCENTER:
case Graphics.BOTTOM|Graphics.LEFT:
case Graphics.BOTTOM|Graphics.RIGHT:
case Graphics.BOTTOM|Graphics.HCENTER:
case Graphics.VCENTER|Graphics.LEFT:
case Graphics.VCENTER|Graphics.RIGHT:
case Graphics.VCENTER|Graphics.HCENTER:
break;
default:
throw new IllegalArgumentException();
}
if(power<0||power>1)
{
throw new IllegalArgumentException();
}
int swing=height>>3;
int offsetX=-((anchor&Graphics.RIGHT)!=0?width:(anchor&Graphics.HCENTER)!=0?width>>1:0);
int offsetY=-((anchor&Graphics.BOTTOM)!=0?height:(anchor&Graphics.VCENTER)!=0?height>>1:0);
if(x_src<0||y_src<0||width<0||height<0||x_src+width>img.getWidth()||y_src+height>img.getHeight())
{
throw new IllegalArgumentException();
}
time++;
for(int n=0,offset;n<height;n++)
{
offset=(int)((swing*(n+20)*Math.sin((double)swing*(height-n)/(n+1)+time)/height)*power);
if(n+offset<0)
{
offset=-n;
}
else if(n+offset>=height)
{
offset=height-n-1;
}
g.setClip(x_dest+offsetX,y_dest+offsetY+n,width,1);
if(inverted)
{
g.drawImage(img,x_dest+offsetX-x_src,y_dest+offsetY-y_src-height+1+(n<<1)+offset,Graphics.LEFT|Graphics.TOP);
}
else
{
g.drawImage(img,x_dest+offsetX-x_src,y_dest+offsetY-y_src-offset,Graphics.LEFT|Graphics.TOP);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -