Turn single pixel on/off or change color in Java
How should I开发者_如何转开发 turn a each pixel on or off and change its color :-/
if you use BufferedImage, there's a method called setRGB which should do what you want.
If you're doing normal painting with a Graphics
(or Graphics2D
) object (a.k.a using AWT/Swing), then drawing a single-pixel line is the normal way:
int x = 100;
int y = 200;
g.drawLine(x, y, x, y);
There are no methods for drawing pixels, to my knowledge, in java. What you do, is do use fillRect or drawLine (on a BufferedImage), with with and height equal to 1.
Use setRGB beforehand to set the color.
However, if you are using this to create a single image, (like a fractal picture generator or similar), then there are much more efficient methods:
int pixels[] = new int[width * height];
//Draw pixels here, by using something like this:
pixels[y*width + x] = (alpha<<24) | (rgb[0]<<16) | (rgb[1]<<8) | rgb[2]
//Convert to an image like so:
MemoryImageSource source = new MemoryImageSource(width,height,pixels,0,width);
Image image = Toolkit.getDefaultToolkit().createImage(source);
精彩评论