From 845860c3dc301f37e80ecdb74e40d613bf51b803 Mon Sep 17 00:00:00 2001 From: marusya Date: Sat, 23 Dec 2023 12:27:11 +0400 Subject: [PATCH] lab2 --- src/AbstractStrategy.java | 82 ++++++ src/DrawingEmptyWheels.java | 26 ++ src/DrawingObjectUsta.java | 28 ++ src/DrawingUsta.java | 241 +++++++----------- src/DrawingUstaBat.java | 157 ++++++++++++ src/DrawingWheel.java | 132 +--------- src/DrawingWheelsBlueCrom.java | 33 +++ src/EntityUsta.java | 15 +- src/EntityUstaBat.java | 15 ++ ...ledArtilleryUnit.form => FormUstaBat.form} | 126 +++++---- src/FormUstaBat.java | 133 ++++++++++ src/FrameUstaBat.java | 16 ++ src/IDrawingWheels.java | 83 ++++++ src/IMoveableObject.java | 6 + src/Main.java | 2 +- src/MainFrameUsta.java | 18 -- src/MoveToCenter.java | 43 ++++ src/MoveToRigthCorner.java | 19 ++ src/ObjectParameters.java | 50 ++++ src/SelfPropelledArtilleryUnit.java | 78 ------ src/Status.java | 5 + 21 files changed, 877 insertions(+), 431 deletions(-) create mode 100644 src/AbstractStrategy.java create mode 100644 src/DrawingEmptyWheels.java create mode 100644 src/DrawingObjectUsta.java create mode 100644 src/DrawingUstaBat.java create mode 100644 src/DrawingWheelsBlueCrom.java create mode 100644 src/EntityUstaBat.java rename src/{SelfPropelledArtilleryUnit.form => FormUstaBat.form} (52%) create mode 100644 src/FormUstaBat.java create mode 100644 src/FrameUstaBat.java create mode 100644 src/IDrawingWheels.java create mode 100644 src/IMoveableObject.java delete mode 100644 src/MainFrameUsta.java create mode 100644 src/MoveToCenter.java create mode 100644 src/MoveToRigthCorner.java create mode 100644 src/ObjectParameters.java delete mode 100644 src/SelfPropelledArtilleryUnit.java create mode 100644 src/Status.java diff --git a/src/AbstractStrategy.java b/src/AbstractStrategy.java new file mode 100644 index 0000000..3c4326f --- /dev/null +++ b/src/AbstractStrategy.java @@ -0,0 +1,82 @@ +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; + } + if(_moveableObject == null) 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/src/DrawingEmptyWheels.java b/src/DrawingEmptyWheels.java new file mode 100644 index 0000000..427452d --- /dev/null +++ b/src/DrawingEmptyWheels.java @@ -0,0 +1,26 @@ +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.Five; + } + + @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/src/DrawingObjectUsta.java b/src/DrawingObjectUsta.java new file mode 100644 index 0000000..11cb34a --- /dev/null +++ b/src/DrawingObjectUsta.java @@ -0,0 +1,28 @@ +public class DrawingObjectUsta implements IMoveableObject { + private DrawingUsta _drawningUsta = null; + public DrawingObjectUsta(DrawingUsta drawningUsta) + { + _drawningUsta = drawningUsta; + } + public ObjectParameters GetObjectPosition() + { + if (_drawningUsta == null || _drawningUsta.EntityUsta == null) + { + return null; + } + return new ObjectParameters(_drawningUsta.GetPosX(), _drawningUsta.GetPosY(), + _drawningUsta.GetWidth(), _drawningUsta.GetHeight()); + } + public int GetStep(){ + if(_drawningUsta == null) return -1; + return (int)(_drawningUsta.EntityUsta.Step()); + } + public boolean CheckCanMove(DyrectionType direction){ + if(_drawningUsta == null) return false; + return _drawningUsta.CanMove(direction); + } + public void MoveObject(DyrectionType direction){ + if(_drawningUsta == null) return; + _drawningUsta.MoveTransport(direction); + } +} diff --git a/src/DrawingUsta.java b/src/DrawingUsta.java index 1d90345..2c279fd 100644 --- a/src/DrawingUsta.java +++ b/src/DrawingUsta.java @@ -1,37 +1,68 @@ import java.awt.*; import java.awt.geom.Path2D; import java.awt.geom.Arc2D; +import java.util.Random; + public class DrawingUsta { public EntityUsta EntityUsta; - private DrawingWheel _drawingWheel; + protected IDrawingWheels _drawingWheels; private int _pictureWidth; private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private final int _ustaWidth = 170; - private final int _ustaHeight = 90; + public int _startPosX; + public int _startPosY; + private int _ustaWidth = 170; + private int _ustaHeight = 90; - public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, - boolean bodyKit, boolean pushka, int width, int height) + public int GetPosX(){ + return _startPosX; + } + public int GetPosY(){ + return _startPosY; + } + public int GetWidth(){ + return _ustaWidth; + } + public int GetHeight(){ + return _ustaHeight; + } + + public DrawingUsta(int speed, double weight, Color bodyColor, int width, int height) { - if (width < _ustaWidth || height < _ustaHeight) { - return false; + return; } _pictureWidth = width; _pictureHeight = height; - EntityUsta = new EntityUsta(); - _drawingWheel = new DrawingWheel(); - EntityUsta.Init(speed, weight, bodyColor, additionalColor, bodyKit, pushka); - return true; + EntityUsta = new EntityUsta(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(); } - public void SetWheelsCount(int weelsCount) { - - _drawingWheel.SetWheelsCount(weelsCount); + public void SetWheelsCount(int wheelsCount){ + _drawingWheels.SetWheelsCount(wheelsCount); } + protected DrawingUsta(int speed, double weight, Color bodyColor, int width, + int height, int ustaWidth, int ustaHeight) + { + this(speed,weight, bodyColor, width, height); + if (width < _ustaWidth || height < _ustaHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _ustaWidth = ustaWidth; + _ustaHeight = ustaHeight; + } + + public void SetPosition(int x, int y) { if (x < 0 || x + _ustaWidth > _pictureWidth) @@ -72,11 +103,7 @@ public class DrawingUsta { if (EntityUsta == null) { return; } - - - Color bodyColor = EntityUsta.BodyColor; - Color additionalColor = EntityUsta.AdditionalColor; Color blackBrush = Color.BLACK; @@ -85,146 +112,56 @@ public class DrawingUsta { int cornerRadius1 = 12; - _drawingWheel.DrawWheels(g, additionalColor, _startPosX, _startPosY); + Path2D path = new Path2D.Double(); - g.setColor(color1); + path.append(new Arc2D.Double(_startPosX + 5, _startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 180, 90, Arc2D.OPEN), true); + + path.append(new Arc2D.Double(_startPosX + 140, _startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 270, 90, Arc2D.OPEN), true); + + path.append(new Arc2D.Double(_startPosX + 140, _startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 0, 90, Arc2D.OPEN), true); + + path.append(new Arc2D.Double(_startPosX + 5, _startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 90, 90, Arc2D.OPEN), true); + path.closePath(); + g.setColor(bodyColor); + g.fill(path); + g.setColor(Color.BLACK); + g.draw(path); + + _drawingWheels.DrawWheels(g, blackBrush, _startPosX, _startPosY, 9,9); + + g.setColor(bodyColor); g.fillRect(_startPosX + 45, _startPosY + 20, 40, 20); g.setColor(Color.BLACK); g.drawRect(_startPosX + 45, _startPosY + 20, 40, 20); - - - g.setColor(color1); - + g.setColor(bodyColor); g.fillRect(_startPosX + 10, _startPosY + 40, 120, 10); - g.setColor(Color.BLACK); g.drawRect(_startPosX + 10, _startPosY + 40, 120, 10); - if(EntityUsta.BodyKit) - { - Path2D path1 = new Path2D.Double(); - path1.moveTo(_startPosX + 15, _startPosY + 40); - path1.lineTo(_startPosX + 15, _startPosY + 30); - path1.lineTo(_startPosX + 15, _startPosY + 35); - path1.lineTo(_startPosX + 25, _startPosY + 35); - path1.lineTo(_startPosX + 30, _startPosY + 30); - path1.lineTo(_startPosX + 35, _startPosY + 40); - path1.closePath(); - - g.setColor(Color.BLACK); - - g.draw(path1); - - g.setColor(color2); - g.fill(path1); - - Path2D path2 = new Path2D.Double(); - path2.moveTo(_startPosX + 15, _startPosY + 30); - path2.lineTo(_startPosX + 40, _startPosY + 20); - path2.lineTo(_startPosX + 40, _startPosY + 25); - path2.lineTo(_startPosX + 25, _startPosY + 35); - path2.closePath(); - - g.setColor(Color.BLACK); - g.draw(path2); - - g.setColor(additionalColor); - g.fill(path2); - - Path2D path3 = new Path2D.Double(); - path3.moveTo(_startPosX + 10, _startPosY + 32); - path3.lineTo(_startPosX, _startPosY + 10); - path3.lineTo(_startPosX + 40, _startPosY - 7); - path3.lineTo(_startPosX + 50, _startPosY + 16); - path3.closePath(); - - g.setColor(Color.BLACK); - g.draw(path3); - - g.setColor(bodyColor); - g.fill(path3); - - Path2D path4 = new Path2D.Double(); - path4.moveTo(_startPosX, _startPosY + 10); - path4.lineTo(_startPosX + 40, _startPosY - 7); - path4.lineTo(_startPosX + 43, _startPosY - 5); - path4.lineTo(_startPosX + 41, _startPosY - 3); - path4.lineTo(_startPosX + 2, _startPosY + 13); - path4.closePath(); - - g.setColor(Color.BLACK); - - g.draw(path4); - - g.setColor(additionalColor); - g.fill(path4); - - Path2D path5 = new Path2D.Double(); - path5.moveTo(_startPosX + 3, _startPosY + 15); - path5.lineTo(_startPosX + 45, _startPosY - 2); - path5.lineTo(_startPosX + 46, _startPosY - 2); - path5.lineTo(_startPosX + 49, _startPosY - 1); - path5.lineTo(_startPosX + 46, _startPosY + 3); - path5.lineTo(_startPosX + 6, _startPosY + 20); - path5.closePath(); - - g.setColor(Color.BLACK); - - g.draw(path5); - - g.setColor(bodyColor); - g.fill(path5); - - Path2D path6 = new Path2D.Double(); - path6.moveTo(_startPosX + 5, _startPosY + 22); - path6.lineTo(_startPosX + 47, _startPosY + 5); - path6.lineTo(_startPosX + 51, _startPosY + 5); - path6.lineTo(_startPosX + 53, _startPosY + 7); - path6.lineTo(_startPosX + 8, _startPosY + 25); - path6.closePath(); - - g.setColor(Color.BLACK); - - g.draw(path6); - - g.setColor(additionalColor); - g.fill(path6); - - Path2D path7 = new Path2D.Double(); - path7.moveTo(_startPosX + 7, _startPosY + 27); - path7.lineTo(_startPosX + 46, _startPosY + 11); - path7.lineTo(_startPosX + 51, _startPosY + 9); - path7.lineTo(_startPosX + 56, _startPosY + 11); - path7.lineTo(_startPosX + 10, _startPosY + 31); - path7.closePath(); - - g.setColor(Color.BLACK); - - g.draw(path7); - - g.setColor(bodyColor); - g.fill(path7); - } - - - - if(EntityUsta.Pushka) - { - g.fillRect(_startPosX + 80, _startPosY + 25, 10, 10); - - g.setColor(Color.BLACK); - g.drawRect(_startPosX + 80, _startPosY + 25, 10, 10); - g.setColor(color1); - g.fillRect(_startPosX + 90, _startPosY + 28, 40, 5); - - g.setColor(Color.BLACK); - g.drawRect(_startPosX + 90, _startPosY + 28, 40, 5); - g.setColor(additionalColor); - g.fillRect(_startPosX + 130, _startPosY + 27, 5, 7); - - g.setColor(Color.BLACK); - g.drawRect(_startPosX + 130, _startPosY + 27, 5, 7); - } } + + public boolean CanMove(DyrectionType direction) + { + if (EntityUsta == null) + { + return false; + } + switch(direction) { + //влево + case Left: + if (_startPosX - EntityUsta.Step() > 0) return true; + break; + case Up: + if (_startPosY - EntityUsta.Step() > 0) return true; + break; + case Right: + if (_startPosX + EntityUsta.Step() < _pictureWidth) return true; + break; + case Down: + if (_startPosY + EntityUsta.Step() < _pictureHeight) return true; + break; + } + return false; + } } diff --git a/src/DrawingUstaBat.java b/src/DrawingUstaBat.java new file mode 100644 index 0000000..d29a735 --- /dev/null +++ b/src/DrawingUstaBat.java @@ -0,0 +1,157 @@ +import java.awt.*; +import java.awt.geom.Path2D; + +public class DrawingUstaBat extends DrawingUsta { + public DrawingUstaBat(int speed, double weight, Color bodyColor, Color additionalColor, + boolean pushka, boolean bodyKit, int width, int height) + { + super(speed, weight, bodyColor, width, height, 170, 90); + if (EntityUsta != null) + { + EntityUsta = new EntityUstaBat(speed, width, bodyColor, additionalColor, pushka, bodyKit); + } + } + @Override + public void DrawTransport(Graphics2D g) + { + if (EntityUsta instanceof EntityUstaBat ustabat) ///////// WARNING INSTANCEOF + { + Color bodyColor = EntityUsta.BodyColor; + Color additionalColor = ustabat.AdditionalColor; + + Color blackBrush = Color.BLACK; + + Color color1 = new Color(65, 72, 51); + Color color2 = new Color(47, 69, 56); + + if(ustabat.BodyKit) + { + Path2D path1 = new Path2D.Double(); + path1.moveTo(_startPosX + 15, _startPosY + 40); + path1.lineTo(_startPosX + 15, _startPosY + 30); + path1.lineTo(_startPosX + 15, _startPosY + 35); + path1.lineTo(_startPosX + 25, _startPosY + 35); + path1.lineTo(_startPosX + 30, _startPosY + 30); + path1.lineTo(_startPosX + 35, _startPosY + 40); + path1.closePath(); + + g.setColor(Color.BLACK); + + g.draw(path1); + + g.setColor(color2); + g.fill(path1); + + Path2D path2 = new Path2D.Double(); + path2.moveTo(_startPosX + 15, _startPosY + 30); + path2.lineTo(_startPosX + 40, _startPosY + 20); + path2.lineTo(_startPosX + 40, _startPosY + 25); + path2.lineTo(_startPosX + 25, _startPosY + 35); + path2.closePath(); + + g.setColor(Color.BLACK); + g.draw(path2); + + g.setColor(additionalColor); + g.fill(path2); + + Path2D path3 = new Path2D.Double(); + path3.moveTo(_startPosX + 10, _startPosY + 32); + path3.lineTo(_startPosX, _startPosY + 10); + path3.lineTo(_startPosX + 40, _startPosY - 7); + path3.lineTo(_startPosX + 50, _startPosY + 16); + path3.closePath(); + + g.setColor(Color.BLACK); + g.draw(path3); + + g.setColor(bodyColor); + g.fill(path3); + + Path2D path4 = new Path2D.Double(); + path4.moveTo(_startPosX, _startPosY + 10); + path4.lineTo(_startPosX + 40, _startPosY - 7); + path4.lineTo(_startPosX + 43, _startPosY - 5); + path4.lineTo(_startPosX + 41, _startPosY - 3); + path4.lineTo(_startPosX + 2, _startPosY + 13); + path4.closePath(); + + g.setColor(Color.BLACK); + + g.draw(path4); + + g.setColor(additionalColor); + g.fill(path4); + + Path2D path5 = new Path2D.Double(); + path5.moveTo(_startPosX + 3, _startPosY + 15); + path5.lineTo(_startPosX + 45, _startPosY - 2); + path5.lineTo(_startPosX + 46, _startPosY - 2); + path5.lineTo(_startPosX + 49, _startPosY - 1); + path5.lineTo(_startPosX + 46, _startPosY + 3); + path5.lineTo(_startPosX + 6, _startPosY + 20); + path5.closePath(); + + g.setColor(Color.BLACK); + + g.draw(path5); + + g.setColor(bodyColor); + g.fill(path5); + + Path2D path6 = new Path2D.Double(); + path6.moveTo(_startPosX + 5, _startPosY + 22); + path6.lineTo(_startPosX + 47, _startPosY + 5); + path6.lineTo(_startPosX + 51, _startPosY + 5); + path6.lineTo(_startPosX + 53, _startPosY + 7); + path6.lineTo(_startPosX + 8, _startPosY + 25); + path6.closePath(); + + g.setColor(Color.BLACK); + + g.draw(path6); + + g.setColor(additionalColor); + g.fill(path6); + + Path2D path7 = new Path2D.Double(); + path7.moveTo(_startPosX + 7, _startPosY + 27); + path7.lineTo(_startPosX + 46, _startPosY + 11); + path7.lineTo(_startPosX + 51, _startPosY + 9); + path7.lineTo(_startPosX + 56, _startPosY + 11); + path7.lineTo(_startPosX + 10, _startPosY + 31); + path7.closePath(); + + g.setColor(Color.BLACK); + + g.draw(path7); + + g.setColor(bodyColor); + g.fill(path7); + } + + + + + + if(ustabat.Pushka) + { + g.fillRect(_startPosX + 80, _startPosY + 25, 10, 10); + + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 80, _startPosY + 25, 10, 10); + g.setColor(color1); + g.fillRect(_startPosX + 90, _startPosY + 28, 40, 5); + + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 90, _startPosY + 28, 40, 5); + g.setColor(additionalColor); + g.fillRect(_startPosX + 130, _startPosY + 27, 5, 7); + + g.setColor(Color.BLACK); + g.drawRect(_startPosX + 130, _startPosY + 27, 5, 7); + } + super.DrawTransport(g); + } + } +} diff --git a/src/DrawingWheel.java b/src/DrawingWheel.java index d76ff04..6ec2a06 100644 --- a/src/DrawingWheel.java +++ b/src/DrawingWheel.java @@ -2,7 +2,7 @@ import java.awt.*; import java.awt.geom.Arc2D; import java.awt.geom.Path2D; -public class DrawingWheel { +public class DrawingWheel implements IDrawingWheels { private WheelsCount _wheelsCount; public void SetWheelsCount(int enginesCount) { for (WheelsCount val : WheelsCount.values()) { @@ -11,130 +11,16 @@ public class DrawingWheel { return; } } + this._wheelsCount = WheelsCount.Five; } - public void DrawWheels(Graphics2D g, Color color, int startPosX, int startPosY) { - if (_wheelsCount == null) { - return; - } + @Override + public WheelsCount GetWheelsCount() { + return _wheelsCount; + } - int cornerRadius1 = 12; - Color color1 = new Color(65, 72, 51); - - if (_wheelsCount.count >= _wheelsCount.Four.count) { - - Path2D path = new Path2D.Double(); - - path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 180, 90, Arc2D.OPEN), true); - - path.append(new Arc2D.Double(startPosX + 110, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 270, 90, Arc2D.OPEN), true); - - path.append(new Arc2D.Double(startPosX + 110, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 0, 90, Arc2D.OPEN), true); - - path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 90, 90, Arc2D.OPEN), true); - path.closePath(); - - g.setColor(color1); - - g.fill(path); - g.setColor(Color.BLACK); - g.draw(path); - - - /* наверху гусеницы*/ - g.fillOval(startPosX + 47, startPosY + 44, 13, 13); - g.fillOval(startPosX + 62, startPosY + 44, 13, 13); - g.fillOval(startPosX + 77, startPosY + 44, 13, 13); - - /* по середине гусеницы*/ - g.drawOval(startPosX + 40, startPosY + 59, 13, 13); - g.drawOval(startPosX + 55, startPosY + 59, 13, 13); - g.drawOval(startPosX + 70, startPosY + 59, 13, 13); - g.drawOval(startPosX + 85, startPosY + 59, 13, 13); - - - /* два крайних на гусенице*/ - g.fillOval(startPosX + 10, startPosY + 50, 25, 25); - g.fillOval(startPosX + 105, startPosY + 50, 25, 25); - - } - - if (_wheelsCount.count >= _wheelsCount.Five.count) { - Path2D path = new Path2D.Double(); - - path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 180, 90, Arc2D.OPEN), true); - - path.append(new Arc2D.Double(startPosX + 125, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 270, 90, Arc2D.OPEN), true); - - path.append(new Arc2D.Double(startPosX + 125, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 0, 90, Arc2D.OPEN), true); - - path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 90, 90, Arc2D.OPEN), true); - path.closePath(); - - g.setColor(color1); - - g.fill(path); - g.setColor(Color.BLACK); - g.draw(path); - - - /* наверху гусеницы*/ - g.fillOval(startPosX + 47, startPosY + 44, 13, 13); - g.fillOval(startPosX + 62, startPosY + 44, 13, 13); - g.fillOval(startPosX + 77, startPosY + 44, 13, 13); - g.fillOval(startPosX + 92, startPosY + 44, 13, 13); - - /* по середине гусеницы*/ - g.drawOval(startPosX + 40, startPosY + 59, 13, 13); - g.drawOval(startPosX + 55, startPosY + 59, 13, 13); - g.drawOval(startPosX + 70, startPosY + 59, 13, 13); - g.drawOval(startPosX + 85, startPosY + 59, 13, 13); - g.drawOval(startPosX + 100, startPosY + 59, 13, 13); - - - /* два крайних на гусенице*/ - g.fillOval(startPosX + 10, startPosY + 50, 25, 25); - g.fillOval(startPosX + 120, startPosY + 50, 25, 25); - } - - if (_wheelsCount.count >= _wheelsCount.Six.count) { - Path2D path = new Path2D.Double(); - - path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 180, 90, Arc2D.OPEN), true); - - path.append(new Arc2D.Double(startPosX + 140, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 270, 90, Arc2D.OPEN), true); - - path.append(new Arc2D.Double(startPosX + 140, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 0, 90, Arc2D.OPEN), true); - - path.append(new Arc2D.Double(startPosX + 5, startPosY + 50, 2 * cornerRadius1, 2 * cornerRadius1, 90, 90, Arc2D.OPEN), true); - path.closePath(); - - g.setColor(color1); - - g.fill(path); - g.setColor(Color.BLACK); - g.draw(path); - - - /* наверху гусеницы*/ - g.fillOval(startPosX + 47, startPosY + 44, 13, 13); - g.fillOval(startPosX + 62, startPosY + 44, 13, 13); - g.fillOval(startPosX + 77, startPosY + 44, 13, 13); - g.fillOval(startPosX + 92, startPosY + 44, 13, 13); - g.fillOval(startPosX + 107, startPosY + 44, 13, 13); - - /* по середине гусеницы*/ - g.drawOval(startPosX + 40, startPosY + 59, 13, 13); - g.drawOval(startPosX + 55, startPosY + 59, 13, 13); - g.drawOval(startPosX + 70, startPosY + 59, 13, 13); - g.drawOval(startPosX + 85, startPosY + 59, 13, 13); - g.drawOval(startPosX + 100, startPosY + 59, 13, 13); - g.drawOval(startPosX + 115, startPosY + 59, 13, 13); - - - /* два крайних на гусенице*/ - g.fillOval(startPosX + 10, startPosY + 50, 25, 25); - g.fillOval(startPosX + 135, startPosY + 50, 25, 25); - } + 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); } } diff --git a/src/DrawingWheelsBlueCrom.java b/src/DrawingWheelsBlueCrom.java new file mode 100644 index 0000000..02e96ed --- /dev/null +++ b/src/DrawingWheelsBlueCrom.java @@ -0,0 +1,33 @@ +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.Five; + } + + @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.GRAY); + g2d.drawOval(x, y + 3, w-5, h-5); + g2d.setColor(Color.GREEN); + g2d.drawOval(x - 2, y - 2, w, h); + g2d.setColor(Color.WHITE); + g2d.drawOval(x + 3, y, w - 10, h - 10); + + g2d.setColor(Color.BLACK); + g2d.drawOval(x, y, w, h); + } +} diff --git a/src/EntityUsta.java b/src/EntityUsta.java index 72ce791..556a80a 100644 --- a/src/EntityUsta.java +++ b/src/EntityUsta.java @@ -4,21 +4,18 @@ public class EntityUsta { public int Speed; public double Weight; public Color BodyColor; - public Color AdditionalColor; - public boolean BodyKit; - public boolean Pushka; + public EntityUsta(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 bodyKit, boolean pushka) - { + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifbatteries) { Speed = speed; Weight = weight; BodyColor = bodyColor; - AdditionalColor = additionalColor; - BodyKit = bodyKit; - Pushka = pushka; } } diff --git a/src/EntityUstaBat.java b/src/EntityUstaBat.java new file mode 100644 index 0000000..e5ff52c --- /dev/null +++ b/src/EntityUstaBat.java @@ -0,0 +1,15 @@ +import java.awt.*; + +public class EntityUstaBat extends EntityUsta { + public Color AdditionalColor; + public boolean Pushka; + public boolean BodyKit; + public EntityUstaBat(int speed, double weight, Color bodyColor, Color additionalColor, boolean pushka, boolean bodyKit) + { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + Pushka = pushka; + BodyKit = bodyKit; + } + +} diff --git a/src/SelfPropelledArtilleryUnit.form b/src/FormUstaBat.form similarity index 52% rename from src/SelfPropelledArtilleryUnit.form rename to src/FormUstaBat.form index 16c6120..327f8a8 100644 --- a/src/SelfPropelledArtilleryUnit.form +++ b/src/FormUstaBat.form @@ -1,39 +1,93 @@ -
- + + - + - + + + + - - - - - - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + @@ -46,30 +100,16 @@ - - - - - - - - - - - - - - - - - - + + + + - + @@ -77,20 +117,6 @@ - - - - - - - - - - - - - - diff --git a/src/FormUstaBat.java b/src/FormUstaBat.java new file mode 100644 index 0000000..7723294 --- /dev/null +++ b/src/FormUstaBat.java @@ -0,0 +1,133 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormUstaBat { + public DrawingUsta _drawingUsta; + AbstractStrategy _abstractStrategy; + private JButton buttonCreateUstaBat; + private JPanel pictureBox; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + public JComboBox comboBoxStrategy; + private JButton buttonStep; + private JButton buttonCreateUsta; + public JPanel getPictureBox() { + return pictureBox; + } + public FormUstaBat() + { + buttonUp.setName("buttonUp"); + buttonDown.setName("buttonDown"); + buttonLeft.setName("buttonLeft"); + buttonRight.setName("buttonRight"); + + buttonCreateUsta.addActionListener(e -> { + Random rnd = new Random(); + + _drawingUsta = new DrawingUsta(rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + pictureBox.getWidth(), + pictureBox.getHeight()); + + _drawingUsta.SetWheelsCount(rnd.nextInt(4, 7)); + _drawingUsta.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + Draw(); + }); + + buttonCreateUstaBat.addActionListener(e -> { + Random random = new Random(); + + _drawingUsta = new DrawingUstaBat( + 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() + ); + + _drawingUsta.SetWheelsCount(random.nextInt(2, 5)); + _drawingUsta.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + Draw(); + }); + + buttonStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawingUsta == 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 + DrawingObjectUsta(_drawingUsta), 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") -> { + _drawingUsta.MoveTransport(DyrectionType.Up); + } + case ("buttonDown") -> { + _drawingUsta.MoveTransport(DyrectionType.Down); + } + case ("buttonLeft") -> { + _drawingUsta.MoveTransport(DyrectionType.Left); + } + case ("buttonRight") -> { + _drawingUsta.MoveTransport(DyrectionType.Right); + } + } + Draw(); + }; + buttonUp.addActionListener(buttonMoveClickedListener); + buttonDown.addActionListener(buttonMoveClickedListener); + buttonLeft.addActionListener(buttonMoveClickedListener); + buttonRight.addActionListener(buttonMoveClickedListener); + } + public void Draw() { + if (_drawingUsta.EntityUsta == null) { + return; + } + Graphics g = pictureBox.getGraphics(); + pictureBox.paint(g); + _drawingUsta.DrawTransport((Graphics2D) g); + } +} diff --git a/src/FrameUstaBat.java b/src/FrameUstaBat.java new file mode 100644 index 0000000..7072744 --- /dev/null +++ b/src/FrameUstaBat.java @@ -0,0 +1,16 @@ +import javax.swing.*; + +public class FrameUstaBat extends JFrame { + public FormUstaBat _formUstaCollection; + public FrameUstaBat() { + super(); + setTitle("UstaBat"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + _formUstaCollection = new FormUstaBat(); + setContentPane(_formUstaCollection.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(500, 200); + pack(); + setVisible(true); + } +} diff --git a/src/IDrawingWheels.java b/src/IDrawingWheels.java new file mode 100644 index 0000000..2368042 --- /dev/null +++ b/src/IDrawingWheels.java @@ -0,0 +1,83 @@ +import java.awt.*; +import java.awt.geom.Arc2D; +import java.awt.geom.Path2D; + +public interface IDrawingWheels { + void SetWheelsCount(int wheelsCount); + + WheelsCount GetWheelsCount(); + + void DrawWheel(Graphics2D g2d, Color color, int x, int y, int w, int h); + + default void DrawWheels(Graphics2D g, Color color, int startPosX, int startPosY, int drawingWidth, int drawingHeight){ + WheelsCount wheelsCount = GetWheelsCount(); + if(wheelsCount == null) return; + + if (wheelsCount.count <= wheelsCount.Four.count) { + + + /* наверху гусеницы*/ + g.fillOval(startPosX + 47, startPosY + 44, 13, 13); + g.fillOval(startPosX + 62, startPosY + 44, 13, 13); + g.fillOval(startPosX + 77, startPosY + 44, 13, 13); + + /* по середине гусеницы*/ + DrawWheel(g, color, startPosX + 40, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 55, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 70, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 85, startPosY + 59, 12, 12); + + + /* два крайних на гусенице*/ + g.fillOval(startPosX + 10, startPosY + 50, 25, 24); + g.fillOval(startPosX + 105, startPosY + 50, 25, 24); + + } + + if (wheelsCount.count == wheelsCount.Five.count) { + + + /* наверху гусеницы*/ + g.fillOval(startPosX + 47, startPosY + 44, 13, 13); + g.fillOval(startPosX + 62, startPosY + 44, 13, 13); + g.fillOval(startPosX + 77, startPosY + 44, 13, 13); + g.fillOval(startPosX + 92, startPosY + 44, 13, 13); + + /* по середине гусеницы*/ + DrawWheel(g, color, startPosX + 40, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 55, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 70, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 85, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 100, startPosY + 59, 12, 12); + + + /* два крайних на гусенице*/ + g.fillOval(startPosX + 10, startPosY + 50, 25, 24); + g.fillOval(startPosX + 120, startPosY + 50, 25, 24); + } + + if (wheelsCount.count == wheelsCount.Six.count) { + + + /* наверху гусеницы*/ + g.fillOval(startPosX + 47, startPosY + 44, 13, 13); + g.fillOval(startPosX + 62, startPosY + 44, 13, 13); + g.fillOval(startPosX + 77, startPosY + 44, 13, 13); + g.fillOval(startPosX + 92, startPosY + 44, 13, 13); + g.fillOval(startPosX + 107, startPosY + 44, 13, 13); + + /* по середине гусеницы*/ + DrawWheel(g, color, startPosX + 40, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 55, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 70, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 85, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 100, startPosY + 59, 12, 12); + DrawWheel(g, color, startPosX + 115, startPosY + 59, 12, 12); + + + /* два крайних на гусенице*/ + g.fillOval(startPosX + 10, startPosY + 50, 25, 24); + g.fillOval(startPosX + 135, startPosY + 50, 25, 24); + } + } +} diff --git a/src/IMoveableObject.java b/src/IMoveableObject.java new file mode 100644 index 0000000..ba6769d --- /dev/null +++ b/src/IMoveableObject.java @@ -0,0 +1,6 @@ +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean CheckCanMove(DyrectionType direction); + void MoveObject(DyrectionType direction); +} diff --git a/src/Main.java b/src/Main.java index 6ba7eab..a153400 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,6 @@ public class Main { public static void main(String[] args) { - MainFrameUsta mainFrameUsta = new MainFrameUsta(); + FrameUstaBat mainFrame = new FrameUstaBat(); } } diff --git a/src/MainFrameUsta.java b/src/MainFrameUsta.java deleted file mode 100644 index 902c4bd..0000000 --- a/src/MainFrameUsta.java +++ /dev/null @@ -1,18 +0,0 @@ -import javax.swing.*; - -public class MainFrameUsta extends JFrame { - private SelfPropelledArtilleryUnit _formUsta; - - public MainFrameUsta() { - super(); - setTitle("ArtilleryUnit"); - setDefaultCloseOperation(EXIT_ON_CLOSE); - _formUsta = new SelfPropelledArtilleryUnit(); - setContentPane(_formUsta.getPictureBox()); - setDefaultLookAndFeelDecorated(false); - setLocation(500, 50); - setSize(500, 850); - pack(); - setVisible(true); - } -} diff --git a/src/MoveToCenter.java b/src/MoveToCenter.java new file mode 100644 index 0000000..9802e87 --- /dev/null +++ b/src/MoveToCenter.java @@ -0,0 +1,43 @@ +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/src/MoveToRigthCorner.java b/src/MoveToRigthCorner.java new file mode 100644 index 0000000..bd476b5 --- /dev/null +++ b/src/MoveToRigthCorner.java @@ -0,0 +1,19 @@ +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/src/ObjectParameters.java b/src/ObjectParameters.java new file mode 100644 index 0000000..0573525 --- /dev/null +++ b/src/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; + } +} diff --git a/src/SelfPropelledArtilleryUnit.java b/src/SelfPropelledArtilleryUnit.java deleted file mode 100644 index 62bd8c7..0000000 --- a/src/SelfPropelledArtilleryUnit.java +++ /dev/null @@ -1,78 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionListener; -import java.util.Random; - -public class SelfPropelledArtilleryUnit { - DrawingUsta _drawingUsta = new DrawingUsta(); - private JButton buttonCreate; - private JPanel pictureBox; - private JButton buttonUp; - private JButton buttonDown; - private JButton buttonLeft; - private JButton buttonRight; - - public JPanel getPictureBox() { - return pictureBox; - } - public SelfPropelledArtilleryUnit() - { - buttonUp.setName("buttonUp"); - buttonDown.setName("buttonDown"); - buttonLeft.setName("buttonLeft"); - buttonRight.setName("buttonRight"); - - buttonCreate.addActionListener(e -> { - _drawingUsta = new DrawingUsta(); - Random random = new Random(); - - _drawingUsta.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() - ); - - _drawingUsta.SetWheelsCount(random.nextInt(4, 7)); - _drawingUsta.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); - Draw(); - }); - - ActionListener buttonMoveClickedListener = e -> { - String buttonName = ((JButton) e.getSource()).getName(); - - switch (buttonName) { - case ("buttonUp") -> { - _drawingUsta.MoveTransport(DyrectionType.Up); - } - case ("buttonDown") -> { - _drawingUsta.MoveTransport(DyrectionType.Down); - } - case ("buttonLeft") -> { - _drawingUsta.MoveTransport(DyrectionType.Left); - } - case ("buttonRight") -> { - _drawingUsta.MoveTransport(DyrectionType.Right); - } - } - Draw(); - }; - buttonUp.addActionListener(buttonMoveClickedListener); - buttonDown.addActionListener(buttonMoveClickedListener); - buttonLeft.addActionListener(buttonMoveClickedListener); - buttonRight.addActionListener(buttonMoveClickedListener); - } - public void Draw() { - if (_drawingUsta.EntityUsta == null) { - return; - } - Graphics g = pictureBox.getGraphics(); - pictureBox.paint(g); - _drawingUsta.DrawTransport((Graphics2D) g); - } - -} diff --git a/src/Status.java b/src/Status.java new file mode 100644 index 0000000..6467ed7 --- /dev/null +++ b/src/Status.java @@ -0,0 +1,5 @@ +public enum Status { + NotInit, + InProgress, + Finish +} -- 2.25.1