// Scott DeRuiter 3/27/03 // ShowImage.java // Shows an image. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ShowImageBetter extends JApplet { private Image im; private DrawingAreaBetter canvas; public void init ( ) { im = getImage ( getDocumentBase ( ), "Halloween.jpg" ); WaitForImage ( this, im ); canvas = new DrawingAreaBetter ( im ); setContentPane ( canvas ); } public void WaitForImage ( JApplet component, Image image ) { MediaTracker tracker = new MediaTracker ( component ); try { tracker.addImage ( image, 0 ); tracker.waitForID ( 0 ); } catch ( InterruptedException e ) { e.printStackTrace ( ); } } } class DrawingAreaBetter extends JPanel implements MouseListener
{ private int xpos, ypos; private boolean click; private Image image; public DrawingAreaBetter ( Image i ) { image = i; xpos = ypos = 0; click = false; addMouseListener ( this ); setBackground( Color.darkGray ); } public void paintComponent ( Graphics g ) { boolean b; if ( !click ) super.paintComponent ( g ); else b = 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 ) {} }