/home/wpollock1/public_html/Java/Smile.java
// Smile.java - A Java runnable Jar that displays a GIF graphic.
// To create the Jar, compile Smile.java as normal, then:
// jar -cfe Smile.jar Smile Smile*.class Mona.gif
// To run your Jar, either double-click the Jar or:
// java -jar Smile.jar
//
// Written 11/2017 by Wayne Pollock, Tampa FL USA.
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.imageio.ImageIO;
public class Smile extends Frame {
// TODO: make image name a command line argument:
private static final String IMAGE_NAME = "Mona.gif";
private static Image pic;
public static void main ( String[] args ) {
// Load the image from the same place as Smile.class:
try {
pic = ImageIO.read( Smile.class.getResource( IMAGE_NAME ) );
} catch ( IOException e ) {
JOptionPane.showMessageDialog( null, "Cannot read \"" +
IMAGE_NAME + "\"!" );
return; // Abort program
}
Frame me = new Smile();
me.setTitle( IMAGE_NAME );
// Set Frame size to the picture's dimensions, plus extra for the
// titlebar height and Frame border (determined by trial and error):
me.setSize( pic.getWidth( null ) + 40, pic.getHeight( null ) + 60 );
me.setLocationRelativeTo( null ); // Center frame on screen
me.setBackground( Color.CYAN );
// Make closing the window exit the program:
me.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
});
me.setVisible( true );
}
public void paint ( Graphics g ) {
// Draw the picture below the titlebar, centered in the Frame:
g.drawImage( pic, 20, 40, null );
// Draw a red 1-pixel frame around the picture:
g.setColor( Color.RED );
int picWidth = pic.getWidth( null );
int picHeight = pic.getHeight( null );
g.drawRect( 19, 39, picWidth+1, picHeight+1 );
}
}