gpt4 book ai didi

java - 在 Java 中为图像添加边框

转载 作者:行者123 更新时间:2023-11-29 09:55:04 24 4
gpt4 key购买 nike

我正在尝试创建一个图像,通过将像素从旧位置复制到新坐标来为 Java 上的现有图像添加边框。到目前为止,这就是我所做的:

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 的新手。有人可以给我一些指导吗?

最佳答案

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

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);
}
}

关于java - 在 Java 中为图像添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15351604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com