[ Pobierz całość w formacie PDF ]
Area is also a Component.
10. g.drawLine(30, 40, 100, 60);
11. g.drawLine(30, 40, 130, 40);
12. graphicsObject.drawLine(30, 40, 30, 140);
13. graphicsObject.fillRect(20, 30, 100, 50);
14. g.fillRect(100, 300, 100, 50);
15. g.drawOval(250, 350, 100, 100);
16. g.drawOval(200, 300, 200, 200);
17. Insert g.setColor(Color.GREEN) as indicated below:
//Draw Eyes:
g.setColor(Color.BLUE);
g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
g.setColor(Color.GREEN);
g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
18. Replace the following line in the paint method:
g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
with
Color brown =
new Color(200, 150, 0);
g.setColor(brown);
g.fillOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
5640_ch18.fm Page 956 Friday, February 13, 2004 4:52 PM
956 Chapter 18 Swing II
Note that there is no predefined color constant Color.BROWN, so you need to define a color
for brown. You may prefer some other arguments instead of (200, 150, 0) so that you
get a shade of brown that is more to your liking.
19. g.setFont(new Font("SansSerif", Font.BOLD, 14));
20. g.setFont(new Font("SansSerif",
Font.BOLD|Font.ITALIC, 14));
PROGRAMMING PROJECTS
1. Write a skeleton GUI program that implements the WindowListener interface. Write
code for each of the methods in Display 18.1 that simply prints out a message identifying
which event occurred. Print the message out in a text field. Note that your program will not
end when the close-window button is clicked (but will instead simply send a message to the
text field saying that the windowClosing method has been invoked). Include a button
labeled Exit that the user can click to end the program.
2. Enhance the face drawing in Display 18.17 in all of the following ways: Add color so the
eyes are blue and the mouth is red. When the face winks, the line that represents a closed
eye is black not blue. Add a nose and a brown handlebar mustache. Add buttons labeled
"Smile" and "Frown". When the "Frown" button is clicked, the face shows a frown
(upside down smile); when the "Smile" button is clicked, the face shows a smile. When
the user clicks the close-window button, a window pops up to ask if the user is sure he or
she wants to exit, as in Display 18.2.
3. Write a GUI program to sample different fonts. The user enters a line of text in a text field.
The user then selects a font from a font menu. Offer the three guaranteed fonts and at least
two other fonts. The user also selects any desired style modifiers (bold and/or italic) from a
style menu, and selects point size from a point size menu that offers the following choices:
9, 10, 12, 14, 16, 24, and 32. There is also a "Display" button. When the "Display"
button is clicked, the text is displayed in the font, style, and point size chosen. [ Pobierz całość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl karpacz24.htw.pl
Area is also a Component.
10. g.drawLine(30, 40, 100, 60);
11. g.drawLine(30, 40, 130, 40);
12. graphicsObject.drawLine(30, 40, 30, 140);
13. graphicsObject.fillRect(20, 30, 100, 50);
14. g.fillRect(100, 300, 100, 50);
15. g.drawOval(250, 350, 100, 100);
16. g.drawOval(200, 300, 200, 200);
17. Insert g.setColor(Color.GREEN) as indicated below:
//Draw Eyes:
g.setColor(Color.BLUE);
g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
g.setColor(Color.GREEN);
g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
18. Replace the following line in the paint method:
g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
with
Color brown =
new Color(200, 150, 0);
g.setColor(brown);
g.fillOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
5640_ch18.fm Page 956 Friday, February 13, 2004 4:52 PM
956 Chapter 18 Swing II
Note that there is no predefined color constant Color.BROWN, so you need to define a color
for brown. You may prefer some other arguments instead of (200, 150, 0) so that you
get a shade of brown that is more to your liking.
19. g.setFont(new Font("SansSerif", Font.BOLD, 14));
20. g.setFont(new Font("SansSerif",
Font.BOLD|Font.ITALIC, 14));
PROGRAMMING PROJECTS
1. Write a skeleton GUI program that implements the WindowListener interface. Write
code for each of the methods in Display 18.1 that simply prints out a message identifying
which event occurred. Print the message out in a text field. Note that your program will not
end when the close-window button is clicked (but will instead simply send a message to the
text field saying that the windowClosing method has been invoked). Include a button
labeled Exit that the user can click to end the program.
2. Enhance the face drawing in Display 18.17 in all of the following ways: Add color so the
eyes are blue and the mouth is red. When the face winks, the line that represents a closed
eye is black not blue. Add a nose and a brown handlebar mustache. Add buttons labeled
"Smile" and "Frown". When the "Frown" button is clicked, the face shows a frown
(upside down smile); when the "Smile" button is clicked, the face shows a smile. When
the user clicks the close-window button, a window pops up to ask if the user is sure he or
she wants to exit, as in Display 18.2.
3. Write a GUI program to sample different fonts. The user enters a line of text in a text field.
The user then selects a font from a font menu. Offer the three guaranteed fonts and at least
two other fonts. The user also selects any desired style modifiers (bold and/or italic) from a
style menu, and selects point size from a point size menu that offers the following choices:
9, 10, 12, 14, 16, 24, and 32. There is also a "Display" button. When the "Display"
button is clicked, the text is displayed in the font, style, and point size chosen. [ Pobierz całość w formacie PDF ]