Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created June 22, 2009 04:53
Show Gist options
  • Save mrkn/133807 to your computer and use it in GitHub Desktop.
Save mrkn/133807 to your computer and use it in GitHub Desktop.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
// Affine
public class Affine1 extends MIDlet
{
public Affine1()
{
Affine1Canvas c = new Affine1Canvas();
Display.getDisplay(this).setCurrent(c);
}
protected void startApp() {}
protected void pauseApp() {}
protected void destroyApp(boolean flag) {}
}
class Affine1Canvas extends Canvas
{
public static final int BLACK = 0x00000000;
public static final int YELLOW = 0x00FFFF00;
public static final int WHITE = 0x00FFFFFF;
// fill the rectangle area in the pixel data.
//
// @param pixels the pixel data
// @param width width of the pixel data
// @param height height of the pixel data
// @param x x-coordinate of top-left corner of the filling rectangle
// @param y y-coordinate of top-left corner of the filling rectangle
// @param w width of the filling rectangle
// @param h height of the filling rectangle
// @param c color
private void fillRect(int[] pixels, int width, int height,
int x, int y, int w, int h, int c)
{
for (int i = 0; i < h && i + y < height; ++i) {
for (int j = 0; j < w && j + x < width; ++j) {
pixels[i*width + j] = c;
}
}
}
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要素目から順にコピーしてみる
for (int i = 0; i < sample_pixels.length() && i < pixels.length(); ++i)
pixels.setPixel(i, sample_pixels.getPixel(i)); // <*>
}
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