Java program to create and apply border effect to image.

Voronoi diagram generated using JavaFX with 25 random placed points using Euclidean distance (distance order of 2).
Image without border.
Image with border
Image with border.

Main.java

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;

// 2018 TheFlyingKeyboard and released under MIT License 
// theflyingkeyboard.net
public class Main {
    public static void main(String[] args) {
        BorderEffect borderEffect = new BorderEffect(10, new Color(0, 0, 0));

        try {
            ImageIO.write(borderEffect.apply(ImageIO.read(new File("image.png"))),
                    "png", new File("Image with border.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

BorderEffect.java

import java.awt.*;
import java.awt.image.BufferedImage;

// 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class BorderEffect {
    private final int borderSize;
    private final Color color;

    public BorderEffect(int borderSize, Color color) {
        this.borderSize = borderSize;
        this.color = color;
    }

    public BufferedImage apply(BufferedImage bufferedImage) {
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.setPaint(color);

        //Horizontal
        graphics2D.fillRect(0, 0, bufferedImage.getWidth(), borderSize);
        graphics2D.fillRect(0, bufferedImage.getHeight() - borderSize, bufferedImage.getWidth(), borderSize);

        //Vertical
        graphics2D.fillRect(0, 0, borderSize, bufferedImage.getHeight());
        graphics2D.fillRect(bufferedImage.getWidth() - borderSize, 0, borderSize, bufferedImage.getHeight());

        return bufferedImage;
    }
}

 

Java Image Border Effect
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close