问题部分出在您指定的复合材料中:AlphaComposite.SRC
我真的不知道您使用它的目的是什么,但它会覆盖源像素数据。这就是为什么在面板背景上绘制图像时会覆盖面板背景的原因。
如果您还没有阅读,我建议您阅读有关图形复合的内容: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 );
}
}
只需运行它并查看结果:
还有一些关于代码的注释为什么你应该或不应该做某事。
确保您仔细阅读它们。