I've been driving myself crazy with something I originally thought would be incredibly simple. Here's a really simple example of what I'm trying to accomplish: I have a picture sized 800x530, all I need to do is center this picture in a new image that is 800x800 with a black background.

I'm going to need to make this dynamic later, but for now that is all I'm trying to figure out.

Now, I've tried several methods and all I've managed to do is create a new 800x800 image painted black. After I do that I have been trying to copy the pixels (with for loops) from the image I'm trying to center onto the new black image, but I just cannot figure it out. There has to be some really super simple solution I've overlooked.

int  height=800;
    int width = 800;
    Picture picA = new Picture(height, width); //new blank background image
    String p1 = "C:\\Users\\Stephen\\Desktop\\images.jpg";
    Picture myPic = new Picture(p1); //image I'm trying to center    
    
    //set new picA to color black
    Color black = new Color(0,0,0); 
    picA.setAllPixelsToAColor(black);

    Pixel pixel= null;
    Pixel pixel2 = null;    
    int vert= 135;
    int hori= 0;

//********Failed copy pixel method*********
//    for (int h=0+vert; h<height-vert; h++ )
//    {
//      for (int w=0+hori; w<width-hori; w++)
//      {
//        pixel = picA.getPixel(h, w);
//        pixel.setColor(green);
//      }
//    }
//    picA.show();
//*************************************

That sort of a dummy version of one of my failed methods. It just ends up showing me a black 800x800 image.


Please, any help you can give will help me retain my sanity.

I ended up figuring it out by copying colors and putting it on the new image...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.