From 9beb5a38bedf8f83b0ccf86a0ad3a11a301613cd Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 8 Nov 2023 10:22:00 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Resources/arrowDown.png | Bin 0 -> 537 bytes .../Resources/arrowLeft.png | Bin 0 -> 502 bytes .../Resources/arrowRight.png | Bin 0 -> 497 bytes .../Resources/arrowUp.png | Bin 0 -> 538 bytes .../src/DrawningObjects/CountEnum.java | 7 + .../DrawningElectricLocomotive.java | 140 ++++++++++++++++ .../src/DrawningObjects/DrawningWheels.java | 45 ++++++ .../Entities/EntityElectricLocomotive.java | 27 ++++ .../src/Forms/FormElectricLocomotive.java | 150 ++++++++++++++++++ ProjectElectricLocomotive/src/Main.java | 7 + .../src/MovementStrategy/DirectionType.java | 8 + 11 files changed, 384 insertions(+) create mode 100644 ProjectElectricLocomotive/Resources/arrowDown.png create mode 100644 ProjectElectricLocomotive/Resources/arrowLeft.png create mode 100644 ProjectElectricLocomotive/Resources/arrowRight.png create mode 100644 ProjectElectricLocomotive/Resources/arrowUp.png create mode 100644 ProjectElectricLocomotive/src/DrawningObjects/CountEnum.java create mode 100644 ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java create mode 100644 ProjectElectricLocomotive/src/DrawningObjects/DrawningWheels.java create mode 100644 ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java create mode 100644 ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java create mode 100644 ProjectElectricLocomotive/src/Main.java create mode 100644 ProjectElectricLocomotive/src/MovementStrategy/DirectionType.java diff --git a/ProjectElectricLocomotive/Resources/arrowDown.png b/ProjectElectricLocomotive/Resources/arrowDown.png new file mode 100644 index 0000000000000000000000000000000000000000..4b8528dd2dc501932a212be176ce3187c0550a4e GIT binary patch literal 537 zcmV+!0_OdRP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D0j^0zK~!i%?U%bs z15pr$V`Cu*Uckaa!H2Q3@F}c)KMNJH@Q#Z2N~|m_t@Zzu1IsX*?Acp3Fdux7gyhWp zJLl|@Q4j<{5ClOG1crTi`2VsumgIrl{d?B}g!q;}@{|vHfDp%$nz$_+u_sfAWY=NV zDpOw^u)g@x{X@%>Rfu?wRRdJQ7269ndm%3f-qIL8TbBI^I4gloQX+L zkgMl4R7Y;#y@yEfEclRKV>uFnwWbPB=O$s8>eNFd2=gMnM#nj*5D6N}IWvRUPG!*8 zgY+6N%AcpfhDh)i;7xh~aFE-n48nMmnW+#78ZMrhnb=Nc&^VKs=@1FF83sWR1VIo~ bVl?^%F9~%}cHbco00000NkvXXu0mjf=;i7P literal 0 HcmV?d00001 diff --git a/ProjectElectricLocomotive/Resources/arrowLeft.png b/ProjectElectricLocomotive/Resources/arrowLeft.png new file mode 100644 index 0000000000000000000000000000000000000000..2f10384b2fefd898a23031d8c418cc3c0b9fda6c GIT binary patch literal 502 zcmVPx#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D0gFjQK~!i%?U}nu z1VIo*SN#DWU?6DV#~Aqu1_t6E8VM?>fugJ$_&^NA$iTqBLXsHj8C zijJOO^&!-WCA4E=LX^(LC0Z^g?KV4@6&+Pr?Phl9$?jrSbW~@oWwhg8hR`v^j!|Yp zgZ-y7F+IGN@Q$Th=PQ1E|MWy%xyP&vQSDSyXv@Tf(Z6(7eDn;h!)OO94Re6@VV*E6 zKB{GAjdrrqFgm_lqcC@v6(6+^Gs?_V5vH^=`)G}p*)wLvN9{YK7jqAo6(7|*Eo*f0 zy~3>csC^infJ-}LKQmfpr884Sm`BWt?|j28v_}WcTl_7=HdceaTE)&N#3ojUz9YV1 s)gd|*<_0$gca<|Uyfzw*vD52)0eO*aEAq_Z0{{R307*qoM6N<$f;N@Z*#H0l literal 0 HcmV?d00001 diff --git a/ProjectElectricLocomotive/Resources/arrowRight.png b/ProjectElectricLocomotive/Resources/arrowRight.png new file mode 100644 index 0000000000000000000000000000000000000000..0ff5d14507c69e67dad5e202b1e42f16909224b0 GIT binary patch literal 497 zcmVPx#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D0ftFLK~!i%?b$sq zL_rwE@kK}wuZ4mjh;O4(sgydOZ9}5a=oBK}DM%!gDwUf5VK>=K9&x9zIkTCQ{BlXQ zc#fHI@9ws0YHIqLk-#t+o;SHqTy2)uwR*`WiDZTpBY2bGEcBynKMMgr*vi}VVj(7 zrq@ltHaTUqg4W3?a|i2{(Qr-AOcC2m2Rj-*HBe>r7Qi}l(9>`5#=cDMnQ2^RxO44ShVb_DPx#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D0k26!K~!i%?U&6; z!$1^9>q0@nU(kh!h=>bCeHC%x%9U>@;!4Dof++sGP!RPAd;~oocAyM5W|Agt0{6fH zDQ%L|xnq-#<2a7vI8Gs!aP05T_Ogy2c!8~r(Mnw7PgljXjs<+d5D`>ZYgR@pVMPS* zu<1W zwoprNojCRkdm{pRO&4i>CRQt0`Iu>(QX{k{^zCOchp8*080!lu#VUTGb)wj?C-Tsq zNsZBcTi+|Qjj0h?MjT_A5zuG!V`v>SRx3DjW!{2sGxA)NvfgaU92S cO3>;20*tD4k@asgKL7v#07*qoM6N<$f@;d=%m4rY literal 0 HcmV?d00001 diff --git a/ProjectElectricLocomotive/src/DrawningObjects/CountEnum.java b/ProjectElectricLocomotive/src/DrawningObjects/CountEnum.java new file mode 100644 index 0000000..48c288b --- /dev/null +++ b/ProjectElectricLocomotive/src/DrawningObjects/CountEnum.java @@ -0,0 +1,7 @@ +package DrawningObjects; + +public enum CountEnum { + Min, + Mid, + Max +} diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java new file mode 100644 index 0000000..ddb6842 --- /dev/null +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java @@ -0,0 +1,140 @@ +package DrawningObjects; + +import Entities.*; +import MovementStrategy.*; + +import java.awt.*; + +public class DrawningElectricLocomotive { + public EntityElectricLocomotive _entityElectricLocomotive; + private DrawningWheels drawningWheels; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX; + private int _startPosY; + private final int _locomotiveWidth = 120; + private final int _locomotiveHeight = 70; + + public boolean Init(int speed, double weight, Color bodyColor, Color + additionalColor, int countWheels, boolean horns, boolean battery, int width, int height) { + if (width < _locomotiveWidth || height < _locomotiveHeight) { + return false; + } + _pictureWidth = width; + _pictureHeight = height; + _entityElectricLocomotive = new EntityElectricLocomotive(); + _entityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, + horns, battery); + drawningWheels = new DrawningWheels(); + drawningWheels.setCount(countWheels); + return true; + } + + public void SetPosition(int x, int y) { + x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth); + y = Math.min(Math.max(0, y), _pictureHeight - _locomotiveHeight); + _startPosX = x; + _startPosY = y; + } + + public void MoveTransport(DirectionType direction) { + if (_entityElectricLocomotive == null) { + return; + } + switch (direction) { + //влево + case DirectionType.Left: + if (_startPosX - _entityElectricLocomotive.Step > 0) { + _startPosX -= (int) _entityElectricLocomotive.Step; + } + break; + //вверх + case DirectionType.Up: + if (_startPosY - _entityElectricLocomotive.Step > 0) { + _startPosY -= (int) _entityElectricLocomotive.Step; + } + break; + // вправо + case DirectionType.Right: + if (_startPosX + _locomotiveWidth + _entityElectricLocomotive.Step < _pictureWidth) { + _startPosX += (int) _entityElectricLocomotive.Step; + } + break; + //вниз + case DirectionType.Down: + if (_startPosY + _locomotiveHeight + _entityElectricLocomotive.Step < _pictureHeight) { + _startPosY += (int) _entityElectricLocomotive.Step; + } + break; + } + } + + public void DrawTransport(Graphics g) { + if (_entityElectricLocomotive == null) { + return; + } + + g.clearRect(0, 0, _pictureWidth, _pictureHeight); + // корпус электровоза + g.setColor(_entityElectricLocomotive.BodyColor); + + int[] posX = { + _startPosX, + _startPosX + 10, + _startPosX + 120, + _startPosX + 120, + _startPosX + }; + + int[] posY = { + _startPosY + 40, + _startPosY + 20, + _startPosY + 20, + _startPosY + 60, + _startPosY + 60 + }; + + g.fillPolygon(posX, posY, 5); + g.drawLine(_startPosX, _startPosY + 40, + _startPosX + 120, _startPosY + 40); + + // окна + g.setColor(Color.blue); + g.fillRect(_startPosX + 12, _startPosY + 24, 30, 11); + g.fillRect(_startPosX + 55, _startPosY + 24, 11, 11); + g.fillRect(_startPosX + 75, _startPosY + 24, 11, 11); + g.fillRect(_startPosX + 95, _startPosY + 24, 11, 11); + + // колёса + drawningWheels.DrawWheels(g, _startPosX, _startPosY); + + // рога + if (_entityElectricLocomotive.Horns) { + g.setColor(Color.black); + posX = new int[]{ + _startPosX + 50, + _startPosX + 40, + _startPosX + 50, + _startPosX + 60 + }; + + posY = new int[]{ + _startPosY + 20, + _startPosY + 10, + _startPosY, + _startPosY + 10 + }; + g.drawPolygon(posX, posY, 4); + } + // отсек для батарей + if (_entityElectricLocomotive.Battery) { + g.setColor(_entityElectricLocomotive.AdditionalColor); + g.fillRect(_startPosX + 80, _startPosY + 45, 35, 9); + } + } + public void SetPictureSize(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + } +} diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheels.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheels.java new file mode 100644 index 0000000..900dfd0 --- /dev/null +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheels.java @@ -0,0 +1,45 @@ +package DrawningObjects; + +import java.awt.*; + +public class DrawningWheels { + private CountEnum countWhils; + + public void setCount(int count){ + count = Math.min(4, count); + count = Math.max(2, count); + switch (count) { + case 2: + countWhils = CountEnum.Min; + return; + case 3: + countWhils = CountEnum.Mid; + return; + case 4: + countWhils = CountEnum.Max; + return; + } + } + + void DrawWheels(Graphics g, int posX, int posY){ + g.setColor(Color.black); + int count = 0; + switch (countWhils){ + case Min -> { + count = 2; + posX += 15; + } + case Mid -> { + count = 3; + posX += 5; + } + case Max -> { + count = 4; + } + } + for (int i = 0; i < count; ++i){ + g.fillOval(posX + (i * 15), posY + 55, 15, 15); + g.fillOval(posX + 60 + (i * 15), posY + 55, 15, 15); + } + } +} diff --git a/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java new file mode 100644 index 0000000..c4905e0 --- /dev/null +++ b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java @@ -0,0 +1,27 @@ +package Entities; + +import java.awt.*; +import java.lang.FunctionalInterface; + +public class EntityElectricLocomotive { + public int Speed; + public double Weight; + public Color BodyColor; + public Color AdditionalColor; + public boolean Horns; + public boolean Battery; + public double Step; + + public void Init(int speed, double weight, Color bodyColor, Color + additionalColor, boolean horns, boolean battery) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Horns = horns; + Battery = battery; + Step = Speed * 100.0f / Weight; + } + +} diff --git a/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java b/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java new file mode 100644 index 0000000..eca7bef --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java @@ -0,0 +1,150 @@ +package Forms; + +import DrawningObjects.CountEnum; +import DrawningObjects.DrawningElectricLocomotive; +import MovementStrategy.*; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormElectricLocomotive extends JFrame { + private DrawningElectricLocomotive _drawningElectricLocomotive; + private Canvas canvas; + private JButton buttonCreate; + private JButton buttonUp; + private JButton buttonRight; + private JButton buttonDown; + private JButton buttonLeft; + private JTextField numberField; + public void Draw() { + if (_drawningElectricLocomotive == null) + { + return; + } + _drawningElectricLocomotive.DrawTransport(canvas.getGraphics()); + } + private void InitializeComponent(){ + buttonCreate = new JButton("Создать"); + + buttonUp = new JButton(); + buttonUp.setBorderPainted(false); + buttonUp.setFocusPainted(false); + buttonUp.setContentAreaFilled(false); + buttonUp.setName("up"); + buttonUp.setIcon(new ImageIcon("Resources/arrowUp.png")); + buttonUp.setSize(40, 40); + + buttonRight = new JButton(); + buttonRight.setBorderPainted(false); + buttonRight.setFocusPainted(false); + buttonRight.setContentAreaFilled(false); + buttonRight.setName("right"); + buttonRight.setIcon(new ImageIcon("Resources/arrowRight.png")); + buttonRight.setSize(40, 40); + + buttonDown = new JButton(); + buttonDown.setBorderPainted(false); + buttonDown.setFocusPainted(false); + buttonDown.setContentAreaFilled(false); + buttonDown.setName("down"); + buttonDown.setIcon(new ImageIcon("Resources/arrowDown.png")); + buttonDown.setSize(40, 40); + + buttonLeft = new JButton(); + buttonLeft.setBorderPainted(false); + buttonLeft.setFocusPainted(false); + buttonLeft.setContentAreaFilled(false); + buttonLeft.setName("left"); + buttonLeft.setIcon(new ImageIcon("Resources/arrowLeft.png")); + buttonLeft.setSize(40, 40); + + numberField = new JTextField(); + + setSize(1000, 600); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(null); + + canvas = new Canvas(); + canvas.setBounds(0, 0, 1000, 600); + buttonCreate.setBounds(10, 520, 120, 40); + buttonUp.setBounds(50, 430, 40, 40); + buttonDown.setBounds(50, 470, 40, 40); + buttonRight.setBounds(90, 470, 40, 40); + buttonLeft.setBounds(10, 470, 40, 40); + numberField.setBounds(150, 520, 40, 40); + + + add(buttonCreate); + add(buttonUp); + add(buttonDown); + add(buttonRight); + add(buttonLeft); + add(numberField); + add(canvas); + } + private void InitializeLogic(){ + buttonCreate.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + int countWheels; + try { + countWheels = Integer.parseInt(numberField.getText()); + } + catch (Exception ex){ + countWheels = 0; + } + System.out.println(e.getActionCommand()); + Random random = new Random(); + _drawningElectricLocomotive = new DrawningElectricLocomotive(); + _drawningElectricLocomotive.Init(random.nextInt(100, 300), random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + countWheels, + random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1, + 1000, 560); + _drawningElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + Draw(); + } + } + ); + + ActionListener actionListener = new ActionListener() { + public void actionPerformed(ActionEvent e){ + System.out.println(((JButton)(e.getSource())).getName()); + if (_drawningElectricLocomotive == null) + { + return; + } + switch(((JButton)(e.getSource())).getName()){ + case "up": + _drawningElectricLocomotive.MoveTransport(DirectionType.Up); + break; + case "down": + _drawningElectricLocomotive.MoveTransport(DirectionType.Down); + break; + case "left": + _drawningElectricLocomotive.MoveTransport(DirectionType.Left); + break; + case "right": + _drawningElectricLocomotive.MoveTransport(DirectionType.Right); + break; + } + Draw(); + } + }; + + buttonUp.addActionListener(actionListener); + buttonDown.addActionListener(actionListener); + buttonLeft.addActionListener(actionListener); + buttonRight.addActionListener(actionListener); + + } + public FormElectricLocomotive(){ + InitializeComponent(); + InitializeLogic(); + setVisible(true); + } +} diff --git a/ProjectElectricLocomotive/src/Main.java b/ProjectElectricLocomotive/src/Main.java new file mode 100644 index 0000000..e47628f --- /dev/null +++ b/ProjectElectricLocomotive/src/Main.java @@ -0,0 +1,7 @@ +import Forms.*; + +public class Main { + public static void main(String[] args) { + FormElectricLocomotive form = new FormElectricLocomotive(); + } +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/src/MovementStrategy/DirectionType.java b/ProjectElectricLocomotive/src/MovementStrategy/DirectionType.java new file mode 100644 index 0000000..002f701 --- /dev/null +++ b/ProjectElectricLocomotive/src/MovementStrategy/DirectionType.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public enum DirectionType { + Up, + Down, + Left, + Right +}