📄 effectimage.java
字号:
import java.awt.*;import java.awt.image.*;//采用像素的方式,提高图像的处理速度public class EffectImage { public final static int RENDERING_FAST = 0; public final static int RENDERING_SMOOTH = 1; GameApplet applet; int[] imageData; int imgWidth, imgHeight; private EffectImage() {} public EffectImage(GameApplet applet, String sourceImage) { this(applet, sourceImage, null); } public EffectImage(GameApplet applet, String sourceImage, String alphaImage) { this.applet = applet; Image img = applet.getGameMedia().loadImage(sourceImage); imgWidth = img.getWidth(applet); imgHeight = img.getHeight(applet); imageData = new int[imgWidth * imgHeight]; //从指定的范围之中得到相应的像素数据,并保存到一个数组之中 PixelGrabber pg = new PixelGrabber(img, 0, 0, imgWidth, imgHeight, imageData, 0, imgWidth); try { pg.grabPixels(); } catch(Exception e) { e.printStackTrace(); } if (alphaImage != null) { Image alpha = applet.getGameMedia().loadImage(alphaImage); int[] alphaData = new int[imgWidth * imgHeight]; // Alpha is supposed to have same width & height pg = new PixelGrabber(alpha, 0, 0, imgWidth, imgHeight, alphaData, 0, imgWidth); try { pg.grabPixels(); } catch(Exception e) { e.printStackTrace(); } //一个像素由32位来表示,24-32表示透明度,16-234表示红色,8-15表示绿色,0-7表示蓝色 for (int i=0 ; i<imageData.length ; i++) { imageData[i] = (imageData[i] & 0xFFFFFF) | ((alphaData[i] & 0xFF0000) << 8); } } } public Image getRotate(double theta, double x, double y) { return getFastRotate(theta, x, y); } public Image getRotate(double theta, double x, double y, int rendering) { if (rendering == EffectImage.RENDERING_SMOOTH) { //return } return getFastRotate(theta, x, y); } private Image getFastRotate(double theta, double x, double y) { int[] output = new int[imageData.length]; // Reusable values double cosTheta = Math.cos(-theta); double sinTheta = Math.sin(-theta); double deltaX = x * (1. - cosTheta) + y * sinTheta; double deltaY = y * (1. - sinTheta) - y * cosTheta; // Calculate for (int j=0 ; j<imgHeight ; j++) { for (int i=0 ; i<imgWidth ; i++) { // For each output point // Find its origin int orX = (int)Math.round(i * cosTheta - j * sinTheta + deltaX); int orY = (int)Math.round(i * sinTheta + j * cosTheta + deltaY); if (orX < 0 || orX >= imgWidth || orY < 0 || orY >= imgHeight) { // out of scope output[i + j * imgWidth] = 0; } else { // Compute value output[i + j * imgWidth] = imageData[orX + orY * imgWidth]; } } } return applet.createImage(new MemoryImageSource(imgWidth, imgHeight, output, 0, imgWidth)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -