/home/wpollock1/public_html/Java/Smile3.java
// <APPLET CODE="Smile3" WIDTH="330" HEIGHT="410">
// <PARAM NAME="IMAGE" VALUE="Mona.gif">
// </APPLET>
// Smile3.java - A Java swing applet that displays a GIF graphic.
// This version demonstrates the use of javax.imageio package.
// The JPanels are used to get the red border effect.
//
// Written 2014 by Wayne Pollock, Tampa FL USA.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.border.*;
public class Smile3 extends JApplet {
private static final long serialVersionUID = 1L; // Prevents javac warning
@Override
public void init() {
try { // Always build and access swing GUIs from the EDT:
SwingUtilities.invokeAndWait( new Runnable() {
@Override public void run() {
createGUI();
}
} );
} catch ( Exception e ) {
e.printStackTrace(System.err);
}
}
private void createGUI() {
String imageName = getParameter( "IMAGE" );
JLabel picLbl = null;
// Read image and create JLable from it:
try {
if ( imageName != null ) {
URL url = getClass().getResource( imageName );
BufferedImage image = ImageIO.read( url );
ImageIcon picIcon = new ImageIcon( image );
picLbl = new JLabel( picIcon );
}
} catch ( IOException e ) {
picLbl = new JLabel( "Image not available!" );
}
// Set the background color to Cyan (same as HTML page):
JPanel p1 = new JPanel();
p1.setBackground( Color.CYAN );
p1.setOpaque( true );
// Add a 4 pixel red border around the image:
JPanel p2 = new JPanel( new FlowLayout( FlowLayout.CENTER, 0, 0 ) );
p2.setBorder( new LineBorder( Color.RED, 4 ) );
p1.add( p2 );
p2.add( picLbl );
add( p1 );
}
}