From 2b2b700c59b85f80eadd6247b158041bd0c16f61 Mon Sep 17 00:00:00 2001 From: Timourka Date: Sat, 7 Oct 2023 21:04:33 +0400 Subject: [PATCH 1/5] commit1 --- laba1Loco/EntityTrain.java | 39 ++++++++++++++++++++++++ laba1Loco/{Form1.java => FormTrain.java} | 4 +-- laba1Loco/Main.java | 2 +- laba1Loco/Status.java | 7 +++++ 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 laba1Loco/EntityTrain.java rename laba1Loco/{Form1.java => FormTrain.java} (98%) create mode 100644 laba1Loco/Status.java diff --git a/laba1Loco/EntityTrain.java b/laba1Loco/EntityTrain.java new file mode 100644 index 0000000..c884cec --- /dev/null +++ b/laba1Loco/EntityTrain.java @@ -0,0 +1,39 @@ +package laba1Loco; + +import java.awt.*; +import java.util.*; +import javax.swing.*; +import javax.swing.Timer; +import java.awt.event.*; + +public class EntityTrain { + /// + /// Скорость + /// + public int Speed; + /// + /// Вес + /// + public double Weight; + /// + /// Основной цвет + /// + public Color BodyColor; + /// + /// Шаг перемещения поезда + /// + public double Step; + /// + /// Инициализация полей объекта-класса Локомотива + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + public EntityTrain(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Step = (double)Speed * 100 / Weight; + } +} diff --git a/laba1Loco/Form1.java b/laba1Loco/FormTrain.java similarity index 98% rename from laba1Loco/Form1.java rename to laba1Loco/FormTrain.java index 6ad7e67..1e9ad91 100644 --- a/laba1Loco/Form1.java +++ b/laba1Loco/FormTrain.java @@ -6,7 +6,7 @@ import javax.swing.*; import javax.swing.Timer; import java.awt.event.*; -public class Form1{ +public class FormTrain{ private DrawingLoco _drawingLoco; Canvas canv; @@ -14,7 +14,7 @@ public class Form1{ canv.repaint(); } - public Form1(){ + public FormTrain(){ JFrame w=new JFrame ("Loco"); JButton buttonCreate = new JButton("create"); JButton up = new JButton(); diff --git a/laba1Loco/Main.java b/laba1Loco/Main.java index 21414f2..3367941 100644 --- a/laba1Loco/Main.java +++ b/laba1Loco/Main.java @@ -2,6 +2,6 @@ package laba1Loco; public class Main{ public static void main(String[] args) { - Form1 form1 = new Form1(); + FormTrain formTrain = new FormTrain(); } } \ No newline at end of file diff --git a/laba1Loco/Status.java b/laba1Loco/Status.java new file mode 100644 index 0000000..1bbe051 --- /dev/null +++ b/laba1Loco/Status.java @@ -0,0 +1,7 @@ +package laba1Loco; + +public enum Status { + NotInit, + InProgress, + Finish +} -- 2.25.1 From 567a87a8485005905395a0ad59f4ea291d0241b3 Mon Sep 17 00:00:00 2001 From: Timourka Date: Sun, 8 Oct 2023 01:10:54 +0400 Subject: [PATCH 2/5] Base --- laba1Loco/AbstractStrategy.java | 122 +++++++++++ laba1Loco/DrawingLoco.java | 337 ++++++++++------------------- laba1Loco/DrawingTrain.java | 193 +++++++++++++++++ laba1Loco/DrawningObjectTrain.java | 22 ++ laba1Loco/EntityLoco.java | 89 +++----- laba1Loco/EntityTrain.java | 7 +- laba1Loco/FormTrain.java | 121 +++++++++-- laba1Loco/IMoveableObject.java | 23 ++ laba1Loco/MoveToBorder.java | 48 ++++ laba1Loco/MoveToCenter.java | 48 ++++ laba1Loco/ObjectParameters.java | 46 ++++ 11 files changed, 750 insertions(+), 306 deletions(-) create mode 100644 laba1Loco/AbstractStrategy.java create mode 100644 laba1Loco/DrawingTrain.java create mode 100644 laba1Loco/DrawningObjectTrain.java create mode 100644 laba1Loco/IMoveableObject.java create mode 100644 laba1Loco/MoveToBorder.java create mode 100644 laba1Loco/MoveToCenter.java create mode 100644 laba1Loco/ObjectParameters.java diff --git a/laba1Loco/AbstractStrategy.java b/laba1Loco/AbstractStrategy.java new file mode 100644 index 0000000..0229b0e --- /dev/null +++ b/laba1Loco/AbstractStrategy.java @@ -0,0 +1,122 @@ +package laba1Loco; + +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(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected boolean MoveLeft() { return MoveTo(Direction.Left);} + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected boolean MoveRight() { return MoveTo(Direction.Right);} + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected boolean MoveUp() { return MoveTo(Direction.Up);} + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected boolean MoveDown() { return MoveTo(Direction.Down);} + /// + /// Параметры объекта + /// + protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); } + /// + /// Шаг объекта + /// + /// + protected int GetStep() + { + if (_state != Status.InProgress) + { + return 0; + } + return _moveableObject.GetStep(); + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract boolean IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private boolean MoveTo(Direction Direction) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(Direction)) + { + _moveableObject.MoveObject(Direction); + return true; + } + return false; + } +} diff --git a/laba1Loco/DrawingLoco.java b/laba1Loco/DrawingLoco.java index 8e4c1be..23d62bb 100644 --- a/laba1Loco/DrawingLoco.java +++ b/laba1Loco/DrawingLoco.java @@ -6,239 +6,122 @@ import javax.swing.*; import javax.swing.Timer; import java.awt.event.*; -public class DrawingLoco{ +public class DrawingLoco extends DrawingTrain{ - private WheelDrawing wheelDrawing; + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес + /// Цвет кузова + /// Дополнительный цвет + /// Признак наличия трубы + /// Признак наличия бака + /// Признак наличия паровозной полосы + /// Ширина картинки + /// Высота картинки + public DrawingLoco(int speed, double weight, Color bodyColor, int _numWheel, Color additionalColor, boolean tube, boolean fuelTank, boolean locoLine, int width, int height) + { + super(speed, weight, bodyColor, _numWheel, width, height); + EntityTrain = new EntityLoco(speed, weight, bodyColor, _numWheel, additionalColor, tube, fuelTank, locoLine); + _locoWidth = ((EntityLoco)EntityTrain).FuelTank ? 169 : 83; + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + _startPosX = Math.min(x, _pictureWidth-_locoWidth); + _startPosY = Math.min(y, _pictureHeight-_locoHeight); + } + /// + /// Прорисовка объекта + /// + /// + public void DrawTransport(Graphics2D g2d) + { - /// - /// Класс-сущность - /// - public EntityLoco _EntityLoco; - /// - /// Ширина окна - /// - private int _pictureWidth; - /// - /// Высота окна - /// - private int _pictureHeight; - /// - /// Левая координата прорисовки локомотива - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки локомотива - /// - private int _startPosY; - /// - /// Ширина прорисовки локомотива - /// - private int _locoWidth = 83; - /// - /// Высота прорисовки локомотива - /// - private int _locoHeight = 41; - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес - /// Цвет кузова - /// Дополнительный цвет - /// Признак наличия трубы - /// Признак наличия бака - /// Признак наличия паровозной полосы - /// Ширина картинки - /// Высота картинки - /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean tube, boolean fuelTank, boolean locoLine, int numWheel, int width, int height) + if (EntityTrain == null) { - _pictureWidth = width; - _pictureHeight = height; - if (_pictureHeight < _locoHeight || _pictureWidth < _locoWidth) - return false; - _EntityLoco = new EntityLoco(); - _EntityLoco.Init(speed, weight, bodyColor, additionalColor, tube, fuelTank, locoLine, numWheel); - if (fuelTank) - _locoWidth = 169; - wheelDrawing = new WheelDrawing(); - wheelDrawing.setNumWheel(numWheel); - return true; + return; } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) + + super.DrawTransport(g2d); + + //wheels + wheelDrawing.Draw(_startPosX, _startPosY, ((EntityLoco)EntityTrain).FuelTank, EntityTrain.BodyColor, g2d); + + g2d.setColor(((EntityLoco)EntityTrain).AdditionalColor); + if (((EntityLoco)EntityTrain).Tube) { - _startPosX = Math.min(x, _pictureWidth-_locoWidth); - _startPosY = Math.min(y, _pictureHeight-_locoHeight); - } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(Direction direction) - { - if (_EntityLoco == null){ - return; - } - switch (direction) - { - //влево - case Left: - if (_startPosX - _EntityLoco.Step > 0) - { - _startPosX -= (int)_EntityLoco.Step; - } - break; - //вверх - case Up: - if (_startPosY - _EntityLoco.Step > 0) - { - _startPosY -= (int)_EntityLoco.Step; - } - break; - // вправо - case Right: - if (_startPosX + _locoWidth + _EntityLoco.Step < _pictureWidth) - { - _startPosX += (int)_EntityLoco.Step; - } - break; - //вниз - case Down: - if (_startPosY + _locoHeight + _EntityLoco.Step < _pictureHeight) - { - _startPosY += (int)_EntityLoco.Step; - } - break; - } - } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics2D g2d) - { - - if (_EntityLoco == null) - { - return; - } - - // body - g2d.setColor(_EntityLoco.BodyColor); - g2d.drawPolyline(new int[]{ - _startPosX + 8, _startPosX + 79, _startPosX + 79, _startPosX + 4, _startPosX + 4, _startPosX + 8, - }, new int[]{ - _startPosY+10, _startPosY+10, _startPosY+32, _startPosY+32, _startPosY+20, _startPosY+10 - }, 6); - g2d.drawPolyline(new int[]{ - _startPosX + 4, _startPosX + 29, _startPosX + 29, _startPosX + 37, _startPosX + 37, _startPosX + 79, _startPosX + 37, _startPosX + 37, _startPosX + 29, _startPosX + 29, - }, new int[]{ - _startPosY+21, _startPosY+21, _startPosY+14, _startPosY+14, _startPosY+21, _startPosY+21, _startPosY+21, _startPosY+29, _startPosY+29, _startPosY+21 - }, 10); - - // trucks - g2d.fillPolygon(new int[]{ -_startPosX + 0,_startPosX + 5,_startPosX + 32,_startPosX + 36, - }, new int[]{ -_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37 - }, 4); - g2d.fillPolygon(new int[]{ -_startPosX + 44,_startPosX + 49,_startPosX + 76,_startPosX + 80, - }, new int[]{ -_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37 - }, 4); - - //back - g2d.fillPolygon(new int[]{ -_startPosX + 79,_startPosX + 82,_startPosX + 82,_startPosX + 79, - }, new int[]{ -_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30 - }, 4); - - - //windows - g2d.setColor(Color.BLUE); - g2d.drawRect(_startPosX + 10, _startPosY + 12, 6, 7); - g2d.drawRect(_startPosX + 19, _startPosY + 12, 6, 7); - g2d.drawRect(_startPosX + 72, _startPosY + 12, 6, 7); - - //wheels - wheelDrawing.Draw(_startPosX, _startPosY, _EntityLoco.FuelTank, _EntityLoco.BodyColor, g2d); - - g2d.setColor(_EntityLoco.AdditionalColor); - if (_EntityLoco.Tube) - { - g2d.drawPolyline(new int[] { + g2d.drawPolyline(new int[] { _startPosX + 40,_startPosX + 40,_startPosX + 45,_startPosX + 41,_startPosX + 41,_startPosX + 44,_startPosX + 44,_startPosX + 45,_startPosX + 45, - },new int[] { + },new int[] { _startPosY+9,_startPosY+3,_startPosY+3,_startPosY+3,_startPosY,_startPosY,_startPosY+3,_startPosY+3,_startPosY+9, - },9); - } - if (_EntityLoco.LocoLine) - { - g2d.drawPolyline(new int[] { - _startPosX + 60,_startPosX + 38, - },new int[] { - _startPosY+10,_startPosY+32 - },2); - g2d.drawPolyline(new int[] { - _startPosX + 65,_startPosX + 43, - },new int[] { - _startPosY+10,_startPosY+32 - },2); - g2d.drawPolyline(new int[] { - _startPosX + 70,_startPosX + 48, - },new int[] { - _startPosY+10,_startPosY+32 - },2); - } - if (_EntityLoco.FuelTank) - { - // body - g2d.setColor(_EntityLoco.BodyColor); - g2d.drawPolyline(new int[]{ - _startPosX + 89, _startPosX + 164, _startPosX + 164, _startPosX + 89, _startPosX + 89, - }, new int[]{ - _startPosY+10, _startPosY+10, _startPosY+32, _startPosY+32, _startPosY+10 - }, 5); - g2d.drawPolyline(new int[]{ - _startPosX + 89, _startPosX + 164, - }, new int[]{ - _startPosY+21, _startPosY+21, - }, 2); - - // trucks - g2d.fillPolygon(new int[]{ -_startPosX + 0+85,_startPosX + 5+85,_startPosX + 32+85,_startPosX + 36+85, - }, new int[]{ -_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37 - }, 4); - g2d.fillPolygon(new int[]{ -_startPosX + 44+85,_startPosX + 49+85,_startPosX + 76+85,_startPosX + 80+85, - }, new int[]{ -_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37 - }, 4); - - //back - g2d.fillPolygon(new int[]{ -_startPosX + 79+85,_startPosX + 82+85,_startPosX + 82+85,_startPosX + 79+85, - }, new int[]{ -_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30 - }, 4); - - - //front - g2d.fillPolygon(new int[]{ -_startPosX + 86,_startPosX + 89,_startPosX + 89,_startPosX + 86, - }, new int[]{ -_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30 - }, 4); - - } + },9); } + if (((EntityLoco)EntityTrain).LocoLine) + { + g2d.drawPolyline(new int[] { + _startPosX + 60,_startPosX + 38, + },new int[] { + _startPosY+10,_startPosY+32 + },2); + g2d.drawPolyline(new int[] { + _startPosX + 65,_startPosX + 43, + },new int[] { + _startPosY+10,_startPosY+32 + },2); + g2d.drawPolyline(new int[] { + _startPosX + 70,_startPosX + 48, + },new int[] { + _startPosY+10,_startPosY+32 + },2); + } + if (((EntityLoco)EntityTrain).FuelTank) + { + // body + g2d.setColor(EntityTrain.BodyColor); + g2d.drawPolyline(new int[]{ + _startPosX + 89, _startPosX + 164, _startPosX + 164, _startPosX + 89, _startPosX + 89, + }, new int[]{ + _startPosY+10, _startPosY+10, _startPosY+32, _startPosY+32, _startPosY+10 + }, 5); + g2d.drawPolyline(new int[]{ + _startPosX + 89, _startPosX + 164, + }, new int[]{ + _startPosY+21, _startPosY+21, + }, 2); + + // trucks + g2d.fillPolygon(new int[]{ +_startPosX + 0+85,_startPosX + 5+85,_startPosX + 32+85,_startPosX + 36+85, + }, new int[]{ +_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37 + }, 4); + g2d.fillPolygon(new int[]{ +_startPosX + 44+85,_startPosX + 49+85,_startPosX + 76+85,_startPosX + 80+85, + }, new int[]{ +_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37 + }, 4); + + //back + g2d.fillPolygon(new int[]{ +_startPosX + 79+85,_startPosX + 82+85,_startPosX + 82+85,_startPosX + 79+85, + }, new int[]{ +_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30 + }, 4); + + + //front + g2d.fillPolygon(new int[]{ +_startPosX + 86,_startPosX + 89,_startPosX + 89,_startPosX + 86, + }, new int[]{ +_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30 + }, 4); + + } + } } \ No newline at end of file diff --git a/laba1Loco/DrawingTrain.java b/laba1Loco/DrawingTrain.java new file mode 100644 index 0000000..abfbf12 --- /dev/null +++ b/laba1Loco/DrawingTrain.java @@ -0,0 +1,193 @@ +package laba1Loco; + +import java.awt.*; +import java.util.*; +import javax.swing.*; +import javax.swing.Timer; +import java.awt.event.*; + +public class DrawingTrain { + protected WheelDrawing wheelDrawing; + /// + /// Класс-сущность + /// + public EntityTrain EntityTrain; + /// + /// Ширина окна + /// + protected int _pictureWidth; + /// + /// Высота окна + /// + protected int _pictureHeight; + /// + /// Левая координата прорисовки локомотива + /// + protected int _startPosX; + /// + /// Верхняя кооридната прорисовки локомотива + /// + protected int _startPosY; + /// + /// Ширина прорисовки локомотива + /// + protected int _locoWidth = 83; + /// + /// Высота прорисовки локомотива + /// + protected int _locoHeight = 41; + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес + /// Цвет кузова + /// Ширина картинки + /// Высота картинки + public DrawingTrain(int speed, double weight, Color bodyColor, int _numWheel, int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureHeight < _locoHeight || _pictureWidth < _locoWidth) + return; + EntityTrain = new EntityTrain(speed, weight, bodyColor, _numWheel); + wheelDrawing = new WheelDrawing(); + wheelDrawing.setNumWheel(_numWheel); + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + _startPosX = Math.min(x, _pictureWidth - _locoWidth); + _startPosY = Math.min(y, _pictureHeight - _locoHeight); + } + /// + /// Координата X объекта + /// + public int GetPosX (){return _startPosX;} + /// + /// Координата Y объекта + /// + public int GetPosY (){return _startPosY;} + /// + /// Ширина объекта + /// + public int GetWidth (){return _locoWidth;} + /// + /// Высота объекта + /// + public int GetHeight (){return _locoHeight;} + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public boolean CanMove(Direction direction) + { + if (EntityTrain == null) + { + return false; + } + switch (direction) + { + case Left: + return _startPosX - EntityTrain.Step > 0; + case Right: + return _startPosX + _locoWidth + EntityTrain.Step < _pictureWidth; + case Up: + return _startPosY - EntityTrain.Step > 0; + case Down: + return _startPosY + _locoHeight + EntityTrain.Step < _pictureHeight; + default: + return false; + } + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(Direction direction) + { + if (!CanMove(direction) || EntityTrain == null) + { + return; + } + switch (direction) + { + //влево + case Left: + _startPosX -= (int)EntityTrain.Step; + break; + //вверх + case Up: + _startPosY -= (int)EntityTrain.Step; + break; + // вправо + case Right: + _startPosX += (int)EntityTrain.Step; + break; + //вниз + case Down: + _startPosY += (int)EntityTrain.Step; + break; + } + } + + /// + /// Прорисовка объекта + /// + /// + public void DrawTransport(Graphics2D g2d) + { + + if (EntityTrain == null) + { + return; + } + + // body + g2d.setColor(EntityTrain.BodyColor); + g2d.drawPolyline(new int[]{ + _startPosX + 8, _startPosX + 79, _startPosX + 79, _startPosX + 4, _startPosX + 4, _startPosX + 8, + }, new int[]{ + _startPosY+10, _startPosY+10, _startPosY+32, _startPosY+32, _startPosY+20, _startPosY+10 + }, 6); + g2d.drawPolyline(new int[]{ + _startPosX + 4, _startPosX + 29, _startPosX + 29, _startPosX + 37, _startPosX + 37, _startPosX + 79, _startPosX + 37, _startPosX + 37, _startPosX + 29, _startPosX + 29, + }, new int[]{ + _startPosY+21, _startPosY+21, _startPosY+14, _startPosY+14, _startPosY+21, _startPosY+21, _startPosY+21, _startPosY+29, _startPosY+29, _startPosY+21 + }, 10); + + // trucks + g2d.fillPolygon(new int[]{ +_startPosX + 0,_startPosX + 5,_startPosX + 32,_startPosX + 36, + }, new int[]{ +_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37 + }, 4); + g2d.fillPolygon(new int[]{ +_startPosX + 44,_startPosX + 49,_startPosX + 76,_startPosX + 80, + }, new int[]{ +_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37 + }, 4); + + //back + g2d.fillPolygon(new int[]{ +_startPosX + 79,_startPosX + 82,_startPosX + 82,_startPosX + 79, + }, new int[]{ +_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30 + }, 4); + + + //windows + g2d.setColor(Color.BLUE); + g2d.drawRect(_startPosX + 10, _startPosY + 12, 6, 7); + g2d.drawRect(_startPosX + 19, _startPosY + 12, 6, 7); + g2d.drawRect(_startPosX + 72, _startPosY + 12, 6, 7); + + //wheels + wheelDrawing.Draw(_startPosX, _startPosY, false, null, g2d); + } +} diff --git a/laba1Loco/DrawningObjectTrain.java b/laba1Loco/DrawningObjectTrain.java new file mode 100644 index 0000000..79c43ad --- /dev/null +++ b/laba1Loco/DrawningObjectTrain.java @@ -0,0 +1,22 @@ +package laba1Loco; + +public class DrawningObjectTrain implements IMoveableObject { + private DrawingTrain _drawingTrain = null; + public DrawningObjectTrain(DrawingTrain drawingTrain) + { + _drawingTrain = drawingTrain; + } + public ObjectParameters GetObjectPosition(){ + if (_drawingTrain == null || _drawingTrain.EntityTrain == + null) + { + return null; + } + return new ObjectParameters(_drawingTrain.GetPosX(), + _drawingTrain.GetPosY(), _drawingTrain.GetWidth(), _drawingTrain.GetHeight()); + } + public int GetStep(){ return (int)_drawingTrain.EntityTrain.Step; } + public boolean CheckCanMove(Direction direction) { return _drawingTrain.CanMove(direction);} + public void MoveObject(Direction direction) { _drawingTrain.MoveTransport(direction); } + +} diff --git a/laba1Loco/EntityLoco.java b/laba1Loco/EntityLoco.java index fa6f254..27d16d3 100644 --- a/laba1Loco/EntityLoco.java +++ b/laba1Loco/EntityLoco.java @@ -6,64 +6,35 @@ import javax.swing.*; import javax.swing.Timer; import java.awt.event.*; -public class EntityLoco{ +public class EntityLoco extends EntityTrain{ + public Color AdditionalColor; /// - /// Скорость - /// - public int Speed; - /// - /// Вес - /// - public double Weight; - /// - /// Основной цвет - /// - public Color BodyColor; - /// - /// Дополнительный цвет (для опциональных элементов) - /// - public Color AdditionalColor; - /// - /// Признак (опция) наличия трубы - /// - public boolean Tube; - /// - /// Признак (опция) наличия бака - /// - public boolean FuelTank; - /// - /// Признак (опция) наличия паровозной полосы - /// - public boolean LocoLine; - /// - /// Шаг перемещения автомобиля - /// - public double Step; - /// - /// количество колёс [2;4] - /// - public int numWheel; - /// - /// Инициализация полей объекта-класса спортивного автомобиля - /// - /// Скорость - /// Вес автомобиля - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия трубы - /// Признак наличия бака - /// Признак паровозной гоночной полосы - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, boolean tube, boolean fuelTank, boolean locoLine, int _numWheel) - { - numWheel = _numWheel; - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Tube = tube; - FuelTank = fuelTank; - LocoLine = locoLine; - Step = (double)Speed * 100 / Weight; - } + /// Признак (опция) наличия трубы + /// + public boolean Tube; + /// Признак (опция) наличия топливного бака + /// + public boolean FuelTank; + /// + /// Признак (опция) наличия паровозной полосы + /// + public boolean LocoLine; + /// + /// Инициализация полей объекта-класса Локомотива + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия трубы + /// Признак наличия бака + /// Признак наличия паровозной полосы + public EntityLoco(int speed, double weight, Color bodyColor, int _numWheel, Color additionalColor, boolean tube, boolean fuelTank, boolean locoLine) + { + super(speed, weight, bodyColor, _numWheel); + AdditionalColor = additionalColor; + Tube = tube; + FuelTank = fuelTank; + LocoLine = locoLine; + } } \ No newline at end of file diff --git a/laba1Loco/EntityTrain.java b/laba1Loco/EntityTrain.java index c884cec..a25d1d2 100644 --- a/laba1Loco/EntityTrain.java +++ b/laba1Loco/EntityTrain.java @@ -24,16 +24,21 @@ public class EntityTrain { /// public double Step; /// + /// количество колёс [2;4] + /// + public int numWheel; + /// /// Инициализация полей объекта-класса Локомотива /// /// Скорость /// Вес автомобиля /// Основной цвет - public EntityTrain(int speed, double weight, Color bodyColor) + public EntityTrain(int speed, double weight, Color bodyColor, int _numWheel) { Speed = speed; Weight = weight; BodyColor = bodyColor; + numWheel = _numWheel; Step = (double)Speed * 100 / Weight; } } diff --git a/laba1Loco/FormTrain.java b/laba1Loco/FormTrain.java index 1e9ad91..dbd54b1 100644 --- a/laba1Loco/FormTrain.java +++ b/laba1Loco/FormTrain.java @@ -7,8 +7,11 @@ import javax.swing.Timer; import java.awt.event.*; public class FormTrain{ - private DrawingLoco _drawingLoco; + private DrawingTrain _drawingTrain; + private AbstractStrategy abstractStrategy; Canvas canv; + static int pictureBoxWidth = 980; + static int pictureBoxHeight = 560; public void Draw(){ canv.repaint(); @@ -17,6 +20,14 @@ public class FormTrain{ public FormTrain(){ JFrame w=new JFrame ("Loco"); JButton buttonCreate = new JButton("create"); + JButton buttonCreateLocomotive = new JButton("createLocomotive"); + JButton buttonStrategysStep = new JButton("strategys step"); + JComboBox comboBoxStrategy = new JComboBox( + new String[]{ + "Элемент списка 1", + "Элемент списка 2", + "Элемент списка 3" + }); JButton up = new JButton(); up.setBorderPainted(false); up.setFocusPainted(false); @@ -41,20 +52,86 @@ public class FormTrain{ right.setContentAreaFilled(false); right.setName("right"); right.setIcon(new ImageIcon("D:\\Coffee\\PIbd-21_Kouvshinoff_T._A._WarmlyLocomotive._Harder\\laba1Loco\\images\\arowR340x259.png")); - + buttonStrategysStep.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (_drawingTrain == null) + { + return; + } + if (comboBoxStrategy.isEnabled()) + { + switch (comboBoxStrategy.getSelectedIndex()) + { + case 0: + abstractStrategy = new MoveToCenter(); + break; + case 1: + abstractStrategy = new MoveToBorder(); + break; + default: + abstractStrategy = null; + break; + }; + if (abstractStrategy == null) + { + return; + } + abstractStrategy.SetData(new + DrawningObjectTrain(_drawingTrain), pictureBoxWidth, + pictureBoxHeight); + comboBoxStrategy.setEnabled(false); + } + if (abstractStrategy == null) + { + return; + } + abstractStrategy.MakeStep(); + Draw(); + if (abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.setEnabled(true); + abstractStrategy = null; + } + } + } + ); + buttonCreateLocomotive.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + System.out.println(e.getActionCommand()); + Random random = new Random(); + _drawingTrain = new DrawingLoco( + random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + random.nextInt(2, 5), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + random.nextInt(0, 2) == 1, + random.nextInt(0, 2) == 1, + random.nextInt(0, 2) == 1, + pictureBoxWidth, + pictureBoxHeight); + _drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + canv._drawingTrain = _drawingTrain; + Draw(); + } + } + ); buttonCreate.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println(e.getActionCommand()); Random random = new Random(); - _drawingLoco = new DrawingLoco(); - _drawingLoco.Init(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.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1, - random.nextInt(2, 5), 1000, 560); - _drawingLoco.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); - canv._drawingLoco = _drawingLoco; + _drawingTrain = new DrawingTrain( + random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + random.nextInt(2, 5), + 1000, + 560); + _drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + canv._drawingTrain = _drawingTrain; Draw(); } } @@ -62,22 +139,22 @@ public class FormTrain{ ActionListener actioListener = new ActionListener() { public void actionPerformed(ActionEvent e){ System.out.println(((JButton)(e.getSource())).getName()); - if (_drawingLoco == null) + if (_drawingTrain == null) { return; } switch(((JButton)(e.getSource())).getName()){ case "up": - _drawingLoco.MoveTransport(Direction.Up); + _drawingTrain.MoveTransport(Direction.Up); break; case "down": - _drawingLoco.MoveTransport(Direction.Down); + _drawingTrain.MoveTransport(Direction.Down); break; case "left": - _drawingLoco.MoveTransport(Direction.Left); + _drawingTrain.MoveTransport(Direction.Left); break; case "right": - _drawingLoco.MoveTransport(Direction.Right); + _drawingTrain.MoveTransport(Direction.Right); break; } Draw(); @@ -92,33 +169,39 @@ public class FormTrain{ w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setLayout(null); canv = new Canvas(); - canv.setBounds(0, 0, 1000, 600); + canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); buttonCreate.setBounds(2, 540, 100, 20); + buttonCreateLocomotive.setBounds(105, 540, 150, 20); up.setBounds(900, 480, 40, 40); down.setBounds(900, 520, 40, 40); left.setBounds(860, 520, 40, 40); right.setBounds(940, 520, 40, 40); + comboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20); + buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20); w.add(canv); w.add(buttonCreate); + w.add(buttonCreateLocomotive); w.add(up); w.add(down); w.add(left); w.add(right); + w.add(comboBoxStrategy); + w.add(buttonStrategysStep); w.setVisible (true); } } class Canvas extends JComponent{ - public DrawingLoco _drawingLoco; + public DrawingTrain _drawingTrain; public Canvas(){ } public void paintComponent (Graphics g){ - if (_drawingLoco == null){ + if (_drawingTrain == null){ return; } super.paintComponents (g) ; Graphics2D g2d = (Graphics2D)g; - _drawingLoco.DrawTransport(g2d); + _drawingTrain.DrawTransport(g2d); super.repaint(); } } \ No newline at end of file diff --git a/laba1Loco/IMoveableObject.java b/laba1Loco/IMoveableObject.java new file mode 100644 index 0000000..db55062 --- /dev/null +++ b/laba1Loco/IMoveableObject.java @@ -0,0 +1,23 @@ +package laba1Loco; + +public interface IMoveableObject { + /// + /// Получение координаты X объекта + /// + ObjectParameters GetObjectPosition(); + /// + /// Шаг объекта + /// + int GetStep(); + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + boolean CheckCanMove(Direction direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(Direction direction); +} diff --git a/laba1Loco/MoveToBorder.java b/laba1Loco/MoveToBorder.java new file mode 100644 index 0000000..46cd829 --- /dev/null +++ b/laba1Loco/MoveToBorder.java @@ -0,0 +1,48 @@ +package laba1Loco; + +public class MoveToBorder extends AbstractStrategy{ + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.RightBorder() <= FieldWidth && + objParams.RightBorder() + GetStep() >= FieldWidth && + objParams.DownBorder() <= FieldHeight && + objParams.DownBorder() + GetStep() >= FieldHeight; + } + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder() - FieldWidth; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.DownBorder() - FieldHeight; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/laba1Loco/MoveToCenter.java b/laba1Loco/MoveToCenter.java new file mode 100644 index 0000000..9421036 --- /dev/null +++ b/laba1Loco/MoveToCenter.java @@ -0,0 +1,48 @@ +package laba1Loco; + +public class MoveToCenter extends AbstractStrategy { + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return + Math.abs(objParams.ObjectMiddleHorizontal() - FieldWidth / 2) <= GetStep() + && + Math.abs(objParams.ObjectMiddleVertical() - FieldHeight / 2) <= GetStep(); + } + 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/laba1Loco/ObjectParameters.java b/laba1Loco/ObjectParameters.java new file mode 100644 index 0000000..1be0fe9 --- /dev/null +++ b/laba1Loco/ObjectParameters.java @@ -0,0 +1,46 @@ +package laba1Loco; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private 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;} + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} -- 2.25.1 From ae7baff59703c8a01d057e9b15bb6d4b8f479476 Mon Sep 17 00:00:00 2001 From: Timourka Date: Sun, 8 Oct 2023 19:50:49 +0400 Subject: [PATCH 3/5] base --- laba1Loco/FormTrain.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/laba1Loco/FormTrain.java b/laba1Loco/FormTrain.java index dbd54b1..ec696a4 100644 --- a/laba1Loco/FormTrain.java +++ b/laba1Loco/FormTrain.java @@ -24,9 +24,8 @@ public class FormTrain{ JButton buttonStrategysStep = new JButton("strategys step"); JComboBox comboBoxStrategy = new JComboBox( new String[]{ - "Элемент списка 1", - "Элемент списка 2", - "Элемент списка 3" + "к центру", + "к краю", }); JButton up = new JButton(); up.setBorderPainted(false); -- 2.25.1 From ab1835aec79d0bbf5033b84df4cd208967196f51 Mon Sep 17 00:00:00 2001 From: Timourka Date: Mon, 9 Oct 2023 21:18:09 +0400 Subject: [PATCH 4/5] difficult --- laba1Loco/DrawingTrain.java | 20 ++++- laba1Loco/FormTrain.java | 6 +- laba1Loco/IWheelDrawing.java | 15 ++++ laba1Loco/WheelDrawingBalls.java | 80 +++++++++++++++++ laba1Loco/WheelDrawingDavidStar.java | 87 +++++++++++++++++++ ...elDrawing.java => WheelDrawingSimple.java} | 6 +- 6 files changed, 205 insertions(+), 9 deletions(-) create mode 100644 laba1Loco/IWheelDrawing.java create mode 100644 laba1Loco/WheelDrawingBalls.java create mode 100644 laba1Loco/WheelDrawingDavidStar.java rename laba1Loco/{WheelDrawing.java => WheelDrawingSimple.java} (95%) diff --git a/laba1Loco/DrawingTrain.java b/laba1Loco/DrawingTrain.java index abfbf12..6fbf441 100644 --- a/laba1Loco/DrawingTrain.java +++ b/laba1Loco/DrawingTrain.java @@ -7,7 +7,7 @@ import javax.swing.Timer; import java.awt.event.*; public class DrawingTrain { - protected WheelDrawing wheelDrawing; + protected IWheelDrawing wheelDrawing; /// /// Класс-сущность /// @@ -51,7 +51,21 @@ public class DrawingTrain { if (_pictureHeight < _locoHeight || _pictureWidth < _locoWidth) return; EntityTrain = new EntityTrain(speed, weight, bodyColor, _numWheel); - wheelDrawing = new WheelDrawing(); + Random random = new Random(); + switch(random.nextInt(0, 3)){ + case 0: + wheelDrawing = new WheelDrawingSimple(); + break; + case 1: + wheelDrawing = new WheelDrawingBalls(); + break; + case 2: + wheelDrawing = new WheelDrawingDavidStar(); + break; + default: + wheelDrawing = new WheelDrawingSimple(); + break; + } wheelDrawing.setNumWheel(_numWheel); } /// @@ -188,6 +202,6 @@ _startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30 g2d.drawRect(_startPosX + 72, _startPosY + 12, 6, 7); //wheels - wheelDrawing.Draw(_startPosX, _startPosY, false, null, g2d); + wheelDrawing.Draw(_startPosX, _startPosY, false, EntityTrain.BodyColor, g2d); } } diff --git a/laba1Loco/FormTrain.java b/laba1Loco/FormTrain.java index ec696a4..4612bbb 100644 --- a/laba1Loco/FormTrain.java +++ b/laba1Loco/FormTrain.java @@ -11,7 +11,7 @@ public class FormTrain{ private AbstractStrategy abstractStrategy; Canvas canv; static int pictureBoxWidth = 980; - static int pictureBoxHeight = 560; + static int pictureBoxHeight = 580; public void Draw(){ canv.repaint(); @@ -127,8 +127,8 @@ public class FormTrain{ random.nextInt(1000, 3000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), random.nextInt(2, 5), - 1000, - 560); + pictureBoxWidth, + pictureBoxHeight); _drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); canv._drawingTrain = _drawingTrain; Draw(); diff --git a/laba1Loco/IWheelDrawing.java b/laba1Loco/IWheelDrawing.java new file mode 100644 index 0000000..fe986f1 --- /dev/null +++ b/laba1Loco/IWheelDrawing.java @@ -0,0 +1,15 @@ +package laba1Loco; + +import java.awt.*; +import java.util.*; +import javax.swing.*; +import javax.swing.Timer; +import java.awt.event.*; + +public interface IWheelDrawing{ + public NumWheel getNumWheel(); + + public void setNumWheel(int kwheel); + + public void Draw(int _startPosX, int _startPosY, boolean fuelTank, Color color, Graphics2D g2d); +} \ No newline at end of file diff --git a/laba1Loco/WheelDrawingBalls.java b/laba1Loco/WheelDrawingBalls.java new file mode 100644 index 0000000..c61a78a --- /dev/null +++ b/laba1Loco/WheelDrawingBalls.java @@ -0,0 +1,80 @@ +package laba1Loco; + +import java.awt.*; +import java.util.*; +import javax.swing.*; +import javax.swing.Timer; +import java.awt.event.*; + +public class WheelDrawingBalls implements IWheelDrawing{ + private NumWheel numWheel; + + public NumWheel getNumWheel() { + return numWheel; + } + + public void setNumWheel(int kwheel){ + switch(kwheel){ + case 2: + numWheel = NumWheel.TwoWheel; + break; + case 3: + numWheel = NumWheel.ThreeWheel; + break; + case 4: + numWheel = NumWheel.FourWheel; + break; + default: + numWheel = NumWheel.TwoWheel; + System.out.println("ВСё плохо, колво колёс почему то не соответтвовало критериям, количество колёс:" + Integer.toString(kwheel) + "но вывели мы как будто их было 2"); + break; + } + } + + private void DrawWheel(int _startPosX, int _startPosY, Color color, Graphics2D g2d){ + g2d.setColor(color); + g2d.fillOval(_startPosX, _startPosY, 8, 8); + + g2d.setColor(Color.PINK); + g2d.fillOval( _startPosX+3, _startPosY+1, 2, 3); + g2d.fillOval( _startPosX+1, _startPosY+4, 2, 2); + g2d.fillOval( _startPosX+6, _startPosY+4, 2, 2); + } + + public void Draw(int _startPosX, int _startPosY, boolean fuelTank, Color color, Graphics2D g2d){ + + DrawWheel(_startPosX + 3, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 26, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 46, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 72, _startPosY + 34, color, g2d); + + if (numWheel == NumWheel.TwoWheel){ + DrawWheel(_startPosX + 14, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 59, _startPosY + 34, color, g2d); + } + if (numWheel == NumWheel.FourWheel){ + DrawWheel(_startPosX + 11, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 18, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 55, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 63, _startPosY + 34, color, g2d); + } + + if (fuelTank){ + DrawWheel(_startPosX + 3+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 26+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 46+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 72+85, _startPosY + 34, color, g2d); + + if (numWheel == NumWheel.TwoWheel){ + DrawWheel(_startPosX + 14+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 59+85, _startPosY + 34, color, g2d); + } + if (numWheel == NumWheel.FourWheel){ + DrawWheel(_startPosX + 11+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 18+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 55+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 63+85, _startPosY + 34, color, g2d); + } + } + } + } diff --git a/laba1Loco/WheelDrawingDavidStar.java b/laba1Loco/WheelDrawingDavidStar.java new file mode 100644 index 0000000..ab43f3b --- /dev/null +++ b/laba1Loco/WheelDrawingDavidStar.java @@ -0,0 +1,87 @@ +package laba1Loco; + +import java.awt.*; +import java.util.*; +import javax.swing.*; +import javax.swing.Timer; +import java.awt.event.*; + +public class WheelDrawingDavidStar implements IWheelDrawing{ + private NumWheel numWheel; + + public NumWheel getNumWheel() { + return numWheel; + } + + public void setNumWheel(int kwheel){ + switch(kwheel){ + case 2: + numWheel = NumWheel.TwoWheel; + break; + case 3: + numWheel = NumWheel.ThreeWheel; + break; + case 4: + numWheel = NumWheel.FourWheel; + break; + default: + numWheel = NumWheel.TwoWheel; + System.out.println("ВСё плохо, колво колёс почему то не соответтвовало критериям, количество колёс:" + Integer.toString(kwheel) + "но вывели мы как будто их было 2"); + break; + } + } + + private void DrawWheel(int _startPosX, int _startPosY, Color color, Graphics2D g2d){ + g2d.setColor(color); + g2d.fillOval(_startPosX, _startPosY, 8, 8); + + g2d.setColor(Color.WHITE); + g2d.drawPolygon(new int[]{ + _startPosX + 1, _startPosX + 7, _startPosX + 3 + }, new int[]{ + _startPosY + 2, _startPosY + 2, _startPosY + 7 + }, 3); + g2d.drawPolygon(new int[]{ + _startPosX + 1, _startPosX + 7, _startPosX + 4 + }, new int[]{ + _startPosY + 6, _startPosY + 6, _startPosY + 1 + }, 3); + } + + public void Draw(int _startPosX, int _startPosY, boolean fuelTank, Color color, Graphics2D g2d){ + + DrawWheel(_startPosX + 3, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 26, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 46, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 72, _startPosY + 34, color, g2d); + + if (numWheel == NumWheel.TwoWheel){ + DrawWheel(_startPosX + 14, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 59, _startPosY + 34, color, g2d); + } + if (numWheel == NumWheel.FourWheel){ + DrawWheel(_startPosX + 11, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 18, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 55, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 63, _startPosY + 34, color, g2d); + } + + if (fuelTank){ + DrawWheel(_startPosX + 3+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 26+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 46+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 72+85, _startPosY + 34, color, g2d); + + if (numWheel == NumWheel.TwoWheel){ + DrawWheel(_startPosX + 14+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 59+85, _startPosY + 34, color, g2d); + } + if (numWheel == NumWheel.FourWheel){ + DrawWheel(_startPosX + 11+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 18+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 55+85, _startPosY + 34, color, g2d); + DrawWheel(_startPosX + 63+85, _startPosY + 34, color, g2d); + } + } + } + } diff --git a/laba1Loco/WheelDrawing.java b/laba1Loco/WheelDrawingSimple.java similarity index 95% rename from laba1Loco/WheelDrawing.java rename to laba1Loco/WheelDrawingSimple.java index 33935fb..50077e7 100644 --- a/laba1Loco/WheelDrawing.java +++ b/laba1Loco/WheelDrawingSimple.java @@ -6,10 +6,10 @@ import javax.swing.*; import javax.swing.Timer; import java.awt.event.*; -public class WheelDrawing{ +public class WheelDrawingSimple implements IWheelDrawing{ private NumWheel numWheel; - public NumWheel getSomeProperty() { + public NumWheel getNumWheel() { return numWheel; } @@ -31,7 +31,7 @@ public class WheelDrawing{ } } - void Draw(int _startPosX, int _startPosY, boolean fuelTank, Color color, Graphics2D g2d){ + public void Draw(int _startPosX, int _startPosY, boolean fuelTank, Color color, Graphics2D g2d){ g2d.setColor(color); -- 2.25.1 From 9abbe9bdc0b04219347ac67732a98d112fc5d584 Mon Sep 17 00:00:00 2001 From: Timourka Date: Mon, 9 Oct 2023 21:35:00 +0400 Subject: [PATCH 5/5] difficult --- laba1Loco/DrawingLoco.java | 6 +++--- laba1Loco/WheelDrawingSimple.java | 20 -------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/laba1Loco/DrawingLoco.java b/laba1Loco/DrawingLoco.java index 23d62bb..4b9aea3 100644 --- a/laba1Loco/DrawingLoco.java +++ b/laba1Loco/DrawingLoco.java @@ -50,9 +50,6 @@ public class DrawingLoco extends DrawingTrain{ super.DrawTransport(g2d); - //wheels - wheelDrawing.Draw(_startPosX, _startPosY, ((EntityLoco)EntityTrain).FuelTank, EntityTrain.BodyColor, g2d); - g2d.setColor(((EntityLoco)EntityTrain).AdditionalColor); if (((EntityLoco)EntityTrain).Tube) { @@ -123,5 +120,8 @@ _startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30 }, 4); } + + //wheels + wheelDrawing.Draw(_startPosX, _startPosY, ((EntityLoco)EntityTrain).FuelTank, EntityTrain.BodyColor, g2d); } } \ No newline at end of file diff --git a/laba1Loco/WheelDrawingSimple.java b/laba1Loco/WheelDrawingSimple.java index 50077e7..2392750 100644 --- a/laba1Loco/WheelDrawingSimple.java +++ b/laba1Loco/WheelDrawingSimple.java @@ -36,54 +36,34 @@ public class WheelDrawingSimple implements IWheelDrawing{ g2d.setColor(color); g2d.fillOval(_startPosX + 3, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 4, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 26, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 27, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 46, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 47, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 72, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 73, _startPosY + 35, 6, 6); if (numWheel == NumWheel.TwoWheel){ g2d.fillOval(_startPosX + 14, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 15, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 59, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 60, _startPosY + 35, 6, 6); } if (numWheel == NumWheel.FourWheel){ g2d.fillOval(_startPosX + 11, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 12, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 18, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 19, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 55, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 56, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 63, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 64, _startPosY + 35, 6, 6); } if (fuelTank){ g2d.fillOval(_startPosX + 3+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 4+85, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 26+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 27+85, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 46+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 47+85, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 72+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 73+85, _startPosY + 35, 6, 6); if (numWheel == NumWheel.TwoWheel){ g2d.fillOval(_startPosX + 14+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 15+85, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 59+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 60+85, _startPosY + 35, 6, 6); } if (numWheel == NumWheel.FourWheel){ g2d.fillOval(_startPosX + 11+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 12+85, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 18+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 19+85, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 55+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 56+85, _startPosY + 35, 6, 6); g2d.fillOval(_startPosX + 63+85, _startPosY + 34, 8, 8); - g2d.fillOval(_startPosX + 64+85, _startPosY + 35, 6, 6); } } } -- 2.25.1