Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.graphics;
     18 
     19 import android.os.Parcel;
     20 import android.os.Parcelable;
     21 
     22 
     23 /**
     24  * Point holds two integer coordinates
     25  */
     26 public class Point implements Parcelable {
     27     public int x;
     28     public int y;
     29 
     30     public Point() {}
     31 
     32     public Point(int x, int y) {
     33         this.x = x;
     34         this.y = y;
     35     }
     36 
     37     public Point(Point src) {
     38         this.x = src.x;
     39         this.y = src.y;
     40     }
     41 
     42     /**
     43      * Set the point's x and y coordinates
     44      */
     45     public void set(int x, int y) {
     46         this.x = x;
     47         this.y = y;
     48     }
     49 
     50     /**
     51      * Negate the point's coordinates
     52      */
     53     public final void negate() {
     54         x = -x;
     55         y = -y;
     56     }
     57 
     58     /**
     59      * Offset the point's coordinates by dx, dy
     60      */
     61     public final void offset(int dx, int dy) {
     62         x += dx;
     63         y += dy;
     64     }
     65 
     66     /**
     67      * Returns true if the point's coordinates equal (x,y)
     68      */
     69     public final boolean equals(int x, int y) {
     70         return this.x == x && this.y == y;
     71     }
     72 
     73     @Override
     74     public boolean equals(Object o) {
     75         if (this == o) return true;
     76         if (o == null || getClass() != o.getClass()) return false;
     77 
     78         Point point = (Point) o;
     79 
     80         if (x != point.x) return false;
     81         if (y != point.y) return false;
     82 
     83         return true;
     84     }
     85 
     86     @Override
     87     public int hashCode() {
     88         int result = x;
     89         result = 31 * result + y;
     90         return result;
     91     }
     92 
     93     @Override
     94     public String toString() {
     95         return "Point(" + x + ", " + y + ")";
     96     }
     97 
     98     /**
     99      * Parcelable interface methods
    100      */
    101     @Override
    102     public int describeContents() {
    103         return 0;
    104     }
    105 
    106     /**
    107      * Write this point to the specified parcel. To restore a point from
    108      * a parcel, use readFromParcel()
    109      * @param out The parcel to write the point's coordinates into
    110      */
    111     @Override
    112     public void writeToParcel(Parcel out, int flags) {
    113         out.writeInt(x);
    114         out.writeInt(y);
    115     }
    116 
    117     public static final Parcelable.Creator<Point> CREATOR = new Parcelable.Creator<Point>() {
    118         /**
    119          * Return a new point from the data in the specified parcel.
    120          */
    121         public Point createFromParcel(Parcel in) {
    122             Point r = new Point();
    123             r.readFromParcel(in);
    124             return r;
    125         }
    126 
    127         /**
    128          * Return an array of rectangles of the specified size.
    129          */
    130         public Point[] newArray(int size) {
    131             return new Point[size];
    132         }
    133     };
    134 
    135     /**
    136      * Set the point's coordinates from the data stored in the specified
    137      * parcel. To write a point to a parcel, call writeToParcel().
    138      *
    139      * @param in The parcel to read the point's coordinates from
    140      */
    141     public void readFromParcel(Parcel in) {
    142         x = in.readInt();
    143         y = in.readInt();
    144     }
    145 }
    146