Introduction to Java Programming-Comprehensive Version (6th Edition)

 

[Page 1041]

Programming Exercises

Section 30.2 MVC

30.1*** ( Creating MVC components ) Create a model, named ChartModel , which holds data in an array of double elements named data , and the names for the data in an array of strings named dataName . For example, the enrollment data stored in the array data are for in the array dataName . These two properties have their respective get methods, but not individual set methods . Both properties are set together in the setChartData(String[] newDataName, double[] newData) method so that they can be displayed properly. Create a view named PieChart to present the data in a pie chart, and create a view named BarChart to present the data in a bar chart, as shown in Figure 30.28.
Figure 30.28. The two views, PieChart and BarChart , receive data from the ChartModel .

Hint

Each pie represents a percentage of the total data. Color the pie using the colors from an array named colors, which is Color.red , Color.yellow , Color.green , Color.blue , Color.cyan , Color. magenta , Color.orange , Color.pink , Color.darkGray . Use colors[i % colors.length] for the i th pie. Use black color to display the data names.

30.2* ( Revising Listing 30.3 CircleController.java ) CircleController uses a text field to obtain a new radius and a combo box to obtain a Boolean value to specify whether the circle is filled. Add a new row in CircleController to let the user choose color using the JColorChooser component, as shown in Figure 30.29. The new row consists of a label with text Color, a label to display color, and an eclipse button. The user can click the eclipse button to display a JColorChooser dialog box. Once the user selects a color, the color is displayed as the background for the label on the left of the eclipse button.
Figure 30.29. Clicking the eclipse button displays the color chooser dialog box for specifying a color.


[Page 1042]

Sections 30.5 “30.6

30.3** ( Synchronizing spinners ) The date spinner is synchronized with the day, month, and year spinners in Listing 30.6, SpinnerModelEditorDemo.java. Improve it to synchronize the day, month, and year spinners with the date spinner. In other words, when a new value is selected in the date spinner, the values in the day, month, and year spinners are updated accordingly .
30.4* ( Custom spinner model ) Develop a custom spinner model that represents a sequence of numbers of power 2, that is, 1, 2, 4, 8, 16, 32, and so on. Your model should implement AbstractSpinnerModel . The registration/deregistration methods for ChangeListener have already been implemented in AbstractSpinnerModel . You need to implement getNextValue() , getPreviousValue() , getValue() , and setValue(Object) methods.
30.5* ( Reversing the numbers displayed in a spinner ) The numbers displayed in a spinner increase when the up-arrow button is clicked and decrease when the down-arrow button is clicked. You can reverse the sequence by creating a new model that extends SpinnerNumberModel and overrides the getNextValue and getPreviousValue methods.

Sections 30.7 “30.9

30.6* ( Removing selected items in a list ) Modify Listing 30.8, ListModelDemo.java, to meet the following requirements:
  • Remove all the selected items from the list when the Remove selected item button is clicked.

  • Enable the items to be deleted using the DELETE key.

30.7* ( Custom list cell renderer ) Listing 30.10, ListCellRendererDemo.java, has two types of images for each country. The small images are used for display in the list, and the large ones are used for display outside the list. Assume that only the large images are available. Rewrite the custom cell renderer to use a JPanel instead of a JLabel for rendering a cell. Each cell consists of an image and a string. Display the image in an ImageViewer and the string in a label. The ImageViewer component was introduced in Listing 13.15, ImageViewer.java. The image can be stretched in an ImageViewer . Set the size of an image viewer to as shown in Figure 30.30. Revise Listing 30.10 to test the new custom cell renderer.
Figure 30.30. ImageViewer is used to render the image in the list.

30.8* ( Deleting selected items in a list using the DELETE key ) Modify Listing 30.10, ListCellRendererDemo.java, to delete selected items from the list using the DELETE key. After some items are deleted from the list, the index of a selected item in the list does not match the index of the item in the bigIcons array. As a result, you cannot use the image icon in the bigIcons array to display the image to the right side of the split pane. Revise the program to retrieve the icon from the selected item in the list and display it, as shown in Figure 30.31.
[Page 1043]
Figure 30.31. Images in the list are also used for display in the label placed in the right side of a split pane.

30.9** ( Rendering figures ) Create a program that shows a list of geometrical shapes along with a label in an instance of JList , as shown in Figure 30.32. Display the selected figure in a panel when selecting a figure from the list. The figures can be drawn using the FigurePanel class in Listing 13.5, FigurePanel.java.
Figure 30.32. The list displays geometrical shapes and their names.

30.10** (List of clocks) Write a program that displays a list of cities and their local times in a clock, as shown in Figure 30.33. When a city is selected in the list, its clock is displayed in a large picture on the right.
Figure 30.33. The list displays cities and clocks.


[Page 1044]

Section 30.10 JComboBox

30.11** ( Creating custom cell renderer in a combo box ) Create a program that shows a list of geometrical shapes along with a label in a combo box, as shown in Figure 30.34. This exercise may share the list cell renderer with the preceding exercise.
Figure 30.34. The combo box contains a list of geometrical shapes and the shape names.

30.12** ( Rendering colored text ) Write a program that enables the user to choose the foreground colors for a label, as shown in Figure 30.35. The combo box contains thirteen standard colors ( BLACK , BLUE , CYAN , DARK_GRAY , GRAY , GREEN , LIGHT_GRAY , MAGENTA , ORANGE , PINK , RED , WHITE , YELLOW ). Each color name in the combo box uses its own color for its foreground.
Figure 30.35. The combo box contains a list of color names, each of which uses its own color for its foreground.

30.13* ( Deleting a selected item in a combo box using the DELETE key ) Modify Listing 30.11, ComboBoxCellRendererDemo.java, to delete the selected item from the combo box using the DELETE key.

 

Категории