]> git.basschouten.com Git - openhab-addons.git/blob
50c3cd8a9f78e33028ddc9822c0c8ac12e3c1d2c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nanoleaf.internal.layout.shape;
14
15 import java.awt.Graphics2D;
16 import java.awt.geom.Rectangle2D;
17 import java.util.Arrays;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.nanoleaf.internal.layout.ImagePoint2D;
22 import org.openhab.binding.nanoleaf.internal.layout.Point2D;
23 import org.openhab.binding.nanoleaf.internal.layout.ShapeType;
24
25 /**
26  * A square shape.
27  *
28  * @author Jørgen Austvik - Initial contribution
29  */
30 @NonNullByDefault
31 public class Square extends Shape {
32     public Square(ShapeType shapeType, int panelId, Point2D position, int orientation) {
33         super(shapeType, panelId, position, orientation);
34     }
35
36     @Override
37     public List<Point2D> generateOutline() {
38         int sideLength = (int) getShapeType().getSideLength();
39
40         Point2D current = getPosition();
41         Point2D corner2 = new Point2D(current.getX() + sideLength, current.getY());
42         Point2D corner3 = new Point2D(current.getX() + sideLength, current.getY() + sideLength);
43         Point2D corner4 = new Point2D(current.getX(), current.getY() + sideLength);
44         return Arrays.asList(getPosition(), corner2, corner3, corner4);
45     }
46
47     @Override
48     protected ImagePoint2D labelPosition(Graphics2D graphics, List<ImagePoint2D> outline) {
49         // Center of square is average of oposite corners
50         ImagePoint2D p0 = outline.get(0);
51         ImagePoint2D p2 = outline.get(2);
52
53         Rectangle2D rect = graphics.getFontMetrics().getStringBounds(Integer.toString(getPanelId()), graphics);
54
55         return new ImagePoint2D((p0.getX() + p2.getX()) / 2 - (int) (rect.getWidth() / 2),
56                 (p0.getY() + p2.getY()) / 2 + (int) (rect.getHeight() / 2));
57     }
58 }