View Javadoc
1   package nl.tudelft.simulation.dsol.animation.gis;
2   
3   import java.awt.geom.Rectangle2D;
4   import java.io.IOException;
5   import java.io.ObjectInputStream;
6   import java.io.ObjectOutputStream;
7   import java.io.Serializable;
8   
9   /**
10   * The SerializableRectangle2d class is a serializable version of the <code>java.awt.geom.Rectangle2D</code> class, yet in an
11   * immutable form.
12   * <p>
13   * Copyright (c) 2020-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
14   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
15   * project is distributed under a three-clause BSD-style license, which can be found at
16   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
17   * </p>
18   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   */
20  public abstract class SerializableRectangle2d extends Rectangle2D implements Serializable
21  {
22      /** */
23      private static final long serialVersionUID = 20201015L;
24  
25      /**
26       * constructs a new SerializableRectangle2d.
27       */
28      protected SerializableRectangle2d()
29      {
30          super();
31      }
32  
33      /**
34       * The SerializableRectangle2d.Double class is a serializable version of the <code>java.awt.geom.Rectangle2D.Double</code>
35       * class.
36       */
37      public static class Double extends SerializableRectangle2d implements Serializable
38      {
39          /** */
40          private static final long serialVersionUID = 20201015L;
41  
42          /** the rectangle. */
43          private Rectangle2D.Double rectangle;
44  
45          /**
46           * constructs a new SerializableRectangle2D.Double.
47           */
48          public Double()
49          {
50              this.rectangle = new Rectangle2D.Double();
51          }
52  
53          /**
54           * constructs a new SerializableRectangle2d.Double.
55           * @param x double; lower x
56           * @param y double; lower y
57           * @param w double; width
58           * @param h double; height
59           */
60          public Double(final double x, final double y, final double w, final double h)
61          {
62              this.rectangle = new Rectangle2D.Double(x, y, w, h);
63          }
64  
65          /** {@inheritDoc} */
66          @Override
67          public Rectangle2D createIntersection(final Rectangle2D r)
68          {
69              return this.rectangle.createIntersection(r);
70          }
71  
72          /** {@inheritDoc} */
73          @Override
74          public Rectangle2D createUnion(final Rectangle2D r)
75          {
76              return this.rectangle.createUnion(r);
77          }
78  
79          /** {@inheritDoc} */
80          @Override
81          public Rectangle2D getBounds2D()
82          {
83              return this.rectangle.getBounds2D();
84          }
85  
86          /** {@inheritDoc} */
87          @Override
88          public double getHeight()
89          {
90              return this.rectangle.getHeight();
91          }
92  
93          /** {@inheritDoc} */
94          @Override
95          public double getWidth()
96          {
97              return this.rectangle.getWidth();
98          }
99  
100         /** {@inheritDoc} */
101         @Override
102         public double getX()
103         {
104             return this.rectangle.getX();
105         }
106 
107         /** {@inheritDoc} */
108         @Override
109         public double getY()
110         {
111             return this.rectangle.getY();
112         }
113 
114         /** {@inheritDoc} */
115         @Override
116         public boolean isEmpty()
117         {
118             return this.rectangle.isEmpty();
119         }
120 
121         /** {@inheritDoc} */
122         @Override
123         public int outcode(final double x, final double y)
124         {
125             return this.rectangle.outcode(x, y);
126         }
127 
128         /** {@inheritDoc} */
129         @Override
130         public void setRect(final double x, final double y, final double w, final double h)
131         {
132             this.rectangle.setRect(x, y, w, h);
133         }
134 
135         /** {@inheritDoc} */
136         @Override
137         public void setRect(final Rectangle2D r)
138         {
139             this.rectangle.setRect(r);
140         }
141 
142         /** {@inheritDoc} */
143         @Override
144         public String toString()
145         {
146             return this.rectangle.toString();
147         }
148 
149         /**
150          * Serialize the object to the stream.
151          * @param out ObjectOutputStream; the outputstream
152          * @throws java.io.IOException on exception
153          */
154         private void writeObject(final ObjectOutputStream out) throws java.io.IOException
155         {
156             out.writeDouble(this.rectangle.getX());
157             out.writeDouble(this.rectangle.getY());
158             out.writeDouble(this.rectangle.getWidth());
159             out.writeDouble(this.rectangle.getHeight());
160         }
161 
162         /**
163          * Read a serialized object from the stream.
164          * @param in ObjectInputStream; the input
165          * @throws IOException on exception
166          */
167         private void readObject(final ObjectInputStream in) throws IOException
168         {
169             this.rectangle = new Rectangle2D.Double(in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble());
170         }
171     }
172 
173     /**
174      * The SerializableRectangle2d.Float class is a serializable version of the <code>java.awt.geom.Rectangle2D.Double</code>
175      * class.
176      */
177     public static class Float extends SerializableRectangle2d
178     {
179 
180         /** the rectangle. */
181         private Rectangle2D rectangle;
182 
183         /**
184          * constructs a new SerializableRectangle2d.Float.
185          */
186         public Float()
187         {
188             this.rectangle = new Rectangle2D.Float();
189         }
190 
191         /**
192          * constructs a new SerializableRectangle2d.Float.
193          * @param x float; the lower x
194          * @param y float; the lower y
195          * @param w float; the width
196          * @param h float; the height
197          */
198         public Float(final float x, final float y, final float w, final float h)
199         {
200             this.rectangle = new Rectangle2D.Float(x, y, w, h);
201         }
202 
203         /** {@inheritDoc} */
204         @Override
205         public Rectangle2D createIntersection(final Rectangle2D r)
206         {
207             return this.rectangle.createIntersection(r);
208         }
209 
210         /** {@inheritDoc} */
211         @Override
212         public Rectangle2D createUnion(final Rectangle2D r)
213         {
214             return this.rectangle.createUnion(r);
215         }
216 
217         /** {@inheritDoc} */
218         @Override
219         public Rectangle2D getBounds2D()
220         {
221             return this.rectangle.getBounds2D();
222         }
223 
224         /** {@inheritDoc} */
225         @Override
226         public double getHeight()
227         {
228             return this.rectangle.getHeight();
229         }
230 
231         /** {@inheritDoc} */
232         @Override
233         public double getWidth()
234         {
235             return this.rectangle.getWidth();
236         }
237 
238         /** {@inheritDoc} */
239         @Override
240         public double getX()
241         {
242             return this.rectangle.getX();
243         }
244 
245         /** {@inheritDoc} */
246         @Override
247         public double getY()
248         {
249             return this.rectangle.getY();
250         }
251 
252         /** {@inheritDoc} */
253         @Override
254         public boolean isEmpty()
255         {
256             return this.rectangle.isEmpty();
257         }
258 
259         /** {@inheritDoc} */
260         @Override
261         public int outcode(final double x, final double y)
262         {
263             return this.rectangle.outcode(x, y);
264         }
265 
266         /**
267          * @param x float; the lower x
268          * @param y float; the lower y
269          * @param w float; the width
270          * @param h float; the height
271          */
272         public void setRect(final float x, final float y, final float w, final float h)
273         {
274             this.rectangle.setRect(x, y, w, h);
275         }
276 
277         /** {@inheritDoc} */
278         @Override
279         public void setRect(final double x, final double y, final double w, final double h)
280         {
281             this.rectangle.setRect(x, y, w, h);
282         }
283 
284         /** {@inheritDoc} */
285         @Override
286         public void setRect(final Rectangle2D r)
287         {
288             this.rectangle.setRect(r);
289         }
290 
291         /** {@inheritDoc} */
292         @Override
293         public String toString()
294         {
295             return this.rectangle.toString();
296         }
297 
298         /**
299          * Serialize the object to the stream.
300          * @param out ObjectOutputStream; the stream
301          * @throws IOException on IOException
302          */
303         private void writeObject(final ObjectOutputStream out) throws IOException
304         {
305             out.writeDouble(this.rectangle.getX());
306             out.writeDouble(this.rectangle.getY());
307             out.writeDouble(this.rectangle.getWidth());
308             out.writeDouble(this.rectangle.getHeight());
309         }
310 
311         /**
312          * Read a serialized object from the stream.
313          * @param in ObjectInputStream; the stream
314          * @throws IOException on IOException
315          */
316         private void readObject(final ObjectInputStream in) throws IOException
317         {
318             this.rectangle = new Rectangle2D.Float();
319             this.rectangle.setRect(in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble());
320         }
321     }
322 }