View Javadoc

1   /*
2    * this class is for generating a java.awt.Rectangle using interface ShapeBean and the method genShape(...)
3    * the rectngle (Shape) will be using to define a clickable part on the ImageIcon
4    * all methods set and get and constructor without argument are for generating a xml file with java.beans.XMLEncoder
5    */
6   package org.lsst.ccs.utilities.zonesui;
7   
8   import java.awt.Shape;
9   
10  /**
11   *
12   * @author Pascal Durieu
13   */
14  public class ZPolygon implements ShapeBean{
15      private String[] pointCoords;
16      
17      public ZPolygon() {}
18      
19      public ZPolygon (String... str) {
20          this.pointCoords = str;
21      }
22      
23      //get method
24      public String[] getPoints () {return this.pointCoords;}
25      public void setPoints(String... st) {this.pointCoords = st;}
26      
27      //must repeat the first point at the end of the array for generating the polygon
28      //size of array is +1
29      public int getNbPoint () {
30          return (pointCoords.length+1);
31          
32      }    
33      public int[][] getXY() {
34          int[][] tabXY = new int[getNbPoint()][2];
35          for (int i=0;i<(getNbPoint()-1);i++) {
36              String[] coords = pointCoords[i].split(",");            
37              tabXY[i][0] = Integer.parseInt(coords[0]);
38              tabXY[i][1] = Integer.parseInt(coords[1]);
39          }
40          //repeat the first element to the last (cf Polygon class constructor's)
41          tabXY[getNbPoint()-1][0] = tabXY[0][0];
42          tabXY[getNbPoint()-1][1] = tabXY[0][1];
43           
44          return tabXY;
45      }
46      
47      public int[] getX(int[][] tab) {
48          int[] tabX = new int[tab.length];
49          for (int i=0;i<tab.length;i++) {
50              tabX[i] = tab[i][0];
51          }
52          return tabX;
53      }
54      public int[] getY(int[][] tab) {
55          int[] tabY = new int[tab.length];   
56          for (int i=0;i<tab.length;i++) {
57              tabY[i] = tab[i][1];         
58          }
59          return tabY;
60      }
61          
62      //method from ShapeBean interface to generate a Shape
63      @Override
64      public Shape genShape() {
65          return new java.awt.Polygon(getX(getXY()),getY(getXY()),getNbPoint() );
66      }
67      @Override
68      public String toString() {      
69          int[] ix = getX(getXY());
70          int[] iy = getY(getXY());
71          String str = "APolygon with "+ getNbPoint()+" points:";
72          for (int i=0;i<getNbPoint();i++) {
73              str += " ("+ix[i]+","+iy[i]+")";
74          }
75          return str;
76      }
77      
78  }