From ef95d416548c05ca8cb0181eddebff24ae5a29bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Sat, 19 Nov 2022 17:28:39 +0400 Subject: [PATCH 1/2] =?UTF-8?q?1=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 3 + .idea/.name | 1 + .../PIbd-21_Ihonkina_E.S._MotorBoat._Hard.iml | 9 ++ .idea/encodings.xml | 7 + .idea/misc.xml | 6 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 7 + ProjectMotorBoatHard.iml | 11 ++ src/AdditionalDirection.java | 13 ++ src/Direction.java | 6 + src/DrawingBoat.java | 119 +++++++++++++++++ src/DrawingPaddle.java | 40 ++++++ src/EntityBoat.java | 24 ++++ src/FormBoat.form | 0 src/FormBoat.java | 125 ++++++++++++++++++ src/images/down.png | 0 src/images/left.png | 0 src/images/right.jpg | 0 src/images/right.png | 0 src/images/up.jpg | 0 src/images/up.png | 0 21 files changed, 379 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/PIbd-21_Ihonkina_E.S._MotorBoat._Hard.iml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 ProjectMotorBoatHard.iml create mode 100644 src/AdditionalDirection.java create mode 100644 src/Direction.java create mode 100644 src/DrawingBoat.java create mode 100644 src/DrawingPaddle.java create mode 100644 src/EntityBoat.java create mode 100644 src/FormBoat.form create mode 100644 src/FormBoat.java create mode 100644 src/images/down.png create mode 100644 src/images/left.png create mode 100644 src/images/right.jpg create mode 100644 src/images/right.png create mode 100644 src/images/up.jpg create mode 100644 src/images/up.png 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/.name b/.idea/.name new file mode 100644 index 0000000..444d0e9 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +ProjectMotorBoatHard.iml \ No newline at end of file diff --git a/.idea/PIbd-21_Ihonkina_E.S._MotorBoat._Hard.iml b/.idea/PIbd-21_Ihonkina_E.S._MotorBoat._Hard.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/PIbd-21_Ihonkina_E.S._MotorBoat._Hard.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e1f11fa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..09e91bf --- /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..b5fbbd9 --- /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..288b36b --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/ProjectMotorBoatHard.iml b/ProjectMotorBoatHard.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/ProjectMotorBoatHard.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/AdditionalDirection.java b/src/AdditionalDirection.java new file mode 100644 index 0000000..a266a65 --- /dev/null +++ b/src/AdditionalDirection.java @@ -0,0 +1,13 @@ +public enum AdditionalDirection { + + OnePaddle(1), + TwoPaddle(2), + ThreePaddle(3); + private final int count; + AdditionalDirection(int value){ + count = value; + } + public int getCount() { + return count; + } +} diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..996463a --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} \ No newline at end of file diff --git a/src/DrawingBoat.java b/src/DrawingBoat.java new file mode 100644 index 0000000..d8d8def --- /dev/null +++ b/src/DrawingBoat.java @@ -0,0 +1,119 @@ +import javax.swing.*; +import java.awt.*; +public class DrawingBoat extends JComponent { + public EntityBoat Boat; + private float _startPosX; + private float _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _boatWidth = 90; + private final int _boatHeight = 80; + private DrawingPaddle _paddles; + public DrawingBoat() { + super(); + } + public void Init(int speed, float weight, Color bodyColor, int paddleCount) + { + Boat = new EntityBoat(); + Boat.Init(speed, weight, bodyColor); + _paddles = new DrawingPaddle(); + _paddles.SetPaddlesAmount(paddleCount); + } + public void SetPosition(int x, int y, int width, int height) + { + if (width <= _boatWidth + x || height <= _boatHeight + y || x<0 || y<0) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + public void MoveTransport(Direction direction) + { + if (_pictureWidth==null || _pictureHeight==null) + { + 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 paintComponent(Graphics gr) + { + super.paintComponent(gr); + Graphics2D g=(Graphics2D)gr; + if (_startPosX < 0 || _startPosY < 0 + || _pictureHeight==null || _pictureWidth==null) + { + return; + } + Color pen = new Color(0,0,0); + //границы лодки + int [] pointsX = new int[]{(int)(_startPosX),(int)(_startPosX + 50),(int)(_startPosX + 70),(int)(_startPosX + 50),(int)(_startPosX)}; + int [] pointsY = new int[]{(int)(_startPosY),(int)(_startPosY),(int)(_startPosY + 20),(int)(_startPosY + 40),(int)(_startPosY+40)}; + + try { pen = Boat.BodyColor(); } + catch (Exception e) {} + + g.setPaint(pen); + g.fillPolygon(pointsX,pointsY,5); + g.setPaint(Color.black); + g.drawPolygon(pointsX,pointsY,5); + + g.setPaint(Color.yellow); + g.fillOval( (int)(_startPosX + 5),(int)( _startPosY + 10), 50, 20); + g.setPaint(Color.black); + g.drawOval( (int)(_startPosX + 5),(int)( _startPosY + 10), 50, 20); + _paddles.DrawPaddles(gr, (int)_startPosX, (int)_startPosY, pen); + + super.repaint(); + } + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _boatWidth || _pictureHeight <= _boatHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _boatWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _boatWidth; + } + if (_startPosY + _boatHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _boatHeight; + } + } +} diff --git a/src/DrawingPaddle.java b/src/DrawingPaddle.java new file mode 100644 index 0000000..d4327b3 --- /dev/null +++ b/src/DrawingPaddle.java @@ -0,0 +1,40 @@ +import javax.swing.*; +import java.awt.*; +public class DrawingPaddle extends JComponent { + private AdditionalDirection _paddle; + public void SetPaddlesAmount(int rpaddlesAmount) { + for (AdditionalDirection item: _paddle.values()) { + if (item.getCount() == rpaddlesAmount) { + _paddle = item; + return; + } + } + } + public void DrawPaddles(Graphics gr, int _startPosPaddlesX, int _startPosPaddlessY, Color pen) { + super.paintComponent(gr); + Graphics2D g=(Graphics2D)gr; + + _startPosPaddlesX +=40; + _startPosPaddlessY +=33; + if (_paddle.getCount() >= 1) { + paintPaddle(g, _startPosPaddlesX, _startPosPaddlessY, _startPosPaddlessY + 16,pen); + } + if (_paddle.getCount() >= 2) { + paintPaddle(g, _startPosPaddlesX -10, _startPosPaddlessY -41, _startPosPaddlessY -51,pen); + } + if (_paddle.getCount() >= 3) { + paintPaddle(g, _startPosPaddlesX -20, _startPosPaddlessY, _startPosPaddlessY + 16,pen); + } + } + protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1,int _startPosY2, Color pen){ + try { g.setPaint(pen); } + catch (Exception e) { + g.setPaint(Color.black); + } + g.fillRect(_startPosX,_startPosY1,4,17); + g.fillOval(_startPosX-1, _startPosY2, 6, 10); + g.setPaint(Color.black); + g.drawRect(_startPosX,_startPosY1,4,17); + g.drawOval(_startPosX-1, _startPosY2, 6, 10); + } +} diff --git a/src/EntityBoat.java b/src/EntityBoat.java new file mode 100644 index 0000000..1b9d07b --- /dev/null +++ b/src/EntityBoat.java @@ -0,0 +1,24 @@ +import java.awt.*; +import java.util.Random; + +public class EntityBoat { + private int speed; + public int Speed() { return speed; } + private float weight; + public float Weight() { + return weight; + } + private Color bodyColor; + public Color BodyColor() { + return bodyColor; + } + public float Step() + { return Speed() * 20 / Weight(); } + public void Init(int speed, float weight, Color bodyColor) + { + Random random = new Random(); + this.speed = speed <= 0 ? random.nextInt(50, 150) : speed; + this.weight = weight <= 0 ? random.nextInt(40, 70) : weight; + this.bodyColor = bodyColor; + } +} diff --git a/src/FormBoat.form b/src/FormBoat.form new file mode 100644 index 0000000..e69de29 diff --git a/src/FormBoat.java b/src/FormBoat.java new file mode 100644 index 0000000..37f65ac --- /dev/null +++ b/src/FormBoat.java @@ -0,0 +1,125 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.Random; +public class FormBoat extends JFrame { + public static void main(String[] args) { + + FormBoat window = new FormBoat(); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.pack(); + window.setLocationRelativeTo(null); + window.setVisible(true); + } + + private JPanel mainPanel; + private JPanel statusStrip; + private JLabel toolStripStatusLabelSpeed; + private JLabel toolStripStatusLabelBodyColor; + private JLabel toolStripStatusLabelWeight; + private JPanel buttonsBox; + private JButton buttonLeft; + private JButton buttonRight; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonCreate; + private JRadioButton radioButtonPaddle1; + private JRadioButton radioButtonPaddle2; + private JRadioButton radioButtonPaddle3; + private JPanel radioButtonsBox; + private JPanel pictureBoxBoat; + private DrawingBoat _boat; + private int pictureBoxBoatWidth; + private int pictureBoxBoatHeight; + ButtonGroup buttonGroupPaddlesRadBut; + public FormBoat() { + super("Моторная лодка"); + buttonGroupPaddlesRadBut = new ButtonGroup(); + buttonGroupPaddlesRadBut.add(radioButtonPaddle1); + buttonGroupPaddlesRadBut.add(radioButtonPaddle2); + buttonGroupPaddlesRadBut.add(radioButtonPaddle3); + setPreferredSize(new Dimension(1000, 700)); + getContentPane().add(mainPanel); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + pictureBoxBoat.remove(_boat); + } catch (Exception c) { + } + Random random = new Random(); + _boat = new DrawingBoat(); + _boat.Init(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256), + random.nextInt(0, 256), random.nextInt(0, 256)), GetRollersAmount()); + ChangePictureBoxBoatBorders(); + _boat.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxBoatWidth, pictureBoxBoatHeight); + toolStripStatusLabelSpeed.setText("Скорость: " + _boat.Boat.Speed()); + toolStripStatusLabelWeight.setText("Вес: " + _boat.Boat.Weight()); + + toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_boat.Boat.BodyColor().getRGB())); + pictureBoxBoat.add(_boat, BorderLayout.CENTER); + } + }); + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + ChangePictureBoxBoatBorders(); + if (_boat == null) return; + _boat.ChangeBorders(pictureBoxBoatWidth, pictureBoxBoatHeight); + try { + pictureBoxBoat.remove(_boat); + } catch (Exception c) { + } + pictureBoxBoat.add(_boat, BorderLayout.CENTER); + } + }); + //джижение + ButtonsMove buttonsMove = new ButtonsMove(); + buttonUp.setName("Up"); + buttonLeft.setName("Left"); + buttonRight.setName("Right"); + buttonDown.setName("Down"); + buttonUp.addActionListener(buttonsMove); + buttonLeft.addActionListener(buttonsMove); + buttonRight.addActionListener(buttonsMove); + buttonDown.addActionListener(buttonsMove); + } + class ButtonsMove implements ActionListener { + public void actionPerformed(ActionEvent e) { + if (_boat == null) return; + JButton temp = (JButton) e.getSource(); + String name = temp.getName(); + switch (name) { + case "Up" -> _boat.MoveTransport(Direction.Up); + case "Right" -> _boat.MoveTransport(Direction.Right); + case "Left" -> _boat.MoveTransport(Direction.Left); + case "Down" -> _boat.MoveTransport(Direction.Down); + } + } + } + + private void ChangePictureBoxBoatBorders() { + char[] temp = pictureBoxBoat.getSize().toString().toCharArray(); + for (int i = 0; i < temp.length; i++) { + if (!Character.isDigit(temp[i])) { + temp[i] = ' '; + } + } + String width = new String(temp); + String[] parameters = width.split("\\s*(\\s|,|!|\\.)\\s*", 4); + pictureBoxBoatWidth = Integer.parseInt(parameters[1]); + pictureBoxBoatHeight = Integer.parseInt(parameters[2]); + } + private int GetRollersAmount() { + if (radioButtonPaddle1.isSelected()) { + return 1; + } + else if (radioButtonPaddle2.isSelected()) { + return 2; + } else { + return 3; + } + } +} diff --git a/src/images/down.png b/src/images/down.png new file mode 100644 index 0000000..e69de29 diff --git a/src/images/left.png b/src/images/left.png new file mode 100644 index 0000000..e69de29 diff --git a/src/images/right.jpg b/src/images/right.jpg new file mode 100644 index 0000000..e69de29 diff --git a/src/images/right.png b/src/images/right.png new file mode 100644 index 0000000..e69de29 diff --git a/src/images/up.jpg b/src/images/up.jpg new file mode 100644 index 0000000..e69de29 diff --git a/src/images/up.png b/src/images/up.png new file mode 100644 index 0000000..e69de29 -- 2.25.1 From 769fedd755687f8b71f2d6ce19ae82994cb3b7e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Sat, 19 Nov 2022 18:01:29 +0400 Subject: [PATCH 2/2] =?UTF-8?q?2=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FormBoat.form | 190 +++++++++++++++++++++++++++++++++++++++++++ src/images/down.png | Bin 0 -> 407 bytes src/images/left.png | Bin 0 -> 463 bytes src/images/right.jpg | 0 src/images/right.png | Bin 0 -> 446 bytes src/images/up.jpg | 0 src/images/up.png | Bin 0 -> 413 bytes 7 files changed, 190 insertions(+) delete mode 100644 src/images/right.jpg delete mode 100644 src/images/up.jpg diff --git a/src/FormBoat.form b/src/FormBoat.form index e69de29..89f3a52 100644 --- a/src/FormBoat.form +++ b/src/FormBoat.form @@ -0,0 +1,190 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/images/down.png b/src/images/down.png index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..b1e74ad26da962539bbc909637518ea6f876350e 100644 GIT binary patch literal 407 zcmV;I0cie-P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0W3*GK~z{r-IzfR zf-n$8iwos4UV%$^A@^LvYp~#2CPe4!Z>$1krlk>m$%I;_!%MLRt!{$b06QMPj%h}! zs$$EA<`U)^IZL3dHnbGIHCG-r6cst}sG<0mwP_l*;uII@`<|^h#YME3M2hZx*heg2 zh%8|=7Q<5hH*bvv>mL07GXmt+v$f1NX9Q2zsR-%1jx9UU+Rw%F0$o_17n}+L{XR6y zH_S(%&&S^P1-ySENYd^VDe)-*#yvt^*N@~9GE)*@x|ggiGbaJ&&j@YXJ_fHLgF3@7 zu<{tmih}~VGQLKP_*s%8qmcm186-tUD*<#_#3Eyn0QyX#k+DhuTQ-Twq(}h!2r?&= zA_1~~g!_k<$hf1#Ng}me)@wj`(Hse|k-sIJy8{~6Dc&11rpo{T002ovPDHLkV1n6A BqO|}3 literal 0 HcmV?d00001 diff --git a/src/images/left.png b/src/images/left.png index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..2ec2c67a64604d70524350b264ba2f685d82f4f1 100644 GIT binary patch literal 463 zcmV;=0WkiFP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0c1%;K~z{r&6Znk zgD?<94L=e4=sNgeVQs%zOP9dDP$ZQ44h#xo#@Not$&s#j`Ef7?+&nz4kfv$e3)K&_ z+cq0WSduN$bsb6kY{yn)i_G&(5?||~9hf62jN^#O@bD6t6_kwXmn5}>4y6QARYa4Z z7!qwQCSf@mKy58L5qKDxAA;RDg(H@6YM7P9#rj`Vviqw=q&P3>OVLpJ^bG1aBBiQ_5w^Ful5A1IZ ztu3%qfwK|W|Gv0@0?0c!ECbl`9WudsBvpn-Po|X9g;A;u%5fMe_ME$qhPpyam0_Nx zWct3RGB+Qsy~|ZR|9fO1oV}=;g*qY(cP8(|PGRLz1VCn4mOzF_%Yq~k(6%iJ*>!y+ zS%hI3Ogu}nMF3=CuVM*BB7i5c6~!XJQsyNQOFl%zxqodq<>j|csbByA002ovPDHLk FV1o2Qx$*!2 literal 0 HcmV?d00001 diff --git a/src/images/right.jpg b/src/images/right.jpg deleted file mode 100644 index e69de29..0000000 diff --git a/src/images/right.png b/src/images/right.png index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..95973828540ce25a865870f3dc0c5ae968b4455d 100644 GIT binary patch literal 446 zcmV;v0YUzWP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0aHmtK~z{r#gm#)sPXZ}I^NC#>lQ_Hwa0_3=ka6E~s6K`< zLK3G50I8NMajJmupb&{B0-qG`TbD}0nC=NdiK*GbzyN zNhNZVgKB~O?2jY|ssfUHo@X9SQ~geer{_-X7X=AmLO7g_%71|*ABMqvKY~gHKL1eMRf##mH+?%07*qoM6N<$g26$tSO5S3 literal 0 HcmV?d00001 diff --git a/src/images/up.jpg b/src/images/up.jpg deleted file mode 100644 index e69de29..0000000 diff --git a/src/images/up.png b/src/images/up.png index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8fceff9519e5004103da2be36dbe6a05909befd0 100644 GIT binary patch literal 413 zcmV;O0b>4%P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0WwKMK~z{r-IzfR zf-n$8!G+-<-h%}flQn1Y2yD2O3!*b+hCnUtblN81OMYl@TArm^wEh6q3AoWwMP1ib z55Z$9Y7xBni`q!C4BnbpOt(#*5ImH_J zx~`+vf@^HdmZayAoQOqoJpA~$-i#pnRTJhkGlHvEgeesUKf