java - Saving two BufferedImages within a LayeredPane -



java - Saving two BufferedImages within a LayeredPane -

i working layeredpane contains 2 images, 1 on each layer. have been working on method obtains image positions (based on label positions images in) , save bottom image (lower 1 within layeredpane) , top 1 if @ covering bottom image (may part of image), have been having problem , i'm bit unsure on how working properly.

i have been stuck working on quite while help existing code or thoughts on how should approach way big help me.

thanks in advance.

public void saveimagelayering(bufferedimage topimg,bufferedimage bottomimg, jlabel toplabel, jlabel bottomlabel) { int width = bottomimg.getwidth(); int height = bottomimg.getheight(); point bottompoint = new point(); point toppoint = new point(); bottompoint = bottomlabel.getlocation(); toppoint = toplabel.getlocation(); system.out.println("image x coordinate " + bottompoint.x); system.out.println("image y coordinate " + bottompoint.y); //arrays store bottom image int bottomredimgarray[][] = new int[width][height]; int bottomgreenimgarray[][] = new int[width][height]; int bottomblueimgarray[][] = new int[width][height]; //arrays store top image int topredimgarray[][] = new int[width][height]; int topgreenimgarray[][] = new int[width][height]; int topblueimgarray[][] = new int[width][height]; //loop through bottom image , pixels rgb values for(int = bottompoint.x; < width; i++){ for(int j = bottompoint.y; j < height; j++){ //set pixel equal rgb value of pixel beingness looked @ pixel = new color(bottomimg.getrgb(i, j)); //contain rgb values in respective rgb arrays bottomredimgarray[i][j] = pixel.getred(); bottomgreenimgarray[i][j] = pixel.getgreen(); bottomblueimgarray[i][j] = pixel.getblue(); } } //create new image same size old bufferedimage newbottomimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); //set values within 2d array new image (int x1 = 0; x1 < width; x1++){ (int y1 = 0; y1 < height; y1++){ //putting values buffered image int newpixel = (int) bottomredimgarray[x1][y1]; newpixel = (newpixel << 8) + (int) bottomgreenimgarray[x1][y1]; newpixel = (newpixel << 8) + (int) bottomblueimgarray[x1][y1]; newbottomimage.setrgb(x1, y1, newpixel); } } //create rectangle around bottom image check if coordinates of top in within , save ones rectangle rec = new rectangle(bottompoint.x, bottompoint.y, bottomimg.getwidth(), bottomimg.getheight()); //loop through top image , pixels rgb values for(int = bottompoint.x; < bottomimg.getwidth(); i++){ for(int j = bottompoint.y; j < bottomimg.getheight(); j++){ //if top image within lower image getrgb values if (rec.contains(toppoint)) { //___________________________________________________________doesnt contain any.. if (firstpointfound == true) { //set pixel equal rgb value of pixel beingness looked @ pixel = new color(topimg.getrgb(i, j)); //contain rgb values in respective rgb arrays topredimgarray[i][j] = pixel.getred(); topgreenimgarray[i][j] = pixel.getgreen(); topblueimgarray[i][j] = pixel.getblue(); } else { firstpoint = new point(i, j); firstpointfound = true; } } } } //create new image same size old bufferedimage newtopimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); //set values within 2d array new image (int x1 = 0; x1 < topimg.getwidth(); x1++){ (int y1 = 0; y1 < topimg.getheight(); y1++){ //putting values buffered image int newpixel = (int) topredimgarray[x1][y1]; newpixel = (newpixel << 8) + (int) topgreenimgarray[x1][y1]; newpixel = (newpixel << 8) + (int) topblueimgarray[x1][y1]; newtopimage.setrgb(x1, y1, newpixel); } } bufferedimage newimage = new bufferedimage(width, height, bufferedimage.type_int_argb); //uses graphics.drawimage() place them on top of each other graphics g = newimage.getgraphics(); g.drawimage(newbottomimage, bottompoint.x, bottompoint.y, null); g.drawimage(newtopimage, firstpoint.x, firstpoint.y, null); seek { //then save image 1 time in right order file outputfile = new file("saved_layered_image.png"); imageio.write(newimage, "png", outputfile); joptionpane.showmessagedialog(null, "new image saved successfully"); } grab (ioexception e) { e.printstacktrace(); } }

i'm not sure why you're messing around pixels, however, thought relatively simple

basically, want create 3rd "merged" image same size bottomimage. there, want paint bottomimage onto merged image @ 0x0.

then need calculate distance topimage away bottomimage's location , paint @ point.

bufferedimage merged = new bufferedimage(bottomimg.getwidth(), bottomimg.getheight(), bufferedimage.type_int_argb); graphics2d g2d = master.creategraphics(); g2d.drawimage(bottomimg, 0, 0, this); int x = toppoint .x - bottompoint .x; int y = toppoint .y - bottompoint .y; g2d.drawimage(topimg, x, y, this); g2d.dispose();

using basic idea, able produce these...

java image coordinates bufferedimage jlayeredpane

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -