Smile3.java

 1: // <APPLET CODE="Smile3" WIDTH="330" HEIGHT="410">
 2: //    <PARAM NAME="IMAGE" VALUE="Mona.gif">
 3: // </APPLET>
 4: // Smile3.java - A Java swing applet that displays a GIF graphic.
 5: // This version demonstrates the use of javax.imageio package.
 6: // The JPanels are used to get the red border effect.
 7: //
 8: // Written 2014 by Wayne Pollock, Tampa FL USA.
 9: 
10: import java.awt.*;
11: import java.awt.image.BufferedImage;
12: import java.net.URL;
13: import java.io.IOException;
14: import javax.imageio.*;
15: import javax.swing.*;
16: import javax.swing.border.*;
17: 
18: public class Smile3 extends JApplet {
19:    private static final long serialVersionUID = 1L;  // Prevents javac warning
20: 
21:    @Override
22:    public void init() {
23:       try {  // Always build and access swing GUIs from the EDT:
24:          SwingUtilities.invokeAndWait( new Runnable() {
25:             @Override public void run() {
26:                createGUI();
27:             }
28:          } );
29:       } catch ( Exception e ) {
30:          e.printStackTrace(System.err);
31:       }
32:    }
33: 
34:    private void createGUI() {
35:       String imageName = getParameter( "IMAGE" );
36:       JLabel picLbl = null;
37: 
38:       // Read image and create JLable from it:
39:       try {
40:          if ( imageName != null ) {
41:             URL url = getClass().getResource( imageName );
42:             BufferedImage image = ImageIO.read( url );
43:             ImageIcon picIcon = new ImageIcon( image );
44:             picLbl = new JLabel( picIcon );
45:          }
46:       } catch ( IOException e ) {
47:          picLbl = new JLabel( "Image not available!" );
48:       }
49: 
50:       // Set the background color to Cyan (same as HTML page):
51:       JPanel p1 = new JPanel();
52:       p1.setBackground( Color.CYAN );
53:       p1.setOpaque( true );
54: 
55:       // Add a 4 pixel red border around the image:
56:       JPanel p2 = new JPanel( new FlowLayout( FlowLayout.CENTER, 0, 0 ) );
57:       p2.setBorder( new LineBorder( Color.RED, 4 ) );
58:       p1.add( p2 );
59:       p2.add( picLbl );
60: 
61:       add( p1 );
62:    }
63: }