Created
July 6, 2009 04:42
-
-
Save mrkn/141267 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
// 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; | |
} | |
public int getPixel(int x, int y) | |
{ | |
return this.pixels[index(x, y)]; | |
} | |
public void setPixel(int x, int y, int v) | |
{ | |
this.pixels[index(x, y)] = v; | |
} | |
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