Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created July 6, 2009 04:38
Show Gist options
  • Save mrkn/141266 to your computer and use it in GitHub Desktop.
Save mrkn/141266 to your computer and use it in GitHub Desktop.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
// Affine
public class Affine4 extends MIDlet
{
public Affine4()
{
Affine4Canvas c = new Affine4Canvas();
Display.getDisplay(this).setCurrent(c);
}
protected void startApp() {}
protected void pauseApp() {}
protected void destroyApp(boolean flag) {}
}
class Affine4Canvas extends Canvas
{
public static final int BLACK = 0x00000000;
public static final int YELLOW = 0x00FFFF00;
public static final int WHITE = 0x00FFFFFF;
private double[] m;
public Affine3Canvas()
{
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 = {
this.m[0], this.m[1], this.m[2],
this.m[3], this.m[4], this.m[5] };
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 = {
sx, 0.0, 0.0,
0.0, sy, 0.0 };
combine(mm);
}
// 原点を中心とする回転
//
// @param degrees the angle of rotation in degrees
public void rotate(double degrees)
{
double radians = deg2rad(degrees);
double c = Math.cos(radians);
double s = Math.sin(radians);
double[] mm = {
c, -s, 0.0,
s, c, 0.0 };
combine(mm);
}
private double deg2rad(double degrees)
{
return Math.PI * degrees / 180.0;
}
// 平行移動
//
// @param tx the amount of translation along the X-angle
// @param ty the amount of translation along the Y-angle
public void translate(double tx, double ty)
{
double[] mm = {
1.0, 0.0, tx,
0.0, 1.0, ty };
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];
}
public double reverseTransformX(double xx, double yy)
{
double d = this.m[0]*this.m[4] - this.m[1]*this.m[3];
return (this.m[4]*xx - this.m[1]*yy
+ (this.m[1]*this.m[5] - this.m[2]*this.m[4]))/d;
}
public double reverseTransformY(double xx, double yy)
{
double d = this.m[0]*this.m[4] - this.m[1]*this.m[3];
return (-this.m[3]*xx + this.m[0]*yy
+ (this.m[2]*this.m[3] - this.m[0]*this.m[5]))/d;
}
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");
// (0) 画像サイズを画面に合わせる
//double sx = pixels.getWidth() / (double)sample_pixels.getWidth();
//double sy = pixels.getHeight() / (double)sample_pixels.getHeight();
//scale(sx, sy);
// (1) 画面の中心を原点に移動
translate(-sample_pixels.getWidth() / 2.0,
-sample_pixels.getHeight() / 2.0);
// (2) 原点を中心に45度回転
rotate(25.0);
// (3) 原点を適切に移動する
translate(sample_pixels.getWidth() / 2.0,
sample_pixels.getHeight() / 2.0);
// (4) 縦横を 1.05 倍する。
// scale(0.5, 0.5);
// 画面全体に対して描く
for (int yy = 0; yy < pixels.getHeight(); ++yy) {
for (int xx = 0; xx < pixels.getWidth(); ++xx) {
// 画面の座標 (xx, yy) から画像の座標 (x, y) を求める
int x = (int)reverseTransformX(xx, yy);
int y = (int)reverseTransformY(xx, yy);
if (0 <= x && x < sample_pixels.getWidth() &&
0 <= y && y < sample_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