diff --git a/src/com/valkryst/VTerminal/printer/EllipsePrinter.java b/src/com/valkryst/VTerminal/printer/EllipsePrinter.java index ad220a62a1..a6e29c0d79 100644 --- a/src/com/valkryst/VTerminal/printer/EllipsePrinter.java +++ b/src/com/valkryst/VTerminal/printer/EllipsePrinter.java @@ -5,15 +5,14 @@ import com.valkryst.VTerminal.misc.ShapeAlgorithms; import lombok.*; +import java.awt.Dimension; import java.awt.Point; @EqualsAndHashCode @ToString public class EllipsePrinter { - /** The width of the ellipse to print. */ - @Getter private int width = 2; - /** The height of the ellipse to print. */ - @Getter private int height = 2; + /** The width/height of the ellipse to print. */ + @Getter private final Dimension dimensions = new Dimension(2, 2); /** The character to print the ellipse with. */ @Getter @Setter private char printChar = '█'; @@ -24,17 +23,14 @@ public class EllipsePrinter { * @param panel * The panel. * - * @param row - * The y-axis (row) coordinate of the top-left character. - * - * @param column - * The x-axis (column) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the panel is null. */ - public void print(final @NonNull Panel panel, final int row, final int column) { - print(panel.getScreen(), row, column); + public void print(final @NonNull Panel panel, final Point position) { + print(panel.getScreen(), position); } /** @@ -43,17 +39,14 @@ public void print(final @NonNull Panel panel, final int row, final int column) { * @param screen * The screen. * - * @param row - * The y-axis (row) coordinate of the top-left character. - * - * @param column - * The x-axis (column) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the screen is null. */ - public void print(final @NonNull Screen screen, final int row, final int column) { - for (final Point point : ShapeAlgorithms.getEllipse(column, row, width, height)) { + public void print(final @NonNull Screen screen, final Point position) { + for (final Point point : ShapeAlgorithms.getEllipse(position, dimensions)) { screen.write(printChar, point); } } @@ -64,17 +57,14 @@ public void print(final @NonNull Screen screen, final int row, final int column) * @param panel * The panel. * - * @param row - * The y-axis (row) coordinate of the top-left character. - * - * @param column - * The x-axis (column) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the panel is null. */ - public void printFilled(final @NonNull Panel panel, final int row, final int column) { - printFilled(panel.getScreen(), row, column); + public void printFilled(final @NonNull Panel panel, final Point position) { + printFilled(panel.getScreen(), position); } /** @@ -83,17 +73,14 @@ public void printFilled(final @NonNull Panel panel, final int row, final int col * @param screen * The screen. * - * @param row - * The y-axis (row) coordinate of the top-left character. - * - * @param column - * The x-axis (column) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the screen is null. */ - public void printFilled(final @NonNull Screen screen, final int row, final int column) { - for (final Point point : ShapeAlgorithms.getFilledEllipse(column, row, width, height)) { + public void printFilled(final @NonNull Screen screen, final Point position) { + for (final Point point : ShapeAlgorithms.getFilledEllipse(position, dimensions)) { screen.write(printChar, point); } } @@ -109,7 +96,7 @@ public void printFilled(final @NonNull Screen screen, final int row, final int c */ public EllipsePrinter setWidth(final int width) { if (width > 0) { - this.width = width; + dimensions.setSize(width, dimensions.height); } return this; @@ -126,7 +113,7 @@ public EllipsePrinter setWidth(final int width) { */ public EllipsePrinter setHeight(final int height) { if (height > 0) { - this.height = height; + dimensions.setSize(dimensions.width, height); } return this; diff --git a/src/com/valkryst/VTerminal/printer/RectanglePrinter.java b/src/com/valkryst/VTerminal/printer/RectanglePrinter.java index 223501d894..06510b75be 100644 --- a/src/com/valkryst/VTerminal/printer/RectanglePrinter.java +++ b/src/com/valkryst/VTerminal/printer/RectanglePrinter.java @@ -7,16 +7,15 @@ import lombok.*; import org.json.simple.JSONObject; +import java.awt.Dimension; import java.awt.Point; import java.util.Optional; @EqualsAndHashCode @ToString public class RectanglePrinter { - /** The width of the rectangle to print. */ - @Getter private int width = 2; - /** The height of the rectangle to print. */ - @Getter private int height = 2; + /** The width/height of the rectangle to print. */ + @Getter private final Dimension dimensions = new Dimension(2, 2); /** The character to fill the rectangle with, if a filled rectangle is drawn. */ @Getter @Setter private char fillChar = '█'; @@ -33,17 +32,14 @@ public class RectanglePrinter { * @param panel * The panel. * - * @param column - * The x-axis (column) coordinate of the top-left character. - * - * @param row - * The y-axis (row) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the panel is null. */ - public void print(final @NonNull Panel panel, final int column, final int row) { - print(panel.getScreen(), column, row); + public void print(final @NonNull Panel panel, final Point position) { + print(panel.getScreen(), position); } /** @@ -55,29 +51,30 @@ public void print(final @NonNull Panel panel, final int column, final int row) { * @param screen * The screen. * - * @param column - * The x-axis (column) coordinate of the top-left character. - * - * @param row - * The y-axis (row) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the screen is null. */ - public void print(final @NonNull Screen screen, final int column, final int row) { - final int lastRow = row + height - 1; - final int lastColumn = column + width - 1; + public void print(final @NonNull Screen screen, final Point position) { + final int width = dimensions.width; + final int height = dimensions.height; + final int x = position.x; + final int y = position.y; + + final int lastRow = y + height - 1; + final int lastColumn = x + width - 1; final Point writePosition = new Point(0, 0); // Draw Corners: - writePosition.setLocation(column, row); - screen.write(rectangleType.getTopLeft(), writePosition); + screen.write(rectangleType.getTopLeft(), position); - writePosition.setLocation(lastColumn, row); + writePosition.setLocation(lastColumn, y); screen.write(rectangleType.getTopRight(), writePosition); - writePosition.setLocation(column, lastRow); + writePosition.setLocation(x, lastRow); screen.write(rectangleType.getBottomLeft(), writePosition); writePosition.setLocation(lastColumn, lastRow); @@ -85,19 +82,19 @@ public void print(final @NonNull Screen screen, final int column, final int row) // Draw Left/Right Sides: for (int i = 1 ; i < height - 1 ; i++) { - writePosition.setLocation(column, row + i); + writePosition.setLocation(x, y + i); screen.write(rectangleType.getVertical(), writePosition); - writePosition.setLocation(lastColumn, row + i); + writePosition.setLocation(lastColumn, y + i); screen.write(rectangleType.getVertical(), writePosition); } // Draw Top/Bottom Sides: for (int i = 1 ; i < width - 1 ; i++) { - writePosition.setLocation(column + i, row); + writePosition.setLocation(x + i, y); screen.write(rectangleType.getHorizontal(), writePosition); - writePosition.setLocation(column + i, lastRow); + writePosition.setLocation(x + i, lastRow); screen.write(rectangleType.getHorizontal(), writePosition); } @@ -107,20 +104,20 @@ public void print(final @NonNull Screen screen, final int column, final int row) final char[] titleChars = title.toCharArray(); for (int i = 2; i < width - 2 && i - 2 < titleChars.length; i++) { - writePosition.setLocation(column + i, row); + writePosition.setLocation(x + i, y); screen.write(titleChars[i - 2], writePosition); } // Draw Title Borders: - writePosition.setLocation(column + 1, row); + writePosition.setLocation(x + 1, y); screen.write(rectangleType.getConnectorLeft(), writePosition); - writePosition.setLocation(column + titleChars.length + 2, row); + writePosition.setLocation(x + titleChars.length + 2, y); screen.write(rectangleType.getConnectorRight(), writePosition); } // Handle Connectors: - setConnectors(screen, column, row); + setConnectors(screen, position); } /** @@ -129,17 +126,14 @@ public void print(final @NonNull Screen screen, final int column, final int row) * @param panel * The panel. * - * @param column - * The x-axis (column) coordinate of the top-left character. - * - * @param row - * The y-axis (row) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the panel is null. */ - public void printFilled(final @NonNull Panel panel, final int column, final int row) { - printFilled(panel.getScreen(), column, row); + public void printFilled(final @NonNull Panel panel, final Point position) { + printFilled(panel.getScreen(), position); } /** @@ -151,19 +145,19 @@ public void printFilled(final @NonNull Panel panel, final int column, final int * @param screen * The screen. * - * @param column - * The x-axis (column) coordinate of the top-left character. - * - * @param row - * The y-axis (row) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the screen is null. */ - public void printFilled(final @NonNull Screen screen, final int column, final int row) { - print(screen, column, row); + public void printFilled(final @NonNull Screen screen, final Point position) { + print(screen, position); + + final Dimension dimension = new Dimension(this.dimensions.width - 2, this.dimensions.height - 2); + position.setLocation(position.x + 1, position.y + 1); - for (final Point point : ShapeAlgorithms.getFilledRectangle(column + 1, row + 1, width - 2, height - 2)) { + for (final Point point : ShapeAlgorithms.getFilledRectangle(position, dimension)) { screen.write(fillChar, point); } } @@ -200,7 +194,7 @@ public void printFromJSON(final @NonNull Screen screen, final JSONObject jsonObj this.rectangleType = RectangleType.valueOf(rectangleType.toUpperCase()); } - print(screen, column, row); + print(screen, new Point(column, row)); } /** @@ -210,18 +204,15 @@ public void printFromJSON(final @NonNull Screen screen, final JSONObject jsonObj * @param screen * The screen. * - * @param column - * The x-axis (column) coordinate of the top-left character. - * - * @param row - * The y-axis (row) coordinate of the top-left character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the screen is null. */ - private void setConnectors(final @NonNull Screen screen, final int column, final int row) { - for (final Point point : ShapeAlgorithms.getRectangle(column, row, width, height)) { - setConnector(screen, point.x, point.y); + private void setConnectors(final @NonNull Screen screen, final Point position) { + for (final Point point : ShapeAlgorithms.getRectangle(position, dimensions)) { + setConnector(screen, point); } } @@ -232,25 +223,22 @@ private void setConnectors(final @NonNull Screen screen, final int column, final * @param screen * The screen. * - * @param column - * The x-axis (column) coordinate of the character. - * - * @param row - * The y-axis (row) coordinate of the character. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @throws NullPointerException * If the screen is null. */ - private void setConnector(final @NonNull Screen screen, final int column, final int row) { - final boolean validTop = hasValidTopNeighbour(screen, column, row); - final boolean validBottom = hasValidBottomNeighbour(screen, column, row); - final boolean validLeft = hasValidLeftNeighbour(screen, column, row); - final boolean validRight = hasValidRightNeighbour(screen, column, row); + private void setConnector(final @NonNull Screen screen, final Point position) { + final boolean validTop = hasValidTopNeighbour(screen, new Point(position)); + final boolean validBottom = hasValidBottomNeighbour(screen, new Point(position)); + final boolean validLeft = hasValidLeftNeighbour(screen, new Point(position)); + final boolean validRight = hasValidRightNeighbour(screen, new Point(position)); final boolean[] neighbourPattern = new boolean[]{validRight, validTop, validLeft, validBottom}; final Optional optChar = rectangleType.getCharacterByNeighbourPattern(neighbourPattern); - optChar.ifPresent(character -> screen.write(character, new Point(column, row))); + optChar.ifPresent(character -> screen.write(character, position)); } /** @@ -258,11 +246,8 @@ private void setConnector(final @NonNull Screen screen, final int column, final * RectangleType and that the character of the top-neighbour can be * connected to. * - * @param column - * The x-axis (column) coordinate of the cell. - * - * @param row - * The y-axis (row) coordinate of the cell. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @return * If the top-neighbour is valid. @@ -270,9 +255,10 @@ private void setConnector(final @NonNull Screen screen, final int column, final * @throws NullPointerException * If the screen is null. */ - private boolean hasValidTopNeighbour(final @NonNull Screen screen, final int column, final int row) { + private boolean hasValidTopNeighbour(final @NonNull Screen screen, final Point position) { try { - return rectangleType.isValidTopCharacter(screen.getCharacterAt(new Point(column, row - 1))); + position.setLocation(position.x, position.y - 1); + return rectangleType.isValidTopCharacter(screen.getCharacterAt(position)); } catch (final IllegalArgumentException e) { return false; } @@ -283,11 +269,8 @@ private boolean hasValidTopNeighbour(final @NonNull Screen screen, final int col * RectangleType and that the character of the bottom-neighbour can be * connected to. * - * @param column - * The x-axis (column) coordinate of the cell. - * - * @param row - * The y-axis (row) coordinate of the cell. + * @param position + * The x/y-axis (column/row) coordinates of the top-left character. * * @return * If the bottom-neighbour is valid. @@ -295,9 +278,10 @@ private boolean hasValidTopNeighbour(final @NonNull Screen screen, final int col * @throws NullPointerException * If the screen is null. */ - private boolean hasValidBottomNeighbour(final @NonNull Screen screen, final int column, final int row) { + private boolean hasValidBottomNeighbour(final @NonNull Screen screen, final Point position) { try { - return rectangleType.isValidBottomCharacter(screen.getCharacterAt(new Point(column, row + 1))); + position.setLocation(position.x, position.y + 1); + return rectangleType.isValidBottomCharacter(screen.getCharacterAt(position)); } catch (final IllegalArgumentException e) { return false; } @@ -308,11 +292,8 @@ private boolean hasValidBottomNeighbour(final @NonNull Screen screen, final int * RectangleType and that the character of the left-neighbour can be * connected to. * - * @param column - * The x-axis (column) coordinate of the cell. - * - * @param row - * The y-axis (row) coordinate of the cell. + * @param position + * The x/y-axis (column/row) coordinates of the cell. * * @return * If the left-neighbour is valid. @@ -320,9 +301,10 @@ private boolean hasValidBottomNeighbour(final @NonNull Screen screen, final int * @throws NullPointerException * If the screen is null. */ - private boolean hasValidLeftNeighbour(final @NonNull Screen screen, final int column, final int row) { + private boolean hasValidLeftNeighbour(final @NonNull Screen screen, final Point position) { try { - return rectangleType.isValidLeftCharacter(screen.getCharacterAt(new Point(column - 1, row))); + position.setLocation(position.x - 1, position.y); + return rectangleType.isValidLeftCharacter(screen.getCharacterAt(position)); } catch (final IllegalArgumentException e) { return false; } @@ -333,11 +315,8 @@ private boolean hasValidLeftNeighbour(final @NonNull Screen screen, final int co * RectangleType and that the character of the right-neighbour can be * connected to. * - * @param column - * The x-axis (column) coordinate of the cell. - * - * @param row - * The y-axis (row) coordinate of the cell. + * @param position + * The x/y-axis (column/row) coordinates of the cell. * * @return * If the right-neighbour is valid. @@ -345,9 +324,10 @@ private boolean hasValidLeftNeighbour(final @NonNull Screen screen, final int co * @throws NullPointerException * If the screen is null. */ - private boolean hasValidRightNeighbour(final @NonNull Screen screen, final int column, final int row) { + private boolean hasValidRightNeighbour(final @NonNull Screen screen, final Point position) { try { - return rectangleType.isValidRightCharacter(screen.getCharacterAt(new Point(column + 1, row))); + position.setLocation(position.x + 1, position.y); + return rectangleType.isValidRightCharacter(screen.getCharacterAt(position)); } catch (final IllegalArgumentException e) { return false; } @@ -364,7 +344,7 @@ private boolean hasValidRightNeighbour(final @NonNull Screen screen, final int c */ public RectanglePrinter setWidth(final int width) { if (width > 0) { - this.width = width; + dimensions.setSize(width, dimensions.height); } return this; @@ -381,7 +361,7 @@ public RectanglePrinter setWidth(final int width) { */ public RectanglePrinter setHeight(final int height) { if (height > 0) { - this.height = height; + dimensions.setSize(dimensions.width, height); } return this; diff --git a/test/com/valkryst/VTerminal/samples/component/SampleComponents.java b/test/com/valkryst/VTerminal/samples/component/SampleComponents.java index d0fb39e8ee..22cf728c69 100644 --- a/test/com/valkryst/VTerminal/samples/component/SampleComponents.java +++ b/test/com/valkryst/VTerminal/samples/component/SampleComponents.java @@ -15,6 +15,7 @@ import javax.swing.Timer; import java.awt.Color; +import java.awt.Point; import java.io.IOException; import java.net.URISyntaxException; @@ -36,13 +37,13 @@ public static void main(final String[] args) throws IOException, URISyntaxExcept printer.setRectangleType(RectangleType.HEAVY); printer.setWidth(80); printer.setHeight(24); - printer.print(panel, 0, 0); + printer.print(panel, new Point(0, 0)); printer.setWidth(24); - printer.print(panel, 0, 0); + printer.print(panel, new Point(0, 0)); printer.setWidth(48); - printer.print(panel, 0, 0); + printer.print(panel, new Point(0, 0)); // Title #1 final LabelBuilder labelBuilder = new LabelBuilder(); diff --git a/test/com/valkryst/VTerminal/samples/printer/SampleEllipsePrinter.java b/test/com/valkryst/VTerminal/samples/printer/SampleEllipsePrinter.java index 36dccf3b6b..e80294fbfd 100644 --- a/test/com/valkryst/VTerminal/samples/printer/SampleEllipsePrinter.java +++ b/test/com/valkryst/VTerminal/samples/printer/SampleEllipsePrinter.java @@ -6,6 +6,7 @@ import com.valkryst.VTerminal.font.FontLoader; import com.valkryst.VTerminal.printer.EllipsePrinter; +import java.awt.Point; import java.io.IOException; import java.net.URISyntaxException; @@ -23,9 +24,9 @@ public static void main(final String[] args) throws IOException, URISyntaxExcept final EllipsePrinter printer = new EllipsePrinter(); printer.setWidth(6); printer.setHeight(8); - printer.print(panel, 10, 10); + printer.print(panel, new Point(10, 10)); - printer.printFilled(panel, 10, 40); + printer.printFilled(panel, new Point(10, 40)); panel.draw(); } diff --git a/test/com/valkryst/VTerminal/samples/printer/SampleLinePrinter.java b/test/com/valkryst/VTerminal/samples/printer/SampleLinePrinter.java index 519d887994..063e24c895 100644 --- a/test/com/valkryst/VTerminal/samples/printer/SampleLinePrinter.java +++ b/test/com/valkryst/VTerminal/samples/printer/SampleLinePrinter.java @@ -7,6 +7,7 @@ import com.valkryst.VTerminal.misc.ShapeAlgorithms; import com.valkryst.VTerminal.printer.LinePrinter; +import java.awt.Dimension; import java.awt.Point; import java.io.IOException; import java.net.URISyntaxException; @@ -24,7 +25,7 @@ public static void main(final String[] args) throws IOException, URISyntaxExcept Thread.sleep(100); - final List circlePoints = ShapeAlgorithms.getEllipse(10, 10, 5, 5); + final List circlePoints = ShapeAlgorithms.getEllipse(new Point(10, 10), new Dimension(5, 5)); final LinePrinter printer = new LinePrinter(); for (final Point point : circlePoints) { diff --git a/test/com/valkryst/VTerminal/samples/printer/SampleRectanglePrinter.java b/test/com/valkryst/VTerminal/samples/printer/SampleRectanglePrinter.java index 848d93aec7..1e4c205d2f 100644 --- a/test/com/valkryst/VTerminal/samples/printer/SampleRectanglePrinter.java +++ b/test/com/valkryst/VTerminal/samples/printer/SampleRectanglePrinter.java @@ -7,6 +7,7 @@ import com.valkryst.VTerminal.printer.RectanglePrinter; import com.valkryst.VTerminal.printer.RectangleType; +import java.awt.Point; import java.io.IOException; import java.net.URISyntaxException; @@ -27,31 +28,31 @@ public static void main(final String[] args) throws IOException, URISyntaxExcept printer.setTitle("Type: Simple"); printer.setWidth(48); printer.setHeight(24); - printer.print(panel, 0, 0); + printer.print(panel, new Point(0, 0)); printer.setRectangleType(RectangleType.THIN); printer.setTitle("Type: Thin"); printer.setWidth(42); printer.setHeight(20); - printer.print(panel, 2, 2); + printer.print(panel, new Point(2, 2)); printer.setRectangleType(RectangleType.HEAVY); printer.setTitle("Type: Heavy"); printer.setWidth(38); printer.setHeight(16); - printer.print(panel, 4, 4); + printer.print(panel, new Point(4, 4)); printer.setRectangleType(RectangleType.MIXED_HEAVY_HORIZONTAL); printer.setTitle("Type: Mixed Heavy Horizontal"); printer.setWidth(34); printer.setHeight(12); - printer.print(panel, 6, 6); + printer.print(panel, new Point(6, 6)); printer.setRectangleType(RectangleType.MIXED_HEAVY_VERTICAL); printer.setTitle("Type: Mixed Heavy Vertical"); printer.setWidth(30); printer.setHeight(8); - printer.print(panel, 8, 8); + printer.print(panel, new Point(8, 8)); // Right Rect @@ -59,34 +60,34 @@ public static void main(final String[] args) throws IOException, URISyntaxExcept printer.setTitle(""); printer.setWidth(31); printer.setHeight(24); - printer.print(panel, 49, 0); + printer.print(panel, new Point(49, 0)); // Creates top box in right rect printer.setHeight(6); - printer.print(panel, 49, 0); + printer.print(panel, new Point(49, 0)); // Creates middle box in right rect printer.setHeight(12); - printer.print(panel, 49, 0); + printer.print(panel, new Point(49, 0)); // Creates bottom box in right rect printer.setHeight(18); - printer.print(panel, 49, 0); + printer.print(panel, new Point(49, 0)); // Creates left box in right rect printer.setWidth(6); printer.setHeight(24); - printer.print(panel, 49, 0); + printer.print(panel, new Point(49, 0)); // Creates halves top left box in right rect printer.setWidth(3); printer.setHeight(6); - printer.print(panel, 49, 0); + printer.print(panel, new Point(49, 0)); // Creates box spanning the right boxes in the right rect printer.setWidth(8); printer.setHeight(18); - printer.print(panel, 59, 3); + printer.print(panel, new Point(59, 3)); panel.draw(); }