Reputation:
I've been trying to draw sort of an 'overlay' on top of a JApplet for debugging, but come to find out; it doesn't want to.
Although! If I change the JApplet to a JPanel, then it works perfectly fine.
I can't find any logical reasoning behind this rather than that JApplet itself extends Applet which is an AWT component, and JPanel extends JComponent which is the root swing component. (As far as I'm concerned)
An animated image of what's going on: http://puu.sh/5NUV2/7aa1c26671.gif
Here is the code I'm working with:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class OverlayTest extends JFrame {
public static final Color OVERLAY_COLOR = new Color( 200, 10, 10, 127 );
public static void main( String[] args ) {
new OverlayTest();
}
public JPanel overlay;
public Component applet;
public OverlayTest() {
this.setTitle( "" );
this.setDefaultCloseOperation( EXIT_ON_CLOSE );
this.setLayout( new BorderLayout() );
this.setResizable( false );
this.applet = new JApplet() {
{
this.setBackground( Color.BLACK );
this.setPreferredSize( new Dimension( 256, 256 ) );
}
@Override
public void paint( Graphics g ) {
g.setColor( this.getBackground() );
g.fillRect( 0, 0, this.getWidth(), this.getHeight() );
g.setColor( Color.ORANGE );
g.drawRect( 16, 16, this.getWidth() - 32, this.getHeight() - 32 );
System.out.println( "Painted applet" );
}
};
this.overlay = new JPanel( null ) {
{
this.setOpaque( false );
OverlayTest.this.setGlassPane( this );
this.setVisible( true );
}
@Override
public void paint( Graphics g ) {
g.setColor( OVERLAY_COLOR );
g.fillOval( 0, 0, this.getWidth(), this.getHeight() );
System.out.println( "Painted overlay" );
}
};
this.add( this.applet, BorderLayout.CENTER );
this.pack();
this.setVisible( true );
}
}
Upvotes: 0
Views: 249
Reputation: 347214
A list of possible issues...
JApplet
is conceptual very similar to a JFrame
, it has a JRootPane
which contains the content and other elementsInstead of trying to set the frame's glass pane, why not set the applets?
Upvotes: 1