gpt4 book ai didi

java - 在透明面板上旋转透明图像

转载 作者:行者123 更新时间:2023-11-30 09:26:05 26 4
gpt4 key购买 nike

我一直在开发一个程序,该程序涉及在透明形式上旋转部分透明的图像。图像的绘制最初效果很好,我还将自定义面板的背景颜色设置为透明的浅蓝色,效果也很好。当我尝试旋转我的图像时,我的问题就开始了。

为了旋转它,我必须将 panel.getGraphics() 转换为 Graphics2D。当我这样做时,透明度消失了,所以我完成了我的旋转代码,然后阅读了透明度。我发现我可以设置 Graphics2D 的合成,这正是我所做的,如下所示:

@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
g2d.setColor(new Color(0, 0, 200, 90));
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.rotate(radians);
g2d.drawImage(img, 0, 0, null);

repaint();
}

当我运行它时,我得到如下表格(请注意,这不是我的正常图像):

Before Rotation

这几乎是我想要的,只是它没有显示透明的蓝色背景。但是,如果我旋转图像,蓝色显示:

After Rotation

最佳答案

问题部分出在您指定的复合 Material 中:AlphaComposite.SRC
我真的不知道你用它做什么,但它会覆盖源像素数据。这就是为什么当在其上绘制图像时面板背景会被覆盖的原因。

如果你还没有读过,我建议你读一读关于图形中的复合 Material : http://docs.oracle.com/javase/tutorial/2d/advanced/compositing.html

无论如何,看看这个例子是如何完成类似的事情的:
(这只是其中一种可能性——您可以通过其他十种方式做到这一点)

public class SmileyTest
{
private static Color bg = new Color ( 0, 0, 255, 128 );
private static float angle = 0f;

public static void main ( String[] args )
{
final ImageIcon icon = new ImageIcon ( SmileyTest.class.getResource ( "icons/smiley.png" ) );

JDialog frame = new JDialog ();
frame.setLayout ( new BorderLayout () );

// We should not use default background and opaque panel - that might cause repaint problems
// This is why we use JPanel with transparent background painted and opacity set to false
JPanel transparentPanel = new JPanel ( new BorderLayout () )
{
protected void paintComponent ( Graphics g )
{
super.paintComponent ( g );
g.setColor ( bg );
g.fillRect ( 0, 0, getWidth (), getHeight () );
}
};
transparentPanel.setOpaque ( false );
frame.add ( transparentPanel );

// Image in another component
final JComponent component = new JComponent ()
{
protected void paintComponent ( Graphics g )
{
super.paintComponent ( g );

Graphics2D g2d = ( Graphics2D ) g;

// For better image quality when it is rotated
g2d.setRenderingHint ( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );

// Rotating area using image middle as rotation center
g2d.rotate ( angle * Math.PI / 180, getWidth () / 2, getHeight () / 2 );

// Transparency for image
g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_OVER, 0.5f ) );

// Draing image
g2d.drawImage ( icon.getImage (), 0, 0, null );
}
};
transparentPanel.add ( component );

// Rotation animation (24 frames per second)
new Timer ( 1000 / 48, new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
angle += 0.5f;
component.repaint ();
}
} ).start ();

frame.setUndecorated ( true );
AWTUtilities.setWindowOpaque ( frame, false );
frame.setSize ( icon.getIconWidth (), icon.getIconHeight () );
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}
}

只需运行它并查看结果: enter image description here

还有一些关于代码的评论,说明您应该或不应该做某事的原因。 请务必仔细阅读。

关于java - 在透明面板上旋转透明图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15063390/

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