// Return a static image of width and height
public static Image getStaticImage(int w, int h) {
int[] pixels = new int[w * h];
Random rand = new Random();
int index = 0;
// Loop through the pixels of our new image.
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int random = rand.nextInt(2);
int r = 0;
int g = 0;
int b = 0;
// If random is one, make it white
// Otherwise it is a black pixel
if (random == 1) {
r = 255;
g = 255;
b = 255;
}
// Assemble the pixels (here the 255 is your alpha)
pixels[index++] = (255 << 24) | (r << 16) | (g << 8) | b;
}
}
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w,h,pixels,0,w));
}
Posted: March 20, 2023
Return to the snippets listing