From ea3b630eb9b5cc6d81c0363bb95f7e22cad994a4 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:07:53 +0300 Subject: [PATCH 01/15] unsaved from previous --- src/test/java/commonGraphics/ColorUtils.java | 1 + .../commonGraphics/panels/MinimalPanel.java | 12 +++-- .../sidePanels/AbstractSectionPanel.java | 3 +- .../commonLeftSidePanel/CommonTopSection.java | 3 +- .../java/playerTest/graphics/TestWindow.java | 48 +++++++++++++++-- .../panels/centralPanel/BottomSection.java | 48 +++++++++++++++++ .../panels/centralPanel/CentralPanel.java | 54 +++++++++++++++++++ .../MainPanel.java} | 18 ++++--- .../panels/leftSidePanel/LeftSidePanel.java | 7 ++- .../panels/rightSidePanel/BottomSection.java | 3 +- .../panels/rightSidePanel/RightSidePanel.java | 3 ++ .../playerTest/models/engines/Vehicle.java | 32 +++++++++++ 12 files changed, 209 insertions(+), 23 deletions(-) create mode 100644 src/test/java/playerTest/graphics/panels/centralPanel/BottomSection.java create mode 100644 src/test/java/playerTest/graphics/panels/centralPanel/CentralPanel.java rename src/test/java/playerTest/graphics/panels/{CentralPanel.java => centralPanel/MainPanel.java} (57%) create mode 100644 src/test/java/playerTest/models/engines/Vehicle.java diff --git a/src/test/java/commonGraphics/ColorUtils.java b/src/test/java/commonGraphics/ColorUtils.java index be14ba2..9505439 100644 --- a/src/test/java/commonGraphics/ColorUtils.java +++ b/src/test/java/commonGraphics/ColorUtils.java @@ -6,6 +6,7 @@ //Color utilities. public final class ColorUtils { + @SuppressWarnings("unused") public static final @NotNull Color TRANSPARENT_BLACK = new Color(0, 0, 0, 0); //gets a gray color from brightness and alpha diff --git a/src/test/java/commonGraphics/panels/MinimalPanel.java b/src/test/java/commonGraphics/panels/MinimalPanel.java index 43fd783..3212ba1 100644 --- a/src/test/java/commonGraphics/panels/MinimalPanel.java +++ b/src/test/java/commonGraphics/panels/MinimalPanel.java @@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import static commonGraphics.ColorUtils.TRANSPARENT_BLACK; +import static consoleUtils.SimplePrinting.printLine; //a minimal panel with most common features public abstract class MinimalPanel extends JPanel { @@ -22,7 +22,7 @@ public MinimalPanel(@Nullable Color background, @Nullable Color borderColor, boolean drawBorders, @Nullable Color diagonalColor, boolean drawDiagonals) { super(); - setBackground(Objects.requireNonNullElse(background, TRANSPARENT_BLACK)); + setBackground(background); this.borderColor = Objects.requireNonNullElse(borderColor, DEFAULT_BORDERS_AND_DIAGONALS_COLOR); this.drawBorders = drawBorders; this.diagonalColor = Objects.requireNonNullElse(diagonalColor, DEFAULT_BORDERS_AND_DIAGONALS_COLOR); @@ -44,8 +44,8 @@ public final void setDrawDiagonals(boolean drawDiagonals) { // @Override - protected void paintComponent(@NotNull Graphics g) { - super.paintComponent(g); + public void paint(@NotNull Graphics g) { + super.paint(g); mainPaint(g); finalPaint(g); } @@ -73,4 +73,8 @@ private void drawPanelDiagonals(@NotNull Graphics g, int width, int height) { g.drawLine(0, 0, width, height); g.drawLine(0, height, width, 0); } + + public void printSizeToConsole(@NotNull String message) { + printLine(message + " size: " + getWidth() + " x " + getHeight()); + } } \ No newline at end of file diff --git a/src/test/java/commonGraphics/panels/sidePanels/AbstractSectionPanel.java b/src/test/java/commonGraphics/panels/sidePanels/AbstractSectionPanel.java index ef0b268..e6ba590 100644 --- a/src/test/java/commonGraphics/panels/sidePanels/AbstractSectionPanel.java +++ b/src/test/java/commonGraphics/panels/sidePanels/AbstractSectionPanel.java @@ -4,7 +4,6 @@ import org.jetbrains.annotations.Nullable; -import static commonGraphics.ColorUtils.TRANSPARENT_BLACK; import commonGraphics.panels.FixedHorizontalPanel; //a horizontal section for a vertical side-panel @@ -14,7 +13,7 @@ public AbstractSectionPanel(int height, @Nullable Color borderColor, boolean drawBorders, @Nullable Color diagonalColor, boolean drawDiagonals) { super( - height, TRANSPARENT_BLACK, + height, null, borderColor, drawBorders, diagonalColor, drawDiagonals); } diff --git a/src/test/java/commonGraphics/panels/sidePanels/commonLeftSidePanel/CommonTopSection.java b/src/test/java/commonGraphics/panels/sidePanels/commonLeftSidePanel/CommonTopSection.java index 63d5897..ab30945 100644 --- a/src/test/java/commonGraphics/panels/sidePanels/commonLeftSidePanel/CommonTopSection.java +++ b/src/test/java/commonGraphics/panels/sidePanels/commonLeftSidePanel/CommonTopSection.java @@ -6,7 +6,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import static commonGraphics.ColorUtils.TRANSPARENT_BLACK; import commonGraphics.panels.MinimalPanel; import commonGraphics.panels.sidePanels.SectionContainerInterface; @@ -17,7 +16,7 @@ public abstract class CommonTopSection extends MinimalPanel implements SectionCo // public CommonTopSection(@Nullable Color borderColor, @Nullable Color diagonalColor) { super( - TRANSPARENT_BLACK, + null, borderColor, false, diagonalColor, false); this.borderColor = borderColor; diff --git a/src/test/java/playerTest/graphics/TestWindow.java b/src/test/java/playerTest/graphics/TestWindow.java index a65fd63..47fc566 100644 --- a/src/test/java/playerTest/graphics/TestWindow.java +++ b/src/test/java/playerTest/graphics/TestWindow.java @@ -1,15 +1,21 @@ package playerTest.graphics; import java.awt.Color; +import java.awt.Graphics; import java.awt.LayoutManager; +import java.awt.BorderLayout; import javax.swing.BoxLayout; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import static commonGraphics.ColorUtils.getGray; +import static consoleUtils.SimplePrinting.printLine; + +import commonGraphics.panels.MinimalPanel; import commonGraphics.UpdatingWindow; import playerTest.graphics.panels.leftSidePanel.LeftSidePanel; -import playerTest.graphics.panels.CentralPanel; +import playerTest.graphics.panels.centralPanel.CentralPanel; import playerTest.graphics.panels.rightSidePanel.RightSidePanel; // @@ -33,9 +39,41 @@ public TestWindow() { public void addPanels() { LayoutManager layout = new BoxLayout(getContentPane(), BoxLayout.X_AXIS); getContentPane().setLayout(layout); - add(new LeftSidePanel(this, MAIN_PANEL_BACKGROUND_COLOR, MAIN_PANEL_BORDER_COLOR)); - add(new CentralPanel(MAIN_PANEL_BORDER_COLOR, MAIN_PANEL_BORDER_COLOR)); - add(new RightSidePanel(MAIN_PANEL_BACKGROUND_COLOR, MAIN_PANEL_BORDER_COLOR)); - // Add more panels here, if needed. + @NotNull LeftSidePanel leftSidePanel = new LeftSidePanel( + this, + MAIN_PANEL_BACKGROUND_COLOR, MAIN_PANEL_BORDER_COLOR); + add(leftSidePanel); + @NotNull CentralAndRightPanel centralAndRightPanel = new CentralAndRightPanel( + MAIN_PANEL_BACKGROUND_COLOR, MAIN_PANEL_BORDER_COLOR); + add(centralAndRightPanel); + + //debugging: + revalidate(); + leftSidePanel.printSizeToConsole("Left side panel created,"); + centralAndRightPanel.printSizeToConsole("CentralAndRightPanel created,"); + } + + // + private static final class CentralAndRightPanel extends MinimalPanel { + // + CentralAndRightPanel(@Nullable Color mainPanelBackgroundColor, @Nullable Color mainPanelBorderColor) { + super(null, null, false, null, false); + printLine("Creating CentralAndRightPanel"); + LayoutManager layout = new BorderLayout(); + setLayout(layout); + @NotNull RightSidePanel rightSidePanel = new RightSidePanel(mainPanelBackgroundColor, mainPanelBorderColor); + //add(rightSidePanel, BorderLayout.EAST); + @NotNull CentralPanel centralPanel = new CentralPanel(mainPanelBackgroundColor, mainPanelBorderColor); + add(centralPanel); + revalidate(); + + //debugging: + rightSidePanel.printSizeToConsole("Right side panel created,"); + centralPanel.printSizeToConsole("Central panel created,"); + } + + // + @Override + public void mainPaint(@NotNull Graphics g) {} } } \ No newline at end of file diff --git a/src/test/java/playerTest/graphics/panels/centralPanel/BottomSection.java b/src/test/java/playerTest/graphics/panels/centralPanel/BottomSection.java new file mode 100644 index 0000000..043e8fb --- /dev/null +++ b/src/test/java/playerTest/graphics/panels/centralPanel/BottomSection.java @@ -0,0 +1,48 @@ +package playerTest.graphics.panels.centralPanel; + +import java.awt.Color; +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static commonGraphics.ColorUtils.getGray; +import static commonGraphics.StringUtils.drawNumberedString; +import static consoleUtils.SimplePrinting.printLine; + +import commonGraphics.panels.sidePanels.AbstractSectionPanel; + +// +final class BottomSection extends AbstractSectionPanel { + private static final int PANEL_HEIGHT = 300; + private static final int @NotNull [] TEXT_LOCATION = new int [] {10, 10}; + private static final @NotNull Color + HEADING_COLOR = Color.white, + TEXT_COLOR = getGray(170, 255); + + // + BottomSection(@Nullable Color borderColor, @Nullable Color diagonalColor) { + super( + PANEL_HEIGHT, + borderColor, true, + diagonalColor, true); + printLine("Creating central panel bottom section"); + printSizeToConsole(""); + } + + // + @Override + public void mainPaint(@NotNull Graphics g) { + g.setColor(HEADING_COLOR); + drawInfoLine(g, "Bottom section", 1); + //TODO: improve this + g.setColor(TEXT_COLOR); + drawInfoLine(g, "Coming soon...", 2); + // Paint more stuff here, if needed. + } + + @SuppressWarnings("SameParameterValue") + private void drawInfoLine(@NotNull Graphics g, @Nullable String line, int lineNumber) { + drawNumberedString(g, line, TEXT_LOCATION, lineNumber); + } +} \ No newline at end of file diff --git a/src/test/java/playerTest/graphics/panels/centralPanel/CentralPanel.java b/src/test/java/playerTest/graphics/panels/centralPanel/CentralPanel.java new file mode 100644 index 0000000..8a9409e --- /dev/null +++ b/src/test/java/playerTest/graphics/panels/centralPanel/CentralPanel.java @@ -0,0 +1,54 @@ +package playerTest.graphics.panels.centralPanel; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.LayoutManager; +import java.awt.BorderLayout; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static consoleUtils.SimplePrinting.printLine; + +import commonGraphics.panels.MinimalPanel; +import commonGraphics.panels.sidePanels.SectionContainerInterface; + +// +public final class CentralPanel extends MinimalPanel implements SectionContainerInterface { + private final @Nullable Color sectionBorderColor, sectionDiagonalColor; + + // + public CentralPanel(@Nullable Color background, @Nullable Color borderColor) { + super( + background, + borderColor, true, + borderColor, true); + printLine("Creating central panel"); + this.sectionBorderColor = borderColor; + this.sectionDiagonalColor = borderColor; + addSections(); + } + + @Override + public void addSections(){ + LayoutManager layout = new BorderLayout(); + setLayout(layout); + //TODO: fix this + @NotNull BottomSection bottomSection = new BottomSection(sectionBorderColor, sectionDiagonalColor); + add(bottomSection, BorderLayout.SOUTH); + @NotNull MainPanel mainPanel = new MainPanel(sectionBorderColor, sectionDiagonalColor); + add(mainPanel); + //validate(); + revalidate(); + + //debugging: + //bottomSection.printSizeToConsole("Central bottom section created,"); + mainPanel.printSizeToConsole("Main panel created,"); + } + + // + @Override + public void mainPaint(@NotNull Graphics g) { + printLine("asd 1"); + } +} \ No newline at end of file diff --git a/src/test/java/playerTest/graphics/panels/CentralPanel.java b/src/test/java/playerTest/graphics/panels/centralPanel/MainPanel.java similarity index 57% rename from src/test/java/playerTest/graphics/panels/CentralPanel.java rename to src/test/java/playerTest/graphics/panels/centralPanel/MainPanel.java index e5bfdf6..aa3c000 100644 --- a/src/test/java/playerTest/graphics/panels/CentralPanel.java +++ b/src/test/java/playerTest/graphics/panels/centralPanel/MainPanel.java @@ -1,4 +1,4 @@ -package playerTest.graphics.panels; +package playerTest.graphics.panels.centralPanel; import java.awt.Color; import java.awt.Graphics; @@ -9,22 +9,27 @@ import commonGraphics.panels.graphicalPanels.CenteredDrawPanel; import commonGraphics.panels.graphicalPanels.ScaledDrawInterface; +import static consoleUtils.SimplePrinting.printLine; +import static consoleUtils.stringTools.NumberFormatter.doubleToString; + // -public final class CentralPanel extends CenteredDrawPanel implements ScaledDrawInterface { +public class MainPanel extends CenteredDrawPanel implements ScaledDrawInterface { private static final double DEFAULT_SCALE = 2.0; private static final @NotNull Color BACKGROUND = Color.black, - TEXT_COLOR = Color.white; + TEXT_COLOR = Color.red;//Color.white; @SuppressWarnings("FieldMayBeFinal") private double scale; // - public CentralPanel(@Nullable Color borderColor, @Nullable Color diagonalColor) { + public MainPanel(@Nullable Color borderColor, @Nullable Color diagonalColor) { super( - BACKGROUND, + null,//BACKGROUND, borderColor, false, - diagonalColor, true); + /*diagonalColor*/Color.red, true); + printLine("Creating main panel"); scale = DEFAULT_SCALE; + printSizeToConsole(""); } // @@ -38,6 +43,7 @@ public double getScale() { public void mainPaint(@NotNull Graphics g) { g.setColor(TEXT_COLOR); g.drawString("A player test", 100, 100); + g.drawString("Scale: " + doubleToString(scale, 3), 100, 120); // Paint more stuff here, if needed. } } \ No newline at end of file diff --git a/src/test/java/playerTest/graphics/panels/leftSidePanel/LeftSidePanel.java b/src/test/java/playerTest/graphics/panels/leftSidePanel/LeftSidePanel.java index 7d6e44d..2b0e63b 100644 --- a/src/test/java/playerTest/graphics/panels/leftSidePanel/LeftSidePanel.java +++ b/src/test/java/playerTest/graphics/panels/leftSidePanel/LeftSidePanel.java @@ -5,6 +5,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static consoleUtils.SimplePrinting.printLine; + import commonGraphics.UpdatingWindow; import commonGraphics.panels.sidePanels.commonLeftSidePanel.CommonLeftSidePanel; import commonGraphics.panels.sidePanels.commonLeftSidePanel.CommonTopSection; @@ -18,8 +20,9 @@ public LeftSidePanel(@NotNull UpdatingWindow window, @Nullable Color background, @Nullable Color borderColor) { super( window, background, - borderColor, true, true, - DIAGONAL_COLOR, false, false); + Color.red/*borderColor*/, true, true, + DIAGONAL_COLOR, true, true); + printLine("Creating left side panel"); addSections(); } diff --git a/src/test/java/playerTest/graphics/panels/rightSidePanel/BottomSection.java b/src/test/java/playerTest/graphics/panels/rightSidePanel/BottomSection.java index 1112d6e..7eb1ecc 100644 --- a/src/test/java/playerTest/graphics/panels/rightSidePanel/BottomSection.java +++ b/src/test/java/playerTest/graphics/panels/rightSidePanel/BottomSection.java @@ -8,7 +8,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import static commonGraphics.ColorUtils.TRANSPARENT_BLACK; import commonGraphics.panels.MinimalPanel; import commonGraphics.panels.sidePanels.SectionContainerInterface; @@ -23,7 +22,7 @@ final class BottomSection extends MinimalPanel implements SectionContainerInterf // BottomSection(@Nullable Color borderColor, boolean drawSectionBorders, @Nullable Color diagonalColor, boolean drawSectionDiagonals) { - super(TRANSPARENT_BLACK, borderColor, DRAW_PANEL_BORDERS, diagonalColor, DRAW_PANEL_DIAGONALS); + super(null, borderColor, DRAW_PANEL_BORDERS, diagonalColor, DRAW_PANEL_DIAGONALS); sectionBorderColor = borderColor; this.drawSectionBorders = drawSectionBorders; sectionDiagonalColor = diagonalColor; diff --git a/src/test/java/playerTest/graphics/panels/rightSidePanel/RightSidePanel.java b/src/test/java/playerTest/graphics/panels/rightSidePanel/RightSidePanel.java index 0496b33..30bb4a9 100644 --- a/src/test/java/playerTest/graphics/panels/rightSidePanel/RightSidePanel.java +++ b/src/test/java/playerTest/graphics/panels/rightSidePanel/RightSidePanel.java @@ -6,6 +6,8 @@ import org.jetbrains.annotations.Nullable; +import static consoleUtils.SimplePrinting.printLine; + import commonGraphics.panels.sidePanels.AbstractSidePanel; // @@ -22,6 +24,7 @@ public RightSidePanel(@Nullable Color background, @Nullable Color borderColor) { PANEL_WIDTH, background, borderColor, true, null, false); + printLine("Creating right side panel"); this.sectionBorderColor = borderColor; this.sectionDiagonalColor = borderColor; addSections(); diff --git a/src/test/java/playerTest/models/engines/Vehicle.java b/src/test/java/playerTest/models/engines/Vehicle.java new file mode 100644 index 0000000..49a3deb --- /dev/null +++ b/src/test/java/playerTest/models/engines/Vehicle.java @@ -0,0 +1,32 @@ +package playerTest.models.engines; + +import java.util.List; + +//initially a ship? +public class Vehicle { + VehicleInformation information; + VehicleExterior exterior; + VehicleInterior interior; + + Vehicle() { + // + } +} + +class VehicleInformation { + String name; + //owner, affiliation, price, history, etc +} + +class VehicleExterior { + // + void getSize() {} +} + +class VehicleInterior { + List rooms; +} + +class VehicleRoom { + // +} \ No newline at end of file From c83c28714a48cddff4dc132c73a0fa448a7ed0a4 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:19:31 +0300 Subject: [PATCH 02/15] test 3 stub --- src/test/java/test3/Test3.java | 17 ++++++++++ src/test/java/test3/graphics/Panel.java | 32 +++++++++++++++++++ .../test3/graphics/TestWindowSettings.java | 20 ++++++++++++ src/test/java/test3/graphics/Window.java | 29 +++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 src/test/java/test3/Test3.java create mode 100644 src/test/java/test3/graphics/Panel.java create mode 100644 src/test/java/test3/graphics/TestWindowSettings.java create mode 100644 src/test/java/test3/graphics/Window.java diff --git a/src/test/java/test3/Test3.java b/src/test/java/test3/Test3.java new file mode 100644 index 0000000..8de110d --- /dev/null +++ b/src/test/java/test3/Test3.java @@ -0,0 +1,17 @@ +package test3; + +import graphicalTestAbstraction.GraphicalTest; +import test3.graphics.Window; + +// +public class Test3 extends GraphicalTest { + // + public static void main(String[] args) { + new Test3(); + } + + // + private Test3() { + super(new Window()); + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/Panel.java b/src/test/java/test3/graphics/Panel.java new file mode 100644 index 0000000..d3f5290 --- /dev/null +++ b/src/test/java/test3/graphics/Panel.java @@ -0,0 +1,32 @@ +package test3.graphics; + +import java.awt.Color; +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; + +import commonGraphics.panels.MinimalPanel; + +// +final class Panel extends MinimalPanel { + private static final @NotNull Color + BACKGROUND = Color.black, + BORDERS_AND_DIAGONALS = Color.orange, + TEXT_COLOR = Color.white; + + // + Panel() { + super( + BACKGROUND, + BORDERS_AND_DIAGONALS, true, + BORDERS_AND_DIAGONALS, true); + } + + // + @Override + public void mainPaint(@NotNull Graphics g) { + g.setColor(TEXT_COLOR); + g.drawString("A window for test 3", 50, 50); + // paint more stuff here, if needed + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/TestWindowSettings.java b/src/test/java/test3/graphics/TestWindowSettings.java new file mode 100644 index 0000000..dbd691c --- /dev/null +++ b/src/test/java/test3/graphics/TestWindowSettings.java @@ -0,0 +1,20 @@ +package test3.graphics; + +import java.awt.Dimension; +import java.awt.Point; + +import org.jetbrains.annotations.NotNull; + +import commonGraphics.WindowSettings; + +// +final class TestWindowSettings extends WindowSettings { + private static final @NotNull Dimension WINDOW_SIZE = new Dimension(400, 300); + private static final @NotNull Point WINDOW_LOCATION = new Point(50, 50); + private static final @NotNull String WINDOW_TITLE = "Test 2"; + + // + TestWindowSettings() { + super(WINDOW_SIZE, WINDOW_LOCATION, WINDOW_TITLE); + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/Window.java b/src/test/java/test3/graphics/Window.java new file mode 100644 index 0000000..accf4ba --- /dev/null +++ b/src/test/java/test3/graphics/Window.java @@ -0,0 +1,29 @@ +package test3.graphics; + +import java.awt.LayoutManager; +import javax.swing.BoxLayout; + +import commonGraphics.UpdatingWindow; + +// +public class Window extends UpdatingWindow { + + // + public Window() { + super(new TestWindowSettings()); //default frame rate + //add stuff before panels here + addPanels(); + //add key listener here + revalidate(); + startUpdating(); + } + + // + @Override + public void addPanels() { + LayoutManager layout = new BoxLayout(getContentPane(), BoxLayout.X_AXIS); + getContentPane().setLayout(layout); + add(new Panel()); + // Add more panels here, if needed. + } +} \ No newline at end of file From fce7f47293e48297172a4cb5c3d8132744af90de Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:21:07 +0300 Subject: [PATCH 03/15] minor fix --- src/test/java/test3/graphics/TestWindowSettings.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/test3/graphics/TestWindowSettings.java b/src/test/java/test3/graphics/TestWindowSettings.java index dbd691c..403acc6 100644 --- a/src/test/java/test3/graphics/TestWindowSettings.java +++ b/src/test/java/test3/graphics/TestWindowSettings.java @@ -9,9 +9,9 @@ // final class TestWindowSettings extends WindowSettings { - private static final @NotNull Dimension WINDOW_SIZE = new Dimension(400, 300); + private static final @NotNull Dimension WINDOW_SIZE = new Dimension(1100, 700); private static final @NotNull Point WINDOW_LOCATION = new Point(50, 50); - private static final @NotNull String WINDOW_TITLE = "Test 2"; + private static final @NotNull String WINDOW_TITLE = "Test 3"; // TestWindowSettings() { From 3995278e913b3b66db4d3ad8dae381749ca7ccb7 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 15:45:32 +0300 Subject: [PATCH 04/15] improvements --- src/test/java/test3/Test3.java | 11 +++- src/test/java/test3/graphics/BottomPanel.java | 59 +++++++++++++++++++ src/test/java/test3/graphics/MainPanel.java | 59 +++++++++++++++++++ src/test/java/test3/graphics/Panel.java | 32 ---------- .../java/test3/graphics/PanelPalette.java | 46 +++++++++++++++ src/test/java/test3/graphics/Window.java | 17 ++++-- src/test/java/test3/player/PaintMode.java | 8 +++ src/test/java/test3/player/Player.java | 23 ++++++++ 8 files changed, 215 insertions(+), 40 deletions(-) create mode 100644 src/test/java/test3/graphics/BottomPanel.java create mode 100644 src/test/java/test3/graphics/MainPanel.java delete mode 100644 src/test/java/test3/graphics/Panel.java create mode 100644 src/test/java/test3/graphics/PanelPalette.java create mode 100644 src/test/java/test3/player/PaintMode.java create mode 100644 src/test/java/test3/player/Player.java diff --git a/src/test/java/test3/Test3.java b/src/test/java/test3/Test3.java index 8de110d..fcb086e 100644 --- a/src/test/java/test3/Test3.java +++ b/src/test/java/test3/Test3.java @@ -1,17 +1,22 @@ package test3; +import org.jetbrains.annotations.NotNull; + import graphicalTestAbstraction.GraphicalTest; +import test3.player.Player; import test3.graphics.Window; // public class Test3 extends GraphicalTest { + private static final @NotNull Player PLAYER = new Player(); + // public static void main(String[] args) { - new Test3(); + new Test3(PLAYER); } // - private Test3() { - super(new Window()); + private Test3(@NotNull Player player) { + super(new Window(player)); } } \ No newline at end of file diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java new file mode 100644 index 0000000..246d805 --- /dev/null +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -0,0 +1,59 @@ +package test3.graphics; + +import java.awt.Color; +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; + +import commonGraphics.panels.FixedHorizontalPanel; +import test3.player.Player; + +// +public class BottomPanel extends FixedHorizontalPanel { + private static final int PANEL_HEIGHT = 200; + private static final @NotNull Color TEXT_COLOR = Color.white; + private final @NotNull Player player; + + // + public BottomPanel(@NotNull Player player, @NotNull PanelPalette palette) { + super(PANEL_HEIGHT, palette.getPanelBackground(), + palette.getPanelBorder(), true, + palette.getPanelDiagonals(), false); + this.player = player; + } + + // + @Override + public void mainPaint(@NotNull Graphics g) { + switch (player.getPaintMode()) { + case FLIGHT -> paintMode_flight(g); + case SHIPYARD -> paintMode_shipyard(g); + case MISSIONS -> paintMode_missions(g); + default -> paintMode_default(g); + } + } + + private void paintMode_default(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Painting for this PaintMode not defined.", 50, 50); + } + + private void paintMode_flight(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Flight mode not ready yet...", 50, 50); + } + + private void paintMode_shipyard(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Shipyard mode not ready yet...", 50, 50); + } + + private void paintMode_missions(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Missions mode not ready yet...", 50, 50); + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/MainPanel.java b/src/test/java/test3/graphics/MainPanel.java new file mode 100644 index 0000000..490e475 --- /dev/null +++ b/src/test/java/test3/graphics/MainPanel.java @@ -0,0 +1,59 @@ +package test3.graphics; + +import java.awt.Color; +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; + +import commonGraphics.panels.MinimalPanel; +import test3.player.Player; + +// +final class MainPanel extends MinimalPanel { + private static final @NotNull Color TEXT_COLOR = Color.white; + private final @NotNull Player player; + + // + MainPanel(@NotNull Player player, @NotNull PanelPalette palette) { + super( + palette.getMainBackground(), + palette.getPanelBorder(), true, + palette.getPanelDiagonals(), false); + this.player = player; + } + + // + @Override + public void mainPaint(@NotNull Graphics g) { + switch (player.getPaintMode()) { + case FLIGHT -> paintMode_flight(g); + case SHIPYARD -> paintMode_shipyard(g); + case MISSIONS -> paintMode_missions(g); + default -> paintMode_default(g); + } + } + + private void paintMode_default(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Painting for this PaintMode not defined.", 50, 50); + } + + private void paintMode_flight(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Flight mode not ready yet...", 50, 50); + } + + private void paintMode_shipyard(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Shipyard mode not ready yet...", 50, 50); + } + + private void paintMode_missions(@NotNull Graphics g) { + setDrawDiagonals(true); + g.setColor(TEXT_COLOR); + g.drawString("Missions mode not ready yet...", 50, 50); + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/Panel.java b/src/test/java/test3/graphics/Panel.java deleted file mode 100644 index d3f5290..0000000 --- a/src/test/java/test3/graphics/Panel.java +++ /dev/null @@ -1,32 +0,0 @@ -package test3.graphics; - -import java.awt.Color; -import java.awt.Graphics; - -import org.jetbrains.annotations.NotNull; - -import commonGraphics.panels.MinimalPanel; - -// -final class Panel extends MinimalPanel { - private static final @NotNull Color - BACKGROUND = Color.black, - BORDERS_AND_DIAGONALS = Color.orange, - TEXT_COLOR = Color.white; - - // - Panel() { - super( - BACKGROUND, - BORDERS_AND_DIAGONALS, true, - BORDERS_AND_DIAGONALS, true); - } - - // - @Override - public void mainPaint(@NotNull Graphics g) { - g.setColor(TEXT_COLOR); - g.drawString("A window for test 3", 50, 50); - // paint more stuff here, if needed - } -} \ No newline at end of file diff --git a/src/test/java/test3/graphics/PanelPalette.java b/src/test/java/test3/graphics/PanelPalette.java new file mode 100644 index 0000000..453dd9f --- /dev/null +++ b/src/test/java/test3/graphics/PanelPalette.java @@ -0,0 +1,46 @@ +package test3.graphics; + +import java.awt.Color; + +import org.jetbrains.annotations.NotNull; + +import commonGraphics.ColorUtils; + +// +public class PanelPalette { + @SuppressWarnings("FieldMayBeFinal") + private @NotNull Color + mainBackground, panelBackground, + panelBorder, panelDiagonals; + + public PanelPalette() { + mainBackground = Color.black; + panelBackground = getOpaqueGray(45); + panelBorder = getOpaqueGray(30); + panelDiagonals = new Color(255, 120, 0); + } + + private @NotNull Color getOpaqueGray(int brightness) { + return ColorUtils.getGray(brightness, 255); + } + + // + public @NotNull Color getMainBackground() { + return mainBackground; + } + + // + public @NotNull Color getPanelBackground() { + return panelBackground; + } + + // + public @NotNull Color getPanelBorder() { + return panelBorder; + } + + // + public @NotNull Color getPanelDiagonals() { + return panelDiagonals; + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/Window.java b/src/test/java/test3/graphics/Window.java index accf4ba..b4e4575 100644 --- a/src/test/java/test3/graphics/Window.java +++ b/src/test/java/test3/graphics/Window.java @@ -1,16 +1,23 @@ package test3.graphics; import java.awt.LayoutManager; -import javax.swing.BoxLayout; +import java.awt.BorderLayout; + +import org.jetbrains.annotations.NotNull; import commonGraphics.UpdatingWindow; +import test3.player.Player; // public class Window extends UpdatingWindow { + private final @NotNull Player player; + private final @NotNull PanelPalette panelPalette; // - public Window() { + public Window(@NotNull Player player) { super(new TestWindowSettings()); //default frame rate + this.player = player; + panelPalette = new PanelPalette(); //add stuff before panels here addPanels(); //add key listener here @@ -21,9 +28,9 @@ public Window() { // @Override public void addPanels() { - LayoutManager layout = new BoxLayout(getContentPane(), BoxLayout.X_AXIS); + @NotNull LayoutManager layout = new BorderLayout(); getContentPane().setLayout(layout); - add(new Panel()); - // Add more panels here, if needed. + add(new BottomPanel(player, panelPalette), BorderLayout.SOUTH); + add(new MainPanel(player, panelPalette)); } } \ No newline at end of file diff --git a/src/test/java/test3/player/PaintMode.java b/src/test/java/test3/player/PaintMode.java new file mode 100644 index 0000000..0319739 --- /dev/null +++ b/src/test/java/test3/player/PaintMode.java @@ -0,0 +1,8 @@ +package test3.player; + +// +public enum PaintMode { + FLIGHT, + SHIPYARD, + MISSIONS +} \ No newline at end of file diff --git a/src/test/java/test3/player/Player.java b/src/test/java/test3/player/Player.java new file mode 100644 index 0000000..46b413e --- /dev/null +++ b/src/test/java/test3/player/Player.java @@ -0,0 +1,23 @@ +package test3.player; + +import org.jetbrains.annotations.NotNull; + +// +public class Player { + private PaintMode paintMode; + + // + public Player() { + paintMode = PaintMode.SHIPYARD; + } + + // + public final @NotNull PaintMode getPaintMode() { + return paintMode; + } + + // + public void setPaintMode(PaintMode paintMode) { + this.paintMode = paintMode; + } +} \ No newline at end of file From 88723dfe18787ef529ee9224b3a44c6dc86a8932 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 16:06:51 +0300 Subject: [PATCH 05/15] key listener --- .../java/test3/graphics/MyKeyListener.java | 39 +++++++++++++++++++ src/test/java/test3/graphics/Window.java | 2 +- src/test/java/test3/player/Player.java | 4 +- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/test/java/test3/graphics/MyKeyListener.java diff --git a/src/test/java/test3/graphics/MyKeyListener.java b/src/test/java/test3/graphics/MyKeyListener.java new file mode 100644 index 0000000..3c2a76f --- /dev/null +++ b/src/test/java/test3/graphics/MyKeyListener.java @@ -0,0 +1,39 @@ +package test3.graphics; + +import org.jetbrains.annotations.NotNull; + +import commonGraphics.AbstractKeyListener; +import test3.player.PaintMode; +import test3.player.Player; + +// +public final class MyKeyListener extends AbstractKeyListener { + private final @NotNull Player player; + + // + public MyKeyListener(@NotNull Player player) { + super(); + this.player = player; + } + + // + @SuppressWarnings("SwitchStatementWithTooFewBranches") + @Override + public void keyActionSwitch_byCode(int keyCode) throws UndefinedKeyActionException { + switch (keyCode) { + //check keys by keyCode here + default -> throw new UndefinedKeyActionException(); + } + } + + // + @Override + public void keyActionSwitch_byText(@NotNull String keyText) throws UndefinedKeyActionException { + switch (keyText) { + case "1" -> player.setPaintMode(PaintMode.FLIGHT); + case "2" -> player.setPaintMode(PaintMode.SHIPYARD); + case "3" -> player.setPaintMode(PaintMode.MISSIONS); + default -> throw new UndefinedKeyActionException(); + } + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/Window.java b/src/test/java/test3/graphics/Window.java index b4e4575..41c82c5 100644 --- a/src/test/java/test3/graphics/Window.java +++ b/src/test/java/test3/graphics/Window.java @@ -20,7 +20,7 @@ public Window(@NotNull Player player) { panelPalette = new PanelPalette(); //add stuff before panels here addPanels(); - //add key listener here + addKeyListener(new MyKeyListener(this.player)); revalidate(); startUpdating(); } diff --git a/src/test/java/test3/player/Player.java b/src/test/java/test3/player/Player.java index 46b413e..a0c12f1 100644 --- a/src/test/java/test3/player/Player.java +++ b/src/test/java/test3/player/Player.java @@ -4,7 +4,7 @@ // public class Player { - private PaintMode paintMode; + private @NotNull PaintMode paintMode; // public Player() { @@ -17,7 +17,7 @@ public Player() { } // - public void setPaintMode(PaintMode paintMode) { + public void setPaintMode(@NotNull PaintMode paintMode) { this.paintMode = paintMode; } } \ No newline at end of file From a151ab6ce990cbf1dff971457bac714c358b6726 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 16:15:16 +0300 Subject: [PATCH 06/15] color improvements --- src/test/java/commonGraphics/ColorUtils.java | 22 +++++++++++++++++-- .../java/test3/graphics/PanelPalette.java | 8 ++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/test/java/commonGraphics/ColorUtils.java b/src/test/java/commonGraphics/ColorUtils.java index 9505439..a640318 100644 --- a/src/test/java/commonGraphics/ColorUtils.java +++ b/src/test/java/commonGraphics/ColorUtils.java @@ -6,11 +6,29 @@ //Color utilities. public final class ColorUtils { + private static final int OPAQUE_ALPHA = 255; + @SuppressWarnings("unused") - public static final @NotNull Color TRANSPARENT_BLACK = new Color(0, 0, 0, 0); + public static final @NotNull Color TRANSPARENT_BLACK = getGray(0, 0); - //gets a gray color from brightness and alpha + /** + * Gets a gray color from brightness and alpha. + * + * @param brightness A brightness value 0-255 (inclusive). + * @param alpha Alpha value for transparency, 0-255 (inclusive). Lower values are more transparent. + * @return A gray color. + */ public static @NotNull Color getGray(int brightness, int alpha) { return new Color(brightness, brightness, brightness, alpha); } + + /** + * Gets an opaque gray color from brightness. + * + * @param brightness A brightness value 0-255 (inclusive). + * @return An opaque gray color. + */ + public static @NotNull Color getOpaqueGray(int brightness) { + return getGray(brightness, OPAQUE_ALPHA); + } } \ No newline at end of file diff --git a/src/test/java/test3/graphics/PanelPalette.java b/src/test/java/test3/graphics/PanelPalette.java index 453dd9f..b277125 100644 --- a/src/test/java/test3/graphics/PanelPalette.java +++ b/src/test/java/test3/graphics/PanelPalette.java @@ -15,15 +15,11 @@ public class PanelPalette { public PanelPalette() { mainBackground = Color.black; - panelBackground = getOpaqueGray(45); - panelBorder = getOpaqueGray(30); + panelBackground = ColorUtils.getOpaqueGray(45); + panelBorder = ColorUtils.getOpaqueGray(30); panelDiagonals = new Color(255, 120, 0); } - private @NotNull Color getOpaqueGray(int brightness) { - return ColorUtils.getGray(brightness, 255); - } - // public @NotNull Color getMainBackground() { return mainBackground; From 2af5b8d0f7c8b279f59b9e19a48d65cc47c8aead Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:48:43 +0300 Subject: [PATCH 07/15] improvements --- src/test/java/test3/Test3.java | 9 +++- src/test/java/test3/graphics/BottomPanel.java | 46 ++++++++++++++-- src/test/java/test3/graphics/MainPanel.java | 52 ++++++++++++++++--- src/test/java/test3/models/CoreModule.java | 7 +++ src/test/java/test3/models/Spacecraft.java | 9 ++++ .../java/test3/models/SpacecraftModule.java | 9 ++++ .../java/test3/models/StructuralModule.java | 9 ++++ src/test/java/test3/models/TestModule.java | 9 ++++ .../player/InsufficientFundsException.java | 17 ++++++ src/test/java/test3/player/Player.java | 42 +++++++++++++-- src/test/java/test3/player/Wallet.java | 31 +++++++++++ 11 files changed, 227 insertions(+), 13 deletions(-) create mode 100644 src/test/java/test3/models/CoreModule.java create mode 100644 src/test/java/test3/models/Spacecraft.java create mode 100644 src/test/java/test3/models/SpacecraftModule.java create mode 100644 src/test/java/test3/models/StructuralModule.java create mode 100644 src/test/java/test3/models/TestModule.java create mode 100644 src/test/java/test3/player/InsufficientFundsException.java create mode 100644 src/test/java/test3/player/Wallet.java diff --git a/src/test/java/test3/Test3.java b/src/test/java/test3/Test3.java index fcb086e..7c3fbe1 100644 --- a/src/test/java/test3/Test3.java +++ b/src/test/java/test3/Test3.java @@ -3,15 +3,22 @@ import org.jetbrains.annotations.NotNull; import graphicalTestAbstraction.GraphicalTest; +import test3.models.TestModule; +import test3.models.Spacecraft; import test3.player.Player; import test3.graphics.Window; // public class Test3 extends GraphicalTest { - private static final @NotNull Player PLAYER = new Player(); + private static final int STARTING_MONEY = 1000; + private static final @NotNull Player PLAYER = new Player(STARTING_MONEY); // public static void main(String[] args) { + PLAYER.addShipPart(new TestModule()); + PLAYER.addShipPart(new TestModule()); + PLAYER.addShipPart(new TestModule()); + PLAYER.addShip(new Spacecraft()); new Test3(PLAYER); } diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java index 246d805..69a53cc 100644 --- a/src/test/java/test3/graphics/BottomPanel.java +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -1,16 +1,20 @@ package test3.graphics; +import java.util.List; +import java.util.ArrayList; import java.awt.Color; import java.awt.Graphics; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import commonGraphics.panels.FixedHorizontalPanel; +import test3.models.SpacecraftModule; import test3.player.Player; // public class BottomPanel extends FixedHorizontalPanel { - private static final int PANEL_HEIGHT = 200; + private static final int PANEL_HEIGHT = 300; private static final @NotNull Color TEXT_COLOR = Color.white; private final @NotNull Player player; @@ -46,9 +50,45 @@ private void paintMode_flight(@NotNull Graphics g) { } private void paintMode_shipyard(@NotNull Graphics g) { - setDrawDiagonals(true); + setDrawDiagonals(false); + int @NotNull [] partDrawLocation = new int [] {100, 100}; + int + partSeparation = 160, + infoY = 180, textOffsetX = -15; + @NotNull List<@NotNull SpacecraftModule> partInventory = player.getShipPartInventory(); + for (int i = 0; i < partInventory.size(); i++) { + @NotNull SpacecraftModule part = partInventory.get(i); + int partX = partDrawLocation[0] + partSeparation * i; + paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}); + paintPartInfo(g, part, new int [] {partX + textOffsetX, infoY}); + } + } + + private void paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] location) { + @NotNull Color partColor = Color.green; + int size = 50; + int @NotNull [] drawStart = new int [] {location[0] - size / 2, location[1] - size / 2}; + g.setColor(partColor); + g.drawRect(drawStart[0], drawStart[1], size, size); + } + + private void paintPartInfo(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] location) { + int textHeight = 15; g.setColor(TEXT_COLOR); - g.drawString("Shipyard mode not ready yet...", 50, 50); + drawStringList(g, location, textHeight, new ArrayList<>() {{ + add("Part: "); + add("coming soon"); + }}); + } + + private void drawStringList(@NotNull Graphics g, int @NotNull [] location, int textHeight, + @NotNull List<@Nullable String> list) { + for (int i = 0; i < list.size(); i++) { + @Nullable String string = list.get(i); + if (string != null) { + g.drawString(string, location[0], location[1] + textHeight * (i + 1)); + } + } } private void paintMode_missions(@NotNull Graphics g) { diff --git a/src/test/java/test3/graphics/MainPanel.java b/src/test/java/test3/graphics/MainPanel.java index 490e475..cb1ead8 100644 --- a/src/test/java/test3/graphics/MainPanel.java +++ b/src/test/java/test3/graphics/MainPanel.java @@ -1,11 +1,15 @@ package test3.graphics; +import java.util.List; +import java.util.ArrayList; import java.awt.Color; import java.awt.Graphics; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import commonGraphics.panels.MinimalPanel; +import test3.player.PaintMode; import test3.player.Player; // @@ -25,7 +29,9 @@ final class MainPanel extends MinimalPanel { // @Override public void mainPaint(@NotNull Graphics g) { - switch (player.getPaintMode()) { + @NotNull PaintMode paintMode = player.getPaintMode(); + paintCommonInfo(g, paintMode); + switch (paintMode) { case FLIGHT -> paintMode_flight(g); case SHIPYARD -> paintMode_shipyard(g); case MISSIONS -> paintMode_missions(g); @@ -33,6 +39,26 @@ public void mainPaint(@NotNull Graphics g) { } } + private void paintCommonInfo(@NotNull Graphics g, @NotNull PaintMode mode) { + int textHeight = 15; + int @NotNull [] textLocation = new int [] {20, 20}; + g.setColor(TEXT_COLOR); + drawStringList(g, textLocation, textHeight, new ArrayList<>() {{ + add("Mode: " + mode); + add("Money: " + player.getWallet().getMoney()); + }}); + } + + private void drawStringList(@NotNull Graphics g, int @NotNull [] location, int textHeight, + @NotNull List<@Nullable String> list) { + for (int i = 0; i < list.size(); i++) { + @Nullable String string = list.get(i); + if (string != null) { + g.drawString(string, location[0], location[1] + textHeight * (i + 1)); + } + } + } + private void paintMode_default(@NotNull Graphics g) { setDrawDiagonals(true); g.setColor(TEXT_COLOR); @@ -41,19 +67,33 @@ private void paintMode_default(@NotNull Graphics g) { private void paintMode_flight(@NotNull Graphics g) { setDrawDiagonals(true); + int x = getWidth() / 2; + int y = getHeight() / 2; g.setColor(TEXT_COLOR); - g.drawString("Flight mode not ready yet...", 50, 50); + g.drawString("Flight mode not ready yet...", x, y); } private void paintMode_shipyard(@NotNull Graphics g) { - setDrawDiagonals(true); - g.setColor(TEXT_COLOR); - g.drawString("Shipyard mode not ready yet...", 50, 50); + setDrawDiagonals(false); + int @NotNull [] drawCenter = new int [] {getWidth() / 2, getHeight() / 2}; + paintShip(g, drawCenter); + } + + private void paintShip(@NotNull Graphics g, int @NotNull [] location) { + @NotNull Color color = Color.green; + int size = 50; + int @NotNull [] drawStart = new int [] { + location[0] - size / 2, + location[1] - size / 2}; + g.setColor(color); + g.drawRect(drawStart[0], drawStart[1], size, size); } private void paintMode_missions(@NotNull Graphics g) { setDrawDiagonals(true); + int x = getWidth() / 2; + int y = getHeight() / 2; g.setColor(TEXT_COLOR); - g.drawString("Missions mode not ready yet...", 50, 50); + g.drawString("Missions mode not ready yet...", x, y); } } \ No newline at end of file diff --git a/src/test/java/test3/models/CoreModule.java b/src/test/java/test3/models/CoreModule.java new file mode 100644 index 0000000..2d2db60 --- /dev/null +++ b/src/test/java/test3/models/CoreModule.java @@ -0,0 +1,7 @@ +package test3.models; + +public class CoreModule extends StructuralModule { + public CoreModule() { + super(); + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/Spacecraft.java b/src/test/java/test3/models/Spacecraft.java new file mode 100644 index 0000000..2910160 --- /dev/null +++ b/src/test/java/test3/models/Spacecraft.java @@ -0,0 +1,9 @@ +package test3.models; + +// +public class Spacecraft { + // + public Spacecraft() { + // + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/SpacecraftModule.java b/src/test/java/test3/models/SpacecraftModule.java new file mode 100644 index 0000000..26e494e --- /dev/null +++ b/src/test/java/test3/models/SpacecraftModule.java @@ -0,0 +1,9 @@ +package test3.models; + +// +public abstract class SpacecraftModule { + // + SpacecraftModule() { + // + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/StructuralModule.java b/src/test/java/test3/models/StructuralModule.java new file mode 100644 index 0000000..d70731c --- /dev/null +++ b/src/test/java/test3/models/StructuralModule.java @@ -0,0 +1,9 @@ +package test3.models; + +// +public class StructuralModule extends SpacecraftModule { + // + public StructuralModule(){ + super(); + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/TestModule.java b/src/test/java/test3/models/TestModule.java new file mode 100644 index 0000000..d01931d --- /dev/null +++ b/src/test/java/test3/models/TestModule.java @@ -0,0 +1,9 @@ +package test3.models; + +// +public class TestModule extends SpacecraftModule { + // + public TestModule() { + super(); + } +} \ No newline at end of file diff --git a/src/test/java/test3/player/InsufficientFundsException.java b/src/test/java/test3/player/InsufficientFundsException.java new file mode 100644 index 0000000..63eaf55 --- /dev/null +++ b/src/test/java/test3/player/InsufficientFundsException.java @@ -0,0 +1,17 @@ +package test3.player; + +// +public final class InsufficientFundsException extends Exception { + private final int missingFunds; + + // + public InsufficientFundsException(int missingFunds) { + super("Insufficient funds exception"); + this.missingFunds = missingFunds; + } + + // + public int getMissingFunds() { + return missingFunds; + } +} \ No newline at end of file diff --git a/src/test/java/test3/player/Player.java b/src/test/java/test3/player/Player.java index a0c12f1..e42ce91 100644 --- a/src/test/java/test3/player/Player.java +++ b/src/test/java/test3/player/Player.java @@ -1,22 +1,58 @@ package test3.player; +import java.util.List; +import java.util.ArrayList; + import org.jetbrains.annotations.NotNull; +import test3.models.Spacecraft; +import test3.models.SpacecraftModule; + // public class Player { - private @NotNull PaintMode paintMode; + private final @NotNull Wallet wallet; + private final @NotNull List<@NotNull Spacecraft> ownedShips; + private final @NotNull List<@NotNull SpacecraftModule> shipPartInventory; + private @NotNull PaintMode paintMode; // for graphical purposes // - public Player() { + public Player(int money) { + wallet = new Wallet(money); + ownedShips = new ArrayList<>(); + shipPartInventory = new ArrayList<>(); paintMode = PaintMode.SHIPYARD; } + public @NotNull Wallet getWallet() { + return wallet; + } + + // + public @NotNull List<@NotNull Spacecraft> getOwnedShips() { + return ownedShips; + } + + // + public void addShip(@NotNull Spacecraft ship) { + ownedShips.add(ship); + } + + // + public @NotNull List<@NotNull SpacecraftModule> getShipPartInventory() { + return shipPartInventory; + } + // + public void addShipPart(@NotNull SpacecraftModule part) { + shipPartInventory.add(part); + } + + // for graphical purposes public final @NotNull PaintMode getPaintMode() { return paintMode; } - // + // for graphical purposes public void setPaintMode(@NotNull PaintMode paintMode) { this.paintMode = paintMode; } diff --git a/src/test/java/test3/player/Wallet.java b/src/test/java/test3/player/Wallet.java new file mode 100644 index 0000000..1ffb2e7 --- /dev/null +++ b/src/test/java/test3/player/Wallet.java @@ -0,0 +1,31 @@ +package test3.player; + +// +public class Wallet { + private int money; + + // + protected Wallet(int money) { + this.money = money; + } + + // + public int getMoney() { + return money; + } + + //adds money; throws an exception, if negative result + public void addMoney(int money) throws InsufficientFundsException { + int result = this.money + money; + if (result < 0) { + throw new InsufficientFundsException(-result); + } else { + this.money = result; + } + } + + //subtracts money; throws an exception, if negative result + public void subtractMoney(int money) throws InsufficientFundsException { + this.addMoney(-money); + } +} \ No newline at end of file From 8841686fd7cdfe6460f18d03fd10203ea24f6cf9 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:01:19 +0300 Subject: [PATCH 08/15] improvements --- src/test/java/test3/Test3.java | 3 ++- src/test/java/test3/graphics/BottomPanel.java | 2 +- src/test/java/test3/graphics/MyKeyListener.java | 8 ++++---- src/test/java/test3/graphics/PanelPalette.java | 2 +- src/test/java/test3/models/Spacecraft.java | 14 ++++++++++++-- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/test/java/test3/Test3.java b/src/test/java/test3/Test3.java index 7c3fbe1..dea37e4 100644 --- a/src/test/java/test3/Test3.java +++ b/src/test/java/test3/Test3.java @@ -4,6 +4,7 @@ import graphicalTestAbstraction.GraphicalTest; import test3.models.TestModule; +import test3.models.CoreModule; import test3.models.Spacecraft; import test3.player.Player; import test3.graphics.Window; @@ -18,7 +19,7 @@ public static void main(String[] args) { PLAYER.addShipPart(new TestModule()); PLAYER.addShipPart(new TestModule()); PLAYER.addShipPart(new TestModule()); - PLAYER.addShip(new Spacecraft()); + PLAYER.addShip(new Spacecraft(new CoreModule())); new Test3(PLAYER); } diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java index 69a53cc..3e3fdf3 100644 --- a/src/test/java/test3/graphics/BottomPanel.java +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -13,7 +13,7 @@ import test3.player.Player; // -public class BottomPanel extends FixedHorizontalPanel { +final class BottomPanel extends FixedHorizontalPanel { private static final int PANEL_HEIGHT = 300; private static final @NotNull Color TEXT_COLOR = Color.white; private final @NotNull Player player; diff --git a/src/test/java/test3/graphics/MyKeyListener.java b/src/test/java/test3/graphics/MyKeyListener.java index 3c2a76f..c73c493 100644 --- a/src/test/java/test3/graphics/MyKeyListener.java +++ b/src/test/java/test3/graphics/MyKeyListener.java @@ -7,7 +7,7 @@ import test3.player.Player; // -public final class MyKeyListener extends AbstractKeyListener { +final class MyKeyListener extends AbstractKeyListener { private final @NotNull Player player; // @@ -30,9 +30,9 @@ public void keyActionSwitch_byCode(int keyCode) throws UndefinedKeyActionExcepti @Override public void keyActionSwitch_byText(@NotNull String keyText) throws UndefinedKeyActionException { switch (keyText) { - case "1" -> player.setPaintMode(PaintMode.FLIGHT); - case "2" -> player.setPaintMode(PaintMode.SHIPYARD); - case "3" -> player.setPaintMode(PaintMode.MISSIONS); + case "F1" -> player.setPaintMode(PaintMode.FLIGHT); + case "F2" -> player.setPaintMode(PaintMode.SHIPYARD); + case "F3" -> player.setPaintMode(PaintMode.MISSIONS); default -> throw new UndefinedKeyActionException(); } } diff --git a/src/test/java/test3/graphics/PanelPalette.java b/src/test/java/test3/graphics/PanelPalette.java index b277125..e68a89f 100644 --- a/src/test/java/test3/graphics/PanelPalette.java +++ b/src/test/java/test3/graphics/PanelPalette.java @@ -7,7 +7,7 @@ import commonGraphics.ColorUtils; // -public class PanelPalette { +final class PanelPalette { @SuppressWarnings("FieldMayBeFinal") private @NotNull Color mainBackground, panelBackground, diff --git a/src/test/java/test3/models/Spacecraft.java b/src/test/java/test3/models/Spacecraft.java index 2910160..06b3186 100644 --- a/src/test/java/test3/models/Spacecraft.java +++ b/src/test/java/test3/models/Spacecraft.java @@ -1,9 +1,19 @@ package test3.models; +import org.jetbrains.annotations.NotNull; + // +@SuppressWarnings("ClassCanBeRecord") public class Spacecraft { + private final @NotNull CoreModule core; + + // + public Spacecraft(@NotNull CoreModule core) { + this.core = core; + } + // - public Spacecraft() { - // + public @NotNull CoreModule getCore() { + return core; } } \ No newline at end of file From b67a3adbd453fa293f84bf9ef5f89235c026cc53 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:42:43 +0300 Subject: [PATCH 09/15] improvements --- src/test/java/test3/Test3.java | 5 ++-- src/test/java/test3/graphics/BottomPanel.java | 16 +++------- src/test/java/test3/graphics/MainPanel.java | 21 ++++++-------- src/test/java/test3/graphics/ShipPainter.java | 19 ++++++++++++ .../java/test3/graphics/ShipPartPainter.java | 20 +++++++++++++ src/test/java/test3/models/CoreModule.java | 7 ----- src/test/java/test3/models/Spacecraft.java | 29 ++++++++++++++++--- .../java/test3/models/SpacecraftModule.java | 29 +++++++++++++++++-- .../java/test3/models/StructuralModule.java | 2 +- src/test/java/test3/models/TestModule.java | 2 +- 10 files changed, 109 insertions(+), 41 deletions(-) create mode 100644 src/test/java/test3/graphics/ShipPainter.java create mode 100644 src/test/java/test3/graphics/ShipPartPainter.java delete mode 100644 src/test/java/test3/models/CoreModule.java diff --git a/src/test/java/test3/Test3.java b/src/test/java/test3/Test3.java index dea37e4..8ae0c9e 100644 --- a/src/test/java/test3/Test3.java +++ b/src/test/java/test3/Test3.java @@ -3,8 +3,8 @@ import org.jetbrains.annotations.NotNull; import graphicalTestAbstraction.GraphicalTest; +import test3.models.StructuralModule; import test3.models.TestModule; -import test3.models.CoreModule; import test3.models.Spacecraft; import test3.player.Player; import test3.graphics.Window; @@ -19,7 +19,8 @@ public static void main(String[] args) { PLAYER.addShipPart(new TestModule()); PLAYER.addShipPart(new TestModule()); PLAYER.addShipPart(new TestModule()); - PLAYER.addShip(new Spacecraft(new CoreModule())); + PLAYER.addShip(new Spacecraft("Ship 1", new StructuralModule())); + PLAYER.addShip(new Spacecraft("Ship 2", new StructuralModule())); new Test3(PLAYER); } diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java index 3e3fdf3..30fc60b 100644 --- a/src/test/java/test3/graphics/BottomPanel.java +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -54,30 +54,22 @@ private void paintMode_shipyard(@NotNull Graphics g) { int @NotNull [] partDrawLocation = new int [] {100, 100}; int partSeparation = 160, - infoY = 180, textOffsetX = -15; + infoY = 180, textOffsetX = -40; @NotNull List<@NotNull SpacecraftModule> partInventory = player.getShipPartInventory(); for (int i = 0; i < partInventory.size(); i++) { @NotNull SpacecraftModule part = partInventory.get(i); int partX = partDrawLocation[0] + partSeparation * i; - paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}); + ShipPartPainter.paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}); paintPartInfo(g, part, new int [] {partX + textOffsetX, infoY}); } } - private void paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] location) { - @NotNull Color partColor = Color.green; - int size = 50; - int @NotNull [] drawStart = new int [] {location[0] - size / 2, location[1] - size / 2}; - g.setColor(partColor); - g.drawRect(drawStart[0], drawStart[1], size, size); - } - private void paintPartInfo(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] location) { int textHeight = 15; g.setColor(TEXT_COLOR); drawStringList(g, location, textHeight, new ArrayList<>() {{ - add("Part: "); - add("coming soon"); + add(part.getName()); + //add more part info lines here }}); } diff --git a/src/test/java/test3/graphics/MainPanel.java b/src/test/java/test3/graphics/MainPanel.java index cb1ead8..eb7cfef 100644 --- a/src/test/java/test3/graphics/MainPanel.java +++ b/src/test/java/test3/graphics/MainPanel.java @@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable; import commonGraphics.panels.MinimalPanel; +import test3.models.Spacecraft; import test3.player.PaintMode; import test3.player.Player; @@ -75,18 +76,14 @@ private void paintMode_flight(@NotNull Graphics g) { private void paintMode_shipyard(@NotNull Graphics g) { setDrawDiagonals(false); - int @NotNull [] drawCenter = new int [] {getWidth() / 2, getHeight() / 2}; - paintShip(g, drawCenter); - } - - private void paintShip(@NotNull Graphics g, int @NotNull [] location) { - @NotNull Color color = Color.green; - int size = 50; - int @NotNull [] drawStart = new int [] { - location[0] - size / 2, - location[1] - size / 2}; - g.setColor(color); - g.drawRect(drawStart[0], drawStart[1], size, size); + @NotNull List<@NotNull Spacecraft> ships = player.getOwnedShips(); + int shipCount = ships.size(); + int gap = getWidth() / (shipCount + 1); + int @NotNull [] firstShipLocation = new int [] {gap, getHeight() / 2}; + for (int i = 0; i < shipCount; i++) { + @NotNull Spacecraft ship = ships.get(i); + ShipPainter.paintShip(g, ship, new int [] {firstShipLocation[0] + gap * i, firstShipLocation[1]}); + } } private void paintMode_missions(@NotNull Graphics g) { diff --git a/src/test/java/test3/graphics/ShipPainter.java b/src/test/java/test3/graphics/ShipPainter.java new file mode 100644 index 0000000..46b14c0 --- /dev/null +++ b/src/test/java/test3/graphics/ShipPainter.java @@ -0,0 +1,19 @@ +package test3.graphics; + +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; + +import test3.models.Spacecraft; + +// +final class ShipPainter { + static void paintShip(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNull [] location) { + int size = ship.getSize(); + int @NotNull [] drawStart = new int [] { + location[0] - size / 2, + location[1] - size / 2}; + g.setColor(ship.getColor()); + g.drawRect(drawStart[0], drawStart[1], size, size); + } +} \ No newline at end of file diff --git a/src/test/java/test3/graphics/ShipPartPainter.java b/src/test/java/test3/graphics/ShipPartPainter.java new file mode 100644 index 0000000..bd3a782 --- /dev/null +++ b/src/test/java/test3/graphics/ShipPartPainter.java @@ -0,0 +1,20 @@ +package test3.graphics; + +import java.awt.Graphics; + +import org.jetbrains.annotations.NotNull; + +import test3.models.SpacecraftModule; + +// +final class ShipPartPainter { + // + static void paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] paintCenter) { + int size = part.getSize(); + int @NotNull [] drawStart = new int [] { + paintCenter[0] - size / 2, + paintCenter[1] - size / 2}; + g.setColor(part.getColor()); + g.drawRect(drawStart[0], drawStart[1], size, size); + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/CoreModule.java b/src/test/java/test3/models/CoreModule.java deleted file mode 100644 index 2d2db60..0000000 --- a/src/test/java/test3/models/CoreModule.java +++ /dev/null @@ -1,7 +0,0 @@ -package test3.models; - -public class CoreModule extends StructuralModule { - public CoreModule() { - super(); - } -} \ No newline at end of file diff --git a/src/test/java/test3/models/Spacecraft.java b/src/test/java/test3/models/Spacecraft.java index 06b3186..24eea4a 100644 --- a/src/test/java/test3/models/Spacecraft.java +++ b/src/test/java/test3/models/Spacecraft.java @@ -1,19 +1,40 @@ package test3.models; +import java.awt.Color; + import org.jetbrains.annotations.NotNull; // -@SuppressWarnings("ClassCanBeRecord") public class Spacecraft { - private final @NotNull CoreModule core; + private final @NotNull String name; + private final @NotNull StructuralModule core; + private final @NotNull Color color; //for graphical purposes // - public Spacecraft(@NotNull CoreModule core) { + public Spacecraft(@NotNull String name, @NotNull StructuralModule core) { + this.name = name; this.core = core; + color = Color.red; + } + + // + public @NotNull String getName() { + return name; } // - public @NotNull CoreModule getCore() { + public @NotNull StructuralModule getCore() { return core; } + + //gets the total size of the ship + public int getSize() { + //TODO: improve this + return core.getSize(); + } + + //for graphical purposes + public @NotNull Color getColor() { + return color; + } } \ No newline at end of file diff --git a/src/test/java/test3/models/SpacecraftModule.java b/src/test/java/test3/models/SpacecraftModule.java index 26e494e..6ebeed2 100644 --- a/src/test/java/test3/models/SpacecraftModule.java +++ b/src/test/java/test3/models/SpacecraftModule.java @@ -1,9 +1,34 @@ package test3.models; +import java.awt.Color; + +import org.jetbrains.annotations.NotNull; + // public abstract class SpacecraftModule { + private final @NotNull String name; + private final int size; + private final @NotNull Color color; // for graphical purposes + + // + protected SpacecraftModule(@NotNull String name, int size) { + this.name = name; + this.size = size; + color = Color.green; + } + // - SpacecraftModule() { - // + public @NotNull String getName() { + return name; + } + + // + public int getSize() { + return size; + } + + //for graphical purposes + public @NotNull Color getColor() { + return color; } } \ No newline at end of file diff --git a/src/test/java/test3/models/StructuralModule.java b/src/test/java/test3/models/StructuralModule.java index d70731c..df0ea08 100644 --- a/src/test/java/test3/models/StructuralModule.java +++ b/src/test/java/test3/models/StructuralModule.java @@ -4,6 +4,6 @@ public class StructuralModule extends SpacecraftModule { // public StructuralModule(){ - super(); + super("Structural module", 50); } } \ No newline at end of file diff --git a/src/test/java/test3/models/TestModule.java b/src/test/java/test3/models/TestModule.java index d01931d..f15bf6f 100644 --- a/src/test/java/test3/models/TestModule.java +++ b/src/test/java/test3/models/TestModule.java @@ -4,6 +4,6 @@ public class TestModule extends SpacecraftModule { // public TestModule() { - super(); + super("Test module", 50); } } \ No newline at end of file From c541f4e345297ce2e6fa6964974ea6b0e420cdfc Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:55:32 +0300 Subject: [PATCH 10/15] improvements --- .../java/test3/graphics/MyKeyListener.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/test/java/test3/graphics/MyKeyListener.java b/src/test/java/test3/graphics/MyKeyListener.java index c73c493..fbb72e0 100644 --- a/src/test/java/test3/graphics/MyKeyListener.java +++ b/src/test/java/test3/graphics/MyKeyListener.java @@ -2,6 +2,8 @@ import org.jetbrains.annotations.NotNull; +import static consoleUtils.SimplePrinting.printLine; + import commonGraphics.AbstractKeyListener; import test3.player.PaintMode; import test3.player.Player; @@ -33,6 +35,41 @@ public void keyActionSwitch_byText(@NotNull String keyText) throws UndefinedKeyA case "F1" -> player.setPaintMode(PaintMode.FLIGHT); case "F2" -> player.setPaintMode(PaintMode.SHIPYARD); case "F3" -> player.setPaintMode(PaintMode.MISSIONS); + //check more universal keys here + default -> { + switch (player.getPaintMode()) { + case FLIGHT -> keyActionSwitch_byText_FlightMode(keyText); + case SHIPYARD -> keyActionSwitch_byText_ShipyardMode(keyText); + case MISSIONS -> keyActionSwitch_byText_MissionsMode(keyText); + default -> throw new UndefinedKeyActionException(); + } + } + } + } + + @SuppressWarnings("SwitchStatementWithTooFewBranches") + private void keyActionSwitch_byText_FlightMode(@NotNull String keyText) throws UndefinedKeyActionException { + switch (keyText) { + case "Space" -> printLine("A space bar has been pressed!"); + //check more flight-specific keys here + default -> throw new UndefinedKeyActionException(); + } + } + + @SuppressWarnings("SwitchStatementWithTooFewBranches") + private void keyActionSwitch_byText_ShipyardMode(@NotNull String keyText) throws UndefinedKeyActionException { + switch (keyText) { + case "Space" -> printLine("A space bar has been pressed!"); + //check more shipyard-specific keys here + default -> throw new UndefinedKeyActionException(); + } + } + + @SuppressWarnings("SwitchStatementWithTooFewBranches") + private void keyActionSwitch_byText_MissionsMode(@NotNull String keyText) throws UndefinedKeyActionException { + switch (keyText) { + case "Space" -> printLine("A space bar has been pressed!"); + //check more missions-specific keys here default -> throw new UndefinedKeyActionException(); } } From bdc9eeddfe36069dda8d311a06e36bde956a0b42 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 19:16:48 +0300 Subject: [PATCH 11/15] improvements --- src/test/java/test3/Test3.java | 9 ++++ src/test/java/test3/graphics/BottomPanel.java | 23 +---------- src/test/java/test3/graphics/ShipPainter.java | 41 +++++++++++++++++++ .../java/test3/graphics/ShipPartPainter.java | 29 ++++++++++++- src/test/java/test3/models/Spacecraft.java | 4 +- .../java/test3/models/SpacecraftModule.java | 4 +- .../java/test3/models/StructuralModule.java | 4 +- src/test/java/test3/models/TestModule.java | 4 +- 8 files changed, 89 insertions(+), 29 deletions(-) diff --git a/src/test/java/test3/Test3.java b/src/test/java/test3/Test3.java index 8ae0c9e..d2dfcaf 100644 --- a/src/test/java/test3/Test3.java +++ b/src/test/java/test3/Test3.java @@ -16,11 +16,20 @@ public class Test3 extends GraphicalTest { // public static void main(String[] args) { + //adds 2 structural modules + PLAYER.addShipPart(new StructuralModule()); + PLAYER.addShipPart(new StructuralModule()); + + //adds 3 test modules PLAYER.addShipPart(new TestModule()); PLAYER.addShipPart(new TestModule()); PLAYER.addShipPart(new TestModule()); + + //adds 2 ships PLAYER.addShip(new Spacecraft("Ship 1", new StructuralModule())); PLAYER.addShip(new Spacecraft("Ship 2", new StructuralModule())); + + //creates the player with some starting money new Test3(PLAYER); } diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java index 30fc60b..a534274 100644 --- a/src/test/java/test3/graphics/BottomPanel.java +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -1,12 +1,10 @@ package test3.graphics; import java.util.List; -import java.util.ArrayList; import java.awt.Color; import java.awt.Graphics; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import commonGraphics.panels.FixedHorizontalPanel; import test3.models.SpacecraftModule; @@ -60,26 +58,7 @@ private void paintMode_shipyard(@NotNull Graphics g) { @NotNull SpacecraftModule part = partInventory.get(i); int partX = partDrawLocation[0] + partSeparation * i; ShipPartPainter.paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}); - paintPartInfo(g, part, new int [] {partX + textOffsetX, infoY}); - } - } - - private void paintPartInfo(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] location) { - int textHeight = 15; - g.setColor(TEXT_COLOR); - drawStringList(g, location, textHeight, new ArrayList<>() {{ - add(part.getName()); - //add more part info lines here - }}); - } - - private void drawStringList(@NotNull Graphics g, int @NotNull [] location, int textHeight, - @NotNull List<@Nullable String> list) { - for (int i = 0; i < list.size(); i++) { - @Nullable String string = list.get(i); - if (string != null) { - g.drawString(string, location[0], location[1] + textHeight * (i + 1)); - } + ShipPartPainter.paintPartInfo(g, part, TEXT_COLOR, new int [] {partX + textOffsetX, infoY}); } } diff --git a/src/test/java/test3/graphics/ShipPainter.java b/src/test/java/test3/graphics/ShipPainter.java index 46b14c0..facf541 100644 --- a/src/test/java/test3/graphics/ShipPainter.java +++ b/src/test/java/test3/graphics/ShipPainter.java @@ -1,14 +1,35 @@ package test3.graphics; +import java.util.List; +import java.util.ArrayList; +import java.awt.Color; import java.awt.Graphics; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import test3.models.Spacecraft; // final class ShipPainter { + private static final int TEXT_HEIGHT = 15; + + //call this to paint static void paintShip(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNull [] location) { + paintParts(g, ship, location); + paintOutline(g, ship, location); + int shipInfoOffsetX = -20; + int @NotNull [] shipInfoLocation = new int [] { + location[0] + shipInfoOffsetX, + location[1] + ship.getSize() / 2}; + paintShipInfo(g, ship, ship.getColor(), shipInfoLocation); + } + + private static void paintParts(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNull [] location) { + ShipPartPainter.paintShipPart(g, ship.getCore(), location); + } + + private static void paintOutline(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNull [] location) { int size = ship.getSize(); int @NotNull [] drawStart = new int [] { location[0] - size / 2, @@ -16,4 +37,24 @@ static void paintShip(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNul g.setColor(ship.getColor()); g.drawRect(drawStart[0], drawStart[1], size, size); } + + private static void paintShipInfo(@NotNull Graphics g, @NotNull Spacecraft ship, @NotNull Color textColor, int @NotNull [] location) { + // + g.setColor(textColor); + drawStringList(g, location, TEXT_HEIGHT, new ArrayList<>() {{ + add(ship.getName()); + //add more ship info lines here + }}); + } + + @SuppressWarnings("SameParameterValue") + private static void drawStringList(@NotNull Graphics g, int @NotNull [] location, int textHeight, + @NotNull List<@Nullable String> list) { + for (int i = 0; i < list.size(); i++) { + @Nullable String string = list.get(i); + if (string != null) { + g.drawString(string, location[0], location[1] + textHeight * (i + 1)); + } + } + } } \ No newline at end of file diff --git a/src/test/java/test3/graphics/ShipPartPainter.java b/src/test/java/test3/graphics/ShipPartPainter.java index bd3a782..b0dc5c3 100644 --- a/src/test/java/test3/graphics/ShipPartPainter.java +++ b/src/test/java/test3/graphics/ShipPartPainter.java @@ -1,14 +1,20 @@ package test3.graphics; +import java.util.List; +import java.util.ArrayList; +import java.awt.Color; import java.awt.Graphics; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import test3.models.SpacecraftModule; // final class ShipPartPainter { - // + private static final int TEXT_HEIGHT = 15; + + //call this to paint part only static void paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] paintCenter) { int size = part.getSize(); int @NotNull [] drawStart = new int [] { @@ -17,4 +23,25 @@ static void paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, i g.setColor(part.getColor()); g.drawRect(drawStart[0], drawStart[1], size, size); } + + //call this to paint part info only + static void paintPartInfo(@NotNull Graphics g, @NotNull SpacecraftModule part, + @NotNull Color textColor, int @NotNull [] location) { + g.setColor(textColor); + drawStringList(g, location, TEXT_HEIGHT, new ArrayList<>() {{ + add(part.getName()); + //add more part info lines here + }}); + } + + @SuppressWarnings("SameParameterValue") + private static void drawStringList(@NotNull Graphics g, int @NotNull [] location, int textHeight, + @NotNull List<@Nullable String> list) { + for (int i = 0; i < list.size(); i++) { + @Nullable String string = list.get(i); + if (string != null) { + g.drawString(string, location[0], location[1] + textHeight * (i + 1)); + } + } + } } \ No newline at end of file diff --git a/src/test/java/test3/models/Spacecraft.java b/src/test/java/test3/models/Spacecraft.java index 24eea4a..a269922 100644 --- a/src/test/java/test3/models/Spacecraft.java +++ b/src/test/java/test3/models/Spacecraft.java @@ -29,8 +29,8 @@ public Spacecraft(@NotNull String name, @NotNull StructuralModule core) { //gets the total size of the ship public int getSize() { - //TODO: improve this - return core.getSize(); + //TODO: improve this and remove the "+10" + return core.getSize() + 10; } //for graphical purposes diff --git a/src/test/java/test3/models/SpacecraftModule.java b/src/test/java/test3/models/SpacecraftModule.java index 6ebeed2..dbb6a5c 100644 --- a/src/test/java/test3/models/SpacecraftModule.java +++ b/src/test/java/test3/models/SpacecraftModule.java @@ -11,10 +11,10 @@ public abstract class SpacecraftModule { private final @NotNull Color color; // for graphical purposes // - protected SpacecraftModule(@NotNull String name, int size) { + protected SpacecraftModule(@NotNull String name, int size, @NotNull Color color) { this.name = name; this.size = size; - color = Color.green; + this.color = color; } // diff --git a/src/test/java/test3/models/StructuralModule.java b/src/test/java/test3/models/StructuralModule.java index df0ea08..f1a7ce0 100644 --- a/src/test/java/test3/models/StructuralModule.java +++ b/src/test/java/test3/models/StructuralModule.java @@ -1,9 +1,11 @@ package test3.models; +import java.awt.Color; + // public class StructuralModule extends SpacecraftModule { // public StructuralModule(){ - super("Structural module", 50); + super("Structural module", 50, Color.green); } } \ No newline at end of file diff --git a/src/test/java/test3/models/TestModule.java b/src/test/java/test3/models/TestModule.java index f15bf6f..b605be3 100644 --- a/src/test/java/test3/models/TestModule.java +++ b/src/test/java/test3/models/TestModule.java @@ -1,9 +1,11 @@ package test3.models; +import java.awt.Color; + // public class TestModule extends SpacecraftModule { // public TestModule() { - super("Test module", 50); + super("Test module", 50, Color.orange); } } \ No newline at end of file From 9097d520685662518d2348c515df98447852f9a3 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 20:24:05 +0300 Subject: [PATCH 12/15] bottom panel mouse listener --- src/test/java/test3/graphics/BottomPanel.java | 113 +++++++++++++++++- src/test/java/test3/graphics/ShipPainter.java | 2 +- .../java/test3/graphics/ShipPartPainter.java | 16 ++- 3 files changed, 127 insertions(+), 4 deletions(-) diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java index a534274..2456494 100644 --- a/src/test/java/test3/graphics/BottomPanel.java +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -3,9 +3,14 @@ import java.util.List; import java.awt.Color; import java.awt.Graphics; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; import org.jetbrains.annotations.NotNull; +import static consoleUtils.SimplePrinting.printLine; + import commonGraphics.panels.FixedHorizontalPanel; import test3.models.SpacecraftModule; import test3.player.Player; @@ -15,6 +20,8 @@ final class BottomPanel extends FixedHorizontalPanel { private static final int PANEL_HEIGHT = 300; private static final @NotNull Color TEXT_COLOR = Color.white; private final @NotNull Player player; + private final @NotNull BottomPanelMouseListener mouseListener; + private final @NotNull BottomPanelMouseMotionListener mouseMotionListener; // public BottomPanel(@NotNull Player player, @NotNull PanelPalette palette) { @@ -22,6 +29,10 @@ public BottomPanel(@NotNull Player player, @NotNull PanelPalette palette) { palette.getPanelBorder(), true, palette.getPanelDiagonals(), false); this.player = player; + mouseListener = new BottomPanelMouseListener(); + addMouseListener(mouseListener); + mouseMotionListener = new BottomPanelMouseMotionListener(); + addMouseMotionListener(mouseMotionListener); } // @@ -57,7 +68,7 @@ private void paintMode_shipyard(@NotNull Graphics g) { for (int i = 0; i < partInventory.size(); i++) { @NotNull SpacecraftModule part = partInventory.get(i); int partX = partDrawLocation[0] + partSeparation * i; - ShipPartPainter.paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}); + ShipPartPainter.paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}, mouseMotionListener.getLocation()); ShipPartPainter.paintPartInfo(g, part, TEXT_COLOR, new int [] {partX + textOffsetX, infoY}); } } @@ -67,4 +78,104 @@ private void paintMode_missions(@NotNull Graphics g) { g.setColor(TEXT_COLOR); g.drawString("Missions mode not ready yet...", 50, 50); } + + // + private static final class BottomPanelMouseListener implements MouseListener { + // + BottomPanelMouseListener() {} + + /** + * Invoked when a mouse button has been pressed on a component. + * + * @param e the event to be processed + */ + @Override + public void mousePressed(MouseEvent e) { + printLine("Mouse pressed at " + e.getX() + " x " + e.getY()); + } + + /** + * Invoked when a mouse button has been released on a component. + * + * @param e the event to be processed + */ + @Override + public void mouseReleased(MouseEvent e) { + printLine("Mouse released at " + e.getX() + " x " + e.getY()); + } + + /** + * Invoked when the mouse button has been clicked (pressed + * and released) on a component. + * + * @param e the event to be processed + */ + @Override + public void mouseClicked(MouseEvent e) {} + + /** + * Invoked when the mouse enters a component. + * + * @param e the event to be processed + */ + @Override + public void mouseEntered(MouseEvent e) {} + + /** + * Invoked when the mouse exits a component. + * + * @param e the event to be processed + */ + @Override + public void mouseExited(MouseEvent e) {} + } + + // + private static final class BottomPanelMouseMotionListener implements MouseMotionListener { + private final int @NotNull [] location; + + // + BottomPanelMouseMotionListener() { + location = new int[2]; + } + + // + public int @NotNull [] getLocation() { + return new int [] {location[0], location[1]}; + } + + private void setLocation(@NotNull MouseEvent e) { + location[0] = e.getX(); + location[1] = e.getY(); + } + + /** + * Invoked when the mouse cursor has been moved onto a component + * but no buttons have been pushed. + * + * @param e the event to be processed + */ + @Override + public void mouseMoved(@NotNull MouseEvent e) { + setLocation(e); + } + + /** + * Invoked when a mouse button is pressed on a component and then + * dragged. {@code MOUSE_DRAGGED} events will continue to be + * delivered to the component where the drag originated until the + * mouse button is released (regardless of whether the mouse position + * is within the bounds of the component). + *

