{"id":2138,"date":"2010-02-14T18:44:20","date_gmt":"2010-02-14T23:44:20","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2138"},"modified":"2010-02-14T18:44:20","modified_gmt":"2010-02-14T23:44:20","slug":"java-how-to-create-dynamic-pngs-jpgs-gifs","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/java-how-to-create-dynamic-pngs-jpgs-gifs\/","title":{"rendered":"Java: How to create dynamic PNGs, JPGs, GIFs."},"content":{"rendered":"<p>Sometimes you need to create graphics, or compose images and have them saved as regular PNGs, JPEGs or GIFs.<\/p>\n<p>Here&#8217;s a quick and dirty reference of how to do it with BufferedImages, Graphics2D and javax.imageio.*.<br \/>\nVery straightforward.<\/p>\n<p>[java]<br \/>\nimport java.awt.Color;<br \/>\nimport java.awt.Graphics2D;<br \/>\nimport java.awt.image.BufferedImage;<br \/>\nimport java.io.File;<br \/>\nimport java.io.IOException;<\/p>\n<p>import javax.imageio.ImageIO;<br \/>\n&#8230;<\/p>\n<p>\/\/1. Create a BufferedImage, in this case a simple 1024&#215;768 using only RGB colors (you could use alpha for example)<br \/>\nBufferedImage bufferedImage = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB);<\/p>\n<p>\/\/2. Get a hold of a Graphics2D object to do all the painting and compositing on the BufferedImage<br \/>\nGraphics2D graphics = bufferedImage.createGraphics();<\/p>\n<p>\/\/3. Do the painting&#8230; in this case, I just filled with yellow<br \/>\ngraphics.setColor(new Color(255,255,0));<br \/>\ngraphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());<\/p>\n<p>\/\/4. Write the image<br \/>\ntry {<br \/>\n    ImageIO.write(bufferedImage, &quot;png&quot;,new File(&quot;test.png&quot;));<br \/>\n} catch (IOException e) {<br \/>\n   e.printStackTrace();<br \/>\n}<br \/>\n[\/java] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need to create graphics, or compose images and have them saved as regular PNGs, JPEGs or GIFs. Here&#8217;s a quick and dirty reference of how to do it with BufferedImages, Graphics2D and javax.imageio.*. Very straightforward. [java] import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; &#8230; \/\/1. Create a BufferedImage, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[],"class_list":["post-2138","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-yu","jetpack-related-posts":[{"id":156,"url":"https:\/\/www.gubatron.com\/blog\/portscanner-only-60-lines-of-code\/","url_meta":{"origin":2138,"position":0},"title":"PortScanner &#8211; Only 60 lines of code","author":"gubatron","date":"May 19, 2005","format":false,"excerpt":"Hi, here's a simple port scanner I wrote today in Java. I needed to see something on my server, perhaps you can find it useful. I would say its pretty fast, you can tweak the number of threads and timeouts to adjust to your server\/connection. I did't do much testing\u2026","rel":"","context":"In &quot;Gubatron&quot;","block_context":{"text":"Gubatron","link":"https:\/\/www.gubatron.com\/blog\/category\/gubatron\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":584,"url":"https:\/\/www.gubatron.com\/blog\/svn-gotchas-importing-a-folder-in-one-step\/","url_meta":{"origin":2138,"position":1},"title":"svn gotchas: Importing a Folder in one step","author":"gubatron","date":"September 6, 2007","format":false,"excerpt":"Up until this day when my friend Gabe told me how, I didn't know how to import a folder, without doing this first: svn mkdir svn+ssh:\/\/server.com\/path\/to\/repo\/myfolder svn import myFolder svn+ssh:\/\/server.com\/path\/to\/repo\/myfolder If I didn't do that, If I just imported the local \"myFolder\", it would end up adding all the files\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":980,"url":"https:\/\/www.gubatron.com\/blog\/javareflection-notes-invoking-a-static-main-method-from-a-dinamically-loaded-class\/","url_meta":{"origin":2138,"position":2},"title":"Java\/Reflection notes: Invoking a static main() method from a dinamically loaded class.","author":"gubatron","date":"November 22, 2008","format":false,"excerpt":"Maybe for some wild reason, your Java application will need to execute a pre launcher that won't know about the Main class it's supposed to invoke until it's being executed. For example, you have distributed your Java application but you used pack200 to compress your jars, and your new application\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3443,"url":"https:\/\/www.gubatron.com\/blog\/my-intellij-keymap-shortcuts\/","url_meta":{"origin":2138,"position":3},"title":"IntelliJ \/ Adroid Studio \/ CLion \/ GoLand \/ PyCharm \/ PHPStorm keymap shortcuts I can&#8217;t live without","author":"gubatron","date":"December 27, 2015","format":false,"excerpt":"Download settings.jar (Import with `File -> Import Settings`) I got to IntelliJ after years of using Emacs and Eclipse. Here's how my keymap looks now that I work with Jetbrains tools, these are the keyboard shortcuts I can't live without as a programmer. \u00a0 Legend (Mac keyboard) \u2318 Command \u21e7\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/12\/5708080052_830593fb94_b.jpg?fit=1024%2C612&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/12\/5708080052_830593fb94_b.jpg?fit=1024%2C612&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/12\/5708080052_830593fb94_b.jpg?fit=1024%2C612&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/12\/5708080052_830593fb94_b.jpg?fit=1024%2C612&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3195,"url":"https:\/\/www.gubatron.com\/blog\/how-to-import-a-backed-up-wallet-in-bitcoin-qt\/","url_meta":{"origin":2138,"position":4},"title":"How to import a backed up wallet in Bitcoin-Qt","author":"gubatron","date":"November 20, 2013","format":false,"excerpt":"The Bitcoin-Qt client has an export feature, but not an import feature, importing a wallet is a bit of a manual process and it can be quite scary for the non-computer saavy since you need to do this kind of stuff because you got a new computer or your old\u2026","rel":"","context":"In &quot;BitCoin&quot;","block_context":{"text":"BitCoin","link":"https:\/\/www.gubatron.com\/blog\/category\/bitcoin-2\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":532,"url":"https:\/\/www.gubatron.com\/blog\/how-to-update-file-timestamps-in-python\/","url_meta":{"origin":2138,"position":5},"title":"How to update file timestamps in Python","author":"gubatron","date":"May 29, 2007","format":false,"excerpt":"Sometimes you can be real picky like me about timestamps of files, for example, during my wedding we had a few digital cameras, and one of the cameras had its internal clock 4 hours behind. So what better way for a lazy guy like you to change timestamps than writing\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=2138"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2138\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}