Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created June 22, 2009 04:54
Show Gist options
  • Save mrkn/133808 to your computer and use it in GitHub Desktop.
Save mrkn/133808 to your computer and use it in GitHub Desktop.
// PixelData.java
import javax.microedition.lcdui.*;
public class PixelData {
private int[] pixels;
private int width;
private int height;
private PixelData(int[] pixels, int width, int height)
{
this.pixels = pixels;
this.width = width;
this.height = height;
}
public PixelData(int width, int height)
{
this(new int[width * height], width, height);
}
public static PixelData createPixelData(String filename)
throws java.io.IOException
{
Image img = Image.createImage(filename);
int[] pixels = new int[img.getWidth() * img.getHeight()];
img.getRGB(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());
return new PixelData(pixels, img.getWidth(), img.getHeight());
}
private int index(int x, int y)
{
return y * this.width + x;
}
// deprecated
public int getPixel(int i)
{
return this.pixels[i];
}
public int getPixel(int x, int y)
{
return this.pixels[index(x, y)];
}
// deprecated
// (あとで消す)
public void setPixel(int i, int v)
{
this.pixels[i] = v;
}
public void setPixel(int x, int y, int v)
{
this.pixels[index(x, y)] = v;
}
// deprecated
// (あとで消す)
public int length() { return this.pixels.length; }
public int getWidth() { return this.width; }
public int getHeight() { return this.height; }
public Image createRGBImage()
{
return Image.createRGBImage(this.pixels, this.width, this.height, false);
}
// fill a given rectangle area
//
// @param x x-coordinate of the filling rectangle
// @param y y-coordinate of the filling rectangle
// @param w width of the filling rectangle
// @param h height of the filling rectangle
// @param c fill color
public void fillRect(int x, int y, int w, int h, int c)
{
for (int i = 0; i < h && i + y < getHeight(); ++i) {
for (int j = 0; j < w && j + x < getWidth(); ++j) {
this.pixels[index(j + x, i + y)] = c;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment