Java tutorial
/* * Copyright (C) 2015-2016 NS Solutions Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.htmlhifive.pitalium.image.model; import java.awt.Rectangle; import java.io.Serializable; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.htmlhifive.pitalium.common.util.JSONUtils; /** * ? */ public class RectangleArea implements Serializable { private static final long serialVersionUID = 1L; /** * ?x */ private final double x; /** * ?y */ private final double y; /** * */ private final double width; /** * ? */ private final double height; /** * ????????? * * @param x ?x * @param y ?y * @param width * @param height ? */ @JsonCreator public RectangleArea(@JsonProperty("x") double x, @JsonProperty("y") double y, @JsonProperty("width") double width, @JsonProperty("height") double height) { this.x = x; this.y = y; this.width = width; this.height = height; } /** * x???? * * @return x */ public double getX() { return x; } /** * y???? * * @return y */ public double getY() { return y; } /** * ?????? * * @return */ public double getWidth() { return width; } /** * ??????? * * @return ? */ public double getHeight() { return height; } /** * ?????????? * * @return ?????? */ public RectangleArea floor() { return new RectangleArea((int) x, (int) y, (int) width, (int) height); } /** * ?????????? * * @return ?????? */ public RectangleArea round() { return new RectangleArea(Math.round(x), Math.round(y), Math.round(width), Math.round(height)); } /** * ????????? * * @return ????? */ public RectangleArea ceil() { return new RectangleArea(Math.ceil(x), Math.ceil(y), Math.ceil(width), Math.ceil(height)); } /** * ???????? * * @param deltaX X??? * @param deltaY Y??? * @return ? */ public RectangleArea move(double deltaX, double deltaY) { return new RectangleArea(x + deltaX, y + deltaY, width, height); } /** * ???? * * @param scale * @return ??? */ public RectangleArea applyScale(double scale) { double scaledX = Math.round(x * scale); double scaledY = Math.round(y * scale); double scaledWidth = Math.round(width * scale); double scaledHeight = Math.round(height * scale); return new RectangleArea(scaledX, scaledY, scaledWidth, scaledHeight); } /** * ???????{@link Rectangle}????? * * @return ???????{@link Rectangle} */ public Rectangle toRectangle() { RectangleArea rect = round(); return new Rectangle((int) rect.x, (int) rect.y, (int) rect.width, (int) rect.height); } /** * ??????? * * @param o ? * @return ????true */ @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } RectangleArea that = (RectangleArea) o; if (Double.compare(that.x, x) != 0) { return false; } if (Double.compare(that.y, y) != 0) { return false; } if (Double.compare(that.width, width) != 0) { return false; } return Double.compare(that.height, height) == 0; } @Override public int hashCode() { int result; long temp; final int hashPrime = 31; final int shiftBit = 32; temp = Double.doubleToLongBits(x); result = (int) (temp ^ (temp >>> shiftBit)); temp = Double.doubleToLongBits(y); result = hashPrime * result + (int) (temp ^ (temp >>> shiftBit)); temp = Double.doubleToLongBits(width); result = hashPrime * result + (int) (temp ^ (temp >>> shiftBit)); temp = Double.doubleToLongBits(height); result = hashPrime * result + (int) (temp ^ (temp >>> shiftBit)); return result; } @Override public String toString() { return JSONUtils.toString(this); } }