Problem is partially in the composite you specify: AlphaComposite.SRC
I don't really know what for did you use it but it overwrites source pixels data. That is why panel background gets overwrited when image is painted over it.
I suggest you to read about composite in graphics if you didn't read it yet:
http://docs.oracle.com/javase/tutorial/2d/advanced/compositing.html
Anyway, see the example how something similar could be done:
(this is just one of possibilities - you could do it in ten other ways)
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 );
}
}
Just run this and see the result:
There are also a few comments over the code why you should or shouldn't do something.
Make sure you read them carefully.
repaint()
inpaintComponent()
. This is creating an infinite loop! – Guillaume Poletrepaint()
when you change the value ofradians
or any other attribute that will modify the current display. Invokingrepaint()
insidepaintComponent()
means that your application is continuously painting which will make your application slow, unresponsive and eat up your CPU. – Guillaume Polet