+ * Due to platform-dependent Drag&Drop implementations, + * {@code MOUSE_DRAGGED} events may not be delivered during a native + * Drag&Drop operation. + * + * @param e the event to be processed + */ + @Override + public void mouseDragged(@NotNull MouseEvent e) { + setLocation(e); + } + } } \ No newline at end of file diff --git a/src/test/java/test3/graphics/ShipPainter.java b/src/test/java/test3/graphics/ShipPainter.java index facf541..92c74df 100644 --- a/src/test/java/test3/graphics/ShipPainter.java +++ b/src/test/java/test3/graphics/ShipPainter.java @@ -26,7 +26,7 @@ static void paintShip(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNul } private static void paintParts(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNull [] location) { - ShipPartPainter.paintShipPart(g, ship.getCore(), location); + ShipPartPainter.paintShipPart(g, ship.getCore(), location, new int[2]); } private static void paintOutline(@NotNull Graphics g, @NotNull Spacecraft ship, int @NotNull [] location) { diff --git a/src/test/java/test3/graphics/ShipPartPainter.java b/src/test/java/test3/graphics/ShipPartPainter.java index b0dc5c3..34b0ce5 100644 --- a/src/test/java/test3/graphics/ShipPartPainter.java +++ b/src/test/java/test3/graphics/ShipPartPainter.java @@ -15,13 +15,25 @@ final class ShipPartPainter { private static final int TEXT_HEIGHT = 15; //call this to paint part only - static void paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] paintCenter) { + static void paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] paintCenter, + int @NotNull [] mouseLocation) { int size = part.getSize(); int @NotNull [] drawStart = new int [] { paintCenter[0] - size / 2, paintCenter[1] - size / 2}; g.setColor(part.getColor()); - g.drawRect(drawStart[0], drawStart[1], size, size); + if (within(drawStart, size, mouseLocation)) { + g.fillRect(drawStart[0], drawStart[1], size, size); + } else { + g.drawRect(drawStart[0], drawStart[1], size, size); + } + } + + private static boolean within(int @NotNull [] drawStart, int size, int @NotNull [] target) { + boolean + withinX = (target[0] >= drawStart[0]) && (target[0] <= drawStart[0] + size), + withinY = (target[1] >= drawStart[1]) && (target[1] <= drawStart[1] + size); + return withinX && withinY; } //call this to paint part info only From 616a22142cb88bd5b8695ecc41d6cf007b4646c1 Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 20:36:37 +0300 Subject: [PATCH 13/15] part recognition --- src/test/java/test3/graphics/BottomPanel.java | 29 +++++++++++++++++-- .../java/test3/graphics/ShipPartPainter.java | 8 +++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java index 2456494..76631e3 100644 --- a/src/test/java/test3/graphics/BottomPanel.java +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -20,6 +20,8 @@ final class BottomPanel extends FixedHorizontalPanel { private static final int PANEL_HEIGHT = 300; private static final @NotNull Color TEXT_COLOR = Color.white; private final @NotNull Player player; + private int activePart; + @SuppressWarnings("FieldCanBeLocal") private final @NotNull BottomPanelMouseListener mouseListener; private final @NotNull BottomPanelMouseMotionListener mouseMotionListener; @@ -29,7 +31,8 @@ public BottomPanel(@NotNull Player player, @NotNull PanelPalette palette) { palette.getPanelBorder(), true, palette.getPanelDiagonals(), false); this.player = player; - mouseListener = new BottomPanelMouseListener(); + activePart = -1; + mouseListener = new BottomPanelMouseListener(this); addMouseListener(mouseListener); mouseMotionListener = new BottomPanelMouseMotionListener(); addMouseMotionListener(mouseMotionListener); @@ -65,14 +68,22 @@ private void paintMode_shipyard(@NotNull Graphics g) { partSeparation = 160, infoY = 180, textOffsetX = -40; @NotNull List<@NotNull SpacecraftModule> partInventory = player.getShipPartInventory(); + activePart = -1; //resets the active part for (int i = 0; i < partInventory.size(); i++) { @NotNull SpacecraftModule part = partInventory.get(i); int partX = partDrawLocation[0] + partSeparation * i; - ShipPartPainter.paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}, mouseMotionListener.getLocation()); + boolean active = ShipPartPainter.paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}, mouseMotionListener.getLocation()); ShipPartPainter.paintPartInfo(g, part, TEXT_COLOR, new int [] {partX + textOffsetX, infoY}); + if (active) { + activePart = i; + } } } + int getActivePart() { + return activePart; + } + private void paintMode_missions(@NotNull Graphics g) { setDrawDiagonals(true); g.setColor(TEXT_COLOR); @@ -81,8 +92,12 @@ private void paintMode_missions(@NotNull Graphics g) { // private static final class BottomPanelMouseListener implements MouseListener { + private final @NotNull BottomPanel panel; + // - BottomPanelMouseListener() {} + BottomPanelMouseListener(@NotNull BottomPanel panel) { + this.panel = panel; + } /** * Invoked when a mouse button has been pressed on a component. @@ -92,6 +107,10 @@ private static final class BottomPanelMouseListener implements MouseListener { @Override public void mousePressed(MouseEvent e) { printLine("Mouse pressed at " + e.getX() + " x " + e.getY()); + int activePart = panel.getActivePart(); + if (activePart >= 0) { + printLine("Part number " + activePart); + } } /** @@ -102,6 +121,10 @@ public void mousePressed(MouseEvent e) { @Override public void mouseReleased(MouseEvent e) { printLine("Mouse released at " + e.getX() + " x " + e.getY()); + int activePart = panel.getActivePart(); + if (activePart >= 0) { + printLine("Part number " + activePart); + } } /** diff --git a/src/test/java/test3/graphics/ShipPartPainter.java b/src/test/java/test3/graphics/ShipPartPainter.java index 34b0ce5..357542f 100644 --- a/src/test/java/test3/graphics/ShipPartPainter.java +++ b/src/test/java/test3/graphics/ShipPartPainter.java @@ -14,19 +14,21 @@ final class ShipPartPainter { private static final int TEXT_HEIGHT = 15; - //call this to paint part only - static void paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] paintCenter, + //call this to paint part only; returns within status (true, if mouse is within this part) + static boolean paintShipPart(@NotNull Graphics g, @NotNull SpacecraftModule part, int @NotNull [] paintCenter, int @NotNull [] mouseLocation) { int size = part.getSize(); int @NotNull [] drawStart = new int [] { paintCenter[0] - size / 2, paintCenter[1] - size / 2}; g.setColor(part.getColor()); - if (within(drawStart, size, mouseLocation)) { + boolean within = within(drawStart, size, mouseLocation); + if (within) { g.fillRect(drawStart[0], drawStart[1], size, size); } else { g.drawRect(drawStart[0], drawStart[1], size, size); } + return within; } private static boolean within(int @NotNull [] drawStart, int size, int @NotNull [] target) { From 0073d14074ac8eaf740dcf17294811ad0425452b Mon Sep 17 00:00:00 2001 From: Oskars AA <66279792+Krumuvecis@users.noreply.github.com> Date: Fri, 12 Apr 2024 20:58:03 +0300 Subject: [PATCH 14/15] ship creation --- src/test/java/test3/Test3.java | 38 +++++++++++-------- src/test/java/test3/graphics/BottomPanel.java | 10 +++++ .../models/WrongModuleTypeException.java | 9 +++++ src/test/java/test3/player/Player.java | 19 +++++++++- 4 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 src/test/java/test3/models/WrongModuleTypeException.java diff --git a/src/test/java/test3/Test3.java b/src/test/java/test3/Test3.java index d2dfcaf..9de26e3 100644 --- a/src/test/java/test3/Test3.java +++ b/src/test/java/test3/Test3.java @@ -12,29 +12,37 @@ // public class Test3 extends GraphicalTest { private static final int STARTING_MONEY = 1000; - private static final @NotNull Player PLAYER = new Player(STARTING_MONEY); // public static void main(String[] args) { + new Test3(new Player(STARTING_MONEY)); + } + + // + private Test3(@NotNull Player player) { + super(new Window(player)); + addInitialShipParts(player); + addInitialShips(player); + } + + private void addInitialShipParts(@NotNull Player player) { //adds 2 structural modules - PLAYER.addShipPart(new StructuralModule()); - PLAYER.addShipPart(new StructuralModule()); + player.addShipPart(new StructuralModule()); + player.addShipPart(new StructuralModule()); //adds 3 test modules - PLAYER.addShipPart(new TestModule()); - PLAYER.addShipPart(new TestModule()); - PLAYER.addShipPart(new TestModule()); + player.addShipPart(new TestModule()); + player.addShipPart(new TestModule()); + player.addShipPart(new TestModule()); - //adds 2 ships - PLAYER.addShip(new Spacecraft("Ship 1", new StructuralModule())); - PLAYER.addShip(new Spacecraft("Ship 2", new StructuralModule())); - - //creates the player with some starting money - new Test3(PLAYER); + //adds 2 more structural modules + player.addShipPart(new StructuralModule()); + player.addShipPart(new StructuralModule()); } - // - private Test3(@NotNull Player player) { - super(new Window(player)); + private void addInitialShips(@NotNull Player player) { + //adds 2 ships + player.addShip(new Spacecraft("Ship 1", new StructuralModule())); + player.addShip(new Spacecraft("Ship 2", new StructuralModule())); } } \ No newline at end of file diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java index 76631e3..5993985 100644 --- a/src/test/java/test3/graphics/BottomPanel.java +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -12,6 +12,7 @@ import static consoleUtils.SimplePrinting.printLine; import commonGraphics.panels.FixedHorizontalPanel; +import test3.models.WrongModuleTypeException; import test3.models.SpacecraftModule; import test3.player.Player; @@ -38,6 +39,10 @@ public BottomPanel(@NotNull Player player, @NotNull PanelPalette palette) { addMouseMotionListener(mouseMotionListener); } + @NotNull Player getPlayer() { + return player; + } + // @Override public void mainPaint(@NotNull Graphics g) { @@ -124,6 +129,11 @@ public void mouseReleased(MouseEvent e) { int activePart = panel.getActivePart(); if (activePart >= 0) { printLine("Part number " + activePart); + try { + panel.getPlayer().createNewShip(activePart); + } catch (@NotNull WrongModuleTypeException exception) { + printLine(exception.getMessage()); + } } } diff --git a/src/test/java/test3/models/WrongModuleTypeException.java b/src/test/java/test3/models/WrongModuleTypeException.java new file mode 100644 index 0000000..677eb56 --- /dev/null +++ b/src/test/java/test3/models/WrongModuleTypeException.java @@ -0,0 +1,9 @@ +package test3.models; + +// +public final class WrongModuleTypeException extends Exception { + // + public WrongModuleTypeException() { + super("Wrong model type exception"); + } +} \ No newline at end of file diff --git a/src/test/java/test3/player/Player.java b/src/test/java/test3/player/Player.java index e42ce91..3b737b3 100644 --- a/src/test/java/test3/player/Player.java +++ b/src/test/java/test3/player/Player.java @@ -5,8 +5,10 @@ import org.jetbrains.annotations.NotNull; -import test3.models.Spacecraft; +import test3.models.WrongModuleTypeException; import test3.models.SpacecraftModule; +import test3.models.StructuralModule; +import test3.models.Spacecraft; // public class Player { @@ -47,6 +49,21 @@ public void addShipPart(@NotNull SpacecraftModule part) { shipPartInventory.add(part); } + // + public void createNewShip(int partInventoryIndex) throws WrongModuleTypeException { + try { + @NotNull SpacecraftModule part = shipPartInventory.get(partInventoryIndex); + if (part instanceof StructuralModule core) { + addShip(new Spacecraft("New ship", core)); + shipPartInventory.remove(partInventoryIndex); + } else { + throw new WrongModuleTypeException(); + } + } catch (@NotNull IndexOutOfBoundsException e) { + throw new RuntimeException(e); + } + } + // for graphical purposes public final @NotNull PaintMode getPaintMode() { return paintMode; From a9c11f4f93f45fa8de695f9b05b959401a0a8de4 Mon Sep 17 00:00:00 2001 From: thelordgod Date: Sat, 13 Apr 2024 01:22:58 +0300 Subject: [PATCH 15/15] hello from zorik --- .../commonGraphics/AbstractKeyListener.java | 2 +- src/test/java/test3/graphics/BottomPanel.java | 64 ++++++++++++----- src/test/java/test3/graphics/MainPanel.java | 72 +++++++++++++++++++ .../java/test3/graphics/MyKeyListener.java | 6 +- .../java/test3/models/ModuleConnection.java | 44 ++++++++++++ src/test/java/test3/models/Spacecraft.java | 13 ++++ .../java/test3/models/SpacecraftModule.java | 50 +++++++++++++ .../java/test3/models/StructuralModule.java | 4 ++ src/test/java/test3/models/TestModule.java | 2 +- src/test/java/test3/player/Player.java | 32 +++++++-- src/test/java/test3/player/ShipyardMode.java | 6 ++ 11 files changed, 270 insertions(+), 25 deletions(-) create mode 100644 src/test/java/test3/models/ModuleConnection.java create mode 100644 src/test/java/test3/player/ShipyardMode.java diff --git a/src/test/java/commonGraphics/AbstractKeyListener.java b/src/test/java/commonGraphics/AbstractKeyListener.java index 3021ef0..349c892 100644 --- a/src/test/java/commonGraphics/AbstractKeyListener.java +++ b/src/test/java/commonGraphics/AbstractKeyListener.java @@ -102,7 +102,7 @@ public UndefinedKeyActionException(int keyCode) { // private static final class KeyActionUpdater extends AbstractUpdater { - private static final long UPDATE_DELAY = 20; // ms + private static final long UPDATE_DELAY = 16; // ms private final @NotNull AbstractKeyListener listener; // diff --git a/src/test/java/test3/graphics/BottomPanel.java b/src/test/java/test3/graphics/BottomPanel.java index 5993985..c4d3b59 100644 --- a/src/test/java/test3/graphics/BottomPanel.java +++ b/src/test/java/test3/graphics/BottomPanel.java @@ -1,5 +1,6 @@ package test3.graphics; +import java.util.ArrayList; import java.util.List; import java.awt.Color; import java.awt.Graphics; @@ -12,6 +13,7 @@ import static consoleUtils.SimplePrinting.printLine; import commonGraphics.panels.FixedHorizontalPanel; +import test3.models.TestModule; import test3.models.WrongModuleTypeException; import test3.models.SpacecraftModule; import test3.player.Player; @@ -21,7 +23,7 @@ final class BottomPanel extends FixedHorizontalPanel { private static final int PANEL_HEIGHT = 300; private static final @NotNull Color TEXT_COLOR = Color.white; private final @NotNull Player player; - private int activePart; + private int hoveredPart; @SuppressWarnings("FieldCanBeLocal") private final @NotNull BottomPanelMouseListener mouseListener; private final @NotNull BottomPanelMouseMotionListener mouseMotionListener; @@ -32,7 +34,7 @@ public BottomPanel(@NotNull Player player, @NotNull PanelPalette palette) { palette.getPanelBorder(), true, palette.getPanelDiagonals(), false); this.player = player; - activePart = -1; + hoveredPart = -1; mouseListener = new BottomPanelMouseListener(this); addMouseListener(mouseListener); mouseMotionListener = new BottomPanelMouseMotionListener(); @@ -73,20 +75,41 @@ private void paintMode_shipyard(@NotNull Graphics g) { partSeparation = 160, infoY = 180, textOffsetX = -40; @NotNull List<@NotNull SpacecraftModule> partInventory = player.getShipPartInventory(); - activePart = -1; //resets the active part + hoveredPart = -1; //resets the hovered part + partInventory = filterParts(partInventory); for (int i = 0; i < partInventory.size(); i++) { @NotNull SpacecraftModule part = partInventory.get(i); int partX = partDrawLocation[0] + partSeparation * i; boolean active = ShipPartPainter.paintShipPart(g, part, new int [] {partX, partDrawLocation[1]}, mouseMotionListener.getLocation()); ShipPartPainter.paintPartInfo(g, part, TEXT_COLOR, new int [] {partX + textOffsetX, infoY}); if (active) { - activePart = i; + hoveredPart = i; } } } - int getActivePart() { - return activePart; + private @NotNull List<@NotNull SpacecraftModule> filterParts(@NotNull List<@NotNull SpacecraftModule> partInventory) { + @NotNull List<@NotNull SpacecraftModule> filteredParts = new ArrayList<>(); + for (@NotNull SpacecraftModule part : partInventory) { + switch (player.getShipyardMode()) { + case CREATE_SHIP -> { + if (part instanceof TestModule) { + continue; + } + } + case ADD_MODULE -> { + } + default -> { + throw new RuntimeException("Undefined ShipyardMode"); + } + } + filteredParts.add(part); + } + return filteredParts; + } + + int getHoveredPart() { + return hoveredPart; } private void paintMode_missions(@NotNull Graphics g) { @@ -112,9 +135,9 @@ private static final class BottomPanelMouseListener implements MouseListener { @Override public void mousePressed(MouseEvent e) { printLine("Mouse pressed at " + e.getX() + " x " + e.getY()); - int activePart = panel.getActivePart(); - if (activePart >= 0) { - printLine("Part number " + activePart); + int hoveredPart = panel.getHoveredPart(); + if (hoveredPart >= 0) { + printLine("Part number " + hoveredPart); } } @@ -126,14 +149,23 @@ public void mousePressed(MouseEvent e) { @Override public void mouseReleased(MouseEvent e) { printLine("Mouse released at " + e.getX() + " x " + e.getY()); - int activePart = panel.getActivePart(); - if (activePart >= 0) { - printLine("Part number " + activePart); - try { - panel.getPlayer().createNewShip(activePart); - } catch (@NotNull WrongModuleTypeException exception) { - printLine(exception.getMessage()); + int hoveredPart = panel.getHoveredPart(); + if (hoveredPart >= 0) { + printLine("Part number " + hoveredPart); + switch (panel.getPlayer().getShipyardMode()) { + case CREATE_SHIP -> { + try { + panel.getPlayer().createNewShip(hoveredPart); + } catch (@NotNull WrongModuleTypeException exception) { + printLine(exception.getMessage()); + } + } + case ADD_MODULE -> {} + default -> { + throw new RuntimeException("Undefined ShipyardMode"); + } } + } } diff --git a/src/test/java/test3/graphics/MainPanel.java b/src/test/java/test3/graphics/MainPanel.java index eb7cfef..32ca819 100644 --- a/src/test/java/test3/graphics/MainPanel.java +++ b/src/test/java/test3/graphics/MainPanel.java @@ -1,5 +1,8 @@ package test3.graphics; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; import java.util.List; import java.util.ArrayList; import java.awt.Color; @@ -93,4 +96,73 @@ private void paintMode_missions(@NotNull Graphics g) { g.setColor(TEXT_COLOR); g.drawString("Missions mode not ready yet...", x, y); } + + private static final class MainPanelMouseListener implements MouseListener { + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseClicked(MouseEvent e) {} + + @Override + public void mouseEntered(MouseEvent e) {} + + @Override + public void mouseExited(MouseEvent e) {} + } + + private static final class MainPanelMouseMotionListener implements MouseMotionListener { + private final int @NotNull [] location; + + // + MainPanelMouseMotionListener() { + location = new int[2]; + } + + // + public int @NotNull [] getLocation() { + return new int [] {location[0], location[1]}; + } + + private void setLocation(@NotNull MouseEvent e) { + location[0] = e.getX(); + location[1] = e.getY(); + } + + /** + * Invoked when the mouse cursor has been moved onto a component + * but no buttons have been pushed. + * + * @param e the event to be processed + */ + @Override + public void mouseMoved(@NotNull MouseEvent e) { + setLocation(e); + } + + /** + * Invoked when a mouse button is pressed on a component and then + * dragged. {@code MOUSE_DRAGGED} events will continue to be + * delivered to the component where the drag originated until the + * mouse button is released (regardless of whether the mouse position + * is within the bounds of the component). + *

+ * Due to platform-dependent Drag&Drop implementations, + * {@code MOUSE_DRAGGED} events may not be delivered during a native + * Drag&Drop operation. + * + * @param e the event to be processed + */ + @Override + public void mouseDragged(@NotNull MouseEvent e) { + setLocation(e); + } + } } \ No newline at end of file diff --git a/src/test/java/test3/graphics/MyKeyListener.java b/src/test/java/test3/graphics/MyKeyListener.java index fbb72e0..55cdf7e 100644 --- a/src/test/java/test3/graphics/MyKeyListener.java +++ b/src/test/java/test3/graphics/MyKeyListener.java @@ -7,6 +7,7 @@ import commonGraphics.AbstractKeyListener; import test3.player.PaintMode; import test3.player.Player; +import test3.player.ShipyardMode; // final class MyKeyListener extends AbstractKeyListener { @@ -56,11 +57,10 @@ private void keyActionSwitch_byText_FlightMode(@NotNull String keyText) throws U } } - @SuppressWarnings("SwitchStatementWithTooFewBranches") private void keyActionSwitch_byText_ShipyardMode(@NotNull String keyText) throws UndefinedKeyActionException { switch (keyText) { - case "Space" -> printLine("A space bar has been pressed!"); - //check more shipyard-specific keys here + case "1" -> player.setShipyardMode(ShipyardMode.CREATE_SHIP); + case "2" -> player.setShipyardMode(ShipyardMode.ADD_MODULE); default -> throw new UndefinedKeyActionException(); } } diff --git a/src/test/java/test3/models/ModuleConnection.java b/src/test/java/test3/models/ModuleConnection.java new file mode 100644 index 0000000..6ee7900 --- /dev/null +++ b/src/test/java/test3/models/ModuleConnection.java @@ -0,0 +1,44 @@ +package test3.models; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class ModuleConnection { + private final @NotNull SpacecraftModule parentModule; + private @Nullable ModuleConnection tetheredConnection; + private boolean isParentConnection; + private final int @NotNull [] location; + private final double orientation; + + public ModuleConnection(@NotNull SpacecraftModule parentModule, int @NotNull [] location, double orientation) { + this.parentModule = parentModule; + this.tetheredConnection = null; + this.isParentConnection = false; + this.location = location; + this.orientation = orientation; + } + + public @NotNull SpacecraftModule getParentModule() { + return parentModule; + } + + public boolean getIsParentConnection() { + return isParentConnection; + } + + public void setIsParentConnection(boolean isParentConnection) { + this.isParentConnection = isParentConnection; + } + + public void setTetheredConnection(@Nullable ModuleConnection tetheredConnection) { + this.tetheredConnection = tetheredConnection; + } + + public @Nullable ModuleConnection getTetheredConnection() { + return tetheredConnection; + } + + public int @NotNull [] getLocation() { + return location; + } +} \ No newline at end of file diff --git a/src/test/java/test3/models/Spacecraft.java b/src/test/java/test3/models/Spacecraft.java index a269922..273c21f 100644 --- a/src/test/java/test3/models/Spacecraft.java +++ b/src/test/java/test3/models/Spacecraft.java @@ -1,6 +1,7 @@ package test3.models; import java.awt.Color; +import java.util.List; import org.jetbrains.annotations.NotNull; @@ -27,6 +28,15 @@ public Spacecraft(@NotNull String name, @NotNull StructuralModule core) { return core; } + // + public @NotNull List<@NotNull ModuleConnection> getFreeConnections() { + @NotNull List<@NotNull ModuleConnection> freeConnections = core.getFreeConnections(); + + + + return freeConnections; + } + //gets the total size of the ship public int getSize() { //TODO: improve this and remove the "+10" @@ -37,4 +47,7 @@ public int getSize() { public @NotNull Color getColor() { return color; } + + public void addModule(SpacecraftModule part) { + } } \ No newline at end of file diff --git a/src/test/java/test3/models/SpacecraftModule.java b/src/test/java/test3/models/SpacecraftModule.java index dbb6a5c..bfd48dc 100644 --- a/src/test/java/test3/models/SpacecraftModule.java +++ b/src/test/java/test3/models/SpacecraftModule.java @@ -1,20 +1,25 @@ package test3.models; import java.awt.Color; +import java.util.ArrayList; +import java.util.List; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; // public abstract class SpacecraftModule { private final @NotNull String name; private final int size; private final @NotNull Color color; // for graphical purposes + private @NotNull List<@NotNull ModuleConnection> connections; // protected SpacecraftModule(@NotNull String name, int size, @NotNull Color color) { this.name = name; this.size = size; this.color = color; + this.connections = new ArrayList<>(); } // @@ -31,4 +36,49 @@ public int getSize() { public @NotNull Color getColor() { return color; } + + public void addConnection(@NotNull ModuleConnection connection){ + connections.add(connection); + } + + // + public @NotNull List<@NotNull ModuleConnection> getConnections(){ + return connections; + } + + // + public @Nullable ModuleConnection getParentConnection() { + for (@NotNull ModuleConnection connection : connections) { + if (connection.getIsParentConnection()) { + return connection; + } + } + return null; + } + + // + public @NotNull List<@NotNull ModuleConnection> getFreeConnections() { + @NotNull List<@NotNull ModuleConnection> freeConnections = new ArrayList<>(); + for (@NotNull ModuleConnection connection : connections) { + if (connection.getTetheredConnection() == null) { + freeConnections.add(connection); + } + } + + return freeConnections; + } + + // + public @NotNull List<@NotNull ModuleConnection> getOccupiedConnections(boolean getStructuralConnections) { + @NotNull List<@NotNull ModuleConnection> occupiedConnections = new ArrayList<>(); + for (@NotNull ModuleConnection connection : connections) { + @Nullable ModuleConnection tetheredConnection = connection.getTetheredConnection(); + if (tetheredConnection != null && (!getStructuralConnections || (!connection.getIsParentConnection() && + tetheredConnection.getParentModule() instanceof StructuralModule))) { + occupiedConnections.add(connection); + } + } + return occupiedConnections; + } + } \ No newline at end of file diff --git a/src/test/java/test3/models/StructuralModule.java b/src/test/java/test3/models/StructuralModule.java index f1a7ce0..7b4c5f4 100644 --- a/src/test/java/test3/models/StructuralModule.java +++ b/src/test/java/test3/models/StructuralModule.java @@ -1,6 +1,10 @@ package test3.models; +import org.jetbrains.annotations.NotNull; + import java.awt.Color; +import java.util.ArrayList; +import java.util.List; // public class StructuralModule extends SpacecraftModule { diff --git a/src/test/java/test3/models/TestModule.java b/src/test/java/test3/models/TestModule.java index b605be3..5f4a819 100644 --- a/src/test/java/test3/models/TestModule.java +++ b/src/test/java/test3/models/TestModule.java @@ -6,6 +6,6 @@ public class TestModule extends SpacecraftModule { // public TestModule() { - super("Test module", 50, Color.orange); + super("Test module", 50, Color.orange, connections); } } \ No newline at end of file diff --git a/src/test/java/test3/player/Player.java b/src/test/java/test3/player/Player.java index 3b737b3..f5bc101 100644 --- a/src/test/java/test3/player/Player.java +++ b/src/test/java/test3/player/Player.java @@ -5,10 +5,9 @@ import org.jetbrains.annotations.NotNull; -import test3.models.WrongModuleTypeException; -import test3.models.SpacecraftModule; -import test3.models.StructuralModule; -import test3.models.Spacecraft; +import test3.models.*; + +import static consoleUtils.SimplePrinting.printLine; // public class Player { @@ -16,6 +15,8 @@ public class Player { private final @NotNull List<@NotNull Spacecraft> ownedShips; private final @NotNull List<@NotNull SpacecraftModule> shipPartInventory; private @NotNull PaintMode paintMode; // for graphical purposes + private @NotNull ShipyardMode shipyardMode; // for graphical purposes + private int selectedPartIndex; // for shipyard purposes // public Player(int money) { @@ -23,6 +24,7 @@ public Player(int money) { ownedShips = new ArrayList<>(); shipPartInventory = new ArrayList<>(); paintMode = PaintMode.SHIPYARD; + shipyardMode = ShipyardMode.CREATE_SHIP; } public @NotNull Wallet getWallet() { @@ -64,13 +66,35 @@ public void createNewShip(int partInventoryIndex) throws WrongModuleTypeExceptio } } + // + public void addPartToConnection(@NotNull ModuleConnection connection, int partInventoryIndex) { + try { + @NotNull SpacecraftModule part = shipPartInventory.get(partInventoryIndex); + connection.setTetheredConnection(part); + shipPartInventory.remove(partInventoryIndex); + } catch (@NotNull IndexOutOfBoundsException e) { + throw new RuntimeException(e); + } + } + // for graphical purposes public final @NotNull PaintMode getPaintMode() { return paintMode; } + // for graphical purposes + public final @NotNull ShipyardMode getShipyardMode() { + return shipyardMode; + } + // for graphical purposes public void setPaintMode(@NotNull PaintMode paintMode) { this.paintMode = paintMode; } + + // for graphical purposes + public void setShipyardMode(@NotNull ShipyardMode shipyardMode) { + printLine("Shipyard mode set to " + shipyardMode.toString()); + this.shipyardMode = shipyardMode; + } } \ No newline at end of file diff --git a/src/test/java/test3/player/ShipyardMode.java b/src/test/java/test3/player/ShipyardMode.java new file mode 100644 index 0000000..cc19d85 --- /dev/null +++ b/src/test/java/test3/player/ShipyardMode.java @@ -0,0 +1,6 @@ +package test3.player; + +public enum ShipyardMode { + CREATE_SHIP, + ADD_MODULE +}