// ImageApplication.java // Demonstrates displaying and scaling an image in an application // Adapted from ImageApplet.java by cmc, 7/25/01 // Updated to Java 5, 2/26/05 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ImageApplication extends JComponent { private Image cup; private final static int HEIGHT=300, WIDTH=300; public ImageApplication() { // get the image cup = Toolkit.getDefaultToolkit().getImage("javacup.gif"); // size the panel setSize(WIDTH, HEIGHT); } public void paintComponent( Graphics g ) { // draw the original image g.drawImage(cup, 0, 0, this); // draw the image again, scaled to fit the width of the applet // and the height of the applet minus 120 pixels g.drawImage(cup, 0, 120, getWidth(), getHeight()-120, this); } public static void main(String a[]) { JFrame frame = new JFrame("Image application demo"); frame.setSize(WIDTH, HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new ImageApplication()); frame.setVisible(true); } }