// Scott DeRuiter 5/6/03 // ShowImageFrame.java // Shows an image. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ShowImageFrame extends JFrame { private int xpos, ypos; private boolean click; private DrawingArea canvas; private Image image; public static void main(String[] args) { Image im = Toolkit.getDefaultToolkit().getImage ( "Halloween.jpg" ); ShowImageFrame showim = new ShowImageFrame ( im ); } public ShowImageFrame ( Image i ) { super ( "ShowImage" ); xpos = ypos = 0; click = false; image = i; WaitForImage ( this, image ); canvas = new DrawingArea ( ); setContentPane ( canvas ); canvas.setBackground( Color.darkGray ); setSize ( 500, 500 ); setDefaultCloseOperation ( DISPOSE_ON_CLOSE ); setLocation ( 20, 50 ); show ( ); } public void WaitForImage ( JFrame component, Image image ) { MediaTracker tracker = new MediaTracker ( component ); try { tracker.addImage ( image, 0 ); tracker.waitForID ( 0 ); } catch ( InterruptedException e ) { e.printStackTrace ( ); } } class DrawingArea extends JPanel implements MouseListener { public DrawingArea ( ) { addMouseListener ( this ); } public void paintComponent ( Graphics g ) { boolean b; if ( !click ) super.paintComponent ( g ); else g.drawImage ( image, xpos, ypos, 270, 180, this ); click = false; } public void mousePressed ( MouseEvent e ) { click = true; xpos = e.getX ( ) - 135; ypos = e.getY ( ) - 90; repaint ( ); } public void mouseClicked ( MouseEvent e ) {} public void mouseReleased ( MouseEvent e ) {} public void mouseEntered ( MouseEvent e ) {} public void mouseExited ( MouseEvent e ) {} } }