gpt4 book ai didi

java - 在 Java Swing 中创建一定宽度的 BevelBorder

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:15:03 25 4
gpt4 key购买 nike

我正在尝试使用边框组合创建如下所示的按钮。

在使用 BorderFactory 或 Bevel 类时,没有提供宽度的选项。

是否可以在 Java Swing 中为 BevelBorder 赋予宽度?

enter image description here

最佳答案

不可以,您不能设置 BevelBorder 的宽度。 BevelBorder 只是在每条边上绘制两条 1px 的线 - 一条用于外部阴影,一条用于内部阴影。不幸的是,仅仅调用 setStroke 是行不通的,因为在每个角上,一种颜色会与另一种颜色重叠,这会产生其他不愉快的视觉效果。

此外,您似乎要求的并不是真正的 BevelBorder。这是一个有 5 种颜色规范的边框:顶部、右侧、底部、左侧和线条颜色。 BevelBorder 没有线条颜色,也没有这样的颜色规范。

我制作了一个扩展 AbstractBorder 的类,它应该符合您的要求:

enter image description here


import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;

import javax.swing.border.AbstractBorder;

public class AdvancedBevelBorder extends AbstractBorder {

private Color topColor, rightColor, bottomColor, leftColor, lineColor;
private int borderWidth;

public AdvancedBevelBorder(Color topColor, Color rightColor, Color bottomColor, Color leftColor, Color lineColor,
int borderWidth) {
setTopColor(topColor);
setRightColor(rightColor);
setBottomColor(bottomColor);
setLeftColor(leftColor);
setLineColor(lineColor);
setBorderWidth(borderWidth);
}

@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);

int h = height;
int w = width;
int bw = getBorderWidth();
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.translate(x, y);

Polygon topPolygon = createPolygon(new Point(0, 0), new Point(w, 0), new Point(w - bw, bw), new Point(bw, bw),
new Point(0, 0));
g2.setColor(getTopColor());
g2.fill(topPolygon);
g2.setColor(getLineColor());
g2.draw(topPolygon);

Polygon rightPolygon = createPolygon(new Point(w - 1, 0), new Point(w - 1, h), new Point(w - bw - 1, h - bw),
new Point(w - bw - 1, bw), new Point(w - 1, 0));
g2.setColor(getRightColor());
g2.fill(rightPolygon);
g2.setColor(getLineColor());
g2.draw(rightPolygon);

Polygon bottomPolygon = createPolygon(new Point(0, h - 1), new Point(w, h - 1), new Point(w - bw, h - bw - 1),
new Point(bw, h - bw - 1), new Point(0, h - 1));
g2.setColor(getBottomColor());
g2.fill(bottomPolygon);
g2.setColor(getLineColor());
g2.draw(bottomPolygon);

Polygon leftPolygon = createPolygon(new Point(0, 0), new Point(0, h), new Point(bw, h - bw), new Point(bw, bw),
new Point(0, 0));
g2.setColor(getLeftColor());
g2.fill(leftPolygon);
g2.setColor(getLineColor());
g2.draw(leftPolygon);

g2.dispose();
}

@Override
public Insets getBorderInsets(Component c) {
return new Insets(getBorderWidth(), getBorderWidth(), getBorderWidth() + 1, getBorderWidth() + 1);
}

private Polygon createPolygon(Point... points) {
Polygon polygon = new Polygon();
for (Point point : points) {
polygon.addPoint(point.x, point.y);
}
return polygon;
}

public Color getTopColor() {
return topColor;
}

public void setTopColor(Color topColor) {
this.topColor = topColor;
}

public Color getRightColor() {
return rightColor;
}

public void setRightColor(Color rightColor) {
this.rightColor = rightColor;
}

public Color getBottomColor() {
return bottomColor;
}

public void setBottomColor(Color bottomColor) {
this.bottomColor = bottomColor;
}

public Color getLeftColor() {
return leftColor;
}

public void setLeftColor(Color leftColor) {
this.leftColor = leftColor;
}

public Color getLineColor() {
return lineColor;
}

public void setLineColor(Color lineColor) {
this.lineColor = lineColor;
}

public int getBorderWidth() {
return borderWidth;
}

public void setBorderWidth(int borderWidth) {
this.borderWidth = borderWidth;
}

}

顺便说一句,我使用以下代码制作了您可以在图像中看到的组件:

AdvancedBevelBorder border = new AdvancedBevelBorder(new Color(120, 172, 220), new Color(55, 93, 128),
new Color(73, 124, 169), new Color(150, 191, 229), new Color(36, 83, 126), 10);
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 60);
}
};
panel.setBackground(new Color(91, 154, 212));
panel.setBorder(border);

关于java - 在 Java Swing 中创建一定宽度的 BevelBorder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38967517/

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