CSC 103

Homework #5: A Java Program

Due on Moodle or paper at 2:40 PM on Wednesday, 20 October

For this project, you will write part of a program in Java, a high-level language developed in the early 1990's. Java has a number of particularly attractive features, including security and portability. It also includes an extensive library of commands for creating graphics, many of which are not very difficult to use.

For this assignment, most of the program will be provided to you. Your contribution will be to write a portion of the program that determines what will appear on the screen, compile the program using an on-line compiler service, and run it by double-clicking on the jar file produced. (Normally, people would simply compile programs using a program running on their own computer. But to avoid having to install and learn to use the Java compiler, we are using this shortcut.)

Exploring the Program

You will begin with a fully functional program, and modify it. Here is the program. Before making any changes, you should practice compiling and running it as is. Here are the steps (this may seem long, but once you have done it a few times it should go smoothly):

  1. Save the program somewhere on your computer (probably on your H: drive) by right-clicking the link and selecting either Save Target as (on Internet Explorer) or Save Link as (on Firefox). Be sure to remember where you put the file.
  2. Go to the web compiler service page. Enter your name in the name box, and then click the Browse button to locate your copy of the program. Select it. The path to this file should now be listed in the adjacent text box.
  3. Click the Make me a JAR file! button at the bottom of the page. After a short time, you should see a page showing the results. If all goes well, there should be a link to your completed .jar file at the bottom. If there are errors displayed, fix them and try again.
  4. Click the .jar file link on the compiler results page and save it on your H: drive. This file contains the compiled Java bytecode for your program. You can double-click it to run, if Java is installed on your computer.
  5. When you run the program, it should pop up a window with the graphics you specified. If you wish to make further changes to see how they look, repeat the steps above.

Understanding the Program

What's happening in this program? We will deliberately omit any discussion of what is going on in most of the program; there is simply too much to go into. However, we can understand the part of the program that actually determines what appears on the screen. Open GreetingCard.java using a text editor like NotePad. Locate the portion that looks like this:

    // Your greeting card drawing commands will go in this section.
    // You can erase the sample commands shown here.
    // MAKE NO CHANGES ABOVE THIS LINE!
    g.setColor(Color.red);
    g.fillOval(0,0,320,240);
    g.setColor(Color.black);
    g.drawString("Greetings!",130,115);
    // MAKE NO CHANGES BELOW THIS LINE!
    // This is the end of the section where you can insert commands.

Of these nine lines, five are comments, meaning they are meant for humans reading the program. The compiler ignores these comment lines, which are identified by the double slash marks at the beginning. The remaining four lines, all beginning with g, tell the computer to do something to the graphics display. They are set up almost as instructions to an artist making a picture. Here is what they mean, more or less:

Java Command Meaning
g.setColor(Color.red); Pick up a red pen. (Everything drawn next will be red.)
g.fillOval(0,0,320,240); Draw a filled-in oval that fits inside a rectangle 320 pixels wide by 240 pixels tall. The upper left corner of the rectangle (not the oval) should be at pixel (0,0), which happens to be the upper left corner of the window.
g.setColor(Color.black); Pick up a black pen. (Everything drawn next will be black instead of red.)
g.drawString("Greetings!",130,115); Put some writing in the window. The lower left corner of the text will be at pixel (130,115), which is somewhere near the middle of the window, but a little to the left. The writing should say, "Greetings!"

Playing with the Program

Once you have the program working, experiment a little! You may change any of the lines in the middle (the ones beginning with g), and note the result. Try the following to begin:

  1. Change the order of the different lines. Can you explain how this changes what you see?
  2. Change the colors. You may choose from: black, blue, cyan, gray, green, magenta, orange, pink, red, white, and yellow. (Make sure you leave the preceding Color. intact.)
  3. Change the numbers you see -- replace the 320 with 160, for example. Can you figure out what is happening?
  4. Add additional shapes, with numbers you choose. Try to set the sizes and positions such that the new shapes will be at least partly visible.

If you are feeling up to it, here are some additional commands you can put in your program, and alter as you see fit:

Java Command Meaning
g.fillRect(20,40,50,30); Draws a filled-in rectangle fifty pixels wide by 30 pixels high, whose upper left corner is at pixel (20,40).
g.drawRect(20,40,50,30); Like g.fillRect(20,40,50,30); only you get just the outline of the rectangle.
g.drawOval(0,0,320,240); Like g.fillOval(0,0,320,240); only you get just the outline of the oval.
g.drawLine(130,120,190,120); Draws a line between pixel (130,120) and pixel (190,120).

Further options are available for those interested in exploring more on their own.

What to hand in

Your homework assignment is to alter the greeting card to make a design that you like. Your altered card should include at least three different graphic elements (e.g., shapes, lines, text, etc.), using at least three different colors. Some of the elements should be overlapped, so that the order they were drawn makes a difference.

For a top grade, show that you can apply some of the more advanced techniques shown in the examples, such as polygons or font changes -- or explore and develop your own. I'm looking for a demonstration that you can generalize from the examples covered in class to implement other similar techniques. Challenge yourself! If you have something in mind that is not demonstrated in the examples, check with the professor to see if it is possible. Making part of your card dependent on user input is an excellent goal, and opens up many possibilities. Creativity and beauty, while not explicitly graded, certainly don't hurt.

Turn in a copy of the final program, along with a screen capture of your finished greeting card.

Hints