Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Java AWT Code Examples

Display an Image in Canvas

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
Display an Image in Canvas import java.awt.*; import java.awt.image.*; public class ImageCanvas extends Canvas { Image image; public ImageCanvas(String name) { MediaTracker media = new MediaTracker(this); image = Toolkit.getDefaultToolkit().getImage(name); media.addImage(image, 0); try { media.waitForID(0); } catch (Exception e) {} } public ImageCanvas(ImageProducer imageProducer) { image = createImage(imageProducer); } public void paint(Graphics g) { g.drawImage(image, 0,0, this); } public static void main(String argv[]) { if (argv.length < 1) { System.out.println ("usage: ImageCanvas.class [image file name]"); System.exit(0); } Frame frame = new Frame(argv[0]); frame.setLayout(new BorderLayout()); frame.add("Center", new ImageCanvas(argv[0])); frame.resize(400,400); frame.show(); } }