Created
June 29, 2009 04:58
-
-
Save mrkn/137470 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.microedition.midlet.*; | |
import javax.microedition.lcdui.*; | |
import java.io.IOException; | |
// Affine | |
public class Affine2 extends MIDlet | |
{ | |
public Affine2() | |
{ | |
Affine2Canvas c = new Affine2Canvas(); | |
Display.getDisplay(this).setCurrent(c); | |
} | |
protected void startApp() {} | |
protected void pauseApp() {} | |
protected void destroyApp(boolean flag) {} | |
} | |
class Affine2Canvas extends Canvas | |
{ | |
public static final int BLACK = 0x00000000; | |
public static final int YELLOW = 0x00FFFF00; | |
public static final int WHITE = 0x00FFFFFF; | |
private double[] m; | |
public Affine2Canvas() | |
{ | |
super(); | |
this.m = new double[6]; | |
loadIdentity(); // 変換の初期状態は恒等変換 | |
} | |
// 恒等変換 (何もしない変換) を設定する | |
public void loadIdentity() | |
{ | |
this.m[0] = 1.0; this.m[1] = 0.0; this.m[2] = 0.0; | |
this.m[3] = 0.0; this.m[4] = 1.0; this.m[5] = 0.0; | |
} | |
// 変換を合成する | |
public void combine(double[] mm) | |
{ | |
double[] old = new double[6]; | |
for (int i = 0; i < 6; ++i) old[i] = this.m[i]; | |
this.m[0] = mm[0]*old[0] + mm[1]*old[3]; | |
this.m[1] = mm[0]*old[1] + mm[1]*old[4]; | |
this.m[2] = mm[0]*old[2] + mm[1]*old[5] + mm[2]; | |
this.m[3] = mm[3]*old[0] + mm[4]*old[3]; | |
this.m[4] = mm[3]*old[1] + mm[4]*old[4]; | |
this.m[5] = mm[3]*old[2] + mm[4]*old[5] + mm[5]; | |
} | |
// 拡大縮小 (スケーリング) | |
// | |
// @param sx magnification of the X direction (X 方向の倍率) | |
// @param sy magnification of the Y direction (Y 方向の倍率) | |
public void scale(double sx, double sy) | |
{ | |
double[] mm = new double[6]; | |
mm[0] = sx; mm[1] = 0.0; mm[2] = 0.0; | |
mm[3] = 0.0; mm[4] = sy; mm[5] = 0.0; | |
combine(mm); | |
} | |
public double transformX(double x, double y) | |
{ | |
return this.m[0]*x + this.m[1]*y + this.m[2]; | |
} | |
public double transformY(double x, double y) | |
{ | |
return this.m[3]*x + this.m[4]*y + this.m[5]; | |
} | |
protected void paint(Graphics g) | |
{ | |
// setup a pixel data | |
PixelData pixels = new PixelData(getWidth(), getHeight()); // <*> | |
pixels.fillRect(0, 0, getWidth(), getHeight(), WHITE); // <*> | |
// load image file | |
try { | |
PixelData sample_pixels = PixelData.createPixelData("/nekobean.png"); | |
double sx = pixels.getWidth() / (double)sample_pixels.getWidth(); | |
double sy = pixels.getHeight() / (double)sample_pixels.getHeight(); | |
scale(sx, sy); | |
// 画像の表示できる範囲だけを画面に描く | |
for (int y = 0; y < sample_pixels.getHeight(); ++y) { | |
for (int x = 0; x < sample_pixels.getWidth(); ++x) { | |
// 画像の座標 (x, y) から画面の座標 (xx, yy) を求める | |
int xx = (int)transformX(x, y); | |
int yy = (int)transformY(x, y); | |
if (0 <= xx && xx < pixels.getWidth() && | |
0 <= yy && yy < pixels.getHeight()) { | |
// 描く | |
int v = sample_pixels.getPixel(x, y); | |
pixels.setPixel(xx, yy, v); | |
} | |
} | |
} | |
} | |
catch (IOException e) { | |
} | |
// fill the screen black | |
g.setColor(BLACK); | |
g.fillRect(0, 0, getWidth(), getHeight()); | |
// create image from pixel data | |
Image img = pixels.createRGBImage(); // <*> | |
// draw image | |
g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment