{"id":27154,"date":"2021-05-12T23:10:06","date_gmt":"2021-05-13T03:10:06","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/controlling-image-loading-programming-code-examples-java-j2ee-j2me-applets-and-basic-graphics\/"},"modified":"2021-05-12T23:10:06","modified_gmt":"2021-05-13T03:10:06","slug":"controlling-image-loading-programming-code-examples-java-j2ee-j2me-applets-and-basic-graphics","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=27154","title":{"rendered":"Controlling Image Loading #Programming Code Examples #Java\/J2EE\/J2ME #Applets and Basic Graphics"},"content":{"rendered":"<pre>\n~~~~~~~~~~~~~~~~~~~\nImageBox.java A class that incorrectly tries to load an image and draw an outline around it. The problem is that the size of the image is requested before the image is completely loaded, thus, returning a width and height of -1.\n~~~~~~~~~~~~~~~~~~~\nimport java.applet.Applet;\nimport java.awt.*;\n\n\/** A class that incorrectly tries to load an image and draw an\n *  outline around it. Don't try this at home.\n *\n ********************\n\npublic class ImageBox extends Applet {\n  private int imageWidth, imageHeight;\n  private Image image;\n\n  public void init() {\n    String imageName = getParameter(&quot;IMAGE&quot;);\n    if (imageName != null) {\n      image = getImage(getDocumentBase(), imageName);\n    } else {\n      image = getImage(getDocumentBase(), &quot;error.gif&quot;);\n    }\n    setBackground(Color.white);\n\n    \/\/ The following is wrong, since the image won't be done\n    \/\/ loading, and -1 will be returned.\n    imageWidth = image.getWidth(this);\n    imageHeight = image.getHeight(this);\n  }\n\n  public void paint(Graphics g) {\n    g.drawImage(image, 0, 0, this);\n    g.drawRect(0, 0, imageWidth, imageHeight);\n  }\n}\n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nBetterImageBox.java An improved version of ImageBox.java. Here a MediaTracker is used to block (wait till the image is completely loaded) before preceding to determine the image size.\n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nimport java.applet.Applet;\nimport java.awt.*;\n\n\/** This version fixes the problems associated with ImageBox by\n *  using a MediaTracker to be sure the image is loaded before\n *  you try to get its dimensions.\n *\n *********************************\n\npublic class BetterImageBox extends Applet {\n  private int imageWidth, imageHeight;\n  private Image image;\n\n  public void init() {\n    String imageName = getParameter(&quot;IMAGE&quot;);\n    if (imageName != null) {\n      image = getImage(getDocumentBase(), imageName);\n    } else {\n      image = getImage(getDocumentBase(), &quot;error.gif&quot;);\n    }\n    setBackground(Color.white);\n    MediaTracker tracker = new MediaTracker(this);\n    tracker.addImage(image, 0);\n    try {\n      tracker.waitForAll();\n    } catch(InterruptedException ie) {}\n    if (tracker.isErrorAny()) {\n      System.out.println(&quot;Error while loading image&quot;);\n    }\n\n    \/\/ This is safe: image is fully loaded\n    imageWidth = image.getWidth(this);\n    imageHeight = image.getHeight(this);\n  }\n\n  public void paint(Graphics g) {\n    g.drawImage(image, 0, 0, this);\n    g.drawRect(0, 0, imageWidth, imageHeight);\n  }\n}\n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nTrackerUtil.java A utility class that lets you load and wait for an image in a single swoop.\n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\nimport java.awt.*;\n\n\/** A utility class that lets you load and wait for an image or\n *  images in one fell swoop. If you are loading multiple\n *  images, only use multiple calls to waitForImage if you\n *  <b>need<\/b> loading to be done serially. Otherwise, use\n *  waitForImages, which loads concurrently, which can be\n *  much faster.\n *\n *******************\n\npublic class TrackerUtil {\n  public static boolean waitForImage(Image image, Component c) {\n    MediaTracker tracker = new MediaTracker(c);\n    tracker.addImage(image, 0);\n    try {\n      tracker.waitForAll();\n    } catch(InterruptedException ie) {}\n    if (tracker.isErrorAny()) {\n      return(false);\n    } else {\n      return(true);\n    }\n  }\n\n  public static boolean waitForImages(Image[] images,\n                                      Component c) {\n    MediaTracker tracker = new MediaTracker(c);\n    for(int i=0; i&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\n<\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10385<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Applets and Basic Graphics<br \/>Tags:Java\/J2EE\/J2MEApplets and Basic Graphics<br \/> Post Data:2017-01-02 16:04:39<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>~~~~~~~~~~~~~~~~~~~ ImageBox.java A class that incorrectly tries to load an image and draw an outline around it. The problem is that the size of the image is requested before the image is completely loaded, thus, returning a width and height of -1. ~~~~~~~~~~~~~~~~~~~ import java.applet.Applet; import java.awt.*; \/** A class that incorrectly tries to load &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=27154\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-27154","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10490,"url":"http:\/\/bangla.sitestree.com\/?p=10490","url_meta":{"origin":27154,"position":0},"title":"Controlling Image Loading","author":"","date":"August 29, 2015","format":false,"excerpt":"~~~~~~~~~~~~~~~~~~~ ImageBox.java A class that incorrectly tries to load an image and draw an outline around it. The problem is that the size of the image is requested before the image is completely loaded, thus, returning a width and height of -1. ~~~~~~~~~~~~~~~~~~~ import java.applet.Applet; import java.awt.*; \/** A class\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10487,"url":"http:\/\/bangla.sitestree.com\/?p=10487","url_meta":{"origin":27154,"position":1},"title":"Loading Images","author":"","date":"August 29, 2015","format":false,"excerpt":"JavaMan1.java Applet that loads an image from a relative URL. ************************************************************* import java.applet.Applet; import java.awt.*; \/** An applet that loads an image from a relative URL. \u00a0* >>>>>>>>>>>>>>>>>>> public class JavaMan1 extends Applet { \u00a0 private Image javaMan; \u00a0 public void init() { \u00a0\u00a0\u00a0 javaMan = getImage(getCodeBase(),\"images\/Java-Man.gif\"); \u00a0 } \u00a0\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27152,"url":"http:\/\/bangla.sitestree.com\/?p=27152","url_meta":{"origin":27154,"position":2},"title":"Loading Images #Programming Code Examples #Java\/J2EE\/J2ME #Applets and Basic Graphics","author":"Author-Check- Article-or-Video","date":"May 12, 2021","format":false,"excerpt":"JavaMan1.java Applet that loads an image from a relative URL. ************************************************************* import java.applet.Applet; import java.awt.*; \/** An applet that loads an image from a relative URL. * >>>>>>>>>>>>>>>>>>> public class JavaMan1 extends Applet { private Image javaMan; public void init() { javaMan = getImage(getCodeBase(),\"images\/Java-Man.gif\"); } public void paint(Graphics g) {\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10402,"url":"http:\/\/bangla.sitestree.com\/?p=10402","url_meta":{"origin":27154,"position":3},"title":"Demonstrates using a TexturePaint to fill an shape with a tiled image","author":"","date":"August 28, 2015","format":false,"excerpt":"^^^^^^^^^^^^^^^^^ TiledImages.java Demonstrates using a TexturePaint to fill an shape with a tiled image. Uses the following class and images: \u00a0\u00a0\u00a0 * ImageUtilities.java Simplifies creating a BufferedImage from an image file. ~~~~~~~~~~~~~~~~~~ import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; \/** An example of using TexturePaint to fill objects with\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26880,"url":"http:\/\/bangla.sitestree.com\/?p=26880","url_meta":{"origin":27154,"position":4},"title":"Demonstrates using a TexturePaint to fill an shape with a tiled image #Programming Code Examples #Java\/J2EE\/J2ME #Drawing","author":"Author-Check- Article-or-Video","date":"May 4, 2021","format":false,"excerpt":"^^^^^^^^^^^^^^^^^ TiledImages.java Demonstrates using a TexturePaint to fill an shape with a tiled image. Uses the following class and images: * ImageUtilities.java Simplifies creating a BufferedImage from an image file. ~~~~~~~~~~~~~~~~~~ import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; \/** An example of using TexturePaint to fill objects with tiled\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27229,"url":"http:\/\/bangla.sitestree.com\/?p=27229","url_meta":{"origin":27154,"position":5},"title":"A demo providing multiple buttons to select a playing card-A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display #Programming Code Examples #Java\/J2EE\/J2ME #Advanced Swing","author":"Author-Check- Article-or-Video","date":"May 14, 2021","format":false,"excerpt":"####################### # CardDemo.java A demo providing multiple buttons to select a playing card. A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display.Uses the following class and images: * CardPanel.java A Panel that displays a playing card. * ImageLabel.java A Canvas for displaying images.\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27154"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27154\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27154"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}