1 /* 2 * Copyright (C) 2011 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 com.android.chimpchat.core; 18 19 /** 20 * A class for holding information about view locations 21 */ 22 public class ChimpRect { 23 public int left; 24 public int top; 25 public int right; 26 public int bottom; 27 28 /** 29 * Creates an empty ChimpRect object. All coordinates are initialized to 0. 30 */ 31 public ChimpRect() {} 32 33 /** 34 * Create a new ChimpRect with the given coordinates. 35 * @param left The X coordinate of the left side of the rectagle 36 * @param top The Y coordinate of the top of the rectangle 37 * @param right The X coordinate of the right side of the rectagle 38 * @param bottom The Y coordinate of the bottom of the rectangle 39 */ 40 public ChimpRect(int left, int top, int right, int bottom) { 41 this.left = left; 42 this.top = top; 43 this.right = right; 44 this.bottom = bottom; 45 } 46 47 /** 48 * A comparison method to determine if the object is equivalent to other ChimpRects. 49 * @param obj The object to compare it to 50 * @return True if the object is an equivalent rectangle, false otherwise. 51 */ 52 @Override 53 public boolean equals(Object obj) { 54 if(obj instanceof ChimpRect){ 55 ChimpRect r = (ChimpRect) obj; 56 if (r != null) { 57 return left == r.left && top == r.top && right == r.right 58 && bottom == r.bottom; 59 } 60 } 61 return false; 62 } 63 64 /** 65 * The width of the ChimpRect 66 * @return the width of the rectangle 67 */ 68 public int getWidth() { 69 return right-left; 70 } 71 72 /** 73 * The height of the ChimpRect 74 * @return the height of the rectangle 75 */ 76 public int getHeight() { 77 return bottom-top; 78 } 79 80 /** 81 * Returns a 2 item int array with the x, y coordinates of the center of the ChimpRect. 82 * @return a 2 item int array. The first item is the x value of the center of the ChimpRect and 83 * the second item is the y value. 84 */ 85 public int[] getCenter() { 86 int[] center = new int[2]; 87 center[0] = left + getWidth() / 2; 88 center[1] = top + getHeight() / 2; 89 return center; 90 } 91 92 /** 93 * Returns a representation of the rectangle in string form 94 * @return a string representation of the rectangle 95 */ 96 public String toString() { 97 StringBuilder sb = new StringBuilder(); 98 sb.append("ChimpRect "); 99 sb.append("top: ").append(top).append(" "); 100 sb.append("right: ").append(right).append(" "); 101 sb.append("bottom: ").append(bottom).append(" "); 102 sb.append("left: ").append(left).append(" "); 103 return sb.toString(); 104 } 105 } 106