gpt4 book ai didi

java - 为什么我看不到用这个 ShadowRenderer 生成的阴影?

转载 作者:行者123 更新时间:2023-11-29 03:42:17 26 4
gpt4 key购买 nike

我正在尝试使用 ShadowRenderer从 swingx 为面板创建阴影。这是我到目前为止所做的:

  • 在面板构造函数中创建一次阴影渲染器。

    public CustomPanel() {
    super();
    renderer = new ShadowRenderer(20, 0.5f, Color.RED);
    }
  • 每次调整面板大小时,我都会重新计算新阴影。

    @Override
    public void setBounds(int x, int y, int width, int height) {
    super.setBounds(x, y, width, height);
    shadow = renderer.createShadow(GraphicsUtilities.createCompatibleTranslucentImage(width, height));
    }
  • 然后我覆盖面板的 paintComponent 方法来绘制生成的图像:

    protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();

    g2.drawImage(shadow, 0, 0, null);
    //super.paintComponent(g);
    }

但是 shadow 图像从未显示。为什么?我读了this除了我的代码之外,我还绘制了一种由阴影渲染器生成的“阴影”图像。

最佳答案

这是一个 shortened example of DropShadowDemo

JXPanel panel = new JXPanel() {
int shadowSize = 40;
ShadowRenderer renderer = new ShadowRenderer(shadowSize/ 2, 0.5f, Color.RED);
BufferedImage imageA =
XTestUtils.loadDefaultImage("moon.jpg");
BufferedImage shadow;

@Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
// not really needed here - the base image size is fixed
shadow = renderer.createShadow(imageA);
}

@Override
protected void paintComponent(Graphics g) {
int x = (getWidth() - imageA.getWidth()) / 2;
int y = (getHeight() - imageA.getHeight()) / 2;

Graphics2D g2 = (Graphics2D) g;
Composite c = g2.getComposite();
g2.setComposite(AlphaComposite.SrcOver.derive(renderer.getOpacity()));
g.drawImage(shadow, x - shadowSize / 2, y - shadowSize / 2, null);
g2.setComposite(c);
g.drawImage(imageA, x, y, null);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(imageA.getWidth() + shadowSize, imageA.getHeight()+ shadowSize);
}

};
panel.setOpaque(false);

关于java - 为什么我看不到用这个 ShadowRenderer 生成的阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12597472/

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