Java padding image

后端 未结 3 1235
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-13 21:38

I am working on creating an online image editing tool.Looking for some refernce how can I add an image with white space on right side.For example see this image

3条回答
  •  日久生厌
    2021-02-13 22:22

    Presumably, you want to create a new image from an existing image, where the new image has white space on the left and right?

    Suppose the unpadded image was a BufferedImage and is called 'image'. Suppose the amount of whitespace you want on each side is 'w'. What you want to do is create a new BufferedImage wider than the original, then paint the entire thing white, and finally draw the smaller image on top of it:

    BufferedImage newImage = new BufferedImage(image.getWidth() + 2 * w, image.getHeight(), image.getType());
    
    Graphics g = newImage.getGraphics();
    
    g.setColor(Color.white);
    g.fillRect(0, 0, image.getWidth() + 2 * w, image.getHeight());
    g.drawImage(image, w, 0, null);
    g.dispose();
    

提交回复
热议问题