添加边框在Java中的图像 [英] Adding a border to an image in Java

查看:112
本文介绍了添加边框在Java中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个由他们的老位置,以新坐标复制像素增加了一个边框关于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);
  }

不知道我在我评论该行做错了。我是新来的Java。有人可以给我一些指导?

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

推荐答案

一种方法是使用一个基于Swing边框。

One way is to use a Swing based 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);
    }
}

这篇关于添加边框在Java中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