Quiz 7 (Exercise for Chap. 12)

  1. Fill in the blanks of the following program which every INTERVAL milliseconds adds a new randomly placed rectangle (piece of cheese) to a list and draws the list of rectangles. Meanwhile a mouse is trying to eat the cheese, i.e., every time the mouse clicks on a rectangle it disappears from the list. When there are no more rectangles in the list, stop the timer. Note, you can use the Rectangle.contains(Point) method.
    /**
       This program draws a new rectangle (cheese bar) on the panel
    each second.  If a mouse clicks on a cheese bar then it disappears.
    The game is over when all bars are gone.
     */
    
    import javax.swing.JPanel ;
    import javax.swing.JFrame ;
    import java.awt.Graphics ;
    import java.awt.Dimension ;
    import java.awt.Graphics2D ;
    import java.awt.Point ;
    import java.awt.event.MouseEvent ;
    import java.awt.event.MouseListener ;
    import java.awt.event.MouseAdapter ;
    import java.awt.event.ActionListener ;
    import java.awt.event.ActionEvent ;
    import java.awt.Rectangle ;
    import javax.swing.Timer ;
    import java.util.ArrayList ;
    import java.util.Random ;
    
    
    public class CheeseTest
    {
        public static void main(String[] args)
        {
            JFrame frame = new JFrame() ;
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
            CheesePanel panel = new CheesePanel() ;
            frame.getContentPane().add(panel) ;
            frame.pack() ;
            frame.show() ;
        }
    }
    /**
       The cheese panel draws the cheese bars and sets up the timer and 
       the listener for the mouse.
     */
    
    class CheesePanel extends JPanel
    {
        //CONSTANTS
        private final static int INTERVAL = 1000 ;
        private final static int RECT_W = 40, RECT_H = 15 ;
        private final static int PANEL_W = 400, PANEL_H = 400 ;
        /**
           Instance variables
               -- a timer to keep time	 
               -- a list to remember where the rectangles are.
               -- a random number generator to choose spots for rectangles
         */
        private Timer timer ;
        private ArrayList list ;
        private Random random ;
    
        
        /**
           Creates the timer and starts it.  DMI of the mouse listener also.
         */
        public CheesePanel() 
        {
            setPreferredSize(new Dimension(PANEL_W, PANEL_H)) ;
            random = new Random() ;
            list = new ArrayList() ;
            ActionListener listener = createTimerListener() ;
            timer = new Timer(INTERVAL, listener) ;
            timer.start() ;
            MouseListener mouse= createMouseListener() ;
            addMouseListener(mouse) ;
        }
        /**
           Paints the cheese bars.
         */
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g) ;
            Graphics2D g2 = (Graphics2D) g ;
            //Write a loop here to paint all the rectangles in the list.        
            _______________________________________________________________
            _______________________________________________________________
            _______________________________________________________________
            _______________________________________________________________
            _______________________________________________________________
            _______________________________________________________________
        }
        
        /**
           Returns a listener that reacts to the timer by adding 
           a new randomly placed rectangle to the list and asking 
           the panel to repaint the list of rectangles. 
        */
        public ActionListener createTimerListener()
        {
            class TimerListener implements ActionListener {
                public void actionPerformed(ActionEvent event)
                {
                   _________________________________________________________
                   _________________________________________________________
                   _________________________________________________________
                   _________________________________________________________
                   _________________________________________________________
                   _________________________________________________________
                }
            }
            return new TimerListener() ;
        }
        /**
           Returns a mouse listener which reacts to the mouse by going
           through the list of rectangles to see if the point at which
           the mouse clicked is inside any rectangles.  Any such 
           rectangle is removed from the list.  If there are no more 
           rectangles in the list, then it tells the timer to stop.  It
           tells the panel to repaint itself if it removed a rectangle.
         */
        public MouseListener createMouseListener()
        {
            class CheeseListener extends MouseAdapter {
                public void mouseClicked(MouseEvent event)
                {
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                   __________________________________________________________
                }
            }
            MouseListener listener = new CheeseListener() ;
            return listener ;
        }
    
    }