import java.awt.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ShowBoxing
{
   private static int[] giftX;					// x-Koordinaten vom Geschenk
   private static int[] giftY;					// y-Koordinaten vom Geschenk
   private static int[] boxX = new int[4];	// x-Koordinaten der umschließenden Box
   private static int[] boxY = new int[4];	// y-Koordinaten vom umschließenden Box
	private static int anzahlEcken;

   private static void readGift(String fileName)
   {
      String s = null;
      int first = 0;
      int last = 0;
      int x = 0;
      int y = 0;

      try
      {
         //open input file
         LineNumberReader f = new LineNumberReader(new FileReader(fileName));
          
         //read the number of points
         s = f.readLine();
         anzahlEcken = Integer.parseInt(s);
          
         //allocate an array of suitable length
         giftX = new int[anzahlEcken];
         giftY = new int[anzahlEcken];

         //read the list of points
         for(int i = 0; i < anzahlEcken; i++)
         {
            s = f.readLine();
            first = 0;
            last = s.indexOf(',',first);
            x = Integer.parseInt(s.substring(first,last));
            first = last+1;
            last = s.length();
            y = Integer.parseInt(s.substring(first,last));     
            giftX[i] = x;    
            giftY[i] = y;
         }
         f.close();
      }
		catch (IOException exception) 
      {
         System.out.println("An error occurred when reading the input file");
      }       
    }

   private static void computeBox()
   {
      // Startwerte setzen
      int xmin = giftX[0];
      int xmax = giftX[0];
      int ymin = giftY[0];
      int ymax = giftY[0];
		
      // Suchen der Minima und Maxima für x- und y-Koordinate
      for(int i = 1; i < anzahlEcken; i++)
      {
         if(giftX[i] < xmin) {xmin = giftX[i];}
         if(giftX[i] > xmax) {xmax = giftX[i];}
         if(giftY[i] < ymin) {ymin = giftY[i];}
         if(giftY[i] > ymax) {ymax = giftY[i];}
      }

		// Eckdaten der Box, für 'drawPolygon'
      boxX[0] = xmin;
      boxX[1] = xmin;
      boxX[2] = xmax;
      boxX[3] = xmax;
       
      boxY[0] = ymin;
      boxY[1] = ymax;
      boxY[2] = ymax;
      boxY[3] = ymin;

      System.out.println("The size of the box is: " + (xmax - xmin)*(ymax - ymin));
   }


	// kleine Erweiterung, benötigt für Ausgabe
	public static class drawPolygon extends JPanel
	{
		@Override
		public void paintComponent(Graphics g) 
		{
			super.paintComponent(g);
            
			g.setColor(Color.BLUE);
			Polygon polyBox = new Polygon(boxX, boxY, boxX.length);
			g.drawPolygon(polyBox);
            
			g.setColor(Color.RED);
			Polygon polyGift = new Polygon(giftX, giftY, giftX.length);
			g.drawPolygon(polyGift);
		}
        
		@Override
		public Dimension getPreferredSize() 
		{
			Dimension s = new Dimension();
			s.width = 800;
			s.height = 600;
			return s;
		}
	}

	public static void main(String[] args)
   {
      JFrame frame = new JFrame("GiftBoxing");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
      if(args.length > 0)
      {
         readGift(args[0]);
         computeBox();
      }
        
      drawPolygon poly = new drawPolygon();
        
      frame.setContentPane(poly);
            
      frame.pack();
      frame.setVisible(true);
   }
	
} // Ende class ShowBoxing