Compare commits
2 Commits
3b693a35eb
...
cc59420fbf
Author | SHA1 | Date | |
---|---|---|---|
cc59420fbf | |||
352e557d37 |
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -1,58 +0,0 @@
|
|||||||
package ProjectElectricLocomotive.src;
|
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
|
|
||||||
public class AdditionalClass {
|
|
||||||
private AdditionalType _additionalType;
|
|
||||||
|
|
||||||
public AdditionalType getAdditionalType()
|
|
||||||
{
|
|
||||||
return _additionalType;
|
|
||||||
}
|
|
||||||
public void setAdditonalType(int value)
|
|
||||||
{
|
|
||||||
switch (value)
|
|
||||||
{
|
|
||||||
case 1:
|
|
||||||
_additionalType = AdditionalType.ONE;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
_additionalType = AdditionalType.TWO;
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
_additionalType = AdditionalType.THREE;
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
_additionalType = AdditionalType.FOURTH;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
_additionalType = AdditionalType.ONE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void drawingWheels(Graphics g, Double startPosX, Double startPosY, Color wheelsColor)
|
|
||||||
{
|
|
||||||
Graphics2D g2d = (Graphics2D) g;
|
|
||||||
|
|
||||||
|
|
||||||
g.setColor(wheelsColor);
|
|
||||||
|
|
||||||
switch (_additionalType)
|
|
||||||
{
|
|
||||||
case FOURTH:
|
|
||||||
g.fillOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 10, 10);
|
|
||||||
g.drawOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 8, 8);
|
|
||||||
case THREE:
|
|
||||||
g.fillOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 10, 10);
|
|
||||||
g.drawOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 8, 8);
|
|
||||||
case TWO:
|
|
||||||
g.fillOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 10, 10);
|
|
||||||
g.drawOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 8, 8);
|
|
||||||
case ONE:
|
|
||||||
g.fillOval(startPosX.intValue() + 9, startPosY.intValue() + 37, 10, 10);
|
|
||||||
g.drawOval(startPosX.intValue() + 9, startPosY.intValue() + 37, 8, 8);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
package ProjectElectricLocomotive.src;
|
|
||||||
public enum AdditionalType {
|
|
||||||
ONE,
|
|
||||||
TWO,
|
|
||||||
THREE,
|
|
||||||
FOURTH;
|
|
||||||
}
|
|
19
ProjectElectricLocomotive/src/CountWheels.java
Normal file
19
ProjectElectricLocomotive/src/CountWheels.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package ProjectElectricLocomotive.src;
|
||||||
|
|
||||||
|
public enum CountWheels
|
||||||
|
{
|
||||||
|
One(1),
|
||||||
|
Two(2),
|
||||||
|
Three(3),
|
||||||
|
Fourth(4);
|
||||||
|
private final int Value;
|
||||||
|
|
||||||
|
CountWheels(int value) {
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetCountWheels() {
|
||||||
|
return Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,19 @@
|
|||||||
package ProjectElectricLocomotive.src;
|
package ProjectElectricLocomotive.src;
|
||||||
|
|
||||||
public enum DirectionType {
|
public enum DirectionType
|
||||||
Up,
|
{
|
||||||
|
Unknown(-1),
|
||||||
|
Up(1),
|
||||||
|
Down(2),
|
||||||
|
Left(3),
|
||||||
|
Right(4);
|
||||||
|
private final int Value;
|
||||||
|
|
||||||
Down,
|
DirectionType(int value) {
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
Left,
|
public int getValue() {
|
||||||
|
return Value;
|
||||||
Right,
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,233 +0,0 @@
|
|||||||
package ProjectElectricLocomotive.src;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
|
|
||||||
public class DrawningElectricLocomotive {
|
|
||||||
|
|
||||||
public EntityElectricLocomotive EntityElectricLocomotive;
|
|
||||||
|
|
||||||
private Double pictureWidth;
|
|
||||||
private Double pictureHeight;
|
|
||||||
private Double startPosX;
|
|
||||||
private Double startPosY;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public AdditionalClass _additionalClass;
|
|
||||||
|
|
||||||
private final int drawningElectricLocomotiveWidth = 83;
|
|
||||||
private final int drawningElectricLocomotiveHeight = 45;
|
|
||||||
|
|
||||||
public void init(int speed, double weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement, int wheelsCount) {
|
|
||||||
EntityElectricLocomotive = new EntityElectricLocomotive();
|
|
||||||
_additionalClass = new AdditionalClass();
|
|
||||||
_additionalClass.setAdditonalType(wheelsCount);
|
|
||||||
EntityElectricLocomotive.init(speed, weight, bodyColor, additionalColor, electricHorns, batteryPlacement);
|
|
||||||
pictureWidth = null;
|
|
||||||
pictureHeight = null;
|
|
||||||
startPosX = null;
|
|
||||||
startPosY = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean setPictureSize(int width, int height) {
|
|
||||||
if (drawningElectricLocomotiveHeight > height || drawningElectricLocomotiveWidth > width) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
pictureWidth = (double)width;
|
|
||||||
pictureHeight = (double)height;
|
|
||||||
if (startPosY != null && startPosY + drawningElectricLocomotiveHeight < pictureHeight) {
|
|
||||||
startPosY = pictureHeight - drawningElectricLocomotiveHeight;
|
|
||||||
}
|
|
||||||
if (startPosX != null && startPosX + drawningElectricLocomotiveWidth < pictureWidth) {
|
|
||||||
startPosX = pictureWidth - drawningElectricLocomotiveWidth;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPosition(int x, int y) {
|
|
||||||
if (pictureHeight == null || pictureWidth == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
startPosX = (double)x;
|
|
||||||
startPosY = (double)y;
|
|
||||||
|
|
||||||
if (drawningElectricLocomotiveHeight + y > pictureHeight) {
|
|
||||||
startPosY = pictureHeight - drawningElectricLocomotiveHeight;
|
|
||||||
}
|
|
||||||
if (drawningElectricLocomotiveWidth + x > pictureWidth) {
|
|
||||||
startPosX = pictureWidth - drawningElectricLocomotiveWidth;
|
|
||||||
}
|
|
||||||
if (x < 0) {
|
|
||||||
startPosX = (double)0;
|
|
||||||
}
|
|
||||||
if (y < 0) {
|
|
||||||
startPosY = (double)0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean moveTransport(DirectionType direction) {
|
|
||||||
if (EntityElectricLocomotive == null || startPosX == null || startPosY == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (direction) {
|
|
||||||
case Left:
|
|
||||||
if (startPosX - EntityElectricLocomotive.getStep() > 0) {
|
|
||||||
startPosX -= EntityElectricLocomotive.getStep();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
case Up:
|
|
||||||
if (startPosY - EntityElectricLocomotive.getStep() > 0) {
|
|
||||||
startPosY -= EntityElectricLocomotive.getStep();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
case Right:
|
|
||||||
if (startPosX + drawningElectricLocomotiveWidth + EntityElectricLocomotive.getStep() < pictureWidth) {
|
|
||||||
startPosX += EntityElectricLocomotive.getStep();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
case Down:
|
|
||||||
if (startPosY + drawningElectricLocomotiveHeight + EntityElectricLocomotive.getStep() < pictureHeight) {
|
|
||||||
startPosY += EntityElectricLocomotive.getStep();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Image getImage() {
|
|
||||||
// Создаем новое изображение с заданными размерами
|
|
||||||
BufferedImage image = new BufferedImage(pictureWidth.intValue(), pictureHeight.intValue(), BufferedImage.TYPE_INT_ARGB);
|
|
||||||
Graphics2D g2d = image.createGraphics();
|
|
||||||
|
|
||||||
// Вызываем метод прорисовки нашего объекта DrawningElectricLocomotive
|
|
||||||
drawTransport(g2d);
|
|
||||||
|
|
||||||
_additionalClass.drawingWheels(g2d, startPosX, startPosY, EntityElectricLocomotive.getAdditionalColor());
|
|
||||||
|
|
||||||
// Освобождаем ресурсы графического контекста
|
|
||||||
g2d.dispose();
|
|
||||||
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void drawTransport(Graphics g) {
|
|
||||||
if (EntityElectricLocomotive == null || startPosX == null || startPosY == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Создание перьев и кистей для прорисовки электровоза
|
|
||||||
Color blackColor = Color.BLACK;
|
|
||||||
Color deepSkyBlueColor = new Color(0, 191, 255);
|
|
||||||
Color yellowColor = Color.YELLOW;
|
|
||||||
|
|
||||||
Graphics2D g2d = (Graphics2D) g;
|
|
||||||
|
|
||||||
// Инициализация опорных точек для прорисовки корпуса
|
|
||||||
int pointStartX = startPosX.intValue() + 75;
|
|
||||||
int pointStartY = startPosY.intValue() + 20;
|
|
||||||
|
|
||||||
Point pointStart = new Point(pointStartX, pointStartY);
|
|
||||||
Point point1 = new Point(pointStartX, pointStartY - 15);
|
|
||||||
Point point2 = new Point(startPosX.intValue() + 10, pointStartY - 15);
|
|
||||||
Point pointFinish = new Point(startPosX.intValue() + 5, pointStartY);
|
|
||||||
|
|
||||||
// Инициализация опорных точек для прорисовки первой "юбки"
|
|
||||||
Point point3 = new Point(startPosX.intValue() + 7, startPosY.intValue() + 37);
|
|
||||||
Point point4 = new Point(startPosX.intValue(), startPosY.intValue() + 43);
|
|
||||||
Point point5 = new Point(startPosX.intValue() + 7, startPosY.intValue() + 43);
|
|
||||||
|
|
||||||
// Инициализация опорных точек для прорисовки второй "юбки"
|
|
||||||
Point point6 = new Point(startPosX.intValue() + 69, startPosY.intValue() + 37);
|
|
||||||
Point point7 = new Point(startPosX.intValue() + 82, startPosY.intValue() + 43);
|
|
||||||
Point point8 = new Point(startPosX.intValue() + 69, startPosY.intValue() + 43);
|
|
||||||
|
|
||||||
// Совокупность точек полигона уголка первой "юбки"
|
|
||||||
Point[] firstTrianglePoints = { point3, point4, point5 };
|
|
||||||
|
|
||||||
// Совокупность точек полигона уголка второй "юбки"
|
|
||||||
Point[] secondTrianglePoints = { point6, point7, point8 };
|
|
||||||
|
|
||||||
// Прорисовка уголков "юбок"
|
|
||||||
g.setColor(blackColor);
|
|
||||||
g.fillPolygon(new Polygon(new int[]{ point3.x, point4.x, point5.x }, new int[]{ point3.y, point4.y, point5.y }, 3));
|
|
||||||
g.fillPolygon(new Polygon(new int[]{ point6.x, point7.x, point8.x }, new int[]{ point6.y, point7.y, point8.y }, 3));
|
|
||||||
|
|
||||||
// Прорисовка корпуса
|
|
||||||
g.drawRect(startPosX.intValue() + 5, startPosY.intValue() + 20, 70, 17);
|
|
||||||
g.drawLine(point3.x, point3.y, point4.x, point4.y);
|
|
||||||
g.drawLine(pointStart.x, pointStart.y, point1.x, point1.y);
|
|
||||||
g.drawLine(point1.x, point1.y, point2.x, point2.y);
|
|
||||||
g.drawLine(point2.x, point2.y, pointFinish.x, pointFinish.y);
|
|
||||||
|
|
||||||
// Прорисовка окон
|
|
||||||
g.setColor(deepSkyBlueColor);
|
|
||||||
g.drawRect(startPosX.intValue() + 12, startPosY.intValue() + 9, 8, 8);
|
|
||||||
g.drawRect(startPosX.intValue() + 63, startPosY.intValue() + 9, 8, 8);
|
|
||||||
|
|
||||||
// Прорисовка двух передних колёс
|
|
||||||
|
|
||||||
//g.fillOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 10, 10);
|
|
||||||
//g.setColor(blackColor);
|
|
||||||
|
|
||||||
//g.drawOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 8, 8);
|
|
||||||
|
|
||||||
// Прорисовка двух задних колёс
|
|
||||||
//g.setColor(Color.YELLOW);
|
|
||||||
//g.fillOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 10, 10);
|
|
||||||
//g.fillOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 10, 10);
|
|
||||||
g.setColor(blackColor);
|
|
||||||
//g.drawOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 8, 8);
|
|
||||||
//g.drawOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 8, 8);
|
|
||||||
|
|
||||||
// Прорисовка заднего "шлюза"
|
|
||||||
g.fillRect(startPosX.intValue() + 75, startPosY.intValue() + 9, 6, 27);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (EntityElectricLocomotive.isElectricHorns()) {
|
|
||||||
// Инициализация опорных точек для прорисовки "рогов"
|
|
||||||
Point pointHorns1 = new Point(startPosX.intValue() + 13, startPosY.intValue() + 5);
|
|
||||||
Point pointHorns2 = new Point(startPosX.intValue() + 16, startPosY.intValue() + 2);
|
|
||||||
Point pointHorns3 = new Point(startPosX.intValue() + 11, startPosY.intValue());
|
|
||||||
|
|
||||||
g.setColor(EntityElectricLocomotive.getAdditionalColor());
|
|
||||||
|
|
||||||
// Прорисовка "рогов" электровоза
|
|
||||||
g.drawLine(pointHorns1.x, pointHorns1.y, pointHorns2.x, pointHorns2.y);
|
|
||||||
g.drawLine(pointHorns2.x, pointHorns2.y, pointHorns3.x, pointHorns3.y);
|
|
||||||
|
|
||||||
g.setColor(blackColor);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (EntityElectricLocomotive.isBatteryPlacement()) {
|
|
||||||
// Инициализация опорных точек для прорисовки молнии на хранилище батарей
|
|
||||||
Point pointLightning1 = new Point(startPosX.intValue() + 39, startPosY.intValue() + 37);
|
|
||||||
Point pointLightning2 = new Point(startPosX.intValue() + 37, startPosY.intValue() + 39);
|
|
||||||
Point pointLightning3 = new Point(startPosX.intValue() + 39, startPosY.intValue() + 40);
|
|
||||||
Point pointLightning4 = new Point(startPosX.intValue() + 37, startPosY.intValue() + 41);
|
|
||||||
|
|
||||||
g.setColor(EntityElectricLocomotive.getAdditionalColor());
|
|
||||||
|
|
||||||
// Прорисовка "хранилища батарей" электровоза
|
|
||||||
|
|
||||||
g.fillRect(startPosX.intValue() + 36, startPosY.intValue() + 37, 8, 4);
|
|
||||||
g.setColor(yellowColor);
|
|
||||||
g.drawLine(pointLightning1.x, pointLightning1.y, pointLightning2.x, pointLightning2.y);
|
|
||||||
g.drawLine(pointLightning2.x, pointLightning2.y, pointLightning3.x, pointLightning3.y);
|
|
||||||
g.drawLine(pointLightning3.x, pointLightning3.y, pointLightning4.x, pointLightning4.y);
|
|
||||||
|
|
||||||
g.setColor(blackColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,89 @@
|
|||||||
|
package ProjectElectricLocomotive.src.Drawnings;
|
||||||
|
|
||||||
|
import ProjectElectricLocomotive.src.Entities.EntityElectricLocomotive;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class DrawingElectricLocomotive extends DrawingLocomotive
|
||||||
|
{
|
||||||
|
|
||||||
|
public IDrawingOrnament _ornament;
|
||||||
|
|
||||||
|
public DrawingElectricLocomotive(int speed, float weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement)
|
||||||
|
{
|
||||||
|
super(83,45);
|
||||||
|
_entityLocomotive = new EntityElectricLocomotive(speed, weight, bodyColor, additionalColor, electricHorns, batteryPlacement);
|
||||||
|
Random rnd = new Random();
|
||||||
|
int varOrnament = rnd.nextInt(3);
|
||||||
|
|
||||||
|
switch (varOrnament)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
_ornament = new DrawingOrnamentWheels();
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
_ornament = new DrawningSecondOrnamentWheels();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
_ornament = new DrawingWheels();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ornament.SetCountWheels(rnd.nextInt(1,5));
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void DrawTransport(Graphics g)
|
||||||
|
{
|
||||||
|
if (!(_entityLocomotive instanceof EntityElectricLocomotive _entityElectricLocomotive) || _startPosX == null) return;
|
||||||
|
|
||||||
|
super.DrawTransport(g);
|
||||||
|
|
||||||
|
g.setColor(Color.white);
|
||||||
|
g.fillRect(_startPosX + 48, _startPosY + 38, 20, 10);
|
||||||
|
g.fillRect(_startPosX + 9, _startPosY + 38, 20, 10);
|
||||||
|
|
||||||
|
// Создание перьев и кистей для прорисовки электровоза
|
||||||
|
Color blackColor = Color.BLACK;
|
||||||
|
Color deepSkyBlueColor = new Color(0, 191, 255);
|
||||||
|
Color yellowColor = Color.YELLOW;
|
||||||
|
|
||||||
|
if (_entityElectricLocomotive.GetElectricHorns()) {
|
||||||
|
// Инициализация опорных точек для прорисовки "рогов"
|
||||||
|
Point pointHorns1 = new Point(_startPosX + 13, _startPosY + 5);
|
||||||
|
Point pointHorns2 = new Point(_startPosX + 16, _startPosY + 2);
|
||||||
|
Point pointHorns3 = new Point(_startPosX + 11, _startPosY);
|
||||||
|
|
||||||
|
g.setColor(_entityElectricLocomotive.GetAdditionalColor());
|
||||||
|
|
||||||
|
// Прорисовка "рогов" электровоза
|
||||||
|
g.drawLine(pointHorns1.x, pointHorns1.y, pointHorns2.x, pointHorns2.y);
|
||||||
|
g.drawLine(pointHorns2.x, pointHorns2.y, pointHorns3.x, pointHorns3.y);
|
||||||
|
|
||||||
|
g.setColor(blackColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_entityElectricLocomotive.GetBatteryPlacement()) {
|
||||||
|
// Инициализация опорных точек для прорисовки молнии на хранилище батарей
|
||||||
|
Point pointLightning1 = new Point(_startPosX + 39, _startPosY + 37);
|
||||||
|
Point pointLightning2 = new Point(_startPosX + 37, _startPosY + 39);
|
||||||
|
Point pointLightning3 = new Point(_startPosX + 39, _startPosY + 40);
|
||||||
|
Point pointLightning4 = new Point(_startPosX + 37, _startPosY + 41);
|
||||||
|
|
||||||
|
g.setColor(_entityElectricLocomotive.GetAdditionalColor());
|
||||||
|
|
||||||
|
// Прорисовка "хранилища батарей" электровоза
|
||||||
|
|
||||||
|
|
||||||
|
g.fillRect(_startPosX.intValue() + 36, _startPosY + 37, 8, 4);
|
||||||
|
g.setColor(yellowColor);
|
||||||
|
g.drawLine(pointLightning1.x, pointLightning1.y, pointLightning2.x, pointLightning2.y);
|
||||||
|
g.drawLine(pointLightning2.x, pointLightning2.y, pointLightning3.x, pointLightning3.y);
|
||||||
|
g.drawLine(pointLightning3.x, pointLightning3.y, pointLightning4.x, pointLightning4.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
_ornament.DrawWheels(g, _startPosX, _startPosY);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
111
ProjectElectricLocomotive/src/Drawnings/DrawingField.java
Normal file
111
ProjectElectricLocomotive/src/Drawnings/DrawingField.java
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
package ProjectElectricLocomotive.src.Drawnings;
|
||||||
|
|
||||||
|
|
||||||
|
import ProjectElectricLocomotive.src.DirectionType;
|
||||||
|
import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive;
|
||||||
|
import ProjectElectricLocomotive.src.FormElectricLocomotive;
|
||||||
|
import ProjectElectricLocomotive.src.MovementStrategy.*;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class DrawingField extends JPanel {
|
||||||
|
private final FormElectricLocomotive field;
|
||||||
|
DrawingLocomotive _drawingLocomotive;
|
||||||
|
private AbstractStrategy _strategy = null;
|
||||||
|
public DrawingField(FormElectricLocomotive field) {
|
||||||
|
this.field = field;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g)
|
||||||
|
{
|
||||||
|
if (_drawingLocomotive == null) return;
|
||||||
|
_drawingLocomotive.DrawTransport(g);
|
||||||
|
}
|
||||||
|
public void UpButtonAction()
|
||||||
|
{
|
||||||
|
if (_drawingLocomotive == null) return;
|
||||||
|
_drawingLocomotive.MoveTransport(DirectionType.Up);
|
||||||
|
}
|
||||||
|
public void DownButtonAction()
|
||||||
|
{
|
||||||
|
if (_drawingLocomotive == null) return;
|
||||||
|
_drawingLocomotive.MoveTransport(DirectionType.Down);
|
||||||
|
}
|
||||||
|
public void RightButtonAction()
|
||||||
|
{
|
||||||
|
if (_drawingLocomotive == null) return;
|
||||||
|
_drawingLocomotive.MoveTransport(DirectionType.Right);
|
||||||
|
}
|
||||||
|
public void LeftButtonAction()
|
||||||
|
{
|
||||||
|
if (_drawingLocomotive == null) return;
|
||||||
|
_drawingLocomotive.MoveTransport(DirectionType.Left);
|
||||||
|
}
|
||||||
|
public void CreateObject(String type){
|
||||||
|
Random rnd = new Random();
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case "DrawningLocomotive":
|
||||||
|
_drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300),
|
||||||
|
rnd.nextInt(1000, 3000),
|
||||||
|
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
||||||
|
break;
|
||||||
|
case "DrawningElectricLocomotive":
|
||||||
|
_drawingLocomotive = new DrawingElectricLocomotive(rnd.nextInt(100, 300),
|
||||||
|
rnd.nextInt(1000, 3000),
|
||||||
|
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||||
|
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||||
|
rnd.nextBoolean(),
|
||||||
|
rnd.nextBoolean());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_drawingLocomotive.SetPictureSize(getWidth(),getHeight());
|
||||||
|
_drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CreateButtonElectricLocomotiveAction(){
|
||||||
|
CreateObject("DrawningElectricLocomotive");
|
||||||
|
}
|
||||||
|
public void CreateButtonLocomotiveAction(){
|
||||||
|
CreateObject("DrawningLocomotive");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ButtonStrategyStepAction()
|
||||||
|
{
|
||||||
|
if(_drawingLocomotive == null) return;
|
||||||
|
if (field.comboBoxStrategy.isEnabled())
|
||||||
|
{
|
||||||
|
int selectedIndex = field.comboBoxStrategy.getSelectedIndex();
|
||||||
|
switch (selectedIndex) {
|
||||||
|
case 0:
|
||||||
|
_strategy = new MoveToCenter();
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
_strategy = new MoveToBorder();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
_strategy = null;
|
||||||
|
}
|
||||||
|
if(_strategy == null) return;
|
||||||
|
_strategy.SetData(new MoveableLocomotive(_drawingLocomotive), getWidth(), getHeight());
|
||||||
|
}
|
||||||
|
if (_strategy == null) return;
|
||||||
|
field.comboBoxStrategy.setEnabled(false);
|
||||||
|
_strategy.MakeStep();
|
||||||
|
|
||||||
|
if(_strategy.getStatus() == StrategyStatus.Finish)
|
||||||
|
{
|
||||||
|
field.comboBoxStrategy.setEnabled(true);
|
||||||
|
_strategy = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ResizeField(){
|
||||||
|
if (_drawingLocomotive == null) return;
|
||||||
|
_drawingLocomotive.SetPictureSize(getWidth(),getHeight());
|
||||||
|
}
|
||||||
|
}
|
177
ProjectElectricLocomotive/src/Drawnings/DrawingLocomotive.java
Normal file
177
ProjectElectricLocomotive/src/Drawnings/DrawingLocomotive.java
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
package ProjectElectricLocomotive.src.Drawnings;
|
||||||
|
|
||||||
|
|
||||||
|
import ProjectElectricLocomotive.src.DirectionType;
|
||||||
|
|
||||||
|
import ProjectElectricLocomotive.src.Entities.EntityLocomotive;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class DrawingLocomotive {
|
||||||
|
public EntityLocomotive _entityLocomotive;
|
||||||
|
public EntityLocomotive GetEntityLocomotive()
|
||||||
|
{
|
||||||
|
return _entityLocomotive;
|
||||||
|
}
|
||||||
|
protected Integer _startPosX = null;
|
||||||
|
public Integer getPosX() {
|
||||||
|
return _startPosX;
|
||||||
|
}
|
||||||
|
protected Integer _startPosY = null;
|
||||||
|
public Integer getPosY() {
|
||||||
|
return _startPosY;
|
||||||
|
}
|
||||||
|
private Integer _pictureWidth = null;
|
||||||
|
public int getWidth() {
|
||||||
|
return _drawingLocomotiveWidth;
|
||||||
|
}
|
||||||
|
private Integer _pictureHeight = null;
|
||||||
|
public int getHeight() {
|
||||||
|
return _drawingLocomotiveHeight;
|
||||||
|
}
|
||||||
|
private int _drawingLocomotiveWidth = 83;
|
||||||
|
private int _drawingLocomotiveHeight = 45;
|
||||||
|
|
||||||
|
public DrawingLocomotive(int speed, float weight, Color bodyColor)
|
||||||
|
{
|
||||||
|
this._entityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DrawingLocomotive()
|
||||||
|
{
|
||||||
|
_pictureWidth = null;
|
||||||
|
_pictureHeight = null;
|
||||||
|
_startPosX = null;
|
||||||
|
_startPosY = null;
|
||||||
|
}
|
||||||
|
protected DrawingLocomotive(int drawningLocomotiveWidth, int drawningLocomotiveHeight)
|
||||||
|
{
|
||||||
|
this._drawingLocomotiveWidth = drawningLocomotiveWidth;
|
||||||
|
this._drawingLocomotiveHeight = drawningLocomotiveHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean SetPictureSize(int width, int height)
|
||||||
|
{
|
||||||
|
if (_drawingLocomotiveWidth > width || _drawingLocomotiveHeight > height)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
if (_startPosX != null && _drawingLocomotiveWidth + _startPosX > width) _startPosX = _pictureWidth - _drawingLocomotiveWidth;
|
||||||
|
if (_startPosY != null && _startPosY + _drawingLocomotiveHeight > _pictureHeight) _startPosY = _pictureHeight - _drawingLocomotiveHeight;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public void SetPosition(int x, int y)
|
||||||
|
{
|
||||||
|
if (_pictureHeight == null || _pictureWidth == null) return;
|
||||||
|
_startPosX = x;
|
||||||
|
_startPosY = y;
|
||||||
|
if (x + _drawingLocomotiveWidth > _pictureWidth || y + _drawingLocomotiveHeight > _pictureHeight || x < 0 || y < 0)
|
||||||
|
{
|
||||||
|
if (x < 0) _startPosX = 0;
|
||||||
|
if (y < 0 ) _startPosY = 0;
|
||||||
|
if (x + _drawingLocomotiveWidth > _pictureWidth) _startPosX = _pictureWidth - _drawingLocomotiveWidth;
|
||||||
|
if (y + _drawingLocomotiveHeight > _pictureHeight) _startPosY = _pictureHeight - _drawingLocomotiveHeight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean MoveTransport(DirectionType direction)
|
||||||
|
{
|
||||||
|
if( _startPosX == null || _startPosY == null) return false;
|
||||||
|
switch (direction)
|
||||||
|
{
|
||||||
|
// Влево
|
||||||
|
case Left:
|
||||||
|
if(_startPosX - _entityLocomotive.Step >= 0)
|
||||||
|
{
|
||||||
|
_startPosX -= (int)_entityLocomotive.Step;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
// Вправо
|
||||||
|
case Right:
|
||||||
|
if(_startPosX + _entityLocomotive.Step + _drawingLocomotiveWidth < _pictureWidth)
|
||||||
|
{
|
||||||
|
_startPosX += (int)_entityLocomotive.Step;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
// Вверх
|
||||||
|
case Up:
|
||||||
|
if(_startPosY - _entityLocomotive.Step >= 0)
|
||||||
|
{
|
||||||
|
_startPosY -= (int)_entityLocomotive.Step;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
// Вниз
|
||||||
|
case Down:
|
||||||
|
if(_startPosY + _entityLocomotive.Step + _drawingLocomotiveHeight < _pictureHeight)
|
||||||
|
{
|
||||||
|
_startPosY += (int)_entityLocomotive.Step;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DrawTransport(Graphics g) {
|
||||||
|
if (_entityLocomotive == null || _startPosX == null || _startPosY == null) return;
|
||||||
|
|
||||||
|
// Создание перьев и кистей для прорисовки электровоза
|
||||||
|
Color blackColor = Color.BLACK;
|
||||||
|
Color deepSkyBlueColor = new Color(0, 191, 255);
|
||||||
|
Color yellowColor = Color.YELLOW;
|
||||||
|
|
||||||
|
// Инициализация опорных точек для прорисовки корпуса
|
||||||
|
int pointStartX = _startPosX + 75;
|
||||||
|
int pointStartY = _startPosY + 20;
|
||||||
|
|
||||||
|
Point pointStart = new Point(pointStartX, pointStartY);
|
||||||
|
Point point1 = new Point(pointStartX, pointStartY - 15);
|
||||||
|
Point point2 = new Point(_startPosX + 10, pointStartY - 15);
|
||||||
|
Point pointFinish = new Point(_startPosX + 5, pointStartY);
|
||||||
|
|
||||||
|
// Инициализация опорных точек для прорисовки первой "юбки"
|
||||||
|
Point point3 = new Point(_startPosX + 7, _startPosY + 37);
|
||||||
|
Point point4 = new Point(_startPosX, _startPosY + 43);
|
||||||
|
Point point5 = new Point(_startPosX + 7, _startPosY + 43);
|
||||||
|
|
||||||
|
// Инициализация опорных точек для прорисовки второй "юбки"
|
||||||
|
Point point6 = new Point(_startPosX + 69, _startPosY + 37);
|
||||||
|
Point point7 = new Point(_startPosX + 82, _startPosY + 43);
|
||||||
|
Point point8 = new Point(_startPosX + 69, _startPosY + 43);
|
||||||
|
|
||||||
|
// Совокупность точек полигона уголка первой "юбки"
|
||||||
|
Point[] firstTrianglePoints = { point3, point4, point5 };
|
||||||
|
|
||||||
|
// Совокупность точек полигона уголка второй "юбки"
|
||||||
|
Point[] secondTrianglePoints = { point6, point7, point8 };
|
||||||
|
|
||||||
|
g.setColor(blackColor);
|
||||||
|
g.fillOval(_startPosX + 58, _startPosY + 37, 10, 10);
|
||||||
|
g.drawOval(_startPosX + 58, _startPosY + 37, 8, 8);
|
||||||
|
g.fillOval(_startPosX + 48, _startPosY + 37, 10, 10);
|
||||||
|
g.drawOval(_startPosX + 48, _startPosY + 37, 8, 8);
|
||||||
|
g.fillOval(_startPosX + 19, _startPosY + 37, 10, 10);
|
||||||
|
g.drawOval(_startPosX + 19, _startPosY + 37, 8, 8);
|
||||||
|
g.fillOval(_startPosX + 9, _startPosY + 37, 10, 10);
|
||||||
|
g.drawOval(_startPosX + 9, _startPosY + 37, 8, 8);
|
||||||
|
|
||||||
|
// Прорисовка уголков "юбок"
|
||||||
|
g.setColor(_entityLocomotive.GetBodyColor());
|
||||||
|
g.fillPolygon(new Polygon(new int[]{ point3.x, point4.x, point5.x }, new int[]{ point3.y, point4.y, point5.y }, 3));
|
||||||
|
g.fillPolygon(new Polygon(new int[]{ point6.x, point7.x, point8.x }, new int[]{ point6.y, point7.y, point8.y }, 3));
|
||||||
|
|
||||||
|
// Прорисовка корпуса
|
||||||
|
g.drawRect(_startPosX + 5, _startPosY + 20, 70, 17);
|
||||||
|
g.drawLine(point3.x, point3.y, point4.x, point4.y);
|
||||||
|
g.drawLine(pointStart.x, pointStart.y, point1.x, point1.y);
|
||||||
|
g.drawLine(point1.x, point1.y, point2.x, point2.y);
|
||||||
|
g.drawLine(point2.x, point2.y, pointFinish.x, pointFinish.y);
|
||||||
|
|
||||||
|
// Прорисовка окон
|
||||||
|
g.setColor(deepSkyBlueColor);
|
||||||
|
g.drawRect(_startPosX + 12, _startPosY + 9, 8, 8);
|
||||||
|
g.drawRect(_startPosX+ 63, _startPosY + 9, 8, 8);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package ProjectElectricLocomotive.src.Drawnings;
|
||||||
|
|
||||||
|
import ProjectElectricLocomotive.src.CountWheels;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingOrnamentWheels implements IDrawingOrnament
|
||||||
|
{
|
||||||
|
private CountWheels _wheels;
|
||||||
|
@Override
|
||||||
|
public void SetCountWheels(int count)
|
||||||
|
{
|
||||||
|
for(CountWheels temp : CountWheels.values())
|
||||||
|
{
|
||||||
|
if(temp.GetCountWheels() >= count)
|
||||||
|
{
|
||||||
|
_wheels = temp;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void DrawWheels(Graphics g,int _startPosX, int _startPosY)
|
||||||
|
{
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
switch (_wheels.GetCountWheels())
|
||||||
|
{
|
||||||
|
case 4:
|
||||||
|
g.drawOval(_startPosX + 58, _startPosY + 37, 8, 8);
|
||||||
|
g.drawLine(_startPosX + 62,_startPosY + 38, _startPosX + 62, _startPosY + 38 + 6 );
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
g.drawOval(_startPosX + 48, _startPosY + 37, 8, 8);
|
||||||
|
g.drawLine(_startPosX + 52,_startPosY + 38, _startPosX + 52, _startPosY + 38 + 6 );
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
g.drawOval(_startPosX + 19, _startPosY + 37, 8, 8);
|
||||||
|
g.drawLine(_startPosX + 23,_startPosY + 38, _startPosX + 23, _startPosY + 38 + 6 );
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
g.drawOval(_startPosX + 9, _startPosY + 37, 8, 8);
|
||||||
|
g.drawLine(_startPosX + 13,_startPosY + 38, _startPosX + 13, _startPosY + 38 + 6 );
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java
Normal file
43
ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package ProjectElectricLocomotive.src.Drawnings;
|
||||||
|
|
||||||
|
import ProjectElectricLocomotive.src.CountWheels;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
public class DrawingWheels implements IDrawingOrnament
|
||||||
|
{
|
||||||
|
private CountWheels _wheels;
|
||||||
|
@Override
|
||||||
|
public void SetCountWheels(int count)
|
||||||
|
{
|
||||||
|
for(CountWheels temp : CountWheels.values())
|
||||||
|
{
|
||||||
|
if(temp.GetCountWheels() >= count)
|
||||||
|
{
|
||||||
|
_wheels = temp;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void DrawWheels(Graphics g,int _startPosX, int _startPosY)
|
||||||
|
{
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
switch (_wheels.GetCountWheels())
|
||||||
|
{
|
||||||
|
case 4:
|
||||||
|
g.fillOval(_startPosX + 58, _startPosY + 37, 10, 10);
|
||||||
|
g.drawOval(_startPosX + 58, _startPosY + 37, 8, 8);
|
||||||
|
case 3:
|
||||||
|
g.fillOval(_startPosX + 48, _startPosY + 37, 10, 10);
|
||||||
|
g.drawOval(_startPosX + 48, _startPosY + 37, 8, 8);
|
||||||
|
case 2:
|
||||||
|
g.fillOval(_startPosX + 19, _startPosY + 37, 10, 10);
|
||||||
|
g.drawOval(_startPosX + 19, _startPosY + 37, 8, 8);
|
||||||
|
case 1:
|
||||||
|
g.fillOval(_startPosX + 9, _startPosY + 37, 10, 10);
|
||||||
|
g.drawOval(_startPosX + 9, _startPosY + 37, 8, 8);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package ProjectElectricLocomotive.src.Drawnings;
|
||||||
|
|
||||||
|
import ProjectElectricLocomotive.src.CountWheels;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawningSecondOrnamentWheels implements IDrawingOrnament
|
||||||
|
{
|
||||||
|
private CountWheels _wheels;
|
||||||
|
@Override
|
||||||
|
public void SetCountWheels(int count)
|
||||||
|
{
|
||||||
|
for(CountWheels temp : CountWheels.values())
|
||||||
|
{
|
||||||
|
if(temp.GetCountWheels() >= count)
|
||||||
|
{
|
||||||
|
_wheels = temp;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void DrawWheels(Graphics g,int _startPosX, int _startPosY)
|
||||||
|
{
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
switch (_wheels.GetCountWheels())
|
||||||
|
{
|
||||||
|
case 4:
|
||||||
|
g.drawOval(_startPosX + 58, _startPosY + 37, 8, 8);
|
||||||
|
g.drawLine(_startPosX + 58,_startPosY + 38, _startPosX + 58 + 6, _startPosY + 37 + 6 );
|
||||||
|
g.drawLine(_startPosX + 64,_startPosY + 38, _startPosX + 58, _startPosY + 37 + 6 );
|
||||||
|
case 3:
|
||||||
|
g.drawOval(_startPosX + 48, _startPosY + 37, 8, 8);
|
||||||
|
g.drawLine(_startPosX + 48,_startPosY + 38, _startPosX + 48 + 6, _startPosY + 37 + 6 );
|
||||||
|
g.drawLine(_startPosX + 54,_startPosY + 38, _startPosX + 48, _startPosY + 37 + 6 );
|
||||||
|
case 2:
|
||||||
|
g.drawOval(_startPosX + 19, _startPosY + 37, 8, 8);
|
||||||
|
g.drawLine(_startPosX + 19,_startPosY + 38, _startPosX + 19 + 6, _startPosY + 37 + 6 );
|
||||||
|
g.drawLine(_startPosX + 25,_startPosY + 38, _startPosX + 19, _startPosY + 37 + 6 );
|
||||||
|
case 1:
|
||||||
|
g.drawOval(_startPosX + 9, _startPosY + 37, 8, 8);
|
||||||
|
g.drawLine(_startPosX + 9,_startPosY + 38, _startPosX + 9 + 6, _startPosY + 37 + 6 );
|
||||||
|
g.drawLine(_startPosX + 15,_startPosY + 38, _startPosX + 9, _startPosY + 37 + 6 );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package ProjectElectricLocomotive.src.Drawnings;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public interface IDrawingOrnament
|
||||||
|
{
|
||||||
|
void SetCountWheels(int count);
|
||||||
|
void DrawWheels(Graphics g, int _startPosX, int _startPosY);
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package ProjectElectricLocomotive.src.Entities;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class EntityElectricLocomotive extends EntityLocomotive {
|
||||||
|
|
||||||
|
private Color AdditionalColor;
|
||||||
|
public Color GetAdditionalColor()
|
||||||
|
{
|
||||||
|
return AdditionalColor;
|
||||||
|
}
|
||||||
|
private boolean ElectricHorns;
|
||||||
|
public boolean GetElectricHorns()
|
||||||
|
{
|
||||||
|
return ElectricHorns;
|
||||||
|
}
|
||||||
|
private boolean BatteryPlacement;
|
||||||
|
public boolean GetBatteryPlacement()
|
||||||
|
{
|
||||||
|
return BatteryPlacement;
|
||||||
|
}
|
||||||
|
public EntityElectricLocomotive(int speed, float weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement)
|
||||||
|
{
|
||||||
|
super(speed, weight, bodyColor);
|
||||||
|
AdditionalColor = additionalColor;
|
||||||
|
ElectricHorns = electricHorns;
|
||||||
|
BatteryPlacement = batteryPlacement;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
36
ProjectElectricLocomotive/src/Entities/EntityLocomotive.java
Normal file
36
ProjectElectricLocomotive/src/Entities/EntityLocomotive.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package ProjectElectricLocomotive.src.Entities;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class EntityLocomotive
|
||||||
|
{
|
||||||
|
private int Speed;
|
||||||
|
public int GetSpeed()
|
||||||
|
{
|
||||||
|
return Speed;
|
||||||
|
}
|
||||||
|
private float Weight;
|
||||||
|
public float GetWeight()
|
||||||
|
{
|
||||||
|
return Weight;
|
||||||
|
}
|
||||||
|
private Color BodyColor;
|
||||||
|
public Color GetBodyColor()
|
||||||
|
{
|
||||||
|
return BodyColor;
|
||||||
|
}
|
||||||
|
public float Step;
|
||||||
|
public float GetStep()
|
||||||
|
{
|
||||||
|
return Step;
|
||||||
|
}
|
||||||
|
public EntityLocomotive(int speed, float weight, Color bodyColor)
|
||||||
|
{
|
||||||
|
Random rnd = new Random();
|
||||||
|
Speed = speed <= 0 ? rnd.nextInt(100, 300) : speed;
|
||||||
|
Weight = weight <= 0 ? rnd.nextInt(1000, 3000) : weight;
|
||||||
|
BodyColor = bodyColor;
|
||||||
|
Step = Speed * 100 / Weight;
|
||||||
|
}
|
||||||
|
}
|
@ -1,117 +0,0 @@
|
|||||||
package ProjectElectricLocomotive.src;
|
|
||||||
import java.awt.Color;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Класс-сущность "Тепловоз"
|
|
||||||
*/
|
|
||||||
public class EntityElectricLocomotive {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Скорость
|
|
||||||
*/
|
|
||||||
private int speed;
|
|
||||||
|
|
||||||
private int wheelsCount;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Вес
|
|
||||||
*/
|
|
||||||
private double weight;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Основной цвет
|
|
||||||
*/
|
|
||||||
private Color bodyColor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Дополнительный цвет (для дополнительных элементов)
|
|
||||||
*/
|
|
||||||
private Color additionalColor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Признак (опция) наличия "корпус"
|
|
||||||
*/
|
|
||||||
private boolean electricHorns;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Признак (опция) наличия места под батарею
|
|
||||||
*/
|
|
||||||
private boolean batteryPlacement;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Инициализация полей объекта-класса тепловоза
|
|
||||||
*
|
|
||||||
* @param speed Скорость
|
|
||||||
* @param weight Вес
|
|
||||||
* @param bodyColor Основной цвет
|
|
||||||
* @param additionalColor Дополнительный цвет
|
|
||||||
* @param electricHorns Наличие "корпуса"
|
|
||||||
* @param batteryPlacement Наличие места под батарею
|
|
||||||
*/
|
|
||||||
public void init(int speed, double weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement) {
|
|
||||||
this.speed = speed;
|
|
||||||
this.weight = weight;
|
|
||||||
this.bodyColor = bodyColor;
|
|
||||||
this.additionalColor = additionalColor;
|
|
||||||
this.electricHorns = electricHorns;
|
|
||||||
this.batteryPlacement = batteryPlacement;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Получение шага перемещения объекта
|
|
||||||
*
|
|
||||||
* @return Шаг перемещения
|
|
||||||
*/
|
|
||||||
public double getStep() {
|
|
||||||
return speed * 100 / weight;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Геттеры и сеттеры для приватных полей
|
|
||||||
public int getSpeed() {
|
|
||||||
return speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSpeed(int speed) {
|
|
||||||
this.speed = speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getWeight() {
|
|
||||||
return weight;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setWeight(double weight) {
|
|
||||||
this.weight = weight;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color getBodyColor() {
|
|
||||||
return bodyColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBodyColor(Color bodyColor) {
|
|
||||||
this.bodyColor = bodyColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color getAdditionalColor() {
|
|
||||||
return additionalColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAdditionalColor(Color additionalColor) {
|
|
||||||
this.additionalColor = additionalColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isElectricHorns() {
|
|
||||||
return electricHorns;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setElectricHorns(boolean electricHorns) {
|
|
||||||
this.electricHorns = electricHorns;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isBatteryPlacement() {
|
|
||||||
return batteryPlacement;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBatteryPlacement(boolean batteryPlacement) {
|
|
||||||
this.batteryPlacement = batteryPlacement;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,62 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectElectricLocomotive.src.FormElectricLocomotive">
|
|
||||||
<grid id="27dc6" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
|
||||||
<margin top="0" left="0" bottom="0" right="0"/>
|
|
||||||
<constraints>
|
|
||||||
<xy x="20" y="20" width="500" height="400"/>
|
|
||||||
</constraints>
|
|
||||||
<properties/>
|
|
||||||
<border type="none"/>
|
|
||||||
<children>
|
|
||||||
<vspacer id="a5a06">
|
|
||||||
<constraints>
|
|
||||||
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
</vspacer>
|
|
||||||
<component id="a8703" class="javax.swing.JButton" binding="buttonUp">
|
|
||||||
<constraints>
|
|
||||||
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
<properties>
|
|
||||||
<text value="Up"/>
|
|
||||||
</properties>
|
|
||||||
</component>
|
|
||||||
<component id="4aff6" class="javax.swing.JButton" binding="buttonRight">
|
|
||||||
<constraints>
|
|
||||||
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
<properties>
|
|
||||||
<text value="Right"/>
|
|
||||||
</properties>
|
|
||||||
</component>
|
|
||||||
<component id="861ee" class="javax.swing.JButton" binding="buttonDown">
|
|
||||||
<constraints>
|
|
||||||
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
<properties>
|
|
||||||
<text value="Down"/>
|
|
||||||
</properties>
|
|
||||||
</component>
|
|
||||||
<component id="4d9c3" class="javax.swing.JButton" binding="buttonLeft">
|
|
||||||
<constraints>
|
|
||||||
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
<properties>
|
|
||||||
<background color="-3561946"/>
|
|
||||||
<enabled value="true"/>
|
|
||||||
<hideActionText value="true"/>
|
|
||||||
<text value="Left"/>
|
|
||||||
<verticalAlignment value="0"/>
|
|
||||||
</properties>
|
|
||||||
</component>
|
|
||||||
<component id="e4bb7" class="javax.swing.JButton" binding="buttonCreate">
|
|
||||||
<constraints>
|
|
||||||
<grid row="2" column="0" row-span="2" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
<properties>
|
|
||||||
<text value="Create"/>
|
|
||||||
</properties>
|
|
||||||
</component>
|
|
||||||
</children>
|
|
||||||
</grid>
|
|
||||||
</form>
|
|
@ -1,124 +1,162 @@
|
|||||||
package ProjectElectricLocomotive.src;
|
package ProjectElectricLocomotive.src;
|
||||||
|
|
||||||
|
import ProjectElectricLocomotive.src.Drawnings.DrawingField;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ComponentAdapter;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ComponentEvent;
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class FormElectricLocomotive extends JFrame {
|
public class FormElectricLocomotive extends JFrame{
|
||||||
|
private int Width;
|
||||||
|
private int Height;
|
||||||
|
|
||||||
private DrawningElectricLocomotive _drawningElectricLocomotive;
|
JPanel BottomPanel = new JPanel();
|
||||||
|
JPanel CreatePanel = new JPanel();
|
||||||
|
JPanel BottomAndCreatePanel = new JPanel();
|
||||||
|
JPanel DimPanel = new JPanel();
|
||||||
|
JPanel UPanel = new JPanel();
|
||||||
|
JPanel DPanel = new JPanel();
|
||||||
|
JPanel LRPanel = new JPanel();
|
||||||
|
|
||||||
private JPanel panel;
|
JPanel CBPanel = new JPanel();
|
||||||
private JButton buttonCreate;
|
JPanel CPBandButtonPanel = new JPanel();
|
||||||
private JButton buttonUp;
|
|
||||||
private JButton buttonDown;
|
|
||||||
private JButton buttonLeft;
|
|
||||||
private JButton buttonRight;
|
|
||||||
private JLabel pictureBoxElectricLocomotive;
|
|
||||||
|
|
||||||
public FormElectricLocomotive() {
|
DrawingField field = new DrawingField(this);
|
||||||
initComponents();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initComponents() {
|
JButton ButtonStep = new JButton("Шаг");
|
||||||
setTitle("Electric Locomotive");
|
|
||||||
setVisible(true);
|
JButton ButtonCreateElectricLocomotive=new JButton("CreateElectricLocomotive");
|
||||||
setSize(520, 300);
|
JButton ButtonCreateLocomotive=new JButton("CreateLocomotive");
|
||||||
|
|
||||||
|
public JComboBox<String> comboBoxStrategy = new JComboBox<>();
|
||||||
|
|
||||||
|
JButton ButtonUp=new JButton("Up");
|
||||||
|
|
||||||
|
|
||||||
|
JButton ButtonDown=new JButton("Down");
|
||||||
|
|
||||||
|
|
||||||
|
JButton ButtonRight=new JButton("Right");
|
||||||
|
|
||||||
|
JButton ButtonLeft=new JButton("Left");
|
||||||
|
public FormElectricLocomotive(){
|
||||||
|
super("Electric Locomotive");
|
||||||
|
setSize(900,500);
|
||||||
|
Width = getWidth();
|
||||||
|
Height = getHeight();
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
ShowWindow();
|
||||||
|
RefreshWindow();
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
panel = new JPanel();
|
public void ShowWindow(){
|
||||||
buttonCreate = new JButton("Create");
|
|
||||||
buttonUp = new JButton("Up");
|
|
||||||
buttonDown = new JButton("Down");
|
|
||||||
buttonLeft = new JButton("Left");
|
|
||||||
buttonRight = new JButton("Right");
|
|
||||||
pictureBoxElectricLocomotive = new JLabel();
|
|
||||||
|
|
||||||
panel.setLayout(new FlowLayout());
|
Dimension dimen = new Dimension(30,30);
|
||||||
panel.add(buttonCreate);
|
|
||||||
panel.add(buttonUp);
|
|
||||||
panel.add(buttonDown);
|
|
||||||
panel.add(buttonLeft);
|
|
||||||
panel.add(buttonRight);
|
|
||||||
add(panel, BorderLayout.NORTH);
|
|
||||||
add(pictureBoxElectricLocomotive, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
buttonCreate.addActionListener(new ActionListener() {
|
ButtonUp.setPreferredSize(dimen);
|
||||||
@Override
|
ButtonUp.addActionListener(e->{
|
||||||
public void actionPerformed(ActionEvent e) {
|
field.UpButtonAction();
|
||||||
createElectricLocomotive();
|
repaint();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
buttonUp.addActionListener(new ActionListener() {
|
ButtonDown.setPreferredSize(dimen);
|
||||||
@Override
|
ButtonDown.addActionListener(e->{
|
||||||
public void actionPerformed(ActionEvent e) {
|
field.DownButtonAction();
|
||||||
moveTransport(DirectionType.Up);
|
repaint();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
buttonDown.addActionListener(new ActionListener() {
|
ButtonRight.setPreferredSize(dimen);
|
||||||
@Override
|
ButtonRight.addActionListener(e->{
|
||||||
public void actionPerformed(ActionEvent e) {
|
field.RightButtonAction();
|
||||||
moveTransport(DirectionType.Down);
|
repaint();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
buttonLeft.addActionListener(new ActionListener() {
|
ButtonLeft.setPreferredSize(dimen);
|
||||||
@Override
|
ButtonLeft.addActionListener(e->{
|
||||||
public void actionPerformed(ActionEvent e) {
|
field.LeftButtonAction();
|
||||||
moveTransport(DirectionType.Left);
|
repaint();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
buttonRight.addActionListener(new ActionListener() {
|
LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0));
|
||||||
|
LRPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
LRPanel.add(ButtonLeft);
|
||||||
|
LRPanel.add(ButtonRight);
|
||||||
|
|
||||||
|
UPanel.setLayout(new FlowLayout());
|
||||||
|
UPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
UPanel.add(ButtonUp);
|
||||||
|
|
||||||
|
DPanel.setLayout(new FlowLayout());
|
||||||
|
DPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
DPanel.add(ButtonDown);
|
||||||
|
|
||||||
|
DimPanel.setLayout(new BoxLayout(DimPanel,BoxLayout.Y_AXIS));
|
||||||
|
DimPanel.setBackground(new Color(0,0,0,0));
|
||||||
|
DimPanel.add(UPanel);
|
||||||
|
DimPanel.add(LRPanel);
|
||||||
|
DimPanel.add(DPanel);
|
||||||
|
add(DimPanel);
|
||||||
|
|
||||||
|
CreatePanel.setLayout(new FlowLayout());
|
||||||
|
CreatePanel.setBackground(new Color(0,0,0,0));
|
||||||
|
|
||||||
|
CreatePanel.add(ButtonCreateLocomotive);
|
||||||
|
ButtonCreateLocomotive.addActionListener(e->{
|
||||||
|
field.CreateButtonLocomotiveAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
CreatePanel.add(ButtonCreateElectricLocomotive);
|
||||||
|
ButtonCreateElectricLocomotive.addActionListener(e->{
|
||||||
|
field.CreateButtonElectricLocomotiveAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
comboBoxStrategy.setPreferredSize(new Dimension(80, 40));
|
||||||
|
comboBoxStrategy.addItem("К центру");
|
||||||
|
comboBoxStrategy.addItem("К краю");
|
||||||
|
ButtonStep.setPreferredSize(new Dimension(80, 40));
|
||||||
|
ButtonStep.addActionListener(e->{
|
||||||
|
field.ButtonStrategyStepAction();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
CBPanel.setLayout(new FlowLayout());
|
||||||
|
CBPanel.setBackground(new Color(0, 0, 0, 0));
|
||||||
|
CBPanel.add(comboBoxStrategy);
|
||||||
|
CBPanel.add(ButtonStep);
|
||||||
|
CPBandButtonPanel.setLayout(new BoxLayout(CPBandButtonPanel, BoxLayout.X_AXIS));
|
||||||
|
CPBandButtonPanel.setBackground(new Color(0, 0, 0, 0));
|
||||||
|
CPBandButtonPanel.add(CBPanel);
|
||||||
|
add(CPBandButtonPanel);
|
||||||
|
|
||||||
|
BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel, BoxLayout.Y_AXIS));
|
||||||
|
BottomAndCreatePanel.setBackground(new Color(0,0,0,0));
|
||||||
|
BottomAndCreatePanel.add(CreatePanel);
|
||||||
|
BottomAndCreatePanel.add(BottomPanel);
|
||||||
|
|
||||||
|
add(BottomAndCreatePanel);
|
||||||
|
add(field);
|
||||||
|
|
||||||
|
addComponentListener(new ComponentAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void componentResized(ComponentEvent e) {
|
||||||
moveTransport(DirectionType.Right);
|
super.componentResized(e);
|
||||||
|
Width = getWidth();
|
||||||
|
Height = getHeight();
|
||||||
|
field.ResizeField();
|
||||||
|
repaint();
|
||||||
|
RefreshWindow();
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
public void RefreshWindow(){
|
||||||
private void createElectricLocomotive() {
|
field.setLayout(new FlowLayout());
|
||||||
Random random = new Random();
|
CPBandButtonPanel.setBounds(Width - 200, 0, 190, 140);
|
||||||
_drawningElectricLocomotive = new DrawningElectricLocomotive();
|
BottomAndCreatePanel.setBounds(-330, Height - 110, Width, 180);
|
||||||
_drawningElectricLocomotive.init(
|
DimPanel.setBounds(Width - 170, Height - 170, 190, 140);
|
||||||
random.nextInt(200) + 100,
|
|
||||||
random.nextInt(2000) + 1000,
|
|
||||||
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(),
|
|
||||||
random.nextInt(0,5)
|
|
||||||
);
|
|
||||||
_drawningElectricLocomotive.setPictureSize(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight());
|
|
||||||
_drawningElectricLocomotive.setPosition(random.nextInt(pictureBoxElectricLocomotive.getWidth()), random.nextInt(pictureBoxElectricLocomotive.getHeight()));
|
|
||||||
|
|
||||||
draw();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void moveTransport(DirectionType direction) {
|
|
||||||
if (_drawningElectricLocomotive != null) {
|
|
||||||
if (_drawningElectricLocomotive.moveTransport(direction)) {
|
|
||||||
draw();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void draw() {
|
|
||||||
if (_drawningElectricLocomotive != null) {
|
|
||||||
Image image = new ImageIcon(_drawningElectricLocomotive.getImage()).getImage();
|
|
||||||
Image scaledImage = image.getScaledInstance(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight(), Image.SCALE_SMOOTH);
|
|
||||||
pictureBoxElectricLocomotive.setIcon(new ImageIcon(scaledImage));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SwingUtilities.invokeLater(() -> {
|
|
||||||
FormElectricLocomotive form = new FormElectricLocomotive();
|
|
||||||
form.setVisible(true);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,74 @@
|
|||||||
|
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||||
|
|
||||||
|
public abstract class AbstractStrategy
|
||||||
|
{
|
||||||
|
private IMoveableObject _moveableObject = null;
|
||||||
|
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||||
|
protected int fieldWidth;
|
||||||
|
protected int fieldHeight;
|
||||||
|
protected StrategyStatus state;
|
||||||
|
|
||||||
|
protected ObjectParameters getObjectParameters() {
|
||||||
|
return (_moveableObject != null) ? _moveableObject.getObjectPosition() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Integer GetStep() {
|
||||||
|
if (state != StrategyStatus.InProgress) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (_moveableObject != null) ? _moveableObject.getStep() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StrategyStatus getStatus() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetData(IMoveableObject moveableObject, int width, int height) {
|
||||||
|
if (moveableObject == null) {
|
||||||
|
state = StrategyStatus.NotInit;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state = StrategyStatus.InProgress;
|
||||||
|
this._moveableObject = moveableObject;
|
||||||
|
fieldWidth = width;
|
||||||
|
fieldHeight = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MakeStep() {
|
||||||
|
if (state != StrategyStatus.InProgress) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isTargetDestination()) {
|
||||||
|
state = StrategyStatus.Finish;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
moveToTarget();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean MoveLeft() {
|
||||||
|
return moveTo(MovementDirection.Left);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean MoveRight() {
|
||||||
|
return moveTo(MovementDirection.Right);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean MoveUp() {
|
||||||
|
return moveTo(MovementDirection.Up);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean MoveDown() {
|
||||||
|
return moveTo(MovementDirection.Down);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean moveTo(MovementDirection movementDirection) {
|
||||||
|
if (state != StrategyStatus.InProgress) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return (_moveableObject != null) && _moveableObject.tryMoveObject(movementDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void moveToTarget();
|
||||||
|
|
||||||
|
protected abstract boolean isTargetDestination();
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||||
|
|
||||||
|
public interface IMoveableObject
|
||||||
|
{
|
||||||
|
ObjectParameters getObjectPosition();
|
||||||
|
int getStep();
|
||||||
|
boolean tryMoveObject(MovementDirection direction);
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||||
|
|
||||||
|
public class MoveToBorder extends AbstractStrategy
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected void moveToTarget()
|
||||||
|
{
|
||||||
|
ObjectParameters objParams = getObjectParameters();
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int diffX = objParams.getRightBorder() - fieldWidth;
|
||||||
|
if (Math.abs(diffX) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffX > 0) MoveLeft();
|
||||||
|
else MoveRight();
|
||||||
|
}
|
||||||
|
int diffY = objParams.getDownBorder() - fieldHeight;
|
||||||
|
if (Math.abs(diffY) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffY > 0) MoveUp();
|
||||||
|
else MoveDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isTargetDestination() {
|
||||||
|
ObjectParameters objParams = getObjectParameters();
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return objParams.getRightBorder() + GetStep() >= fieldWidth && objParams.getDownBorder() + GetStep() >= fieldHeight;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||||
|
|
||||||
|
public class MoveToCenter extends AbstractStrategy
|
||||||
|
{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void moveToTarget() {
|
||||||
|
ObjectParameters objParams = getObjectParameters();
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int diffX = objParams.getObjectMiddleHorizontal() - fieldWidth / 2;
|
||||||
|
if(Math.abs(diffX) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffX > 0) MoveLeft();
|
||||||
|
else MoveRight();
|
||||||
|
}
|
||||||
|
int diffY = objParams.getObjectMiddleVertical() - fieldHeight / 2;
|
||||||
|
if (Math.abs(diffY) > GetStep())
|
||||||
|
{
|
||||||
|
if (diffY > 0) MoveUp();
|
||||||
|
else MoveDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isTargetDestination() {
|
||||||
|
ObjectParameters objParams = getObjectParameters();
|
||||||
|
if (objParams == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return objParams.getObjectMiddleHorizontal() - GetStep() <= fieldWidth / 2 && objParams.getObjectMiddleHorizontal() + GetStep() >= fieldWidth / 2 &&
|
||||||
|
objParams.getObjectMiddleVertical() - GetStep() <= fieldHeight / 2 && objParams.getObjectMiddleVertical() + GetStep() >= fieldHeight / 2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||||
|
|
||||||
|
import ProjectElectricLocomotive.src.DirectionType;
|
||||||
|
import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive;
|
||||||
|
|
||||||
|
public class MoveableLocomotive implements IMoveableObject {
|
||||||
|
private DrawingLocomotive _locomotive = null;
|
||||||
|
public MoveableLocomotive(DrawingLocomotive drawingLocomotive)
|
||||||
|
{
|
||||||
|
_locomotive = drawingLocomotive;
|
||||||
|
}
|
||||||
|
public ObjectParameters getObjectPosition() {
|
||||||
|
if (_locomotive == null || _locomotive.GetEntityLocomotive() == null || _locomotive.getPosX() == null || _locomotive.getPosY() == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ObjectParameters(_locomotive.getPosX(), _locomotive.getPosY(), _locomotive.getWidth(), _locomotive.getHeight());
|
||||||
|
}
|
||||||
|
public int getStep() {
|
||||||
|
if(_locomotive == null || _locomotive.GetEntityLocomotive() == null) return 0;
|
||||||
|
return (int)(_locomotive.GetEntityLocomotive().Step);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean tryMoveObject(MovementDirection direction) {
|
||||||
|
if (_locomotive == null || _locomotive.GetEntityLocomotive() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DirectionType directionType = getDirectionType(direction);
|
||||||
|
return _locomotive.MoveTransport(directionType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DirectionType getDirectionType(MovementDirection direction) {
|
||||||
|
switch (direction) {
|
||||||
|
case Left:
|
||||||
|
return DirectionType.Left;
|
||||||
|
case Right:
|
||||||
|
return DirectionType.Right;
|
||||||
|
case Up:
|
||||||
|
return DirectionType.Up;
|
||||||
|
case Down:
|
||||||
|
return DirectionType.Down;
|
||||||
|
default:
|
||||||
|
return DirectionType.Unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||||
|
|
||||||
|
public enum MovementDirection
|
||||||
|
{
|
||||||
|
Up(1),
|
||||||
|
Down(2),
|
||||||
|
Left(3),
|
||||||
|
Right(4);
|
||||||
|
private final int Value;
|
||||||
|
|
||||||
|
MovementDirection(int value) {
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return Value;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||||
|
|
||||||
|
public class ObjectParameters
|
||||||
|
{
|
||||||
|
private final int x;
|
||||||
|
public int getLeftBorder() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
private final int y;
|
||||||
|
public int getTopBorder() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
private final int width;
|
||||||
|
|
||||||
|
private final int height;
|
||||||
|
|
||||||
|
public ObjectParameters(int x, int y, int width, int height) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getRightBorder() {
|
||||||
|
return x + width;
|
||||||
|
}
|
||||||
|
public int getDownBorder() {
|
||||||
|
return y + height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getObjectMiddleHorizontal() {
|
||||||
|
return x + width / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getObjectMiddleVertical() {
|
||||||
|
return y + height / 2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||||
|
|
||||||
|
public enum StrategyStatus
|
||||||
|
{
|
||||||
|
NotInit,
|
||||||
|
|
||||||
|
InProgress,
|
||||||
|
|
||||||
|
Finish
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user