CSCI A201/A597

Lab Notes Six

Second Summer 2000


Use this lab to make sure you get all your questions about chapter 4, take home quiz 4 and program assignment 4 answered.

Here are some hints, on the problems and quiz questions:

  1. What is the difference between an applet and an application?

    The main point here is where the main method is.

  2. What is the difference between a browser and the applet viewer?
    Limited scope of responsibilities, but what does that mean?

  3. Why do you need an HTML page to run an applet?
    Can you do it otherwise?

  4. Who calls the paint method of an applet? When does the call to the paint method occur?
    We've talked about this in class on Wednesday.

  5. Why does the parameter of the paint method have type Graphics and not Graphics2D?
    The answer is in the book on page 148 (I think).

  6. What is the purpose of a graphic context?
    What does a graphic context store about the environment in which the application runs? The books has a few things about that. See previous questions for where that might be in the book.

  7. How do you specify a text color?
    See page 155 for a start. The answer should be short though.

  8. What is the difference between a font and a font face?
    When you create a font, do you need to specify a face? If you do, is there anything else you need to specify? The section on fonts starts on page 153.

  9. What is the difference between a monospaced font and a proportionally spaced font?
    Page 156 has the answer (I think).

  10. What are serifs?
    Page 154 (but I am not sure).

  11. What is a logical font?
    It's the type of font in which Lewis Carroll's books were printed when he was signing Charles Dodgson. See page 154.

  12. How do you determine the pixel dimensions of a string in a particular font?
    By this we mean if we write "Hello" on the screen in a certain font (face, style, point size) how many pixels wide and how many pixels tall is the bounding rectangle?

  13. Which classes are used in this chapter for drawing graphical shapes?
    I'd just look through it page by page and jot down everything that's used for drawing a graphical shape. Just the names of the classes used and the pages where you found them being used or mentioned to be used would be enough, and your answer will probably be shorter than this hint, I think.

  14. What are the three different classes for specifying rectangles in the Java library?
    Why would there be more than one class for a Rectangle? Have we seen that with anything else? I believe we came close when we read (pages 150, I think) that for an ellipse there are two classes that could be used depending on how you want to store the coordinates, could that be part of the answer? Let me know.

  15. You want to plot a bar chart showing the grade distribution of all students in your class (where A=4.0, F=0). What coordinate system would you choose to make plotting as simple as possible?
    This is like printing average temperatures in Phoenix, AZ. Instead of having 12 months we have 12 possible letter grades. And instead of having average temperatures for each month we would have to represent number of students with that letter grade in our class. Of course, the temperature plotting exercise is carried out in section 4.10, that starts on page 170. Table 4 and Figure 18 are two of my favorites in that section.

  16. Let e be any ellipse. Write Java code to plot the ellipse e and another ellipse of the same size that touches e. Hint: You need to look up the accessors that tell you the dimensions of an ellipse.
    You know that an accessor is a(n instance) method that allows us to look into the internal state of the object that has that method. What is the internal state of an ellipse? I suppose it's made up of the location and dimension of the ellipse. So you can look into the location and dimension for the bounding rectangles, and the easiest way to make them tangent is to have them touch where their bounding rectangles touch, or so I think. Check the API on the web or in the book (page 703 may have some info about the accessors of ellipse objects, although I am not completely sure about that).

  17. Write Java instructions to display the letters X and T in a graphics window, by plotting line segments.
    Write Java code to draw a picture of two lines that cross each other, and another picture of two lines that form an angle of 90 degrees, as they normally do in a T.

  18. Introduce an error in the program Intersect.java, by computing
    double root = Math.sqrt(r * r + (x - a) * (x - a));
    Run the program. What happens to the intersection points?
    This program appears in the book on page 167, in section 4.9 and our lecture notes of Monday July 3 also contain it, it's the last applet presented in the notes, although it's not called Intersect (I think it's called Two in the notes but I'm not entirely sure of that).

  19. Suppose you run the Intersect program and give a value of 30 for the x-position of the vertical line. Without actually running the program, determine what values you will obtain for the intersection points.
    This is the same program as above, so you can experiment with it at will. Enjoy!

And now the problems in the problem set.

1. Write a graphics program that draws your name in red, centered inside a blue rectangle.

