H:\CSC120\Java\lec11\src\MUPanel.java
  1 /* CSC 120 Lecture Example # 11
  2  *
  3  *
  4  *
  5  * Project Description: a program that uses BaseballHitter objects and calls
  6                         methods from that class
  7  *
  8  */
  9 import java.awt.*;
 10 import java.text.DecimalFormat;
 11 import javax.swing.*;
 12 
 13 public class MUPanel extends JPanel {
 14 
 15     // 1. Declare private objects here:
 16     
 17     private BaseballHitter one, two, three, four;
 18     
 19     
 20     // constructor method
 21     public MUPanel() {
 22         setLayout(null);
 23         setPreferredSize(new Dimension(600, 200));
 24         setName("CSC 120 Lecture # 11");
 25         setUp();
 26         setBackground(Color.LIGHT_GRAY);
 27 
 28         // 2. Instantiate objects here by calling "new":
 29         
 30         one = new BaseballHitter("Jadier Molina", 600, 170, 85, false);
 31         two = new BaseballHitter("Jose Ramirez", 528, 148, 99, true);
 32         three = new BaseballHitter("JD Martinez", 530, 175, 122, false);
 33         four = new BaseballHitter("Mike Trout", 431, 137, 68, false);
 34 
 35         
 36 
 37 
 38         
 39         System.out.println( "Here are the baseball hitters we're tracking here in CSC 120:" );
 40         System.out.println( "=============================================================" );
 41         System.out.println("Player one is: " + one.toString());
 42         System.out.println();
 43         System.out.println("Player two is: " + two.toString());
 44         System.out.println();
 45         System.out.println("Player three is: " + three.toString());
 46         System.out.println();
 47         System.out.println("Player four is: " + four.toString());
 48         System.out.println();
 49 
 50 
 51         Integer oldAtBats;
 52         oldAtBats = two.getAtBats();
 53         Integer updatedAtBats = oldAtBats + 1;
 54         two.setAtBats( updatedAtBats );
 55   
 56         Integer tempHits = two.getHits();
 57         two.setHits( tempHits + 1 );
 58         
 59         two.setRBIs( two.getRBIs() + 1 );
 60         
 61         System.out.println();
 62         System.out.println("After the Update, Player two is: " + two.toString());
 63 
 64 
 65         System.out.println();
 66         System.out.println(four.getName() + " currently has " + four.getRBIs() + " rbis");
 67         four.setRBIs( four.getRBIs() + 4 );
 68         System.out.println("     If " + four.getName() + " hits a grand salami tonight, he will have "
 69                 + four.getRBIs() + " rbis");
 70         
 71         DecimalFormat threeDecimalDigits = new DecimalFormat("0.000");
 72         System.out.println();
 73         System.out.println(two.getName() + "'s batting average is: " + two.calculateBattingAvg());
 74         System.out.println(two.getName() + "'s batting average is: " 
 75                 + threeDecimalDigits.format(two.calculateBattingAvg()));
 76         
 77     } // end of constructor
 78     
 79     @Override
 80     public void paintComponent(Graphics g) {
 81         super.paintComponent(g); // This line must be first in this method!
 82 
 83         // 3. Call methods of objects here:
 84 
 85         g.setColor(Color.GREEN.darker());
 86         g.setFont( new Font("Serif", Font.BOLD, 24));
 87         g.drawString("Answers in Java Console", 100, 100);
 88                 
 89                 
 90     } // end of paintComponent()
 91     
 92     
 93     
 94        
 95     
 96     
 97     
 98     /***********************************************
 99      * Do NOT change or delete anything below here!
100      ***********************************************/
101     public void setUp() {
102         for (Component c: getComponents())
103             c.setSize(c.getPreferredSize());
104         JFrame f = new JFrame(getName());
105         f.setContentPane(this);
106         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
107         f.pack();
108         f.setVisible(true);    
109     }
110 
111     public static void main(String args[]){new MUPanel();}
112 
113 } // end of class MUPanel
114