From 400672b6ddca6b39288fb4ac0dbc75d9a5a1f516 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 23:13:09 +0400 Subject: [PATCH] Added ExtraObjects & their Init. --- DrawningLocomotive.java | 19 ++++++++++------ ExtraRoundWheelDraw.java | 40 ++++++++++++++++++++++++++++++++++ ExtraStarWheelDraw.java | 47 ++++++++++++++++++++++++++++++++++++++++ ExtraWheelsDraw.java | 16 ++++++++++---- 4 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 ExtraRoundWheelDraw.java create mode 100644 ExtraStarWheelDraw.java diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index ae45dbd..7e3054c 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -3,7 +3,7 @@ import java.util.Random; class DrawningLocomotive { public EntityLocomotive Locomotive; - public ExtraWheelsDraw extraWheelsDraw; + public IDrawningExtra[] drawningExtra; /// Левая координата отрисовки локомотива protected float _startPosX; /// Верхняя координата отрисовки локомотива @@ -20,7 +20,10 @@ class DrawningLocomotive { private final Random random = new Random(); public DrawningLocomotive(int speed, float weight, Color bodyColor) { - extraWheelsDraw = new ExtraWheelsDraw(random.nextInt(3), bodyColor); + int randExtra = random.nextInt(3); + drawningExtra = new IDrawningExtra[2]; + drawningExtra[0] = new ExtraWheelsDraw(randExtra, bodyColor); + drawningExtra[1] = random.nextBoolean() ? new ExtraRoundWheelDraw(randExtra) : new ExtraStarWheelDraw(randExtra); Locomotive = new EntityLocomotive(speed, weight, bodyColor); } @@ -99,18 +102,20 @@ class DrawningLocomotive { return; } //тело - g.setColor(Color.BLACK); - g.drawRect((int)_startPosX , (int)_startPosY, 110 - 10, 50 - 10); - //окна g.setColor(Locomotive.getBodyColor()); + g.fillRect((int)_startPosX , (int)_startPosY, 110 - 10, 50 - 10); + //окна + g.setColor(Color.BLUE); g.fillRect((int)_startPosX + 10, (int)_startPosY + 10, 10, 10); g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 10); g.fillRect((int)_startPosX + 80, (int)_startPosY + 10, 10, 10); //дверь g.setColor(Color.BLACK); - g.drawRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); + g.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); //колеса - extraWheelsDraw.DrawExtra((int)_startPosX, (int)_startPosY, g); + drawningExtra[0].DrawExtra((int)_startPosX, (int)_startPosY, g); + //extra + drawningExtra[1].DrawExtra((int)_startPosX, (int)_startPosY, g); //движок g.setColor(Locomotive.getBodyColor()); g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30); diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java new file mode 100644 index 0000000..5f28863 --- /dev/null +++ b/ExtraRoundWheelDraw.java @@ -0,0 +1,40 @@ +import java.awt.*; + +public class ExtraRoundWheelDraw implements IDrawningExtra{ + private WheelsCount wheelsCount = WheelsCount.Two; + public void setExtraNum(int num) { + switch (num) { + case 0: { + wheelsCount = WheelsCount.Three; + break; + } + case 1: { + wheelsCount = WheelsCount.Four; + break; + } + default: + break; + } + } + + public ExtraRoundWheelDraw (int num) { + setExtraNum(num); + } + + public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + g.setColor(Color.BLACK); + g.fillOval(startPosX + 5, startPosY + 35, 10, 10); + g.fillOval(startPosX + 95, startPosY + 35, 10, 10); + switch (wheelsCount) { + case Four: { + g.fillOval(startPosX + 75, startPosY + 35, 10, 10); + } + case Three: { + g.fillOval(startPosX + 25, startPosY + 35, 10, 10); + break; + } + default: + break; + } + } +} diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java new file mode 100644 index 0000000..dac1eb8 --- /dev/null +++ b/ExtraStarWheelDraw.java @@ -0,0 +1,47 @@ +import java.awt.*; + +public class ExtraStarWheelDraw implements IDrawningExtra{ + private WheelsCount wheelsCount = WheelsCount.Two; + public void setExtraNum(int num) { + switch (num) { + case 0: { + wheelsCount = WheelsCount.Three; + break; + } + case 1: { + wheelsCount = WheelsCount.Four; + break; + } + default: + break; + } + } + + public ExtraStarWheelDraw (int num) { + setExtraNum(num); + } + + public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + DrawStarOnWheel(startPosX, startPosY + 30, g); + DrawStarOnWheel(startPosX + 90, startPosY + 30, g); + switch (wheelsCount) { + case Four: { + DrawStarOnWheel(startPosX + 70, startPosY + 30, g); + } + case Three: { + DrawStarOnWheel(startPosX + 20, startPosY + 30, g); + break; + } + default: + break; + } + } + + private void DrawStarOnWheel(int startPosX, int startPosY, Graphics2D g) { + g.drawLine(startPosX + 10, startPosY, startPosX + 15, startPosY + 17); + g.drawLine(startPosX + 10, startPosY, startPosX + 5, startPosY + 17); + g.drawLine(startPosX + 15, startPosY + 17, startPosX + 2, startPosY + 8); + g.drawLine(startPosX + 5, startPosY + 17, startPosX + 18, startPosY + 8); + g.drawLine(startPosX + 2, startPosY + 8, startPosX + 18, startPosY + 8); + } +} diff --git a/ExtraWheelsDraw.java b/ExtraWheelsDraw.java index abe9d15..a515643 100644 --- a/ExtraWheelsDraw.java +++ b/ExtraWheelsDraw.java @@ -24,15 +24,23 @@ public class ExtraWheelsDraw implements IDrawningExtra{ } public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + g.setColor(Color.BLACK); + g.drawOval(startPosX, startPosY + 30, 20, 20); + g.drawOval(startPosX + 90, startPosY + 30, 20, 20); g.setColor(color); - g.drawOval(startPosX, startPosY + 40, 10, 10); - g.drawOval(startPosX + 90, startPosY + 40, 10, 10); + g.fillOval(startPosX, startPosY + 30, 20, 20); + g.fillOval(startPosX + 90, startPosY + 30, 20, 20); switch (wheelsCount) { case Four: { - g.drawOval(startPosX + 70, startPosY + 40, 10, 10); + g.setColor(Color.BLACK); + g.drawOval(startPosX + 70, startPosY + 30, 20, 20); + g.setColor(color); + g.fillOval(startPosX + 70, startPosY + 30, 20, 20); } case Three: { - g.drawOval(startPosX + 20, startPosY + 40, 10, 10); + g.fillOval(startPosX + 20, startPosY + 30, 20, 20); + g.setColor(Color.BLACK); + g.drawOval(startPosX + 20, startPosY + 30, 20, 20); break; } default: