From e89ef6601088c991ddbe225386db2615c3116c2c Mon Sep 17 00:00:00 2001 From: ekallin Date: Sun, 8 Oct 2023 20:20:04 +0400 Subject: [PATCH 01/10] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectElectricLocomotive/DrawingElectricLocomotive.java | 8 ++++---- ProjectElectricLocomotive/DrawingWheel.java | 6 +++--- ProjectElectricLocomotive/FormElectricLocomotive.form | 7 +++++-- .../MainFrameElectricLocomotive.java | 2 +- ProjectElectricLocomotive/WheelsCount.java | 2 +- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java index 027745f..5a40613 100644 --- a/ProjectElectricLocomotive/DrawingElectricLocomotive.java +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -26,19 +26,19 @@ public class DrawingElectricLocomotive { return true; } - public void SetWheelsCount(int weelsCount) { - _drawingWheel.SetWheelsCount(weelsCount); + public void SetWheelsCount(int wheelsCount) { + _drawingWheel.SetWheelsCount(wheelsCount); } public void SetPosition(int x, int y) { if (x < 0 || x + _locoWidth > _pictureWidth) { - x = 20; + x = _pictureWidth - _locoWidth; } if (y < 0 || y + _locoHeight > _pictureHeight) { - y = 20; + y = _pictureHeight - _locoHeight; } _startPosX = x; _startPosY = y; diff --git a/ProjectElectricLocomotive/DrawingWheel.java b/ProjectElectricLocomotive/DrawingWheel.java index f4ee669..5e3f4d0 100644 --- a/ProjectElectricLocomotive/DrawingWheel.java +++ b/ProjectElectricLocomotive/DrawingWheel.java @@ -4,10 +4,10 @@ import java.awt.*; public class DrawingWheel { private WheelsCount _wheelsCount; - public void SetWheelsCount(int enginesCount) { + public void SetWheelsCount(int wheelsCount) { for (WheelsCount val : WheelsCount.values()) { - if (val.count == enginesCount) { - this._wheelsCount = val; + if (val.count == wheelsCount) { + _wheelsCount = val; return; } } diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.form b/ProjectElectricLocomotive/FormElectricLocomotive.form index 9908b3b..24ba903 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.form +++ b/ProjectElectricLocomotive/FormElectricLocomotive.form @@ -3,9 +3,12 @@ - + - + + + + diff --git a/ProjectElectricLocomotive/MainFrameElectricLocomotive.java b/ProjectElectricLocomotive/MainFrameElectricLocomotive.java index 629d685..9dcfab4 100644 --- a/ProjectElectricLocomotive/MainFrameElectricLocomotive.java +++ b/ProjectElectricLocomotive/MainFrameElectricLocomotive.java @@ -12,7 +12,7 @@ public class MainFrameElectricLocomotive extends JFrame { _formElectricLocomotive = new FormElectricLocomotive(); setContentPane(_formElectricLocomotive.getPictureBox()); setDefaultLookAndFeelDecorated(false); - setLocation(500, 50); + setLocation(500, 200); pack(); setVisible(true); } diff --git a/ProjectElectricLocomotive/WheelsCount.java b/ProjectElectricLocomotive/WheelsCount.java index 08e4f6c..0fa62ef 100644 --- a/ProjectElectricLocomotive/WheelsCount.java +++ b/ProjectElectricLocomotive/WheelsCount.java @@ -1,7 +1,7 @@ package ProjectElectricLocomotive; public enum WheelsCount { - Two(2), Three(3), Four(4); + /* Two(2),*/ Three(3), Four(4); public final int count; WheelsCount(int count) { this.count = count; -- 2.25.1 From 31d82d2a9501ccf9598d93cdc41519d1147e6d3d Mon Sep 17 00:00:00 2001 From: ekallin Date: Sun, 8 Oct 2023 20:49:50 +0400 Subject: [PATCH 02/10] ObjectParameters class completed --- ObjectParameters.java | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 ObjectParameters.java diff --git a/ObjectParameters.java b/ObjectParameters.java new file mode 100644 index 0000000..0573525 --- /dev/null +++ b/ObjectParameters.java @@ -0,0 +1,50 @@ +public class ObjectParameters { + private final int _x; + private final int _y; + private final int _width; + private final int _height; + + /// Левая граница + public int LeftBorder() + { + return _x; + }; + + /// Верхняя граница + public int TopBorder() + { + return _y; + } + + /// Правая граница + public int RightBorder() + { + return _x + _width; + } + + /// Нижняя граница + public int DownBorder() + { + return _y + _height; + } + + /// Середина объекта по горизонтали + public int ObjectMiddleHorizontal() + { + return _x + _width / 2; + } + + /// Середина объекта по вертикали + public int ObjectMiddleVertical() + { + return _y + _height / 2; + } + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} -- 2.25.1 From 93622be5ba62a2b6f18d734025f9bdae86bbc350 Mon Sep 17 00:00:00 2001 From: ekallin Date: Mon, 9 Oct 2023 20:56:26 +0400 Subject: [PATCH 03/10] Lab 02 hard without dopClasses --- .../AbstractStrategy.java | 84 ++++++++ .../DrawingElectricLocomotive.java | 188 +++--------------- .../DrawingLocomotive.java | 178 +++++++++++++++++ .../DrawingObjectLocomotive.java | 28 +++ ProjectElectricLocomotive/DyrectionType.java | 2 +- .../EntityElectricLocomotive.java | 18 +- .../EntityLocomotive.java | 25 +++ .../FormElectricLocomotive.form | 99 +++++---- .../FormElectricLocomotive.java | 98 ++++++--- .../IMoveableObject.java | 10 + ProjectElectricLocomotive/MoveToCenter.java | 45 +++++ .../MoveToRigthCorner.java | 23 +++ .../ObjectParameters.java | 1 + ProjectElectricLocomotive/Status.java | 7 + 14 files changed, 569 insertions(+), 237 deletions(-) create mode 100644 ProjectElectricLocomotive/AbstractStrategy.java create mode 100644 ProjectElectricLocomotive/DrawingLocomotive.java create mode 100644 ProjectElectricLocomotive/DrawingObjectLocomotive.java create mode 100644 ProjectElectricLocomotive/EntityLocomotive.java create mode 100644 ProjectElectricLocomotive/IMoveableObject.java create mode 100644 ProjectElectricLocomotive/MoveToCenter.java create mode 100644 ProjectElectricLocomotive/MoveToRigthCorner.java rename ObjectParameters.java => ProjectElectricLocomotive/ObjectParameters.java (96%) create mode 100644 ProjectElectricLocomotive/Status.java diff --git a/ProjectElectricLocomotive/AbstractStrategy.java b/ProjectElectricLocomotive/AbstractStrategy.java new file mode 100644 index 0000000..3030096 --- /dev/null +++ b/ProjectElectricLocomotive/AbstractStrategy.java @@ -0,0 +1,84 @@ +package ProjectElectricLocomotive; + +public abstract class AbstractStrategy { + private IMoveableObject _moveableObject; + + private Status _state = Status.NotInit; + protected int FieldWidth; + protected int FieldHeight; + public Status GetStatus() { return _state; } + + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + + protected boolean MoveLeft(){ + return MoveTo(DyrectionType.Left); + } + + protected boolean MoveRight(){ + return MoveTo(DyrectionType.Right); + } + + protected boolean MoveUp(){ + return MoveTo(DyrectionType.Up); + } + + protected boolean MoveDown(){ + return MoveTo(DyrectionType.Down); + } + + /// Параметры объекта + protected ObjectParameters/*?*/ GetObjectParameters(){ + if(_moveableObject == null) return null; + return _moveableObject.GetObjectPosition(); + } + + protected int/*?*/ GetStep() + { + if (_state != Status.InProgress) + { + return -1; + } + return _moveableObject.GetStep(); + } + protected abstract void MoveToTarget(); + + protected abstract boolean IsTargetDestinaion(); + + private boolean MoveTo(DyrectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(directionType) == false) return false; + { + _moveableObject.MoveObject(directionType); + return true; + } + } +} diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java index 5a40613..b7353d1 100644 --- a/ProjectElectricLocomotive/DrawingElectricLocomotive.java +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -1,173 +1,43 @@ package ProjectElectricLocomotive; import java.awt.*; -public class DrawingElectricLocomotive { - public EntityElectricLocomotive EntityElectricLocomotive; - private DrawingWheel _drawingWheel; - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private final int _locoWidth = 150; - private final int _locoHeight = 50; - - public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, - boolean horns, boolean seifbatteries, int width, int height) +public class DrawingElectricLocomotive extends DrawingLocomotive { + EntityElectricLocomotive electricLocomotive; + public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, + boolean horns, boolean seifBatteries, int width, int height) { - - if (width < _locoWidth || height < _locoHeight) + super(speed, weight, bodyColor, width, height, 85, 50); + if (EntityLocomotive != null) { - return false; + EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); } - _pictureWidth = width; - _pictureHeight = height; - EntityElectricLocomotive = new EntityElectricLocomotive(); - _drawingWheel = new DrawingWheel(); - EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns, seifbatteries); - return true; } - - public void SetWheelsCount(int wheelsCount) { - _drawingWheel.SetWheelsCount(wheelsCount); - } - - public void SetPosition(int x, int y) + @Override + public void DrawTransport(Graphics g) { - if (x < 0 || x + _locoWidth > _pictureWidth) + if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF { - x = _pictureWidth - _locoWidth; - } - if (y < 0 || y + _locoHeight > _pictureHeight) - { - y = _pictureHeight - _locoHeight; - } - _startPosX = x; - _startPosY = y; - } + Color colorBlack = Color.BLACK; + /*Brush blackBrush = new SolidBrush(Color.Black);*/ + Color windows = Color.BLUE; + Color bodyColor = EntityLocomotive.BodyColor; + Color additionalBrush = electricLocomotive.AdditionalColor; - public void MoveTransport(DyrectionType direction){ - if(EntityElectricLocomotive == null) return; - switch(direction) - { - case Up -> { - if(_startPosY - EntityElectricLocomotive.Step() >= 0) - _startPosY -= (int) EntityElectricLocomotive.Step(); + if (electricLocomotive.Horns) { + //horns + g.setColor(colorBlack); + g.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); + g.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); + g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); + g.drawLine(_startPosX + 45, _startPosY + 15, _startPosX + 50, _startPosY + 10); + g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 40, _startPosY); } - case Down -> { - if(_startPosY + EntityElectricLocomotive.Step() + _locoHeight <= _pictureHeight) - _startPosY += (int) EntityElectricLocomotive.Step(); - } - case Left -> { - if(_startPosX - EntityElectricLocomotive.Step() >= 0) - _startPosX -= (int) EntityElectricLocomotive.Step(); - } - case Right -> { - if(_startPosX + EntityElectricLocomotive.Step() + _locoWidth <= _pictureWidth) - _startPosX += (int) EntityElectricLocomotive.Step(); + + if (electricLocomotive.SeifBatteries) { + g.setColor(colorBlack); + g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10); } + super.DrawTransport(g); + } } - public void DrawTransport(Graphics g) { - if (EntityElectricLocomotive == null) { - return; - } - - Graphics2D g2d = (Graphics2D) g; - - Color bodyColor = EntityElectricLocomotive.BodyColor; - Color additionalColor = EntityElectricLocomotive.AdditionalColor; - Color blackBrush = Color.BLACK; - Color windowsColor = Color.BLUE; - - _drawingWheel.DrawWheels(g, additionalColor, _startPosX, _startPosY, 5, 5); - - if(EntityElectricLocomotive.Horns) - { - g2d.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); - g2d.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); - g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); - g2d.drawLine(_startPosX + 45, _startPosY + 15, _startPosX + 50, _startPosY + 10); - g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 40, _startPosY); - g2d.setColor(blackBrush); - } - - //locomotive - Polygon loco = new Polygon(); - - loco.addPoint(_startPosX, _startPosY + 40); - loco.addPoint(_startPosX, _startPosY + 30); - loco.addPoint(_startPosX + 20, _startPosY + 20); - loco.addPoint(_startPosX + 70, _startPosY + 20); - loco.addPoint(_startPosX +80, _startPosY + 30); - loco.addPoint(_startPosX +80, _startPosY + 40); - loco.addPoint(_startPosX +75, _startPosY + 45); - loco.addPoint(_startPosX +5, _startPosY + 45); - loco.addPoint(_startPosX, _startPosY + 40); - - g2d.setColor(blackBrush); - g2d.drawPolygon(loco); - g2d.setColor(bodyColor); - g2d.fillPolygon(loco); - // windows - Polygon window = new Polygon(); - window.addPoint(_startPosX + 10, _startPosY + 30); - window.addPoint(_startPosX +15, _startPosY + 25); - window.addPoint(_startPosX + 20, _startPosY + 25); - window.addPoint(_startPosX + 20, _startPosY + 30); - window.addPoint(_startPosX +10, _startPosY + 30); - - g2d.setColor(blackBrush); - g2d.drawPolygon(window); - g2d.setColor(windowsColor); - g2d.fillPolygon(window); - - g2d.fillRect(_startPosX + 25, _startPosY + 25, 10, 5); - g2d.setColor(windowsColor); - g2d.drawRect(_startPosX + 25, _startPosY + 25, 10, 5); - g2d.setColor(blackBrush); - //locomotive - - if(EntityElectricLocomotive.SeifBatteries) - { - g2d.drawRect(_startPosX + 50, _startPosY + 25, 20, 10); - g2d.setColor(blackBrush); - } - - - //обязательные колеса - //loco - g2d.fillOval(_startPosX + 10, _startPosY + 45, 5, 5); - g2d.fillOval(_startPosX + 25, _startPosY + 45, 5, 5); - g2d.fillOval(_startPosX + 50, _startPosY + 45, 5, 5); - g2d.fillOval(_startPosX + 65, _startPosY + 45, 5, 5); - - - //telega - g2d.setColor(blackBrush); - g2d.fillOval(_startPosX + 95, _startPosY + 45, 5, 5); - g2d.fillOval(_startPosX + 140, _startPosY + 45, 5, 5); - - //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); - - g2d.setColor(additionalColor); - g2d.fillPolygon(telega); - g2d.setColor(blackBrush); - g2d.drawPolygon(telega); - //telejka - - //телега окна - g2d.setColor(blackBrush); - g.drawLine(_startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40); - g2d.setColor(windowsColor); g.fillRect(_startPosX + 95, _startPosY + 30, 10, 5); - g.fillRect(_startPosX + 115, _startPosY + 30, 10, 5); - g.fillRect(_startPosX + 135, _startPosY + 30, 10, 5); - } } diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java new file mode 100644 index 0000000..f482883 --- /dev/null +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -0,0 +1,178 @@ +package ProjectElectricLocomotive; +import java.awt.*; +import java.security.cert.PolicyNode; + +public class DrawingLocomotive { + public EntityLocomotive EntityLocomotive; + private DrawingWheel _drawingWheel; + private int _pictureWidth; + private int _pictureHeight; + public int _startPosX; + public int _startPosY; + private int _locoWidth = 150; + private int _locoHeight = 50; + + public int GetPosX(){ + return _startPosX; + } + public int GetPosY(){ + return _startPosY; + } + public int GetWidth(){ + return _locoWidth; + } + public int GetHeight(){ + return _locoHeight; + } + + public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth) + { + if (width < _locoWidth || heigth < _locoHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = heigth; + EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + } + + protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width, + int height, int locoWidth, int locoHeight) + { + if (width < _locoWidth || height < _locoHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _locoWidth = locoWidth; + _locoHeight = locoHeight; + EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + } + + //Установка позиции + public void SetPosition(int x, int y) + { + if (x < 0 || x + _locoWidth > _pictureWidth) + { + x = _pictureWidth - _locoWidth; + } + if (y < 0 || y + _locoHeight > _pictureHeight) + { + y = _pictureHeight - _locoHeight; + } + _startPosX = x; + _startPosY = y; + } + + public void MoveTransport(DyrectionType direction) { + if (EntityLocomotive == null) { + return; + } + switch (direction) { + case Left: + if (_startPosX - EntityLocomotive.Step() > 0) { + _startPosX -= (int) EntityLocomotive.Step(); + } + break; + case Up: + if (_startPosY - EntityLocomotive.Step() > 0) { + _startPosY -= (int) EntityLocomotive.Step(); + } + break; + case Right: + if (_startPosX + EntityLocomotive.Step() + _locoWidth < _pictureWidth) { + _startPosX += (int) EntityLocomotive.Step(); + } + break; + case Down: + if (_startPosY + EntityLocomotive.Step() + _locoHeight < _pictureHeight) { + _startPosY += (int) EntityLocomotive.Step(); + } + break; + } + } + public void DrawTransport(Graphics g) + { + { + if (EntityLocomotive == null) return; + } + + Color colorBlack = Color.BLACK; + /*Brush blackBrush = new SolidBrush(Color.Black);*/ + Color windows = Color.BLUE; + Color bodyColor = EntityLocomotive.BodyColor; + + //локомотив + g.setColor(bodyColor); + Polygon locoP = new Polygon(); + 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.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); + + + //окошки + Polygon window = new Polygon(); + window.addPoint(_startPosX + 10, _startPosY + 30); + window.addPoint(_startPosX +15, _startPosY + 25); + window.addPoint(_startPosX + 20, _startPosY + 25); + window.addPoint(_startPosX + 20, _startPosY + 30); + window.addPoint(_startPosX +10, _startPosY + 30); + g.setColor(windows); + g.fillPolygon(window); + g.fillRect(_startPosX + 25, _startPosY + 25, 10, 5); + g.setColor(Color.black); + g.drawPolygon(window); + g.drawRect(_startPosX + 25, _startPosY + 25, 10, 5); + + //обязательные колеса + //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); + } + + public boolean CanMove(DyrectionType direction) + { + if (EntityLocomotive == null) + { + return false; + } + switch(direction) { + //влево + case Left: + if (_startPosX - EntityLocomotive.Step() > 0) return true; + break; + case Up: + if (_startPosY - EntityLocomotive.Step() > 0) return true; + break; + case Right: + if (_startPosX + EntityLocomotive.Step() < _pictureWidth) return true; + break; + case Down: + if (_startPosY + EntityLocomotive.Step() < _pictureHeight) return true; + break; + } + return false; + } +} diff --git a/ProjectElectricLocomotive/DrawingObjectLocomotive.java b/ProjectElectricLocomotive/DrawingObjectLocomotive.java new file mode 100644 index 0000000..8f32e6a --- /dev/null +++ b/ProjectElectricLocomotive/DrawingObjectLocomotive.java @@ -0,0 +1,28 @@ +package ProjectElectricLocomotive; + +public class DrawingObjectLocomotive implements IMoveableObject { + private DrawingLocomotive _drawningLocomotive = null; + + public DrawingObjectLocomotive(DrawingLocomotive drawningLocomotive) + { + _drawningLocomotive = drawningLocomotive; + } + public ObjectParameters GetObjectPosition() + { + if (_drawningLocomotive == null || _drawningLocomotive.EntityLocomotive == null) + { + return null; + } + return new ObjectParameters(_drawningLocomotive.GetPosX(), _drawningLocomotive.GetPosY(), + _drawningLocomotive.GetWidth(), _drawningLocomotive.GetHeight()); + } + public int GetStep(){ + return (int)(_drawningLocomotive.EntityLocomotive.Step()); + } + public boolean CheckCanMove(DyrectionType direction){ + return _drawningLocomotive.CanMove(direction); + } + public void MoveObject(DyrectionType direction){ + _drawningLocomotive.MoveTransport(direction); + } +} diff --git a/ProjectElectricLocomotive/DyrectionType.java b/ProjectElectricLocomotive/DyrectionType.java index d833f54..f08a494 100644 --- a/ProjectElectricLocomotive/DyrectionType.java +++ b/ProjectElectricLocomotive/DyrectionType.java @@ -1,5 +1,5 @@ package ProjectElectricLocomotive; public enum DyrectionType { - Up, Down, Left, Right + Up, Down, Left, Right; } diff --git a/ProjectElectricLocomotive/EntityElectricLocomotive.java b/ProjectElectricLocomotive/EntityElectricLocomotive.java index 492585f..2be3ca3 100644 --- a/ProjectElectricLocomotive/EntityElectricLocomotive.java +++ b/ProjectElectricLocomotive/EntityElectricLocomotive.java @@ -2,25 +2,17 @@ package ProjectElectricLocomotive; import java.awt.*; -public class EntityElectricLocomotive { - public int Speed; - public double Weight; - public Color BodyColor; +public class EntityElectricLocomotive extends EntityLocomotive { public Color AdditionalColor; public boolean Horns; + public boolean SeifBatteries; - public double Step() + public EntityElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries) { - return (double) Speed * 100 / Weight; - } - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, - boolean horns, boolean seifBatteries) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; + super(speed, weight, bodyColor); AdditionalColor = additionalColor; Horns = horns; SeifBatteries = seifBatteries; } + } diff --git a/ProjectElectricLocomotive/EntityLocomotive.java b/ProjectElectricLocomotive/EntityLocomotive.java new file mode 100644 index 0000000..aaf6deb --- /dev/null +++ b/ProjectElectricLocomotive/EntityLocomotive.java @@ -0,0 +1,25 @@ +package ProjectElectricLocomotive; + +import java.awt.*; + +public class EntityLocomotive { + public int Speed; + public double Weight; + public Color BodyColor; + + public EntityLocomotive(int speed, double weight, Color bodyColor) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + + public double Step() + { + return (double) Speed * 100 / Weight; + } + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifbatteries) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.form b/ProjectElectricLocomotive/FormElectricLocomotive.form index 24ba903..9b1c126 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.form +++ b/ProjectElectricLocomotive/FormElectricLocomotive.form @@ -1,64 +1,62 @@
- + - + - - + + - - - - - - + - + - - - - - + - - - - + - + - - - - - + - - - - + + + + + + + + + + + + + + + + + + + + + - - - - + @@ -71,9 +69,24 @@ + + + + + + + + + + + + + + + - + @@ -90,13 +103,23 @@ - + - + + + + + - + + + + + + + diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index 49f6cf2..ee0b9fa 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -1,17 +1,23 @@ package ProjectElectricLocomotive; import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class FormElectricLocomotive { - DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive(); - private JButton buttonCreate; + //DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive(); + private JButton buttonCreateElectricLocomotive; private JPanel pictureBox; private JButton buttonUp; private JButton buttonDown; private JButton buttonLeft; private JButton buttonRight; + public JComboBox comboBoxStrategy; + private JButton buttonStep; + private JButton buttonCreateLocomotive; + private DrawingLocomotive _drawingLocomotive; + private AbstractStrategy _abstractStrategy; public JPanel getPictureBox() { return pictureBox; @@ -23,41 +29,82 @@ public class FormElectricLocomotive { buttonLeft.setName("buttonLeft"); buttonRight.setName("buttonRight"); - buttonCreate.addActionListener(e -> { - _drawingElectricLocomotive = new DrawingElectricLocomotive(); - Random random = new Random(); - - _drawingElectricLocomotive.Init( - random.nextInt(100, 300), - random.nextInt(1000, 3000), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - random.nextBoolean(), - random.nextBoolean(), - pictureBox.getWidth(), - pictureBox.getHeight() - ); - - _drawingElectricLocomotive.SetWheelsCount(random.nextInt(2, 5)); - _drawingElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + 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.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), + 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()); + _drawingLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + Draw(); + }); + + buttonStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawingLocomotive == null) { + return; + } + if (comboBoxStrategy.isEnabled()) { + switch(comboBoxStrategy.getSelectedIndex()) + { + case 0: + _abstractStrategy = new MoveToCenter(); + break; + case 1: + _abstractStrategy = new MoveToRigthCorner(); + break; + default: + _abstractStrategy = null; + break; + } ; + if (_abstractStrategy == null) { + return; + } + _abstractStrategy.SetData(new + DrawingObjectLocomotive(_drawingLocomotive), pictureBox.getWidth(), + pictureBox.getHeight()); + comboBoxStrategy.setEnabled(false); + } + if (_abstractStrategy == null) { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } + } + }); + + ActionListener buttonMoveClickedListener = e -> { String buttonName = ((JButton) e.getSource()).getName(); switch (buttonName) { case ("buttonUp") -> { - _drawingElectricLocomotive.MoveTransport(DyrectionType.Up); + _drawingLocomotive.MoveTransport(DyrectionType.Up); } case ("buttonDown") -> { - _drawingElectricLocomotive.MoveTransport(DyrectionType.Down); + _drawingLocomotive.MoveTransport(DyrectionType.Down); } case ("buttonLeft") -> { - _drawingElectricLocomotive.MoveTransport(DyrectionType.Left); + _drawingLocomotive.MoveTransport(DyrectionType.Left); } case ("buttonRight") -> { - _drawingElectricLocomotive.MoveTransport(DyrectionType.Right); + _drawingLocomotive.MoveTransport(DyrectionType.Right); } } Draw(); @@ -68,12 +115,11 @@ public class FormElectricLocomotive { buttonRight.addActionListener(buttonMoveClickedListener); } public void Draw() { - if (_drawingElectricLocomotive.EntityElectricLocomotive == null) { + if (_drawingLocomotive.EntityLocomotive == null) { return; } Graphics g = pictureBox.getGraphics(); pictureBox.paint(g); - _drawingElectricLocomotive.DrawTransport(g); + _drawingLocomotive.DrawTransport(g); } - } diff --git a/ProjectElectricLocomotive/IMoveableObject.java b/ProjectElectricLocomotive/IMoveableObject.java new file mode 100644 index 0000000..c102340 --- /dev/null +++ b/ProjectElectricLocomotive/IMoveableObject.java @@ -0,0 +1,10 @@ +package ProjectElectricLocomotive; +import java.awt.*; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + + boolean CheckCanMove(DyrectionType direction); + void MoveObject(DyrectionType direction); +} diff --git a/ProjectElectricLocomotive/MoveToCenter.java b/ProjectElectricLocomotive/MoveToCenter.java new file mode 100644 index 0000000..405efed --- /dev/null +++ b/ProjectElectricLocomotive/MoveToCenter.java @@ -0,0 +1,45 @@ +package ProjectElectricLocomotive; + +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) return false; + + return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal() + GetStep() + >= FieldWidth / 2 && objParams.ObjectMiddleVertical() <= FieldHeight / 2 && objParams.ObjectMiddleVertical() + + GetStep() >= FieldHeight / 2; + } + + @Override + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) return; + var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectElectricLocomotive/MoveToRigthCorner.java b/ProjectElectricLocomotive/MoveToRigthCorner.java new file mode 100644 index 0000000..67b4ce5 --- /dev/null +++ b/ProjectElectricLocomotive/MoveToRigthCorner.java @@ -0,0 +1,23 @@ +package ProjectElectricLocomotive; + +public class MoveToRigthCorner extends AbstractStrategy { + @Override + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) return false; + + return objParams.RightBorder() >= FieldWidth - GetStep() && objParams.DownBorder() >= FieldHeight - GetStep(); + } + + @Override + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) return; + + if (objParams.RightBorder() < FieldWidth - GetStep()) MoveRight(); + if (objParams.DownBorder() < FieldHeight - GetStep()) MoveDown(); + + } +} diff --git a/ObjectParameters.java b/ProjectElectricLocomotive/ObjectParameters.java similarity index 96% rename from ObjectParameters.java rename to ProjectElectricLocomotive/ObjectParameters.java index 0573525..ffa5140 100644 --- a/ObjectParameters.java +++ b/ProjectElectricLocomotive/ObjectParameters.java @@ -1,3 +1,4 @@ +package ProjectElectricLocomotive; public class ObjectParameters { private final int _x; private final int _y; diff --git a/ProjectElectricLocomotive/Status.java b/ProjectElectricLocomotive/Status.java new file mode 100644 index 0000000..54bdeaf --- /dev/null +++ b/ProjectElectricLocomotive/Status.java @@ -0,0 +1,7 @@ +package ProjectElectricLocomotive; + +public enum Status { + NotInit, + InProgress, + Finish +} -- 2.25.1 From 4284b48c00f75bd548c1426198faa648480e9c10 Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 10 Oct 2023 01:30:20 +0400 Subject: [PATCH 04/10] 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); + } + } +} -- 2.25.1 From 37a3d7d8d2ffd8763df08487dd3f9f451becf9dd Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 10 Oct 2023 13:30:54 +0400 Subject: [PATCH 05/10] READY LabWork02 HARD --- .../DrawingElectricLocomotive.java | 3 --- .../DrawingLocomotive.java | 10 +--------- .../DrawingObjectLocomotive.java | 1 - .../EntityElectricLocomotive.java | 1 - ProjectElectricLocomotive/EntityLocomotive.java | 2 -- .../FormElectricLocomotive.java | 1 - ProjectElectricLocomotive/IDrawingWheels.java | 17 +++++++++++++---- ProjectElectricLocomotive/IMoveableObject.java | 1 - .../MoveToRigthCorner.java | 2 -- ProjectElectricLocomotive/WheelsCount.java | 2 +- 10 files changed, 15 insertions(+), 25 deletions(-) diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java index 3b3e67c..004ca7e 100644 --- a/ProjectElectricLocomotive/DrawingElectricLocomotive.java +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -16,9 +16,6 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF { Color colorBlack = Color.BLACK; - Color windows = Color.BLUE; - Color bodyColor = EntityLocomotive.BodyColor; - Color additionalBrush = electricLocomotive.AdditionalColor; if (electricLocomotive.Horns) { //horns diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java index 8949adc..3e7b17b 100644 --- a/ProjectElectricLocomotive/DrawingLocomotive.java +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -56,7 +56,6 @@ public class DrawingLocomotive { _pictureHeight = height; _locoWidth = locoWidth; _locoHeight = locoHeight; - // EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); } public void SetWheelsCount(int wheelsCount){ @@ -105,6 +104,7 @@ public class DrawingLocomotive { break; } } + public void DrawTransport(Graphics g) { { @@ -112,7 +112,6 @@ public class DrawingLocomotive { } Color colorBlack = Color.BLACK; - /*Brush blackBrush = new SolidBrush(Color.Black);*/ Color windows = Color.BLUE; Color bodyColor = EntityLocomotive.BodyColor; @@ -133,7 +132,6 @@ public class DrawingLocomotive { g.setColor(colorBlack); g.drawPolygon(locoP); - //окошки Polygon window = new Polygon(); window.addPoint(_startPosX + 10, _startPosY + 30); @@ -155,11 +153,6 @@ public class DrawingLocomotive { 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(); @@ -175,7 +168,6 @@ public class DrawingLocomotive { g.fillPolygon(telega); g.setColor(colorBlack); g.drawPolygon(telega); - //telejka //телега окна g.setColor(colorBlack); diff --git a/ProjectElectricLocomotive/DrawingObjectLocomotive.java b/ProjectElectricLocomotive/DrawingObjectLocomotive.java index 9e34da2..8e48184 100644 --- a/ProjectElectricLocomotive/DrawingObjectLocomotive.java +++ b/ProjectElectricLocomotive/DrawingObjectLocomotive.java @@ -2,7 +2,6 @@ package ProjectElectricLocomotive; public class DrawingObjectLocomotive implements IMoveableObject { private DrawingLocomotive _drawningLocomotive = null; - public DrawingObjectLocomotive(DrawingLocomotive drawningLocomotive) { _drawningLocomotive = drawningLocomotive; diff --git a/ProjectElectricLocomotive/EntityElectricLocomotive.java b/ProjectElectricLocomotive/EntityElectricLocomotive.java index 2be3ca3..846943b 100644 --- a/ProjectElectricLocomotive/EntityElectricLocomotive.java +++ b/ProjectElectricLocomotive/EntityElectricLocomotive.java @@ -5,7 +5,6 @@ import java.awt.*; public class EntityElectricLocomotive extends EntityLocomotive { public Color AdditionalColor; public boolean Horns; - public boolean SeifBatteries; public EntityElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries) { diff --git a/ProjectElectricLocomotive/EntityLocomotive.java b/ProjectElectricLocomotive/EntityLocomotive.java index aaf6deb..be4cdb4 100644 --- a/ProjectElectricLocomotive/EntityLocomotive.java +++ b/ProjectElectricLocomotive/EntityLocomotive.java @@ -6,13 +6,11 @@ public class EntityLocomotive { public int Speed; public double Weight; public Color BodyColor; - public EntityLocomotive(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; } - public double Step() { return (double) Speed * 100 / Weight; diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index 2437878..dfe35f0 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -58,7 +58,6 @@ public class FormElectricLocomotive { _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 index 9faad6f..1d1abfd 100644 --- a/ProjectElectricLocomotive/IDrawingWheels.java +++ b/ProjectElectricLocomotive/IDrawingWheels.java @@ -7,8 +7,6 @@ public interface IDrawingWheels { 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){ @@ -18,10 +16,21 @@ public interface IDrawingWheels { int wheelWidth = 9; int wheelHeight = 9; - if (wheelsCount.count >= wheelsCount.Three.count) { + if(wheelsCount.count <= wheelsCount.Two.count){ + DrawWheel(g2d, color, startPosX + 95, startPosY + 45, 9, 9); + DrawWheel(g2d, color, startPosX + 140, startPosY + 45, 9, 9); + } + + if (wheelsCount.count == wheelsCount.Three.count) { + DrawWheel(g2d, color, startPosX + 95, startPosY + 45, 9, 9); + DrawWheel(g2d, color, startPosX + 140, startPosY + 45, 9, 9); + DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight); } - if (wheelsCount.count >= wheelsCount.Four.count) { + if (wheelsCount.count == wheelsCount.Four.count) { + DrawWheel(g2d, color, startPosX + 95, startPosY + 45, 9, 9); + DrawWheel(g2d, color, startPosX + 140, startPosY + 45, 9, 9); + DrawWheel(g2d, color, startPosX + 105, startPosY + 45, wheelWidth, wheelHeight); DrawWheel(g2d, color, startPosX + 130, startPosY + 45, wheelWidth, wheelHeight); } diff --git a/ProjectElectricLocomotive/IMoveableObject.java b/ProjectElectricLocomotive/IMoveableObject.java index c102340..d10bced 100644 --- a/ProjectElectricLocomotive/IMoveableObject.java +++ b/ProjectElectricLocomotive/IMoveableObject.java @@ -4,7 +4,6 @@ import java.awt.*; public interface IMoveableObject { ObjectParameters GetObjectPosition(); int GetStep(); - boolean CheckCanMove(DyrectionType direction); void MoveObject(DyrectionType direction); } diff --git a/ProjectElectricLocomotive/MoveToRigthCorner.java b/ProjectElectricLocomotive/MoveToRigthCorner.java index 67b4ce5..0551cdf 100644 --- a/ProjectElectricLocomotive/MoveToRigthCorner.java +++ b/ProjectElectricLocomotive/MoveToRigthCorner.java @@ -9,7 +9,6 @@ public class MoveToRigthCorner extends AbstractStrategy { return objParams.RightBorder() >= FieldWidth - GetStep() && objParams.DownBorder() >= FieldHeight - GetStep(); } - @Override protected void MoveToTarget() { @@ -18,6 +17,5 @@ public class MoveToRigthCorner extends AbstractStrategy { if (objParams.RightBorder() < FieldWidth - GetStep()) MoveRight(); if (objParams.DownBorder() < FieldHeight - GetStep()) MoveDown(); - } } diff --git a/ProjectElectricLocomotive/WheelsCount.java b/ProjectElectricLocomotive/WheelsCount.java index 0fa62ef..08e4f6c 100644 --- a/ProjectElectricLocomotive/WheelsCount.java +++ b/ProjectElectricLocomotive/WheelsCount.java @@ -1,7 +1,7 @@ package ProjectElectricLocomotive; public enum WheelsCount { - /* Two(2),*/ Three(3), Four(4); + Two(2), Three(3), Four(4); public final int count; WheelsCount(int count) { this.count = count; -- 2.25.1 From 2895044e8b1c7e42164cc0d43b812114b14893b8 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sat, 21 Oct 2023 19:25:05 +0400 Subject: [PATCH 06/10] bad code, with comment and without modifications. --- .../DrawingLocomotive.java | 4 + .../FormElectricLocomotive.form | 24 ++++-- .../FormElectricLocomotive.java | 24 ++++-- .../FormLocomotiveCollections.form | 83 ++++++++++++++++++ .../FormLocomotiveCollections.java | 59 +++++++++++++ .../LocomotiveGenericCollection.java | 86 +++++++++++++++++++ .../MainFrameElectricLocomotive.java | 7 +- ProjectElectricLocomotive/SetGeneric.java | 63 ++++++++++++++ 8 files changed, 333 insertions(+), 17 deletions(-) create mode 100644 ProjectElectricLocomotive/FormLocomotiveCollections.form create mode 100644 ProjectElectricLocomotive/FormLocomotiveCollections.java create mode 100644 ProjectElectricLocomotive/LocomotiveGenericCollection.java create mode 100644 ProjectElectricLocomotive/SetGeneric.java diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java index 3e7b17b..c794f80 100644 --- a/ProjectElectricLocomotive/DrawingLocomotive.java +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -202,4 +202,8 @@ public class DrawingLocomotive { } return false; } + + public IMoveableObject GetMoveableObject() { + return new DrawingObjectLocomotive(this); + } } diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.form b/ProjectElectricLocomotive/FormElectricLocomotive.form index 9b1c126..2734e4b 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.form +++ b/ProjectElectricLocomotive/FormElectricLocomotive.form @@ -1,6 +1,6 @@ - + @@ -15,12 +15,12 @@ - + - + @@ -28,7 +28,7 @@ - + @@ -56,7 +56,7 @@ - + @@ -71,7 +71,7 @@ - + @@ -86,7 +86,7 @@ - + @@ -105,7 +105,7 @@ - + @@ -121,6 +121,14 @@ + + + + + + + + diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index dfe35f0..7250958 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -6,7 +6,7 @@ import java.awt.event.ActionListener; import java.util.Random; public class FormElectricLocomotive { - DrawingLocomotive _drawingLocomotive; + public DrawingLocomotive _drawingLocomotive; AbstractStrategy _abstractStrategy; private JButton buttonCreateElectricLocomotive; private JPanel pictureBox; @@ -17,6 +17,9 @@ public class FormElectricLocomotive { public JComboBox comboBoxStrategy; private JButton buttonStep; private JButton buttonCreateLocomotive; + private JButton ButtonSelectLocomotive; + public DrawingLocomotive SelectedLocomotive; + public boolean IsSelect = false; public JPanel getPictureBox() { return pictureBox; @@ -30,11 +33,9 @@ public class FormElectricLocomotive { buttonCreateLocomotive.addActionListener(e -> { Random rnd = new Random(); + Color color = JColorChooser.showDialog(null, "Цвет", null); _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)), + rnd.nextInt(1000, 3000), color, pictureBox.getWidth(), pictureBox.getHeight()); @@ -45,11 +46,14 @@ public class FormElectricLocomotive { buttonCreateElectricLocomotive.addActionListener(e -> { Random random = new Random(); + Color color = JColorChooser.showDialog(null, "Цвет", null); + Color addColor = JColorChooser.showDialog(null, "Цвет2", null); + _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)), + color, + addColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), @@ -61,6 +65,12 @@ public class FormElectricLocomotive { Draw(); }); + ButtonSelectLocomotive.addActionListener(e->{ + SelectedLocomotive = _drawingLocomotive; + IsSelect = true; +// DialogResult = DialogResult.OK; + }); + buttonStep.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form new file mode 100644 index 0000000..abfa939 --- /dev/null +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -0,0 +1,83 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java new file mode 100644 index 0000000..4b55913 --- /dev/null +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -0,0 +1,59 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; +import java.awt.*; + +public class FormLocomotiveCollections extends JFrame { + private JPanel MainPanel; + private JPanel pictureBoxCollections; + private JPanel Instruments; + private JButton ButtonAddLocomotive; + private JTextField textField1; + private JButton ButtonRefreshCollection; + private JButton ButtonRemoveLocomotive; + public DrawingLocomotive loco; + FormElectricLocomotive _formElectricLocomotive; + private final LocomotiveGenericCollection _locomotives; + public JPanel getPictureBoxCollections() { + return MainPanel; + } + public FormLocomotiveCollections() + { + _locomotives = new LocomotiveGenericCollection<>(pictureBoxCollections.getWidth(), + pictureBoxCollections.getHeight()); + + ButtonAddLocomotive.addActionListener(e->{ + FormElectricLocomotive form = new FormElectricLocomotive(); + VisFormElectricLocomotive(); + + if (_locomotives == null) return; + + loco = form._drawingLocomotive; + if(loco != null) { + //проверяем, удалось ли нам загрузить объект + if (_locomotives.AddOverload(loco) != -1 && form.IsSelect == true) { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); + Refresh(); + //pictureBoxCollections = _locomotives.ShowLocomotives(); + } else { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект"); + } + } + }); + } + public void VisFormElectricLocomotive(){ + setTitle("ElectroLoco"); + _formElectricLocomotive = new FormElectricLocomotive(); + setContentPane(_formElectricLocomotive.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(200, 500); + pack(); + setVisible(true); + } + + public void Refresh(){ + Graphics g = pictureBoxCollections.getGraphics(); + pictureBoxCollections.paint(g); + _locomotives.ShowLocomotives(g); + } +} diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java new file mode 100644 index 0000000..535464c --- /dev/null +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -0,0 +1,86 @@ +package ProjectElectricLocomotive; + +import java.awt.*; + +public class LocomotiveGenericCollection +{ + //ширина/высота окна + private final int _pictureWidth; + private final int _pictureHeight; + //ширина/высота занимаемого места + private final int _placeSizeWidth = 85; + private final int _placeSizeHeight = 50; + + /// Набор объектов + private final SetGeneric _collection; + + public LocomotiveGenericCollection(int picWidth, int picHeight) + { + // немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло... + // высчитываем размер массива для setgeneric + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width*height); + } + + /// Перегрузка оператора сложения + //да емае, почему в яве все по-другому?... + + public int AddOverload(T loco){ + if(loco == null){ + return -1; + } + return _collection.Insert(loco); + } + + public T SubOverload(int pos){ + return _collection.Remove(pos); + } + + // получение объекта imoveableObj + public U GetU(int pos) +{ + return (U)_collection.Get(pos).GetMoveableObject(); +} + + /// Вывод всего набора объектов + public void ShowLocomotives(Graphics gr) + { + DrawBackground(gr); + DrawObjects(gr); + } + private void DrawBackground(Graphics g) + { + Color blackColor = Color.BLACK; + g.setColor(blackColor); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + //линия рамзетки места + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + private void DrawObjects(Graphics g) + { + int HeightObjCount = _pictureHeight / _placeSizeHeight; + int WidthObjCount = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _collection.Count(); i++) + { + T type = _collection.Get(i); + if (type != null) + { + type.SetPosition( + (int)(i / HeightObjCount * _placeSizeWidth), + (HeightObjCount - 1) * _placeSizeHeight - (int)(i % HeightObjCount * _placeSizeHeight) + ); + type.DrawTransport(g); + } + } + } +} diff --git a/ProjectElectricLocomotive/MainFrameElectricLocomotive.java b/ProjectElectricLocomotive/MainFrameElectricLocomotive.java index 9dcfab4..4d4d1b4 100644 --- a/ProjectElectricLocomotive/MainFrameElectricLocomotive.java +++ b/ProjectElectricLocomotive/MainFrameElectricLocomotive.java @@ -4,13 +4,16 @@ import javax.swing.*; public class MainFrameElectricLocomotive extends JFrame { private FormElectricLocomotive _formElectricLocomotive; + private FormLocomotiveCollections _formLocomotiveCollections; public MainFrameElectricLocomotive() { super(); setTitle("ElectroLoco"); setDefaultCloseOperation(EXIT_ON_CLOSE); - _formElectricLocomotive = new FormElectricLocomotive(); - setContentPane(_formElectricLocomotive.getPictureBox()); + //_formElectricLocomotive = new FormElectricLocomotive(); + _formLocomotiveCollections = new FormLocomotiveCollections(); + //setContentPane(_formElectricLocomotive.getPictureBox()); + setContentPane(_formLocomotiveCollections.getPictureBoxCollections()); setDefaultLookAndFeelDecorated(false); setLocation(500, 200); pack(); diff --git a/ProjectElectricLocomotive/SetGeneric.java b/ProjectElectricLocomotive/SetGeneric.java new file mode 100644 index 0000000..3d0e778 --- /dev/null +++ b/ProjectElectricLocomotive/SetGeneric.java @@ -0,0 +1,63 @@ +package ProjectElectricLocomotive; + +public class SetGeneric{ + private T[] _places; + public int Count(){ + return _places.length; + } + public SetGeneric(int count) { + _places = (T[]) new DrawingLocomotive[count]; + } + + public int Insert(T loco) + { + return Insert(loco, 0); + } + + public int Insert(T loco, int position) + { + int NoEmpty = 0, temp = 0; + for (int i = position; i < Count(); i++) + { + if (_places[i] != null) NoEmpty++; + } + if (NoEmpty == Count() - position - 1) return -1; + + if (position < Count() && position >= 0) + { + for (int j = position; j < Count(); j++) + { + if (_places[j] == null) + { + temp = j; + break; + } + } + // shift right + for (int i = temp; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = loco; + return position; + } + return -1; + } + + public T Remove(int position) + { + if (position >= Count() || position < 0) + return null; + + T tmp = _places[position]; + _places[position] = null; + return tmp; + } + + public T Get(int position) + { + // TODO проверка позиции + if (position < 0 || position >= Count()) return null; + return _places[position]; + } +} -- 2.25.1 From f69fe262f844cf7f2925e2fa14ebce724c3dd653 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sat, 21 Oct 2023 22:32:22 +0400 Subject: [PATCH 07/10] Code almost ready --- .../FormElectricLocomotive.java | 2 +- .../FormLocomotiveCollections.form | 2 +- .../FormLocomotiveCollections.java | 77 +++++++++++-------- .../FrameElectricLocomotive.java | 18 +++++ .../FrameLocomotiveCollection.java | 18 +++++ .../LocomotiveGenericCollection.java | 2 +- ProjectElectricLocomotive/Main.java | 2 +- .../MainFrameElectricLocomotive.java | 22 ------ 8 files changed, 87 insertions(+), 56 deletions(-) create mode 100644 ProjectElectricLocomotive/FrameElectricLocomotive.java create mode 100644 ProjectElectricLocomotive/FrameLocomotiveCollection.java delete mode 100644 ProjectElectricLocomotive/MainFrameElectricLocomotive.java diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index 7250958..fa27453 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -17,7 +17,7 @@ public class FormElectricLocomotive { public JComboBox comboBoxStrategy; private JButton buttonStep; private JButton buttonCreateLocomotive; - private JButton ButtonSelectLocomotive; + public JButton ButtonSelectLocomotive; public DrawingLocomotive SelectedLocomotive; public boolean IsSelect = false; diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index abfa939..28e0da3 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -58,7 +58,7 @@
- + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 4b55913..9b3028c 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -3,55 +3,72 @@ package ProjectElectricLocomotive; import javax.swing.*; import java.awt.*; -public class FormLocomotiveCollections extends JFrame { +public class FormLocomotiveCollections { private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; private JButton ButtonAddLocomotive; - private JTextField textField1; + private JTextField textFieldNumber; private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; public DrawingLocomotive loco; FormElectricLocomotive _formElectricLocomotive; private final LocomotiveGenericCollection _locomotives; + public JPanel getPictureBoxCollections() { return MainPanel; } - public FormLocomotiveCollections() - { - _locomotives = new LocomotiveGenericCollection<>(pictureBoxCollections.getWidth(), - pictureBoxCollections.getHeight()); - ButtonAddLocomotive.addActionListener(e->{ - FormElectricLocomotive form = new FormElectricLocomotive(); - VisFormElectricLocomotive(); + public FormLocomotiveCollections() { + _locomotives = new LocomotiveGenericCollection<>(400, 300); - if (_locomotives == null) return; - loco = form._drawingLocomotive; - if(loco != null) { - //проверяем, удалось ли нам загрузить объект - if (_locomotives.AddOverload(loco) != -1 && form.IsSelect == true) { - JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); - Refresh(); - //pictureBoxCollections = _locomotives.ShowLocomotives(); - } else { - JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект"); + ButtonAddLocomotive.addActionListener(e -> { + FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive(); + frameElectricLocomotive.setVisible(true); + frameElectricLocomotive._formLocomotiveCollection.ButtonSelectLocomotive.addActionListener(e2 -> { + loco = frameElectricLocomotive._formLocomotiveCollection._drawingLocomotive; + frameElectricLocomotive.dispose(); + if (loco != null) { + //проверяем, удалось ли нам загрузить объект + if (_locomotives.AddOverload(loco) != -1) { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); + Refresh(); + } else { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект"); + } } + }); + }); + + ButtonRemoveLocomotive.addActionListener(e->{ + try { + int pos = Integer.parseInt(textFieldNumber.getText()); + if (_locomotives.SubOverload(pos) != null) { + Refresh(); + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Объект удален", + "Успех", + JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Не удалось удалить объект", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + } + } catch (Exception ex) { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Неверное значение", + "Ошибка", + JOptionPane.ERROR_MESSAGE); } }); - } - public void VisFormElectricLocomotive(){ - setTitle("ElectroLoco"); - _formElectricLocomotive = new FormElectricLocomotive(); - setContentPane(_formElectricLocomotive.getPictureBox()); - setDefaultLookAndFeelDecorated(false); - setLocation(200, 500); - pack(); - setVisible(true); - } - public void Refresh(){ + ButtonRefreshCollection.addActionListener(e->{ + Refresh(); + }); + } + public void Refresh() { Graphics g = pictureBoxCollections.getGraphics(); pictureBoxCollections.paint(g); _locomotives.ShowLocomotives(g); diff --git a/ProjectElectricLocomotive/FrameElectricLocomotive.java b/ProjectElectricLocomotive/FrameElectricLocomotive.java new file mode 100644 index 0000000..abf9d6b --- /dev/null +++ b/ProjectElectricLocomotive/FrameElectricLocomotive.java @@ -0,0 +1,18 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; + +public class FrameElectricLocomotive extends JFrame { + public FormElectricLocomotive _formLocomotiveCollection; + public FrameElectricLocomotive() { + super(); + setTitle("ElectroLoco"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + _formLocomotiveCollection = new FormElectricLocomotive(); + setContentPane(_formLocomotiveCollection.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(500, 200); + pack(); + setVisible(true); + } +} diff --git a/ProjectElectricLocomotive/FrameLocomotiveCollection.java b/ProjectElectricLocomotive/FrameLocomotiveCollection.java new file mode 100644 index 0000000..7883880 --- /dev/null +++ b/ProjectElectricLocomotive/FrameLocomotiveCollection.java @@ -0,0 +1,18 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; + +public class FrameLocomotiveCollection extends JFrame { + public FormLocomotiveCollections _formLocomotiveCollections; + public FrameLocomotiveCollection(){ + super(); + setTitle("LocoCollection"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + _formLocomotiveCollections = new FormLocomotiveCollections(); + setContentPane(_formLocomotiveCollections.getPictureBoxCollections()); + setDefaultLookAndFeelDecorated(false); + setLocation(400, 50); + pack(); + setVisible(true); + } +} diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index 535464c..9abe2b2 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -8,7 +8,7 @@ public class LocomotiveGenericCollection Date: Mon, 23 Oct 2023 12:05:54 +0400 Subject: [PATCH 08/10] Lab 4 base on C# in lab 4 hard on java --- .../ProjectElectricLocomotive/img/arrowDown.jpg | Bin 0 -> 702 bytes .../ProjectElectricLocomotive/img/arrowLeft.jpg | Bin 0 -> 706 bytes .../ProjectElectricLocomotive/img/arrowRight.jpg | Bin 0 -> 702 bytes .../ProjectElectricLocomotive/img/arrowUp.jpg | Bin 0 -> 706 bytes 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowDown.jpg create mode 100644 out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowLeft.jpg create mode 100644 out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowRight.jpg create mode 100644 out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowUp.jpg diff --git a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowDown.jpg b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowDown.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2d41e934bd5fb7cb8c8714d8dc7db3f6fbb203db GIT binary patch literal 702 zcmex=kSFKvnvHC#j1O51viho)>VwJvM^~|F+7hZR9eClQsBo=7c zWAL{6s$lA@$_Gtm>R(kRZR~X|o+u(wA@S_*sjY0B3%{q8-tfqHdSS|qsYllpoVMB? z+4iQ6;pvnw3Tp1!UG1-p>NBKex|QpqdWv>mbuxau`-D92j;G9Jd9}6MZSu(>GMy6LE_*g9V zUH_eYL@u;+PHDE+Te+h@uAErIVfNcI`&IwNlP0|{jeL#YY*@A9R=-upr{_tcUe~vO zUN+N5{6VN1hjZbp`rz$s2XB9kbE;{+dck^I9>avK=XP@3SgWz?zDr8rhFP~R-8g3Q i`_gS;d$-q*fB$E25x-z@)st)O0Sk792QvKsZvp@o0^z^_ literal 0 HcmV?d00001 diff --git a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowLeft.jpg b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowLeft.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5f03da1e254208cb67698990b3e6146f3b2c352b GIT binary patch literal 706 zcmex=X_-jEpc@CT5UB85uZ)ggFI8M8$xLL=dtt zRUlD8xE{FZEe0NDMxe#af(-Ty?;ktt+OoE5(}ZTbTj8NI8J52`TQ~pmM6bADQ`7Bb zbC|f^M=VLuSeG^@(D8{*kz@m(?URUO&MJ@2t<~0XVNHoJi25zWeq>@9|D7_w=5O~x zKK-0M@jH+0tFsU6HgP{m_|e@_{Mw3*udz5MDj>^g&8EEBzTbalosv87SaageKg%`$ zeB0vL6SU;tBnD&O2Z3t*jR^urk6&=oc`k9n;iv1i^E#_feXf0SaGfvjt=NjWGKcd+ zx)`QCTR0(`=f`YS{e@l&GVY}E)i3GU;=*&jw`Xa^w=xmlphJhN%I0smU6!A>@cw^> z2`U%A-ri=)HAVCfK{(3EuKTo_!! z)+Fhtc+}Eul^&ap@l4QOGIi&SGSQuGGlJDEo=5EZJoSn8 z4KBteC*_`y2@BPvrYvO+Ywe7Svs_zYclA`_*6o~+FL|x!4bACPO3hz?#cH12sh<7) mva3RmeQxOIh;wh`WN>WhZok^{YLn<=hYViE1t#+UZvp^{OyCXx literal 0 HcmV?d00001 diff --git a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowRight.jpg b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowRight.jpg new file mode 100644 index 0000000000000000000000000000000000000000..41134aa26e21aa8247198a4d35962be34118bae3 GIT binary patch literal 702 zcmex=WICV(Up}Vflj8 zU1}Uok*Ag2BGL<)w`tn_ONm_Uwrch5H+puA2P`~9{IV+J4(kre4==iRrYqFDn?bH zZBtDLkGE zQ1R6K=J#&u-uY$Bi??sM)g9|`XS(B$!;W2|6H}(LDPL18>S>$9GSlz0`kym_PVvbB z&e}^RxlL*jG00di{KxN(G<*KlGT|GxvCMlm7qGOhD?Q8qrL)qEh$r6{} gd*}H4b9;UL_rD|J4=k>FvaLN}!OrkNhX4Of0J5yy$p8QV literal 0 HcmV?d00001 diff --git a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowUp.jpg b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowUp.jpg new file mode 100644 index 0000000000000000000000000000000000000000..74ea15e1491c7584847014bff2d4997206cf343d GIT binary patch literal 706 zcmex=P*_A%OcAI`1R)JG z10*VlY9vtV|1Aa{W=5dJ%z_N|4DTO1?Ao%nYtw{gyIbL*GZ~h@He1hsd7@Wbu&L?x zN~-&SE<*2Ec?4RS9j(a z4Ncy?KGHs=(n_tZE4{>TdOPW7zx5JRHu=nz!>B&xSpUuQi5iEFx;8Isn&R;*`@~AA znUkEp?)W!xo7J_jD7UIxdx}^~CT%wsT9)2xsdsR>%xm$uqD@>`<;Q%41ZA95m4yj}wwi>q=v>`K#q{7Dr(VRJ^p z`M&J$7wYG(nk~%s(LGZA{r;vf*58{-VM8QluF1G-N1ieR7Z1-L+`_VsfYlZUN#g`Wyn>L}(Ic!UVWw^r$nW-N|nvDEE kGxcbOEK#%AnykoO&|_K^lQ2Kmj_Gg)FXI9ew&wpg0f?>D%m4rY literal 0 HcmV?d00001 -- 2.25.1 From 34f72ac391a45d78c66b70f63bcccd49e77d2408 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sun, 29 Oct 2023 17:10:37 +0400 Subject: [PATCH 09/10] Lab 3 Ready hard --- .../DopClassParameters.java | 65 ++++++++++++++ .../DrawingElectricLocomotive.java | 13 ++- .../DrawingLocomotive.java | 21 ++++- .../FormDopClassParameters.form | 38 ++++++++ .../FormDopClassParameters.java | 86 +++++++++++++++++++ .../FormElectricLocomotive.java | 1 - .../FormLocomotiveCollections.form | 12 ++- .../FormLocomotiveCollections.java | 17 +++- .../FrameDopClassParameters.java | 19 ++++ .../FrameElectricLocomotive.java | 4 +- .../FrameLocomotiveCollection.java | 3 +- .../LocomotiveGenericCollection.java | 3 - 12 files changed, 262 insertions(+), 20 deletions(-) create mode 100644 ProjectElectricLocomotive/DopClassParameters.java create mode 100644 ProjectElectricLocomotive/FormDopClassParameters.form create mode 100644 ProjectElectricLocomotive/FormDopClassParameters.java create mode 100644 ProjectElectricLocomotive/FrameDopClassParameters.java diff --git a/ProjectElectricLocomotive/DopClassParameters.java b/ProjectElectricLocomotive/DopClassParameters.java new file mode 100644 index 0000000..23fb7e8 --- /dev/null +++ b/ProjectElectricLocomotive/DopClassParameters.java @@ -0,0 +1,65 @@ +package ProjectElectricLocomotive; + +import java.util.ArrayList; +import java.util.Random; + +public class DopClassParameters { + public T[] _entityLocomotive; + public U[] _idrawningWheels; + + int _locomotivesCount; + int _idrawningWheelsCount; + + private int _pictureWidth; + private int _pictureHeight; + + public DopClassParameters(int count, int width, int height) { + _entityLocomotive = (T[]) new EntityLocomotive[count]; + _idrawningWheels = (U[]) new IDrawingWheels[count]; + _pictureWidth = width; + _pictureHeight = height; + _locomotivesCount = 0; + _idrawningWheelsCount = 0; + } + + public void Add(T entityLocoObj) { + if (_locomotivesCount < _entityLocomotive.length) { + _entityLocomotive[_locomotivesCount] = entityLocoObj; + _locomotivesCount++; + } + } + + public void Add(U idrawingWheels) { + if (_idrawningWheelsCount < _idrawningWheels.length) { + _idrawningWheels[_idrawningWheelsCount] = idrawingWheels; + _idrawningWheelsCount++; + } + } + + public DrawingLocomotive RandomLocomotive(int pictureWidth, int pictureHeight) { + Random rnd = new Random(); + if (_entityLocomotive == null || _idrawningWheels == null) { + return null; + } + T entityLocomotive = _entityLocomotive[rnd.nextInt(_locomotivesCount)]; + U idrawingWheels = _idrawningWheels[rnd.nextInt(_idrawningWheelsCount)]; + + DrawingLocomotive drawLocomotive; + if (entityLocomotive instanceof EntityElectricLocomotive) { + drawLocomotive = new DrawingElectricLocomotive( + (EntityElectricLocomotive) entityLocomotive, + idrawingWheels, + pictureWidth, + pictureHeight + ); + } else { + drawLocomotive = new DrawingLocomotive( + entityLocomotive, + idrawingWheels, + pictureWidth, + pictureHeight + ); + } + return drawLocomotive; + } +} diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java index 004ca7e..19db7f5 100644 --- a/ProjectElectricLocomotive/DrawingElectricLocomotive.java +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -1,6 +1,7 @@ package ProjectElectricLocomotive; import java.awt.*; public class DrawingElectricLocomotive extends DrawingLocomotive { + public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries, int width, int height) { @@ -10,16 +11,22 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); } } + + public DrawingElectricLocomotive(EntityElectricLocomotive entityElectricLocomotive, IDrawingWheels iDrawingWheels, + int width, int height) + { + super(entityElectricLocomotive,iDrawingWheels, width, height, 150, 50); + } @Override public void DrawTransport(Graphics g) { if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF { - Color colorBlack = Color.BLACK; + Color addColor = electricLocomotive.AdditionalColor; if (electricLocomotive.Horns) { //horns - g.setColor(colorBlack); + g.setColor(addColor); g.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); g.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); @@ -28,7 +35,7 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { } if (electricLocomotive.SeifBatteries) { - g.setColor(colorBlack); + g.setColor(addColor); g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10); } super.DrawTransport(g); diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java index c794f80..f301d5f 100644 --- a/ProjectElectricLocomotive/DrawingLocomotive.java +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -26,14 +26,14 @@ public class DrawingLocomotive { return _locoHeight; } - public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth) + public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int height) { - if (width < _locoWidth || heigth < _locoHeight) + if (width < _locoWidth || height < _locoHeight) { return; } _pictureWidth = width; - _pictureHeight = heigth; + _pictureHeight = height; EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); Random rnd = new Random(); @@ -58,6 +58,21 @@ public class DrawingLocomotive { _locoHeight = locoHeight; } + protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels, + int width, int height){ + EntityLocomotive = entityLocomotive; + _drawingWheels = iDrawingWheels; + _pictureWidth = width; + _pictureHeight = height; + } + + protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels, + int width, int height, int locoWidth, int locoHeight){ + this(entityLocomotive, iDrawingWheels, width, height); + _locoWidth = locoWidth; + _locoHeight = locoHeight; + } + public void SetWheelsCount(int wheelsCount){ _drawingWheels.SetWheelsCount(wheelsCount); } diff --git a/ProjectElectricLocomotive/FormDopClassParameters.form b/ProjectElectricLocomotive/FormDopClassParameters.form new file mode 100644 index 0000000..e0de2f6 --- /dev/null +++ b/ProjectElectricLocomotive/FormDopClassParameters.form @@ -0,0 +1,38 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectElectricLocomotive/FormDopClassParameters.java b/ProjectElectricLocomotive/FormDopClassParameters.java new file mode 100644 index 0000000..1c842d5 --- /dev/null +++ b/ProjectElectricLocomotive/FormDopClassParameters.java @@ -0,0 +1,86 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormDopClassParameters { + private JButton ButtonGenerationRandomLocomotive; + private JPanel pictureBoxGenerated; + Random rnd; + DrawingLocomotive drawingLocomotive; + DopClassParameters _dopClassParameters; + + public JPanel getPictureBoxGenerated() { + return pictureBoxGenerated; + } + + public FormDopClassParameters() { + pictureBoxGenerated.setPreferredSize(new Dimension(400, 300)); + _dopClassParameters = new DopClassParameters<>( + 10, + pictureBoxGenerated.getWidth(), + pictureBoxGenerated.getHeight() + ); + + ButtonGenerationRandomLocomotive.addActionListener(e -> { + AddEntityLocomotive(); + AddRandomWheels(); + + drawingLocomotive = _dopClassParameters.RandomLocomotive( + pictureBoxGenerated.getWidth(), + pictureBoxGenerated.getHeight() + ); + drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + Draw(); + }); + } + + public void AddEntityLocomotive() { + rnd = new Random(); + EntityLocomotive entityLocomotive; + Color color = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); + if (rnd.nextBoolean()) { + entityLocomotive = new EntityLocomotive( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + color + ); + } else { + entityLocomotive = new EntityElectricLocomotive( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + color, + color, + rnd.nextBoolean(), + rnd.nextBoolean() + ); + } + _dopClassParameters.Add(entityLocomotive); + } + + public void AddRandomWheels() { + rnd = new Random(); + IDrawingWheels iDrawingWheels; + int wheelsChoice = rnd.nextInt(0, 3); + if (wheelsChoice == 0) { + iDrawingWheels = new DrawingWheel(); + } else if (wheelsChoice == 1) { + iDrawingWheels = new DrawingEmptyWheels(); + } else { + iDrawingWheels = new DrawingWheelsBlueCrom(); + } + iDrawingWheels.SetWheelsCount(rnd.nextInt(2, 5)); + _dopClassParameters.Add(iDrawingWheels); + } + + public void Draw() { + if (drawingLocomotive.EntityLocomotive == null) { + return; + } + Graphics g = pictureBoxGenerated.getGraphics(); + pictureBoxGenerated.paint(g); + drawingLocomotive.DrawTransport(g); + } + +} diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index fa27453..1b9ae8b 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -68,7 +68,6 @@ public class FormElectricLocomotive { ButtonSelectLocomotive.addActionListener(e->{ SelectedLocomotive = _drawingLocomotive; IsSelect = true; -// DialogResult = DialogResult.OK; }); buttonStep.addActionListener(new ActionListener() { diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index 28e0da3..b1bcaa2 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -21,7 +21,7 @@
- + @@ -39,7 +39,7 @@ - + @@ -76,6 +76,14 @@ + + + + + + + + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 9b3028c..3a48f7f 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -4,6 +4,7 @@ import javax.swing.*; import java.awt.*; public class FormLocomotiveCollections { + FrameDopClassParameters frameDopClassParameters; private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; @@ -11,9 +12,9 @@ public class FormLocomotiveCollections { private JTextField textFieldNumber; private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; + private JButton ButtonCreateRandomLoco; public DrawingLocomotive loco; - FormElectricLocomotive _formElectricLocomotive; - private final LocomotiveGenericCollection _locomotives; + LocomotiveGenericCollection _locomotives; public JPanel getPictureBoxCollections() { return MainPanel; @@ -41,7 +42,13 @@ public class FormLocomotiveCollections { }); }); - ButtonRemoveLocomotive.addActionListener(e->{ + ButtonCreateRandomLoco.addActionListener(e->{ + if(frameDopClassParameters!=null) frameDopClassParameters.dispose(); + frameDopClassParameters = new FrameDopClassParameters(); + frameDopClassParameters.setVisible(true); + }); + + ButtonRemoveLocomotive.addActionListener(e -> { try { int pos = Integer.parseInt(textFieldNumber.getText()); if (_locomotives.SubOverload(pos) != null) { @@ -64,10 +71,12 @@ public class FormLocomotiveCollections { } }); - ButtonRefreshCollection.addActionListener(e->{ + ButtonRefreshCollection.addActionListener(e -> { Refresh(); }); + } + public void Refresh() { Graphics g = pictureBoxCollections.getGraphics(); pictureBoxCollections.paint(g); diff --git a/ProjectElectricLocomotive/FrameDopClassParameters.java b/ProjectElectricLocomotive/FrameDopClassParameters.java new file mode 100644 index 0000000..e5a06ac --- /dev/null +++ b/ProjectElectricLocomotive/FrameDopClassParameters.java @@ -0,0 +1,19 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; + +public class FrameDopClassParameters extends JFrame { + public FormDopClassParameters _formDopClassParameters; + + public FrameDopClassParameters(){ + super(); + setTitle("Рандомный локомотив"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + _formDopClassParameters = new FormDopClassParameters(); + setContentPane(_formDopClassParameters.getPictureBoxGenerated()); + setDefaultLookAndFeelDecorated(false); + setLocation(500, 200); + pack(); + setVisible(true); + } +} diff --git a/ProjectElectricLocomotive/FrameElectricLocomotive.java b/ProjectElectricLocomotive/FrameElectricLocomotive.java index abf9d6b..9344738 100644 --- a/ProjectElectricLocomotive/FrameElectricLocomotive.java +++ b/ProjectElectricLocomotive/FrameElectricLocomotive.java @@ -6,8 +6,8 @@ public class FrameElectricLocomotive extends JFrame { public FormElectricLocomotive _formLocomotiveCollection; public FrameElectricLocomotive() { super(); - setTitle("ElectroLoco"); - setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle("Электролокомотив"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); _formLocomotiveCollection = new FormElectricLocomotive(); setContentPane(_formLocomotiveCollection.getPictureBox()); setDefaultLookAndFeelDecorated(false); diff --git a/ProjectElectricLocomotive/FrameLocomotiveCollection.java b/ProjectElectricLocomotive/FrameLocomotiveCollection.java index 7883880..189aba5 100644 --- a/ProjectElectricLocomotive/FrameLocomotiveCollection.java +++ b/ProjectElectricLocomotive/FrameLocomotiveCollection.java @@ -6,8 +6,7 @@ public class FrameLocomotiveCollection extends JFrame { public FormLocomotiveCollections _formLocomotiveCollections; public FrameLocomotiveCollection(){ super(); - setTitle("LocoCollection"); - setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle("Коллекция");setDefaultCloseOperation(EXIT_ON_CLOSE); _formLocomotiveCollections = new FormLocomotiveCollections(); setContentPane(_formLocomotiveCollections.getPictureBoxCollections()); setDefaultLookAndFeelDecorated(false); diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index 9abe2b2..cf1207c 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -10,14 +10,11 @@ public class LocomotiveGenericCollection _collection; public LocomotiveGenericCollection(int picWidth, int picHeight) { - // немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло... - // высчитываем размер массива для setgeneric int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _pictureWidth = picWidth; -- 2.25.1 From e36d02dd7523a5cdef164810e452c0675598fddc Mon Sep 17 00:00:00 2001 From: ekallin Date: Mon, 6 Nov 2023 17:02:05 +0400 Subject: [PATCH 10/10] revert 34f72ac391a45d78c66b70f63bcccd49e77d2408 revert Lab 3 Ready hard --- .../DopClassParameters.java | 65 -------------- .../DrawingElectricLocomotive.java | 13 +-- .../DrawingLocomotive.java | 21 +---- .../FormDopClassParameters.form | 38 -------- .../FormDopClassParameters.java | 86 ------------------- .../FormElectricLocomotive.java | 1 + .../FormLocomotiveCollections.form | 12 +-- .../FormLocomotiveCollections.java | 17 +--- .../FrameDopClassParameters.java | 19 ---- .../FrameElectricLocomotive.java | 4 +- .../FrameLocomotiveCollection.java | 3 +- .../LocomotiveGenericCollection.java | 3 + 12 files changed, 20 insertions(+), 262 deletions(-) delete mode 100644 ProjectElectricLocomotive/DopClassParameters.java delete mode 100644 ProjectElectricLocomotive/FormDopClassParameters.form delete mode 100644 ProjectElectricLocomotive/FormDopClassParameters.java delete mode 100644 ProjectElectricLocomotive/FrameDopClassParameters.java diff --git a/ProjectElectricLocomotive/DopClassParameters.java b/ProjectElectricLocomotive/DopClassParameters.java deleted file mode 100644 index 23fb7e8..0000000 --- a/ProjectElectricLocomotive/DopClassParameters.java +++ /dev/null @@ -1,65 +0,0 @@ -package ProjectElectricLocomotive; - -import java.util.ArrayList; -import java.util.Random; - -public class DopClassParameters { - public T[] _entityLocomotive; - public U[] _idrawningWheels; - - int _locomotivesCount; - int _idrawningWheelsCount; - - private int _pictureWidth; - private int _pictureHeight; - - public DopClassParameters(int count, int width, int height) { - _entityLocomotive = (T[]) new EntityLocomotive[count]; - _idrawningWheels = (U[]) new IDrawingWheels[count]; - _pictureWidth = width; - _pictureHeight = height; - _locomotivesCount = 0; - _idrawningWheelsCount = 0; - } - - public void Add(T entityLocoObj) { - if (_locomotivesCount < _entityLocomotive.length) { - _entityLocomotive[_locomotivesCount] = entityLocoObj; - _locomotivesCount++; - } - } - - public void Add(U idrawingWheels) { - if (_idrawningWheelsCount < _idrawningWheels.length) { - _idrawningWheels[_idrawningWheelsCount] = idrawingWheels; - _idrawningWheelsCount++; - } - } - - public DrawingLocomotive RandomLocomotive(int pictureWidth, int pictureHeight) { - Random rnd = new Random(); - if (_entityLocomotive == null || _idrawningWheels == null) { - return null; - } - T entityLocomotive = _entityLocomotive[rnd.nextInt(_locomotivesCount)]; - U idrawingWheels = _idrawningWheels[rnd.nextInt(_idrawningWheelsCount)]; - - DrawingLocomotive drawLocomotive; - if (entityLocomotive instanceof EntityElectricLocomotive) { - drawLocomotive = new DrawingElectricLocomotive( - (EntityElectricLocomotive) entityLocomotive, - idrawingWheels, - pictureWidth, - pictureHeight - ); - } else { - drawLocomotive = new DrawingLocomotive( - entityLocomotive, - idrawingWheels, - pictureWidth, - pictureHeight - ); - } - return drawLocomotive; - } -} diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java index 19db7f5..004ca7e 100644 --- a/ProjectElectricLocomotive/DrawingElectricLocomotive.java +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -1,7 +1,6 @@ package ProjectElectricLocomotive; import java.awt.*; public class DrawingElectricLocomotive extends DrawingLocomotive { - public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries, int width, int height) { @@ -11,22 +10,16 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); } } - - public DrawingElectricLocomotive(EntityElectricLocomotive entityElectricLocomotive, IDrawingWheels iDrawingWheels, - int width, int height) - { - super(entityElectricLocomotive,iDrawingWheels, width, height, 150, 50); - } @Override public void DrawTransport(Graphics g) { if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF { - Color addColor = electricLocomotive.AdditionalColor; + Color colorBlack = Color.BLACK; if (electricLocomotive.Horns) { //horns - g.setColor(addColor); + g.setColor(colorBlack); g.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); g.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); @@ -35,7 +28,7 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { } if (electricLocomotive.SeifBatteries) { - g.setColor(addColor); + g.setColor(colorBlack); g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10); } super.DrawTransport(g); diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java index f301d5f..c794f80 100644 --- a/ProjectElectricLocomotive/DrawingLocomotive.java +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -26,14 +26,14 @@ public class DrawingLocomotive { return _locoHeight; } - public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int height) + public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth) { - if (width < _locoWidth || height < _locoHeight) + if (width < _locoWidth || heigth < _locoHeight) { return; } _pictureWidth = width; - _pictureHeight = height; + _pictureHeight = heigth; EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); Random rnd = new Random(); @@ -58,21 +58,6 @@ public class DrawingLocomotive { _locoHeight = locoHeight; } - protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels, - int width, int height){ - EntityLocomotive = entityLocomotive; - _drawingWheels = iDrawingWheels; - _pictureWidth = width; - _pictureHeight = height; - } - - protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels, - int width, int height, int locoWidth, int locoHeight){ - this(entityLocomotive, iDrawingWheels, width, height); - _locoWidth = locoWidth; - _locoHeight = locoHeight; - } - public void SetWheelsCount(int wheelsCount){ _drawingWheels.SetWheelsCount(wheelsCount); } diff --git a/ProjectElectricLocomotive/FormDopClassParameters.form b/ProjectElectricLocomotive/FormDopClassParameters.form deleted file mode 100644 index e0de2f6..0000000 --- a/ProjectElectricLocomotive/FormDopClassParameters.form +++ /dev/null @@ -1,38 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/ProjectElectricLocomotive/FormDopClassParameters.java b/ProjectElectricLocomotive/FormDopClassParameters.java deleted file mode 100644 index 1c842d5..0000000 --- a/ProjectElectricLocomotive/FormDopClassParameters.java +++ /dev/null @@ -1,86 +0,0 @@ -package ProjectElectricLocomotive; - -import javax.swing.*; -import java.awt.*; -import java.util.Random; - -public class FormDopClassParameters { - private JButton ButtonGenerationRandomLocomotive; - private JPanel pictureBoxGenerated; - Random rnd; - DrawingLocomotive drawingLocomotive; - DopClassParameters _dopClassParameters; - - public JPanel getPictureBoxGenerated() { - return pictureBoxGenerated; - } - - public FormDopClassParameters() { - pictureBoxGenerated.setPreferredSize(new Dimension(400, 300)); - _dopClassParameters = new DopClassParameters<>( - 10, - pictureBoxGenerated.getWidth(), - pictureBoxGenerated.getHeight() - ); - - ButtonGenerationRandomLocomotive.addActionListener(e -> { - AddEntityLocomotive(); - AddRandomWheels(); - - drawingLocomotive = _dopClassParameters.RandomLocomotive( - pictureBoxGenerated.getWidth(), - pictureBoxGenerated.getHeight() - ); - drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); - Draw(); - }); - } - - public void AddEntityLocomotive() { - rnd = new Random(); - EntityLocomotive entityLocomotive; - Color color = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); - if (rnd.nextBoolean()) { - entityLocomotive = new EntityLocomotive( - rnd.nextInt(100, 300), - rnd.nextInt(1000, 3000), - color - ); - } else { - entityLocomotive = new EntityElectricLocomotive( - rnd.nextInt(100, 300), - rnd.nextInt(1000, 3000), - color, - color, - rnd.nextBoolean(), - rnd.nextBoolean() - ); - } - _dopClassParameters.Add(entityLocomotive); - } - - public void AddRandomWheels() { - rnd = new Random(); - IDrawingWheels iDrawingWheels; - int wheelsChoice = rnd.nextInt(0, 3); - if (wheelsChoice == 0) { - iDrawingWheels = new DrawingWheel(); - } else if (wheelsChoice == 1) { - iDrawingWheels = new DrawingEmptyWheels(); - } else { - iDrawingWheels = new DrawingWheelsBlueCrom(); - } - iDrawingWheels.SetWheelsCount(rnd.nextInt(2, 5)); - _dopClassParameters.Add(iDrawingWheels); - } - - public void Draw() { - if (drawingLocomotive.EntityLocomotive == null) { - return; - } - Graphics g = pictureBoxGenerated.getGraphics(); - pictureBoxGenerated.paint(g); - drawingLocomotive.DrawTransport(g); - } - -} diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index 1b9ae8b..fa27453 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -68,6 +68,7 @@ public class FormElectricLocomotive { ButtonSelectLocomotive.addActionListener(e->{ SelectedLocomotive = _drawingLocomotive; IsSelect = true; +// DialogResult = DialogResult.OK; }); buttonStep.addActionListener(new ActionListener() { diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index b1bcaa2..28e0da3 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -21,7 +21,7 @@
- + @@ -39,7 +39,7 @@ - + @@ -76,14 +76,6 @@ - - - - - - - - diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 3a48f7f..9b3028c 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -4,7 +4,6 @@ import javax.swing.*; import java.awt.*; public class FormLocomotiveCollections { - FrameDopClassParameters frameDopClassParameters; private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; @@ -12,9 +11,9 @@ public class FormLocomotiveCollections { private JTextField textFieldNumber; private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; - private JButton ButtonCreateRandomLoco; public DrawingLocomotive loco; - LocomotiveGenericCollection _locomotives; + FormElectricLocomotive _formElectricLocomotive; + private final LocomotiveGenericCollection _locomotives; public JPanel getPictureBoxCollections() { return MainPanel; @@ -42,13 +41,7 @@ public class FormLocomotiveCollections { }); }); - ButtonCreateRandomLoco.addActionListener(e->{ - if(frameDopClassParameters!=null) frameDopClassParameters.dispose(); - frameDopClassParameters = new FrameDopClassParameters(); - frameDopClassParameters.setVisible(true); - }); - - ButtonRemoveLocomotive.addActionListener(e -> { + ButtonRemoveLocomotive.addActionListener(e->{ try { int pos = Integer.parseInt(textFieldNumber.getText()); if (_locomotives.SubOverload(pos) != null) { @@ -71,12 +64,10 @@ public class FormLocomotiveCollections { } }); - ButtonRefreshCollection.addActionListener(e -> { + ButtonRefreshCollection.addActionListener(e->{ Refresh(); }); - } - public void Refresh() { Graphics g = pictureBoxCollections.getGraphics(); pictureBoxCollections.paint(g); diff --git a/ProjectElectricLocomotive/FrameDopClassParameters.java b/ProjectElectricLocomotive/FrameDopClassParameters.java deleted file mode 100644 index e5a06ac..0000000 --- a/ProjectElectricLocomotive/FrameDopClassParameters.java +++ /dev/null @@ -1,19 +0,0 @@ -package ProjectElectricLocomotive; - -import javax.swing.*; - -public class FrameDopClassParameters extends JFrame { - public FormDopClassParameters _formDopClassParameters; - - public FrameDopClassParameters(){ - super(); - setTitle("Рандомный локомотив"); - setDefaultCloseOperation(DISPOSE_ON_CLOSE); - _formDopClassParameters = new FormDopClassParameters(); - setContentPane(_formDopClassParameters.getPictureBoxGenerated()); - setDefaultLookAndFeelDecorated(false); - setLocation(500, 200); - pack(); - setVisible(true); - } -} diff --git a/ProjectElectricLocomotive/FrameElectricLocomotive.java b/ProjectElectricLocomotive/FrameElectricLocomotive.java index 9344738..abf9d6b 100644 --- a/ProjectElectricLocomotive/FrameElectricLocomotive.java +++ b/ProjectElectricLocomotive/FrameElectricLocomotive.java @@ -6,8 +6,8 @@ public class FrameElectricLocomotive extends JFrame { public FormElectricLocomotive _formLocomotiveCollection; public FrameElectricLocomotive() { super(); - setTitle("Электролокомотив"); - setDefaultCloseOperation(DISPOSE_ON_CLOSE); + setTitle("ElectroLoco"); + setDefaultCloseOperation(EXIT_ON_CLOSE); _formLocomotiveCollection = new FormElectricLocomotive(); setContentPane(_formLocomotiveCollection.getPictureBox()); setDefaultLookAndFeelDecorated(false); diff --git a/ProjectElectricLocomotive/FrameLocomotiveCollection.java b/ProjectElectricLocomotive/FrameLocomotiveCollection.java index 189aba5..7883880 100644 --- a/ProjectElectricLocomotive/FrameLocomotiveCollection.java +++ b/ProjectElectricLocomotive/FrameLocomotiveCollection.java @@ -6,7 +6,8 @@ public class FrameLocomotiveCollection extends JFrame { public FormLocomotiveCollections _formLocomotiveCollections; public FrameLocomotiveCollection(){ super(); - setTitle("Коллекция");setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle("LocoCollection"); + setDefaultCloseOperation(EXIT_ON_CLOSE); _formLocomotiveCollections = new FormLocomotiveCollections(); setContentPane(_formLocomotiveCollections.getPictureBoxCollections()); setDefaultLookAndFeelDecorated(false); diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index cf1207c..9abe2b2 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -10,11 +10,14 @@ public class LocomotiveGenericCollection _collection; public LocomotiveGenericCollection(int picWidth, int picHeight) { + // немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло... + // высчитываем размер массива для setgeneric int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _pictureWidth = picWidth; -- 2.25.1