View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.samples.AirScanner;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.Image;
6   import java.awt.image.BufferedImage;
7   import java.text.DateFormat;
8   import java.text.DecimalFormat;
9   import java.text.SimpleDateFormat;
10  import java.util.Date;
11  import java.util.TimeZone;
12  
13  /**
14   * ToolBox class covers methods that help solve comon/easy tasks.
15   *
16   * @author vejmanm
17   */
18  public class ToolBox
19  {
20      /**
21       * Returns maximum value from the input array.
22       *
23       * @param array Input array.
24       * @return Returns maximum value within the array.
25       */
26      public static int getMax(int[] array)
27      {
28          int max = 0;
29          for(int i : array)
30          {
31              if(i > max)
32              {
33                  max = i;
34              }
35          }
36          return max;
37      }
38  
39      /**
40       * Uses DateFormat to format the input value to hours, minutes, seconds and
41       * mili seconds.
42       *
43       * @param elapsed Elapsed time in miliseconds.
44       * @return Returns string time formated as HH:mm:ss.SSS.
45       */
46      public static String getTime(long elapsed)
47      {
48          DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
49          df.setTimeZone(TimeZone.getTimeZone("GMT+0"));
50          return (df.format(new Date(elapsed)));
51      }
52  
53      /**
54       * Uses Math.min method to determine the minimum of the four input values.
55       *
56       * @param i first number
57       * @param j second number
58       * @param k third nubmer
59       * @param l fourth number
60       * @return Returns minimal value from the four input integers
61       */
62      public static int getMin(int i, int j, int k, int l)
63      {
64          return Math.min(Math.min(i, j), Math.min(k, l));
65      }
66  
67      /**
68       * Determines an angle in degrees based on sin and cos of the angle.
69       *
70       * @param sx Sinus x
71       * @param cx Cosinus x
72       * @return Returns angle in degrees.
73       */
74      public static double getAngle(double sx, double cx)
75      {
76          if(sx > 0 && cx > 0)
77          {
78              return Math.acos(Math.abs(cx)) * 180 / Math.PI;
79          }
80          else if(sx > 0 && cx <= 0)
81          {
82              return 180 - Math.acos(Math.abs(cx)) * 180 / Math.PI;
83          }
84          else if(sx <= 0 && cx <= 0)
85          {
86              return 180 + Math.acos(Math.abs(cx)) * 180 / Math.PI;
87          }
88          else
89          {
90              return 360 - Math.acos(Math.abs(cx)) * 180 / Math.PI;
91          }
92      }
93  
94      /**
95       * Uses DecimalFormat to format the <B>value</B>.
96       *
97       * @param value Double value to convert to string.
98       * @return Returns string representation of the <b>value</b> formated to two
99       * decimal places.
100      */
101     public static String getTwoDecimalPlaces(double value)
102     {
103         DecimalFormat df = new DecimalFormat("#.##");
104         return df.format(value);
105     }
106 
107     /**
108      * Creates new a image enlarged by <b>x</b> and <b>y</b> and draws old image
109      * at <b>offsetX</b>, <b>offsetY</b>.
110      *
111      * @param original Original image to be stretched.
112      * @param x Widht enlargement.
113      * @param y Height enlargement.
114      * @param offsetX Offset for original picture to be drawn at x-axis.
115      * @param offsetY Offset for original picture to be drawn at y-axis.
116      * @return Returns narger image with black background.
117      */
118     public static BufferedImage resizeImage(Image original, int x, int y, int offsetX, int offsetY)
119     {
120         BufferedImage resized = new BufferedImage(original.getWidth(null) + x, original.getHeight(null) + y, BufferedImage.TYPE_INT_ARGB);
121         Graphics2D g = resized.createGraphics();
122         g.setColor(Color.BLACK);
123         g.fillRect(0, 0, resized.getWidth(), resized.getHeight());
124         g.drawImage(original, offsetX, offsetY, null);
125         return resized;
126     }
127 
128     /**
129      * Creates new array enlongated by <b>x</b> and <b>y</b> and fills it with
130      * old walues at <b>offsetX</b>, <b>offsetY</b>.
131      *
132      * @param original Original array to be lengthened.
133      * @param x Width enlargement.
134      * @param y Height enlargement.
135      * @param offsetX Offset for original data at x-axis.
136      * @param offsetY Offset for original data at y-axis.
137      * @return Returns resized array.
138      */
139     public static double[][] resizeArray(double[][] original, int x, int y, int offsetX, int offsetY)
140     {
141         double[][] resized = initArray(original.length + x, original[0].length + y);
142 
143         for(int i = 0; i < original.length; i++)
144         {
145             System.arraycopy(original[i], 0, resized[i + offsetX], offsetY, original[0].length);
146         }
147         return resized;
148     }
149 
150     /**
151      * Initial value will be Double.MIN_VALUE. Zeroes would be missunderstood
152      * while creating an image.
153      */
154     public static double[][] initArray(int width, int height)
155     {
156         double data[][] = new double[width][height];
157         for(int i = 0; i < data.length; i++)
158         {
159             for(int j = 0; j < data[i].length; j++)
160             {
161                 data[i][j] = Double.MIN_VALUE;
162             }
163         }
164         return data;
165     }
166 }