{"id":10402,"date":"2015-08-28T07:59:01","date_gmt":"2015-08-28T11:59:01","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10402"},"modified":"2015-08-24T09:59:27","modified_gmt":"2015-08-24T13:59:27","slug":"demonstrates-using-a-texturepaint-to-fill-an-shape-with-a-tiled-image","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10402","title":{"rendered":"Demonstrates using a TexturePaint to fill an shape with a tiled image"},"content":{"rendered":"<pre>^^^^^^^^^^^^^^^^^\r\nTiledImages.java Demonstrates using a TexturePaint to fill an shape with a tiled image. Uses the following class and images:\r\n\r\n\u00a0\u00a0\u00a0 * ImageUtilities.java Simplifies creating a BufferedImage from an image file. \r\n~~~~~~~~~~~~~~~~~~\r\nimport javax.swing.*;\r\nimport java.awt.*;\r\nimport java.awt.geom.*;\r\nimport java.awt.image.*;\r\n\r\n\/** An example of using TexturePaint to fill objects with tiled\r\n\u00a0*\u00a0 images. Uses the getBufferedImage method of ImageUtilities \r\n\u00a0*\u00a0 to load an Image from a file and turn that into a \r\n\u00a0*\u00a0 BufferedImage.\r\n\u00a0*\r\n\u00a0****************\r\n\r\npublic class TiledImages extends JPanel {\r\n\u00a0 private String dir = System.getProperty(\"user.dir\");\r\n\u00a0 private String imageFile1 = dir + \"\/images\/marty.jpg\";\r\n\u00a0 private TexturePaint imagePaint1;\r\n\u00a0 private Rectangle imageRect;\r\n\u00a0 private String imageFile2 = dir + \"\/images\/bluedrop.gif\";\r\n\u00a0 private TexturePaint imagePaint2;\r\n\u00a0 private int[] xPoints = { 30, 700, 400 };\r\n\u00a0 private int[] yPoints = { 30, 30, 600 };\r\n\u00a0 private Polygon imageTriangle =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new Polygon(xPoints, yPoints, 3);\r\n\u00a0 public TiledImages() {\r\n\u00a0\u00a0\u00a0 BufferedImage image =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ImageUtilities.getBufferedImage(imageFile1, this);\r\n\u00a0\u00a0\u00a0 imageRect = new Rectangle(235, 70, image.getWidth(),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 image.getHeight());\r\n\u00a0\u00a0\u00a0 imagePaint1 = new TexturePaint(image, imageRect);\r\n\u00a0\u00a0\u00a0 image = ImageUtilities.getBufferedImage(imageFile2, this);\r\n\u00a0\u00a0\u00a0 imagePaint2 =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 new TexturePaint(image, new Rectangle(0, 0, 32, 32));\r\n\u00a0 }\r\n\r\n\u00a0 public void paintComponent(Graphics g) {\r\n\u00a0\u00a0\u00a0 super.paintComponent(g);\r\n\u00a0\u00a0\u00a0 Graphics2D g2d = (Graphics2D)g;\r\n\u00a0\u00a0\u00a0 g2d.setPaint(imagePaint2);\r\n\u00a0\u00a0\u00a0 g2d.fill(imageTriangle);\r\n\u00a0\u00a0\u00a0 g2d.setPaint(Color.blue);\r\n\u00a0\u00a0\u00a0 g2d.setStroke(new BasicStroke(5));\r\n\u00a0\u00a0\u00a0 g2d.draw(imageTriangle);\r\n\u00a0\u00a0\u00a0 g2d.setPaint(imagePaint1);\r\n\u00a0\u00a0\u00a0 g2d.fill(imageRect);\r\n\u00a0\u00a0\u00a0 g2d.setPaint(Color.black);\r\n\u00a0\u00a0\u00a0 g2d.draw(imageRect);\r\n\u00a0 }\r\n\r\n\u00a0 public static void main(String[] args) {\r\n\u00a0\u00a0\u00a0 WindowUtilities.openInJFrame(new TiledImages(), 750, 650);\r\n\u00a0 }\r\n}\r\n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\r\nImageUtilities.java Simplifies creating a BufferedImage from an image file.\r\n&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;\r\nimport java.awt.*;\r\nimport java.awt.image.*;\r\n\r\n\/** A class that simplifies a few common image operations, in\r\n\u00a0*\u00a0 particular, creating a BufferedImage from an image file and\r\n\u00a0*\u00a0 using MediaTracker to wait until an image or several images\r\n\u00a0*\u00a0 are done loading.\r\n\u00a0*\r\n\u00a0********************\r\n\r\npublic class ImageUtilities {\r\n\u00a0 \r\n\u00a0 \/** Create Image from a file, then turn that into a\r\n\u00a0\u00a0 *\u00a0 BufferedImage.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static BufferedImage getBufferedImage(String imageFile,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Component c) {\r\n\u00a0\u00a0\u00a0 Image image = c.getToolkit().getImage(imageFile);\r\n\u00a0\u00a0\u00a0 waitForImage(image, c);\r\n\r\n\u00a0\u00a0\u00a0 BufferedImage bufferedImage =\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 new BufferedImage(image.getWidth(c), image.getHeight(c),\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BufferedImage.TYPE_INT_RGB);\r\n\u00a0\u00a0\u00a0 Graphics2D g2d = bufferedImage.createGraphics();\r\n\u00a0\u00a0\u00a0 g2d.drawImage(image, 0, 0, c);\r\n\u00a0\u00a0\u00a0 return(bufferedImage);\r\n\u00a0 }\r\n\r\n\u00a0 \/** Take an Image associated with a file, and wait until it is\r\n\u00a0\u00a0 *\u00a0 done loading (just a simple application of MediaTracker).\r\n\u00a0\u00a0 *\u00a0 If you are loading multiple images, don't use this\r\n\u00a0\u00a0 *\u00a0 consecutive times; instead, use the version that takes\r\n\u00a0\u00a0 *\u00a0 an array of images.\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static boolean waitForImage(Image image, Component c) {\r\n\u00a0\u00a0\u00a0 MediaTracker tracker = new MediaTracker(c);\r\n\u00a0\u00a0\u00a0 tracker.addImage(image, 0);\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 tracker.waitForAll();\r\n\u00a0\u00a0\u00a0 } catch(InterruptedException ie) {}\r\n\u00a0\u00a0\u00a0 return(!tracker.isErrorAny());\r\n\u00a0 }\r\n\r\n\u00a0 \/** Take some Images associated with files, and wait until they\r\n\u00a0\u00a0 *\u00a0 are done loading (just a simple application of\r\n\u00a0\u00a0 *\u00a0 MediaTracker).\r\n\u00a0\u00a0 *\/\r\n\r\n\u00a0 public static boolean waitForImages(Image[] images, Component c)\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0 MediaTracker tracker = new MediaTracker(c);\r\n\u00a0\u00a0\u00a0 for(int i=0; i<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>^^^^^^^^^^^^^^^^^ 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 tiled \u00a0*\u00a0 images. Uses the &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10402\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1417,1424],"tags":[706,308,285],"class_list":["post-10402","post","type-post","status-publish","format-standard","hentry","category-code-programming-samples--","category-javaj2eej2me","tag-code","tag-java","tag-285","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":26880,"url":"http:\/\/bangla.sitestree.com\/?p=26880","url_meta":{"origin":10402,"position":0},"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":10142,"url":"http:\/\/bangla.sitestree.com\/?p=10142","url_meta":{"origin":10402,"position":1},"title":"ImportAttribute.jsp Page that demonstrates the import attribute of the page directive. Uses the ServletUtilities class","author":"","date":"August 9, 2015","format":false,"excerpt":"ImportAttribute.jsp\u00a0 Page that demonstrates the import attribute of the page directive. Uses the ServletUtilities class (Check Servlet Section) <!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\"> <!-- Example of the import attribute of the page directive. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press,\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":26814,"url":"http:\/\/bangla.sitestree.com\/?p=26814","url_meta":{"origin":10402,"position":2},"title":"ImportAttribute.jsp  Page that demonstrates the import attribute of the page directive. Uses the ServletUtilities class #Programming Code Examples #Java\/J2EE\/J2ME #JSP","author":"Author-Check- Article-or-Video","date":"May 2, 2021","format":false,"excerpt":"ImportAttribute.jsp Page that demonstrates the import attribute of the page directive. Uses the ServletUtilities class (Check Servlet Section) <!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\"> <!-- Example of the import attribute of the page directive. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press,\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":10343,"url":"http:\/\/bangla.sitestree.com\/?p=10343","url_meta":{"origin":10402,"position":3},"title":"Uses a FileDialog to choose the file to display","author":"","date":"August 27, 2015","format":false,"excerpt":"DisplayFile.java **************** import java.awt.*; import java.awt.event.*; import java.io.*; \/** Uses a FileDialog to choose the file to display. \u00a0*************** \u00a0 public class DisplayFile extends CloseableFrame \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 implements ActionListener { \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 public static void main(String[] args) { \u00a0\u00a0\u00a0 new DisplayFile(); \u00a0 } \u00a0 private Button loadButton; \u00a0 private TextArea\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":10160,"url":"http:\/\/bangla.sitestree.com\/?p=10160","url_meta":{"origin":10402,"position":4},"title":"ShowMessage.java Servlet that demonstrates the use of initialization parameters.","author":"","date":"August 16, 2015","format":false,"excerpt":"ShowMessage.java\u00a0 Servlet that demonstrates the use of initialization parameters. Remember that, to use this servlet, you have to do three things: \u00a0\u00a0\u00a0 * Put the modified web.xml file in the WEB-INF directory. \u00a0\u00a0\u00a0 * Restart the server. \u00a0\u00a0\u00a0 * Use the registered servlet name (i.e., the URL http:\/\/host\/servlet\/ShowMsg), not the\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":10146,"url":"http:\/\/bangla.sitestree.com\/?p=10146","url_meta":{"origin":10402,"position":5},"title":"PluginApplet.jsp Page that demonstrates the use of jsp:plugin.","author":"","date":"August 12, 2015","format":false,"excerpt":"PluginApplet.jsp\u00a0 Page that demonstrates the use of jsp:plugin. Requires you to compile and install PluginApplet.java, TextPanel.java, DrawingPanel.java, and WindowUtilities.java\u00a0 Since these are classes sent to the client to used by applets, the .class files should be in the same directory as the JSP page, not in the WEB-INF\/classes directory where\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":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10402","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\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10402"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10402\/revisions"}],"predecessor-version":[{"id":10403,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10402\/revisions\/10403"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10402"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}