user1993381
user1993381

Reputation: 179

Adding a border to an image in Java

I am trying to create an image that adds a border to an existing image on Java by copying the pixels from their old locations to new coordinates. So far, this is what I have done:

public static NewPic border (NewPic p, int borderWidth, Pixel borderColor) {
   int w = p.getWidth();
   int h = p.getHeight();

   Pixel src[][] = p.getBitmap();
   Pixel tgt[][] = new Pixel[h][w];

   for (int x = 0; x < w; x++) {
     for (int y = 0; y < h; y++) {
       tgt[y][x + y + borderWidth]  = src[x][y]; // this is probably where I a messing up
     }
  }
  return new NewPic(tgt);
  }

Not sure what I am doing wrong in the line where I commented. I am new to Java. Can someone give me some guidance?

Upvotes: 1

Views: 19766

Answers (5)

JaRo
JaRo

Reputation: 376

Clean Java simplest way:

String imagePath = "this/is/your/image.jpg";
BufferedImage myPicture = ImageIO.read(new File(imagePath));
Graphics2D g = (Graphics2D) myPicture.getGraphics();
g.setStroke(new BasicStroke(3));
g.setColor(Color.BLUE);
g.drawRect(10, 10, myPicture.getWidth() - 20, myPicture.getHeight() - 20);
ImageIO.write(myPicture, "jpg", new File(imagePath));

Upvotes: 6

Richard
Richard

Reputation: 54

I know this thread is from 2013 but I came across this, and I have a solution that preserves what OP was trying to do:

public static NewPic border (NewPic p, int borderWidth, Pixel borderColor) 
{
   int w = p.getWidth();
   int h = p.getHeight();

   Pixel src[][] = p.getBitmap();
   Pixel tgt[][] = new Pixel[h + borderWidth * 2][w + borderWidth * 2];

   for (int x = 0; x < w + borderWidth; x++) 
   {
      for (int y = 0; y < h + borderWidth; y++) 
      {
         if (x >= borderWidth && x < w - borderWidth && y >= borderWidth && y < h - borderWidth)
         {
            tgt[x][y] = src[x - borderWidth][y - borderWidth];
         }
         else
         {
            tgt[x][y] = borderColor;
         }
      }
   }
   return new NewPic(tgt);
}

Upvotes: 1

Manish
Manish

Reputation: 104

I used following logic to add border to any image:

BufferedImage source = ImageIO.read(original);
int borderedImageWidth = width + (borderLeft * 2);
int borderedImageHeight = height + (borderTop * 2);
BufferedImage img = new BufferedImage(borderedImageWidth, borderedImageHeight, BufferedImage.TYPE_3BYTE_BGR);
img.createGraphics();
Graphics2D g = (Graphics2D) img.getGraphics();
g.setColor(Color.YELLOW);
g.fillRect(0, 0, borderedImageWidth, borderedImageHeight);
g.drawImage(source, borderLeft, borderTop, width + borderLeft, height + borderTop, 0, 0, width, height, Color.YELLOW, null);
File output = File.createTempFile("output", ".png");
ImageIO.write(img, "png", outputFile);

This will draw an image over a yellow rectangle whose size is greater then the image, thus providing a border to image.

Upvotes: 3

Andrew Thompson
Andrew Thompson

Reputation: 168825

One way is to use a Swing based border.

Image with Border

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;

class ImageBorder {

    public static void main(String[] args) {
    Runnable r = new Runnable() {

        @Override
        public void run() {
        JPanel gui = new JPanel(new BorderLayout());
        // to contrast the 'picture frame' border created below
        gui.setBorder(new LineBorder(Color.BLUE, 12));

        Image image = // your image here..
            new BufferedImage(400,50,BufferedImage.TYPE_INT_RGB);
        JLabel l = new JLabel(new ImageIcon(image));
        Border b1 = new BevelBorder(
            BevelBorder.LOWERED, Color.LIGHT_GRAY, Color.DARK_GRAY);
        Border b2 = new LineBorder(Color.GRAY, 12);
        Border b3 = new BevelBorder(
            BevelBorder.LOWERED, Color.LIGHT_GRAY, Color.DARK_GRAY);
        Border bTemp = new CompoundBorder(b1,b2);
        Border b = new CompoundBorder(bTemp,b3);
        l.setBorder(b);

        gui.add(l);

        JOptionPane.showMessageDialog(null, gui);
        }
    };
    // Swing GUIs should be created and updated on the EDT
    // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
    SwingUtilities.invokeLater(r);
    }
}

Upvotes: 5

syb0rg
syb0rg

Reputation: 8247

You could create a BorderedBufferedImage that accepts an int for borderThickness, a Color for the borderColor, and a BufferedImage.

This website might also offer some help:

import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import javax.swing.BorderFactory; 

class testImagePanel{
    public static void main(String[] args){

    BufferedImage image = null;
    ImagePanel imagePanel = null;

    try{
        image = ImageIO.read(new File("Pictures/pl.jpg"));
        imagePanel = new ImagePanel(image);
    }catch(IOException  e){
         System.err.println("Trying to read in image "+e);
    }

    JFrame frame = new JFrame("Example");
    frame.add(imagePanel);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);   

    }
}

public class ImagePanel extends JPanel {

BufferedImage image;
Dimension size;

public ImagePanel(BufferedImage image) {
    this.image = image;
    this.size = new Dimension();
    size.setSize(image.getWidth(), image.getHeight());
    this.setBackground(Color.WHITE);
    this.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
}

@Override
protected void paintComponent(Graphics g) {
    // Center image in this component.
    int x = (getWidth() - size.width)/2;
    int y = (getHeight() - size.height)/2;
    g.drawImage(image, x, y, this);
}
@Override
public Dimension getPreferredSize() { return size; }
}

Upvotes: 3

Related Questions