Multiple Polygon Example

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class PolyExample
{
   Polygon[][] tri = new Polygon[5][5];
   Drawing draw = new Drawing();
   int row = -1, col = -1;
   
   public PolyExample()
   {
      int[] x = {0, 100, 50};
      int[] y = {0, 0, 100};
      for (int i = 0; i < 5; i++)
      {
         for (int j = 0; j < 5 ; j++)
         {
            tri[i][j] = new Polygon(x, y, 3);
            for (int k = 0; k < 3; k++)
            {
               x[k] += 100;
            }
            
         }
         x[0] = 0;
         x[1] = 100;
         x[2] = 50;
         for (int k = 0; k < 3; k++)
         {
            y[k] += 100;
         }
      }
      JFrame frame = new JFrame("Polygon Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(draw);
      draw.addMouseListener(new MouseListen());
      frame.setSize(550,550);
      frame.setVisible(true);
   }
   
   class MouseListen extends MouseAdapter
   {
      public void mouseReleased(MouseEvent e)
      {
         int x = e.getX();
         int y = e.getY();
         row = col = -1;
         for (int i = 0; i < 5; i++)
            for (int j = 0; j < 5; j++)
               if (tri[i][j].contains(x,y))
               {
                  row = i;
                  col = j;
               }
         draw.repaint();
      }
   }
   
   class Drawing extends JComponent
   {
      public void paint(Graphics g)
      {
         g.setColor(Color.blue);
         for (int i = 0; i < 5; i++)
            for (int j= 0; j < 5; j++)
               g.fillPolygon(tri[i][j]);
         if (row != -1)
         {
            g.setColor(Color.red);
            g.fillPolygon(tri[row][col]);
         }
      }
   }
   
   public static void main(String [] args)
   {
      new PolyExample();
   }
}