MCS273 Introduction to C++ Programming (January 2013)

Some introductory graphics programs


Overview

The goals of this lab are twofold: to give you some experience using the graphics system developed by Horstmann, and to give you some more practice writing C++ programs.

I am providing the following auxiliary document which you should peruse before doing the corresponding part of the lab:

Importing a Project with Horstmann's graphic files (1 point)

  1. Download the file HorstmannGraphics.zip to convenient location (in Chrome, for example, you do this by right clicking on the link and then selecting "Save Link As ...").

  2. Go to Eclipse, right-click on open in the Project Explorer, and select "Import...".

  3. Open the "General" triangle, select "Existing Projects into Workspace", and click Next.

  4. Click the "Select archive file" radio button, browse for and select the HorstmannGraphics.zip, file and push the open button. After doing that, press the Finish button, and the project should be in your Eclipse workspace.

  5. Build the file in Eclipse and then run one or more of the executable files in a Terminal window.

  6. Take a look at some of the cpp files as well as the makefile.

  7. Check-off: Show me that you have imported the project correctly and that you have run a program in Terminal.

  8. Following are the other two archived projects I will be using in the class today:


    You will probably want to import these two projects as well.

Drawing triangles (1 point)

The goal of this part of the lab is to write the program that prompts the user to click on three points and then draws a triangle joining the three points.

  1. Create a new C++ makefile project called GraphicsFun. Copy the cccfiles directory from the project HorstmannGraphics into GraphicsFun. You will do all your work for this lab in the project GraphicsFun.

  2. Create a new source file called triangle.cpp and copy the contents of the file triangle.cpp into it. The program triangle.cc is the shell of the program you are going to expand.

  3. You will next need to create the makefile that will will do the necessary building for this lab. You should of course model it on the makefile in the HorstmannGraphics. In fact, you might as well copy that makefile into your project and modify it there.

  4. Now add the necessary code to triangle.cpp. Hint: To give the user feedback about the click, it is a nice touch to draw the point after each click.

       Point p = cwin.get_mouse("Please click on the first point");
       cwin << p;  /* Feedback for the user */
    

  5. Check-off: Show me your completed code and demonstrate that it works as stipulated. Also, show me your makefile and demonstrate that it causes the correct compilations to occur.

Drawing a clock (1 point)

The goal of this part of the lab is to write the program that draws a clock face with the current time:



Hint: You need to determine the angles of the hour hand and the minute hand. The angle of the minute hand is easy: The minute hand travels 360 degrees in 60 minutes. The angle of the hour hand is harder; it travels 360 degrees in 12 * 60 minutes.

In addition, you are going to have to modify makefile so that it will simultaneously maintain both this program and the one you just wrote.

  1. Create a shell of a program called, say, clock.cpp, in the GraphicsFun project. You will need to add cccfiles/ccc_time.o to the LDFLAGS variables, as well as the -lm.

  2. Now add the necessary code to clock.cpp so that it draws a clock face with the current time. Be sure that you don't use magic numbers in your code; use constants instead. Note that it's easiest to measure all angles clockwise from midnight.

  3. Check-off: Show me your completed code and demonstrate that it works as stipulated. Also, show me your makefile and demonstrate that it causes the correct compilations to occur.

Drawing a better clock (1 point)

  1. Write another program called, say, better-clock.cpp the add twelve ``tick-marks'' around the clock you drew in the previous part corresponding to the twelve hours. Be sure to update Makefile so that it maintains all three programs.

  2. You are free to use a for loop even though you haven't read about it in the book yet. The following piece of C++ code outputs the numbers from 1 to 12:

       for (int i = 1; i <= 12; i++) {
          cout << i << endl;
       }
    

  3. (optional improvement) Add text to the clock giving the twelve hours. Be sure the text is placed in a visually appealing manner.

  4. Check-off: Show me your completed code and demonstrate that it works as stipulated.