<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;AkQMR3c5eip7ImA9WhRUE0s.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417</id><updated>2012-01-23T17:26:26.922-08:00</updated><category term="i/o" /><category term="lightweight component" /><category term="arguments" /><category term="documentation" /><category term="web" /><category term="literal" /><category term="void" /><category term="video game" /><category term="angle brackets" /><category term="events" /><category term="Math" /><category term="method" /><category term="applet" /><category term="array" /><category term="restore" /><category term="inheritance" /><category term="mouse" /><category term="collision detection" /><category term="Java version" /><category term="classes" /><category term="Scanner" /><category term="List" /><category term="variables" /><category term="object code" /><category term="semicolons" /><category term="humor" /><category term="main()" /><category term="packages" /><category term="type" /><category term="threads" /><category term="listeners" /><category term="local" /><category term="example" /><category term="graphics" /><category term="save" /><category term="JPanel" /><category term="JFrame" /><category term="curly braces" /><category term="output" /><category term="asterisk" /><category term="read" /><category term="JDK" /><category term="dot notation" /><category term="System.out.println()" /><category term="calculations" /><category term="reference" /><category term="book review" /><category term="square brackets" /><category term="regular expressions" /><category term="framework" /><category term="methods" /><category term="numeric" /><category term="inner class" /><category term="loop" /><category term="public" /><category term="initializer" /><category term="code blocks" /><category term="signature" /><category term="import" /><category term="input" /><category term="Comments" /><category term="environment" /><category term="collection" /><category term="IDE" /><category term="application" /><category term="interface" /><category term="Resources" /><category term="comparison" /><category term="paintComponent()" /><category term="parentheses" /><category term="virtual machine" /><category term="constants" /><category term="trigonometry" /><category term="file" /><category term="array initializer" /><category term="embedded" /><category term="JVM" /><category term="declaration" /><category term="static" /><category term="objects" /><category term="name" /><category term="why java" /><category term="Java" /><category term="star" /><category term="API" /><category term="graphics Swing CSS" /><category term="constructor" /><category term="expressions" /><category term="file names" /><category term="dates" /><category term="paint()" /><category term="source code" /><category term="operators" /><category term="heavyweight component" /><category term="command line" /><category term="assignment" /><category term="equals" /><category term="beginner" /><title>A Beginning Programmer's Guide to Java</title><subtitle type="html">Java Programming Mysteries Explained for Those Learning to Program for the First Time, and for Experienced Programmers Just Learning Java</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://beginwithjava.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://beginwithjava.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>71</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/ABeginningProgrammersGuideToJava" /><feedburner:info uri="abeginningprogrammersguidetojava" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;AkQMR3c-eip7ImA9WhRUE0s.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-1845045222891929237</id><published>2012-01-23T17:15:00.000-08:00</published><updated>2012-01-23T17:26:26.952-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-23T17:26:26.952-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="why java" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Java version" /><category scheme="http://www.blogger.com/atom/ns#" term="command line" /><category scheme="http://www.blogger.com/atom/ns#" term="JDK" /><category scheme="http://www.blogger.com/atom/ns#" term="environment" /><category scheme="http://www.blogger.com/atom/ns#" term="JVM" /><category scheme="http://www.blogger.com/atom/ns#" term="beginner" /><title>Java under Mac OS X 10.7 Lion</title><content type="html">Lion is the first version of Mac OS X that doesn't come with Java already installed. Earlier versions not only had the &lt;a href="http://beginwithjava.blogspot.com/2008/07/java-virtual-machine-adapter-cables-for.html"&gt;Java Virtual Machine&lt;/a&gt; or runtime element installed, but they also had the JDK installed, which is the part that lets you write your own Java programs.&lt;br /&gt;&lt;br /&gt;Once, Java was an important part of Apple's strategy. They maintained their own version of Java for the Mac and encouraged its use by developers every bit as much as they encouraged the use of Objective-C.&lt;br /&gt;&lt;br /&gt;The success of the iPhone and the iPad have changed that, however. Apple decided that Java would not be part of those platforms. The lack of Java is the very reason I've chosen not to get an iPhone. Instead, I get phones with Java so that I can run my own Java applications that I write for my own use.&lt;br /&gt;&lt;br /&gt;Now that Lion doesn't have Java pre-installed, what do you do? Fortunately, a deal has been struck where Java is still available for the Mac. It's much the same arrangement as Java for other platforms. In fact, it's easier to install Java on your Mac than any other platform. And you get the JDK along with the runtime (JVM) environment.&lt;br /&gt;&lt;br /&gt;Simply use Finder to go to Applications=&gt;Utilities. There, start Terminal. Once Terminal starts, type in the command 'javac'. Your Mac will tell you that Java isn't installed, and let you install it.&lt;br /&gt;&lt;br /&gt;I'm pleased that the Mac has not entirely forsaken Java, even if it's not an integral part as it has been. The basic software supplied with the Mac has declined severely over the past few years, but fortunately Java is still available and easy to install when you want it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-1845045222891929237?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XYp-p-oaFa3HNdJ3eGJd2KtnWDk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XYp-p-oaFa3HNdJ3eGJd2KtnWDk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XYp-p-oaFa3HNdJ3eGJd2KtnWDk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XYp-p-oaFa3HNdJ3eGJd2KtnWDk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/1eQ9lfGwa2s" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/1845045222891929237?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/1845045222891929237?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/1eQ9lfGwa2s/java-under-mac-os-x-107-lion.html" title="Java under Mac OS X 10.7 Lion" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2012/01/java-under-mac-os-x-107-lion.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkQFR3g9eSp7ImA9WhRWGU8.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-2054368502599722711</id><published>2012-01-06T23:58:00.000-08:00</published><updated>2012-01-07T00:18:36.661-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-07T00:18:36.661-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="IDE" /><category scheme="http://www.blogger.com/atom/ns#" term="why java" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="embedded" /><category scheme="http://www.blogger.com/atom/ns#" term="source code" /><category scheme="http://www.blogger.com/atom/ns#" term="environment" /><category scheme="http://www.blogger.com/atom/ns#" term="JVM" /><category scheme="http://www.blogger.com/atom/ns#" term="virtual machine" /><title>Embedded Java</title><content type="html">Embedded Java sounds almost like an oxymoron. Taking a high level, large, interpreted language like Java and using it in an application field dominated by assembly and C certainly seems odd. But it's a reality.&lt;br /&gt;&lt;br /&gt;For one thing, the microcontrollers of today are not the limited 1K to 2K ROM, 8-bit, 200kHz machine cycle rate CPUs of yesteryear. They're more powerful than the desktop systems of 10-15 years ago in many cases. And we were playing Quake II on those. So maybe Java isn't quite such a stretch, after all.&lt;br /&gt; &lt;br /&gt;&lt;a href="http://www.harbaum.org/till/nanovm/index.shtml"&gt;NanoVM&lt;/a&gt; is a small, subscale Java-ish virtual machine that runs on tiny 8-bit microcontrollers like the AVR ATMega8. It's not a full Java, but it covers most of what's interesting to the microcontroller programmer.&lt;br /&gt;&lt;br /&gt;The &lt;a href="http://www.systronix.com/jstamp/"&gt;JStamp&lt;/a&gt; is a Java development system for the aJile micros. It's also not a full Java SE implementation (what would you do with the extra bits, even if it was?), but it is a verifiable real-time Java system for embedded development.&lt;br /&gt;&lt;br /&gt;Also, &lt;a href="http://www.is2t.com/en/index.php"&gt;IS2T&lt;/a&gt; has MicroEJ, another embedded Java for a number of processors. Everything from the basics of execution and I/O up to GUIs, SOAs, and safety-critical libraries are available.&lt;br /&gt;&lt;br /&gt;I still remember when it was considered laughable to consider a C-language embedded development system. But it has either nudged assembly out of the top spot for embedded development or is close to it, now. Any language that is popular enough can be brought to the task now, and Java is no exception.&lt;br /&gt;&lt;br /&gt;It's all just a bunch of ones and zeroes in the end.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-2054368502599722711?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zv4McpifgvSzWmUDdfvoF9urvwI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zv4McpifgvSzWmUDdfvoF9urvwI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zv4McpifgvSzWmUDdfvoF9urvwI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zv4McpifgvSzWmUDdfvoF9urvwI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/zVlg8_k-te8" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/2054368502599722711?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/2054368502599722711?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/zVlg8_k-te8/embedded-java.html" title="Embedded Java" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2012/01/embedded-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUQMQH8ycCp7ImA9WhdaEUs.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-3996188950826485295</id><published>2011-10-20T19:47:00.000-07:00</published><updated>2011-10-20T20:23:01.198-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-20T20:23:01.198-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="application" /><category scheme="http://www.blogger.com/atom/ns#" term="why java" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Java version" /><category scheme="http://www.blogger.com/atom/ns#" term="applet" /><category scheme="http://www.blogger.com/atom/ns#" term="video game" /><category scheme="http://www.blogger.com/atom/ns#" term="source code" /><category scheme="http://www.blogger.com/atom/ns#" term="environment" /><category scheme="http://www.blogger.com/atom/ns#" term="JVM" /><category scheme="http://www.blogger.com/atom/ns#" term="beginner" /><title>Mobile Java</title><content type="html">One of the nice things about Java is that is supported on more than desktop platforms, and has been for a long time. This means there is not only a large library of existing software, but also well-tuned development systems to use with mobile platforms.&lt;br /&gt;&lt;br /&gt;By "mobile platform", I'm referring to smartphones and tablets. There are other mobile platforms, but these are the most common ones. Netbooks may also run a "mobile" operating system, or they may run a normal desktop OS. Those that run a normal desktop OS will run normal &lt;a href="http://www.oracle.com/technetwork/java/javase/overview/index.html"&gt;Java SE&lt;/a&gt; applications. Java SE is "Java, Standard Edition", the version that typically runs on a desktop or laptop computer.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.oracle.com/technetwork/java/javame/index.html"&gt;Java ME&lt;/a&gt; is Java, Mobile Edition. It runs on most smartphones, and many tablets. It is very similar to the Java SE version covered in most of my articles. In fact, it is possible to write many applications using a subset of Java that will run without change under both Java SE and Java ME.&lt;br /&gt;&lt;br /&gt;But normally a Java ME application will use user interface objects and interfaces that are specific to Java ME. In many ways these are more sophisticated than the ones for Java SE. Creating many types of graphical interfaces, such as tiled graphics, is easier in the mobile edition than in standard Java.&lt;br /&gt;&lt;br /&gt;I have been writing small, simple applications for my cellphones for about ten years now. It's nice to be able to write your own little application for your own unique needs. I started writing Java applications for my &lt;a href="http://en.wikipedia.org/wiki/Nokia_3600/3650"&gt;Nokia 3650&lt;/a&gt;, called a "feature phone" at the time I got it. It was a Symbian Series 60 phone that ran an early version of Java ME with a very basic library of GUI features.&lt;br /&gt;&lt;br /&gt;My next phone was a step up the Java ladder. It was a &lt;a href="http://catsonkeyboards.blogspot.com/2009/05/sciphone-g2-first-impressions.html"&gt;Sciphone G2&lt;/a&gt;, a fake Android phone. I didn't mind that it was "fake", it ran a real version of Java ME with updated GUI capabilities, which made it far easier to write applications for.&lt;br /&gt;&lt;br /&gt;My current phone is a &lt;a href="http://us.blackberry.com/smartphones/blackberrycurve8900/"&gt;Blackberry Curve 8900&lt;/a&gt;. It runs Java ME with all the latest bells and whistles, plus a lot of Blackberry add-ons that make it easy to access the phone's features.&lt;br /&gt;&lt;br /&gt;With my Nokia, I had a special Java development environment provided by Nokia that included a simulation of my phone, so that I could see how my programs would run before I put them on the phone. With the G2 I was on my own. I ran a standard Java ME development environment from within &lt;a href="http://eclipseme.org/"&gt;Eclipse&lt;/a&gt;, a great Java &lt;a href="http://beginwithjava.blogspot.com/2008/07/killer-java-ides-for-beginners-bluej.html"&gt;integrated development environment&lt;/a&gt;. The version linked above is a version specific to Java ME.&lt;br /&gt;&lt;br /&gt;Now I'm back to having a &lt;a href="http://supportforums.blackberry.com/t5/Java-Development/tkb-p/java_dev%40tkb?labels=tutorial"&gt;development environment&lt;/a&gt; provided by my phone's maker. I have a program that simulates my phone on my computer, which again allows me to try out my programs before I put them on the phone (with my G2 I tested them as well as I could, then loaded them on the phone and hoped for the best.) It is build on Eclipse, so it is still very familiar. There is also a slew of information on the Blackberry site (linked above) about Java development.&lt;br /&gt;&lt;br /&gt;Unfortunately, the tutorials on the site don't exactly match the actual current version of the software, but it's close enough it's not too hard to figure out. One thing that confused me, however, is the installation instructions. I thought I had to install the version of Eclipse they called for before installing the "Blackberry Java Eclipse Add-On". It's an add-on, right?&lt;br /&gt;&lt;br /&gt;Well, it turns out that the "add-on" from Blackberry is actually the entire thing, Eclipse and all. So you just need to do that one download to get the development environment. Then download the simulator for your phone and any others you want to test your software on. Finally, apply for a signature key to make it so that you can "sign" your software to allow it to be installed on the phone through the software manager or Over The Air (OTA) when using the Blackberry-specific libraries.&lt;br /&gt;&lt;br /&gt;If you'd rather not do this, you can develop software using a plain-jane version of Java ME, then transfer the software to your phone however you please. I put the software I developed for my Sciphone G2 on the memory card for my Blackberry, and it runs just fine.&lt;br /&gt;&lt;br /&gt;Translating applications between Java SE and Java ME can be simple for ones with minimal amounts of graphics, like programs that mainly use text, buttons, and text entry boxes for communication. Things like games, with a more involved use of graphics, take more effort to translate between the two versions of Java. For these, I usually re-use the game logic code without changes, then rewrite the graphical display parts of the program from scratch. Because I use good object-oriented coding practices (most of the time), this isn't too much effort.&lt;br /&gt;&lt;br /&gt;Java ME applets are easy to translate, though I write almost all of my Java software as applications now.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-3996188950826485295?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/rgMSq2DcNnmEeoJtKMEOwfEa8QI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/rgMSq2DcNnmEeoJtKMEOwfEa8QI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/rgMSq2DcNnmEeoJtKMEOwfEa8QI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/rgMSq2DcNnmEeoJtKMEOwfEa8QI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/tTaPEP5ZHPY" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3996188950826485295?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3996188950826485295?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/tTaPEP5ZHPY/mobile-java.html" title="Mobile Java" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/10/mobile-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU8DQXY-cSp7ImA9WhdWEkU.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-5600550905472850838</id><published>2011-09-05T21:00:00.000-07:00</published><updated>2011-09-05T21:37:50.859-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-05T21:37:50.859-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="initializer" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Java version" /><category scheme="http://www.blogger.com/atom/ns#" term="numeric" /><category scheme="http://www.blogger.com/atom/ns#" term="literal" /><title>Numeric Literal Values</title><content type="html">Java 7 has added a new feature, binary literals for integer number types (byte, short, int, and long.) Before we can get excited about this, we need to know what that means. In earlier versions of Java, we have had several other types of literal value.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Let's Get Literal&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;What is a "literal"? A literal is an explicit value. For example, in this line:&lt;br /&gt;&lt;tt style="color:#0077ff;"&gt;int number=123;&lt;/tt&gt;&lt;br /&gt;the value &lt;tt style="color:#0077ff;"&gt;123&lt;/tt&gt; is a "literal". Since it's a normal base-10 value, we might say it is a "decimal literal". Decimal values don't have to be marked in any special way.&lt;br /&gt;&lt;br /&gt;A hexadecimal literal looks like this:&lt;br /&gt;&lt;tt style="color:#0077ff;"&gt;int number=0x007b;&lt;/tt&gt;&lt;br /&gt;The value &lt;tt style="color:#0077ff;"&gt;0x007b&lt;/tt&gt; is a hexadecimal (base 16) number that is expressed literally (stated explicitly.) The &lt;tt style="color:#0077ff;"&gt;0x&lt;/tt&gt; at the beginning is what marks it as a hexadecimal value. The leading zero shows that it's a numerical value, not a variable name or keyword or something like that. The x marks it as a hexadecimal number.&lt;br /&gt;&lt;br /&gt;Hexadecimal numbers take binary values four bits at a time (a bit is a single one or zero value) and puts them into a single digit. Octal (base 8) digits each represent the values of three bits.&lt;br /&gt;&lt;br /&gt;To tell Java you're using an octal value, place a leading zero in front of the number itself:&lt;br /&gt;&lt;tt style="color:#0077ff;"&gt;int number=0173;&lt;/tt&gt;&lt;br /&gt;Here the number &lt;tt style="color:#0077ff;"&gt;0173&lt;/tt&gt; is an octal value equal to 123 in decimal. If we accidentally put a zero in front of a decimal integer, we'll either get an error (if we use the digits 8 or 9 in that number) or the value will be interpreted incorrectly. For example, if we want to put the value of one hundred twenty-three into a variable, but instead of the line we have above we accidentally add a zero, like this:&lt;br /&gt;&lt;tt style="color:#0077ff;"&gt;int number=0123;&lt;/tt&gt;&lt;br /&gt;The value of the number will be interpreted as eighty-three, not one hundred twenty-three. So watch out for that.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;New for Java 7--Binary Literals&lt;/b&gt;&lt;br /&gt;In Java 7 we get a new ability, the use of binary numbers. Since you know that you can encode binary values into both octal and hex either three or four bits at a time, this may not seem like a really big deal. In fact, there's been a request to add this feature to Java for a long time, but it's been low priority.&lt;br /&gt;&lt;br /&gt;But now it's finally here.&lt;br /&gt;&lt;br /&gt;So you can define values as individual bits, like this:&lt;br /&gt;&lt;tt style="color:#0077ff;"&gt;int number=0b01111011;&lt;/tt&gt;&lt;br /&gt;That's the binary for one hundred twenty-three, by the way. Why is this important?&lt;br /&gt;&lt;br /&gt;As Java becomes more mature, and more widely used, it is used in more places. Over time, high level languages that deal with things at a very abstract level tend to get used in more situations where it is nice to have the high level features while also having the ability to have low level control or simulation of the actual computer hardware. In cases like these, it is nice to be able to manipulate single bits in places where they represent specific individual signals in the computer system or simulation.&lt;br /&gt;&lt;br /&gt;For example, let's say we want to recreate an old 1980s computer in a Java program, like a Commodore 64. The Commodore 64 reads several signal lines directly on its joystick ports. We want to be able to simulate the behavior of these ports so that we can play old C-64 games in our simulation. Each of the lines represents a switch in the joystick. So we may take a particular keypress and represent it as the following binary value:&lt;br /&gt;&lt;br /&gt;&lt;tt style="color:#0077ff;"&gt;byte stick_up=0b00001;&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;In this case, we show it setting the lowest order bit. We might have another like this:&lt;br /&gt;&lt;br /&gt;&lt;tt style="color:#0077ff;"&gt;byte fire_switch=0b100000;&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;Since these each equate to specific electrical signals that get "turned into bits" in the C-64's CIA chip, it's nice to be able to tell them to Java as bits. We could have used hex or octal, but it's a bit nicer to see them as individual bits.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-5600550905472850838?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/FqBibFqZy78kxWVAWTv2tWFjvQA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FqBibFqZy78kxWVAWTv2tWFjvQA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/FqBibFqZy78kxWVAWTv2tWFjvQA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/FqBibFqZy78kxWVAWTv2tWFjvQA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/H9yFPKDJz5Q" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/5600550905472850838?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/5600550905472850838?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/H9yFPKDJz5Q/numeric-literal-values.html" title="Numeric Literal Values" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/09/numeric-literal-values.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU8EQ3ozfyp7ImA9WhZbGUs.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-6404273345857000369</id><published>2011-06-24T17:16:00.000-07:00</published><updated>2011-06-24T18:03:22.487-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-24T18:03:22.487-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="comparison" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="type" /><category scheme="http://www.blogger.com/atom/ns#" term="operators" /><category scheme="http://www.blogger.com/atom/ns#" term="equals" /><category scheme="http://www.blogger.com/atom/ns#" term="variables" /><category scheme="http://www.blogger.com/atom/ns#" term="assignment" /><title>Java Variable Value Assignment: Left Equals Right, Left Becomes Right</title><content type="html">One of those things that vexes beginning programmers is assigning values to a variable in Java. Part of what makes it so vexing is that the problem is practically invisible to an experienced programmer. Another part of it is that we use the equals sign (=) to do the assignment.&lt;br /&gt;&lt;br /&gt;Take a look at these statements:&lt;br /&gt;&lt;br /&gt;&lt;code style="color:#33ff33;"&gt;int i = 1;&lt;/code&gt;&lt;br /&gt;&lt;code style="color:#33ff33;"&gt;j=7;&lt;/code&gt;&lt;br /&gt;&lt;code style="color:#ff3333;"&gt;7+count=c;&lt;/code&gt;&lt;br /&gt;&lt;code style="color:#ff3333;"&gt;b+14=a+9;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The first two are fine (assuming that 7 is a valid value for whatever type of variable &lt;tt&gt;j&lt;/tt&gt; is), but the last two are wrong.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Remember All That Algebra We Taught You? Well, We Broke It.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;After you've gone to all the trouble to learn how to deal with things like &lt;tt&gt;7+count=c&lt;/tt&gt; and &lt;tt&gt;b+14=a+9&lt;/tt&gt; in algebra class, now we take what you know and turn it on its ear in programming class. Sweet, eh?&lt;br /&gt;&lt;br /&gt;The problem is that the &lt;tt&gt;=&lt;/tt&gt; we use in programming has pretty much nothing to do with the &lt;tt&gt;=&lt;/tt&gt; that you use in math. It's just close enough to be confusing. In Java, the equals sign is an &lt;em&gt;assignment&lt;/em&gt; operator, not an indicator of equality between values as it is in math. There is a comparison operator for equality in Java, it's &lt;tt&gt;==&lt;/tt&gt;, and we'll talk about it shortly. First, let's talk assignment.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Assignment&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In computers, we use the term assignment to describe sticking a value into the computer's memory so that we can get it back later. It's like using the memory in a programmable calculator. We put some number into our calculator's memory so that we can get it back later for another part of our calculation. It's pretty much the same thing with computers.&lt;br /&gt;&lt;br /&gt;With Java, we can store all kinds of values, not just numbers. We can store sections of text, called &lt;em&gt;strings&lt;/em&gt; in a &lt;tt&gt;String&lt;/tt&gt; variable, for example. (Java prefers the term "member" over variable, in general. For what we're talking about now, the two terms are interchangeable.) We can also store more than one thing at a time, like all the information about a bicycle in a Bicycle object that keeps track of a bicycle's color, frame size, tire size, gear ratios, etc., all together in one object.&lt;br /&gt;&lt;br /&gt;But we're here to just look at assignment today, not the wide variety of things we can assign as values.&lt;br /&gt;&lt;br /&gt;To store a value on a calculator, you use some key sequence like &lt;tt&gt;M+&lt;/tt&gt; or &lt;tt&gt;STO 00&lt;/tt&gt; to store the value in the calculator's display to memory. With Java, we use the equals sign to store some value on the right into whatever is on the left side of the equals sign. We read from left to right like this:&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;count = a;&lt;/tt&gt;&lt;br /&gt;"count equals a"&lt;br /&gt;&lt;br /&gt;This means we take the value of &lt;tt&gt;a&lt;/tt&gt; and place it in &lt;tt&gt;count&lt;/tt&gt;. But a better way to read that equals sign is to use the word "becomes" instead of "equals":&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;count = 9;&lt;/tt&gt;&lt;br /&gt;"count &lt;b&gt;becomes&lt;/b&gt; nine"&lt;br /&gt;&lt;br /&gt;This makes it more obvious that we're putting in a new value that changes the value of &lt;tt&gt;count&lt;/tt&gt;. In this case, we're putting in a value of 9. The statement up above would be "count becomes a" or, better yet, "count becomes a's value." Because what we're doing in &lt;tt&gt;count= a;&lt;/tt&gt; is taking the value in variable &lt;tt&gt;a&lt;/tt&gt; and copying it into &lt;tt&gt;count&lt;/tt&gt;. If we then print the value in &lt;tt&gt;count&lt;/tt&gt;, it will be the same as whatever is in &lt;tt&gt;a&lt;/tt&gt; at that time.&lt;br /&gt;&lt;br /&gt;We can also compute a value to put into our variable.&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;count = a + 1;&lt;/tt&gt;&lt;br /&gt;"count becomes a plus one"&lt;br /&gt;&lt;br /&gt;This makes the value in &lt;tt&gt;count&lt;/tt&gt; be one more than the current value of &lt;tt&gt;a&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;What we &lt;b&gt;can't&lt;/b&gt; do is change sides around the equals sign. In algebra, &lt;em&gt;c = a + 1&lt;/em&gt; is the same as &lt;em&gt;a + 1 = c&lt;/em&gt;. But in Java that doesn't work:&lt;br /&gt;&lt;br /&gt;&lt;tt style="color:#ff3333;"&gt;a + 1 = count&lt;/tt&gt;&lt;br /&gt;This is wrong in Java. Does not compute. Norman, Norman, help me Norman!&lt;br /&gt;&lt;br /&gt;We would be trying to make a+1 become the value in count. The problem is, you can't have a storage location named "a+1". Java doesn't see this the way your algebra trained eyes do.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Comparisons&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In normal math, when we use the equals sign we are making a comparison. We are stating that what's on one side of the equals sign is the same as what's on the other side. For most algebra problems, we assume that the statement is true and try to find values for the variables that result in that.&lt;br /&gt;&lt;br /&gt;Another use of equals in some math problems is to state that two things are equal when they may or may not be, then determine whether that statement is true or not. You remember those problems, they looked something like this:&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;State whether each of the following is true or false.&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;1. 11 = 6 + 5&lt;br /&gt;2. 14 = 3 x 7&lt;br /&gt;3 65 - 14 = 17 x 3&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;In Java, the equals sign is already used for assigning values to variables. How do we do a comparison to see whether it's true or not?&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;The == Operator&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;In some languages, &lt;tt&gt;=&lt;/tt&gt; does both jobs. But in Java, there's a second operator for doing equality comparisons, &lt;tt&gt;==&lt;/tt&gt;, "equals equals". When we want to see if two values are the same, we compare them using a statement something like this:&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;if (count == 100) then stop();&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;This compares &lt;tt&gt;count&lt;/tt&gt; to the value &lt;tt&gt;100&lt;/tt&gt;, and executes the method &lt;tt&gt;stop()&lt;/tt&gt; if the result of the comparison is &lt;tt&gt;true&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;The Dangers of the = Operator&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Something to look out for is using &lt;tt&gt;=&lt;/tt&gt; by accident in such a situation. Like this:&lt;br /&gt;&lt;br /&gt;&lt;tt style="color:#ff3333;"&gt;if (count = 100) then stop();&lt;/tt&gt;&lt;br /&gt;Don't do this if you want to compare count to 100!&lt;br /&gt;&lt;br /&gt;What this does is &lt;em&gt;assign&lt;/em&gt; the value 100 to &lt;tt&gt;count&lt;/tt&gt;. If the assignment is successful (which it almost certainly will be, if the program compiles), the program will then do &lt;tt&gt;stop()&lt;/tt&gt; every time it hits this statement! Because the assignment was successful, so the result is considered to be &lt;tt&gt;true&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Summary&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The equals sign makes the variable on the left equal the computed value from the right side of the equals sign:&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;name = "Mergatroyd";&lt;/tt&gt;&lt;br /&gt;"name becomes Mergatroyd"&lt;br /&gt;&lt;br /&gt;It doesn't work the other way around (putting something from the left into the variable on the right.)&lt;br /&gt;&lt;br /&gt;If you want to compare to values to see if they're equal, use "equals equals":&lt;br /&gt;&lt;tt&gt;if (state == "done") then exit();&lt;/tt&gt;&lt;br /&gt;"if state equals equals done then exit" or&lt;br /&gt;"if state is equal to done then exit"&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-6404273345857000369?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/6A142BrMy9A75QixV-0mEcz2ORk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6A142BrMy9A75QixV-0mEcz2ORk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/6A142BrMy9A75QixV-0mEcz2ORk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/6A142BrMy9A75QixV-0mEcz2ORk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/tJUY_-bQrYs" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/6404273345857000369?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/6404273345857000369?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/tJUY_-bQrYs/java-variable-value-assignment-left.html" title="Java Variable Value Assignment: Left Equals Right, Left Becomes Right" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/06/java-variable-value-assignment-left.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUQBQXg_fSp7ImA9WhZUFEU.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-8639049499449465739</id><published>2011-06-07T13:16:00.000-07:00</published><updated>2011-06-07T14:49:10.645-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-07T14:49:10.645-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dates" /><category scheme="http://www.blogger.com/atom/ns#" term="methods" /><category scheme="http://www.blogger.com/atom/ns#" term="Math" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="calculations" /><category scheme="http://www.blogger.com/atom/ns#" term="API" /><category scheme="http://www.blogger.com/atom/ns#" term="trigonometry" /><title>Doing Math in Java part 2:Functions</title><content type="html">The basics of math in Java using operators was covered in &lt;a href="http://beginwithjava.blogspot.com/2011/05/doing-math-in-java-part-1-operators.html"&gt;Part 1: Operators&lt;/a&gt;. There I went over the standard operations built into the language itself, rather than in its APIs (libraries).&lt;br /&gt;&lt;br /&gt;But that's hardly the end of useful math operations. Especially when doing graphics, where all those geometric and trigonometric functions come in so useful. Square root, cosine, absolute values, etc.&lt;br /&gt;&lt;br /&gt;If you're wandering into the Java APIs unguided, the java.math package might catch your eye right away. Unfortunately, it's a false trail when what you're looking for is a simple general purpose math package (it's a nice special-purpose math package for particular sorts of calculation, however.) &lt;br /&gt;&lt;br /&gt;What you really want is the &lt;a href="http://download.oracle.com/javase/6/docs/api/java/lang/Math.html"&gt;java.lang.Math&lt;/a&gt; class, in the java.lang package.&lt;br /&gt;&lt;br /&gt;This has most of the standard math functions you're used to. They're defined as static methods, so you don't need to create a Math object to use them. You just call them like this:&lt;br /&gt;&lt;br /&gt;&lt;pre style="color:#0077ff;"&gt;&lt;tt&gt;    float distance = Math.sqrt(dx*dx + dy*dy);&lt;br /&gt;    float speed = Math.abs(velocity);&lt;/tt&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;These call the square root function &lt;code&gt;Math.sqrt()&lt;/code&gt; and absolute value function &lt;code&gt;Math.abs()&lt;/code&gt;. There are a slew of others like degree to radian conversion, the standard trig functions, and so on.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Date Calculations&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Calculations with dates are another standard sort of math that's not covered by standard operators. In the package java.util we've got &lt;a href="http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html"&gt;the Calendar class&lt;/a&gt;. The Calendar class itself is an abstract class. This means that it's a class used to build other classes with, not to use directly. The &lt;a href="http://download.oracle.com/javase/6/docs/api/java/util/GregorianCalendar.html"&gt;GregorianCalendar&lt;/a&gt; class is a concrete implementation of &lt;code&gt;Calendar&lt;/code&gt; that you can use directly. The Calendar object's add() method will add or subtract some amount of time--days, minutes, hours, etc.--to or from a given date (the date that the object is set to:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Create a GregorianCalendar Object&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Set its date&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Use its add() method to add or subtract the time difference.&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre&gt;&lt;tt style="color#00ff33;"&gt;GregorianCalendar myDate;&lt;br /&gt;myDate = new GregorianCalendar(2011, 6, 7); //set the date to 07 June 2011.&lt;br /&gt;myDate.add(Calendar.Date, 165); // add 165 days to that date.&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;before()&lt;/code&gt;, &lt;code&gt;after()&lt;/code&gt; and &lt;code&gt;compareTo&lt;/code&gt; will return whether one date occurs before or after another. Computing the number of days between two dates is a bit more complex, unfortunately, as are similar calculations. Basically, the procedure is to use getTime() to convert both dates to time values, get the difference, then divide that by the time units you want to see the difference in (e.g. divide by 24 hours periods to get the difference in days.)&lt;br /&gt;&lt;br /&gt;Unfortunately, the Calendar class demonstrates one of the problems with much of Java. It's an overly generalized class that prevents simple solutions to many simple and obvious problems. The older Date class took a more direct approach, but it has been deprecated in favor of the Calendar based classes. Unfortunately, even obtaining a printable date value according to the system's current locale takes a fair bit of set up in Java. A better solution would have been a very general class like Calendar behind a more usable class for solving conventional problems, but Java's development got it backward. We got the simple class first, it's inflexibility for solving certain problems resulted in criticism, then came a general class, which is no good at doing simple and obvious things for most users. The extra layer that we should have to make Calendar more usable has not yet appeared in the standard API (and possibly never will.)&lt;br /&gt;&lt;br /&gt;So it takes a lot of code to do what should only take a line or two, when working with dates.&lt;br /&gt;&lt;br /&gt;Still, give the Calendar and GregorianCalendar class a look-over. Even though it takes some extra code and set-up to do some simple tasks, the solutions to these problems are well established and many examples are available. It's just not as simple as System.out.println(myDate);&lt;br /&gt;&lt;br /&gt;I'll be adding articles dealing specifically with dates myself at a future date.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-8639049499449465739?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/VH_dtwknytdFPQgrON-GivoReJw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VH_dtwknytdFPQgrON-GivoReJw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/VH_dtwknytdFPQgrON-GivoReJw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VH_dtwknytdFPQgrON-GivoReJw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/cW2n8rm-q8Y" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/8639049499449465739?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/8639049499449465739?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/cW2n8rm-q8Y/doing-math-in-java-part-2functions.html" title="Doing Math in Java part 2:Functions" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/06/doing-math-in-java-part-2functions.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0MAQHw7cCp7ImA9WhZVFU4.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-4486090902111824962</id><published>2011-05-27T14:31:00.000-07:00</published><updated>2011-05-27T15:30:41.208-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-27T15:30:41.208-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Math" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="asterisk" /><category scheme="http://www.blogger.com/atom/ns#" term="operators" /><category scheme="http://www.blogger.com/atom/ns#" term="expressions" /><title>Doing Math in Java part 1: Operators</title><content type="html">One of the most valuable uses of a computer is...computing. That is, acting as a mathematical calculator. Java has a plethora of tools for doing computation.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Operators&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The standard math functions that are built into the Java language are called &lt;em&gt;operators&lt;/em&gt;. These are the sort of math functions you would expect to find on a simple calculator, plus some extras:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;tt&gt;&lt;pre&gt;+ - * / % ++ --&lt;/pre&gt;&lt;/tt&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;There are other operators used for comparisons, these are the math operators. You can learn more about them in the &lt;a href="http://java.sun.com/docs/books/jls/"&gt;Java Language Specification&lt;/a&gt;, specifically in &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.2"&gt;Integer Operations&lt;/a&gt;, &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.4"&gt;Floating-Point Operations&lt;/a&gt;, and more extensively through &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html"&gt;Chapter 15, Expressions&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;+&lt;/tt&gt; and &lt;tt&gt;-&lt;/tt&gt; do what you would expect, they add and subtract one value from another. &lt;tt&gt;*&lt;/tt&gt; is the symbol used for multiplication, instead of the &amp;times; used in math class. &lt;tt&gt;*&lt;/tt&gt; is used because it's easier for the computer to recognize it as a math operator, &amp;times just looks like a letter 'x' to the computer. Similarly, &lt;tt&gt;/&lt;/tt&gt; is used for division. The &amp;divide; sign isn't on most keyboards, but / has been common on keyboards even before they were attached to computers.&lt;br /&gt;&lt;br /&gt;The % sign doesn't do what you might, at first, expect. It doesn't calculate a percentage, as it does on most calculators. It returns the remainder of a division. For example, if you do 10/3 (ten divided by three), the result will be 3. But what if you want to get the remainder? That's what % does. 10%3 will return a value of 1. '%' is sometimes called "modulo". It's a bit of a misnomer, mathematically, but the use of the term is common and well understood, so it's not unusual to pronounce 10%3 as "ten modulo three". This is a very valuable function for keeping a value within a certain range, such as keeping an object located onscreen in graphics by having it "wrap" from one side of the screen to the other when it reaches an edge.&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;++&lt;/tt&gt; and &lt;tt&gt;--&lt;/tt&gt; are the "increment" and "decrement" operators. Increment means "go up by one unit", and decrement "go down by one unit". So if you've got a variable like:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;int i = 5;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And you do &lt;code&gt;i++&lt;/code&gt;, the value of &lt;code&gt;i&lt;/code&gt; will become 6. Doing &lt;code&gt;i--&lt;/code&gt; will take away 1. &lt;tt&gt;++&lt;/tt&gt; and &lt;tt&gt;--&lt;/tt&gt; can be placed either before or after the value they act on, for example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;number = i++;&lt;br /&gt;count = --j;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;There will be different effects on your program depending on whether they come before or after the value. You can find information on that in the Java Language Specification as well, in sections &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.14"&gt;15.14&lt;/a&gt; and &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.15"&gt;15.15&lt;/a&gt; which cover the two ways of using these operators. In general, it's best to stick to using them &lt;em&gt;after&lt;/em&gt; your value until you understand the differences.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Assignment Operators&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;You can also do math as part of assigning a value. For example, let's say we want to add three to a value in our program. The obvious way to do this would be:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;i = i + 3;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This takes the prior value of &lt;tt&gt;i&lt;/tt&gt;, adds 3 to it, then puts the result back into variable &lt;tt&gt;i&lt;/tt&gt;. With an &lt;em&gt;assignment operator&lt;/em&gt; we can do the same thing more tersely:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;i += 3;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;We've combined the &lt;tt&gt;+&lt;/tt&gt; with &lt;tt&gt;=&lt;/tt&gt; to create an operator. This does the same thing as &lt;code&gt;i = i + 3;&lt;/code&gt;. Your program doesn't care which way you type it. The same thing can be done with the other operators that take two values:&lt;br /&gt;&lt;br /&gt;&lt;code style="color:#0077ff;"&gt;i *= 4; // Multiply i by four, store result back in i.&lt;br /&gt;i /= 2; // Divide i in half.&lt;br /&gt;i %= 3; // Put the remainder of i/3 in i.&lt;br /&gt;i -= 7; //Subtract seven from i.&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;More Complex Math&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;This is what we can do with the basic math operators built into the language. To get more operations, like those found on a scientific or programmer's calculator, we use methods of some of the standard packages of Java. Which I'll cover in a future article.&lt;br /&gt;&lt;br /&gt;Until then, have a look at &lt;a href="http://download.oracle.com/javase/6/docs/api/java/lang/Math.html"&gt;java.lang.Math&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-4486090902111824962?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/7827VPLccBMAVDvBlUF4ASijSCM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7827VPLccBMAVDvBlUF4ASijSCM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/7827VPLccBMAVDvBlUF4ASijSCM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/7827VPLccBMAVDvBlUF4ASijSCM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/ifKpolenD-M" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/4486090902111824962?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/4486090902111824962?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/ifKpolenD-M/doing-math-in-java-part-1-operators.html" title="Doing Math in Java part 1: Operators" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/05/doing-math-in-java-part-1-operators.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQASHw8eCp7ImA9WhZWGU8.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-5424100688292430178</id><published>2011-05-20T13:49:00.000-07:00</published><updated>2011-05-20T14:52:29.270-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-20T14:52:29.270-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="example" /><category scheme="http://www.blogger.com/atom/ns#" term="static" /><category scheme="http://www.blogger.com/atom/ns#" term="methods" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="type" /><category scheme="http://www.blogger.com/atom/ns#" term="objects" /><category scheme="http://www.blogger.com/atom/ns#" term="classes" /><title>Your Own Java Classes</title><content type="html">To use Java effectively, you want to create and use your own classes. This is one of the great powers of object-oriented languages--the ability to construct programs out of independent building-blocks that cut large problems down into small, easily solvable ones. There's a lot more that can be said, and much of it is elsewhere. So I'll get right into a very simple, very basic example here.&lt;br /&gt;&lt;br /&gt;We're going to create a very simple object class that will print a "Hello" message to the screen when called. This class will have two different methods that will do the same thing, though we'll vary the messages they print a bit so that we can see which one is doing the work.&lt;br /&gt;&lt;br /&gt;Here's our class: &lt;a href="http://saundby.com/beginwithjava/code/HelloClass.java"&gt;(download HelloClass.Java)&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="color:#0077ff;"&gt;public class HelloClass{&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * sayHello() prints "Hello!" to the Java console output.&lt;br /&gt; */&lt;br /&gt; public void sayHello(){&lt;br /&gt;  System.out.println("Hello!\n");&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * doHello() prints "Hello, hello!" to the Java console output.&lt;br /&gt; * It's static, so you don't need to instatiate a HelloClass &lt;br /&gt; * object to use it.&lt;br /&gt; */&lt;br /&gt; public static void doHello(){&lt;br /&gt;  System.out.println("Hello, hello!\n");&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;} // End of HelloClass&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The two methods we have to print messages are &lt;tt&gt;sayHello()&lt;/tt&gt; and &lt;tt&gt;doHello()&lt;/tt&gt;. To use &lt;tt&gt;sayHello()&lt;/tt&gt;, we need to have a &lt;tt&gt;HelloClass&lt;/tt&gt; object created, then call that object's &lt;tt&gt;sayHello()&lt;/tt&gt; method.&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;doHello()&lt;/tt&gt;, however, is a &lt;tt&gt;static&lt;/tt&gt; method. This means is belongs to the class, not to any object of the class. So we can use it without any &lt;tt&gt;HelloClass&lt;/tt&gt; objects being created first.&lt;br /&gt;&lt;br /&gt;Here's a class that uses these methods as described: &lt;a href="http://saundby.com/beginwithjava/code/UseHello.java"&gt;(download UseHello.java)&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="color:#00ff77;"&gt;public class UseHello{&lt;br /&gt; public static void main(String[] arg){&lt;br /&gt;&lt;br /&gt;  // We can use doHello() without a HelloClass object:&lt;br /&gt;  HelloClass.doHello(); // call the HelloClass's doHello() method.&lt;br /&gt;&lt;br /&gt;  // But we need to create a HelloClass object to use sayHello():&lt;br /&gt;  HelloClass hello=new HelloClass();&lt;br /&gt;  hello.sayHello(); // call hello's sayHello() method.&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;} // End of UseHello.&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If we try to call &lt;tt&gt;sayHello()&lt;/tt&gt; without first creating a HelloClass object, like this:&lt;br /&gt;&lt;tt&gt;HelloClass.sayHello();&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;then we'll get the dreaded "calling a non-static method from a static context" error message. That's letting you know that you need to instantiate (or create) a HelloClass object first, then tell that object to call its method.&lt;br /&gt;&lt;br /&gt;Static methods are useful for things like general arithmetic and calculation or other methods that might be used in a way where state information is unimportant. But beware, it's easy to create static methods when what's really wanted is an object that does what you want.&lt;br /&gt;&lt;br /&gt;Files available for download through: &lt;a href="http://saundby.com/beginwithjava/"&gt;http://saundby.com/beginwithjava/&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-5424100688292430178?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/GaMH9ppMYSs1HRnRYkM7SJ8IRms/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/GaMH9ppMYSs1HRnRYkM7SJ8IRms/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/GaMH9ppMYSs1HRnRYkM7SJ8IRms/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/GaMH9ppMYSs1HRnRYkM7SJ8IRms/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/TACyctz-c7k" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/5424100688292430178?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/5424100688292430178?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/TACyctz-c7k/your-own-java-classes.html" title="Your Own Java Classes" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/05/your-own-java-classes.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEADR3w6fip7ImA9WhZWF0k.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-5700286505227258184</id><published>2011-05-13T11:35:00.000-07:00</published><updated>2011-05-18T11:19:36.216-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-18T11:19:36.216-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="collection" /><category scheme="http://www.blogger.com/atom/ns#" term="array" /><category scheme="http://www.blogger.com/atom/ns#" term="application" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="read" /><category scheme="http://www.blogger.com/atom/ns#" term="output" /><category scheme="http://www.blogger.com/atom/ns#" term="file" /><category scheme="http://www.blogger.com/atom/ns#" term="input" /><category scheme="http://www.blogger.com/atom/ns#" term="i/o" /><category scheme="http://www.blogger.com/atom/ns#" term="beginner" /><category scheme="http://www.blogger.com/atom/ns#" term="variables" /><title>A Java CSV File Reader</title><content type="html">One of the most common types of data file is a CSV (Comma Separated Value) file. They can be exported by many popular applications, notable spreadsheet programs like Excel and Numbers. They are easy to read into your Java programs once you know how.&lt;br /&gt;&lt;br /&gt;Reading the file is as simple as &lt;a href="http://beginwithjava.blogspot.com/2011/05/java-file-save-and-file-load-text.html"&gt;reading a text file&lt;/a&gt;. The file has to be opened, a BufferedReader object is created to read the data in a line at a time.&lt;br /&gt;&lt;br /&gt;Once a line of data has been read, we make sure that it's not &lt;tt&gt;null&lt;/tt&gt;, or empty. If it is, we've hit the end of the file and there's no more data to read. If it isn't, we then use the split() method that's a member of Java's String object. This will split a string into an array of Strings using a delimiter that we give it.&lt;br /&gt;&lt;br /&gt;The delimiter for a CSV file is a comma, of course. Once we've split() the string, we have all the element in an Array from which our Java programs can use the data. For this example, I just use a &lt;tt&gt;for&lt;/tt&gt; loop to print out the data, but I could just as well sort on the values of one of the cells, or whatever I need to do with it in my program.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;The Steps&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Open the file with a BufferedReader object to read it a line at a time.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Check to see if we've got actual data to make sure we haven't finished the file.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Split the line we read into an &lt;i&gt;Array&lt;/i&gt; of String using String.split()&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Use our data.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;The Program&lt;/b&gt;&lt;br /&gt;&lt;pre style="color:#0077ff;"&gt;&lt;span style="color:#00ff77;"&gt;// CSVRead.java&lt;br /&gt;//Reads a Comma Separated Value file and prints its contents.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;import java.util.Arrays;&lt;br /&gt;&lt;br /&gt;public class CSVRead{&lt;br /&gt;&lt;br /&gt; public static void main(String[] arg) throws Exception {&lt;br /&gt;&lt;br /&gt;  BufferedReader CSVFile = &lt;br /&gt;        new BufferedReader(new FileReader("Example.csv"));&lt;br /&gt;&lt;br /&gt;  String dataRow = CSVFile.readLine(); &lt;span style="color:#00ff77;"&gt;// Read first line.&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#00ff77;"&gt;  // The while checks to see if the data is null. If &lt;br /&gt;  // it is, we've hit the end of the file. If not, &lt;br /&gt;  // process the data.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  while (dataRow != null){&lt;br /&gt;   String[] dataArray = dataRow.split(",");&lt;br /&gt;   for (String item:dataArray) { &lt;br /&gt;      System.out.print(item + "\t"); &lt;br /&gt;   }&lt;br /&gt;   System.out.println(); // Print the data line.&lt;br /&gt;   dataRow = CSVFile.readLine(); &lt;span style="color:#00ff77;"&gt;// Read next line of data.&lt;/span&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;span style="color:#00ff77;"&gt;  // Close the file once all data has been read.&lt;/span&gt;&lt;br /&gt;  CSVFile.close();&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#00ff77;"&gt;  // End the printout with a blank line.&lt;/span&gt;&lt;br /&gt;  System.out.println();&lt;br /&gt;&lt;br /&gt; } &lt;span style="color:#00ff77;"&gt;//main()&lt;/span&gt;&lt;br /&gt;} &lt;span style="color:#00ff77;"&gt;// CSVRead&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Downloads&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://saundby.com/beginwithjava/code/CSVRead.java"&gt;This program&lt;/a&gt;, and &lt;a href="http://saundby.com/beginwithjava/code/Example.csv"&gt;an example CSV file&lt;/a&gt; to use it with (a section of a spreadsheed I use to keep track of my integrated circuits) are available at &lt;a href="http://saundby.com/beginwithjava/index.shtml"&gt;my code archive&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Writing to CSV Files with Java&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Writing to a CSV file is as simple as &lt;a href="http://beginwithjava.blogspot.com/2011/05/java-file-save-and-file-load-text.html"&gt;writing a text file&lt;/a&gt;. In this case, we write a comma between each field, and a newline at the end of each record.&lt;br /&gt;&lt;br /&gt;Give it a try, starting with &lt;a href="http://saundby.com/beginwithjava/code/TextSave.java"&gt;TextSave.java&lt;/a&gt;, modify it appropriately, then see what your favorite spreadsheet program thinks of the results.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-5700286505227258184?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/RpJ8Eb1TX3ntnRq26WETj1k7Uyc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RpJ8Eb1TX3ntnRq26WETj1k7Uyc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/RpJ8Eb1TX3ntnRq26WETj1k7Uyc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RpJ8Eb1TX3ntnRq26WETj1k7Uyc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/4r5JMSJkE9c" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/5700286505227258184?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/5700286505227258184?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/4r5JMSJkE9c/java-csv-file-reader.html" title="A Java CSV File Reader" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/05/java-csv-file-reader.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0cDR38zfip7ImA9WhZXF0w.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-2080885946092751334</id><published>2011-05-06T12:58:00.000-07:00</published><updated>2011-05-06T13:51:16.186-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-06T13:51:16.186-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="packages" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="output" /><category scheme="http://www.blogger.com/atom/ns#" term="file" /><category scheme="http://www.blogger.com/atom/ns#" term="input" /><category scheme="http://www.blogger.com/atom/ns#" term="i/o" /><category scheme="http://www.blogger.com/atom/ns#" term="variables" /><title>Java File Save and File Load: Text</title><content type="html">We've looked at &lt;a href="http://beginwithjava.blogspot.com/2011/04/java-file-save-and-file-load-objects.html"&gt;saving and loading objects&lt;/a&gt; to files. If we need to exchange information for use by a different program than our own it will seldom be convenient to save objects. Text files are commonly used to do this.&lt;br /&gt;&lt;br /&gt;Text files are even simpler to deal with than Object files, thanks to classes in the java.io package. FileWriter gives us everything we need to write from our program to a text file. All we need to do is feed it String data.&lt;br /&gt;&lt;br /&gt;FileReader lets us access a file, but using a BufferedReader makes it a lot easier to handle reading data from a file as lines of text.&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#77ff00;"&gt;As with object files, the basic steps are:&lt;br /&gt;1. Open a file.&lt;br /&gt;2. Write or read data.&lt;br /&gt;3. Close the file.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here are example programs, the first writes a simple text file. After you run it, you can take a look at the file it creates (in the same directory) using your favorite text viewer. You'll see normal text written line by line.&lt;br /&gt;&lt;br /&gt;Then run the second program. It reads in the information line by line. It takes advantage of the fact that we know the format of the data file to read what's in it back into Java objects. You can do the same thing with any file that you either know the format of, or can detect the format of. For example, reading data from CSV files saved by spreadsheets (I'll provide a specific example of this in a future article.)&lt;br /&gt;&lt;br /&gt;As always, you can download the program files from &lt;a href="http://saundby.com/beginwithjava/index.shtml"&gt;the Begin With Java Code Archive&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;&lt;a href="http://saundby.com/beginwithjava/code/TextSave.java"&gt;TextSave.java&lt;/a&gt;&lt;/h3&gt;&lt;pre style="color:#0077ff;"&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;&lt;br /&gt;public class TextSave{&lt;br /&gt;&lt;br /&gt;  public static void main(String[] arg) throws Exception {&lt;br /&gt;    // Create some data to write.&lt;br /&gt;    int x=1, y=2, z=3;&lt;br /&gt;    String name = "Galormadron", race = "elf";&lt;br /&gt;    boolean hyperactive = true;&lt;br /&gt;&lt;br /&gt;    // Set up the FileWriter with our file name.&lt;br /&gt;    FileWriter saveFile = new FileWriter("TextSave.txt");&lt;br /&gt;&lt;br /&gt;    // Write the data to the file.&lt;br /&gt;    saveFile.write("\n");&lt;br /&gt;    saveFile.write(x + "\n");&lt;br /&gt;    saveFile.write(y + "\n");&lt;br /&gt;    saveFile.write(z + "\n");&lt;br /&gt;    saveFile.write(name + "\n");&lt;br /&gt;    saveFile.write(race + "\n");&lt;br /&gt;    saveFile.write(Boolean.toString(hyperactive) + "\n");&lt;br /&gt;    saveFile.write("\n");&lt;br /&gt;&lt;br /&gt;    // All done, close the FileWriter.&lt;br /&gt;    saveFile.close();&lt;br /&gt;&lt;br /&gt;  } //main()&lt;br /&gt;} // TextSave&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;&lt;a href="http://saundby.com/beginwithjava/code/TextRead.java"&gt;TextRead.java&lt;/a&gt;&lt;/h3&gt;&lt;pre style="color:#00ff77;"&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;&lt;br /&gt;public class TextRead{&lt;br /&gt;&lt;br /&gt;  public static void main(String[] arg) throws Exception {&lt;br /&gt;    int x, y, z;&lt;br /&gt;    String name = "", race = "";&lt;br /&gt;    boolean hyperactive;&lt;br /&gt;&lt;br /&gt;    BufferedReader saveFile=&lt;br /&gt;        new BufferedReader(new FileReader("TextSave.txt"));&lt;br /&gt;&lt;br /&gt;    // Throw away the blank line at the top.&lt;br /&gt;    saveFile.readLine(); &lt;br /&gt;    // Get the integer value from the String.&lt;br /&gt;    x = Integer.parseInt(saveFile.readLine()); &lt;br /&gt;    y = Integer.parseInt(saveFile.readLine());&lt;br /&gt;    z = Integer.parseInt(saveFile.readLine());&lt;br /&gt;    name = saveFile.readLine();&lt;br /&gt;    race = saveFile.readLine();&lt;br /&gt;    hyperactive = Boolean.parseBoolean(saveFile.readLine());&lt;br /&gt;    // Not needed, but read blank line at the bottom.&lt;br /&gt;    saveFile.readLine(); &lt;br /&gt;&lt;br /&gt;    saveFile.close();&lt;br /&gt;&lt;br /&gt;    // Print out the values.&lt;br /&gt;    System.out.println("x=" + x + " y=" + y + " z=" + z + "\n");&lt;br /&gt;    System.out.println("name: " + name + " race: " + race + "\n");&lt;br /&gt;    if (hyperactive) &lt;br /&gt;      System.out.println("Oh, yeah. He's hyperactive all right.");&lt;br /&gt;    else System.out.println("What a mellow dude.");&lt;br /&gt;    System.out.println();&lt;br /&gt;&lt;br /&gt;  } //main()&lt;br /&gt;} // TextRead&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-2080885946092751334?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/98CcQ4eZE3ZvNvom8NLl8y68A4U/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/98CcQ4eZE3ZvNvom8NLl8y68A4U/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/98CcQ4eZE3ZvNvom8NLl8y68A4U/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/98CcQ4eZE3ZvNvom8NLl8y68A4U/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/G0Ef9wadmus" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/2080885946092751334?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/2080885946092751334?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/G0Ef9wadmus/java-file-save-and-file-load-text.html" title="Java File Save and File Load: Text" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/05/java-file-save-and-file-load-text.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUQCQXc9eCp7ImA9WhZRGEw.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-1805513051637340135</id><published>2011-04-14T12:51:00.000-07:00</published><updated>2011-04-14T13:36:00.960-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-14T13:36:00.960-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="example" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="restore" /><category scheme="http://www.blogger.com/atom/ns#" term="read" /><category scheme="http://www.blogger.com/atom/ns#" term="output" /><category scheme="http://www.blogger.com/atom/ns#" term="file" /><category scheme="http://www.blogger.com/atom/ns#" term="input" /><category scheme="http://www.blogger.com/atom/ns#" term="i/o" /><category scheme="http://www.blogger.com/atom/ns#" term="save" /><title>Java File Save and File Load: Objects</title><content type="html">&lt;a href="#restore"&gt;&lt;tt&gt;Jump to Reading Data from Files with Java&amp;gt;&amp;gt;&lt;/tt&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a name="save"&gt;&lt;h3&gt;Saving Data to Files with Java&lt;/h3&gt;&lt;/a&gt;&lt;br /&gt;Saving objects to a file in Java has a few steps to it, but it's pretty easy. We open a file to write to, create a "stream" for putting objects into the file, write the objects to that stream to put them in the file, then close the stream and file when we're done.&lt;br /&gt;&lt;br /&gt;To reiterate:&lt;br /&gt;&lt;span style="color:#77dd00;"&gt;&lt;br /&gt;1. Open a file.&lt;br /&gt;&lt;br /&gt;2. Open an object stream to the file.&lt;br /&gt;&lt;br /&gt;3. Write the objects to that stream.&lt;br /&gt;&lt;br /&gt;4. Close the stream and file.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;1. Opening the File&lt;/b&gt;&lt;br /&gt;To open a file for writing, we use a FileOutputStream object. When we construct the new FileOutputStream, we give it a file name to open. If the file name exists, it opens the existing file. Otherwise, it creates a new file. To keep things simple, we're not going to check for a file existing here, or anything like that.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;span style="color:#00aa00;"&gt;FileOutputStream saveFile = new FileOutputStream("saveFile.sav");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;2. Open an Object Stream&lt;/b&gt;&lt;br /&gt;To write objects to the FileOutputStream, we create an ObjectOutputStream. We give it the FileOutputStream object we've set up when we construct the new ObjectOutputStream object.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;span style="color:#00aa00;"&gt;ObjectOutputStream save = ObjectOutputStream(saveFile);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;3. Write Objects&lt;/b&gt;&lt;br /&gt;When we write objects to the ObjectOutputStream, they are sent out through it to the FileOutputStream and into the file.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;span style="color:#00aa00;"&gt;save.writeObject(objectToSave);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;4. Close Up&lt;/b&gt;&lt;br /&gt;When we close the ObjectOutputStream, it'll also close our FileOutputStream for us, so this is just one step.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;span style="color:#00aa00;"&gt;save.close();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Example Program:&lt;br /&gt;&lt;pre style="color:#0077ee;font-size:9pt;"&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;&lt;br /&gt;public class SaveObjects{&lt;br /&gt;&lt;br /&gt;  public static void main(String[] arg){&lt;br /&gt;&lt;br /&gt;    // Create some data objects for us to save.&lt;br /&gt;    boolean powerSwitch=true;&lt;br /&gt;    int x=9, y=150, z= 675;&lt;br /&gt;    String name="Galormadron", setting="on", plant="rutabaga";&lt;br /&gt;    ArrayList&lt;String&gt; stuff = new ArrayList&lt;String&gt;();&lt;br /&gt;    stuff.add("One");&lt;br /&gt;    stuff.add("Two");&lt;br /&gt;    stuff.add("Three");&lt;br /&gt;    stuff.add("Four");&lt;br /&gt;    stuff.add("Five");&lt;br /&gt;&lt;br /&gt;    try{  // Catch errors in I/O if necessary.&lt;br /&gt;      // Open a file to write to, named SavedObjects.sav.&lt;br /&gt;      FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");&lt;br /&gt;&lt;br /&gt;      // Create an ObjectOutputStream to put objects into save file.&lt;br /&gt;      ObjectOutputStream save = new ObjectOutputStream(saveFile);&lt;br /&gt;&lt;br /&gt;      // Now we do the save.&lt;br /&gt;      save.writeObject(powerSwitch);&lt;br /&gt;      save.writeObject(x);&lt;br /&gt;      save.writeObject(y);&lt;br /&gt;      save.writeObject(z);&lt;br /&gt;      save.writeObject(name);&lt;br /&gt;      save.writeObject(setting);&lt;br /&gt;      save.writeObject(plant);&lt;br /&gt;      save.writeObject(stuff);&lt;br /&gt;&lt;br /&gt;      // Close the file.&lt;br /&gt;      save.close(); // This also closes saveFile.&lt;br /&gt;    }&lt;br /&gt;    catch(Exception exc){&lt;br /&gt;      exc.printStackTrace(); // If there was an error, print the info.&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="restore"&gt;&lt;h3&gt;Reading Data Files with Java&lt;/h3&gt;&lt;/a&gt;&lt;br /&gt;Now we need to know how to get data back from a file with Java. Since the data objects were stored using an ObjectOutputStream, they are saved in a way that makes them easy to read back using an ObjectInputStream.&lt;br /&gt;&lt;br /&gt;There's one problem with ObjectImputStream, however. It doesn't return objects of the specific classes that they were originally. It just returns generic Objects. To get things back into their original class, we have to cast them into that type. Usually that means we need to know what their class was when they were written. It's possible to save class information along with the data, but in this case we just assume that we're reading back a file we have written, so we already know what class everything should be. I'll go into the details of reading other files in another article.&lt;br /&gt;&lt;br /&gt;The steps are practically the same as for writing objects to a file:&lt;br /&gt;&lt;span style="color:#77dd00;"&gt;&lt;br /&gt;1. Open a file.&lt;br /&gt;&lt;br /&gt;2. Open an object stream from the file.&lt;br /&gt;&lt;br /&gt;3. Read the objects to that stream.&lt;br /&gt;&lt;br /&gt;4. Close the stream and file.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;1. Opening the File&lt;/b&gt;&lt;br /&gt;To open a file for &lt;i&gt;reading&lt;/i&gt;, we use a FileInputStream object. When we construct the new FileIntputStream, we give it a file name to open. If the file name exists, it opens the existing file. Otherwise, we get an error which will print out a nastygram when we get to our catch statement. To keep things simple, we're not going to check for a file existing here.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;span style="color:#00aa00;"&gt;FileInputStream saveFile = new FileInputStream("saveFile.sav");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;2. Open an Object Stream&lt;/b&gt;&lt;br /&gt;To read objects to the FileInputStream, we create an ObjectInputStream. We give it the FileInputStream object we've set up when we construct the new ObjectInputStream.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;span style="color:#00aa00;"&gt;ObjectInputStream restore = ObjectInputStream(saveFile);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;3. Read Objects&lt;/b&gt;&lt;br /&gt;When we read objects from the ObjectInputStream, it gets then from the file.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;span style="color:#00aa00;"&gt;Object obj = restore.readObject();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;3 and a half. Cast Back to Class&lt;/b&gt;&lt;br /&gt;Step 3. only restores a generic Object. If we know the original class, we should cast the Object back to its original class when we read it. If we had stored a String object, we'd read it something like this:&lt;br /&gt;&lt;span style="color:#00aa00;"&gt;String name = (String) restore.readObject();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;4. Close Up&lt;/b&gt;&lt;br /&gt;When we close the ObjectInputStream, it'll also close our FileInputStream for us, so this is just one step.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;span style="color:#00aa00;"&gt;restore.close();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here's an example program to read back information saved by the first example program:&lt;br /&gt;&lt;pre style="color:#0077ee;font-size:9pt;"&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;&lt;br /&gt;public class RestoreObjects{&lt;br /&gt;&lt;br /&gt; public static void main(String[] arg){&lt;br /&gt;&lt;br /&gt;  // Create the data objects for us to restore.&lt;br /&gt;  boolean powerSwitch=false;&lt;br /&gt;  int x=0, y=0, z=0;&lt;br /&gt;  String name="", setting="", plant="";&lt;br /&gt;  ArrayList&lt;String&gt; stuff = new ArrayList&lt;String&gt;();&lt;br /&gt;  &lt;br /&gt;  // Wrap all in a try/catch block to trap I/O errors.&lt;br /&gt;  try{&lt;br /&gt;   // Open file to read from, named SavedObjects.sav.&lt;br /&gt;   FileInputStream saveFile = new FileInputStream("SaveObj.sav");&lt;br /&gt;&lt;br /&gt;   // Create an ObjectInputStream to get objects from save file.&lt;br /&gt;   ObjectInputStream save = new ObjectInputStream(saveFile);&lt;br /&gt;&lt;br /&gt;   // Now we do the restore.&lt;br /&gt;   // readObject() returns a generic Object, we cast those back&lt;br /&gt;   // into their original class type.&lt;br /&gt;   // For primitive types, use the corresponding reference class.&lt;br /&gt;   powerSwitch = (Boolean) save.readObject();&lt;br /&gt;   x = (Integer) save.readObject();&lt;br /&gt;   y = (Integer) save.readObject();&lt;br /&gt;   z = (Integer) save.readObject();&lt;br /&gt;   name = (String) save.readObject();&lt;br /&gt;   setting = (String) save.readObject();&lt;br /&gt;   plant = (String) save.readObject();&lt;br /&gt;   stuff = (ArrayList&lt;String&gt;) save.readObject();&lt;br /&gt;&lt;br /&gt;   // Close the file.&lt;br /&gt;   save.close(); // This also closes saveFile.&lt;br /&gt;  }&lt;br /&gt;  catch(Exception exc){&lt;br /&gt;   exc.printStackTrace(); // If there was an error, print the info.&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  // Print the values, to see that they've been recovered.&lt;br /&gt;  System.out.println("\nRestored Object Values:\n");&lt;br /&gt;  System.out.println("\tpowerSwitch: " + powerSwitch);&lt;br /&gt;  System.out.println("\tx=" + x + " y=" + y + " z=" + z);&lt;br /&gt;  System.out.println("\tname: " + name);&lt;br /&gt;  System.out.println("\tsetting: " + setting);&lt;br /&gt;  System.out.println("\tplant: " + plant);&lt;br /&gt;  System.out.println("\tContents of stuff: ");&lt;br /&gt;  System.out.println("\t\t" + stuff);&lt;br /&gt;  System.out.println();&lt;br /&gt;&lt;br /&gt;  // All done.&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="#save"&gt;&amp;lt;&amp;lt;Return to How to Save to a File with Java&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The example programs can be downloaded at:&lt;br /&gt;&lt;a href="http://saundby.com/beginwithjava/"&gt;Begin With Java Code Downloads&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-1805513051637340135?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/P66dB1tHH5MeHehNaN-OYOWkKOE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/P66dB1tHH5MeHehNaN-OYOWkKOE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/P66dB1tHH5MeHehNaN-OYOWkKOE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/P66dB1tHH5MeHehNaN-OYOWkKOE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/KSw-zfNTAFQ" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/1805513051637340135?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/1805513051637340135?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/KSw-zfNTAFQ/java-file-save-and-file-load-objects.html" title="Java File Save and File Load: Objects" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/04/java-file-save-and-file-load-objects.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Dk4BSXk5eSp7ImA9WhZTFks.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-46492333077886577</id><published>2011-03-20T16:22:00.000-07:00</published><updated>2011-03-20T16:49:18.721-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-20T16:49:18.721-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Java version" /><category scheme="http://www.blogger.com/atom/ns#" term="reference" /><category scheme="http://www.blogger.com/atom/ns#" term="JDK" /><category scheme="http://www.blogger.com/atom/ns#" term="Resources" /><title>Cleaning Out the Sun References</title><content type="html">Today I'm going to start cleaning out the references in old posts to Sun Microsystems. Sun is gone now, except for a page that tells you it's gone and points toward Oracle. Oracle bought Sun, and now owns Java.&lt;br /&gt;&lt;br /&gt;Many of my old articles point toward the previous Sun website. I'll be changing those to point to current locations for the same resources, mostly to be found on &lt;a href="http://java.net/"&gt;java.net&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;If you come across something I appear to have missed, feel free to email me with the article title so that I can fix it. Thanks!&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;One More Thing&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;It's worth noting that since some of these articles were written, the official online resources have improved. I still think there are some major gotchas for new learners in Java, but things are definitely better than when I started this blog.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-46492333077886577?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Ga7n39gGM5s4BNj8eET-wiEfPuM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Ga7n39gGM5s4BNj8eET-wiEfPuM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Ga7n39gGM5s4BNj8eET-wiEfPuM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Ga7n39gGM5s4BNj8eET-wiEfPuM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/4NVp-ppZ44k" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/46492333077886577?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/46492333077886577?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/4NVp-ppZ44k/cleaning-out-sun-references.html" title="Cleaning Out the Sun References" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/03/cleaning-out-sun-references.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0EGRXg-cCp7ImA9Wx9WGUU.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-2347481635657183349</id><published>2011-01-25T11:52:00.000-08:00</published><updated>2011-01-25T13:07:04.658-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-25T13:07:04.658-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="file names" /><category scheme="http://www.blogger.com/atom/ns#" term="packages" /><category scheme="http://www.blogger.com/atom/ns#" term="application" /><category scheme="http://www.blogger.com/atom/ns#" term="signature" /><category scheme="http://www.blogger.com/atom/ns#" term="classes" /><category scheme="http://www.blogger.com/atom/ns#" term="command line" /><category scheme="http://www.blogger.com/atom/ns#" term="name" /><category scheme="http://www.blogger.com/atom/ns#" term="source code" /><category scheme="http://www.blogger.com/atom/ns#" term="JVM" /><category scheme="http://www.blogger.com/atom/ns#" term="declaration" /><category scheme="http://www.blogger.com/atom/ns#" term="public" /><category scheme="http://www.blogger.com/atom/ns#" term="arguments" /><category scheme="http://www.blogger.com/atom/ns#" term="environment" /><title>Java's File Names and Class Names</title><content type="html">Java is picky about the file names you use.&lt;br /&gt;&lt;br /&gt;Each source file can contain one public class. The source file's name has to be the name of that class. By convention, the source file uses a &lt;b&gt;&lt;tt&gt;.java&lt;/tt&gt;&lt;/b&gt; filename extension (a tail end of a file name that marks the file as being of a particular type of file.)&lt;br /&gt;&lt;br /&gt;So, for a class declared as such:&lt;br /&gt;&lt;pre style="color:#ff0000;"&gt;&lt;br /&gt;public class HelloWorld{&lt;br /&gt;...&lt;/pre&gt;&lt;br /&gt;The file it is stored in should be named &lt;b&gt;&lt;tt&gt;HelloWorld.java&lt;/tt&gt;&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;The capitalization should be the same in both cases. Some operating systems don't notice whether file names are capitalized or not. It doesn't matter, you should be in the habit of using the correct capitalization in case you work on a system that does care about capitalization.&lt;br /&gt;&lt;br /&gt;When you compile your file with javac, you pass the full file name to javac:&lt;br /&gt;&lt;br /&gt;&lt;pre style="color:#00ff00;"&gt;javac HelloWorld.java&lt;/pre&gt;&lt;br /&gt;Let's say we save the file under a different name. We write the following program:&lt;br /&gt;&lt;br /&gt;&lt;pre style="color:#00ffff;"&gt;public class HelloThere{&lt;br /&gt;    public static void main(String arg[]){&lt;br /&gt;        System.out.println("Hello There.");&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Then we save it as HelloWorld.java, and try to compile it:&lt;br /&gt;&lt;pre  style="color:#ff0000; font-size: 9pt;"&gt;&lt;br /&gt;&gt;javac HelloWorld.java&lt;br /&gt;FileName.java:1: class HelloThere is public, should be declared &lt;br /&gt;in a file named HelloThere.java&lt;br /&gt;public class HelloThere{&lt;br /&gt;       ^&lt;br /&gt;1 error&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Java lets us know that it won't compile until we rename the file appropriately (according to its rules.)&lt;br /&gt;&lt;br /&gt;So let's rename the file. Let's call it &lt;b&gt;&lt;tt&gt;HelloThere.javasource&lt;/tt&gt;&lt;/b&gt;. Seems a bit more explicit than just &lt;b&gt;&lt;tt&gt;.java&lt;/b&gt;&lt;/tt&gt;, right? Let's run the compiler:&lt;br /&gt;&lt;br /&gt;&lt;pre  style="color:#ff0000; font-size: 9pt;"&gt;&lt;br /&gt;&gt;javac HelloThere.javasource &lt;br /&gt;error: Class names, 'HelloThere.javasource', are only accepted &lt;br /&gt;if annotation processing is explicitly requested&lt;br /&gt;1 error&lt;/pre&gt;&lt;br /&gt;Java's still not happy with us. Annotation processing? That's when we include extra information in the program about the program itself. We're not bothering with that just now. So we should just name the file &lt;b&gt;&lt;tt&gt;HelloThere.java&lt;/tt&gt;&lt;/b&gt;, and not get fancy with our file names.&lt;br /&gt;&lt;br /&gt;But, under the right circumstances, javac &lt;i&gt;does&lt;/i&gt; allow file name extensions other than &lt;b&gt;&lt;tt&gt;.java&lt;/b&gt;&lt;/tt&gt;. That's why we always type in the full file name, including &lt;b&gt;&lt;tt&gt;.java&lt;/b&gt;&lt;/tt&gt;, when we use javac. We say 'javac HelloThere.java', not just 'javac HelloThere'. Javac can't assume that we mean a &lt;b&gt;&lt;tt&gt;.java&lt;/b&gt;&lt;/tt&gt; file, though that's what it will usually be.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ffff00;"&gt;The Class File&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Once we make javac happy with a proper file name, and a program with no errors, javac produces a new file. This file will have the original file name, but with &lt;b&gt;&lt;tt&gt;.java&lt;/b&gt;&lt;/tt&gt; replaced with &lt;b&gt;&lt;tt&gt;.class&lt;/b&gt;&lt;/tt&gt;. This is your &lt;i&gt;bytecode&lt;/i&gt; file, the file that the Java Virtual Machine can run.&lt;br /&gt;&lt;br /&gt;When we run the program with Java, we're running the .class file. In the case of HelloThere, we're running the &lt;b&gt;&lt;tt&gt;HelloThere.class&lt;/b&gt;&lt;/tt&gt; file. But we don't type in the full file name. Why?&lt;br /&gt;&lt;br /&gt;Unlike javac, java &lt;i&gt;requires&lt;/i&gt; a .class file. That's all it will work with. There's no opportunity to have a different extension to the file name. So it &lt;i&gt;assumes&lt;/i&gt; the .class part of the file name. But that's not the whole story.&lt;br /&gt;&lt;br /&gt;If you add .class yourself, here's what you'll get:&lt;br /&gt;&lt;pre style="color:#ff0000; font-size: 9pt;"&gt;&lt;br /&gt;&gt;java HelloThere.class&lt;br /&gt;Exception in thread "main" java.lang.NoClassDefFoundError: &lt;br /&gt; HelloThere/class&lt;br /&gt;Caused by: java.lang.ClassNotFoundException: HelloThere.class&lt;br /&gt; at java.net.URLClassLoader$1.run(URLClassLoader.java:202)&lt;br /&gt; at java.security.AccessController.doPrivileged(Native Method)&lt;br /&gt; at java.net.URLClassLoader.findClass(URLClassLoader.java:190)&lt;br /&gt; at java.lang.ClassLoader.loadClass(ClassLoader.java:307)&lt;br /&gt; at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)&lt;br /&gt; at java.lang.ClassLoader.loadClass(ClassLoader.java:248)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Pretty ugly. What we're actually doing when we type "&lt;tt&gt;java HelloThere&lt;/tt&gt;" is telling Java to run the &lt;i&gt;class&lt;/i&gt; &lt;tt&gt;HelloThere&lt;/tt&gt;. Java assumes that it will find this in a file called "&lt;tt&gt;HelloThere.class&lt;/tt&gt;", so that's what it's looking for first. &lt;br /&gt;&lt;br /&gt;We're &lt;i&gt;not&lt;/i&gt; telling Java to run the &lt;i&gt;file&lt;/i&gt; &lt;tt&gt;HelloThere.class&lt;/tt&gt;, we're telling it to run the &lt;i&gt;class&lt;/i&gt; HelloThere, which it expects to find in the file &lt;tt&gt;HelloThere.class&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;But what if we ask for another class that doesn't have its own .class file?&lt;br /&gt;&lt;br /&gt;Just for fun, let's change HelloThere.java like this, and see what happens:&lt;br /&gt;&lt;pre style="color:#00ffff;"&gt;public class HelloThere{&lt;br /&gt; public static void main(String[] arg){&lt;br /&gt;  System.out.println("Hello.");&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#ffff00;"&gt;class HelloZoik{&lt;br /&gt; public static void main(String[] arg){&lt;br /&gt;  System.out.println("Zoiks!");&lt;br /&gt; }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;After we edit it, we compile with '&lt;tt&gt;javac HelloThere.java&lt;/tt&gt;' and hold our breath.&lt;br /&gt;&lt;br /&gt;Hurray! No errors!&lt;br /&gt;&lt;br /&gt;Now we have a second class, HelloZoik, in the &lt;tt&gt;HelloThere.class&lt;/tt&gt; file. Can Java find it?&lt;br /&gt;&lt;br /&gt;Let's try:&lt;br /&gt;&lt;pre  style="color:#00ff00;"&gt; java HelloZoik&lt;br /&gt;Zoiks!&lt;/pre&gt;&lt;br /&gt;It worked! Java found our class inside &lt;tt&gt;HelloThere.class&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;This shows it's not the file name that we're calling with the 'java' command, it's the class name.&lt;br /&gt;&lt;br /&gt;If Java doesn't find the class inside a file with the same name as the class followed by .class, it'll look in the other .class files available.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-2347481635657183349?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/8yAoPMRXIjJOEdY7SU6v0vu5BnI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8yAoPMRXIjJOEdY7SU6v0vu5BnI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/8yAoPMRXIjJOEdY7SU6v0vu5BnI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/8yAoPMRXIjJOEdY7SU6v0vu5BnI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/khH5sMajzWQ" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/2347481635657183349?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/2347481635657183349?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/khH5sMajzWQ/javas-file-names-and-class-names.html" title="Java's File Names and Class Names" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/01/javas-file-names-and-class-names.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkQBQX49fyp7ImA9Wx9XEkw.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-8668902242557385461</id><published>2011-01-04T23:13:00.000-08:00</published><updated>2011-01-04T23:59:10.067-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-04T23:59:10.067-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="application" /><category scheme="http://www.blogger.com/atom/ns#" term="why java" /><category scheme="http://www.blogger.com/atom/ns#" term="Java version" /><category scheme="http://www.blogger.com/atom/ns#" term="JVM" /><title>Should I Still Learn Java?</title><content type="html">With all the controversy surrounding Java thanks to the purchase of Sun by Oracle, the lawsuits flying back and forth over the Java Community Process, the Apache Foundation, Android, and all the rest, &lt;i style="color:#00FF77;"&gt;does it still make sense to learn Java?&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;After all, the demise and abandonment of Java is being predicted practically every day.&lt;br /&gt;&lt;br /&gt;I say &lt;b&gt;Yes&lt;/b&gt;, now is the time to learn Java. No matter what your programming skill or background, Java is a valuable language to learn, it will be used and useful for a &lt;i&gt;long&lt;/i&gt; time to come.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Never a Dull Moment&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The Java language has been a-swirl in controversy since its public announcement. It has not become "the" language in many of the areas it originally claimed to be "the" language to use, but yet it &lt;i&gt;has&lt;/i&gt; become popular in many other areas. In fact, Java is neck and neck with the language C for being the most popular computer language:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html"&gt;TIOBE Software Community Index&lt;/a&gt; (of most  popular programming languages.)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://langpop.com/"&gt;langpop.com&lt;/a&gt; Programming Language Popularity Rankings.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.devtopics.com/most-popular-programming-languages/"&gt;Devtopics.com&lt;/a&gt; Most Popular Programming Languages.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Why Popularity Matters&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Why does popularity matter? Because it entrenches a programming language, not just for now, but for many years to come. In fact, every language that has ever become deeply entrenched is still with us, so there's no way of knowing just how long popularity will keep a programming language alive.&lt;br /&gt;&lt;br /&gt;When I was first learning to program, the big languages were assembly (the "&lt;i&gt;real&lt;/i&gt;" programmer's language of the time), BASIC, FORTRAN, and COBOL. Every single one of those is still a viable language today. Though if you'd asked me then, I would never have thought COBOL would still be with us today. I would never have believed how popular it is, either.&lt;br /&gt;&lt;br /&gt;But COBOL was re-invented in the late 80's. And there were a lot of big-money installations running on it. ADA failed to displace it. It's still here, and it's still a valuable part of a programmer's resume for many jobs.&lt;br /&gt;&lt;br /&gt;FORTRAN is a language I expected to re-invent itself. It had already done so by the time I learned it (I first programmed in the original FORTRAN, but FORTRAN IV was already in common use.) But with the promulgation of Pascal, Modula-2, and C in the 80's I figured FORTRAN would be pushed into the recesses of obscurity. I was wrong.&lt;br /&gt;&lt;br /&gt;Modula-2 was mishandled by the company that owned the rights to it, so it never took off as well as it might have. Pascal took off even though it was never intended to be anything but a classroom language. C took off since it didn't have Modula-2's licensing disadvantages and it had enough of its advantages to become the "next generation language" of its day.&lt;br /&gt;&lt;br /&gt;Modula-2 is still with us, but it's insignificant among languages today. Because it never got popular. The others I mentioned got popular, and they're still popular today. Yeah, even Pascal. You could learn Pascal today and do a lot with it (though I don't recommend it unless you want the academic challenge of broadening yourself as a programmer.) I still write software in Pascal, though mostly for my own use, and only for older computer systems.&lt;br /&gt;&lt;br /&gt;The key to a programming language's longevity is popularity. Once a language becomes sufficiently popular, for all practical purposes it will never die.&lt;br /&gt;&lt;br /&gt;Java is that popular.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;The Many Javas&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Java has become deeply ingrained into the modern computer infrastructure. Not only does the &lt;a href="http://beginwithjava.blogspot.com/2008/07/java-virtual-machine-adapter-cables-for.html"&gt;Java Virtual Machine&lt;/a&gt; support a lot more languages than Java itself, but Java has spawned other languages so close to itself that if you know Java you can pick up the other languages without significant effort. C# is the most popular of these spin off languages.&lt;br /&gt;&lt;br /&gt;Plus, there are different versions of Java. The Mobile Edition, used on cell phones, smart phones, and PDAs, is a major programming language for these platforms. Each of these platforms has its individual programming suite, and associated language. Their second language, the Esperanto of the portable world, is Java. Programmers that want their software to move easily between platforms often choose to write their code in Java.&lt;br /&gt;&lt;br /&gt;The use of Java on servers is rife as well. Java Enterprise Edition became the most popular use of the Java language when Java was still struggling to be used as an applications programming language over a decade ago. Some say that Java on the server saved the Java language. Certainly this is what makes Java a good language to learn for professional reasons.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;The Most Important Reason&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The most important reason to ignore all the hullabaloo about Java's impending demise and not worry about learning it is that:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;it is a good language that's fairly easy to learn,&lt;/li&gt;&lt;br /&gt;&lt;li&gt;expressive enough to do a lot of different things effectively, &lt;/li&gt;&lt;br /&gt;&lt;li&gt;easy to develop sophisticated modern programs in&lt;/li&gt;&lt;br /&gt;&lt;li&gt;without too much work for an individual or small group of developers,&lt;/li&gt;&lt;br /&gt;&lt;li&gt;gives access to all the important parts of the machine (graphics, sound, filesystem, peripherals)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;and what you learn travels well to other languages when you go on to learn them.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;It's not going to go away any time soon. There's too much momentum. There's no need to worry. Ever since the launch of Java I've heard that it's going to be gone or unusable tomorrow. History shows that just doesn't happen to popular programming languages.&lt;br /&gt;&lt;br /&gt;If you learn Java now, you may still be using it 20 years from now. Or 30.&lt;br /&gt;&lt;br /&gt;When I sat down to a card punch to write my first program 38 years ago as I write this, the computer lab know-it-all came to look over my shoulder.&lt;br /&gt;&lt;br /&gt;"FORTRAN!" he said. "Why are you wasting your time with &lt;i&gt;that&lt;/i&gt; language? It's a dead, old language. Did you know it's the &lt;i&gt;oldest&lt;/i&gt; computer language? If you really want to be a programmer, you should start right out in BAL*! That's what real programmers use, and you're going to be behind if you waste your time on anything else."&lt;br /&gt;&lt;br /&gt;FORTRAN doesn't make the "top 10" in programming languages much any more, but it gave me a good start. It's still among the most popular languages, around #20, or just out of the top 10 if you only count general purpose programming languages (that is, not counting scripting languages, query languages, application-specific languages, and so on.)&lt;br /&gt;&lt;br /&gt;And learning FORTRAN never kept me from learning structured languages, AI languages, Object Oriented languages, and so on. Even though my first program included the "dreaded" GO TO statement.&lt;br /&gt;&lt;br /&gt;There's never been a better time to learn to program. And there's never been a better time to learn Java (the language is in the best shape it's ever been!)&lt;br /&gt;&lt;br /&gt;*Basic Assembly Language, a version of assembly language for IBM computers.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-8668902242557385461?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/sevuwXEY4XIquVV3QP_EAEDmwPM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sevuwXEY4XIquVV3QP_EAEDmwPM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/sevuwXEY4XIquVV3QP_EAEDmwPM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/sevuwXEY4XIquVV3QP_EAEDmwPM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/HiNhYxaaUG0" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/8668902242557385461?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/8668902242557385461?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/HiNhYxaaUG0/should-i-still-learn-java.html" title="Should I Still Learn Java?" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2011/01/should-i-still-learn-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkEEQ3c8fSp7ImA9Wx5XFkU.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-145693695164899120</id><published>2010-09-02T09:49:00.000-07:00</published><updated>2010-09-16T17:50:02.975-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-16T17:50:02.975-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="application" /><category scheme="http://www.blogger.com/atom/ns#" term="video game" /><category scheme="http://www.blogger.com/atom/ns#" term="listeners" /><category scheme="http://www.blogger.com/atom/ns#" term="graphics" /><title>Interactive Keyboard Input In Java: KeyListeners</title><content type="html">In a console application, you can get keyboard input using the &lt;a href="http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html"&gt;Scanner&lt;/a&gt; class, as described in &lt;a href="http://beginwithjava.blogspot.com/2008/07/getting-keyboard-input-for-console-apps.html"&gt;Keyboard Input for Console Apps&lt;/a&gt;. In an graphical app, though, you can use one of the classes built to accept text input (e.g. &lt;a href="http://download.oracle.com/javase/6/docs/api/java/awt/TextArea.html"&gt;TextArea&lt;/a&gt; or &lt;a href="http://download.oracle.com/javase/6/docs/api/javax/swing/JTextField.html"&gt;JTextField&lt;/a&gt;) or add code to your application to respond directly to the keyboard.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://lh5.ggpht.com/_dE7iaOkEf6s/TIAmopem3_I/AAAAAAAABCI/rKmV7tK5Xjk/GetKey.png" width="480" height="320" style="display: block; margin-left: auto;     margin-right: auto; " alt="Keyboard input in the Java GUI made simple." /&gt;&lt;h5 align="center"&gt;Playing with Today's Program&lt;/h5&gt;&lt;br /&gt;There are two basic ways of doing this. One is to set up &lt;a href="http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html"&gt;Key Bindings&lt;/a&gt;, which maps keystrokes to actions in your application similar to accelerator keys or menu keyboard equivalents. The other is to use a &lt;a href="http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html"&gt;Key Listener&lt;/a&gt;, similar to the &lt;a href="http://download.oracle.com/javase/tutorial/uiswing/events/mouselistener.html"&gt;Mouse Listener&lt;/a&gt;, which I detailed in &lt;a href="http://beginwithjava.blogspot.com/2008/08/simple-mouse-interaction-in-java.html"&gt;Simple Mouse Interaction&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;In this example we're going to use Key Listeners. There is less overhead to setting up a KeyListener when you just need to use a few keys. Key Bindings require more overhead to set up, but when you want to bind actions to a lot of different keystrokes, and manage the actions bound to particular keystrokes at a higher level, Key Bindings are better to use than a simple KeyListener.&lt;br /&gt;&lt;br /&gt;As its name implies, a KeyListener is an &lt;a href="http://download.oracle.com/javase/tutorial/uiswing/events/intro.html"&gt;Event Listener&lt;/a&gt;. If you're not sure what that is, read my &lt;a href="http://beginwithjava.blogspot.com/2009/01/listeners-can-java-hear-you.html"&gt;article on Listeners&lt;/a&gt; or follow the prior link to Oracle/Sun's description.&lt;br /&gt;&lt;br /&gt;Here's a program that demonstrates simple keyboard interaction. It's based on the &lt;a href="http://saundby.com/beginwithjava/code/MousePanel.java"&gt;MousePanel&lt;/a&gt; program I presented in &lt;a href="http://beginwithjava.blogspot.com/2008/08/simple-mouse-interaction-in-java.html"&gt;Simple Mouse Interaction&lt;/a&gt;. It acts as a sort of "Etch-a-Sketch". You can &lt;a href="http://saundby.com/beginwithjava/code/KeyPanel.java"&gt;download the KeyPanel program source&lt;/a&gt; from my &lt;a href="http://saundby.com/beginwithjava/"&gt;Java code site&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-family: sans; color:#ff7700; font-size:90%;"&gt;// Import the basic necessary classes.&lt;br /&gt;import java.awt.*;&lt;br /&gt;import java.awt.event.*;&lt;br /&gt;import javax.swing.*;&lt;br /&gt;&lt;br /&gt;public class KeyPanel extends JPanel implements KeyListener{&lt;br /&gt;&lt;br /&gt;    public KeyPanel(){&lt;br /&gt;        super();&lt;br /&gt;        pointX=0;&lt;br /&gt;        pointY=0;&lt;br /&gt;        oldX=0;&lt;br /&gt;        oldY=0;&lt;br /&gt;        addKeyListener(this);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    int pointX, pointY, oldX, oldY;&lt;br /&gt;    boolean erase;&lt;br /&gt;&lt;br /&gt;    public void paintComponent(Graphics g){&lt;br /&gt;    // Erase the board if it's been requested.&lt;br /&gt;        if (erase) {&lt;br /&gt;           g.clearRect(0, 0 , getBounds().width, getBounds().height);&lt;br /&gt;           erase = false; // We're done, turn off this flag now.&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    // Draw gray where the pointer was..&lt;br /&gt;        g.setColor(Color.GRAY); &lt;br /&gt;        g.fillRect(oldX-2, oldY-2, 4, 4);&lt;br /&gt;    // Draw "Cursor" at current location in black.&lt;br /&gt;        g.setColor(Color.BLACK);&lt;br /&gt;        g.fillRect(pointX-2,pointY-2, 4, 4);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void keyPressed(KeyEvent key){&lt;br /&gt;&lt;br /&gt;    // Copy the last clicked location into the 'old' variables.&lt;br /&gt;        oldX=pointX;&lt;br /&gt;        oldY=pointY;&lt;br /&gt;    // Move the current point depending on which key was pressed.&lt;br /&gt;    if (key.getKeyCode() == key.VK_DOWN){&lt;br /&gt;        pointY=pointY+5;&lt;br /&gt;        if (pointY &gt; getBounds().height){&lt;br /&gt;            pointY=getBounds().height;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    if (key.getKeyCode() == key.VK_UP){&lt;br /&gt;        pointY=pointY-5;&lt;br /&gt;        if (pointY &lt; 0){pointY=0;}&lt;br /&gt;    }&lt;br /&gt;    if (key.getKeyCode() == key.VK_LEFT){&lt;br /&gt;        pointX=pointX-5;&lt;br /&gt;        if (pointX &lt; 0){pointX=0;}&lt;br /&gt;    }&lt;br /&gt;    if (key.getKeyCode() == key.VK_RIGHT){&lt;br /&gt;        pointX=pointX+5;&lt;br /&gt;        if (pointX &gt; getBounds().width){&lt;br /&gt;            pointX=getBounds().width;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color:#dddd00;"&gt;// Set a flag to erase the screen if Space is pressed.&lt;br /&gt;    if (key.getKeyCode() == key.VK_SPACE){&lt;br /&gt;        erase = true;&lt;br /&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    // Tell the panel that we need to redraw things.&lt;br /&gt;        repaint();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;/* The following methods have to be here to comply&lt;br /&gt;   with the MouseListener interface, but we don't&lt;br /&gt;   use them, so their code blocks are empty. */&lt;br /&gt;    public void keyTyped(KeyEvent key){ }   &lt;br /&gt;    public void keyReleased(KeyEvent key){ }&lt;br /&gt;&lt;br /&gt;    public static void main(String arg[]){&lt;br /&gt;        JFrame frame = new JFrame("Use Arrows to Draw, Space to Erase.");&lt;br /&gt;        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;br /&gt;        frame.setSize(640,480);&lt;br /&gt;&lt;br /&gt;        KeyPanel panel = new KeyPanel();&lt;br /&gt;        frame.setContentPane(panel);&lt;br /&gt;        frame.setVisible(true);&lt;br /&gt;&lt;br /&gt;        &lt;span style="color:#dddd00;"&gt;// We *must* do this to see KeyEvents.&lt;br /&gt;        panel.setFocusable(true);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        // Initialize the drawing pointer.&lt;br /&gt;        panel.oldX=panel.getBounds().width/2;&lt;br /&gt;        panel.oldY=panel.getBounds().height/2;&lt;br /&gt;        panel.pointX=panel.oldX;&lt;br /&gt;        panel.pointY=panel.oldY;&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Using this technique with the &lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-java-video-game-kernel.html"&gt;Simple Video Game Kernel&lt;/a&gt; would be similar. The VGKernel would extend KeyListener, register itself, and implement the KeyListener methods. But in those methods, rather than performing the operations that result from the keypress, as in this program, you would want to simply set a flag to show that the key has been pressed. Then, in your core game logic you would test to see whether the key has been pressed, and perform the appropriate actions.&lt;br /&gt;&lt;br /&gt;That way the actions are performed at the appropriate time in your game, and not just whenever the key happens to get pressed. Reacting to a key when it is pressed is appropriate for a turn-based game, but not for a real-time game. In a real-time game the action happens according to the timing of the TimerTask that drives the game, which is why we just note that a key has been pressed, and wait until the TimerTask occurs to actually conduct the action related to that key. This would be similar to what we do with the space key here, which sets a flag to tell paintComponent() to erase the screen.&lt;br /&gt;&lt;br /&gt;Give this program a try, see if you can extend it to allow the user to select colors to draw with or change the size of the drawing pen.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-145693695164899120?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/TNxAW-B5KpIjYfg1CfVvXrJFeA8/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TNxAW-B5KpIjYfg1CfVvXrJFeA8/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/TNxAW-B5KpIjYfg1CfVvXrJFeA8/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/TNxAW-B5KpIjYfg1CfVvXrJFeA8/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/RTBdPB1TwZY" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/145693695164899120?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/145693695164899120?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/RTBdPB1TwZY/interactive-keyboard-input-in-java.html" title="Interactive Keyboard Input In Java: KeyListeners" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_dE7iaOkEf6s/TIAmopem3_I/AAAAAAAABCI/rKmV7tK5Xjk/s72-c/GetKey.png" height="72" width="72" /><feedburner:origLink>http://beginwithjava.blogspot.com/2010/09/interactive-keyboard-input-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkMFRnY4fSp7ImA9Wx5RGEQ.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-1696198198046186853</id><published>2010-08-26T12:12:00.000-07:00</published><updated>2010-08-26T23:26:57.835-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-26T23:26:57.835-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="application" /><category scheme="http://www.blogger.com/atom/ns#" term="command line" /><category scheme="http://www.blogger.com/atom/ns#" term="System.out.println()" /><title>Calling System Commands in Java</title><content type="html">Let's suppose you want to run another program on your system from within Java. Personally, I first decided I wanted to do this for the sake of a prank. You may have more businesslike purposes in mind, yourself.&lt;br /&gt;&lt;br /&gt;It's not too hard to do this since Java 1.5, which added the &lt;a href="http://download.oracle.com/javase/6/docs/api/java/lang/Process.html"&gt;Process&lt;/a&gt; and &lt;a href="http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html"&gt;ProcessBuilder&lt;/a&gt; classes.&lt;br /&gt;&lt;br /&gt;Here's an example program that starts up Firefox at a particular website:&lt;br /&gt;&lt;pre style="color:#ff7700;"&gt;public class OpenSite{&lt;br /&gt;  public static void main(String arg[]){&lt;br /&gt;    try { Process p = new ProcessBuilder("firefox", &lt;br /&gt;            "http://beginwithjava.blogspot.com/").start(); }&lt;br /&gt;   catch(java.io.IOException io){ }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;I've compressed the various parts of the action down to one line here:&lt;br /&gt;&lt;pre style="color:#ff7700;"&gt;Process p = new ProcessBuilder("firefox", "http://beginwithjava.blogspot.com/").start();&lt;/pre&gt;&lt;br /&gt;I'm creating an "anonymous" ProcessBuilder object, calling its start() method, which returns a Process, which I name simply "p".&lt;br /&gt;&lt;br /&gt;The whole thing is wrapped up in a try...catch structure because javac wants to see us deal with any I/O errors that result. In this program, I just ignore them.&lt;br /&gt;&lt;br /&gt;So you'll want to make sure that you either catch those errors and deal with them, or that they will be unimportant enough that they don't matter.&lt;br /&gt;&lt;br /&gt;Also note that the Java program's process will last as long as the external program you call. So if you don't shut down this instance of the firefox process, you'll have Java still running.&lt;br /&gt;&lt;br /&gt;If you want to see something fun you can do with this on Mac, check out my article on &lt;a href="http://catsonkeyboards.blogspot.com/"&gt;my other blog&lt;/a&gt; about &lt;a href="http://catsonkeyboards.blogspot.com/2010/08/making-mac-speak-in-java.html"&gt;adding speech&lt;/a&gt; to the &lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-java-video-game-kernel.html"&gt;Simple Video Game Kernel&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-1696198198046186853?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/VkPezfnFGAk0rxXq0jQyFy8jdrA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VkPezfnFGAk0rxXq0jQyFy8jdrA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/VkPezfnFGAk0rxXq0jQyFy8jdrA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VkPezfnFGAk0rxXq0jQyFy8jdrA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/cYH26IkKfZs" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/1696198198046186853?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/1696198198046186853?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/cYH26IkKfZs/calling-system-commands-in-java.html" title="Calling System Commands in Java" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/calling-system-commands-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkUARHozeSp7ImA9Wx5RF00.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-7588140278137478814</id><published>2010-08-24T18:23:00.000-07:00</published><updated>2010-08-24T18:37:25.481-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-24T18:37:25.481-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="video game" /><category scheme="http://www.blogger.com/atom/ns#" term="API" /><category scheme="http://www.blogger.com/atom/ns#" term="framework" /><category scheme="http://www.blogger.com/atom/ns#" term="classes" /><category scheme="http://www.blogger.com/atom/ns#" term="graphics" /><category scheme="http://www.blogger.com/atom/ns#" term="events" /><title>Using Game Controllers with Java</title><content type="html">In the past, I've covered using &lt;a href="http://beginwithjava.blogspot.com/2008/08/simple-mouse-interaction-in-java.html"&gt;mice&lt;/a&gt; as input devices, and covered the general input mechanism of &lt;a href="http://beginwithjava.blogspot.com/2009/01/listeners-can-java-hear-you.html"&gt;Listeners&lt;/a&gt;. I've also discussed &lt;a href="http://beginwithjava.blogspot.com/2008/07/getting-keyboard-input-for-console-apps.html"&gt;keyboard&lt;/a&gt; input for console applications, and I'll soon be covering &lt;a href="http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html"&gt;Key Bindings&lt;/a&gt; as a way of using the keyboard in GUI applications.&lt;br /&gt;&lt;br /&gt;But there's no facility in Java itself that deals with game pads easily. To date, it's been necessary to create your own ActionListeners from scratch. But not any more. Thanks to the &lt;a href="https://jinput.dev.java.net/"&gt;JInput&lt;/a&gt; project, there's an easier way to hook up game controllers to your software.&lt;br /&gt;&lt;br /&gt;JInput attempts to do discovery on your game controller, to figure out what its setting are, what buttons and controls it has, what the center positions are of analog sticks, and so on. All that messy stuff that makes rolling your own so darn painful. It's not 100% universal, but for most controllers and most games, it will do the job admirably. If you want better for a specific controller of your own, you can extend the classes to handle your stick (and maybe feed that information back to the JInput team so that they can decide whether to include it in future releases.&lt;br /&gt;&lt;br /&gt;It's multi-platform, Windows, Mac OS X, and Linux. So it doesn't have the limitations of a lot of the other gamepad code implementations that use native code, thus limiting themselves to one platform (usually Windows.)&lt;br /&gt;&lt;br /&gt;If you want to see an implementation using JInput, check out &lt;a href="http://www.greenfoot.org/doc/gamepad/"&gt;Greenfoot with Gamepads&lt;/a&gt;. It's a good, clear example of using JInput in a general fashion.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-7588140278137478814?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/_U4jNtIxz6exr1rQ0sLxj9L6fIo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_U4jNtIxz6exr1rQ0sLxj9L6fIo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/_U4jNtIxz6exr1rQ0sLxj9L6fIo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/_U4jNtIxz6exr1rQ0sLxj9L6fIo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/cl-sHgizwEw" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/7588140278137478814?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/7588140278137478814?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/cl-sHgizwEw/using-game-controllers-with-java.html" title="Using Game Controllers with Java" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/using-game-controllers-with-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUEAQHo8fSp7ImA9Wx5RGE0.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-9148504894051974301</id><published>2010-08-22T19:12:00.000-07:00</published><updated>2010-08-26T00:27:21.475-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-26T00:27:21.475-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Math" /><category scheme="http://www.blogger.com/atom/ns#" term="video game" /><category scheme="http://www.blogger.com/atom/ns#" term="graphics" /><category scheme="http://www.blogger.com/atom/ns#" term="collision detection" /><title>More Collision Detection: Bouncing the Right Way</title><content type="html">A common result of a collision between two objects in a video game is a bounce. To do this right, we have to know which direction to make things bounce. Here's one way to do that easily in Java. I've got a ball that's going to bounce in one of eight directions depending on how it hits a brick.&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;img src="http://lh5.ggpht.com/_dE7iaOkEf6s/THCPvSDDtNI/AAAAAAAABB4/RQU2xdSJN20/BallBounce.jpg" alt="Diagram showing the results of a ball hitting a brick and bouncing, depending on how it strikes the brick. Collision detection in action." /&gt;&lt;/center&gt;&lt;br /&gt;I've marked the changes to the ball's velocity that will occur as a result of each possible type of collision. The three arrows on the left side all show that the xVel (x velocity) is a negative value, the three arrows on the bottom all show that yVel (y velocity) should become a positive value.&lt;br /&gt;&lt;br /&gt;In the cases of the corners, both the x and y velocities are set. The bounces off the middles of the sides all cause one velocity to be set, but the other is left alone. For example, a bounce off the right side of the brick sets the xVel to a positive value, but the yVel is unchanged. So if yVel was a positive value before (meaning the ball is travelling down on screen) then it will keep travelling down. If it was negative--moving up--then it will keep moving up. Only the x direction of movement will change. It will start moving from left to right instead of right to left.&lt;br /&gt;&lt;br /&gt;It's really easy to &lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-collision-detection-in-java.html"&gt;detect a collision&lt;/a&gt;, but now we need to know the relationship between the two colliding objects so that we can make the ball bounce in the correct direction. As we learned in &lt;a href="http://beginwithjava.blogspot.com/2010/08/stopping-jonathan-livingston-seagull.html"&gt;Stopping Jonathan Livingston Seagull&lt;/a&gt;, the effect of how much we move an object before we check a collision can cause problems if we don't understand the limits of how we detect them.&lt;br /&gt;&lt;br /&gt;In this program, our ball's movement is presently set at one quarter of its width. We could go as high as one half its width and still have our collision detection routines work properly here. Fortunately, we want to keep the distance the ball moves each turn short to make the movement look smoother.&lt;br /&gt;&lt;br /&gt;When we hit the brick, we want to see where it hits the ball. This will tell the ball which way to bounce, just like a real ball. In the drawing above, we have a red brick on a cyan (blue-green) playfield. The ball is dark blue. Where the ball is &lt;i&gt;intersecting&lt;/i&gt;, or overlapping, the brick is shown in yellow. Now we just need to know which side of the ball is getting "pushed in" this way. Then we bounce the opposite way.&lt;br /&gt;&lt;br /&gt;Here's the program, the key code we're talking about is highlighted:&lt;br /&gt;&lt;pre style="color:#dd6600;font-family:monospace; font-size:90%;"&gt;/* A simple video game style kernel, revision 4.&lt;br /&gt;   by Mark Graybill, August 2010&lt;br /&gt;   Has a ball bouncing off a brick.&lt;br /&gt;   Because the ball moves at a fixed speed,&lt;br /&gt;   slow enough we won't miss a collision,&lt;br /&gt;   we can use simple colliision detection.&lt;br /&gt;&lt;br /&gt;   Uses the Brick class from the article&lt;br /&gt;   "Multiple Constructor Methods"&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;import java.awt.*;&lt;br /&gt;import javax.swing.*;&lt;br /&gt;import java.lang.Math;&lt;br /&gt;&lt;br /&gt;public class VidBrick extends JPanel{&lt;br /&gt;&lt;br /&gt;public Rectangle screen, bounds; // The screen area and boundary.&lt;br /&gt;public JFrame frame; // A JFrame to put the graphics into.&lt;br /&gt;public VGTimerTask vgTask; // The TimerTask that runs the game.&lt;br /&gt;public VGBall ball; // The game ball, a subclass of Rectangle.&lt;br /&gt;private Brick brick; // A brick for the ball to interact with.&lt;br /&gt;&lt;br /&gt;/* This sets up the screen area, and creates instances of&lt;br /&gt; the JFrame, VGBall, VGTimerTask, etc that we'll need. */&lt;br /&gt;  public VidBrick(){&lt;br /&gt;    super();&lt;br /&gt;    screen = new Rectangle(0, 0, 600, 400);&lt;br /&gt;    bounds = new Rectangle(0, 0, 600, 400); // Give some temporary values.&lt;br /&gt;    ball = new VGBall();&lt;br /&gt;    frame = new JFrame("VidBrick");&lt;br /&gt;    vgTask = new VGTimerTask();&lt;br /&gt;    brick = new Brick();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;  class VGTimerTask extends TimerTask{&lt;br /&gt;    public void run(){&lt;br /&gt;      ball.move();&lt;br /&gt;      frame.repaint();&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  class VGBall extends Rectangle{&lt;br /&gt;    int xVel, yVel; // The ball's velocity.&lt;br /&gt;    Color ballColor; // The color of the ball.&lt;br /&gt;&lt;br /&gt;/* Create a VGBall with default location of upper left&lt;br /&gt;  corner, size of 20x20 pixels, moving at one quarter&lt;br /&gt;  its height and width per turn--plus it's blue. */&lt;br /&gt;    public VGBall(){&lt;br /&gt;      super(0, 0, 20, 20);&lt;br /&gt;      xVel = width/4;&lt;br /&gt;      yVel = height/4;&lt;br /&gt;      ballColor=new Color(0, 0, 128);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;/* Move the Ball. */&lt;br /&gt;    public void move(){&lt;br /&gt;      // Move the ball according to the game rules.&lt;br /&gt;      x+=xVel; // Move horizontally.&lt;br /&gt;      y+=yVel; // Move vertically.&lt;br /&gt;&lt;br /&gt;      // Detect edges and bounce if necessary.&lt;br /&gt;      if (x &gt; (bounds.width - width)){&lt;br /&gt;        xVel = -xVel; // reverse movement.&lt;br /&gt;        x = bounds.width -  width; // Set location to screen edge.&lt;br /&gt;      }&lt;br /&gt;      if (y &gt; (bounds.height - height)){&lt;br /&gt;        yVel = -yVel; // reverse movement.&lt;br /&gt;        y = bounds.height - height;&lt;br /&gt;      }&lt;br /&gt;      if (x &lt;= 0) { xVel = -xVel; x = 0; }&lt;br /&gt;      if (y &lt;= 0) { yVel = -yVel; y = 0; }&lt;br /&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;     // Detect Brick and bounce if necessary.&lt;br /&gt;      if (intersects(brick)) {&lt;br /&gt;        // Get the intersection rectangle to find out which way to bounce.&lt;br /&gt;        Rectangle iRect = intersection(brick);&lt;br /&gt;        // If we hit on the left side, go left (negative x velocity).&lt;br /&gt;        if ((x+(width/2))&lt;(iRect.x+(iRect.width/2))){xVel=(0-Math.abs(xVel));}&lt;br /&gt;        // If we hit on the right side, go right (positive x velocity).&lt;br /&gt;        if ((x+(width/2))&gt;(iRect.x+(iRect.width/2))){xVel=Math.abs(xVel);}&lt;br /&gt;        // If we hit on the top, go up.&lt;br /&gt;        if ((y+(height/2))&lt;(iRect.y+(iRect.height/2))){yVel=(0-Math.abs(yVel));}&lt;br /&gt;        // If we hit on the bottom, go down.&lt;br /&gt;        if ((y+(height/2))&gt;(iRect.y+(iRect.height/2))){yVel=Math.abs(yVel);}&lt;br /&gt;      } // if intersects&lt;br /&gt;&lt;/span&gt;    }&lt;br /&gt;&lt;br /&gt;/* Draw the ball into the provided graphics context. Preserves&lt;br /&gt;   the context's drawing color setting. */&lt;br /&gt;&lt;br /&gt;    public void draw(Graphics g){&lt;br /&gt;    // the ball draws itself in the graphics context given.&lt;br /&gt;      Color gcColor = g.getColor(); // Preserve the present color.&lt;br /&gt;      g.setColor(ballColor); // Use the ball's color for the ball.&lt;br /&gt;      g.fillRect(x, y, width, height); // Draw the ball.&lt;br /&gt;      g.setColor(gcColor); // Restore prior color.&lt;br /&gt;    } // end draw()&lt;br /&gt;  } // end of class VGBall&lt;br /&gt;&lt;br /&gt;  public void paintComponent(Graphics g){&lt;br /&gt;    // Get the drawing area bounds for game logic.&lt;br /&gt;    bounds = g.getClipBounds();&lt;br /&gt;    // Clear the drawing area.&lt;br /&gt;    g.clearRect(screen.x, screen.y, screen.width, screen.height);&lt;br /&gt;    // Draw the brick.&lt;br /&gt;    g.setColor(brick.getColor());&lt;br /&gt;    g.fillRect(brick.x, brick.y, brick.width, brick.height);&lt;br /&gt;    // Draw the ball.&lt;br /&gt;    ball.draw(g);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;/* Main program loop. */&lt;br /&gt;  public static void main(String arg[]){&lt;br /&gt;&lt;br /&gt;    java.util.Timer vgTimer = new java.util.Timer();  // Create a Timer object&lt;br /&gt;    VidBrick panel = new VidBrick(); &lt;br /&gt;    &lt;br /&gt;    panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;br /&gt;    panel.frame.setSize(panel.screen.width, panel.screen.height);&lt;br /&gt;&lt;br /&gt;    panel.frame.setContentPane(panel); &lt;br /&gt;    panel.frame.setVisible(true);&lt;br /&gt;&lt;br /&gt;    // Set up the brick.&lt;br /&gt;    panel.brick.x = panel.screen.width/3;&lt;br /&gt;    panel.brick.y = panel.screen.height/3;&lt;br /&gt;    panel.brick.width = panel.screen.width/3;&lt;br /&gt;    panel.brick.height = panel.screen.height/3;&lt;br /&gt;&lt;br /&gt;    // Set up a timer to do the vgTask regularly.&lt;br /&gt;    vgTimer.schedule(panel.vgTask, 0, 33);&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The way we figure out which way to bounce is to find the center of the ball and the center of the intersection rectangle (the yellow box in the picture, where the ball and the brick overlap.) Then if the center of the intersection (shown as an orange dot on the pic) is to the right of the center of the ball (a white dot) we bounce left. If it's to the left, we bounce right, if it's above we bounce down, below we bounce up.&lt;br /&gt;&lt;br /&gt;This way we don't have to worry about the size or shape of the brick itself. All we care about is the bit that strikes the ball. Then the ball bounces the correct way.&lt;br /&gt;&lt;br /&gt;If we had wanted to get really fancy, we could have made the ball bounce at different angles depending on the angle between the center of the ball and the center of the intersection rectangle. If you're familiar with trigonometry, take a look at the &lt;a href="http://download-llnw.oracle.com/javase/6/docs/api/java/lang/Math.html"&gt;Math class&lt;/a&gt; in Java and see if you can implement this.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-9148504894051974301?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/-cJKKsWVDVgIuWkuVxKUefzXUd4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-cJKKsWVDVgIuWkuVxKUefzXUd4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/-cJKKsWVDVgIuWkuVxKUefzXUd4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-cJKKsWVDVgIuWkuVxKUefzXUd4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/y0nHOArDWpE" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/9148504894051974301?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/9148504894051974301?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/y0nHOArDWpE/more-collision-detection-bouncing-right.html" title="More Collision Detection: Bouncing the Right Way" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_dE7iaOkEf6s/THCPvSDDtNI/AAAAAAAABB4/RQU2xdSJN20/s72-c/BallBounce.jpg" height="72" width="72" /><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/more-collision-detection-bouncing-right.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0cNSXY6eCp7ImA9Wx5RE0g.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-4405038231415710632</id><published>2010-08-20T19:27:00.000-07:00</published><updated>2010-08-20T19:51:38.810-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-20T19:51:38.810-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="video game" /><category scheme="http://www.blogger.com/atom/ns#" term="humor" /><category scheme="http://www.blogger.com/atom/ns#" term="graphics" /><category scheme="http://www.blogger.com/atom/ns#" term="web" /><title>Web Browsing on the Atari 2600???</title><content type="html">A recent discussion on an Astronomy forum took a turn for the earthly with the announcement of a new web site at &lt;a href="http://www.stellarvue.com/"&gt;StellarVue Telescopes&lt;/a&gt;. People were checking it out, reporting on minor fixes they felt were necessary and trying it with different systems and OSes. Then came the first report of a total crash and burn. From a Commodore 64 user. Next we had reports of failures on Heathkit and Vic-20 systems, too.&lt;br /&gt;&lt;br /&gt;"Aha," I thought," a perfect opportunity to try out my &lt;tt style="font-size:125%; color:#ffff00"&gt;Web Surfer&lt;/tt&gt; cartridge on my Atari 2600!"&lt;br /&gt;&lt;br /&gt;Here are the results:&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;a href="http://saundby.com/atari7800mod/websurfer2600.shtml"&gt;&lt;img src="http://lh6.ggpht.com/_dE7iaOkEf6s/TG5SPycgC0I/AAAAAAAABBg/aPIx7Rns01U/s512/atariwebsvsitemed.jpg" /&gt;&lt;h5&gt;My Atari 2600 with the Web Surfer Cartridge,&lt;br /&gt;Displaying the &lt;a href="http://www.stellarvue.com/"&gt;StellarVue&lt;/a&gt; Web Site.&lt;/h5&gt;&lt;/a&gt;&lt;/center&gt;&lt;br /&gt;&lt;br /&gt;Check out my &lt;a href="http://saundby.com/atari7800mod/"&gt;Atari web page&lt;/a&gt; for more pictures and info on this amazing VCS cart!&lt;br /&gt;&lt;br /&gt;&lt;p style="font-size:90%;font-family:sans-serif;color:#003388;"&gt;&lt;br /&gt;Note to those following my Java Programming Stuff: I was supposed to post this over at my other blog, &lt;a href="http://catsonkeyboards.blogspot.com/"&gt;An Infinite Number of Cats on Keyboards&lt;/a&gt;, but I punched the wrong button on my Blogger Dashboard, and it ended up here instead. Sorry about that. Now back to our regularly scheduled Java program...&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-4405038231415710632?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/2QJY9P51u04gqa5NWPbhYnT-a5g/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2QJY9P51u04gqa5NWPbhYnT-a5g/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/2QJY9P51u04gqa5NWPbhYnT-a5g/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/2QJY9P51u04gqa5NWPbhYnT-a5g/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/IjRyR-CH078" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/4405038231415710632?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/4405038231415710632?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/IjRyR-CH078/web-browsing-on-atari-2600.html" title="Web Browsing on the Atari 2600???" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_dE7iaOkEf6s/TG5SPycgC0I/AAAAAAAABBg/aPIx7Rns01U/s72-c/atariwebsvsitemed.jpg" height="72" width="72" /><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/web-browsing-on-atari-2600.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUIMRns7eCp7ImA9Wx5REUQ.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-3400350468368886782</id><published>2010-08-18T19:15:00.000-07:00</published><updated>2010-08-18T22:59:47.500-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-18T22:59:47.500-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="graphics" /><category scheme="http://www.blogger.com/atom/ns#" term="collision detection" /><title>Stopping Jonathan Livingston Seagull with Java</title><content type="html">In &lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-collision-detection-in-java.html"&gt;Simple Collision Detection&lt;/a&gt; we used the intersects() method of two &lt;a href="http://download.oracle.com/javase/6/docs/api/java/awt/Rectangle.html"&gt;Rectangle&lt;/a&gt; subclasses to see if one object struck another on our playfield. It works, in the example given. But in the example given the velocity of the ball is limited.&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;img src="http://lh4.ggpht.com/_dE7iaOkEf6s/TGyZf_70BCI/AAAAAAAABBE/llMNfBxWiAc/VGBKernel.png" alt="Simple collision detection works fine, here." /&gt;&lt;/center&gt;In the VGBKernel.java example, we have a fairly slow-moving ball detecting collisions with a really large object that's hard to miss.&lt;br /&gt;&lt;br /&gt;If we have a brick that is small, and a ball that is fast, though, the ball could pass right over the brick without even realizing it should have collided with it. In other words, if we had a brick wall stretching across the screen, the ball might fly "through" it when we want it to stop or bounce. Let's say we're falling and want to detect a brick that acts as a floor. The floor goes across the screen between heights 100 and 105. The ball starts at location 75 and has a velocity of 50. Its new location would be 125. We move it there, and check for a collision as we did in VGBKernel. No collision. The ball just flew "through" the brick wall like Jonathan Livingston Seagull!&lt;br /&gt;&lt;br /&gt;Let's make an example program called BallGrav.java to illustrate the problem. BallGrav.java is a slightly modified version of &lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-collision-detection-in-java.html"&gt;VGBKernel.java&lt;/a&gt;. It uses the same &lt;a href="http://beginwithjava.blogspot.com/2010/08/multiple-constructor-methods.html"&gt;Brick class&lt;/a&gt; as we've used before, with no modifications.&lt;br /&gt;&lt;pre style="color:#dd4400;"&gt;/* Based on the video game style kernel, revision 3.&lt;br /&gt;   by Mark Graybill, August 2010&lt;br /&gt;   Demonstrates collision detection problems, and solutions.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;// Import Timer and other useful stuff:&lt;br /&gt;import java.util.*;&lt;br /&gt;// Import the basic graphics classes.&lt;br /&gt;import java.awt.*;&lt;br /&gt;import javax.swing.*;&lt;br /&gt;import java.lang.Math;&lt;br /&gt;&lt;br /&gt;public class BallGrav extends JPanel{&lt;br /&gt;&lt;br /&gt;public Rectangle screen, bounds; // The screen area and boundary.&lt;br /&gt;public JFrame frame; // A JFrame to put the graphics into.&lt;br /&gt;public VGTimerTask vgTask; // The TimerTask that runs the game.&lt;br /&gt;public VGBall ball; // The game ball, a subclass of Rectangle.&lt;br /&gt;private Brick brick; // A brick for the ball to interact with.&lt;br /&gt;&lt;br /&gt;// Create a constructor method:&lt;br /&gt;  public BallGrav(){&lt;br /&gt;    super();&lt;br /&gt;    screen = new Rectangle(0, 0, 600, 400);&lt;br /&gt;    bounds = new Rectangle(0, 0, 600, 400); // Give some temporary values.&lt;br /&gt;    ball = new VGBall();&lt;br /&gt;    frame = new JFrame("BallGrav");&lt;br /&gt;    vgTask = new VGTimerTask();&lt;br /&gt;    brick = new Brick();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;  // Create an inner TimerTask class that has access to the&lt;br /&gt;  // members of the BallGrav.&lt;br /&gt;  class VGTimerTask extends TimerTask{&lt;br /&gt;    public void run(){&lt;br /&gt;      ball.move();&lt;br /&gt;      frame.repaint();&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  // Create an inner VGBall class that has our game logic in it.&lt;br /&gt;  class VGBall extends Rectangle{&lt;br /&gt;    int xVel, yVel; // The ball's velocity.&lt;br /&gt;    Color ballColor; // The color of the ball.&lt;br /&gt;&lt;br /&gt;    public VGBall(){&lt;br /&gt;      super(300, 0, 20, 20);&lt;br /&gt;      xVel = 0; // Start with 0 velocity at center top of screen.&lt;br /&gt;      yVel = 0;&lt;br /&gt;      ballColor=new Color(0, 0, 128);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // Instance methods for VGBall&lt;br /&gt;    public void move(){&lt;br /&gt;      // Accelerate due to "gravity".&lt;br /&gt;      yVel+=3;&lt;br /&gt;      // Move the ball according to the game rules.&lt;br /&gt;      x+=xVel; // Move horizontally.&lt;br /&gt;      y+=yVel; // Move vertically, accelerating as we go.&lt;br /&gt;      // Detect edges and stop movement if we hit them.&lt;br /&gt;      if (x &gt; (bounds.width - width)){&lt;br /&gt;        xVel = 0; // stop movement.&lt;br /&gt;        x = bounds.width -  width; // Set location to screen edge.&lt;br /&gt;      }&lt;br /&gt;      if (y &gt; (bounds.height - height)){&lt;br /&gt;        yVel = 0; // stop movement.&lt;br /&gt;        y = bounds.height - height;&lt;br /&gt;      }&lt;br /&gt;      if (x &lt;= 0) { xVel = 0; x = 0; }&lt;br /&gt;      if (y &lt;= 0) { yVel = 0; y = 0; }&lt;br /&gt;&lt;br /&gt;      // Check for intersection with Brick,&lt;br /&gt;      // change color when touching.&lt;br /&gt;      if (intersects(brick)) {&lt;br /&gt;        //Stop on top of the brick if we hit it.&lt;br /&gt;        yVel = 0;&lt;br /&gt;        y = brick.y - height;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void draw(Graphics g){&lt;br /&gt;    // the ball draws itself in the graphics context given.&lt;br /&gt;      Color gcColor = g.getColor(); // Preserve the present color.&lt;br /&gt;      g.setColor(ballColor); // Use the ball's color for the ball.&lt;br /&gt;      g.fillRect(x, y, width, height); // Draw the ball.&lt;br /&gt;      g.setColor(gcColor); // Restore prior color.&lt;br /&gt;    } // end draw()&lt;br /&gt;&lt;br /&gt;  } // end of class VGBall&lt;br /&gt;&lt;br /&gt;// Now the instance methods:&lt;br /&gt;  public void paintComponent(Graphics g){&lt;br /&gt;    // Get the drawing area bounds for game logic.&lt;br /&gt;    bounds = g.getClipBounds();&lt;br /&gt;    // Clear the drawing area.&lt;br /&gt;    g.clearRect(screen.x, screen.y, screen.width, screen.height);&lt;br /&gt;    // Draw the brick.&lt;br /&gt;    g.setColor(brick.getColor());&lt;br /&gt;    g.fillRect(brick.x, brick.y, brick.width, brick.height);&lt;br /&gt;    // Draw the ball.&lt;br /&gt;    ball.draw(g);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  public static void main(String arg[]){&lt;br /&gt;&lt;br /&gt;    java.util.Timer vgTimer = new java.util.Timer();  // Create a Timer object&lt;br /&gt;    BallGrav panel = new BallGrav(); &lt;br /&gt;    &lt;br /&gt;    panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;br /&gt;    panel.frame.setSize(panel.screen.width, panel.screen.height);&lt;br /&gt;&lt;br /&gt;    panel.frame.setContentPane(panel); &lt;br /&gt;    panel.frame.setVisible(true);&lt;br /&gt;&lt;br /&gt;    // Set up the brick.&lt;br /&gt;    panel.brick.x = 0;&lt;br /&gt;    panel.brick.y = 4 * (panel.screen.height/5);&lt;br /&gt;    panel.brick.width = panel.screen.width;&lt;br /&gt;    panel.brick.height = 5;&lt;br /&gt;    panel.brick.brickColor = new Color(200,50,50);&lt;br /&gt;&lt;br /&gt;    // Set up a timer to do the vgTask regularly.&lt;br /&gt;    vgTimer.schedule(panel.vgTask, 250, 200);&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;This example functions properly. When the ball falls, it strikes the brick floor and stops. &lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;img src="http://lh4.ggpht.com/_dE7iaOkEf6s/TGyZgHqbqCI/AAAAAAAABBM/DclF69Duuy4/BallGrav-ok.png" alt="Image of collision detection working." /&gt;&lt;/center&gt;&lt;br /&gt;&lt;br /&gt;But let's make the ball accelerate a bit faster. Make the following change in Ball's move() method:&lt;br /&gt;&lt;pre style="color:#0033ee;"&gt;      // Accelerate due to "gravity".&lt;br /&gt;      yVel+=&lt;span style="Color:#ffaa00"&gt;5&lt;/span&gt;;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Suddenly, the ball doesn't detect the floor any more, and it goes to the bottom of the screen, right through the floor:&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;img src="http://lh6.ggpht.com/_dE7iaOkEf6s/TGyZfy6eyNI/AAAAAAAABBI/FK-LNGhkx_s/BallGrav-ng.png" alt="Image showing collision detection failure." /&gt;&lt;/center&gt;&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Stopping Jonathan Livingston Gameball from Going Through the Wall&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;What do we do to keep this from happening? Well, one way is to never let the ball move faster than it can detect collisions. Then we'd never let it move more than its own size in any direction. If it's important that it know what side it hits something on (like when we're going to bounce off it), then we may not want to let it go more than half its size in any direction in any one "move." To do this to BallGrav,java, we'd change the code as follows:&lt;br /&gt;&lt;pre style="color:#00ee00;"&gt;      // Accelerate due to "gravity".&lt;br /&gt;      yVel+=5;&lt;br /&gt;      &lt;span style="color:"dddddd;"&gt;// Limit our velocity to preserve collisions.&lt;br /&gt;      if (yVel&gt;height) { yVel = height; }&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Now the ball will never go so fast it never sees the floor.&lt;br /&gt;&lt;br /&gt;But what if we &lt;i&gt;want&lt;/i&gt; the ball to move faster?&lt;br /&gt;&lt;br /&gt;In this case, there are a number of possible solutions. Many of them are fairly complicated, with lots of math or with lots of iteration checking for collisions at each step along the way. Complexity is something we don't want, for a lot of reasons. Here is one possible solution, that uses collision prediction to see if our ball might hit something along the way:&lt;br /&gt;&lt;br /&gt;&lt;pre style="color:#0077ff;"&gt;      // Accelerate due to "gravity".&lt;br /&gt;      yVel+=5;&lt;br /&gt;      // Limit our velocity.&lt;br /&gt;      if (yVel&gt;(height*2)) { yVel = height * 2; }&lt;br /&gt;      // Move the ball according to the game rules.&lt;br /&gt;      // Check for a collision if we're moving fast.&lt;br /&gt;      if (yVel&gt;height) {&lt;br /&gt;        Rectangle halfway = new Rectangle(x,y+(yVel/2), width, height);&lt;br /&gt;        if (halfway.intersects(brick)) { yVel = yVel/2; }&lt;br /&gt;      }&lt;br /&gt;      x+=xVel; // Move horizontally.&lt;br /&gt;      y+=yVel; // Move vertically, accelerating as we go.&lt;/pre&gt;&lt;br /&gt;Here, I have a velocity limit twice as high as before. This allows the ball to move along a lot faster (before, I could &lt;i&gt;see&lt;/i&gt; the ball slow down.) Now, since the ball can move fast enough to fly through things, we first look ahead to see if there's anything in the way. We do this by seeing if there's something halfway there. If so, we cut our original speed in half before we move. Then the collision (if any) happens "naturally."&lt;br /&gt;&lt;br /&gt;This is good enough, if the fastest we can move is twice the ball's height. If we allowed three times its height, we might check at points 1/3 and 2/3 of the way along its path.&lt;br /&gt;&lt;br /&gt;If we don't want limits like this, we pay the price by having a more complex method to check for collisions. Here, I've limited things to movement in only one direction, up and down. If we're going side to side as well, I'd have to check for waypoints along a diagonal path. To get really precise, I'd have to do something like project lines along the corners of the ball from its current position to its intended new position and see if they intersect anything. When you get into more complex shapes than the rectangles we're using now, you get a different set of choices.&lt;br /&gt;&lt;br /&gt;Fortunately, for video games, simple choices are almost always plenty good enough. Checking for collisions at the minimum number of points along the path is almost always good enough. This can be done by dividing the distance to be moved by the largest safe move distance, and checking that many times for collisions along the path.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Avoiding the Problem Entirely&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;You can also just use a different sort of movement that never has any of the objects moving more than some small number of pixels at a time--small enough that you'll never fail to detect a collision. I'll give an example in a future article, but for now I'll just describe it.&lt;br /&gt;&lt;br /&gt;What you do is set a speed at which you move things only, say, one pixel per "move". But you only move them every so often, depending on what their speed is. The fastest objects get moved on every redraw of the screen. Slower objects only get moved on every other update, or every fifth, or something of that sort. That way an object is never moved across the screen by so many pixels at once that it misses seeing a collision with another object.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Collision Detection: Never "Easy"&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Collision detection is one of the programmer's bugaboos. It's one of the places where errors are most prone to occur in code, and it can have terrible effects on a game if it doesn't work properly. The programmer's choices are to accept certain limitations or to make more complex code, or to use a technique of object movement that can either be limiting or eat up a lot of processor time.&lt;br /&gt;&lt;br /&gt;When my students program their video game projects in class, one of our biggest problems in implementing those games is dealing with collision detection. Even though we're working in Greenfoot, which does a lot of the work for us. We still need to be aware of the limitations built into the system, and work within them.&lt;br /&gt;&lt;br /&gt;Now it's your turn. Try reducing the brick in BallGrav.java to a small platform. Now give the ball a constant x velocity. See if you can get the ball to land on that platform reliably, with successful collision detection.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-3400350468368886782?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/cuoaAeHqFcnIG57KI5_Habdqebk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cuoaAeHqFcnIG57KI5_Habdqebk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/cuoaAeHqFcnIG57KI5_Habdqebk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cuoaAeHqFcnIG57KI5_Habdqebk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/vGjVTtVj7T4" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3400350468368886782?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3400350468368886782?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/vGjVTtVj7T4/stopping-jonathan-livingston-seagull.html" title="Stopping Jonathan Livingston Seagull with Java" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_dE7iaOkEf6s/TGyZf_70BCI/AAAAAAAABBE/llMNfBxWiAc/s72-c/VGBKernel.png" height="72" width="72" /><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/stopping-jonathan-livingston-seagull.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0AFQngzeCp7ImA9Wx5REUU.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-317341463367189420</id><published>2010-08-15T12:27:00.000-07:00</published><updated>2010-08-18T19:41:53.680-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-18T19:41:53.680-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="inner class" /><category scheme="http://www.blogger.com/atom/ns#" term="video game" /><category scheme="http://www.blogger.com/atom/ns#" term="graphics" /><category scheme="http://www.blogger.com/atom/ns#" term="collision detection" /><title>Simple Collision Detection in Java</title><content type="html">Collision detection is a basic element of a video game. It is one of the primary means of determining when objects in a game should interact. In Asteroids, there are collisions between the player's shot and the asteroids themselves. There are collisions between the player's shot and the alien spacecraft (hopefully.) There are collisions between the asteroids and the player's ship (argh!) Each of these is critical to the game.&lt;br /&gt;&lt;br /&gt;&lt;center&gt;&lt;img src="http://lh4.ggpht.com/_dE7iaOkEf6s/TGyZf_70BCI/AAAAAAAABBE/llMNfBxWiAc/VGBKernel.png" alt="Screen shot of collision detection in the video game kernel, showing color change of ball when intersecting the brick." /&gt;&lt;/center&gt;&lt;br /&gt;Since we don't have actual physics to determine when there's been a collision, as we have in real life, we need to create a set of rules for when a collision has occured then implement those rules in our code. One way to handle collision detection is to see if two potentially colliding objects intersect at all on the playfield. That is, whether one of them overlays the other.&lt;br /&gt;&lt;br /&gt;Previously, we have worked with &lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-java-video-game-kernel.html"&gt;a simple video game kernel&lt;/a&gt;, then &lt;a href="http://beginwithjava.blogspot.com/2010/08/java-video-game-programming-game-logic.html"&gt;updated it&lt;/a&gt; to make the addition of game logic simpler. We had also created &lt;a href="http://beginwithjava.blogspot.com/2010/08/multiple-constructor-methods.html"&gt;a Brick class&lt;/a&gt; that we can use to interact with the ball in our basic video game kernel.&lt;br /&gt;&lt;br /&gt;Here is a slightly changed version of the simple video game kernel. It uses the Brick class, so Brick.class should be in the same directory as your VGBKernel.java and VGBKernel.class (it wouldn't be a bad idea to have Brick.java there, too, in case you want to play around with it at all to get different effects with this program through changes of your own.)&lt;br /&gt;&lt;pre style="color:#00aacc;"&gt;/* A simple video game style kernel, revision 3.&lt;br /&gt;   by Mark Graybill, August 2010&lt;br /&gt;   Uses an inner class to contain game logic,&lt;br /&gt;   with another inner class as a game timer.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;// Import Timer and other useful stuff:&lt;br /&gt;import java.util.*;&lt;br /&gt;// Import the basic graphics classes.&lt;br /&gt;import java.awt.*;&lt;br /&gt;import javax.swing.*;&lt;br /&gt;import java.lang.Math;&lt;br /&gt;&lt;br /&gt;public class VGBKernel extends JPanel{&lt;br /&gt;&lt;br /&gt;public Rectangle screen, bounds; // The screen area and boundary.&lt;br /&gt;public JFrame frame; // A JFrame to put the graphics into.&lt;br /&gt;public VGTimerTask vgTask; // The TimerTask that runs the game.&lt;br /&gt;public VGBall ball; // The game ball, a subclass of Rectangle.&lt;br /&gt;private Brick brick; // A brick for the ball to interact with.&lt;br /&gt;&lt;br /&gt;// Create a constructor method:&lt;br /&gt;  public VGBKernel(){&lt;br /&gt;    super();&lt;br /&gt;    screen = new Rectangle(0, 0, 600, 400);&lt;br /&gt;    bounds = new Rectangle(0, 0, 600, 400); // Give some temporary values.&lt;br /&gt;    ball = new VGBall();&lt;br /&gt;    frame = new JFrame("VGBKernel");&lt;br /&gt;    vgTask = new VGTimerTask();&lt;br /&gt;    brick = new Brick();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;  // Create an inner TimerTask class that has access to the&lt;br /&gt;  // members of the VGBKernel.&lt;br /&gt;  class VGTimerTask extends TimerTask{&lt;br /&gt;    public void run(){&lt;br /&gt;      ball.move();&lt;br /&gt;      frame.repaint();&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  // Create an inner VGBall class that has our game logic in it.&lt;br /&gt;  class VGBall extends Rectangle{&lt;br /&gt;    int xVel, yVel; // The ball's velocity.&lt;br /&gt;    Color ballColor; // The color of the ball.&lt;br /&gt;&lt;br /&gt;    public VGBall(){&lt;br /&gt;      super(0, 0, 20, 20);&lt;br /&gt;      xVel = width/4;&lt;br /&gt;      yVel = height/4;&lt;br /&gt;      ballColor=new Color(0, 0, 128);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // Instance methods for VGBall&lt;br /&gt;    public void move(){&lt;br /&gt;      // Move the ball according to the game rules.&lt;br /&gt;      x+=xVel; // Move horizontally.&lt;br /&gt;      y+=yVel; // Move vertically.&lt;br /&gt;      // Detect edges and bounce if necessary.&lt;br /&gt;      if (x &gt; (bounds.width - width)){&lt;br /&gt;        xVel = -xVel; // reverse movement.&lt;br /&gt;        x = bounds.width -  width; // Set location to screen edge.&lt;br /&gt;      }&lt;br /&gt;      if (y &gt; (bounds.height - height)){&lt;br /&gt;        yVel = -yVel; // reverse movement.&lt;br /&gt;        y = bounds.height - height;&lt;br /&gt;      }&lt;br /&gt;      if (x &lt;= 0) { xVel = -xVel; x = 0; }&lt;br /&gt;      if (y &lt;= 0) { yVel = -yVel; y = 0; }&lt;br /&gt;&lt;br /&gt;      // Check for intersection with Brick,&lt;br /&gt;      // change color when touching.&lt;br /&gt;      if (intersects(brick)) { ballColor=Color.GREEN; }&lt;br /&gt;      else { ballColor=Color.BLUE; }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void draw(Graphics g){&lt;br /&gt;    // the ball draws itself in the graphics context given.&lt;br /&gt;      Color gcColor = g.getColor(); // Preserve the present color.&lt;br /&gt;      g.setColor(ballColor); // Use the ball's color for the ball.&lt;br /&gt;      g.fillRect(x, y, width, height); // Draw the ball.&lt;br /&gt;      g.setColor(gcColor); // Restore prior color.&lt;br /&gt;    } // end draw()&lt;br /&gt;&lt;br /&gt;  } // end of class VGBall&lt;br /&gt;&lt;br /&gt;// Now the instance methods:&lt;br /&gt;  public void paintComponent(Graphics g){&lt;br /&gt;    // Get the drawing area bounds for game logic.&lt;br /&gt;    bounds = g.getClipBounds();&lt;br /&gt;    // Clear the drawing area.&lt;br /&gt;    g.clearRect(screen.x, screen.y, screen.width, screen.height);&lt;br /&gt;    // Draw the brick.&lt;br /&gt;    g.setColor(brick.getColor());&lt;br /&gt;    g.fillRect(brick.x, brick.y, brick.width, brick.height);&lt;br /&gt;    // Draw the ball.&lt;br /&gt;    ball.draw(g);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  public static void main(String arg[]){&lt;br /&gt;&lt;br /&gt;    java.util.Timer vgTimer = new java.util.Timer();  // Create a Timer object&lt;br /&gt;    VGBKernel panel = new VGBKernel(); &lt;br /&gt;    &lt;br /&gt;    panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;br /&gt;    panel.frame.setSize(panel.screen.width, panel.screen.height);&lt;br /&gt;&lt;br /&gt;    panel.frame.setContentPane(panel); &lt;br /&gt;    panel.frame.setVisible(true);&lt;br /&gt;&lt;br /&gt;    // Set up the brick.&lt;br /&gt;    panel.brick.x = panel.screen.width/4;&lt;br /&gt;    panel.brick.y = panel.screen.height/4;&lt;br /&gt;    panel.brick.width = panel.screen.width/2;&lt;br /&gt;    panel.brick.height = panel.screen.height/2;&lt;br /&gt;&lt;br /&gt;    // Set up a timer to do the vgTask regularly.&lt;br /&gt;    vgTimer.schedule(panel.vgTask, 0, 33);&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The main changes here are the addition of the &lt;tt&gt;brick&lt;/tt&gt; to the VGBKernel (that's why I renamed VGKernel to VGBKernel, by the way--it's now a Video Game Brick Kernel), and I've changed ball into an extension of Rectangle to make collision detection a bit easier.&lt;br /&gt;&lt;br /&gt;To do the actual collision detection, I'm using the method &lt;tt&gt;intersects()&lt;/tt&gt; from the &lt;a href="http://download.oracle.com/javase/6/docs/api/java/awt/Rectangle.html"&gt;Rectangle&lt;/a&gt; class. It tests whether one Rectangle overlaps another in the coordinate space.&lt;br /&gt;&lt;br /&gt;In this case, if the ball is on top of the brick (a rather large brick), then the ball turns green. I could have had any of a number of other effects. For example, when I was first testing this routine I didn't change the color of the ball, instead I had the ball send a message to the Java console that read "Ow!". So whenever the ball passed over the brick I'd get a long string of messages in the console like this:&lt;br /&gt;&lt;br /&gt;&lt;pre style="color:#dd0000;"&gt;Ow!&lt;br /&gt;Ow!&lt;br /&gt;Ow!&lt;br /&gt;Ow!&lt;br /&gt;Ow!&lt;br /&gt;Ow!&lt;br /&gt;Ow!&lt;br /&gt;Ow!&lt;br /&gt;Ow!&lt;/pre&gt;&lt;br /&gt;I could just as well have the program play a sound, draw an image, update a score, or any of a number of different things when a collision occurs. Exactly what should happen is determined by the rules of the video game itself.&lt;br /&gt;&lt;br /&gt;&lt;b style="color:#ff7700;"&gt;Going Further&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Try changing the effect when the ball strikes the brick. The easiest place to start would be making the ball a different color than green. To get more sophisticated, try things like adding a sound, or counting the number of moves that the ball is in contact with the brick. Change the size and location of the brick. Perhaps try adding additional bricks, and have each create a different effect. Try changing the color of the brick when the ball strikes it.&lt;br /&gt;&lt;br /&gt;One thing about the collision detection we are doing here. It only detects when we have actually started to overlap with the brick. In many cases, it's desirable to &lt;i&gt;predict&lt;/i&gt; when a collision will occur as a result of a move before that move occurs. Try to develop a solution to this yourself. I'll be addressing this in a future article, so you can compare what you came up with against my method. Remember that in programming there's almost never just one way to accomplish something. The same effect can usually be produced any of a number of different ways effectively enough to use in a finished program.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-317341463367189420?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/EcfZL7DW98MnwqK2NPazRns01rI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/EcfZL7DW98MnwqK2NPazRns01rI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/EcfZL7DW98MnwqK2NPazRns01rI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/EcfZL7DW98MnwqK2NPazRns01rI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/jHOf71XrRaA" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/317341463367189420?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/317341463367189420?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/jHOf71XrRaA/simple-collision-detection-in-java.html" title="Simple Collision Detection in Java" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_dE7iaOkEf6s/TGyZf_70BCI/AAAAAAAABBE/llMNfBxWiAc/s72-c/VGBKernel.png" height="72" width="72" /><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/simple-collision-detection-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkINQ386cSp7ImA9Wx5SF08.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-3512178018059373547</id><published>2010-08-13T12:35:00.000-07:00</published><updated>2010-08-13T12:43:12.119-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-13T12:43:12.119-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="constructor" /><category scheme="http://www.blogger.com/atom/ns#" term="signature" /><category scheme="http://www.blogger.com/atom/ns#" term="inheritance" /><category scheme="http://www.blogger.com/atom/ns#" term="reference" /><title>Multiple Constructor Methods</title><content type="html">A special type of method that creates an instance of a method is called a &lt;a href="http://beginwithjava.blogspot.com/2008/07/constructor-methods.html"&gt;Constructor Method&lt;/a&gt;. When an object has member variables that are objects, we need to define a constructor method to set up those variables. We'll show how to do that here. If you want a more basic introduction to constructor methods, you may want to take a look at my &lt;a href="http://beginwithjava.blogspot.com/2008/07/constructor-methods.html"&gt;prior article&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;As an example, we're going to create a simple class of object for use with my &lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-java-video-game-kernel.html"&gt;simple video game kernel&lt;/a&gt; in a new version I'll be introducing in an article in the near future. The class definition consists mostly of constructor methods, since the class itself is presently not much more than a &lt;a href="http://download.oracle.com/javase/6/docs/api/java/awt/Rectangle.html"&gt;Rectangle&lt;/a&gt; with an added field.&lt;br /&gt;&lt;br /&gt;&lt;pre style="color:#00dd33;"&gt;import java.awt.*;&lt;br /&gt;&lt;br /&gt;// A Simple class for use in the simple video game examples.&lt;br /&gt;// Mark Graybill, Aug. 2010&lt;br /&gt;&lt;br /&gt;public class Brick extends Rectangle{&lt;br /&gt; Color brickColor;&lt;br /&gt;&lt;br /&gt; public Brick(int newX, int newY, int newWidth, int newHeight){&lt;br /&gt;  super(newX, newY, newWidth, newHeight);&lt;br /&gt;  brickColor = new Color(0, 128, 255);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public Brick(int newX, int newY){&lt;br /&gt;  this(newX, newY, 10, 10);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public Brick(){&lt;br /&gt;  this(0,0,10,10);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void setColor(Color newColor){ brickColor=newColor; }&lt;br /&gt; public Color getColor(){ return brickColor; }&lt;br /&gt;&lt;br /&gt;} // End Brick&lt;/pre&gt;&lt;br /&gt;In this class, we have three forms of constructor, each with a different set of parameters. The one that takes the most parameters is the "base" version. It starts with a call to super(). This calls the constructor for Brick's superclass, or parent class, Rectangle. A look at the documentation for Rectangle shows that it has a constructor that takes four integer arguments, as we use here in super().&lt;br /&gt;&lt;br /&gt;When we use super(), it must be the first thing we do within our constructor method. If we don't use super(), Java will do it automatically as the first thing in a constructor, calling it with no parameters. Since we want to set values for our inherited fields of x, y, width, and height it's better to call super() with those parameters. Otherwise, we could just as well have done something like this:&lt;br /&gt;&lt;pre style="color:#00dd33;"&gt;  public Brick(int newX, int newY, int newWidth, int newHeight){&lt;br /&gt;  x=newX;&lt;br /&gt;  y=newY;&lt;br /&gt;  width=newWidth;&lt;br /&gt;  height=newHeight;&lt;br /&gt;  brickColor = new Color(0, 128, 255);&lt;br /&gt; }&lt;/pre&gt;&lt;br /&gt;This would have the same effect, and Java would insert an invisible call to super() in front of &lt;tt&gt;x=newX;&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;The other constructors use the first method. To do this, they use another special method that's like super(). It's called this(), and it calls another constructor for this class. We can't do a call to Brick(), if we try, the compiler will see it as an undefined symbol:&lt;br /&gt;&lt;pre style="color:#ff0000;"&gt;&lt;br /&gt;&gt;javac Brick.java &lt;br /&gt;Brick.java:11: cannot find symbol&lt;br /&gt;symbol  : method Brick(int,int,int,int)&lt;br /&gt;location: class Brick&lt;br /&gt;    Brick(0,0,10,10);&lt;br /&gt;    ^&lt;br /&gt;1 error&lt;/pre&gt;&lt;br /&gt;So we use &lt;tt&gt;this()&lt;/tt&gt; instead.&lt;br /&gt;&lt;br /&gt;Like super(), the this() method must be the first thing called in the constructor method's body. Since don't call super()--it's in the base constructor--so there's no conflict about which goes first. If you use this(), you don't use super().&lt;br /&gt;&lt;br /&gt;By using this() with the other constructor methods, we can keep all our key code code for the constructor in one place. If we had each constructor setting member values and constants without calling the base constructor method, then we'd end up with repeated code--a prime opportunity for bugs to enter our code if we update the code in one place, but not another. We don't want repeated code! Multiple independent constructors are used in the Java Tutorials, but I expect they're used to keep the lesson simple, &lt;i&gt;not&lt;/i&gt; because they are a good coding practice!&lt;br /&gt;&lt;br /&gt;You can find out more about this() and super() in the &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html"&gt;Java Language Specification&lt;/a&gt;, under &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.8.7.1"&gt;Explicit Constructor Invocations&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;You can probably see that I should really have my base constructor allow a Color to be passed to it, too. Try adding this yourself as an exercise to try out your understanding of constructor methods, then see what &lt;tt&gt;javac&lt;/tt&gt; thinks of your work.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-3512178018059373547?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/H99-tFNBOqRRaOLKzivWAZP1AqQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/H99-tFNBOqRRaOLKzivWAZP1AqQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/H99-tFNBOqRRaOLKzivWAZP1AqQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/H99-tFNBOqRRaOLKzivWAZP1AqQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/OVWcaOVA8GU" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3512178018059373547?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3512178018059373547?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/OVWcaOVA8GU/multiple-constructor-methods.html" title="Multiple Constructor Methods" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/multiple-constructor-methods.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUIMSHo6cCp7ImA9Wx5SFUs.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-5128965538236713304</id><published>2010-08-11T14:52:00.000-07:00</published><updated>2010-08-11T14:53:09.418-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-11T14:53:09.418-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="inner class" /><category scheme="http://www.blogger.com/atom/ns#" term="classes" /><category scheme="http://www.blogger.com/atom/ns#" term="graphics" /><title>Java's Inner Classes: The Keys to the Kingdom</title><content type="html">In my two most recent code examples I've used inner classes in the program.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://beginwithjava.blogspot.com/2010/08/java-video-game-programming-game-logic.html"&gt;Java Video Game Programming: Game Logic&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-java-video-game-kernel.html"&gt;A Simple Java Video Game Kernel&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
An inner class is a Java class that's defined inside another class. In both the examples above, I have a class called VGKernel, which is the class that implements the video game kernel. Inside that class, I define other classes. This has a special effect on the relationship between those classes and VGKernel.&lt;br /&gt;
&lt;br /&gt;
Normally, our objects are &lt;i&gt;encapsulated&lt;/i&gt;, that is, their members (variables and methods) are inaccessible to other classes unless they've been marked as &lt;tt&gt;public&lt;/tt&gt;, and in those cases we take care to make sure that the object can't be messed up using those public members.&lt;br /&gt;
&lt;br /&gt;
Some things in Java don't work very well with the encapsulation. Graphics and Threads are two of them. Fortunately, there's a way around the encapsulation. It lets one object access all the members of another object as if it owned them.&lt;br /&gt;
&lt;br /&gt;
The downside is that this can be a dangerous coding practice, like global variables (check out the sort of problems&amp;nbsp; JavaScript's had with security, if you want to know why global variables are a problem.) Your inner class has complete control over the outer class. Root access, keys to the kingdom. It could wear the outer class like the alien guy wore the redneck's skin in Men in Black.&lt;br /&gt;
&lt;br /&gt;
If used well inner classes solve a multitude of problems. In the case of the two versions of VGKernel, it solves the problem of the ball being able to access the graphical context (at present, the Ball class doesn't really &lt;i&gt;need&lt;/i&gt; to, I'll admit, but we'll see more of why it does this as the code develops in future articles), and it allows VGTimerTask to access the methods of VGKernel.&lt;br /&gt;
&lt;br /&gt;
The Java Tutorial has a &lt;a href="http://download.oracle.com/javase/tutorial/java/javaOO/nested.html"&gt;pretty good section&lt;/a&gt; on inner classes, as one type of nested class.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-5128965538236713304?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/HyFnRtABuYXJ4AVGsN6Fo2hQqhQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HyFnRtABuYXJ4AVGsN6Fo2hQqhQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/HyFnRtABuYXJ4AVGsN6Fo2hQqhQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HyFnRtABuYXJ4AVGsN6Fo2hQqhQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/fNDwqS8m2iE" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/5128965538236713304?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/5128965538236713304?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/fNDwqS8m2iE/javas-inner-classes-keys-to-kingdom.html" title="Java's Inner Classes: The Keys to the Kingdom" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/javas-inner-classes-keys-to-kingdom.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C08EQHw7eSp7ImA9Wx5SE0U.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-3985549657036361861</id><published>2010-08-09T12:21:00.000-07:00</published><updated>2010-08-09T12:23:21.201-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-09T12:23:21.201-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="inner class" /><category scheme="http://www.blogger.com/atom/ns#" term="video game" /><category scheme="http://www.blogger.com/atom/ns#" term="classes" /><category scheme="http://www.blogger.com/atom/ns#" term="JFrame" /><category scheme="http://www.blogger.com/atom/ns#" term="graphics" /><category scheme="http://www.blogger.com/atom/ns#" term="JPanel" /><title>Java Video Game Programming: Game Logic</title><content type="html">The simple video game kernel I &lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-java-video-game-kernel.html"&gt;presented earlier&lt;/a&gt; is a good start, but some compromises were made to make it as simple as possible. Now we're going to start adding back some of the complexity to give us more control over what we can do with it.&lt;br /&gt;
&lt;br /&gt;
The first step is to make the game's ball a real object, rather than some variables within our JFrame.&lt;br /&gt;
&lt;br /&gt;
In simple video games, there is usually some object in the game that does most of the "thinking" with respect to the game's rules. In a game like Pong or Breakout, the ball does most of the "thinking". The paddles move according to user input, but they don't really need to know if they've collided with anything in the game (their own logic keeps them from moving off the playfield.) The bricks don't need to know anything about what's going on, they just need to hang around, then disappear when something tells them to, and perhaps return some information about what score they're worth, if there are different scores for hitting different bricks.&lt;br /&gt;
&lt;br /&gt;
The ball, however, is a more active participant in the game. It needs to know about collisions, respond to the other objects in the game, and generally know what's going on. In other words, it is the object that keeps track of most of the game rules. In a video game, this is referred to as the game logic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;img alt="A bouncing ball in the simple Java video game kernel program." src="http://lh4.ggpht.com/_dE7iaOkEf6s/TF3ymh-I3iI/AAAAAAAABAk/0IYBveA0zw0/Screenshot-VGKernel.jpg" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h5&gt;This simple kernel improves on &lt;a href="http://beginwithjava.blogspot.com/2010/08/simple-java-video-game-kernel.html"&gt;the original&lt;/a&gt;. Turn it into your own Pong, Breakout, or Tank clone.&lt;/h5&gt;&lt;/center&gt;&lt;br /&gt;
In the example below I've made a Ball class. This is where most of the game's logic will reside. Here it is in the ball's move() method. Since the ball will need access to inside information about the playfield and the objects on it, I've made it an inner class of the playfield's class (VGKernel.) As an inner class, it has access to all of VGKernel's members and methods as if it owned them itself. This simplifies the program a lot.&lt;br /&gt;
&lt;br /&gt;
Here is the modified version of the video game kernel. Changes from the original have been highlighted:&lt;br /&gt;
&lt;pre style="color: #dddd00; font-family: courier,monospace; font-size: 0.8em;"&gt;/* A simple video game style kernel, revision 2.
   by Mark Graybill, August 2010
   Uses an inner class to contain game logic,
   with another inner class as a game timer.
*/

// Import Timer and other useful stuff:
import java.util.*;
// Import the basic graphics classes.
import java.awt.*;
import javax.swing.*;
import java.lang.Math;

public class VGKernel extends JPanel{

// This is not a recommended coding practice, just a shortcut.
public Rectangle screen, bounds; // The screen area and boundary.
public JFrame frame; // A JFrame to put the graphics into.
public VGTimerTask vgTask; // The TimerTask that runs the game.
&lt;span style="color: #cc0000;"&gt;public VGBall ball; // The game ball.&lt;/span&gt;
// Create a constructor method:
  public VGKernel(){
    super();
    screen = new Rectangle(0, 0, 600, 400);
    bounds = new Rectangle(0, 0, 600, 400); // Give some temporary values.
   &lt;span style="color: #cc0000;"&gt; ball = new VGBall();&lt;/span&gt;
    frame = new JFrame("VGKernel");
    vgTask = new VGTimerTask();
}

  // Create an inner TimerTask class that has access to the
  // members of the VGKernel.
  class VGTimerTask extends TimerTask{
    public void run(){
      &lt;span style="color: #cc0000;"&gt;ball.move();&lt;/span&gt;
      frame.repaint();
    }
  }

&lt;span style="color: #cc0000;"&gt;  // Create an inner VGBall class that has our game logic in it.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;  class VGBall{&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;    // Accessor methods would be more proper,&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;    // but for now I just make the needed variables public.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;    public int x, y, width, height; // Ball's location and size.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;    int xVel, yVel; // The ball's velocity.&lt;/span&gt;

&lt;span style="color: #cc0000;"&gt;    public VGBall(){&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      x = 0;&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      y = 0;&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      width = 20;&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      height = 20;&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      xVel = width/4;&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      yVel = height/4;&lt;/span&gt;
    &lt;span style="color: #cc0000;"&gt;}&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;    // Instance methods for VGBall&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;    public void move(){&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      // Move the ball according to the game rules.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      x+=xVel; // Move horizontally.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      y+=yVel; // Move vertically.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      // Detect edges and bounce if necessary.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      if (x &amp;gt; (bounds.width - width)){&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;        xVel = -xVel; // reverse movement.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;        x = bounds.width -  width; // Set location to screen edge.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      }&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      if (y &amp;gt; (bounds.height - height)){&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;        yVel = -yVel; // reverse movement.&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;        y = bounds.height - height;&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      }&lt;/span&gt;
     &lt;span style="color: #cc0000;"&gt; if (x &amp;lt;= 0) { xVel = -xVel; x = 0; }&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;      if (y &amp;lt;= 0) { yVel = -yVel; y = 0; }&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;    }&lt;/span&gt;
&lt;span style="color: #cc0000;"&gt;  } // end of class VGBall&lt;/span&gt;

// Now the instance methods:
  public void paintComponent(Graphics g){
    // Get the drawing area bounds for game logic.
    bounds = g.getClipBounds();
    // Clear the drawing area, then draw the ball.
    g.clearRect(screen.x, screen.y, screen.width, screen.height);
    g.fillRect(ball.x, ball.y, ball.width, ball.height);
  }

  public static void main(String arg[]){

    java.util.Timer vgTimer = new java.util.Timer();  // Create a Timer object
    VGKernel panel = new VGKernel(); 
    
    panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.frame.setSize(panel.screen.width, panel.screen.height);

    panel.frame.setContentPane(panel); 
    panel.frame.setVisible(true);

    // Set up a timer to do the vgTask regularly.
    vgTimer.schedule(panel.vgTask, 0,&lt;span style="color: #cc0000;"&gt; 33&lt;/span&gt;);
  }
}&lt;/pre&gt;Now the ball not only is its own class, it has a more sophisticated means of keeping track of its direction of movement (xVel and yVel, tracking velocity as a positive or negative value, as opposed to the simple right and down booleans in the original.) This means that logic in the game could vary the velocity of the ball easily, as well as change its direction of movement.&lt;br /&gt;
&lt;br /&gt;
Other objects on the playfield should be defined in their own classes, outside VGKernel. This will keep VGKernel from becoming too long and from making the program too monolithic and difficult to maintain.&lt;br /&gt;
&lt;br /&gt;
Simple exercises:&lt;br /&gt;
Since the ball has access to VGKernel's members, including the drawing screen, the ball can be made to draw itself. By giving the ball its own draw() method, we can make it easier to change the appearance of the ball at will. Give it a try.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-3985549657036361861?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ATljvbVOkCzrWmS40f-2tGfONDQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ATljvbVOkCzrWmS40f-2tGfONDQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ATljvbVOkCzrWmS40f-2tGfONDQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ATljvbVOkCzrWmS40f-2tGfONDQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/foIiLrBcLuk" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3985549657036361861?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3985549657036361861?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/foIiLrBcLuk/java-video-game-programming-game-logic.html" title="Java Video Game Programming: Game Logic" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_dE7iaOkEf6s/TF3ymh-I3iI/AAAAAAAABAk/0IYBveA0zw0/s72-c/Screenshot-VGKernel.jpg" height="72" width="72" /><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/java-video-game-programming-game-logic.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkQHQHw9fip7ImA9Wx5SFUk.&quot;"><id>tag:blogger.com,1999:blog-2862144722617194417.post-3248326211307958893</id><published>2010-08-04T09:09:00.000-07:00</published><updated>2010-08-11T10:38:51.266-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-11T10:38:51.266-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="inner class" /><category scheme="http://www.blogger.com/atom/ns#" term="application" /><category scheme="http://www.blogger.com/atom/ns#" term="loop" /><category scheme="http://www.blogger.com/atom/ns#" term="threads" /><category scheme="http://www.blogger.com/atom/ns#" term="JFrame" /><category scheme="http://www.blogger.com/atom/ns#" term="graphics" /><category scheme="http://www.blogger.com/atom/ns#" term="paintComponent()" /><category scheme="http://www.blogger.com/atom/ns#" term="JPanel" /><title>A Simple Java Video Game Kernel</title><content type="html">Most video games run continuously, rather than waiting for user input before they do something. The heart of a video game of this sort is called the kernel. The kernel is running in the background, collecting user input and updating the display. It uses logic to determine if the game has been won or lost or otherwise control the state of the game.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;center&gt;&lt;img alt="A bouncing ball in the simple Java video game kernel program." src="http://lh4.ggpht.com/_dE7iaOkEf6s/TF3ymh-I3iI/AAAAAAAABAk/0IYBveA0zw0/Screenshot-VGKernel.jpg" /&gt;&lt;br /&gt;
&lt;h5&gt;This simple kernel sends a ball bouncing around on the screen. Turn it into your own Pong, Breakout, or Tank clone.&lt;/h5&gt;&lt;/center&gt;&lt;br /&gt;
To run this way, Threads are usually used to allow more than one thing to be going on at a time in a Java program. We've looked at a simple way of using threads before, the &lt;a href="http://beginwithjava.blogspot.com/2010/07/repetition-in-java-timer-class.html"&gt;Timer class&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Here's a really, &lt;i&gt;really&lt;/i&gt; simple video game kernel. It has all the basic elements of a video game.&lt;br /&gt;
&lt;pre style="color: #dddd00; font-family: courier,monospace; font-size: 0.8em;"&gt;&lt;span style="color: #6aa84f;"&gt;/* A simple video game style kernel
   by Mark Graybill, August 2010
   Uses the Timer Class to move a ball on a playfield.
*/

