gpt4 book ai didi

java - 我如何删除圆形边框下不需要的背景?

转载 作者:行者123 更新时间:2023-12-04 05:53:33 24 4
gpt4 key购买 nike

我用类来制作圆形边框

类(class)是:

public class RoundedBorder implements Border {
int radius;

public RoundedBorder(int radius) {
this.radius = radius;
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(this.radius/2, this.radius, this.radius/2, this.radius);
}
@Override
public boolean isBorderOpaque() {
return true;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

g.drawRoundRect(x,y,width-1,height-1,radius,radius);
}
}

对于我使用的按钮:
JTextField login_nickname = new JTextField();

login_nickname.setBorder(new RoundedBorder(10));
login_nickname.setPreferredSize(new Dimension(150, 25));

它工作正常,但我想删除角落圆角边框外未使用的背景,我附上了图片来解释更多我的意思,

enter image description here

谢谢你

最佳答案

我会在paintBorder()中做这样的事情:

@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (c.getParent() != null) {
Color bc = g.getColor();
g.setColor(c.getParent().getBackground());
for (int r = 0; r<radius;r++){
g.drawRoundRect(x, y, width - 1, height - 1, r, r);
}
g.setColor(bc);
}
g.drawRoundRect(x, y, width - 1, height - 1, radius, radius);
}

如果组件有一些父容器,我会先绘制带有背景颜色的边框,然后在它的顶部 - 我的圆形边框。

关于java - 我如何删除圆形边框下不需要的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9785911/

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