// SinglePictureApplication.java (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2007-02-08 File created. // 2007-02-08 Last modification. // This program demonstrates how a Java application can show pictures. // The picture files (e.g. .jpg or .png) files must be in the same // folder where the Java program (i.e. the .class file) is located. import java.awt.* ; import javax.swing.* ; class SinglePictureFrame extends JFrame { Image picture_to_show ; static final int FRAME_WIDTH = 800 ; static final int FRAME_HEIGHT = 400 ; public SinglePictureFrame() { setTitle( "SinglePictureFrame" ) ; setSize( FRAME_WIDTH, FRAME_HEIGHT ) ; Toolkit toolkit = Toolkit.getDefaultToolkit() ; picture_to_show = toolkit.getImage( "governor_arnold_schwarzenegger.jpg" ) ; setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ; } public void paint( Graphics graphics ) { super.paint( graphics ) ; int picture_width = picture_to_show.getWidth( this ) ; int picture_height = picture_to_show.getHeight( this ) ; int picture_position_x = 15 ; int picture_position_y = 15 ; // The following statement shows the picture in its natural size. graphics.drawImage( picture_to_show, picture_position_x, picture_position_y, this ) ; picture_position_x = picture_position_x + picture_width + 10 ; // Next we'll show a smaller version of the picture. graphics.drawImage( picture_to_show, picture_position_x, picture_position_y, picture_width / 2, picture_height / 2, this ) ; picture_position_x = picture_position_x + picture_width / 2 + 10 ; // The last statement shows the picture so that its width is enlarged. graphics.drawImage( picture_to_show, picture_position_x, picture_position_y, (int) (picture_width * 1.5), picture_height, this ) ; } } public class SinglePictureApplication { public static void main( String[] not_in_use ) { SinglePictureFrame single_picture_frame = new SinglePictureFrame() ; single_picture_frame.setVisible( true ) ; } }