{"id":94,"date":"2011-05-28T00:49:58","date_gmt":"2011-05-28T05:49:58","guid":{"rendered":"http:\/\/trillworks.com\/nick\/?p=94"},"modified":"2012-04-10T23:47:11","modified_gmt":"2012-04-11T04:47:11","slug":"pong-with-the-java-2d-api","status":"publish","type":"post","link":"https:\/\/trillworks.com\/nick\/2011\/05\/28\/pong-with-the-java-2d-api\/","title":{"rendered":"Pong with the Java 2D API"},"content":{"rendered":"<p>I&#8217;m planning on using the Java 2D API for a project this summer, but I need to get familiarized first. In the previous post I covered <a href=\"http:\/\/trillworks.com\/nick\/2011\/05\/27\/pong-on-the-mc9s12c32\/\">Pong on a the C32 microcontroller<\/a>. Here I&#8217;ll look at a desktop implementation using AWT.<\/p>\n<p>A great starting point is this <a href=\"http:\/\/zetcode.com\/tutorials\/javagamestutorial\/collision\/\">zetcode tutorial<\/a> on game programming. The author covers a series of classic arcade games. I used their code to figure out how to initialize the Swing JPanel and handle keyboard input.<\/p>\n<p>Here&#8217;s my implementation:<br \/>\n<a href=\"http:\/\/trillworks.com\/nick\/wp-content\/uploads\/2011\/05\/pong.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-108\" title=\"pong in java\" src=\"http:\/\/trillworks.com\/nick\/wp-content\/uploads\/2011\/05\/pong.jpg\" alt=\"\" width=\"602\" height=\"303\" srcset=\"https:\/\/trillworks.com\/nick\/wp-content\/uploads\/2011\/05\/pong.jpg 602w, https:\/\/trillworks.com\/nick\/wp-content\/uploads\/2011\/05\/pong-300x150.jpg 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><\/p>\n<p><strong>Controls<\/strong><br \/>\nPlayer 1: Q, A<br \/>\nPlayer 2: Up, Down<br \/>\nPause: Space<br \/>\nRestart: Esc<\/p>\n<p>The collision detection code is almost exactly the same as the C version. This version is a little more robust in that it supports arbitrary window sizes, paddle lengths\/widths\/speeds, and ball velocities. The only real trouble spots were devising a scheme to handle two keys being held down simultaneously and repainting the frame at the correct times. For more fun, consider modifying this version to change the X and Y speeds of the ball based on paddle velocity or collision position.<\/p>\n<p>Here&#8217;s the <a href=\"http:\/\/trillworks.com\/nick\/wp-content\/uploads\/2011\/05\/PongJava2D.zip\">playable jar file<\/a> and the <a href=\"http:\/\/trillworks.com\/nick\/wp-content\/uploads\/2011\/05\/GraphicsFun.zip\">Eclipse project<\/a> with the source.<\/p>\n<p>&nbsp;<\/p>\n<p>Helpful links<br \/>\n<a href=\"http:\/\/forums.bit-tech.net\/showthread.php?t=159922\">http:\/\/forums.bit-tech.net\/showthread.php?t=159922<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m planning on using the Java 2D API for a project this summer, but I need to get familiarized first. In the previous post I covered Pong on a the C32 microcontroller. Here I&#8217;ll look at a desktop implementation using AWT. A great starting point is this zetcode tutorial on game programming. The author covers &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/trillworks.com\/nick\/2011\/05\/28\/pong-with-the-java-2d-api\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Pong with the Java 2D API&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[13,11,10,5,12],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-technical","tag-2d-api","tag-awt","tag-java-2d","tag-pong","tag-swing"],"_links":{"self":[{"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":9,"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":116,"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/posts\/94\/revisions\/116"}],"wp:attachment":[{"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trillworks.com\/nick\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}