H:\CSC120\Java\lec05\src\MUPanel.java
  1 /*
  2     CSC 120 Lecture 5
  3     
  4 
  5     Description:
  6     This program creates a picture of a house, tree, and happy-face sun.
  7     We have changed the code from Lab 2 so that the Tree and Sun are drawn
  8     as separate objects
  9     The purpose is to practice using methods of the Graphics class.
 10 */
 11 
 12 import java.awt.*;
 13 import javax.swing.*;
 14 
 15 public class MUPanel extends JPanel {
 16 
 17     // Declare private data here
 18     private LollipopTree aTree, tree2, lastTree, iLiedThisOneIsTheLastTree;
 19     private SmileySun ourSun, tatooineSun, settingSun;
 20     
 21     
 22     public MUPanel() {
 23         setLayout(null);
 24         setPreferredSize(new Dimension(900, 400));
 25         setName("CSC 120 Lab 2");
 26         setBackground(Color.WHITE); 
 27 
 28         // Instantiate objects and initialize data here
 29         aTree = new LollipopTree(480, 80);
 30         tree2 = new LollipopTree(750, 80);
 31         lastTree = new LollipopTree(600, 250);
 32         iLiedThisOneIsTheLastTree = new LollipopTree(650, 20);
 33         
 34         ourSun = new SmileySun(Color.RED, Color.YELLOW, 60, 60, 120);
 35         tatooineSun = new SmileySun(Color.BLUE, new Color(255, 153, 102), 370, 20, 50);
 36         settingSun = new SmileySun(Color.MAGENTA, Color.ORANGE, 75, 290, 90);
 37         
 38     } // end of MUPanel constructor
 39     
 40 
 41     public void paintComponent(Graphics g) {
 42         super.paintComponent(g);
 43         
 44         // The house
 45         g.setColor(Color.BLACK);
 46         g.drawRect(200, 150, 200, 100);
 47         
 48         // the roof
 49         g.drawLine(200, 150, 300, 90);
 50         g.drawLine(300, 90, 400, 150);
 51         
 52         // the window
 53         g.setColor(Color.YELLOW);
 54         g.fillRect(225, 180, 90, 40);
 55         g.setColor(Color.BLACK);
 56         g.drawRect(225, 180, 90, 40);
 57         g.drawLine(270, 180, 270, 220); 
 58         g.drawLine(225, 200, 315, 200); 
 59         
 60         // the door
 61         g.setColor(Color.BLUE);
 62         g.fillRect(350, 190, 30, 60);
 63         g.setColor(Color.ORANGE);
 64         g.fillOval(365, 220, 10, 10);
 65     
 66         // The tree
 67         aTree.draw(g);
 68         lastTree.draw(g);
 69         tree2.draw(g);
 70         iLiedThisOneIsTheLastTree.draw(g);
 71     
 72         // The sun
 73         ourSun.draw(g);
 74         tatooineSun.draw(g);
 75         settingSun.draw(g);
 76 
 77 
 78     } // end of paintComponent()
 79 
 80     
 81     
 82     /***********************************************
 83      * Do NOT change or delete anything below here!
 84      ***********************************************/
 85     public void frame() {
 86         for (Component c: getComponents())
 87             c.setSize(c.getPreferredSize());
 88         JFrame f = new JFrame(getName());
 89         f.setContentPane(this);
 90         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 91         f.pack();
 92         f.setVisible(true);   
 93         
 94     } // end of frame()
 95 
 96     public static void main(String args[]){new MUPanel().frame();}
 97 
 98 }
 99