//--------------------HouseAnimationFrame---------------

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

/**

   Display houses by pressing mouse with random color & width

   while blinking 3 colored windows.

   @author Kathleen O'Brien updated by Ahmad Mobasseri

*/

public class HouseAnimationFrame extends JFrame

{

   private HousePanel panel;              // Customized panel which extends JPanel

   private static final long serialVersionUID = 1;

 

   public HouseAnimationFrame()                 // Constructor

   {

     setTitle("The Houses with blinking lights");

     setSize(600,400);

 

     panel = new HousePanel();                  // Call HousePanel const. to start building a house

     add(panel,BorderLayout.CENTER);

   }

 

//------------inner class HousePanel-------------------

   class HousePanel extends JPanel implements MouseListener

   {

      private final int WIDTH_MAX = 70;  

      private final int WIDTH_MIN = 20;

      private static final long serialVersionUID = 1;

 

      private HousePanel()                                  // Constructor

      {

          setBackground(Color.WHITE);

          addMouseListener(this);                     // Mouse Event         

      }

     

/***      public void paintComponent(Graphics g)// NOTE, no need as no drawing here.

      {

         super.paintComponent(g);

      }  ***/

     

      public void mousePressed (MouseEvent e)   // Mouse Event handler

      {

        int x = e.getX();

        int y = e.getY();

        Random random = new Random();                 // Random color and width for each house

        Color randColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));

        int randWidth = (int) (Math.floor(Math.random() * (WIDTH_MAX - WIDTH_MIN +1)) + WIDTH_MIN);

        new AnimatedHouse(panel, x, y, randColor, randWidth).start();   // Build a house for each mouse pressed

      }

      public void mouseClicked  (MouseEvent e) {};          // These Events of mouse are not used

      public void mouseReleased (MouseEvent e) {};

      public void mouseEntered  (MouseEvent e) {};

      public void mouseExited   (MouseEvent e) {};

   }//end HousePanel ---------------------------------

 

   // Start the program by creating instance of this class (extends JFrame)

   public static void main(String[] args)

   {

      JFrame myFrame = new HouseAnimationFrame();

      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      myFrame.setVisible(true);

   }

}//end HouseAnimationFrame ---------------------------------