H:\CSC120\Java\lec21\src\MUFrame.java
  1 /* CSC 120 Lecture # 21
  2  * Blase B. Cindric
  3  *
  4  *
  5  * Project Description:  Illustration of use of Random class methods with 
  6  *                       JButtons
  7  * 
  8  */
  9 
 10 import java.awt.*;
 11 import java.util.Random;
 12 import javax.swing.*;
 13 
 14 public class MUFrame extends javax.swing.JFrame {
 15 
 16     
 17     // declare private data here
 18     
 19     private Random generator;
 20     private Integer year = 2021;
 21     private Integer numHeads, numTails;
 22     
 23     
 24     
 25     
 26     
 27     
 28     // constructor method
 29     public MUFrame() {
 30         initComponents();
 31         
 32         generator = new Random();
 33         numHeads = 0;
 34         numTails = 0;
 35         
 36     } // end of constructor
 37     
 38     // declare other methods here
 39     
 40     
 41     
 42     
 43 
 44     /** This method is called from within the constructor to
 45      * initialize the form.
 46      * WARNING: Do NOT modify this code. The content of this method is
 47      * always regenerated by the Form Editor.
 48      */
 49     @SuppressWarnings("unchecked")
 50     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
 51     private void initComponents() {
 52 
 53         drawingPanel = new javax.swing.JPanel();
 54         jScrollPane1 = new javax.swing.JScrollPane();
 55         outputArea = new javax.swing.JTextArea();
 56         randIntButton = new javax.swing.JButton();
 57         randPctButton = new javax.swing.JButton();
 58         coinFlipButton = new javax.swing.JButton();
 59         totalsLabel = new javax.swing.JLabel();
 60 
 61         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 62 
 63         outputArea.setColumns(20);
 64         outputArea.setRows(5);
 65         jScrollPane1.setViewportView(outputArea);
 66 
 67         randIntButton.setText("Generate Random Integer");
 68         randIntButton.addActionListener(new java.awt.event.ActionListener() {
 69             public void actionPerformed(java.awt.event.ActionEvent evt) {
 70                 randIntButtonActionPerformed(evt);
 71             }
 72         });
 73 
 74         randPctButton.setText("Generate Random Hurricane Pct");
 75         randPctButton.addActionListener(new java.awt.event.ActionListener() {
 76             public void actionPerformed(java.awt.event.ActionEvent evt) {
 77                 randPctButtonActionPerformed(evt);
 78             }
 79         });
 80 
 81         coinFlipButton.setText("Flip a Coin");
 82         coinFlipButton.addActionListener(new java.awt.event.ActionListener() {
 83             public void actionPerformed(java.awt.event.ActionEvent evt) {
 84                 coinFlipButtonActionPerformed(evt);
 85             }
 86         });
 87 
 88         totalsLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
 89         totalsLabel.setForeground(new java.awt.Color(102, 0, 204));
 90         totalsLabel.setText("No. of Heads = 0 and No. of Tails = 0");
 91 
 92         javax.swing.GroupLayout drawingPanelLayout = new javax.swing.GroupLayout(drawingPanel);
 93         drawingPanel.setLayout(drawingPanelLayout);
 94         drawingPanelLayout.setHorizontalGroup(
 95             drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 96             .addGroup(drawingPanelLayout.createSequentialGroup()
 97                 .addGroup(drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 98                     .addGroup(drawingPanelLayout.createSequentialGroup()
 99                         .addContainerGap()
100                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 608, javax.swing.GroupLayout.PREFERRED_SIZE))
101                     .addGroup(drawingPanelLayout.createSequentialGroup()
102                         .addGap(20, 20, 20)
103                         .addComponent(randIntButton)
104                         .addGap(55, 55, 55)
105                         .addComponent(randPctButton)
106                         .addGap(78, 78, 78)
107                         .addComponent(coinFlipButton))
108                     .addGroup(drawingPanelLayout.createSequentialGroup()
109                         .addGap(151, 151, 151)
110                         .addComponent(totalsLabel)))
111                 .addContainerGap(101, Short.MAX_VALUE))
112         );
113         drawingPanelLayout.setVerticalGroup(
114             drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
115             .addGroup(drawingPanelLayout.createSequentialGroup()
116                 .addGap(49, 49, 49)
117                 .addGroup(drawingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
118                     .addComponent(randIntButton)
119                     .addComponent(randPctButton)
120                     .addComponent(coinFlipButton))
121                 .addGap(46, 46, 46)
122                 .addComponent(totalsLabel)
123                 .addGap(35, 35, 35)
124                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
125                 .addContainerGap(83, Short.MAX_VALUE))
126         );
127 
128         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
129         getContentPane().setLayout(layout);
130         layout.setHorizontalGroup(
131             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
132             .addComponent(drawingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
133         );
134         layout.setVerticalGroup(
135             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
136             .addGroup(layout.createSequentialGroup()
137                 .addContainerGap()
138                 .addComponent(drawingPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
139                 .addContainerGap(68, Short.MAX_VALUE))
140         );
141 
142         pack();
143     }// </editor-fold>                        
144 
145     private void randIntButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
146         Integer n;
147         
148         n = generator.nextInt(100);
149         outputArea.append("the next random integer is " + n + "\n");
150     }                                             
151 
152     private void randPctButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
153         Double pct;
154         
155         pct = generator.nextDouble();
156         
157         if (pct <= 0.085) {
158             outputArea.append("      A CAT-2 Hurricane or worse will hit in " + year + "!!!!");
159         }
160         else {
161             outputArea.append("      no big hurricane will hit in " + year);
162         }
163         outputArea.append("  (pct = " + pct + ")\n");
164         
165         year++;
166     }                                             
167 
168     private void coinFlipButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
169         Boolean isHeads;
170         
171         isHeads = generator.nextBoolean();
172         
173         if (isHeads == true) {
174             outputArea.append("========> the coin was heads!\n");
175             numHeads++;
176         }
177         else {
178             outputArea.append("========> the coin was TAILS!\n");
179             numTails++;
180         }
181         
182         totalsLabel.setText("No. of Heads = " + numHeads + " and No. of Tails = " + numTails);
183     }                                              
184 
185     /**
186      * @param args the command line arguments
187      */
188     public static void main(String args[]) {
189         java.awt.EventQueue.invokeLater(new Runnable() {
190 
191             public void run() {
192                 new MUFrame().setVisible(true);
193             }
194         });
195     }
196     // Variables declaration - do not modify                     
197     private javax.swing.JButton coinFlipButton;
198     private javax.swing.JPanel drawingPanel;
199     private javax.swing.JScrollPane jScrollPane1;
200     private javax.swing.JTextArea outputArea;
201     private javax.swing.JButton randIntButton;
202     private javax.swing.JButton randPctButton;
203     private javax.swing.JLabel totalsLabel;
204     // End of variables declaration                   
205 }
206