Java 101: My First Applet

Holy crap! It draws shapes using a random number generator!

I have created this useless and ugly applet as part of the amazing (no sarcasm — it’s been a ball) learning experience that is my first Java class.

It draws cascades of squares or circles based on whether the random number generator inside the program pulls a 1 or a 2 out of its ass. But I don’t care how silly it is, because this is the very first program I’ve written that I’ve been able to upload to the web.

I now have the knowledge (and hence, the power) to build a really crappy website in Java. That power is awesome.

The prof said that one of her CS students converted all his homework assignments into applets, threw ’em up on the web, and got a programming job that way. As mind-boggling, nay, stupefying, as that is, it’s nice to know that with some hard work and perseverance behind me, I’m seeing a truly useful application of my new skills. With some more hard work and perseverance, I hope to call myself a halfway decent programmer one of these days.

Here’s what is laughingly referred to as my source code:


// ShapesApplet.java
// draws shapes in broswer based on random number generator

import java.util.Random;
import java.awt.Graphics;
import javax.swing.JApplet;

public class ShapesApplet extends java.applet.Applet
{
int choice;

public void init()
{
// generate a random number & assign it to variable choice
Random randomNumbers = new Random();
choice = 1+ randomNumbers.nextInt( 2 );
}

// draws a cascade of shapes starting from the top-left corner
public void paint( Graphics g )
{
super.paint( g );

for ( int i = 0; i < 10; i++ )
{
switch ( choice )
{
case 1: // draw rectangles
g.drawRect( 10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10 );
break;
case 2: // draw ovals
g.drawOval( 10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10 );
break;
} // end switch
} // end for

g.drawString( "Programmed by Jolie O'Dell", 50, 300 );

} // end method paint
} // end class ShapesApplet

Image courtesy of journeyscoffee.

3 thoughts on “Java 101: My First Applet

Comment

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