From 533756d8147360017d28043685d11badb1e0f843 Mon Sep 17 00:00:00 2001 From: Nikita Potapov <47923521+nikita-potapov@users.noreply.github.com> Date: Mon, 14 Nov 2022 23:40:40 +0400 Subject: [PATCH 1/5] class EntityBoat --- EntityBoat.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 EntityBoat.java diff --git a/EntityBoat.java b/EntityBoat.java new file mode 100644 index 0000000..949e88e --- /dev/null +++ b/EntityBoat.java @@ -0,0 +1,16 @@ +import java.awt.*; +import java.util.Random; + +public class EntityBoat { + public int Speed; + public float Weight; + public Color BodyColor; + public float Step; + + public void Init(int speed, float weight, Color bodyColor) { + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; + Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight; + BodyColor = bodyColor; + } +} -- 2.25.1 From 024c9f863b26cbeb1f911b00bf4414293a8cc70e Mon Sep 17 00:00:00 2001 From: Nikita Potapov <47923521+nikita-potapov@users.noreply.github.com> Date: Mon, 14 Nov 2022 23:42:15 +0400 Subject: [PATCH 2/5] enum Direction --- Direction.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Direction.java diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..a641b80 --- /dev/null +++ b/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} -- 2.25.1 From b3a579a55d56fb61ad95433f5a814b98d0742ab0 Mon Sep 17 00:00:00 2001 From: Nikita Potapov <47923521+nikita-potapov@users.noreply.github.com> Date: Sun, 27 Nov 2022 11:31:05 +0400 Subject: [PATCH 3/5] gui form --- Resources/30px_arrow_down.png | Bin 0 -> 244 bytes Resources/30px_arrow_left.png | Bin 0 -> 249 bytes Resources/30px_arrow_right.png | Bin 0 -> 254 bytes Resources/30px_arrow_up.png | Bin 0 -> 257 bytes Direction.java => src/Direction.java | 2 + src/DrawingBoat.java | 100 +++++++++++++++++ EntityBoat.java => src/EntityBoat.java | 2 + src/FormBoat.form | 142 +++++++++++++++++++++++++ src/FormBoat.java | 18 ++++ src/Program.java | 16 +++ 10 files changed, 280 insertions(+) create mode 100644 Resources/30px_arrow_down.png create mode 100644 Resources/30px_arrow_left.png create mode 100644 Resources/30px_arrow_right.png create mode 100644 Resources/30px_arrow_up.png rename Direction.java => src/Direction.java (82%) create mode 100644 src/DrawingBoat.java rename EntityBoat.java => src/EntityBoat.java (96%) create mode 100644 src/FormBoat.form create mode 100644 src/FormBoat.java create mode 100644 src/Program.java diff --git a/Resources/30px_arrow_down.png b/Resources/30px_arrow_down.png new file mode 100644 index 0000000000000000000000000000000000000000..7aa32554361c5096853e1a67276f0da08b666977 GIT binary patch literal 244 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jloCO|{#S9EWB_ParFHODzD7ehi z#WBRA^X*kbp=JXf7Kd0h`vptS{Ev6>oNcbb%E+|MPpGHcASSz#`|J;%(5YsXCPlX| z`4_P%I7MiuZt2+bTU`Fdv4d8>&d1*W;u}9PPQGe`;i)O&%fk-l?_?tRPSx1g-h*@y$HPFQjp00i_>zopr0ChuOg8%>k literal 0 HcmV?d00001 diff --git a/Resources/30px_arrow_left.png b/Resources/30px_arrow_left.png new file mode 100644 index 0000000000000000000000000000000000000000..9a3dfa2d5b89d36919b575b8befd59eac504d75e GIT binary patch literal 249 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jloCO|{#S9EWB_ParFHODzD7f0w z#WBRA^X#>QybT5%tN{jj53Vl$?f>NBYU3JyHPf{jOaH1E9mr;8UB#fH)~4{?e9Nk~ z3vu7I*RJ}#=fg|0?SAIFMRy$+op|wSoo3oAN7LOKf9|`yP3?k*N4i|l6qD1snTuv$ zjQPIkG?%%2RWks*vIi`7u`I25Nb!YfkCWjxp-pybJbT)&htDnm{r-UW|LqcYO literal 0 HcmV?d00001 diff --git a/Resources/30px_arrow_right.png b/Resources/30px_arrow_right.png new file mode 100644 index 0000000000000000000000000000000000000000..0f06fd0640536a7d699730ab5c192deace71668e GIT binary patch literal 254 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jloCO|{#S9EWB_ParFHODzD7eAX z#WBRA^XxT4z6Jvh)_|-%jjI=bx1YFpb^3~CPt}aK?DfJkWxjYWSfKe_r#9)ulU*;T z1nJ*ey8C5qoP%vpb^!l2!HbX8m#pl6dg^NJ+Ush`CfkKd51qK8b$I46_qk>-%IEHM z?(vKBk5a#UdE2zhhn8$Ty`DMcsJ{i>))#S@ZE}gjVI~7y13T$wCnDw z&$r3~qp!cW(aU;qs`b8Ww3U7Dt`)1|``+zbapnATLGje8$_2ra-H>* zUI~?GXQRn!Gfhq}iHuuhtC_8P?QX(9kLP=sy=Fd8(=v6@0lJXE)78&qol`;+0Nwv% AVgLXD literal 0 HcmV?d00001 diff --git a/Direction.java b/src/Direction.java similarity index 82% rename from Direction.java rename to src/Direction.java index a641b80..5391974 100644 --- a/Direction.java +++ b/src/Direction.java @@ -1,3 +1,5 @@ +package src; + public enum Direction { Up, Down, diff --git a/src/DrawingBoat.java b/src/DrawingBoat.java new file mode 100644 index 0000000..1e59761 --- /dev/null +++ b/src/DrawingBoat.java @@ -0,0 +1,100 @@ +package src; + +import java.awt.*; + +public class DrawingBoat { + public EntityBoat Boat; + private float _startPosX; + private float _startPosY; + private int _pictureWidth = 0; + private int _pictureHeight = 0; + private final int _boatWidth = 100; + private final int _boatHeight = 40; + + public void Init(int speed, float weight, Color bodyColor) { + Boat = new EntityBoat(); + Boat.Init(speed, weight, bodyColor); + } + + public void SetPosition(int x, int y, int width, int height) { + if (x > 0 && x < width - _boatWidth && y > 0 && y < height - _boatHeight) { + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + } + + public void MoveTransport(Direction direction) { + if (_pictureWidth == 0 || _pictureHeight == 0) { + return; + } + switch (direction) { + case Right: + if (_startPosX + _boatWidth + Boat.Step < _pictureWidth) { + _startPosX += Boat.Step; + } + break; + case Left: + if (_startPosX - Boat.Step > 0) { + _startPosX -= Boat.Step; + } + break; + case Up: + if (_startPosY - Boat.Step > 0) { + _startPosY -= Boat.Step; + } + break; + case Down: + if (_startPosY + _boatHeight + Boat.Step < _pictureHeight) { + _startPosY += Boat.Step; + } + break; + } + } + + public void DrawTransport(Graphics g) + { + if (_startPosX < 0 || _startPosY < 0 + || _pictureHeight == 0 || _pictureWidth == 0) + { + return; + } +// todo доделать прорисовку + +// SolidBrush brush = new SolidBrush(Boat.BodyColor); +// +// PointF[] bodyPoints = new PointF[5]; +// bodyPoints[0] = new PointF(_startPosX, _startPosY); +// bodyPoints[1] = new PointF(_startPosX + _boatWidth - _boatWidth / 4, _startPosY); +// bodyPoints[2] = new PointF(_startPosX + _boatWidth, _startPosY + _boatHeight / 2); +// bodyPoints[3] = new PointF(_startPosX + _boatWidth - _boatWidth / 4, _startPosY + _boatHeight); +// bodyPoints[4] = new PointF(_startPosX, _startPosY + _boatHeight); +// +// // Отрисовка корпуса лодки +// g.FillPolygon(brush, bodyPoints); +// g.DrawPolygon(Pens.Black, bodyPoints); +// +// // Отрисовка головы лодки +// g.FillEllipse(Brushes.White, _startPosX + _boatWidth / 8, _startPosY + _boatHeight / 8, +// _boatWidth / 2, _boatHeight - _boatHeight / 4); +// g.DrawEllipse(Pens.Black, _startPosX + _boatWidth / 8, _startPosY + _boatHeight / 8, +// _boatWidth / 2, _boatHeight - _boatHeight / 4); + } + + public void ChangeBorders(int width, int height) { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _boatWidth || _pictureHeight <= _boatHeight) { + _pictureWidth = 0; + _pictureHeight = 0; + return; + } + if (_startPosX + _boatWidth > _pictureWidth) { + _startPosX = _pictureWidth - _boatWidth; + } + if (_startPosY + _boatHeight > _pictureHeight) { + _startPosY = _pictureHeight - _boatHeight; + } + } +} diff --git a/EntityBoat.java b/src/EntityBoat.java similarity index 96% rename from EntityBoat.java rename to src/EntityBoat.java index 949e88e..899cf03 100644 --- a/EntityBoat.java +++ b/src/EntityBoat.java @@ -1,3 +1,5 @@ +package src; + import java.awt.*; import java.util.Random; diff --git a/src/FormBoat.form b/src/FormBoat.form new file mode 100644 index 0000000..7dd7ac3 --- /dev/null +++ b/src/FormBoat.form @@ -0,0 +1,142 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormBoat.java b/src/FormBoat.java new file mode 100644 index 0000000..7e4b68a --- /dev/null +++ b/src/FormBoat.java @@ -0,0 +1,18 @@ +package src; + +import javax.swing.*; + +public class FormBoat { + JPanel PanelWrapper; + private JPanel PictureBox; + private JToolBar StatusStrip; + private JLabel StatusStripLabelSpeed; + private JLabel StatusStripLabelWeight; + private JLabel StatusStripLabelColor; + private JButton ButtonCreate; + private JPanel PanelButtonsMove; + private JButton ButtonDown; + private JButton ButtonUp; + private JButton ButtonLeft; + private JButton ButtonRight; +} diff --git a/src/Program.java b/src/Program.java new file mode 100644 index 0000000..d8d19a6 --- /dev/null +++ b/src/Program.java @@ -0,0 +1,16 @@ +package src; + +import javax.swing.*; + +public class Program { + public static void main(String[] args) { + JFrame.setDefaultLookAndFeelDecorated(false); + JFrame frame = new JFrame("Катамаран"); + frame.setContentPane(new FormBoat().PanelWrapper); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocation(500, 200); + frame.pack(); + frame.setSize(1000, 500); + frame.setVisible(true); + } +} -- 2.25.1 From 8f60ef0fb5d2a3c4efc7097ee2d2e921c9d2640d Mon Sep 17 00:00:00 2001 From: Nikita Potapov <47923521+nikita-potapov@users.noreply.github.com> Date: Sun, 27 Nov 2022 15:51:51 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D1=83=D0=B1=D1=80=D0=B0=D0=BB=20PanelMoveB?= =?UTF-8?q?utton,=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=BE=D1=82=D1=80=D0=B8=D1=81=D0=BE=D0=B2=D0=BA=D1=83?= =?UTF-8?q?=20=D0=BB=D0=BE=D0=B4=D0=BA=D0=B8,=20=D0=BF=D0=BE=D0=B4=D0=BA?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B8=D0=BB=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BA?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 3 +++ .idea/Catamarans.iml | 11 +++++++++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ 5 files changed, 34 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Catamarans.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Catamarans.iml b/.idea/Catamarans.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/.idea/Catamarans.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..f9b8061 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file -- 2.25.1 From 89e167e94971198d0b567c26516d87fc14fefb6f Mon Sep 17 00:00:00 2001 From: Nikita Potapov <47923521+nikita-potapov@users.noreply.github.com> Date: Sun, 27 Nov 2022 18:03:37 +0400 Subject: [PATCH 5/5] final LabWork01 --- src/Direction.java | 8 -- src/DrawingBoat.java | 215 +++++++++++++++++++++++++++---------- src/DrawingBoatPaddle.java | 49 +++++++++ src/EntityBoat.java | 9 +- src/EnumDirection.java | 9 ++ src/EnumPaddlesCount.java | 14 +++ src/FormBoat.form | 134 +++++++++++------------ src/FormBoat.java | 137 ++++++++++++++++++++++- 8 files changed, 436 insertions(+), 139 deletions(-) delete mode 100644 src/Direction.java create mode 100644 src/DrawingBoatPaddle.java create mode 100644 src/EnumDirection.java create mode 100644 src/EnumPaddlesCount.java diff --git a/src/Direction.java b/src/Direction.java deleted file mode 100644 index 5391974..0000000 --- a/src/Direction.java +++ /dev/null @@ -1,8 +0,0 @@ -package src; - -public enum Direction { - Up, - Down, - Left, - Right -} diff --git a/src/DrawingBoat.java b/src/DrawingBoat.java index 1e59761..447fd76 100644 --- a/src/DrawingBoat.java +++ b/src/DrawingBoat.java @@ -1,100 +1,199 @@ package src; import java.awt.*; +import java.util.Random; public class DrawingBoat { - public EntityBoat Boat; - private float _startPosX; - private float _startPosY; - private int _pictureWidth = 0; - private int _pictureHeight = 0; - private final int _boatWidth = 100; - private final int _boatHeight = 40; + // Объект-сущность лодки + public EntityBoat entityBoat; + // Объект отрисовки весел лодки + private DrawingBoatPaddle __drawingBoatPaddle; + // Координата X верхнего левого угла лодки + private float __startPosX; + // Координата Y верхнего левого угла лодки + private float __startPosY; + // Ширина отрисовки лодки + private final int __boatWidth = 100; + // Высота отрисовки лодки + private final int __boatHeight = 60; + // Ширина области отрисовки + private Integer __pictureWidth = null; + // Высота области отрисовки + private Integer __pictureHeight = null; + + // Инициализатор класса public void Init(int speed, float weight, Color bodyColor) { - Boat = new EntityBoat(); - Boat.Init(speed, weight, bodyColor); + entityBoat = new EntityBoat(); + entityBoat.Init(speed, weight, bodyColor); + __drawingBoatPaddle = new DrawingBoatPaddle(); + SetPaddlesCount(); } + // Метод для установки количества весел лодки + public void SetPaddlesCount() { + Random rnd = new Random(); + int paddlesCount = rnd.nextInt(1, 4); + __drawingBoatPaddle.SetEnumNumber(paddlesCount); + } + + // Метод установки позиции и размеров объекта public void SetPosition(int x, int y, int width, int height) { - if (x > 0 && x < width - _boatWidth && y > 0 && y < height - _boatHeight) { - _startPosX = x; - _startPosY = y; - _pictureWidth = width; - _pictureHeight = height; + if (x > 0 && x < width - __boatWidth && y > 0 && y < height - __boatHeight) { + __startPosX = x; + __startPosY = y; + __pictureWidth = width; + __pictureHeight = height; } } - public void MoveTransport(Direction direction) { - if (_pictureWidth == 0 || _pictureHeight == 0) { + // Метод перемешения обхекта + public void MoveTransport(EnumDirection enumDirection) { + // Если не установлен объект-сущность - выходим + if (entityBoat == null) { return; } - switch (direction) { + + // Если не назначены границы области отрисовки - выходим + if (__pictureWidth == null || __pictureHeight == null) { + return; + } + + // Проверяем возможность перемещения и перемещаем обхект по заданному направлению + switch (enumDirection) { case Right: - if (_startPosX + _boatWidth + Boat.Step < _pictureWidth) { - _startPosX += Boat.Step; + if (__startPosX + __boatWidth + entityBoat.Step() < __pictureWidth) { + __startPosX += entityBoat.Step(); } break; case Left: - if (_startPosX - Boat.Step > 0) { - _startPosX -= Boat.Step; + if (__startPosX - entityBoat.Step() > 0) { + __startPosX -= entityBoat.Step(); } break; case Up: - if (_startPosY - Boat.Step > 0) { - _startPosY -= Boat.Step; + if (__startPosY - entityBoat.Step() > 0) { + __startPosY -= entityBoat.Step(); } break; case Down: - if (_startPosY + _boatHeight + Boat.Step < _pictureHeight) { - _startPosY += Boat.Step; + if (__startPosY + __boatHeight + entityBoat.Step() < __pictureHeight) { + __startPosY += entityBoat.Step(); } break; } } - public void DrawTransport(Graphics g) - { - if (_startPosX < 0 || _startPosY < 0 - || _pictureHeight == 0 || _pictureWidth == 0) - { + // Метод отрисовки объекта + public void DrawTransport(Graphics g) { + // Если не установлен объект-сущность - выходим + if (entityBoat == null) { return; } -// todo доделать прорисовку -// SolidBrush brush = new SolidBrush(Boat.BodyColor); -// -// PointF[] bodyPoints = new PointF[5]; -// bodyPoints[0] = new PointF(_startPosX, _startPosY); -// bodyPoints[1] = new PointF(_startPosX + _boatWidth - _boatWidth / 4, _startPosY); -// bodyPoints[2] = new PointF(_startPosX + _boatWidth, _startPosY + _boatHeight / 2); -// bodyPoints[3] = new PointF(_startPosX + _boatWidth - _boatWidth / 4, _startPosY + _boatHeight); -// bodyPoints[4] = new PointF(_startPosX, _startPosY + _boatHeight); -// -// // Отрисовка корпуса лодки -// g.FillPolygon(brush, bodyPoints); -// g.DrawPolygon(Pens.Black, bodyPoints); -// -// // Отрисовка головы лодки -// g.FillEllipse(Brushes.White, _startPosX + _boatWidth / 8, _startPosY + _boatHeight / 8, -// _boatWidth / 2, _boatHeight - _boatHeight / 4); -// g.DrawEllipse(Pens.Black, _startPosX + _boatWidth / 8, _startPosY + _boatHeight / 8, -// _boatWidth / 2, _boatHeight - _boatHeight / 4); + // Если координаты не валидны или размер области отрисовки не установлен - выходим + if (__startPosX < 0 || __startPosY < 0 + || __pictureHeight == null || __pictureWidth == null) { + return; + } + + Graphics2D g2d = (Graphics2D) g; + + int height = __boatHeight - 30; + + // Промежуточные переменные, чтобы не писать каждый раз (int) + int startX = (int) __startPosX; + int startY = (int) __startPosY + 15; + + // Рисуем корпус лодки + // Задаем цвет корпуса + g2d.setColor(entityBoat.BodyColor); + + // Массив координат X для полигона корпуса лодки + int[] xPoints = new int[]{ + startX, + startX + __boatWidth - __boatWidth / 4, + startX + __boatWidth, + startX + __boatWidth - __boatWidth / 4, + startX + }; + + // Массив координат Y для полигона корпуса лодки + int[] yPoints = new int[]{ + startY, + startY, + startY + height / 2, + startY + height, + startY + height + }; + + // Заполняем полигон + g2d.fillPolygon(xPoints, yPoints, 5); + + // Рисуем окантовку + g2d.setColor(Color.BLACK); + g2d.drawPolygon(xPoints, yPoints, 5); + + // Рисуем палубу + // Левая дуга + g2d.drawArc( + startX + __boatWidth / 10, + startY + height / 5, + height - height / 5 - height / 5, + height - height / 5 - height / 5, + 90, 180 + ); + + // Правая дуга + g2d.drawArc( + startX + __boatWidth - __boatWidth / 4 - __boatWidth / 10 - height / 5, + startY + height / 5, + height - height / 5 - height / 5, + height - height / 5 - height / 5, + -90, 180 + ); + + // Верхняя линия + g2d.drawLine( + startX + __boatWidth / 10 + (height - height / 2) / 2, + startY + height / 5, + startX + __boatWidth - __boatWidth / 4 - __boatWidth / 10 - height / 5 + (height - height / 2) / 2, + startY + height / 5 + ); + + // Нижняя линия + g2d.drawLine( + startX + __boatWidth / 10 + (height - height / 2) / 2, + startY + height - height / 5, + startX + __boatWidth - __boatWidth / 4 - __boatWidth / 10 - height / 5 + (height - height / 2) / 2, + startY + height - height / 5 + ); + + // Отрисовка весел лодки + __drawingBoatPaddle.DrawBoatPaddles(g, entityBoat.BodyColor, __startPosX, startY, __boatWidth, height); } + // Метод изменения границ области отрисовки public void ChangeBorders(int width, int height) { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureWidth <= _boatWidth || _pictureHeight <= _boatHeight) { - _pictureWidth = 0; - _pictureHeight = 0; + __pictureWidth = width; + __pictureHeight = height; + + // Если новые размеры области отрисовки меньше, чем размеры отрисовки лодки, + // то обнуляем размеры области отрисовки - рисовать не можем + if (__pictureWidth <= __boatWidth || __pictureHeight <= __boatHeight) { + __pictureWidth = null; + __pictureHeight = null; return; } - if (_startPosX + _boatWidth > _pictureWidth) { - _startPosX = _pictureWidth - _boatWidth; + + // Если выходим за правую границу, то сдвигаемся влево + if (__startPosX + __boatWidth > __pictureWidth) { + __startPosX = __pictureWidth - __boatWidth; } - if (_startPosY + _boatHeight > _pictureHeight) { - _startPosY = _pictureHeight - _boatHeight; + + // Если выходим за нижнюю границу, то сдвигаемся вверх + if (__startPosY + __boatHeight > __pictureHeight) { + __startPosY = __pictureHeight - __boatHeight; } } } diff --git a/src/DrawingBoatPaddle.java b/src/DrawingBoatPaddle.java new file mode 100644 index 0000000..a5f2fe8 --- /dev/null +++ b/src/DrawingBoatPaddle.java @@ -0,0 +1,49 @@ +package src; + +import java.awt.*; + +public class DrawingBoatPaddle { + private EnumPaddlesCount __enumPaddlesCount; + private final BasicStroke __paddleStroke = new BasicStroke(3); + + public void SetEnumNumber(int paddlesCount) { + for (EnumPaddlesCount val : __enumPaddlesCount.values()) { + if (val.enumNumber == paddlesCount) { + __enumPaddlesCount = val; + return; + } + } + } + + public void DrawBoatPaddles(Graphics g, Color color, + float startPosX, float startPosY, int boatWidth, int boatHeight) { + int startX = (int) startPosX; + int startY = (int) startPosY; + + Graphics2D g2d = (Graphics2D) g; + // Задаем цвет как у лодки + g2d.setColor(color); + // Задаем толщину линии побольше + g2d.setStroke(__paddleStroke); + + // Промежуточная переменная, чтобы уменьшить расчеты + float t = boatWidth - (float) boatWidth / 4 - (float) boatWidth / 15; + + for (int i = 0; i < __enumPaddlesCount.enumNumber; i++) { + // Рисуем верхнее весло + g2d.drawLine( + (int) (startX + t - t / 3 * i), + startY, + (int) (startX + t - t / 3 * (i + 1)), + startY - 15 + ); + // Рисуем нижнее весло + g2d.drawLine( + (int) (startX + t - t / 3 * i), + startY + boatHeight, + (int) (startX + t - t / 3 * (i + 1)), + startY + boatHeight + 15 + ); + } + } +} diff --git a/src/EntityBoat.java b/src/EntityBoat.java index 899cf03..77152e8 100644 --- a/src/EntityBoat.java +++ b/src/EntityBoat.java @@ -4,10 +4,17 @@ import java.awt.*; import java.util.Random; public class EntityBoat { + // Скорость лодки public int Speed; + // Вес лодки public float Weight; + // Цвет корпуса public Color BodyColor; - public float Step; + + // Шаг перемещения + public float Step() { + return Speed * 100 / Weight; + } public void Init(int speed, float weight, Color bodyColor) { Random rnd = new Random(); diff --git a/src/EnumDirection.java b/src/EnumDirection.java new file mode 100644 index 0000000..45e77ea --- /dev/null +++ b/src/EnumDirection.java @@ -0,0 +1,9 @@ +package src; + +// Перечисление для направлений движения лодки +public enum EnumDirection { + Up, + Down, + Left, + Right +} diff --git a/src/EnumPaddlesCount.java b/src/EnumPaddlesCount.java new file mode 100644 index 0000000..a5da9e6 --- /dev/null +++ b/src/EnumPaddlesCount.java @@ -0,0 +1,14 @@ +package src; + +// Дополнительное перечисление количества весел +public enum EnumPaddlesCount { + One(1), + Two(2), + Three(3); + + public final int enumNumber; + + EnumPaddlesCount(int i) { + this.enumNumber = i; + } +} diff --git a/src/FormBoat.form b/src/FormBoat.form index 7dd7ac3..ef5a2d4 100644 --- a/src/FormBoat.form +++ b/src/FormBoat.form @@ -8,93 +8,29 @@ - + - + + + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -103,6 +39,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/FormBoat.java b/src/FormBoat.java index 7e4b68a..1ee812e 100644 --- a/src/FormBoat.java +++ b/src/FormBoat.java @@ -1,8 +1,18 @@ package src; import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; public class FormBoat { + protected DrawingBoat _drawingBoat = new DrawingBoat(); + JPanel PanelWrapper; private JPanel PictureBox; private JToolBar StatusStrip; @@ -10,9 +20,134 @@ public class FormBoat { private JLabel StatusStripLabelWeight; private JLabel StatusStripLabelColor; private JButton ButtonCreate; - private JPanel PanelButtonsMove; private JButton ButtonDown; private JButton ButtonUp; private JButton ButtonLeft; private JButton ButtonRight; + + private List __controls; + + // Конструктор формы + public FormBoat() { + // Инициализируем список элементов управления для их перерисовки + InitializeControlsRepaintList(); + + ButtonUp.setName("ButtonUp"); + ButtonDown.setName("ButtonDown"); + ButtonLeft.setName("ButtonLeft"); + ButtonRight.setName("ButtonRight"); + + // Обработчик нажатия кнопки "Создать" + ButtonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + // Создаем новый объект отрисовки лодки + _drawingBoat = new DrawingBoat(); + Random random = new Random(); + + // Инициализируем обхект отрисовки + _drawingBoat.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + + // Устанавливаем позицию и размеры объекта отрисовки + _drawingBoat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), + PictureBox.getWidth(), PictureBox.getHeight()); + + // Обновляем информацию в статусбаре + StatusStripLabelSpeed.setText("Скорость: " + _drawingBoat.entityBoat.Speed + " "); + StatusStripLabelWeight.setText("Вес: " + _drawingBoat.entityBoat.Weight + " "); + Color color = _drawingBoat.entityBoat.BodyColor; + StatusStripLabelColor.setText("Цвет: (" + color.getRed() + ", " + color.getGreen() + ", " + color.getBlue() + ")"); + + // Перерисовываем содержимое + Draw(); + } + }); + + // Обработчик изменения размеров формы + PanelWrapper.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + + // Изменяем граници области отрисовки для объекта отрисовки + _drawingBoat.ChangeBorders(PictureBox.getWidth(), PictureBox.getHeight()); + + // Перерисовываем содержимое + Draw(); + } + }); + + ActionListener buttonMoveClickedListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + // Получаем имя кнопки перемещения + String buttonName = ((JButton) e.getSource()).getName(); + + // В зависимости от нажатой кнопки перемещаяем объект + switch (buttonName) { + case ("ButtonUp"): { + _drawingBoat.MoveTransport(EnumDirection.Up); + } + break; + case ("ButtonDown"): { + _drawingBoat.MoveTransport(EnumDirection.Down); + } + break; + case ("ButtonLeft"): { + _drawingBoat.MoveTransport(EnumDirection.Left); + } + break; + case ("ButtonRight"): { + _drawingBoat.MoveTransport(EnumDirection.Right); + } + break; + } + + // Перерисовываем содержимое + Draw(); + } + }; + + ButtonUp.addActionListener(buttonMoveClickedListener); + ButtonDown.addActionListener(buttonMoveClickedListener); + ButtonLeft.addActionListener(buttonMoveClickedListener); + ButtonRight.addActionListener(buttonMoveClickedListener); + } + + // Метод отрисовки + public void Draw() { + // Если сущности внутри объекта отрисовки нет - выходим + if (_drawingBoat.entityBoat == null) { + return; + } + + // Закрашиваем PictureBox цветом окна (очищаем его) + Graphics g = PictureBox.getGraphics(); + g.setColor(PictureBox.getBackground()); + g.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); + + // Рисуем объект + _drawingBoat.DrawTransport(g); + + // Перерисовываем элементы управления (PictureBox закрашивает их) + RepaintControls(); + } + + // Метод перерисовки элементов управления + private void RepaintControls() { + for (JComponent control : __controls) { + control.repaint(); + } + } + + // Метод инициализации списка элементов управления для их перерисовки + private void InitializeControlsRepaintList() { + __controls = new LinkedList<>(); + __controls.add(ButtonCreate); + __controls.add(ButtonUp); + __controls.add(ButtonDown); + __controls.add(ButtonLeft); + __controls.add(ButtonRight); + } } -- 2.25.1