FSBounds.java
Upload User: lchgslz84
Upload Date: 2013-08-25
Package Size: 3029k
Code Size: 12k
Category:

FlashMX/Flex

Development Platform:

Java

  1. /*
  2.  * FSBounds.java
  3.  * Transform
  4.  * 
  5.  * Copyright (c) 2001-2006 Flagstone Software Ltd. All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without modification, 
  8.  * are permitted provided that the following conditions are met:
  9.  *
  10.  *  * Redistributions of source code must retain the above copyright notice, this
  11.  *    list of conditions and the following disclaimer.
  12.  *  * Redistributions in binary form must reproduce the above copyright notice, 
  13.  *    this list of conditions and the following disclaimer in the documentation 
  14.  *    and/or other materials provided with the distribution.
  15.  *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
  16.  *    may be used to endorse or promote products derived from this software 
  17.  *    without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
  20.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  21.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
  22.  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
  23.  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
  24.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
  25.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
  26.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  27.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  28.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30. package com.flagstone.transform;
  31. /**
  32. The FSBounds class is used to define the area inside which shapes, text fields and 
  33. characters are drawn.
  34.  
  35. <p>In Flash the axes are specified relative to the top left corner of the screen and the bounding area is defined by two pairs of coordinates that identify the top left and bottom right corners of a rectangle.</p>
  36. <img src="doc-files/bounds.gif">
  37. <P><!-- blank paragraph to add space between the image and the next table --></P>
  38. <table class="datasheet">
  39. <tr><th align="left" colspan="2">Attributes</th></tr>
  40. <tr>
  41. <td><a name="FSBounds_0">minX</a></td>
  42. <td>The x-coordinate of the upper left corner of the rectangle.</td>
  43. </tr>
  44. <tr>
  45. <td><a name="FSBounds_1">minY</a></td>
  46. <td>The y-coordinate of the upper left corner of the rectangle.</td>
  47. </tr>
  48. <tr>
  49. <td><a name="FSBounds_2">maxX</a></td>
  50. <td>The x-coordinate of the lower right corner of the rectangle.</td>
  51. </tr>
  52. <tr>
  53. <td><a name="FSBounds_3">maxY</a></td>
  54. <td>The y-coordinate of the lower right corner of the rectangle.</td>
  55. </tr>
  56. </table>
  57. <p>The coordinates for each corner also specify the coordinate range so specifying 
  58. a bounding rectangle with the points (-100,-100) and (100,100) defines a rectangle 
  59. 200 twips by 200 twips with the point (0,0) located in the centre. Specifying the 
  60. points (0,0) and (200,200) defines a rectangle with the same size however the centre 
  61. is now located at (100,100).</p>
  62. <p>The bounding rectangle does not clip the object when it is drawn. Lines and curves 
  63. drawn outside of the rectangle will still be displayed. However when the bounding 
  64. rectangle is defined for an FSMovie object then this defines the size of the Flash 
  65. Player screen and shapes drawn outside of the bounding rectangle will not be displayed.</p>
  66. <h1 class="datasheet">Examples</h1>
  67. <p>To create an FSBounds object simply specify the coordinates of the corners of the 
  68. rectangle in the following order: top left x, top left y, bottom right x, bottom 
  69. right y.</p>
  70. <pre>
  71. FSBounds bounds = new FSBounds(0, 0, 100, 200);
  72. </pre>
  73. <p>Creates a bounding rectangle for an object which is 100 twips wide and 200 twips high. The coordinate range for the object is from 0..100 along the x-axis and 0..200 along the y-axis. The origin (0,0) will be at the top left corner of the object as it is displayed on the screen.
  74. <pre>
  75. FSBounds bounds = new FSBounds (-100, -200, 100, 200);
  76. </pre>
  77. <p>Creates a bounding rectangle for an object which is 200 twips wide and 400 twips high. The coordinate range for the object is from -100..100 along the x-axis and -200..200 along the y-axis. The origin (0,0) will be in the middle of the object as it is displayed on the screen.
  78. <h1 class="datasheet">History</h1>
  79. <p>The FSBounds class corresponds to the RECT data type, in the Macromedia Flash 
  80. (SWF) File Format Specification. It was introduced in Flash 1.</p>
  81.  */
  82. public class FSBounds extends FSTransformObject
  83. {
  84.     private int minX = 0;
  85.     private int minY = 0;
  86.     private int maxX = 0;
  87.     private int maxY = 0;
  88.     
  89.     /**
  90.      * Construct an FSBounds object an initialize it with values decoded from 
  91.      * a binary encoded FSBounds object.
  92.      * 
  93.      * @param coder an FSCoder object containing an FSBounds encoded as binary
  94.      * data.
  95.      */
  96.     public FSBounds(FSCoder coder)
  97.     {
  98.         decode(coder);
  99.     }
  100.     /** Constructs an FSBounds object representing a rectangle with the top left corner at (xl,yl) and bottom right corner at (xr,yr).
  101.         @param xl x-coordinate of the top left corner.
  102.         @param yl y-coordinate of the top left corner.
  103.         @param xr x-coordinate of bottom right corner.
  104.         @param yr y-coordinate of bottom right corner.
  105.         */
  106.     public FSBounds(int xl, int yl, int xr, int yr)
  107.     {
  108.         setMinX(xl);
  109.         setMinY(yl);
  110.         setMaxX(xr);
  111.         setMaxY(yr);
  112.     }
  113.     /**
  114.      * Construct an FSBounds object and initialize it with value from another 
  115.      * FSBounds object.
  116.      * 
  117.      * @param obj an FSBounds object.
  118.      */
  119.     public FSBounds(FSBounds obj)
  120.     {
  121.         minX = obj.minX;
  122.         maxX = obj.maxX;
  123.         minY = obj.minY;
  124.         maxY = obj.maxY;
  125.     }
  126.     /** Gets the x-coordinate of the top left corner of the bounding rectangle.
  127.         @return the x-coordinate of the lower left corner of the bounding rectangle.
  128.         */
  129.     public int getMinX() 
  130.     {
  131.         return minX;
  132.     }
  133.     /** Gets the x-coordinate of the bottom right corner of the bounding rectangle. 
  134.     
  135.         @return the x-coordinate of the bottom right corner of the bounding rectangle.
  136.         */
  137.     public int getMaxX() 
  138.     {
  139.         return maxX;
  140.     }
  141.     /** Gets the y-coordinate of the top left corner of the bounding rectangle. 
  142.     
  143.         @return the y-coordinate of the top left corner of the bounding rectangle.
  144.         */
  145.     public int getMinY() 
  146.     {
  147.         return minY;
  148.     }
  149.     /** Gets the y-coordinate of the bottom right corner of the bounding rectangle. 
  150.     
  151.         @return the y-coordinate of the bottom right corner of the bounding rectangle.
  152.         */
  153.     public int getMaxY()
  154.     {
  155.         return maxY;
  156.     }
  157.     /** Sets the x-coordinate of the top left corner of the bounding rectangle.
  158.         @param aNumber x-coordinate of the lower left corner of the bounding rectangle.
  159.         */
  160.     public void setMinX(int aNumber) 
  161.     {
  162.         minX = aNumber;
  163.     }
  164.     /** Sets the x-coordinate of the bottom right corner of the bounding rectangle.
  165.         
  166.         @param aNumber x-coordinate of the bottom right corner of the bounding rectangle.
  167.         */
  168.     public void setMaxX(int aNumber)
  169.     {
  170.         maxX = aNumber;
  171.     }
  172.     /** Sets the y-coordinate of the upper left corner of the bounding rectangle.
  173.         
  174.         @param aNumber y-coordinate of the lower left corner of the bounding rectangle.
  175.         */
  176.     public void setMinY(int aNumber)
  177.     {
  178.         minY = aNumber;
  179.     }
  180.     /** Sets the y-coordinate of the bottom right corner of the bounding rectangle.
  181.         
  182.         @param aNumber x-coordinate of the bottom right corner of the bounding rectangle.
  183.         */
  184.     public void setMaxY(int aNumber)
  185.     {
  186.         maxY = aNumber;
  187.     }
  188.     /** Gets the width of the rectangle.
  189.         @return the width of the rectangle.
  190.         */
  191.     public int getWidth()
  192.     {
  193.         return maxX - minX;
  194.     }
  195.     /** Gets the height of the rectangle. 
  196.     
  197.         @return the height of the rectangle.
  198.         */
  199.     public int getHeight()
  200.     {
  201.         return maxY - minY;
  202.     }
  203.     /** Sets the x and y coordinates of the lower corner.
  204.     
  205.         @param xl the x-coordinate of the top left corner point.
  206.         @param yl the y-coordinate of the top left corner point.
  207.         */
  208.     public void setMin(int xl, int yl)
  209.     {
  210.         minX = xl;
  211.         minY = yl;
  212.     }
  213.     /** Sets the x and y coordinates of the upper corner. 
  214.         
  215.         @param xr the x-coordinate of the bottom right corner point.
  216.         @param yr the y-coordinate of the bottom right corner point.
  217.         */
  218.     public void setMax(int xr, int yr)
  219.     {
  220.         maxX = xr;
  221.         maxY = yr;
  222.     }
  223.     /** Sets the x and y coordinates of the lower and upper corner point.
  224.             
  225.         @param xl the x-coordinate of the top left corner point.
  226.         @param yl the y-coordinate of the top left corner point.
  227.         @param xr the x-coordinate of the bottom right corner point.
  228.         @param yr the y-coordinate of the bottom right corner point.
  229.         */
  230.     public void setPoints(int xl, int yl, int xr, int yr)
  231.     {
  232.         minX = xl;
  233.         minY = yl;
  234.         maxX = xr;
  235.         maxY = yr;
  236.     }
  237.     
  238.     /** 
  239.      * Returns true if anObject is equal to this one. Objects are considered 
  240.      * equal if they would generate identical binary data when they are encoded 
  241.      * to a Flash file.
  242.      *
  243.      * @return true if this object would be identical to anObject when encoded.
  244.      */
  245.     public boolean equals(Object anObject)
  246.     {
  247.         boolean result = false;
  248.         
  249.         if (super.equals(anObject))
  250.         {
  251.             FSBounds typedObject = (FSBounds)anObject;
  252.             
  253.             result = minX == typedObject.minX;
  254.             result = result && minY == typedObject.minY;
  255.             result = result && maxX == typedObject.maxX;
  256.             result = result && maxY == typedObject.maxY;
  257.         }
  258.         return result;
  259.     }
  260.     public void appendDescription(StringBuffer buffer, int depth)
  261.     {
  262.         buffer.append(name());
  263.         if (depth > 0)
  264.         {
  265.             buffer.append(": { ");
  266.             Transform.append(buffer, "minX", minX);
  267.             Transform.append(buffer, "minY", minY);
  268.             Transform.append(buffer, "maxX", maxX);
  269.             Transform.append(buffer, "maxY", maxY);
  270.             buffer.append("}");
  271.         }
  272.     }
  273.     public int length(FSCoder coder)
  274.     {
  275.         int numberOfBits = 0;
  276.         int fieldSize = FSCoder.size(new int[]{minX, maxX, minY, maxY}, true);
  277.         
  278.         numberOfBits = 5 + fieldSize*4;
  279.         numberOfBits += (numberOfBits % 8 > 0) ? 8 - (numberOfBits % 8) : 0;
  280.         
  281.         return numberOfBits>>3; 
  282.     }
  283.     
  284.     public void encode(FSCoder coder)
  285.     {
  286.         int numberOfBits = FSCoder.size(new int[]{minX, maxX, minY, maxY}, true);
  287.         
  288.         coder.alignToByte();    
  289.         coder.writeBits(numberOfBits, 5);
  290.         coder.writeBits(minX, numberOfBits);
  291.         coder.writeBits(maxX, numberOfBits);
  292.         coder.writeBits(minY, numberOfBits);
  293.         coder.writeBits(maxY, numberOfBits);    
  294.         coder.alignToByte();
  295.     }
  296.     
  297.     public void decode(FSCoder coder)
  298.     {
  299.         coder.alignToByte();
  300.         
  301.         int fieldSize = coder.readBits(5, false);
  302.         
  303.         minX = coder.readBits(fieldSize, true);
  304.         maxX = coder.readBits(fieldSize, true);
  305.         minY = coder.readBits(fieldSize, true);
  306.         maxY = coder.readBits(fieldSize, true);
  307.         
  308.         coder.alignToByte();
  309.     }
  310. }