CPSC 124, Fall 2005
Lab 5: Art(?) with For and Switch

There is only one exercise for this lab: You will write a program that draws several different types of "random art." From a practical point of view, the exercise will let you work with switch statements and for loops, but hopefully it will also be fun. You will be given a start-up file that does a lot of the set-up for the program. You will just have to fill in the paint routine that does the actual drawing.

The program that you will write can be run as either an application or as an applet. As an applet, it can appear on a Web page. Here is my own version of a completed RandomArt applet (but understand that you don't have to do exactly the same kind of "art"):

As an application, the program simply opens a window that shows the same applet that would appear on a web page. To run an application version of the program, go to the directory /classes/f05/cs124/lab5 and use the command java -jar RandomArt1Complete.jar. Alternatively, using the GUI, navigate to the directory. Right-click the icon for the file RandomArt1Complete.jar, and select "Open With", then "Other". When the dialog box pops up, enter java -jar in the input box and click OK. (If you want to always use the java -jar command automatically to open JAR files, check the "Remember application association for this type of file" option before you click OK. If you do this, you just have to click an executable JAR file to run it.)


Linux Tip-of-the-Week

Accessing Your "M" Drive: On the HWS Windows network, your files are stored on a network drive usually referred as your "M" drive. This drive is actually a directory on a computer named fsrv. The name of the directory is the same as your username, with a $ added onto the end. It is possible to access your M drive from Linux. Open a window to your home directory, for example by clicking the Home icon at the bottom of the screen. In the "Location" box at the top of the screen, enter:  smb://fsrv/username$  where username is your two-letter-plus-four-digit user name. A dialog box will pop up where you will be asked to enter your user name (again) and your password -- use your Windows password. You will be able to browse the files on your M drive, and you can copy files to and from your M drive just by dragging them. Note that to read or print Microsoft Word documents, you can use OpenOffice Writer (under "Office" / "Word Processor" in the Start menu -- or just drag the Word document icon onto the Office icon on your desktop).

By the way, the "smb" in the location name refers to the protocol used on Windows network. (A protocol is a specification of what can be done over a network connection and how to do it.) The KDE file browser, Konqueror, knows about other network protocols besides SMB. For example, if you know about FTP (file transfer protocol) or the secure version SFTP, you might be interested to know that you can do FTP and SFTP file transfers in Konqueror. Just enter ftp://computername or sftp://computername in the "Location" box.


Random Art

To begin the lab, open Eclipse. To organize your work, I suggest that you create a new project, named, for example, Lab 5. The folder /classes/f05/cs124/lab5 contains a file named RandomArt1.java that you should use as a starting point for today's lab. Import this file from the file system into your Eclipse project. Once you have RandomArt1.java in Eclipse, you can run this file as a Java Application. (If you don't remember how to do this from last week's lab, please ask for help.)

The RandomArt1.java starter file shows a single type of "art", consisting of a single random line drawn in a random color on a black background. The pattern changes every 5 seconds. The drawing is done in the paint() subroutine. The system calls this subroutine once every 5 seconds. This is set up in other parts of the file, but you don't need to understand any of that for now. The only part of the program that you have to change is the inside of the paint() subroutine.

Exercise: Your task is to re-write the paint() subroutine so it can draw several different types of "art". You should use a switch statement to select among the different types of drawing that your program can do. (We did an example in class that showed how to use a switch statement to randomly select one of several alternatives.) Each case of the switch statement will do a different type of drawing. You must make at least three different types of drawing, and you must use a for statement in each case. For one easy case, you can enclose the random line-drawing code that is already there in a for loop to make it draw 100 or even 1000 lines instead of just one. For the other cases, you can imitate what I did in my own program, or you can use your own imagination. Some part of the grade will be based on artistic merit.

Lab 2 introduced some graphics subroutines that you can use for drawing. You can find more information in Section 6.1 of the textbook. One useful subroutine that was not mentioned in Lab 2 is

My program uses g.fill3DRect() in several cases for drawing rectangles and squares. RandomArt1.java defines another useful function, randomColor(s,b), which is used for creating random colors. This function produces a color with a random hue and with a specified saturation and brightness. The saturation, s, and the brightness, b, must be real numbers in the range 0 to 1. (You can probably best understand these terms by experimenting with the function.) The randomColor function is meant to be used for setting a random color for drawing. For example:

You might be interested to know how I do some of my random art. For the drawing that shows random squares on a black background, I first choose a random integer less than 200 for the size of the square. For the location (x,y) of the top-left corner of the square, I let x be a random integer less than width - size, and I let y be a random integer less than height - size. I use a random saturated, but not-too-bright color for the square.

The drawings that show vertical and horizontal bars on a randomly colored background are based on an exercise used in CS124 by Professor John Vaughn a few years ago. (He claims that the drawings are similar to paintings by the artist Piet Mondrian.) In these drawings, to draw one of the bars, I first decide randomly whether to make it horizontal or vertical bar. For a horizontal bar, I use g.fill3DRect(0,y,width,h), where h is a randomly chosen integer less than 15 that gives the thickness of the bar, and y is a randomly chosen integer less than height that gives the location of the bar. Vertical bars are similar.


Exporting a JAR File

If you want to run your program outside of Eclipse, you can do so as usual, with the command java RandomArt1. Note, however, that you need two class files, RandomArt1.class and RandomArt1$1.class to run the program. (Both class files are generated when you compile RandomArt1.java.) You can make it easier to run the program by bundling it into an executable JAR file, which can be run by clicking an icon. In Eclipse, you can do this with the Export command. This is not a required part of the lab, but if you want to try it, here is how:

Right-click on the project name in the region at the left of the Eclipse window, and select "Export" from the pop-up menu. In the dialog box that appears, click on "JAR file" and then click the "Next" button. In the next screen, you have to specify what to include in the JAR file. The only thing that really needs to be checked is the file RandomArt1.java. You also have to specify a name and location for the JAR file. Use the "Browse" button to do this -- select the destination directory, such as Desktop, and enter a name for the JAR file, such as RandomArt1.jar. You still have to make the JAR file executable. This means specifying the class that contains the main program. Click the "Next" button twice to get to the "JAR Manifest Specification" screen. Click the "Browse" button next to the "Main Class" box near the bottom of the screen. In the pop-up dialog, click on RandomArt1, which is the class that contains the main program, and click "OK". Finally, back in the main screen, click the "Finish" button. If you've done everything correctly, you should have an executable JAR file.


Publishing an Applet

You might like to publish your work on the Web (even though it is not a required part of this lab).

You can publish web pages in your Linux account. There is a directory named www in your Linux home directory. Anything that you put in this directory is published on the Web. This directory is visible on the Web at the address  http://math.hws.edu/~username  where username is your user name. (Note the "~" character in front of the username.) If you put a file named lab5.html in this directory, then its address will be http://math.hws.edu/~username/lab5.html .  You could also make a directory named cs124 inside your www directory, and put lab5.html in that directory. In that case, the web address of the file will be http://math.hws.edu/~username/cs124/lab5.html .

Web pages are usually written in a language called html. You can learn some of the basics of this language in Section 6.2 in the textbook. However, to get started, you can simply type the following (or copy-and-paste it from this web page) into a file named lab5.html:

     <html>
     <head>
     <title>CS 124 Lab 5/title>
     </head>
     <body>
     
     <h1 align=center>My Random Art Applet</h1>
     
     <p align=center>
     <applet code="RandomArt1.class" width=500 height=500>
     </applet>
     </p>
     
     </body>
     </html>

Place the lab5.html file in your www directory, or into a subdirectory of that directory. You must also copy the class files RandomArt1.class and RandomArt1$1.class into the same directory that contains lab5.html. Once you've done that, you should be able to view your random art in any web browser, as long as it supports Java 1.4 or higher.


David J. Eck, September 2005