Martim Alves
Martim Alves

Reputation: 3

How can I add a border to an image in Java?

The border needs to be made out of the closest pixel of the given image, I saw some code online and came up with the following. What am I doing wrong? I'm new to java, and I am not allowed to use any methods.

/**
 * TODO Method to be done. It contains some code that has to be changed
 * 
 * @param enlargeFactorPercentage the border in percentage
 * @param dimAvg                  the radius in pixels to get the average colour
 *                                of each pixel for the border
 * 
 * @return a new image extended with borders
 */
public static BufferedImage addBorders(BufferedImage image, int enlargeFactorPercentage, int dimAvg) {

    // TODO method to be done

    int height = image.getHeight();
    int width = image.getWidth();

    System.out.println("Image height = " + height);
    System.out.println("Image width = " + width);

    // create new image
    BufferedImage bi = new BufferedImage(width, height, image.getType());

    // copy image
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixelRGB = image.getRGB(x, y);
            bi.setRGB(x, y, pixelRGB);
        }
    }

    // draw top and bottom borders

    // draw left and right borders

    // draw corners

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int pixelRGB = image.getRGB(x, y);
            for (enlargeFactorPercentage = 0; enlargeFactorPercentage < 10; enlargeFactorPercentage++){
                bi.setRGB(width, enlargeFactorPercentage, pixelRGB * dimAvg);
                bi.setRGB(enlargeFactorPercentage, height, pixelRGB * dimAvg);
            }
        }
    }

    return bi;

Upvotes: 0

Views: 1262

Answers (2)

camickr
camickr

Reputation: 324108

I am not allowed to use any methods.

What does that mean? How can you write code if you can't use methods from the API?

int enlargeFactorPercentage

What is that for? To me, enlarge means to make bigger. So if you have a factor of 10 and your image is (100, 100), then the new image would be (110, 110), which means the border would be 5 pixels?

Your code is creating the BufferedImage the same size as the original image. So does that mean you make the border 5 pixels and chop off 5 pixels from the original image?

Without proper requirements we can't help.

@return a new image extended with borders

Since you also have a comment that says "extended", I'm going to assume your requirement is to return the larger image.

So the solution I would use is to:

  1. create the BufferedImage at the increased size
  2. get the Graphics2D object from the BufferImage
  3. fill the entire BufferedImage with the color you want for the border using the Graphics2D.fillRect(….) method
  4. paint the original image onto the enlarged BufferedImage using the Graphics2D.drawImage(…) method.

Upvotes: 1

F_Schmidt
F_Schmidt

Reputation: 1132

Hello and welcome to stackoverflow!

Not sure what you mean with "not allowed using methods". Without methods you can not even run a program because the "thing" with public static void main(String[] args) is a method (the main method) and you need it, because it is the program starting point...

But to answer your question: You have to load your image. A possibility would be to use ImageIO. Then you create a 2D graphics object and then you can to drawRectangle() to create a border rectangle:

BufferedImage bi = //load image
Graphics2D g = bi.getGraphics();
g.drawRectangle(0, 0, bi.getHeight(), bi.getWidth());

This short code is just a hint. Try it out and read the documentation from Bufferedimage see here and from Graphics2D

Edit: Please notice that this is not quite correct. With the code above you overdraw the outer pixel-line from the image. If you don't want to cut any pixel of, then you have to scale it up and draw with bi.getHeight()+2 and bi.getWidth()+2. +2 because you need one pixel more at each side of the image.

Upvotes: 0

Related Questions