Enhance the Cafeteria Survey application from Tutorial 16 so that it draws a bar for each rating instead of asterisks. The user should still be able to choose ratings from a JComboBox and press a JButton to enter the selected rating. As ratings are input by the user, a horizontal bar graph appears in the JPanel as seen in Fig. 20.35. When 20 ratings have been entered, disable the Submit Rating JBut- ton.
a) Copying the template to your working directory. Copy the C:Examples Tutorial20ExercisesEnhancedCafeteriaSurvey directory to your C:Simply- Java directory.
b) Opening the DrawJPanel template file. Open the template file DrawJPanel.java in your text editor.
c) Initializing variables. Inside the drawGraph method, before the repaint method call, declare two new int variables x and y, and initialize them to the value 5. Then, declare a new int variable width, but do not initialize it. The value of width will depend on the number of responses for each number. Declare a fourth int variable, height, which will be initialized to 20.
d) Creating a new MyRectangle object. After the declaration of the four int variables that you added in Step c, create a new MyRectangle object and assign it to variable bar.
e) Looping through the responses. After the declaration you added in Step d, begin a for statement that loops from 1 to 10, incrementing by one each time through the
```
1 // CafeteriaSurvey.java
2 // Application that asks 20 users to enter a rating of the quality of
3 // food in the student cafeteria and displays a bar graph of the
4 // data. User can add ratings between the values of 1 and 10, with
5 // 1 being "awful" and 10 being "excellent."
6 import java.awt.*;
7 import javax.swing.*;
8 import java.awt.event.*;
9
10 public class CafeteriaSurvey extends JFrame
11 {
12 // JLabels for labeling x-axis bar graph
13 private JLabel oneJLabel;
14 private JLabel ratingsJLabel;
15 private JLabel tenJLabel;
16
17 // JLabel and JComboBox for choosing a rating
18 private JLabel ratingJLabel;
19 private JComboBox ratingJComboBox;
20
21 // JButton to submit a rating
22 private JButton submitRatingJButton;
23
24 // JLabel and JTextArea for displaying all ratings
25 private JLabel resultJLabel;
26 private DrawJPanel drawingJPanel;
27
28 // JLabels for labeling y-axis of bar graph
29 private JLabel zeroJLabel;
30 private JLabel resultsJLabel;
31 private JLabel twentyJLabel;
32
33 // String array to hold the user's choices
34 private String[] choices = { "1", "2", "3", "4", "5", "6",
35 "7", "8", "9", "10" };
36
37 // integer array to track user responses
38 private int[] responses = new int[ 11 ];
39
40 private int responseCounter = 0; // tracks number of responses
41
42 // no-argument constructor
43 public CafeteriaSurvey()
44 {
45 createUserInterface();
46 }
47
48 // create and position GUI components; register event handlers
49 private void createUserInterface()
50 {
51 // get content pane for attaching GUI components
52 Container contentPane = getContentPane();
53
54 // enable explicit positioning of GUI components
55 contentPane.setLayout( null );
56
57 // set up oneJLabel
58 oneJLabel = new JLabel();
59 oneJLabel.setBounds( 50, 135, 25, 25 );
60 oneJLabel.setText( "1" );
61 contentPane.add( oneJLabel );
62
63 // set up ratingsJLabel
64 ratingsJLabel = new JLabel();
65 ratingsJLabel.setBounds( 10, 245, 50, 25 );
66 ratingsJLabel.setText( "Ratings" );
67 contentPane.add( ratingsJLabel );
68
69 // set up tenJLabel
70 tenJLabel = new JLabel();
71 tenJLabel.setBounds( 50, 370, 25, 25 );
72 tenJLabel.setText( "10" );
73 contentPane.add( tenJLabel );
74
75 // set up ratingJLabel
76 ratingJLabel = new JLabel();
77 ratingJLabel.setBounds( 75, 20, 140, 20 );
78 ratingJLabel.setText( "Rate cafeteria food:" );
79 contentPane.add( ratingJLabel );
80
81 // set up ratingJComboBox
82 ratingJComboBox = new JComboBox( choices );
83 ratingJComboBox.setBounds( 285, 20, 80, 25 );
84 contentPane.add( ratingJComboBox );
85
86 // set up submitJButton
87 submitRatingJButton = new JButton();
88 submitRatingJButton.setBounds( 135, 60, 130, 25 );
89 submitRatingJButton.setText( "Submit Rating" );
90 contentPane.add( submitRatingJButton );
91 submitRatingJButton.addActionListener(
92
93 new ActionListener() // anonymous inner class
94 {
95 // event handler called when
96 // submitRatingJButton is pressed
97 public void actionPerformed( ActionEvent event )
98 {
99 submitRatingJButtonActionPerformed( event );
100 }
101
102 } // end anonymous inner class
103
104 ); // end call to addActionListener
105
106 // set up resultJLabel
107 resultJLabel = new JLabel();
108 resultJLabel.setBounds( 75, 100, 100, 20 );
109 resultJLabel.setText( "Survey results:" );
110 contentPane.add( resultJLabel );
111
112 // set up drawingJPanel
113 drawingJPanel = new DrawJPanel();
114 drawingJPanel.setBounds( 75, 135, 310, 255 );
115 drawingJPanel.setBackground( Color.WHITE );
116 contentPane.add( drawingJPanel );
117
118 // set up zeroJLabel
119 zeroJLabel = new JLabel();
120 zeroJLabel.setBounds( 75, 390, 25, 25 );
121 zeroJLabel.setText( "0" );
122 contentPane.add( zeroJLabel );
123
124 // set up resultsJLabel
125 resultsJLabel = new JLabel();
126 resultsJLabel.setBounds( 200, 405, 50, 25 );
127 resultsJLabel.setText( "Results" );
128 contentPane.add( resultsJLabel );
129
130 // set up twentyJLabel
131 twentyJLabel = new JLabel();
132 twentyJLabel.setBounds( 370, 390, 25, 25 );
133 twentyJLabel.setText( "20" );
134 contentPane.add( twentyJLabel );
135
136 // set properties of application's window
137 setTitle( "Cafeteria Survey" ); // set title bar text
138 setSize( 450, 475 ); // set window size
139 setVisible( true ); // display window
140
141 } // end method createUserInterface
142
143 // submit response and display results
144 private void submitRatingJButtonActionPerformed(
145 ActionEvent event )
146 {
147 responseCounter++; // increment counter
148
149 // variable to hold user input
150 int input = ratingJComboBox.getSelectedIndex() + 1;
151
152 responses[ input ]++; // add user input to responses array
153
154 // draw the bar graph
155 drawingJPanel.drawGraph( responses );
156
157 // if 20 ratings have been entered
158 if ( responseCounter == 20 )
159 {
160 // disable the submitRatingsJButton
161 submitRatingJButton.setEnabled( false );
162 }
163
164 } // end submitJButtonActionPerformed
165
166 // main method
167 public static void main( String args[] )
168 {
169 CafeteriaSurvey application = new CafeteriaSurvey();
170 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
171
172 } // end method main
173
174 } // end class CafeteriaSurvey
```
```
1 // DrawJPanel.java
2 // This class defines the DrawJPanel object.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.Random;
8 import javax.swing.*;
9
10 public class DrawJPanel extends JPanel
11 {
12 // ArrayList object to hold MyRectangle objects
13 private ArrayList barArray = new ArrayList();
14
15 // no-argument constructor
16 public DrawJPanel()
17 {
18 super();
19
20 } // end constructor
21
22 // draw all bar rectangles
23 public void paintComponent( Graphics g )
24 {
25 super.paintComponent( g );
26
27 // create iterator
28 Iterator traverse = barArray.iterator();
29
30 // iterate through ArrayList and draw all MyRectangles
31 while ( traverse.hasNext() )
32 {
33 MyRectangle barRectangle =
34 ( MyRectangle ) traverse.next();
35
36 barRectangle.drawMyRectangle( g ); // draw bar
37
38 } // end while loop
39
40 } // end method paintComponent
41
42 // draw the bar graph
43 public void drawGraph( int[] answers )
44 {
45 // set the x, y, width and height variables
46 int x = 5; // set x position
47 int y = 5; // set y position
48 int width; // set width
49 int height = 20; // set height
50
51 // MyRectangle object to represent the bars
52 MyRectangle bar;
53
54 // loop through each possible answer
55 for ( int counter = 1; counter <= 10; counter++ )
56 {
57 // set the width
58 width = 15 * answers[ counter ];
59
60 // draw bar objects representing user entered data
61 bar = new MyRectangle( x, y, width, height, Color.BLACK );
62 barArray.add( bar );
63
64 // update y position
65 y += 25;
66
67 } // end for loop
50
51 // MyRectangle object to represent the bars
52 MyRectangle bar;
53
54 // loop through each possible answer
55 for ( int counter = 1; counter <= 10; counter++ )
56 {
57 // set the width
58 width = 15 * answers[ counter ];
59
60 // draw bar objects representing user entered data
61 bar = new MyRectangle( x, y, width, height, Color.BLACK );
62 barArray.add( bar );
63
64 // update y position
65 y += 25;
66
67 } // end for loop
68
69 repaint(); // repaint JPanel
70
71 } // end method drawGraph
72
73 } // end class DrawJPanel
```
You might also like to view...
You can add transitions in Normal view or in Slide Sorter view, but it is easier to see the animations demonstrated on multiple slides in ________ view
Fill in the blank(s) with correct word
ASP is very similar to ____ because both languages create dynamic webpages.
A. DHTML B. PHP C. XML D. C#