That's extremely easy, in two steps.
2. Write a graphics program that draws your name four times, in a large serif font, in plain, bold, italic, and bold italic. The names should be stacked on top of each other, with equal distance between them. Each of them should be centered horizontally, and the entire stack should be centered vertically.
This is also easy but make sure you use getWidth() and getHeight() from the applet to center the strings dynamically (regardless of the size of the applet). There is an example of that in the lecture notes of Wednesday. Also in the book in the program on page 158.
3. Write a graphics program that draws twelve strings, one each for the 12 standard colors beside Color.white, each in its own color.
This should be easy.
4. Write a graphics program that prompts the user to enter a radius. Draw a circle with that radius.
Use a modal dialog as in the last applet in the lecture notes of Monday.
5. Write a program that draws two solid circles, one in pink and one in purple. Use a standard color for one of them and a custom color for the other.
Easy. For the second color just put together a certain amount of red, green, and blue, as we did in class on Monday and Wednesday. The code is in the notes and it is in the book as well, on page 165 (or around that page).
6. Draw a "bull's eye" -- a set of concentric rings in alternating red and white colors. Hint: Fill a red circle, then fill a smaller white circle on top, and so on.

This is very easy, but I'd recommend developing the program in stages though. Write a bit, compile, run it, to test the output, then enhance it some more, until you're done.
7. Write a program that fills the applet window with a large ellipse, filled with your favourite color, that touches the window boundaries. The ellipse should resize itself when you resize the window.
So the bounding rectangle needs to resize itself. See how that is done in the program called Six (it's an applet) in the lecture notes of Wednesday. After that getting the ellipse out of the rectangle is straightforward.
8. Write a program that draws the picture of a house. It could be as simple as the figure below, or if you like, make it more elaborate (3-D, skyscraper, marble columns in the entryway, whatever).

I wouldn't make it more elaborate, I'd write a program that draws three rectangles and two lines and I'd practice some of the other problems.
9. Write a program to plot the following face.

Elmo. Adapt the smiling Picasso from the lecture notes of Monday (and last Thursday). Actually the program is in lecture notes of last Thursday and it's called Five. It was shown in class on Monday.
10. Write a program to plot the string "HELLO," using just lines and circles. Do not call drawString, and do not use System.out.
Easy, but don't cheat; 11 lines and one circle. That's all there is to it.
11. Plotting a data set. Make a bar chart to plot the following data set:

Bridge Name Longest Span (ft)
Golden Gate 4,200
Brooklyn 1,595
Delaware Memorial 2,150
Mackinac 3,800

Make the bars horizontal for easier labeling. Hint: Set the window coordinates to 5,000 in the x-direction and 4 in the y-direction.

See figure 18 in the book on page 173 in chapter 4. Changing the coordinates with a unit converter might also be useful, although to keep things simple you can work in screen coordinates from the start. Make your program as general as you want, but I'd say better have it do what is required quickly, and correctly, before running out of time with an over ambitious program design. So you need to draw rectangles proportional to the lengths, and to put the labels in the rectangles, for the bridges that they stand for.
12. Write a graphics program that displays the values of exercise 4.11 as a pie chart.
Here's a pie chart:

You want to make sure that you represent the relative dimensions of the four bridges, that together will form a whole (a circle). So you divide the lenght of each bridge by the sum of all the lengths thus dividing the pie into slices of proportional sizes.

13. Write a program that displays the Olympic rings.

Make the rings colored in the Olympic colors.

Easy problem, just make sure you get the circles in the right colors.
14. Write a graphics program that draws a clock face with a time that the user enters in a text fiels. (The user must enter the time in the format hh:mm, for example 09:45).

Hint: You need to find out 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 x 60 minutes.

You need to use the sinus and cosinus functions for this problem which is otherwise fairly easy (and related to the problem about military time in problem set number 2). If you need help with this problem please let me know and I can help right away. Do not forget I have office hours every night 6-7pm in Lindley 016 in addition to the hours in the morning (10-11) and afternoon (3-4) in LH301G.
15. Change the CarDrawer program to make the car appear twice the size of the original example.
Laborious but easy, unless you use a unit converter, in which case the whole difficulty shifts to using or designing the unit converter.
16. Design a class Car whose constructor takes the top left corner point of the car. Supply a method
draw(Graphics2D g2)
that draws the car. Then populate your screen with a few cars.
Your paint method would look like this:
public void pain(Graphics g) {
  Graphics2D g2 = (Graphics2D)g; 
  Car c1 = new Car(30, 40); 
  c1.draw(g2); 
  Car c2 = new Car(30, 40); 
  c2.draw(g2); 
  Car c3 = new Car(30, 40); 
  c3.draw(g2); 
} 
and the draw method in class Car will look like a paint method in an applet (in that it receives a graphical context as a parameter, which it uses, to draw the lines and whatnot that compose its drawing).
17. Extend Exercise 4.8 by implementing a class House. In the constructor of the House class, supply the bottom left corner of the house and its width and height. Supply a method
draw(Graphics2D g2)
that draws the house. Then populate your screen with a few houses of different sizes.
This is just like the problem above, but the actual drawing will be different (resembling a house more than a car).


Last updated: July 6, 2000 by Adrian German for A201