From 6c478bb1a61d9d08a71928de83e821f63fb7b93d Mon Sep 17 00:00:00 2001 From: rozenkranzz Date: Wed, 5 Feb 2025 19:36:28 +0400 Subject: [PATCH 1/3] LabWork01 is completed --- Liner_Advanced/Liner_Advanced.iml | 1 + Liner_Advanced/res/icons8-arrow-down-60.png | Bin 0 -> 387 bytes Liner_Advanced/res/icons8-arrow-left-60.png | Bin 0 -> 332 bytes Liner_Advanced/res/icons8-arrow-right-60.png | Bin 0 -> 294 bytes Liner_Advanced/res/icons8-arrow-up-60.png | Bin 0 -> 372 bytes .../src/projectliner/DeckDrawing.java | 53 +++++ Liner_Advanced/src/projectliner/DeckEnum.java | 17 ++ .../src/projectliner/DirectionType.java | 8 + .../src/projectliner/DrawingLiner.java | 152 +++++++++++++++ .../src/projectliner/FormLiner.java | 184 ++++++++++++++++++ .../src/projectliner/LinerEntity.java | 70 +++++++ .../src/projectliner/LinerEntityType.java | 8 + 12 files changed, 493 insertions(+) create mode 100644 Liner_Advanced/res/icons8-arrow-down-60.png create mode 100644 Liner_Advanced/res/icons8-arrow-left-60.png create mode 100644 Liner_Advanced/res/icons8-arrow-right-60.png create mode 100644 Liner_Advanced/res/icons8-arrow-up-60.png create mode 100644 Liner_Advanced/src/projectliner/DeckDrawing.java create mode 100644 Liner_Advanced/src/projectliner/DeckEnum.java create mode 100644 Liner_Advanced/src/projectliner/DirectionType.java create mode 100644 Liner_Advanced/src/projectliner/DrawingLiner.java create mode 100644 Liner_Advanced/src/projectliner/FormLiner.java create mode 100644 Liner_Advanced/src/projectliner/LinerEntity.java create mode 100644 Liner_Advanced/src/projectliner/LinerEntityType.java diff --git a/Liner_Advanced/Liner_Advanced.iml b/Liner_Advanced/Liner_Advanced.iml index c90834f..17ba59d 100644 --- a/Liner_Advanced/Liner_Advanced.iml +++ b/Liner_Advanced/Liner_Advanced.iml @@ -3,6 +3,7 @@ + diff --git a/Liner_Advanced/res/icons8-arrow-down-60.png b/Liner_Advanced/res/icons8-arrow-down-60.png new file mode 100644 index 0000000000000000000000000000000000000000..0382f018b4c7de01b547989ecbe6bdbe2cba65ef GIT binary patch literal 387 zcmV-}0et?6P)P!vWF#lNYJ#F4rJ7vL(is}LMH)CIT>7vTyV zxDMyHGZj&M5=cQpseNgh`|k5Q@J&+=d1sI&AcRsNtlB3uR})8~yHPnGK)*aXq&$2Cjhj zOqn6pd_{2YacAEgWPk5?OyR8_M|H@30*8#uW&6V(!Z$cEb&$UElJ$^wmmO2O7}+tT zi<8}?bg{CVkS<;s?7#>ehc4mfipW002ovPDHLkV1g5Fq%8md literal 0 HcmV?d00001 diff --git a/Liner_Advanced/res/icons8-arrow-left-60.png b/Liner_Advanced/res/icons8-arrow-left-60.png new file mode 100644 index 0000000000000000000000000000000000000000..aa76943c0d386ac3fda7bf400299ef2acad244ad GIT binary patch literal 332 zcmeAS@N?(olHy`uVBq!ia0vp^HXzKw1|+Ti+$;i8oCO|{#S9GG!XV7ZFl&wkQ1G9p zi(^Q|oVPc9gD$a63G&ihIwfdTW0V8Cx^JUw0jroqKHHqeMRTlobzeLryls|U--cG#f{JES zc0SXH;0FSYrp$a3BZMDJXgDh;BeXHHIbA+s_8jI7OvmIl^cOR^#cjBnW0Sc0<<&&> zwhgXlP8a`X?-rRM|L(()+sbd>EluUztG02@MfZdmT<5-K>||l9`RRV??D50PU%WZE zGDG-*$QH>0om(OnY`J_f?Pi>H?PlC{r4~CQ_W4xbd0AUoY;pWzYsK!Dt{OmB1D!5% bzl>QogTe~DWM4f)v|+C literal 0 HcmV?d00001 diff --git a/Liner_Advanced/res/icons8-arrow-right-60.png b/Liner_Advanced/res/icons8-arrow-right-60.png new file mode 100644 index 0000000000000000000000000000000000000000..6aabd5d8f9dce2ac4b370065d22f10ef4db26f16 GIT binary patch literal 294 zcmeAS@N?(olHy`uVBq!ia0vp^HXzKw1|+Ti+$;i8oCO|{#S9GG!XV7ZFl&wkQ1FJQ zi(^Q|oVPcvxef)0xL&knEM!W0eSt-KtI-1CO&!Lc<1>q0I}#=;u~k-{wB*j4yL+<8 z&xV;GaFd~D)x-@9^RBKDEMR+-&BdmdmDV0nz+-83Ws$Qkj8IPfBBIe)-OXHX>3aJ{ zFxzSMZaw1&tIRLD&8?H&jyI$&75kI%$?opgNqo!xdPbP-T)JK|W@i1Fwl+Smjrqn8 zRtWtzWc+r(Rnbg*|FOK6I|I60b;Y(uTuBP-1~ankjKBU`xTtgIt@oF<7n-Jlfy!5= YZ!cdfuu2qp0sY9}>FVdQ&MBb@08f>2iU0rr literal 0 HcmV?d00001 diff --git a/Liner_Advanced/res/icons8-arrow-up-60.png b/Liner_Advanced/res/icons8-arrow-up-60.png new file mode 100644 index 0000000000000000000000000000000000000000..bc597829b9d50663565e6bb949ae76fe31c79fa7 GIT binary patch literal 372 zcmeAS@N?(olHy`uVBq!ia0vp^HXzKw1|+Ti+$;i8oCO|{#S9GG!XV7ZFl&wk0|TR) zr;B4q#hkaZ4858i1>EG5E`(X{R**ZendO;)S=h8`az~{rTIU?83*uU2A>^UZ`Xl}8 z`Tf?j-yht<-G4V+XSYI&M5A!Rv56ZP!VX9m_&=BUztT%Rk}TQ%Ui|U zy<0?2EStW~{=~AW+v-m&o4idh<+8`^j!kF1Z!@OMp1O_sl$m<2@Ch^JT;Wq;)8j%W z+&$;q`+H6Jq}cQ71*S_synJRSEt-Dr$eqwhap_-NRMQR|J+OwWbshUP>*a-e0)9+- zvZLi9UwdCkQVj3nHfLV-V^ pictureWidth - drawingLinerWidth) { + x = pictureWidth - drawingLinerWidth; + } + if (y < 0) { + y = 0; + } else if (y > pictureHeight - drawingLinerHeight) { + y = pictureHeight - drawingLinerHeight; + } + startPosX = x; + startPosY = y; + } + + public boolean moveTransport(DirectionType direction) { + if (linerEntity == null || + startPosX == null || startPosY == null || + pictureWidth == null || pictureHeight == null) { + return false; + } + switch (direction) { + case LEFT: + if (startPosX - linerEntity.getStep() > 0) { + startPosX -= (int) linerEntity.getStep(); + } + break; + case UP: + if (startPosY - linerEntity.getStep() > 0) { + startPosY -= (int) linerEntity.getStep(); + } + break; + case RIGHT: + if (startPosX + linerEntity.getStep() < pictureWidth - drawingLinerWidth) { + startPosX += (int) linerEntity.getStep(); + } + break; + case DOWN: + if (startPosY + linerEntity.getStep() < pictureHeight - drawingLinerHeight) { + startPosY += (int) linerEntity.getStep(); + } + break; + default: + return false; + } + return true; + } + + public void setDeckNum(int num) { + deckDrawing.setDeckEnum(num); + } + + public void drawTransport(Graphics g) { + if (linerEntity == null || startPosX == null || startPosY == null) + return; + + int x = startPosX; + int y = startPosY; + + Graphics2D g2d = (Graphics2D) g; + + // Define colors + Color bodyColor = linerEntity.getPrimaryColor(); + Color deckColor = linerEntity.getSecondaryColor(); + Color borderColor = Color.BLACK; + + // Body (hull) + Point[] hullPoints = { + new Point(x + 20, y + 60), // bottom left + new Point(x + 120, y + 60), // bottom right + new Point(x + 140, y + 30), // top right + new Point(x, y + 30) // top left + }; + + g2d.setColor(bodyColor); + g2d.fillPolygon(new int[]{hullPoints[0].x, hullPoints[1].x, + hullPoints[2].x, hullPoints[3].x}, + new int[]{hullPoints[0].y, hullPoints[1].y, + hullPoints[2].y, hullPoints[3].y}, 4); + g2d.setColor(borderColor); + g2d.drawPolygon(new int[]{hullPoints[0].x, hullPoints[1].x, + hullPoints[2].x, hullPoints[3].x}, + new int[]{hullPoints[0].y, hullPoints[1].y, + hullPoints[2].y, hullPoints[3].y}, 4); + +// // First deck +// g2d.setColor(deckColor); +// g2d.fillRect(x + 30, y + 10, 100, 10); +// g2d.setColor(borderColor); +// g2d.drawRect(x + 30, y + 10, 100, 10); + deckDrawing.drawDeck(x, y, borderColor, deckColor, g); + + // Additional drawing logic for pool and extra deck + if (linerEntity.hasPool()) { + g2d.setColor(Color.CYAN); + g2d.fillOval(x + 35, y + 15, 30, 10); + g2d.setColor(Color.BLACK); + g2d.drawOval(x + 35, y + 15, 30, 10); + } + +// if (linerEntity.hasExtraDeck()) { +// g2d.setColor(linerEntity.getSecondaryColor()); +// g2d.fillRect(x + 71, y + 1, 49, 9); +// g2d.setColor(Color.BLACK); +// g2d.drawRect(x + 70, y, 50, 10); +// } + } +} + diff --git a/Liner_Advanced/src/projectliner/FormLiner.java b/Liner_Advanced/src/projectliner/FormLiner.java new file mode 100644 index 0000000..1d88b98 --- /dev/null +++ b/Liner_Advanced/src/projectliner/FormLiner.java @@ -0,0 +1,184 @@ +package projectliner; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.image.BufferedImage; +import java.net.URL; +import java.util.Random; + +public class FormLiner extends JFrame { + private DrawingLiner drawingLiner; + private JPanel pictureBoxLiner; + private Timer moveTimer; + private DirectionType currentDirection; + private int numberOfDecks; + + public FormLiner() { + initializeComponent(); + } + + private void initializeComponent() { + pictureBoxLiner = new JPanel(); + pictureBoxLiner.setPreferredSize(new Dimension(900, 500)); + add(pictureBoxLiner, BorderLayout.CENTER); + + JButton buttonCreateLiner = new JButton("Create Liner"); + buttonCreateLiner.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + buttonCreateLiner_Click(e); + } + }); + + JButton buttonSetDecks = new JButton("Set Decks"); + buttonSetDecks.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + buttonSetDecks_Click(e); + } + }); + + // Create a panel for the bottom region with BorderLayout + JPanel bottomPanel = new JPanel(new BorderLayout()); + + // Create a panel with FlowLayout.LEFT for the "Create Liner" button + JPanel createLinerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); + createLinerPanel.add(buttonCreateLiner); + createLinerPanel.add(buttonSetDecks); + + // Add the "Create Liner" panel to the left side of the bottom panel + bottomPanel.add(createLinerPanel, BorderLayout.WEST); + + // Load arrow images using ClassLoader with the correct path + URL upIconURL = getClass().getClassLoader().getResource("icons8-arrow-up-60.png"); + URL downIconURL = getClass().getClassLoader().getResource("icons8-arrow-down-60.png"); + URL leftIconURL = getClass().getClassLoader().getResource("icons8-arrow-left-60.png"); + URL rightIconURL = getClass().getClassLoader().getResource("icons8-arrow-right-60.png"); + + ImageIcon upIcon = new ImageIcon(upIconURL); + ImageIcon downIcon = new ImageIcon(downIconURL); + ImageIcon leftIcon = new ImageIcon(leftIconURL); + ImageIcon rightIcon = new ImageIcon(rightIconURL); + + // Create buttons with arrow images + JButton buttonMoveUp = new JButton(upIcon); + buttonMoveUp.setName("buttonMoveUp"); + addMoveButtonListeners(buttonMoveUp, DirectionType.UP); + + JButton buttonMoveDown = new JButton(downIcon); + buttonMoveDown.setName("buttonMoveDown"); + addMoveButtonListeners(buttonMoveDown, DirectionType.DOWN); + + JButton buttonMoveLeft = new JButton(leftIcon); + buttonMoveLeft.setName("buttonMoveLeft"); + addMoveButtonListeners(buttonMoveLeft, DirectionType.LEFT); + + JButton buttonMoveRight = new JButton(rightIcon); + buttonMoveRight.setName("buttonMoveRight"); + addMoveButtonListeners(buttonMoveRight, DirectionType.RIGHT); + + // Create a panel for the arrow buttons + JPanel arrowPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); + arrowPanel.add(buttonMoveUp); + arrowPanel.add(buttonMoveDown); + arrowPanel.add(buttonMoveLeft); + arrowPanel.add(buttonMoveRight); + + bottomPanel.add(arrowPanel, BorderLayout.EAST); + add(bottomPanel, BorderLayout.SOUTH); + + moveTimer = new Timer(100, new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + moveTransport(currentDirection); + } + }); + + pack(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + private void addMoveButtonListeners(JButton button, DirectionType direction) { + button.addMouseListener(new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + currentDirection = direction; + moveTimer.start(); + } + + @Override + public void mouseReleased(MouseEvent e) { + moveTimer.stop(); + } + }); + } + + private void drawTransport() { + if (drawingLiner == null) { + return; + } + BufferedImage bitmap = new BufferedImage(pictureBoxLiner.getWidth(), + pictureBoxLiner.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics graphics = bitmap.getGraphics(); + + graphics.setColor(pictureBoxLiner.getBackground()); + graphics.fillRect(0, 0, bitmap.getWidth(), bitmap.getHeight()); + + drawingLiner.drawTransport(graphics); + pictureBoxLiner.getGraphics().drawImage(bitmap, 0, 0, null); + } + + private void buttonCreateLiner_Click(ActionEvent e) { + Random random = new Random(); + drawingLiner = new DrawingLiner(); + drawingLiner.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + LinerEntityType.CARGO, random.nextInt(9000) + 1000, + random.nextInt(90) + 10, random.nextBoolean(), random.nextBoolean()); + drawingLiner.setPictureSize(pictureBoxLiner.getWidth(), + pictureBoxLiner.getHeight()); + drawingLiner.setPosition(random.nextInt(pictureBoxLiner.getWidth()), + random.nextInt(pictureBoxLiner.getHeight())); + + drawingLiner.setDeckNum(numberOfDecks); + + drawTransport(); + } + + private void buttonSetDecks_Click(ActionEvent e) { + String input = JOptionPane.showInputDialog(this, "Enter number of decks:"); + try { + numberOfDecks = Integer.parseInt(input); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(this, "Invalid number format", "Error", JOptionPane.ERROR_MESSAGE); + } + } + + private void moveTransport(DirectionType direction) { + if (drawingLiner == null) { + return; + } + + boolean result = switch (direction) { + case UP -> drawingLiner.moveTransport(DirectionType.UP); + case DOWN -> drawingLiner.moveTransport(DirectionType.DOWN); + case LEFT -> drawingLiner.moveTransport(DirectionType.LEFT); + case RIGHT -> drawingLiner.moveTransport(DirectionType.RIGHT); + }; + + if (result) { + drawTransport(); + } + } + + public static void main(String[] args) { + new FormLiner(); + } +} + diff --git a/Liner_Advanced/src/projectliner/LinerEntity.java b/Liner_Advanced/src/projectliner/LinerEntity.java new file mode 100644 index 0000000..8b99c9d --- /dev/null +++ b/Liner_Advanced/src/projectliner/LinerEntity.java @@ -0,0 +1,70 @@ +package projectliner; + +import java.awt.Color; + +public class LinerEntity { + private int speed; + private double weight; + private Color primaryColor; + private Color secondaryColor; + private LinerEntityType type; + private int capacity; + private int maxPassengers; + private boolean hasExtraDeck; + private boolean hasPool; + + public void init(int speed, double weight, Color primaryColor, + Color secondaryColor, LinerEntityType type, + int capacity, int maxPassengers, + boolean hasExtraDeck, boolean hasPool) { + this.speed = speed; + this.weight = weight; + this.primaryColor = primaryColor; + this.secondaryColor = secondaryColor; + this.type = type; + this.capacity = capacity; + this.maxPassengers = maxPassengers; + this.hasExtraDeck = hasExtraDeck; + this.hasPool = hasPool; + } + + public int getSpeed() { + return speed; + } + + public double getWeight() { + return weight; + } + + public Color getPrimaryColor() { + return primaryColor; + } + + public Color getSecondaryColor() { + return secondaryColor; + } + + public LinerEntityType getType() { + return type; + } + + public int getCapacity() { + return capacity; + } + + public int getMaxPassengers() { + return maxPassengers; + } + + public boolean hasExtraDeck() { + return hasExtraDeck; + } + + public boolean hasPool() { + return hasPool; + } + + public double getStep() { + return speed / (weight / 100); + } +} diff --git a/Liner_Advanced/src/projectliner/LinerEntityType.java b/Liner_Advanced/src/projectliner/LinerEntityType.java new file mode 100644 index 0000000..d54736e --- /dev/null +++ b/Liner_Advanced/src/projectliner/LinerEntityType.java @@ -0,0 +1,8 @@ +package projectliner; + +public enum LinerEntityType { + PASSENGER, + CARGO, + MILITARY, + MIXED +} -- 2.25.1 From b9b074c80999580d8a69ba2bb9d347a1d2343262 Mon Sep 17 00:00:00 2001 From: rozenkranzz Date: Mon, 10 Feb 2025 09:31:20 +0400 Subject: [PATCH 2/3] some fixes again --- Liner_Advanced/src/projectliner/DrawingLiner.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Liner_Advanced/src/projectliner/DrawingLiner.java b/Liner_Advanced/src/projectliner/DrawingLiner.java index 4513a45..03a7bcc 100644 --- a/Liner_Advanced/src/projectliner/DrawingLiner.java +++ b/Liner_Advanced/src/projectliner/DrawingLiner.java @@ -126,11 +126,6 @@ public class DrawingLiner { new int[]{hullPoints[0].y, hullPoints[1].y, hullPoints[2].y, hullPoints[3].y}, 4); -// // First deck -// g2d.setColor(deckColor); -// g2d.fillRect(x + 30, y + 10, 100, 10); -// g2d.setColor(borderColor); -// g2d.drawRect(x + 30, y + 10, 100, 10); deckDrawing.drawDeck(x, y, borderColor, deckColor, g); // Additional drawing logic for pool and extra deck @@ -140,13 +135,6 @@ public class DrawingLiner { g2d.setColor(Color.BLACK); g2d.drawOval(x + 35, y + 15, 30, 10); } - -// if (linerEntity.hasExtraDeck()) { -// g2d.setColor(linerEntity.getSecondaryColor()); -// g2d.fillRect(x + 71, y + 1, 49, 9); -// g2d.setColor(Color.BLACK); -// g2d.drawRect(x + 70, y, 50, 10); -// } } } -- 2.25.1 From fbf7485bd1d444f298099de542d324299f8520aa Mon Sep 17 00:00:00 2001 From: rozenkranzz Date: Mon, 10 Feb 2025 11:01:08 +0400 Subject: [PATCH 3/3] updated constructor --- Liner_Advanced/src/projectliner/DrawingLiner.java | 3 ++- Liner_Advanced/src/projectliner/FormLiner.java | 4 +--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Liner_Advanced/src/projectliner/DrawingLiner.java b/Liner_Advanced/src/projectliner/DrawingLiner.java index 03a7bcc..3363b4b 100644 --- a/Liner_Advanced/src/projectliner/DrawingLiner.java +++ b/Liner_Advanced/src/projectliner/DrawingLiner.java @@ -17,11 +17,12 @@ public class DrawingLiner { public void init(int speed, double weight, Color primaryColor, Color secondaryColor, LinerEntityType type, int capacity, int maxPassengers, - boolean hasExtraDeck, boolean hasPool) { + boolean hasExtraDeck, boolean hasPool, int deckNum) { linerEntity = new LinerEntity(); deckDrawing = new DeckDrawing(); linerEntity.init(speed, weight, primaryColor, secondaryColor, type, capacity, maxPassengers, hasExtraDeck, hasPool); + deckDrawing.setDeckEnum(deckNum); pictureWidth = null; pictureHeight = null; startPosX = null; diff --git a/Liner_Advanced/src/projectliner/FormLiner.java b/Liner_Advanced/src/projectliner/FormLiner.java index 1d88b98..806de6a 100644 --- a/Liner_Advanced/src/projectliner/FormLiner.java +++ b/Liner_Advanced/src/projectliner/FormLiner.java @@ -140,14 +140,12 @@ public class FormLiner extends JFrame { new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), LinerEntityType.CARGO, random.nextInt(9000) + 1000, - random.nextInt(90) + 10, random.nextBoolean(), random.nextBoolean()); + random.nextInt(90) + 10, random.nextBoolean(), random.nextBoolean(), numberOfDecks); drawingLiner.setPictureSize(pictureBoxLiner.getWidth(), pictureBoxLiner.getHeight()); drawingLiner.setPosition(random.nextInt(pictureBoxLiner.getWidth()), random.nextInt(pictureBoxLiner.getHeight())); - drawingLiner.setDeckNum(numberOfDecks); - drawTransport(); } -- 2.25.1