From 9acaae8ff2d63f04f0ddca22fe88ee4c809d474a Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 22 Oct 2022 01:15:02 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B4=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?,=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B0=20=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D1=81=D0=BE=D0=B2=D0=BA=D0=B8=20=D0=BA=D0=BE=D0=BB=D0=B5=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 13 +++++----- DrawningWarmlyLocomotive.java | 6 +++++ EntityWithExtraCreator.java | 47 +++++++++++++++++++++++++++++++++++ ExtraRoundWheelDraw.java | 5 +++- ExtraStarWheelDraw.java | 5 +++- FormLocomotive.java | 4 +-- 6 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 EntityWithExtraCreator.java diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index e89236f..3293091 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -3,7 +3,6 @@ import java.util.Random; public class DrawningLocomotive { public EntityLocomotive Locomotive; - public ExtraWheelsDraw extraWheelsDraw; public IDrawningExtra drawningExtra; /// Левая координата отрисовки локомотива protected float _startPosX; @@ -22,13 +21,12 @@ public class DrawningLocomotive { public DrawningLocomotive(int speed, float weight, Color bodyColor) { int randExtra = random.nextInt(2); - extraWheelsDraw = new ExtraWheelsDraw(randExtra, bodyColor); switch (random.nextInt(3)){ case 0: - drawningExtra = new ExtraStarWheelDraw(randExtra); + drawningExtra = new ExtraStarWheelDraw(randExtra, bodyColor); break; case 1: - drawningExtra = new ExtraRoundWheelDraw(randExtra); + drawningExtra = new ExtraRoundWheelDraw(randExtra, bodyColor); break; case 2: drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor); @@ -37,6 +35,11 @@ public class DrawningLocomotive { Locomotive = new EntityLocomotive(speed, weight, bodyColor); } + public DrawningLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) { + drawningExtra = extra; + Locomotive = locomotive; + } + // Новый конструктор protected DrawningLocomotive (int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) { @@ -122,8 +125,6 @@ public class DrawningLocomotive { //дверь g.setColor(Color.BLACK); g.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); - //колеса - extraWheelsDraw.DrawExtra((int)_startPosX, (int)_startPosY, g); //extra drawningExtra.DrawExtra((int)_startPosX, (int)_startPosY, g); //движок diff --git a/DrawningWarmlyLocomotive.java b/DrawningWarmlyLocomotive.java index f7fd094..4019911 100644 --- a/DrawningWarmlyLocomotive.java +++ b/DrawningWarmlyLocomotive.java @@ -5,6 +5,12 @@ public class DrawningWarmlyLocomotive extends DrawningLocomotive{ super(speed, weight, bodyColor, 140, 70); Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); } + + public DrawningWarmlyLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) { + super(locomotive, extra); + Locomotive = locomotive; + } + @Override public void DrawTransport(Graphics2D g) { diff --git a/EntityWithExtraCreator.java b/EntityWithExtraCreator.java new file mode 100644 index 0000000..b037325 --- /dev/null +++ b/EntityWithExtraCreator.java @@ -0,0 +1,47 @@ +import java.util.Random; + +public class EntityWithExtraCreator { + private T[] entityArr; + private U[] extraArr; + + int entitiesCount = 0; + int extraCount = 0; + + public EntityWithExtraCreator(int countEntities, int countExtra) { + entityArr = (T[]) new Object[countEntities]; + extraArr = (U[]) new Object[countExtra]; + } + + public int Insert(T entityLocomotive) { + if(entitiesCount < entityArr.length) { + entityArr[entitiesCount] = entityLocomotive; + entitiesCount++; + return entitiesCount - 1; + } + return -1; + } + + public int Insert (U extra) { + if(extraCount < extraArr.length) { + extraArr[extraCount] = extra; + extraCount++; + return extraCount - 1; + } + return -1; + } + + public DrawningLocomotive getEntityWithExtra() { + Random random = new Random(); + int getEntityRandomIndex = random.nextInt(entityArr.length); + int getExtraRandomIndex = random.nextInt(extraArr.length); + + T locomotive = entityArr[getEntityRandomIndex]; + U extra = extraArr[getExtraRandomIndex]; + + if (locomotive instanceof EntityWarmlyLocomotive) { + return new DrawningWarmlyLocomotive(locomotive, extra); + } + return new DrawningLocomotive(locomotive, extra); + } + +} diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java index 5f28863..e14642b 100644 --- a/ExtraRoundWheelDraw.java +++ b/ExtraRoundWheelDraw.java @@ -2,6 +2,7 @@ import java.awt.*; public class ExtraRoundWheelDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; + private ExtraWheelsDraw extraWheelsDraw; public void setExtraNum(int num) { switch (num) { case 0: { @@ -17,11 +18,13 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{ } } - public ExtraRoundWheelDraw (int num) { + public ExtraRoundWheelDraw (int num, Color bodyColor) { setExtraNum(num); + extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); } public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + extraWheelsDraw.DrawExtra(startPosX, startPosY, g); g.setColor(Color.BLACK); g.fillOval(startPosX + 5, startPosY + 35, 10, 10); g.fillOval(startPosX + 95, startPosY + 35, 10, 10); diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java index dac1eb8..3b415d6 100644 --- a/ExtraStarWheelDraw.java +++ b/ExtraStarWheelDraw.java @@ -2,6 +2,7 @@ import java.awt.*; public class ExtraStarWheelDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; + private ExtraWheelsDraw extraWheelsDraw; public void setExtraNum(int num) { switch (num) { case 0: { @@ -17,11 +18,13 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ } } - public ExtraStarWheelDraw (int num) { + public ExtraStarWheelDraw (int num, Color bodyColor) { setExtraNum(num); + extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); } public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + extraWheelsDraw.DrawExtra(startPosX, startPosY, g); DrawStarOnWheel(startPosX, startPosY + 30, g); DrawStarOnWheel(startPosX + 90, startPosY + 30, g); switch (wheelsCount) { diff --git a/FormLocomotive.java b/FormLocomotive.java index ed8a82e..bab32f4 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -38,8 +38,8 @@ public class FormLocomotive extends JComponent{ modifiedButton.addActionListener(e -> { Random rnd = new Random(); - Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); - Color colorSecond = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); _locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst,