From f9a8216e8c2f614f8fc5482bafccd71e3a410386 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 22 Nov 2023 09:18:12 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=202=20?= =?UTF-8?q?=D1=81=D0=B4=D0=B0=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningElectricLocomotive.java | 6 +- .../DrawningObjects/DrawningLocomotive.java | 47 +++++++++++++--- .../src/DrawningObjects/DrawningWheels.java | 55 ++++++++++++------- .../DrawningWheelsPolygon.java | 22 ++++++++ .../DrawningObjects/DrawningWheelsStar.java | 26 +++++++++ .../src/DrawningObjects/IDrawWheels.java | 9 +++ .../Entities/EntityElectricLocomotive.java | 5 +- .../src/Entities/EntityLocomotive.java | 7 +-- .../src/Forms/FormElectricLocomotive.java | 17 ++++-- 9 files changed, 151 insertions(+), 43 deletions(-) create mode 100644 ProjectElectricLocomotive/src/DrawningObjects/DrawningWheelsPolygon.java create mode 100644 ProjectElectricLocomotive/src/DrawningObjects/DrawningWheelsStar.java create mode 100644 ProjectElectricLocomotive/src/DrawningObjects/IDrawWheels.java diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java index 130807f..a4f4099 100644 --- a/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java @@ -8,13 +8,13 @@ import java.awt.*; public class DrawningElectricLocomotive extends DrawningLocomotive { public DrawningElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, - int countWheels, + int typeWheels, int countWheels, boolean horns, boolean battery, int width, int height){ - super(speed, weight, bodyColor, countWheels, width, height, 120, 70); + super(speed, weight, bodyColor, typeWheels, countWheels, width, height, 120, 70); if (entityLocomotive != null){ entityLocomotive = new EntityElectricLocomotive( - speed, weight, bodyColor, additionalColor, countWheels, horns, battery); + speed, weight, bodyColor, additionalColor, horns, battery); } } @Override diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java index abf0e06..204954a 100644 --- a/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java @@ -11,9 +11,10 @@ public class DrawningLocomotive { protected int _pictureHeight; protected int _startPosX; protected int _startPosY; - protected int _locomotiveWidth = 120; - protected int _locomotiveHeight = 70; + protected int _locomotiveWidth = 140; + protected int _locomotiveHeight = 75; + protected IDrawWheels wheels; public int GetPosX() { return _startPosX; @@ -32,18 +33,33 @@ public class DrawningLocomotive { } public DrawningLocomotive(int speed, double weight, - Color bodyColor, int countWheels, + Color bodyColor, + int typeWheels, int countWheels, int width, int height) { if (width < _locomotiveWidth || height < _locomotiveHeight) { return; } _pictureWidth = width; _pictureHeight = height; - entityLocomotive = new EntityLocomotive(speed, weight, bodyColor, countWheels); + switch (typeWheels){ + case 0: + wheels = new DrawningWheels(); + break; + case 1: + wheels = new DrawningWheelsPolygon(); + break; + case 2: + wheels = new DrawningWheelsStar(); + break; + } + wheels.setWheelsCount(countWheels); + + entityLocomotive = new EntityLocomotive(speed, weight, bodyColor); } public DrawningLocomotive(int speed, double weight, - Color bodyColor, int countWheels, + Color bodyColor, + int typeWheels, int countWheels, int width, int height, int locomotiveWidth, int locomotiveHeight) { if (width < locomotiveWidth || height < locomotiveHeight) { @@ -53,7 +69,20 @@ public class DrawningLocomotive { _pictureHeight = height; _locomotiveWidth = locomotiveWidth; _locomotiveHeight = locomotiveHeight; - entityLocomotive = new EntityLocomotive(speed, weight, bodyColor, countWheels); + switch (typeWheels){ + case 0: + wheels = new DrawningWheels(); + break; + case 1: + wheels = new DrawningWheelsPolygon(); + break; + case 2: + wheels = new DrawningWheelsStar(); + break; + } + wheels.setWheelsCount(countWheels); + + entityLocomotive = new EntityLocomotive(speed, weight, bodyColor); } public void SetPosition(int x, int y) { @@ -75,8 +104,8 @@ public class DrawningLocomotive { int[] posX = { _startPosX, _startPosX + 10, - _startPosX + 120, - _startPosX + 120, + _startPosX + 140, + _startPosX + 140, _startPosX }; @@ -100,7 +129,7 @@ public class DrawningLocomotive { g.fillRect(_startPosX + 95, _startPosY + 24, 11, 11); // колёса - entityLocomotive.DrawningWheels.DrawWheels(g, _startPosX, _startPosY); + wheels.DrawWheels(g, _startPosX, _startPosY); } public void SetPictureSize(int width, int height) { diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheels.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheels.java index 900dfd0..b51edcc 100644 --- a/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheels.java +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheels.java @@ -2,29 +2,24 @@ package DrawningObjects; import java.awt.*; -public class DrawningWheels { - private CountEnum countWhils; +public class DrawningWheels implements IDrawWheels { + private CountEnum _wheelsCount; + final protected int _wheelsSize = 18; - 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; - } + @Override + public CountEnum WheelsCount() { + return _wheelsCount; + } + protected void drawWheel(Graphics g, int posX, int posY){ + + g.fillOval(posX, posY, _wheelsSize, _wheelsSize); } - void DrawWheels(Graphics g, int posX, int posY){ + @Override + public void DrawWheels(Graphics g, int posX, int posY){ g.setColor(Color.black); int count = 0; - switch (countWhils){ + switch (_wheelsCount){ case Min -> { count = 2; posX += 15; @@ -38,8 +33,28 @@ public class DrawningWheels { } } 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); + drawWheel(g, posX + (i * 18), posY + 55); + drawWheel(g, posX + 75 + (i * 18), posY + 55); } } + + @Override + public void setWheelsCount(int count) { + count = Math.min(4, count); + count = Math.max(2, count); + switch (count) { + case 2: + _wheelsCount = CountEnum.Min; + break; + case 3: + _wheelsCount = CountEnum.Mid; + break; + case 4: + _wheelsCount = CountEnum.Max; + break; + } + } + public String toString(){ + return "Обычное колёса"; + } } diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheelsPolygon.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheelsPolygon.java new file mode 100644 index 0000000..5da5cff --- /dev/null +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheelsPolygon.java @@ -0,0 +1,22 @@ +package DrawningObjects; + +import java.awt.*; + +public class DrawningWheelsPolygon extends DrawningWheels { + private final int _segmentsCount = 6; + private final double _angle = Math.PI * 2 / _segmentsCount; + @Override + protected void drawWheel(Graphics g, int posX, int posY){ + g.drawOval(posX, posY, _wheelsSize, _wheelsSize); + int[] pointsPosX = new int[_segmentsCount]; + int[] pointsPosY = new int[_segmentsCount]; + for (int i = 0; i < _segmentsCount; ++i){ + pointsPosX[i] = (int)((Math.cos(_angle * i) + 1) * _wheelsSize / 2) + posX; + pointsPosY[i] = (int)((Math.sin(_angle * i) + 1) * _wheelsSize / 2) + posY; + } + g.drawPolygon(pointsPosX, pointsPosY, _segmentsCount); + } + public String toString(){ + return "колёса с узором многоугольник"; + } +} diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheelsStar.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheelsStar.java new file mode 100644 index 0000000..b252512 --- /dev/null +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningWheelsStar.java @@ -0,0 +1,26 @@ +package DrawningObjects; + +import java.awt.*; + +public class DrawningWheelsStar extends DrawningWheels { + private final int _segmentsCount = 8; + private final double _angle = Math.PI * 2 / _segmentsCount; + @Override + protected void drawWheel(Graphics g, int posX, int posY){ + g.drawOval(posX, posY, _wheelsSize, _wheelsSize); + int[] pointsPosX = new int[_segmentsCount]; + int[] pointsPosY = new int[_segmentsCount]; + for (int i = 0; i < _segmentsCount; ++i){ + pointsPosX[i] = (int)((Math.cos(_angle * i) + 1) * _wheelsSize / 2) + posX; + pointsPosY[i] = (int)((Math.sin(_angle * i) + 1) * _wheelsSize / 2) + posY; + } + for (int i = 0; i < _segmentsCount; ++i){ + int inversePoint = (i + _segmentsCount / 2 + 1) % _segmentsCount; + g.drawLine(pointsPosX[i], pointsPosY[i], + pointsPosX[inversePoint], pointsPosY[inversePoint]); + } + } + public String toString(){ + return "колёса с узором звезда"; + } +} diff --git a/ProjectElectricLocomotive/src/DrawningObjects/IDrawWheels.java b/ProjectElectricLocomotive/src/DrawningObjects/IDrawWheels.java new file mode 100644 index 0000000..7b66e95 --- /dev/null +++ b/ProjectElectricLocomotive/src/DrawningObjects/IDrawWheels.java @@ -0,0 +1,9 @@ +package DrawningObjects; + +import java.awt.*; + +public interface IDrawWheels { + public CountEnum WheelsCount(); + public void DrawWheels(Graphics g, int posX, int posY); + public void setWheelsCount(int count); +} diff --git a/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java index 90f308d..2646f71 100644 --- a/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java @@ -1,5 +1,7 @@ package Entities; +import DrawningObjects.IDrawWheels; + import java.awt.*; import java.lang.FunctionalInterface; @@ -9,9 +11,8 @@ public class EntityElectricLocomotive extends EntityLocomotive { public boolean Battery; public EntityElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, - int countWheels, boolean horns, boolean battery) { - super(speed, weight, bodyColor, countWheels); + super(speed, weight, bodyColor); Horns = horns; Battery = battery; } diff --git a/ProjectElectricLocomotive/src/Entities/EntityLocomotive.java b/ProjectElectricLocomotive/src/Entities/EntityLocomotive.java index 11e29f8..d656a43 100644 --- a/ProjectElectricLocomotive/src/Entities/EntityLocomotive.java +++ b/ProjectElectricLocomotive/src/Entities/EntityLocomotive.java @@ -1,6 +1,6 @@ package Entities; -import DrawningObjects.DrawningWheels; +import DrawningObjects.IDrawWheels; import java.awt.*; @@ -9,14 +9,11 @@ public class EntityLocomotive { public double Weight; public Color BodyColor; public double Step; - public DrawningWheels DrawningWheels; - public EntityLocomotive(int speed, double weight, Color bodyColor, int countWheels) + public EntityLocomotive(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; Step = Speed * 100.0f / Weight; - DrawningWheels = new DrawningWheels(); - DrawningWheels.setCount(countWheels); } } diff --git a/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java b/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java index 867ad02..afb49a1 100644 --- a/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java @@ -22,6 +22,7 @@ public class FormElectricLocomotive extends JFrame { private JButton buttonStep; private JTextField numberField; private JComboBox comboBoxStrategy; + private JComboBox comboBoxWheels; public void Draw() { if (_drawningLocomotive == null) { @@ -41,9 +42,13 @@ public class FormElectricLocomotive extends JFrame { comboBoxStrategy = new JComboBox<>(); comboBoxStrategy.addItem(new MoveToCenter()); - comboBoxStrategy.addItem(new MoveToBorder()); + comboBoxWheels = new JComboBox<>(); + comboBoxWheels.addItem(new DrawningWheels()); + comboBoxWheels.addItem(new DrawningWheelsPolygon()); + comboBoxWheels.addItem(new DrawningWheelsStar()); + buttonUp = new JButton(); buttonUp.setBorderPainted(false); buttonUp.setFocusPainted(false); @@ -90,9 +95,10 @@ public class FormElectricLocomotive extends JFrame { buttonDown.setBounds(50, 420, 40, 40); buttonRight.setBounds(90, 420, 40, 40); buttonLeft.setBounds(10, 420, 40, 40); - numberField.setBounds(220, 520, 40, 40); + numberField.setBounds(220, 470, 40, 40); buttonStep.setBounds(770, 50, 200, 30); comboBoxStrategy.setBounds(770, 10, 200, 30); + comboBoxWheels.setBounds(220, 520, 250, 40); add(buttonCreateElectricLocomotive); @@ -104,6 +110,7 @@ public class FormElectricLocomotive extends JFrame { add(numberField); add(buttonStep); add(comboBoxStrategy); + add(comboBoxWheels); add(canvas); } private void InitializeLogic(){ @@ -116,6 +123,8 @@ public class FormElectricLocomotive extends JFrame { } catch (Exception ex) { countWheels = 0; } + IDrawWheels _drawWheels = (IDrawWheels) comboBoxWheels.getSelectedItem(); + _drawWheels.setWheelsCount(countWheels); System.out.println(e.getActionCommand()); Random random = new Random(); _drawningLocomotive = new DrawningElectricLocomotive( @@ -126,7 +135,7 @@ public class FormElectricLocomotive extends JFrame { new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), - countWheels, + comboBoxWheels.getSelectedIndex(), countWheels, random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1, canvas.getWidth(), canvas.getHeight() ); @@ -152,7 +161,7 @@ public class FormElectricLocomotive extends JFrame { new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), - countWheels, + comboBoxWheels.getSelectedIndex(), countWheels, canvas.getWidth(), canvas.getHeight() ); _drawningLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));