2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
4 * lanterna is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
;
22 * A 2-d position in 'terminal space'. Please note that the coordinates are 0-indexed, meaning 0x0 is the top left
23 * corner of the terminal. This object is immutable so you cannot change it after it has been created. Instead, you
24 * can easily create modified 'clones' by using the 'with' methods.
28 public class TerminalPosition
{
31 * Constant for the top-left corner (0x0)
33 public static final TerminalPosition TOP_LEFT_CORNER
= new TerminalPosition(0, 0);
35 * Constant for the 1x1 position (one offset in both directions from top-left)
37 public static final TerminalPosition OFFSET_1x1
= new TerminalPosition(1, 1);
39 private final int row
;
40 private final int column
;
43 * Creates a new TerminalPosition object, which represents a location on the screen. There is no check to verify
44 * that the position you specified is within the size of the current terminal and you can specify negative positions
47 * @param column Column of the location, or the "x" coordinate, zero indexed (the first column is 0)
48 * @param row Row of the location, or the "y" coordinate, zero indexed (the first row is 0)
50 public TerminalPosition(int column
, int row
) {
56 * Returns the index of the column this position is representing, zero indexed (the first column has index 0).
57 * @return Index of the column this position has
59 public int getColumn() {
64 * Returns the index of the row this position is representing, zero indexed (the first row has index 0)
65 * @return Index of the row this position has
72 * Creates a new TerminalPosition object representing a position with the same column index as this but with a
74 * @param row Index of the row for the new position
75 * @return A TerminalPosition object with the same column as this but with a specified row index
77 public TerminalPosition
withRow(int row
) {
78 if(row
== 0 && this.column
== 0) {
79 return TOP_LEFT_CORNER
;
81 return new TerminalPosition(this.column
, row
);
85 * Creates a new TerminalPosition object representing a position with the same row index as this but with a
86 * supplied column index.
87 * @param column Index of the column for the new position
88 * @return A TerminalPosition object with the same row as this but with a specified column index
90 public TerminalPosition
withColumn(int column
) {
91 if(column
== 0 && this.row
== 0) {
92 return TOP_LEFT_CORNER
;
94 return new TerminalPosition(column
, this.row
);
98 * Creates a new TerminalPosition object representing a position on the same row, but with a column offset by a
99 * supplied value. Calling this method with delta 0 will return this, calling it with a positive delta will return
100 * a terminal position <i>delta</i> number of columns to the right and for negative numbers the same to the left.
101 * @param delta Column offset
102 * @return New terminal position based off this one but with an applied offset
104 public TerminalPosition
withRelativeColumn(int delta
) {
108 return withColumn(column
+ delta
);
112 * Creates a new TerminalPosition object representing a position on the same column, but with a row offset by a
113 * supplied value. Calling this method with delta 0 will return this, calling it with a positive delta will return
114 * a terminal position <i>delta</i> number of rows to the down and for negative numbers the same up.
115 * @param delta Row offset
116 * @return New terminal position based off this one but with an applied offset
118 public TerminalPosition
withRelativeRow(int delta
) {
122 return withRow(row
+ delta
);
126 * Creates a new TerminalPosition object that is 'translated' by an amount of rows and columns specified by another
127 * TerminalPosition. Same as calling
128 * <code>withRelativeRow(translate.getRow()).withRelativeColumn(translate.getColumn())</code>
129 * @param translate How many columns and rows to translate
130 * @return New TerminalPosition that is the result of the original with added translation
132 public TerminalPosition
withRelative(TerminalPosition translate
) {
133 return withRelative(translate
.getColumn(), translate
.getRow());
137 * Creates a new TerminalPosition object that is 'translated' by an amount of rows and columns specified by the two
138 * parameters. Same as calling
139 * <code>withRelativeRow(deltaRow).withRelativeColumn(deltaColumn)</code>
140 * @param deltaColumn How many columns to move from the current position in the new TerminalPosition
141 * @param deltaRow How many rows to move from the current position in the new TerminalPosition
142 * @return New TerminalPosition that is the result of the original position with added translation
144 public TerminalPosition
withRelative(int deltaColumn
, int deltaRow
) {
145 return withRelativeRow(deltaRow
).withRelativeColumn(deltaColumn
);
149 public String
toString() {
150 return "[" + column
+ ":" + row
+ "]";
154 public int hashCode() {
156 hash
= 23 * hash
+ this.row
;
157 hash
= 23 * hash
+ this.column
;
161 public boolean equals(int columnIndex
, int rowIndex
) {
162 return this.column
== columnIndex
&&
163 this.row
== rowIndex
;
167 public boolean equals(Object obj
) {
171 if (getClass() != obj
.getClass()) {
174 final TerminalPosition other
= (TerminalPosition
) obj
;
175 return this.row
== other
.row
&& this.column
== other
.column
;