Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/util/FlexRectangle.java


1   /*
2    * FlexRectangle.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:31 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.util;
12  
13  import java.awt.Dimension;
14  import java.awt.Point;
15  import java.util.StringTokenizer;
16  
17  /**
18   * This class extends Rectangle so that it can be saved as a string
19   * in the database or in a text file and be reconstructed.
20   * Basically, this is a very simple Serializable interface.
21   * Two methods readObject (create an object from a string) 
22   * and writeObject (transforms an object to a string) implement
23   * that functionality.
24   */
25  public class FlexRectangle
26     extends java.awt.Rectangle
27  {
28     /**
29      * Constructs a new rectangle.
30      */
31     public FlexRectangle() {
32     }
33  
34     /**
35      * Constructs and initializes a rectangle with the specified parameters.
36      * @param x the x coordinate
37      * @param y the y coordinate
38      * @param width the width of the rectangle
39      * @param height the height of the rectangle
40      */
41     public FlexRectangle(int x, int y, int width, int height) {
42        super(x, y, width, height);
43     }
44  
45     /**
46      * Constructs a rectangle and initializes it with the specified width and 
47      * height parameters.
48      * @param width the width of the rectangle
49      * @param height the height of the rectangle
50      */
51     public FlexRectangle(int width, int height) {
52        super(width, height);
53     }
54  
55     /**
56      * Constructs a rectangle and initializes it to a specified point and  
57      * dimension.
58      * @param p the point
59      * @param d dimension 
60      */
61     public FlexRectangle(Point p, Dimension d) {
62        super(p, d);
63     }
64      
65     /**
66      * Constructs a rectangle and initializes it to the specified point.
67      * @param p the value of the x and y coordinate
68      */
69     public FlexRectangle(Point p) {
70        super(p);
71     }
72     
73     /**
74      * Constructs a rectangle and initializes it to the specified width and 
75      * height.
76      * @param d the value of the width and height
77      */
78     public FlexRectangle(Dimension d) {
79        super(d);
80     }
81  
82     public String writeObject()
83     {
84        return this.x + "|" + this.y + "|" + this.width + "|" + this.height;
85     }
86  
87     public static FlexRectangle readObject(String s)
88     {
89        StringTokenizer st = new StringTokenizer(s, "|"); 
90        int[] dims = new int[4];
91        
92        try
93        {
94           for (int i = 0; i < 4; i++)
95           { 
96              dims[i] = Integer.parseInt(st.nextToken()); 
97           } 
98        }
99        catch(NumberFormatException e)
100       {
101          return new FlexRectangle(20, 20, 400, 600);
102       }
103       
104       return new FlexRectangle(dims[0], dims[1], dims[2], dims[3]);
105    }
106 
107 }  // end of class