1 package demo.notification.whiteboard; 2 3 4 5 import java.io.File ; 6 7 import java.awt.Canvas ; 8 9 import java.awt.Toolkit ; 10 11 import java.awt.MediaTracker ; 12 13 import java.awt.Dimension ; 14 15 import java.awt.Image ; 16 17 import java.awt.Graphics ; 18 19 20 21 23 25 26 27 public class PicView extends Canvas { 28 29 private Toolkit toolkit; 30 31 private MediaTracker mediaTracker; 32 33 private Image image; 34 35 36 37 public PicView(String filename) { 38 39 super(); 40 41 toolkit = Toolkit.getDefaultToolkit(); 42 43 mediaTracker = new MediaTracker (this); 44 45 image = loadImage(filename); 46 47 setSize(getDim() ); 48 49 } 50 51 52 53 private Dimension getDim() { 54 55 Dimension d = new Dimension (0, 0); 56 57 if (image != null) { 58 59 d.width = image.getWidth (this); 60 61 d.height = image.getHeight (this); 62 63 } 64 65 return d; 66 67 } 68 69 70 71 Image loadImage(String filename) { 72 73 if ( !new File (filename).exists ()) { 74 75 System.out.println("File "+filename+" not Found !"); 76 77 System.exit(1); 78 79 } 80 81 82 83 Image imageData = toolkit.getImage(filename); 84 85 mediaTracker.addImage(imageData,0); 86 87 88 89 try { 90 91 mediaTracker.waitForID(0); 92 93 } catch (InterruptedException ie) {} 94 95 96 97 return imageData; 98 99 } 100 101 102 103 public void paint(Graphics g) { 104 105 g.clearRect (0, 0, getDim().width, getDim().height); 106 107 g.drawImage(image,0,0,null); 108 109 } 110 111 } 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | Popular Tags |