Function
set() disregards transparencies, and any other fancies!
So, we're stuck w/
image(), which is perhaps slower, but more advanced!
A very good strategy is to create
PImages in advance while within
setup();
so they're ready to be quick displayed once in
draw()!
1st, we need to create a transparent
PImage outta the fused tile from those loaded images.
I did something similar below:
And you can see it online at the link below:
2nd, once we've got a
PImage tile, we can fill up the canvas w/ it.
Then, we can create another
PImage from it, to be used as
background()!
I did something like that here:
After all of that, we spare your program to keep doing it all the time within
draw()!
A single
background() is enough to show the result from all of it!!!
Check it out:
WARNING: I've not run it, 'cause I don't have those images to load. Cross your fingers!
// http://forum.processing.org/topic/
// using-set-when-dealing-with-images-containing-transparency-causes-problems
import java.util.Arrays;
final static byte DIM = 32, FPS = 50;
final static String TILE1 = "32x32Pic.png";
final static String TILE2 = "32x32TransparencyPic.png";
final static String GFX = JAVA2D; // use JAVA2D or P2D
PImage bg;
void setup() {
size(768, 512, GFX);
smooth();
frameRate(FPS);
imageMode(CORNER);
makeCanvasTransparent();
// Loads & displays tile images in the same place, merging them:
image(loadImage(TILE1), 0, 0);
image(loadImage(TILE2), 0, 0);
// Copy fused 32x32 image into a PImage:
final PImage tile = createImage(DIM, DIM, ARGB);
tile.copy(g, 0, 0, DIM, DIM, 0, 0, DIM, DIM);
//tile.copy(get(), 0, 0, DIM, DIM, 0, 0, DIM, DIM);
// Fill whole canvas w/ tiled fused image
// and copy resultant background to another PImage:
bg = createTiledBackground(tile);
}
void makeCanvasTransparent() {
// Dumps canvas' PGraphics g into pixels[]:
loadPixels();
// Makes PGraphics g 100% transparent black:
Arrays.fill(pixels, 0);
//for (int i = pixels.length; i-- != 0; pixels[i] = 0);
updatePixels();
}
PImage createTiledBackground(PImage img) {
final int w = img.width, h = img.height;
int row = height/h;
while (row-- != 0)
for ( int col = width/w; col-- != 0;
image(img, col*w, row*h) );
return get();
}
void draw() {
background(bg);
}