gpt4 book ai didi

java - 如何为带圆角的 JDialog 设置 3D 边框?

转载 作者:搜寻专家 更新时间:2023-11-01 01:13:45 25 4
gpt4 key购买 nike

我可以像在 How to create a rounded title border in Java Swing 中一样向我的 JDialog 添加圆角边框.但它仍然是一种颜色。我想让边框看起来像 3D。

这是我尝试过的方法。

Graphics2D g2d = (Graphics2D) g;
Color c1 = getBackground();
Color c2 = color1.darker();
int w = getWidth();
int h = getHeight();

GradientPaint gp = new GradientPaint(
0, 0, c1,
0, h, c2);

g2d.setPaint(gp);
g2d.fill3DRect(0,0, w, h,true);

然后,没有 3D 外观,但边框的边框颜色加宽了。
我怎样才能做到这一点?

任何示例代码或链接将不胜感激。

最佳答案

3D Border

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.*;

public class ThreeDimensionalBorder extends AbstractBorder {

private static final long serialVersionUID = 1L;
private Color color;
private int thickness = 8;
private int radii = 8;
private Insets insets = null;
private BasicStroke stroke = null;
private int strokePad;
RenderingHints hints;
int shadowPad = 3;

ThreeDimensionalBorder(Color color) {
this(color, 128, 8);
}

ThreeDimensionalBorder(Color color, int transparency, int shadowWidth) {
this.color = color;
shadowPad = shadowWidth;

stroke = new BasicStroke(thickness);
strokePad = thickness/2;

hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

int pad = radii + strokePad;
int bottomPad = pad + strokePad + shadowPad;
int rightPad = pad + strokePad + shadowPad;
insets = new Insets(pad,pad,bottomPad+shadowPad,rightPad);
}

@Override
public Insets getBorderInsets(Component c) {
return insets;
}

@Override
public Insets getBorderInsets(Component c, Insets insets) {
return getBorderInsets(c);
}

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

Graphics2D g2 = (Graphics2D)g;

int bottomLineY = height-thickness-shadowPad;

RoundRectangle2D.Double bubble = new RoundRectangle2D.Double(
0+strokePad,
0+strokePad,
width-thickness-shadowPad,
bottomLineY,
radii,
radii
);

Area area = new Area(bubble);

g2.setRenderingHints(hints);

g2.setColor(color);
g2.setStroke(stroke);
g2.draw(area);

Area shadowArea = new Area(new Rectangle(0,0,width,height));
shadowArea.subtract(area);
g.setClip(shadowArea);
Color shadow = new Color(color.getRed(),color.getGreen(),color.getBlue(),128);
g2.setColor(shadow);
g2.translate(shadowPad,shadowPad);
g2.draw(area);
AffineTransform at = g2.getTransform();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel p = new JPanel();
String t = "The quick brown fox jumps over the lazy dog!";

JLabel l1 = new JLabel(t);
l1.setBorder(new ThreeDimensionalBorder(Color.MAGENTA.darker(),128,4));
p.add(l1);

JLabel l2 = new JLabel(t);
l2.setBorder(new ThreeDimensionalBorder(Color.BLACK,200,5));
p.add(l2);

JLabel l3 = new JLabel(t);
l3.setBorder(new ThreeDimensionalBorder(Color.BLUE,40,6));
p.add(l3);

JOptionPane.showMessageDialog(null, p);
}
});
}
}

关于java - 如何为带圆角的 JDialog 设置 3D 边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12233928/

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