I have an image of size w by h. In Java, I need to create an image that is size w by h+20 where the top w by 20 pixels is white, and the rest of the image is the same as the original image.
Essentially I want to know how I can add 20 pixels of white to the top of an existing buffered image.
So it would be something like:
public static void main (String[] args) {
BufferedImage originalImage = [the original image with a specific file path];
...code to create a new image 20 pixels higher...
...code to paint originalImage 20 pixels down on the new image
...code to save the new image...
}
Suggestion:
GraphicsConfiguration.createCompatibleImage(int width, int height)to create aBufferedImageof the same width, but with a height that’s +20.BufferedImage.createGraphics()to obtain theGraphics2Dobject of this image.Graphics.setColor(Color c)andGraphics.fillRect(int x, int y, int width, int height)to draw the white topGraphics.drawImage(Image img, int x, int y, ImageObserver observer)to draw the original image to the specified coordinates of the new image.