java - How to make overlapping shapes produce a new color? -


i'm making game in there red , blue shapes moving around on screen. i've looked high , low how make anywhere overlap different color (purple). using java2d, understanding not support shaders. looked drawing red shapes 1 bufferedimage , blue shapes another, trying use alphacomposite combine colors , draw screen, never produced correct results. i'm using 127,0,0 , 0,0,127 red , blue instead of 255 because 255,0,255 looks, in opinion, terrible purple. this.

thanks copeg's suggestion, able figure out. here code segment (context: shapes i'm drawing attacks):

    //attacks     bufferedimage attackimg = new bufferedimage(s_width, s_height, bufferedimage.type_int_argb);     graphics2d ag = (graphics2d) attackimg.getgraphics();     //make of attackimg transparent image     ag.setcomposite(alphacomposite.clear);     ag.fillrect(0, 0, s_width, s_height);     ag.setcomposite(alphacomposite.srcover);      //render red attacks attackimg     ag.setcolor(new color(127, 0, 0, 255));     for(shape s : redattacks)         ag.fill(s);      //render overlap areas using composites attackimg     ag.setcolor(new color(127, 0, 127, 255));     ag.setcomposite(alphacomposite.getinstance(alphacomposite.src_in));     g.setcolor(new color(0, 0, 127, 255));     for(shape s : blueattacks)     {         ag.fill(s);         g.fill(s); //render blue attacks     }     //render red , purple attacks     g.drawimage(attackimg, 0, 0, null); 

Comments