How I Met That Java Package

Posted: June 8, 2011 by Jinkchak in Programming
Tags: , , , , , , ,

So, kids! You know, life is very mysterious. I stumbled upon the “CS106A” YouTube channel, quite by accident (and I’ll leave that story for another time). I wonder how I would have felt about Java if I hadn’t – I shudder at the thought. It was the lectures in this channel, and particularly, the marvelous way in which Professor Mehran Sahami taught them that really made me discover things I hadn’t seen earlier.

I was first introduced to Karel The Robot, and that was how I met that Java package…Just kidding. This is not that package. Hypothetical Hi-Five! Awesome!

Anyway, Karel is a programming language, named after Karel Capek, a Czech writer who coined the word “robot”. With just a few, clearly defined instructions, Karel can be programmed to perform simple tasks in a world comprised of grids. The entire documentation on Karel is available @ http://www.stanford.edu/class/cs106a/book/karel-the-robot-learns-java.pdf .

You'll understand this once you start using Karel

But the story doesn’t end here. To understand what happens in the not too distant future, let’s go back a few days. I had just reached this website – http://www.stanford.edu/class/cs106a/WWW-winter/index.html . I was wondering what to do here, until I began reading http://www.stanford.edu/class/cs106a/WWW-winter/index.html . I came across the link to Stanford Eclipse @ http://www.stanford.edu/class/cs106a/software/eclipse-helios-pc.zip. I installed it, and was clearly impressed with the new Stanford Menu that had been incorporated in Eclipse, to extend its functionality.

Then, I read this – http://www.stanford.edu/class/cs106a/WWW-winter/handouts/06-karel-in-eclipse.pdf, and downloaded http://www.stanford.edu/class/cs106a/WWW-winter/assignments/Assignment1.zip .

The Assignment came with the “karel” package. All I had to do now, was import this assignment using the Stanford Menu in Eclipse, and voila! I could begin playing around with Karel. And I had a marvelous time. A few months down the line, the good folks of Stanford introduced Karel in JavaScript, which is available here – http://stanfordkarel.appspot.com/ . With that, Karel can now be directly programmed in any web browser. If you want to see how, just copy and paste the code available @ http://www.pastie.org/pastes/2036764/text in that box @ : http://stanfordkarel.appspot.com/

A few days after I played around with the robot, I saw a few more lectures, and it was then that I found out about the mysterious “acm.jar” file. At first, I passed it off as just another lame package. I thought to myself, “There are so many packages in Java. Why do I need some more?” – How wrong I was! I was too lazy to download the blank project from the CS106A website @ http://www.stanford.edu/class/cs106a/materials/BlankProject.zip , because of my silly internals and because my college was getting on my nerves. Instead, I continued working with “standard” Java. Fast forward to a few days later, and you’d find that I did download that project. How did I reach that situation? Well, you see, kids, I was reading a book authored by Patrick Naughton, and the guy had me totally confused by the time I reached the chapter on Applets. Why was I reading that book? It had been recommended by my college. On top of that, what really gave me the creeps was the fact that the book discussed only old, deprecated methods.

So, one day, I decided I had had enough of this nonsense. And in this state of fury, I watched http://www.youtube.com/watch?v=NPzPnycCFuE&feature=BFa&list=SP84A56BC7F4A1F852&index=5 . Seeing the benefits of this package, I downloaded http://www.stanford.edu/class/cs106a/materials/BlankProject.zip, imported it into Stanford Eclipse using the Stanford Menu and learned how to use the “ACM” package from http://www.youtube.com/playlist?p=PL84A56BC7F4A1F852 , and that, kids, was how I met that Java package. I suddenly realized how easy Java was, and how beautiful object-oriented programming really is, and since then, life has never been the same again.

With that package, I could choose whether my program needed a graphical interface or a command-line interface, by importing acm.graphics.*; or acm.program.*; respectively, or I could combine both if I wanted. This package had been made by the Java Task Force, and I learned from http://jtf.acm.org/ that the ACM JTF had been convened “To review the Java language, APIs, and tools from the perspective of introductory computing education and to develop a stable collection of pedagogical resources that will make it easier to teach Java to first-year computing students without having those students overwhelmed by its complexity”, with Eric Roberts of Stanford University as its chairman. To understand what this package is capable of, visit http://www.stanford.edu/class/cs106a/WWW-winter/handouts/

A few weeks later, I had learned enough to re-create the “Breakout” game, first developed by Atari, in 1976. I’m sure you are terribly familiar with this game and if you’re not, here’s something that will refresh your memory – http://en.wikipedia.org/wiki/Breakout_%28video_game%29 . I know it’s not great or anything, but I just want to show you what you can do with this awesome package. So don’t say I didn’t warn you!

To play the game, visit https://www.filesanywhere.com/fs/v.aspx?v=8a6c65895e5e73a66ca0

Looking on the bright side, I’ll show you how to create it by giving you the source code too @ http://pastie.org/2036162 . There’s also a way to run these programs within your browser (after all, the GraphicsProgram Class just extends the functionality of the Applet Class). What this means is – you don’t have to use the line “java –cp .;acm.jar Breakout” to run the program using the command prompt.  All you need to do is click on “Display.html”, and everything else will be done for you. Use the mouse as the control device.

When the game starts, along with some background Music, you’ll see 100 bricks on the top, a ball in the center, and a block (called a paddle) at the bottom. The paddle can be moved only in the horizontal direction using the Mouse. To start playing, just click the left mouse button. The ball moves off with random velocity, and as you progress further, the speed of the ball increases. On contact with a brick, the ball gets deflected after it destroys it. Depending on which part of the paddle the ball comes in contact with, the ball gets deflected by an appropriate angle. During all this, you’ll find that every object  (except for the image) keeps changing colours randomly. Why? For some reason, the changing colours give me a sense of tension! The main objective of the game is to destroy all the bricks without allowing the ball touch the bottom, and you’re allowed to lose twice, before you fail completely, and the theme music of Alfred Hitchcock’s “Psycho” starts playing in the background.

Visit https://www.filesanywhere.com/fs/v.aspx?v=8a6c65895e5e73a66ca0

Unfortunately, I don’t know how to bypass the security of Java in order to make this game playable using a web server. I uploaded this game to some free web-hosting servers, and tried running the applet from there. Since the program reads image and audio files such as .jpg, .au, etc., from the disk, it wasn’t allowed to run. The console told me that this was just a precaution to ensure security – Applets aren’t allowed to read and write to a client’s disk, unless the applet is being run offline, which would be the case when you download and run the program from https://www.filesanywhere.com/fs/v.aspx?v=8a6c65895e5e73a66ca0 .  I would be grateful if anyone could tell me if there’s a way to overcome this.

..And that, kids, was how I met that Java package. It was legen…wait for it…dary. LEGENDARY!

So, kids, I’m sorry that this article wasn’t extremely long, and sorry I couldn’t make you all sit down for 6 seasons (and more to come)… 😛

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s