// Import Timer and other useful stuff:
import java.util.*;
// Import the basic graphics classes.
import java.awt.*;
import javax.swing.*;

public class VGKernel extends JPanel{

// Set up the objects and variables we'll want.
public Rectangle screen, ball; // The screen area and ball location/size.
public Rectangle bounds;  // The boundaries of the drawing area.
public JFrame frame; // A JFrame to put the graphics into.
public VGTimerTask vgTask; // The TimerTask that runs the game.
public boolean down, right; // Direction of ball's travel.

// Create a constructor method that initializes things:
  public VGKernel(){
    super();
    screen = new Rectangle(0, 0, 600, 400);
    ball   = new Rectangle(0, 0, 20, 20);
    bounds = new Rectangle(0, 0, 600, 400); // Give some starter values.
    frame = new JFrame("VGKernel");
    vgTask = new VGTimerTask();
}&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;  // Create an inner TimerTask class that has access to the&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;  // members of the VGKernel.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;  class VGTimerTask extends TimerTask{&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    public void run(){&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;      moveBall();&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;      frame.repaint();&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    }&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;  }&lt;/span&gt;

&lt;span style="color: #6aa84f;"&gt;// Now the instance methods:&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;  public void paintComponent(Graphics g){&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    // Get the drawing area bounds for game logic.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    bounds = g.getClipBounds();&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    // Clear the drawing area, then draw the ball.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    g.clearRect(screen.x, screen.y, screen.width, screen.height);&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    g.fillRect(ball.x, ball.y, ball.width, ball.height);&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;  }&lt;/span&gt;

&lt;span style="color: #6aa84f;"&gt;  public void moveBall(){&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;  // Ball should really be its own class with this as a method.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    if (right) ball.x+=ball.width; // If right is true, move ball right,&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    else ball.x-=ball.width;       // otherwise move left.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    if (down)  ball.y+=ball.height; // Same for up/down.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    else ball.y-=ball.width;&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    if (ball.x &amp;gt; (bounds.width - ball.width)) // Detect edges and bounce.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;      { right = false; ball.x = bounds.width -  ball.width; }&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    if (ball.y &amp;gt; (bounds.height - ball.height))&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;      { down  = false; ball.y = bounds.height - ball.height;}&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    if (ball.x &amp;lt;= 0) { right = true; ball.x = 0; }&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    if (ball.y &amp;lt;= 0) { down  = true; ball.y = 0; }&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;  }&lt;/span&gt;

&lt;span style="color: #6aa84f;"&gt;  public static void main(String arg[]){&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    java.util.Timer vgTimer = new java.util.Timer();  // Create a Timer.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    VGKernel panel = new VGKernel(); // Create and instance of our kernel.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    &lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    // Set the intial ball movement direction.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    panel.down = true;&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    panel.right = true;&lt;/span&gt;

&lt;span style="color: #6aa84f;"&gt;    // Set up our JFRame&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    panel.frame.setSize(panel.screen.width, panel.screen.height);&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    panel.frame.setContentPane(panel); &lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    panel.frame.setVisible(true);&lt;/span&gt;

&lt;span style="color: #6aa84f;"&gt;    // Set up a timer to do the vgTask regularly.&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;    vgTimer.schedule(panel.vgTask, 0, 100);&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;  }&lt;/span&gt;
&lt;span style="color: #6aa84f;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
This example can be expanded with methods to get control inputs, additional players on the playfield (like paddles), and logic to determine when someone scores.&lt;br /&gt;
&lt;br /&gt;
The code here is far from perfect, but I've made some compromises to make things as simple as I could while still showing a full working example. Not that any code that runs and does what is supposed to is really bad, but there are other, better ways of doing this. But this works and is fairly easy to understand.&lt;br /&gt;
&lt;br /&gt;
What the program does is create a JPanel that has an inner class (a class defined within itself) of VGTimerTask. The VGTimerTask is a kind of TimerTask, which can be scheduled to occur on a regular basis by a Timer. Since VGTimerTask is an inner class of VGPanel, it has access to all the members of VGPanel. This is critical. Without that, it wouldn't be able to access the ball and redraw the screen easily (it can still be done, but in a more complex way.)&lt;br /&gt;
&lt;br /&gt;
Timer is a decent way of running a simple game, but more complex games should use some other timing mechanism. java.util.Timer is affected by a number of outside events, so to get smoother, more reliable timing you a timer like the one in the Java3D package would work better.&lt;br /&gt;
&lt;div style="color: #e69138;"&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;&lt;div style="color: #e69138;"&gt;&lt;b&gt;A Simple Improvement&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;
There are many ways of improving on this basic example. One way that is very simple is to smooth the animation. The movement of the ball is pretty jerky. This is caused by both the distance that the ball moves each "turn", and by the time between screen updates. We can smooth out the animation by addressing both of these.&lt;br /&gt;
&lt;br /&gt;
First, let's change moveBall() to shift the ball a smaller distance each time:&lt;br /&gt;
&lt;pre style="color: #6fa8dc; font-family: courier,monospace; font-size: 0.8em;"&gt;public void moveBall(){
  // Ball should really be its own class with this as a method.
    if (right) ball.x+=ball.width&lt;span style="color: #cc0000;"&gt;/4&lt;/span&gt;; // If right is true, move ball right,
    else ball.x-=ball.width&lt;span style="color: #cc0000;"&gt;/4&lt;/span&gt;;       // otherwise move left.
    if (down)  ball.y+=ball.height&lt;span style="color: #cc0000;"&gt;/4&lt;/span&gt;; // Same for up/down.
    else ball.y-=ball.width&lt;span style="color: #cc0000;"&gt;/4&lt;/span&gt;;
    if (ball.x &amp;gt; (bounds.width - ball.width)) // Detect edges and bounce.
      { right = false; ball.x = bounds.width -  ball.width; }
    if (ball.y &amp;gt; (bounds.height - ball.height))
      { down  = false; ball.y = bounds.height - ball.height;}
    if (ball.x &amp;lt;= 0) { right = true; ball.x = 0; }
    if (ball.y &amp;lt;= 0) { down  = true; ball.y = 0; }
  }&lt;/pre&gt;Now the ball is being moved only one quarter of its size each turn.&lt;br /&gt;
&lt;br /&gt;
Next, change the Timer schedule to draw the screen every 20 milliseconds instead of every 100 milliseconds:&lt;br /&gt;
&lt;pre style="color: #dddd00; font-family: courier,monospace; font-size: 0.8em;"&gt;/&lt;span style="color: #3d85c6;"&gt;/ Set up a timer to do the vgTask regularly.
    vgTimer.schedule(panel.vgTask, 0, &lt;/span&gt;&lt;span style="color: #e06666;"&gt;20&lt;/span&gt;&lt;span style="color: #3d85c6;"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
Now you have a ball that moves a lot smoother.&lt;br /&gt;
&lt;br /&gt;
I'll be expanding on this basic kernel and improving it in future articles, starting with &lt;a href="http://beginwithjava.blogspot.com/2010/08/java-video-game-programming-game-logic.html"&gt;Java Video game Programming: Game Logic&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2862144722617194417-3248326211307958893?l=beginwithjava.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zfdiDWHZesAPjQ-qWc1YvxYZNiE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zfdiDWHZesAPjQ-qWc1YvxYZNiE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zfdiDWHZesAPjQ-qWc1YvxYZNiE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zfdiDWHZesAPjQ-qWc1YvxYZNiE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/ABeginningProgrammersGuideToJava/~4/QtQ9ThMz02I" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3248326211307958893?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2862144722617194417/posts/default/3248326211307958893?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/ABeginningProgrammersGuideToJava/~3/QtQ9ThMz02I/simple-java-video-game-kernel.html" title="A Simple Java Video Game Kernel" /><author><name>Mark A. Graybill</name><uri>http://www.blogger.com/profile/05472603072142005189</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="24" height="32" src="http://bp2.blogger.com/_dE7iaOkEf6s/SG0NVJ77v4I/AAAAAAAAAPM/xieZWPw6Dw4/S220/selfpic_sm.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_dE7iaOkEf6s/TF3ymh-I3iI/AAAAAAAABAk/0IYBveA0zw0/s72-c/Screenshot-VGKernel.jpg" height="72" width="72" /><feedburner:origLink>http://beginwithjava.blogspot.com/2010/08/simple-java-video-game-kernel.html</feedburner:origLink></entry></feed>

