| CSCI A201/A597Lab Notes Six Second Summer 2000 |
Here are some hints, on the problems and quiz questions:
The main point here is where the main method is.
Limited scope of responsibilities, but what does that mean?
Can you do it otherwise?
paint method of an applet? When
does the call to the paint method occur?
We've talked about this in class on Wednesday.
paint method have
type Graphics and not Graphics2D?
The answer is in the book on page 148 (I think).
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.
See page 155 for a start. The answer should be short though.
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.
Page 156 has the answer (I think).
Page 154 (but I am not sure).
It's the type of font in which Lewis Carroll's books were printed when he was signing Charles Dodgson. See page 154.
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?
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.
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.
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.
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).
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.
Intersect.java, by computing Run the program. What happens to the intersection points?double root = Math.sqrt(r * r + (x - a) * (x - a));
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 calledIntersect(I think it's calledTwoin the notes but I'm not entirely sure of that).
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 usegetWidth()andgetHeight()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:
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
that draws the car. Then populate your screen with a few cars.draw(Graphics2D g2) Your |
| 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
that draws the house. Then populate your screen with a few houses of different sizes.draw(Graphics2D g2) |
This is just like the problem above, but the actual drawing will be different (resembling a house more than a car).