From 4284b48c00f75bd548c1426198faa648480e9c10 Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 10 Oct 2023 01:30:20 +0400 Subject: [PATCH] Lab 02 hard with dopClasses, mb ready --- .../AbstractStrategy.java | 6 +- .../DrawingElectricLocomotive.java | 7 +- .../DrawingEmptyWheels.java | 28 ++++++++ .../DrawingLocomotive.java | 65 ++++++++++++++----- .../DrawingObjectLocomotive.java | 5 +- ProjectElectricLocomotive/DrawingWheel.java | 32 +++------ .../DrawingWheelsBlueCrom.java | 35 ++++++++++ .../FormElectricLocomotive.java | 28 +++++--- ProjectElectricLocomotive/IDrawingWheels.java | 29 +++++++++ 9 files changed, 179 insertions(+), 56 deletions(-) create mode 100644 ProjectElectricLocomotive/DrawingEmptyWheels.java create mode 100644 ProjectElectricLocomotive/DrawingWheelsBlueCrom.java create mode 100644 ProjectElectricLocomotive/IDrawingWheels.java diff --git a/ProjectElectricLocomotive/AbstractStrategy.java b/ProjectElectricLocomotive/AbstractStrategy.java index 3030096..7eebaf2 100644 --- a/ProjectElectricLocomotive/AbstractStrategy.java +++ b/ProjectElectricLocomotive/AbstractStrategy.java @@ -2,7 +2,6 @@ package ProjectElectricLocomotive; public abstract class AbstractStrategy { private IMoveableObject _moveableObject; - private Status _state = Status.NotInit; protected int FieldWidth; protected int FieldHeight; @@ -52,17 +51,18 @@ public abstract class AbstractStrategy { } /// Параметры объекта - protected ObjectParameters/*?*/ GetObjectParameters(){ + protected ObjectParameters GetObjectParameters(){ if(_moveableObject == null) return null; return _moveableObject.GetObjectPosition(); } - protected int/*?*/ GetStep() + protected int GetStep() { if (_state != Status.InProgress) { return -1; } + if(_moveableObject == null) return -1; return _moveableObject.GetStep(); } protected abstract void MoveToTarget(); diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java index b7353d1..3b3e67c 100644 --- a/ProjectElectricLocomotive/DrawingElectricLocomotive.java +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -1,11 +1,10 @@ package ProjectElectricLocomotive; import java.awt.*; -public class DrawingElectricLocomotive extends DrawingLocomotive { - EntityElectricLocomotive electricLocomotive; +public class DrawingElectricLocomotive extends DrawingLocomotive { public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries, int width, int height) { - super(speed, weight, bodyColor, width, height, 85, 50); + super(speed, weight, bodyColor, width, height, 150, 50); if (EntityLocomotive != null) { EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); @@ -17,7 +16,6 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF { Color colorBlack = Color.BLACK; - /*Brush blackBrush = new SolidBrush(Color.Black);*/ Color windows = Color.BLUE; Color bodyColor = EntityLocomotive.BodyColor; Color additionalBrush = electricLocomotive.AdditionalColor; @@ -37,7 +35,6 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10); } super.DrawTransport(g); - } } } diff --git a/ProjectElectricLocomotive/DrawingEmptyWheels.java b/ProjectElectricLocomotive/DrawingEmptyWheels.java new file mode 100644 index 0000000..fea531b --- /dev/null +++ b/ProjectElectricLocomotive/DrawingEmptyWheels.java @@ -0,0 +1,28 @@ +package ProjectElectricLocomotive; + +import java.awt.*; + +public class DrawingEmptyWheels implements IDrawingWheels{ + private WheelsCount _wheelsCount; + @Override + public void SetWheelsCount(int wheelsCount) { + for (WheelsCount val : WheelsCount.values()) { + if (val.count == wheelsCount) { + _wheelsCount = val; + return; + } + } + this._wheelsCount = WheelsCount.Three; + } + + @Override + public WheelsCount GetWheelsCount() { + return _wheelsCount; + } + + @Override + public void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h) { + g2d.setColor(Color.BLACK); + g2d.drawOval(x, y, w, h); + } +} diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java index f482883..8949adc 100644 --- a/ProjectElectricLocomotive/DrawingLocomotive.java +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -1,10 +1,11 @@ package ProjectElectricLocomotive; import java.awt.*; import java.security.cert.PolicyNode; +import java.util.Random; public class DrawingLocomotive { public EntityLocomotive EntityLocomotive; - private DrawingWheel _drawingWheel; + protected IDrawingWheels _drawingWheels; private int _pictureWidth; private int _pictureHeight; public int _startPosX; @@ -34,11 +35,19 @@ public class DrawingLocomotive { _pictureWidth = width; _pictureHeight = heigth; EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + + Random rnd = new Random(); + int WhatWheels = rnd.nextInt(0, 3); + + if(WhatWheels == 0) _drawingWheels = new DrawingWheel(); + if(WhatWheels == 1) _drawingWheels = new DrawingEmptyWheels(); + if(WhatWheels == 2) _drawingWheels = new DrawingWheelsBlueCrom(); } protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int height, int locoWidth, int locoHeight) { + this(speed,weight, bodyColor, width, height); if (width < _locoWidth || height < _locoHeight) { return; @@ -47,7 +56,11 @@ public class DrawingLocomotive { _pictureHeight = height; _locoWidth = locoWidth; _locoHeight = locoHeight; - EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + // EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + } + + public void SetWheelsCount(int wheelsCount){ + _drawingWheels.SetWheelsCount(wheelsCount); } //Установка позиции @@ -118,15 +131,6 @@ public class DrawingLocomotive { g.fillPolygon(locoP); g.setColor(colorBlack); - /*locoP.addPoint(_startPosX, _startPosY + 40); - locoP.addPoint(_startPosX, _startPosY + 30); - locoP.addPoint(_startPosX + 20, _startPosY + 20); - locoP.addPoint(_startPosX + 70, _startPosY + 20); - locoP.addPoint(_startPosX +80, _startPosY + 30); - locoP.addPoint(_startPosX +80, _startPosY + 40); - locoP.addPoint(_startPosX +75, _startPosY + 45); - locoP.addPoint(_startPosX +5, _startPosY + 45); - locoP.addPoint(_startPosX, _startPosY + 40);*/ g.drawPolygon(locoP); @@ -146,10 +150,41 @@ public class DrawingLocomotive { //обязательные колеса //loco - g.fillOval(_startPosX + 10, _startPosY + 45, 5, 5); - g.fillOval(_startPosX + 25, _startPosY + 45, 5, 5); - g.fillOval(_startPosX + 50, _startPosY + 45, 5, 5); - g.fillOval(_startPosX + 65, _startPosY + 45, 5, 5); + g.fillOval(_startPosX + 10, _startPosY + 45, 9, 9); + g.fillOval(_startPosX + 25, _startPosY + 45, 9, 9); + g.fillOval(_startPosX + 50, _startPosY + 45, 9, 9); + g.fillOval(_startPosX + 65, _startPosY + 45, 9, 9); + + //telega + g.setColor(colorBlack); + g.fillOval(_startPosX + 95, _startPosY + 45, 9, 9); + g.fillOval(_startPosX + 140, _startPosY + 45, 9, 9); + + //telejka + Polygon telega = new Polygon(); + + telega.addPoint(_startPosX + 90, _startPosY + 25); + telega.addPoint(_startPosX + 95, _startPosY + 20); + telega.addPoint(_startPosX + 145, _startPosY + 20); + telega.addPoint(_startPosX + 150, _startPosY + 25); + telega.addPoint(_startPosX + 150, _startPosY + 45); + telega.addPoint(_startPosX + 90, _startPosY + 45); + telega.addPoint(_startPosX + 90, _startPosY + 25); + + g.setColor(bodyColor); + g.fillPolygon(telega); + g.setColor(colorBlack); + g.drawPolygon(telega); + //telejka + + //телега окна + g.setColor(colorBlack); + g.drawLine(_startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40); + g.setColor(windows); g.fillRect(_startPosX + 95, _startPosY + 30, 10, 5); + g.fillRect(_startPosX + 115, _startPosY + 30, 10, 5); + g.fillRect(_startPosX + 135, _startPosY + 30, 10, 5); + + _drawingWheels.DrawWheels(g, colorBlack, _startPosX, _startPosY, 9,9); } public boolean CanMove(DyrectionType direction) diff --git a/ProjectElectricLocomotive/DrawingObjectLocomotive.java b/ProjectElectricLocomotive/DrawingObjectLocomotive.java index 8f32e6a..9e34da2 100644 --- a/ProjectElectricLocomotive/DrawingObjectLocomotive.java +++ b/ProjectElectricLocomotive/DrawingObjectLocomotive.java @@ -17,12 +17,15 @@ public class DrawingObjectLocomotive implements IMoveableObject { _drawningLocomotive.GetWidth(), _drawningLocomotive.GetHeight()); } public int GetStep(){ + if(_drawningLocomotive == null) return -1; return (int)(_drawningLocomotive.EntityLocomotive.Step()); } public boolean CheckCanMove(DyrectionType direction){ + if(_drawningLocomotive == null) return false; return _drawningLocomotive.CanMove(direction); } public void MoveObject(DyrectionType direction){ - _drawningLocomotive.MoveTransport(direction); + if(_drawningLocomotive == null) return; + _drawningLocomotive.MoveTransport(direction); } } diff --git a/ProjectElectricLocomotive/DrawingWheel.java b/ProjectElectricLocomotive/DrawingWheel.java index 5e3f4d0..6806611 100644 --- a/ProjectElectricLocomotive/DrawingWheel.java +++ b/ProjectElectricLocomotive/DrawingWheel.java @@ -2,7 +2,7 @@ package ProjectElectricLocomotive; import java.awt.*; -public class DrawingWheel { +public class DrawingWheel implements IDrawingWheels { private WheelsCount _wheelsCount; public void SetWheelsCount(int wheelsCount) { for (WheelsCount val : WheelsCount.values()) { @@ -11,32 +11,16 @@ public class DrawingWheel { return; } } + this._wheelsCount = WheelsCount.Three; } - private void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h) { + @Override + public WheelsCount GetWheelsCount() { + return _wheelsCount; + } + + public void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h) { g2d.setColor(Color.BLACK); g2d.fillOval(x, y, w, h); } - - public void DrawWheels(Graphics g, Color color, int startPosX, int startPosY, int drawingWidth, int drawingHeight) { - if (_wheelsCount == null) { - return; - } - - Graphics2D g2d = (Graphics2D) g; - int wheelWidth = 5; - int wheelHeight = 5; - - if (_wheelsCount.count >= _wheelsCount.Three.count) { - DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight - ); - } - - if (_wheelsCount.count >= _wheelsCount.Four.count) { - DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight - ); - DrawWheel(g2d, color, startPosX + 130, startPosY + 45, wheelWidth, wheelHeight - ); - } - } } diff --git a/ProjectElectricLocomotive/DrawingWheelsBlueCrom.java b/ProjectElectricLocomotive/DrawingWheelsBlueCrom.java new file mode 100644 index 0000000..de53af4 --- /dev/null +++ b/ProjectElectricLocomotive/DrawingWheelsBlueCrom.java @@ -0,0 +1,35 @@ +package ProjectElectricLocomotive; + +import java.awt.*; + +public class DrawingWheelsBlueCrom implements IDrawingWheels{ + private WheelsCount _wheelsCount; + @Override + public void SetWheelsCount(int wheelsCount) { + for (WheelsCount val : WheelsCount.values()) { + if (val.count == wheelsCount) { + _wheelsCount = val; + return; + } + } + this._wheelsCount = WheelsCount.Three; + } + + @Override + public WheelsCount GetWheelsCount() { + return _wheelsCount; + } + + @Override + public void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h) { + g2d.setColor(Color.BLUE); + g2d.drawOval(x, y + 3, w + 2, h + 2); + g2d.setColor(Color.RED); + g2d.drawOval(x - 2, y - 2, w + 2, h + 2); + g2d.setColor(Color.MAGENTA); + g2d.drawOval(x + 3, y, w + 3, h + 3); + + g2d.setColor(Color.BLACK); + g2d.drawOval(x, y, w, h); + } +} diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index ee0b9fa..2437878 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -6,7 +6,8 @@ import java.awt.event.ActionListener; import java.util.Random; public class FormElectricLocomotive { - //DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive(); + DrawingLocomotive _drawingLocomotive; + AbstractStrategy _abstractStrategy; private JButton buttonCreateElectricLocomotive; private JPanel pictureBox; private JButton buttonUp; @@ -16,8 +17,6 @@ public class FormElectricLocomotive { public JComboBox comboBoxStrategy; private JButton buttonStep; private JButton buttonCreateLocomotive; - private DrawingLocomotive _drawingLocomotive; - private AbstractStrategy _abstractStrategy; public JPanel getPictureBox() { return pictureBox; @@ -31,22 +30,35 @@ public class FormElectricLocomotive { buttonCreateLocomotive.addActionListener(e -> { Random rnd = new Random(); - _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), rnd.nextInt(1000, 3000), - new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), - pictureBox.getWidth(), pictureBox.getHeight()); + _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + new Color(rnd.nextInt(0, 256), + rnd.nextInt(0, 256), + rnd.nextInt(0, 256)), + pictureBox.getWidth(), + pictureBox.getHeight()); + + _drawingLocomotive.SetWheelsCount(rnd.nextInt(2, 5)); _drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); Draw(); }); buttonCreateElectricLocomotive.addActionListener(e -> { Random random = new Random(); - _drawingLocomotive = new DrawingElectricLocomotive(random.nextInt(100, 300),random.nextInt(1000, 3000), + _drawingLocomotive = new DrawingElectricLocomotive( + 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)), random.nextBoolean(), random.nextBoolean(), - pictureBox.getWidth(), pictureBox.getHeight()); + pictureBox.getWidth(), + pictureBox.getHeight() + ); + + _drawingLocomotive.SetWheelsCount(random.nextInt(2, 5)); _drawingLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + //drawingElectricLocomotive.SetWheelsCount(random.nextInt(2,5)); Draw(); }); diff --git a/ProjectElectricLocomotive/IDrawingWheels.java b/ProjectElectricLocomotive/IDrawingWheels.java new file mode 100644 index 0000000..9faad6f --- /dev/null +++ b/ProjectElectricLocomotive/IDrawingWheels.java @@ -0,0 +1,29 @@ +package ProjectElectricLocomotive; + +import java.awt.*; + +public interface IDrawingWheels { + void SetWheelsCount(int wheelsCount); + + WheelsCount GetWheelsCount(); + + // WheelsCount GetWheelsCount(); + + void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h); + + default void DrawWheels(Graphics g, Color color, int startPosX, int startPosY, int drawingWidth, int drawingHeight){ + WheelsCount wheelsCount = GetWheelsCount(); + if(wheelsCount == null) return; + Graphics2D g2d = (Graphics2D) g; + int wheelWidth = 9; + int wheelHeight = 9; + + if (wheelsCount.count >= wheelsCount.Three.count) { + DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight); + } + if (wheelsCount.count >= wheelsCount.Four.count) { + DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight); + DrawWheel(g2d, color, startPosX + 130, startPosY + 45, wheelWidth, wheelHeight); + } + } +}