Лабораторная 2

This commit is contained in:
Никита Белянин 2023-10-29 18:57:32 +04:00
parent 9bb10333ef
commit 41dc8a4325
20 changed files with 362 additions and 434 deletions

29
Tank/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

View File

@ -1,3 +1,8 @@
# Default ignored files # Default ignored files
/shelf/ /shelf/
/workspace.xml /workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="19" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -1,82 +1,93 @@
public abstract class AbstractStrategy { public abstract class AbstractStrategy {
// Перемещаемый объект
private IMoveableObject _moveableObject;
// Перемещаемый объект // Статус перемещения
private IMoveableObject _moveableObject; private Status _state = Status.NotInit;
// Статус перемещения // Ширина поля
private Status _state = Status.NotInit; protected int FieldWidth;
// Ширина поля // Высота поля
protected int FieldWidth; protected int FieldHeight;
// Высота поля // Статус перемещения
protected int FieldHeight; public Status GetStatus() {
return _state;
}
// Статус перемещения // Установка данных
public Status GetStatus() { return _state; } public void SetData(IMoveableObject moveableObject, int width, int height) {
if (moveableObject == null) {
// Установка данных _state = Status.NotInit;
public void SetData(IMoveableObject moveableObject, int width, int height) { return;
if (moveableObject == null) {
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
} }
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
// Шаг перемещения // Шаг перемещения
public void MakeStep() { public void MakeStep() {
if (_state != Status.InProgress) { if (_state != Status.InProgress) {
return; return;
}
if (IsTargetDestinaion()) {
_state = Status.Finish;
return;
}
MoveToTarget();
} }
if (IsTargetDestination()) {
// Перемещение влево _state = Status.Finish;
protected boolean MoveLeft() { return MoveTo(Direction.Left);} return;
// Перемещение вправо
protected boolean MoveRight() { return MoveTo(Direction.Right);}
// Перемещение вверх
protected boolean MoveUp() { return MoveTo(Direction.Up);}
// Перемещение вниз
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();
} }
MoveToTarget();
}
// Перемещение к цели // Перемещение влево
protected abstract void MoveToTarget(); protected boolean MoveLeft() {
return MoveTo(Direction.Left);
}
// Достигнута ли цель // Перемещение вправо
protected abstract boolean IsTargetDestinaion(); protected boolean MoveRight() {
return MoveTo(Direction.Right);
}
// Попытка перемещения в требуемом направлении // Перемещение вверх
private boolean MoveTo(Direction Direction) { protected boolean MoveUp() {
if (_state != Status.InProgress) { return MoveTo(Direction.Up);
return false; }
}
if (_moveableObject.CheckCanMove(Direction)) { // Перемещение вниз
_moveableObject.MoveObject(Direction); protected boolean MoveDown() {
return true; 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 IsTargetDestination();
// Попытка перемещения в требуемом направлении
private boolean MoveTo(Direction Direction) {
if (_state != Status.InProgress) {
return false; return false;
} }
} if (_moveableObject.CheckCanMove(Direction)) {
_moveableObject.MoveObject(Direction);
return true;
}
return false;
}
}

View File

@ -4,9 +4,6 @@ import java.util.Random;
public class DrawingArmoVehicle { public class DrawingArmoVehicle {
protected IOrnamentForm OrnamentsForm; protected IOrnamentForm OrnamentsForm;
public EntityArmoVehicle ArmoVehicle; public EntityArmoVehicle ArmoVehicle;
public EntityArmoVehicle getTank() {
return ArmoVehicle;
}
protected int _pictureWidth; protected int _pictureWidth;
protected int _pictureHeight; protected int _pictureHeight;
protected int _startPosX; protected int _startPosX;
@ -27,7 +24,7 @@ public class DrawingArmoVehicle {
OrnamentsForm = new DrawingWheelsCombination(); OrnamentsForm = new DrawingWheelsCombination();
break; break;
case 1: case 1:
OrnamentsForm = new DrawingAsteriskOrnament(); OrnamentsForm = new DrawingStarOrnament();
break; break;
case 2: case 2:
OrnamentsForm = new DrawingSuspensionOrnament(); OrnamentsForm = new DrawingSuspensionOrnament();
@ -47,7 +44,7 @@ public class DrawingArmoVehicle {
public int GetPosX (){ return _startPosX; } public int GetPosX (){ return _startPosX; }
public int GetPosY (){ return _startPosY; } public int GetPosY (){ return _startPosY; }
public int GetWidth (){ return _TankWidth; } public int GetWidth (){ return _TankWidth; }
public int GetHeight (){return _TankHeight;} public int GetHeight (){ return _TankHeight; }
public boolean CanMove(Direction direction) { public boolean CanMove(Direction direction) {
if (ArmoVehicle == null) { if (ArmoVehicle == null) {
@ -91,38 +88,20 @@ public class DrawingArmoVehicle {
} }
} }
public void SetWheels(int number) {
OrnamentsForm.setDigit(number);
}
public void DrawTransport(Graphics2D g) { public void DrawTransport(Graphics2D g) {
if (ArmoVehicle == null) { if (ArmoVehicle == null) {
return; return;
} }
g.setColor(Color.BLUE); // body
g.setColor(ArmoVehicle.BodyColor);
int[] xPoints = {_startPosX + 5, _startPosX + 140, _startPosX + 130,_startPosX + 12}; int[] xPoints = {_startPosX + 5, _startPosX + 140, _startPosX + 130,_startPosX + 12};
int[] yPoints = {_startPosY + 30, _startPosY + 30, _startPosY + 42, _startPosY + 42}; int[] yPoints = {_startPosY + 30, _startPosY + 30, _startPosY + 42, _startPosY + 42};
int nPoints = 4; int nPoints = xPoints.length;
g.drawPolygon(xPoints,yPoints,nPoints); g.drawPolygon(xPoints,yPoints,nPoints);
g.fillPolygon(xPoints,yPoints,nPoints); g.fillPolygon(xPoints,yPoints,nPoints);
//wheels //wheels
OrnamentsForm.Draw(g, _startPosX, _startPosY); OrnamentsForm.Draw(g, _startPosX, _startPosY);
} }
}
public void ChangeBorders(int width,int height) {
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth<=_TankWidth||_pictureHeight<=_TankHeight) {
_pictureWidth = Integer.parseInt(null);
_pictureHeight = Integer.parseInt(null);
return;
}
if (_startPosX + _TankWidth > _pictureWidth) {
_startPosX = _pictureWidth - _TankWidth;
}
if (_startPosY + _TankHeight > _pictureHeight) {
_startPosY = _pictureHeight - _TankHeight;
}
}
}

View File

@ -1,17 +1,17 @@
public class DrawingObjectTank implements IMoveableObject { public class DrawingObjectTank implements IMoveableObject {
private DrawingArmoVehicle _ArmoVehicle = null; private DrawingArmoVehicle _drawingArmoVehicle = null;
public DrawingObjectTank(DrawingArmoVehicle drawingTrain) public DrawingObjectTank(DrawingArmoVehicle drawingTank)
{ {
_ArmoVehicle = drawingTrain; _drawingArmoVehicle = drawingTank;
} }
public ObjectParameters GetObjectPosition() { public ObjectParameters GetObjectPosition() {
if (_ArmoVehicle == null || _ArmoVehicle.ArmoVehicle == null) { if (_drawingArmoVehicle == null || _drawingArmoVehicle.ArmoVehicle == null) {
return null; return null;
} }
return new ObjectParameters(_ArmoVehicle.GetPosX(),_ArmoVehicle.GetPosY(), return new ObjectParameters(_drawingArmoVehicle.GetPosX(),_drawingArmoVehicle.GetPosY(),
_ArmoVehicle.GetWidth(), _ArmoVehicle.GetHeight()); _drawingArmoVehicle.GetWidth(), _drawingArmoVehicle.GetHeight());
} }
public int GetStep(){ return (int)_ArmoVehicle.ArmoVehicle.Step; } public int GetStep(){ return (int)_drawingArmoVehicle.ArmoVehicle.Step; }
public boolean CheckCanMove(Direction direction) { return _ArmoVehicle.CanMove(direction);} public boolean CheckCanMove(Direction direction) { return _drawingArmoVehicle.CanMove(direction);}
public void MoveObject(Direction direction) { _ArmoVehicle.MoveTransport(direction); } public void MoveObject(Direction direction) { _drawingArmoVehicle.MoveTransport(direction); }
} }

View File

@ -1,8 +1,8 @@
import java.awt.*; import java.awt.*;
public class DrawingAsteriskOrnament implements IOrnamentForm { public class DrawingStarOrnament implements IOrnamentForm {
private CountWheels wheels; private CountWheels wheels;
private Color additionalColor; private Color StarColor;
public CountWheels getNumWheel() { public CountWheels getNumWheel() {
return wheels; return wheels;
} }
@ -25,8 +25,8 @@ public class DrawingAsteriskOrnament implements IOrnamentForm {
} }
public void CaterpillarStar(Graphics g, int _startPosX, int _startPosY) { public void CaterpillarStar(Graphics g, int _startPosX, int _startPosY) {
additionalColor = Color.RED; StarColor = Color.RED;
g.setColor(additionalColor); g.setColor(StarColor);
int xPontsStar[] = {_startPosX + 15, _startPosX + 18, _startPosX + 21, _startPosX + 18, _startPosX + 19, _startPosX + 16, _startPosX + 12, _startPosX + 12, _startPosX + 11, _startPosX + 15, _startPosX + 16}; int xPontsStar[] = {_startPosX + 15, _startPosX + 18, _startPosX + 21, _startPosX + 18, _startPosX + 19, _startPosX + 16, _startPosX + 12, _startPosX + 12, _startPosX + 11, _startPosX + 15, _startPosX + 16};
int yPontsStar[] = {_startPosY + 35, _startPosY + 38, _startPosY + 38, _startPosY + 42, _startPosY + 45, _startPosY + 42, _startPosY + 45, _startPosY + 41, _startPosY + 38, _startPosY + 38, _startPosY + 35}; int yPontsStar[] = {_startPosY + 35, _startPosY + 38, _startPosY + 38, _startPosY + 42, _startPosY + 45, _startPosY + 42, _startPosY + 45, _startPosY + 41, _startPosY + 38, _startPosY + 38, _startPosY + 35};
g.drawPolygon(xPontsStar, yPontsStar, xPontsStar.length); g.drawPolygon(xPontsStar, yPontsStar, xPontsStar.length);

View File

@ -2,7 +2,7 @@ import java.awt.*;
public class DrawingSuspensionOrnament implements IOrnamentForm { public class DrawingSuspensionOrnament implements IOrnamentForm {
private CountWheels wheels; private CountWheels wheels;
private Color additionalColor; private Color SuspensionColor;
public CountWheels getNumWheel() { public CountWheels getNumWheel() {
return wheels; return wheels;
} }
@ -29,14 +29,19 @@ public class DrawingSuspensionOrnament implements IOrnamentForm {
g.fillOval(10 + _startPosX, 42 + _startPosY, 20, 20); g.fillOval(10 + _startPosX, 42 + _startPosY, 20, 20);
} }
public void DrawSuspension(Graphics g, int _startPosX, int _startPosY) {
SuspensionColor = Color.BLUE;
g.setColor(SuspensionColor);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3);
}
public void Draw(Graphics g, int _startPosX, int _startPosY) { public void Draw(Graphics g, int _startPosX, int _startPosY) {
if (wheels == CountWheels.Two){ if (wheels == CountWheels.Two) {
DrawWheels(g,_startPosX, _startPosY); DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY); DrawWheels(g,_startPosX + 100, _startPosY);
additionalColor = Color.RED;
g.setColor(additionalColor); DrawSuspension(g, _startPosX, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 73, _startPosY);
g.fillRect(_startPosX + 100, _startPosY + 50, 10, 3);
} }
if (wheels == CountWheels.Three) { if (wheels == CountWheels.Three) {
@ -44,9 +49,9 @@ public class DrawingSuspensionOrnament implements IOrnamentForm {
DrawWheels(g,_startPosX + 50, _startPosY); DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY); DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3); DrawSuspension(g, _startPosX, _startPosY);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 24, _startPosY);
g.fillRect(_startPosX + 100, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 73, _startPosY);
} }
if (wheels == CountWheels.Four) { if (wheels == CountWheels.Four) {
@ -56,10 +61,10 @@ public class DrawingSuspensionOrnament implements IOrnamentForm {
DrawWheels(g,_startPosX + 75, _startPosY); DrawWheels(g,_startPosX + 75, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY); DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3); DrawSuspension(g, _startPosX, _startPosY);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 24, _startPosY);
g.fillRect(_startPosX + 80, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 53, _startPosY);
g.fillRect(_startPosX + 101, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 73, _startPosY);
} }
} }
} }

View File

@ -2,32 +2,25 @@ import java.awt.*;
public class DrawingTank extends DrawingArmoVehicle { public class DrawingTank extends DrawingArmoVehicle {
protected IOrnamentForm OrnamentsForm; protected IOrnamentForm OrnamentsForm;
private boolean WheelsOrnament;
private Color AdditionalColor;
private boolean OrnamentAdd; private boolean OrnamentAdd;
public DrawingTank(int speed, double weight, Color bodyColor, int _numWheel, Color additionalColor, boolean bodyKit, boolean caterpillar, boolean tower, int width, int height, boolean wheelsOrnament, boolean ornamentAdd) public DrawingTank(int speed, double weight, Color bodyColor, int _numWheel, Color additionalColor, boolean bodyKit, boolean caterpillar, boolean tower, int width, int height, boolean ornamentAdd) {
{
super(speed, weight, bodyColor, _numWheel, width, height); super(speed, weight, bodyColor, _numWheel, width, height);
ArmoVehicle = new EntityTank(speed, weight, bodyColor, _numWheel, additionalColor, bodyKit, caterpillar, tower); ArmoVehicle = new EntityTank(speed, weight, bodyColor, _numWheel, additionalColor, bodyKit, caterpillar, tower);
_TankWidth = ((EntityTank)ArmoVehicle).BodyKit ? 169 : 83; _TankWidth = ((EntityTank)ArmoVehicle).BodyKit ? 169 : 83;
this.AdditionalColor = additionalColor;
this.OrnamentAdd = ornamentAdd; this.OrnamentAdd = ornamentAdd;
} }
// Установка позиции
public void SetPosition(int x, int y) { public void SetPosition(int x, int y) {
_startPosX = Math.min(x, _pictureWidth-_TankWidth); _startPosX = Math.min(x, _pictureWidth-_TankWidth);
_startPosY = Math.min(y, _pictureHeight-_TankHeight); _startPosY = Math.min(y, _pictureHeight-_TankHeight);
} }
public boolean isOrnamentAdd() { private boolean setOrnamentAdd() { return OrnamentAdd; }
return OrnamentAdd;
}
private void setOrnamentAdd(boolean ornamentAdd) {
this.OrnamentAdd = ornamentAdd;
}
// Прорисовка объекта
public void DrawTransport(Graphics2D g) { public void DrawTransport(Graphics2D g) {
if (ArmoVehicle == null) { if (ArmoVehicle == null) {
return; return;
@ -35,7 +28,7 @@ public class DrawingTank extends DrawingArmoVehicle {
super.DrawTransport(g); super.DrawTransport(g);
if (((EntityTank) ArmoVehicle).BodyKit) { if (((EntityTank) ArmoVehicle).BodyKit) {
g.setColor(Color.DARK_GRAY); g.setColor(((EntityTank) ArmoVehicle).AdditionalColor);
int[] xPointsBody = {_startPosX + 52, _startPosX + 52, _startPosX + 40, _startPosX + 15,_startPosX + 15, _startPosX + 60,_startPosX + 90,_startPosX + 120,_startPosX + 100,_startPosX + 95, _startPosX + 90}; int[] xPointsBody = {_startPosX + 52, _startPosX + 52, _startPosX + 40, _startPosX + 15,_startPosX + 15, _startPosX + 60,_startPosX + 90,_startPosX + 120,_startPosX + 100,_startPosX + 95, _startPosX + 90};
int[] yPointsBody = {_startPosY + 30, _startPosY + 27, _startPosY + 23, _startPosY + 18,_startPosY + 15, _startPosY + 11,_startPosY + 11,_startPosY + 20,_startPosY + 25,_startPosY + 27,_startPosY + 30}; int[] yPointsBody = {_startPosY + 30, _startPosY + 27, _startPosY + 23, _startPosY + 18,_startPosY + 15, _startPosY + 11,_startPosY + 11,_startPosY + 20,_startPosY + 25,_startPosY + 27,_startPosY + 30};
int nPointsBody = xPointsBody.length; int nPointsBody = xPointsBody.length;
@ -51,10 +44,10 @@ public class DrawingTank extends DrawingArmoVehicle {
} }
if (((EntityTank) ArmoVehicle).Tower) { if (((EntityTank) ArmoVehicle).Tower) {
g.setColor(AdditionalColor); g.setColor(Color.DARK_GRAY);
// Орудие // Орудие
g.drawRect(_startPosX + 112, _startPosY + 17, 60, 4); g.drawRect(_startPosX + 112, _startPosY + 17, 60, 5);
g.fillRect(_startPosX + 112, _startPosY + 17, 60, 4); g.fillRect(_startPosX + 112, _startPosY + 17, 60, 5);
// Зенитное орудие // Зенитное орудие
int[] xPointsGun = {_startPosX + 45, _startPosX + 45, _startPosX + 41, _startPosX + 41, _startPosX + 42, _startPosX + 41, _startPosX + 44,_startPosX + 50 ,_startPosX + 52,_startPosX + 53, _startPosX + 58}; int[] xPointsGun = {_startPosX + 45, _startPosX + 45, _startPosX + 41, _startPosX + 41, _startPosX + 42, _startPosX + 41, _startPosX + 44,_startPosX + 50 ,_startPosX + 52,_startPosX + 53, _startPosX + 58};
@ -66,4 +59,4 @@ public class DrawingTank extends DrawingArmoVehicle {
g.fillPolygon(xPointsGun,yPointsGun,nPointsGun); g.fillPolygon(xPointsGun,yPointsGun,nPointsGun);
} }
} }
} }

View File

@ -1,9 +1,8 @@
import java.awt.*; import java.awt.*;
public class DrawingWheelsCombination implements IOrnamentForm { public class DrawingWheelsCombination implements IOrnamentForm {
private CountWheels _wheels;
private CountWheels wheels; private CountWheels wheels;
private Color additionalColor; private Color CombinationColor;
public CountWheels getNumWheel() { public CountWheels getNumWheel() {
return wheels; return wheels;
} }
@ -26,14 +25,20 @@ public class DrawingWheelsCombination implements IOrnamentForm {
} }
public void CaterpillarStar(Graphics g, int _startPosX, int _startPosY) { public void CaterpillarStar(Graphics g, int _startPosX, int _startPosY) {
additionalColor = Color.RED; CombinationColor = Color.RED;
g.setColor(additionalColor); g.setColor(CombinationColor);
int xPontsStar[] = {_startPosX + 15, _startPosX + 18, _startPosX + 21, _startPosX + 18, _startPosX + 19, _startPosX + 16, _startPosX + 12, _startPosX + 13, _startPosX + 12, _startPosX + 15, _startPosX + 16}; int xPontsStar[] = {_startPosX + 15, _startPosX + 18, _startPosX + 21, _startPosX + 18, _startPosX + 19, _startPosX + 16, _startPosX + 12, _startPosX + 13, _startPosX + 12, _startPosX + 15, _startPosX + 16};
int yPontsStar[] = {_startPosY + 35, _startPosY + 38, _startPosY + 38, _startPosY + 42, _startPosY + 45, _startPosY + 42, _startPosY + 45, _startPosY + 41, _startPosY + 38, _startPosY + 38, _startPosY + 35}; int yPontsStar[] = {_startPosY + 35, _startPosY + 38, _startPosY + 38, _startPosY + 42, _startPosY + 45, _startPosY + 42, _startPosY + 45, _startPosY + 41, _startPosY + 38, _startPosY + 38, _startPosY + 35};
g.drawPolygon(xPontsStar, yPontsStar, xPontsStar.length); g.drawPolygon(xPontsStar, yPontsStar, xPontsStar.length);
g.fillPolygon(xPontsStar, yPontsStar, xPontsStar.length); g.fillPolygon(xPontsStar, yPontsStar, xPontsStar.length);
} }
public void DrawSuspension(Graphics g, int _startPosX, int _startPosY) {
CombinationColor = Color.BLUE;
g.setColor(CombinationColor);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3);
}
public void DrawWheels(Graphics g, int _startPosX, int _startPosY){ public void DrawWheels(Graphics g, int _startPosX, int _startPosY){
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.fillOval(10 + _startPosX, 42 + _startPosY, 20, 20); g.fillOval(10 + _startPosX, 42 + _startPosY, 20, 20);
@ -44,8 +49,8 @@ public class DrawingWheelsCombination implements IOrnamentForm {
DrawWheels(g,_startPosX, _startPosY); DrawWheels(g,_startPosX, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY); DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3); DrawSuspension(g, _startPosX, _startPosY);
g.fillRect(_startPosX + 101, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 73, _startPosY);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12); CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 105, _startPosY + 12); CaterpillarStar(g,_startPosX + 105, _startPosY + 12);
@ -56,9 +61,9 @@ public class DrawingWheelsCombination implements IOrnamentForm {
DrawWheels(g,_startPosX + 50, _startPosY); DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY); DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3); DrawSuspension(g, _startPosX, _startPosY);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 24, _startPosY);
g.fillRect(_startPosX + 100, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 73, _startPosY);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12); CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 55, _startPosY + 12); CaterpillarStar(g,_startPosX + 55, _startPosY + 12);
@ -71,10 +76,10 @@ public class DrawingWheelsCombination implements IOrnamentForm {
DrawWheels(g,_startPosX + 50, _startPosY); DrawWheels(g,_startPosX + 50, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY); DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3); DrawSuspension(g, _startPosX, _startPosY);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 24, _startPosY);
g.fillRect(_startPosX + 80, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 53, _startPosY);
g.fillRect(_startPosX + 101, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 73, _startPosY);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12); CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 30, _startPosY + 12); CaterpillarStar(g,_startPosX + 30, _startPosY + 12);
@ -89,10 +94,10 @@ public class DrawingWheelsCombination implements IOrnamentForm {
DrawWheels(g,_startPosX + 75, _startPosY); DrawWheels(g,_startPosX + 75, _startPosY);
DrawWheels(g,_startPosX + 100, _startPosY); DrawWheels(g,_startPosX + 100, _startPosY);
g.fillRect(_startPosX + 27, _startPosY + 50, 10, 3); DrawSuspension(g, _startPosX, _startPosY);
g.fillRect(_startPosX + 51, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 24, _startPosY);
g.fillRect(_startPosX + 80, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 53, _startPosY);
g.fillRect(_startPosX + 101, _startPosY + 50, 10, 3); DrawSuspension(g,_startPosX + 73, _startPosY);
CaterpillarStar(g,_startPosX + 5, _startPosY + 12); CaterpillarStar(g,_startPosX + 5, _startPosY + 12);
CaterpillarStar(g,_startPosX + 30, _startPosY + 12); CaterpillarStar(g,_startPosX + 30, _startPosY + 12);

View File

@ -3,9 +3,6 @@ import java.awt.*;
public class EntityArmoVehicle { public class EntityArmoVehicle {
public int Speed; public int Speed;
public double Weight; public double Weight;
public double getWeight() {
return Weight;
}
public Color BodyColor; public Color BodyColor;
public double Step; public double Step;
public int numWheel; public int numWheel;
@ -15,6 +12,6 @@ public class EntityArmoVehicle {
Weight = weight; Weight = weight;
BodyColor = bodyColor; BodyColor = bodyColor;
numWheel = _numWheel; numWheel = _numWheel;
Step = (double) Speed * 300 / Weight; Step = (double) Speed * 200 / Weight;
} }
} }

View File

@ -1,112 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormTank">
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="5" column-count="6" 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="980" height="560"/>
</constraints>
<properties>
<minimumSize width="960" height="560"/>
<preferredSize width="960" height="560"/>
</properties>
<border type="none"/>
<children>
<hspacer id="f9ba0">
<constraints>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="ce5ea">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="ac2ff" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
</component>
<component id="b1382" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
</component>
<component id="8b2ff" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<hideActionText value="true"/>
<horizontalTextPosition value="0"/>
<text value=""/>
<verticalAlignment value="0"/>
</properties>
</component>
<component id="25771" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="4" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<horizontalTextPosition value="0"/>
<text value=""/>
</properties>
</component>
<component id="aefae" class="javax.swing.JComboBox" binding="comboBoxStrategy" custom-create="true">
<constraints>
<grid row="0" column="3" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="c79a6" class="javax.swing.JButton" binding="buttonStep">
<constraints>
<grid row="1" column="3" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
<component id="d1f31" class="javax.swing.JButton" binding="buttonCreateVehicle">
<constraints>
<grid row="3" column="1" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать бронемашину"/>
</properties>
</component>
<component id="35f01" class="javax.swing.JButton" binding="buttonCreateTank">
<constraints>
<grid row="3" column="0" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать Танк"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -1,149 +1,198 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.Random; import java.util.Random;
public class FormTank { public class FormTank {
DrawingArmoVehicle _drawingTank; private DrawingArmoVehicle _drawingArmoVehicle;
AbstractStrategy _abstractStrategy; private AbstractStrategy abstractStrategy;
private JButton buttonCreateTank; Canvas canv;
private JPanel pictureBox; static int pictureBoxWidth = 980;
private JButton buttonDown; static int pictureBoxHeight = 560;
private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonCreateVehicle;
private JComboBox comboBoxStrategy;
private JButton buttonStep;
public JPanel getPictureBox() { public void Draw() {
return pictureBox; canv.repaint();
} }
public FormTank() { public FormTank() {
buttonUp.setName("buttonUp"); JFrame Frame = new JFrame("Tank");
buttonDown.setName("buttonDown"); JButton buttonCreateArmoVehicle = new JButton("Создать Бронемашину");
buttonLeft.setName("buttonLeft"); JButton buttonCreateTank = new JButton("Создать Танк");
buttonRight.setName("buttonRight"); JButton buttonStrategysStep = new JButton("Шаг");
JComboBox ComboBoxStrategy = new JComboBox(new String[]{"к центру", "к краю формочки"});
ImageIcon iconUp = new ImageIcon("Resources/KeyUp.png"); Icon iconUp = new ImageIcon("Resources/KeyUp.png");
buttonUp.setIcon(iconUp); JButton up = new JButton(iconUp);
ImageIcon iconDown = new ImageIcon("Resources/KeyDown.png"); up.setName("up");
buttonDown.setIcon(iconDown);
ImageIcon iconRight = new ImageIcon("Resources/KeyRight.png");
buttonRight.setIcon(iconRight);
ImageIcon iconLeft = new ImageIcon("Resources/KeyLeft.png");
buttonLeft.setIcon(iconLeft);
buttonCreateTank.addActionListener(e -> { Icon iconDown = new ImageIcon("Resources/KeyDown.png");
Random random = new Random(); JButton down = new JButton(iconDown);
_drawingTank = new DrawingTank( down.setName("down");
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextInt(2, 6),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
true,
true,
true,
pictureBox.getWidth(),
pictureBox.getHeight(),
true,
true
);
_drawingTank.SetWheels(random.nextInt(2, 6)); Icon iconRight = new ImageIcon("Resources/KeyRight.png");
_drawingTank.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); JButton right = new JButton(iconRight);
right.setName("right");
Draw(); Icon iconLeft = new ImageIcon("Resources/KeyLeft.png");
}); JButton left = new JButton(iconLeft);
left.setName("left");
buttonCreateVehicle.addActionListener(e -> { buttonStrategysStep.addActionListener(
Random random = new Random(); new ActionListener() {
_drawingTank = new DrawingArmoVehicle( public void actionPerformed(ActionEvent e) {
random.nextInt(100, 300), if (_drawingArmoVehicle == null) {
random.nextInt(1000, 3000), return;
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), }
random.nextInt(2, 6), if (ComboBoxStrategy.isEnabled()) {
pictureBox.getWidth(), switch (ComboBoxStrategy.getSelectedIndex()) {
pictureBox.getHeight() case 0:
); abstractStrategy = new MoveToCenter();
break;
case 1:
abstractStrategy = new MoveToBorder();
break;
default:
abstractStrategy = null;
break;
}
_drawingTank.SetWheels(random.nextInt(2, 6)); if (abstractStrategy == null) {
_drawingTank.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); return;
}
Draw(); abstractStrategy.SetData(new
}); DrawingObjectTank(_drawingArmoVehicle), pictureBoxWidth, pictureBoxHeight);
ComboBoxStrategy.setEnabled(false);
buttonStep.addActionListener(e -> { }
if (_drawingTank == null) if (abstractStrategy == null) {
return; return;
if (comboBoxStrategy.isEnabled()) { }
_abstractStrategy = null; abstractStrategy.MakeStep();
int comboBoxStrategySelectedIndex = comboBoxStrategy.getSelectedIndex(); Draw();
if (comboBoxStrategySelectedIndex == 0) { if (abstractStrategy.GetStatus() == Status.Finish) {
_abstractStrategy = new MoveToCenter(); ComboBoxStrategy.setEnabled(true);
} else if (comboBoxStrategySelectedIndex == 1) { abstractStrategy = null;
_abstractStrategy = new MoveToBorder(); }
}
} }
if (_abstractStrategy == null) );
buttonCreateTank.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_drawingArmoVehicle = new DrawingTank(
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, 6),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
true,
true,
true,
pictureBoxWidth,
pictureBoxHeight,
true
);
_drawingArmoVehicle.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingArmoVehicle = _drawingArmoVehicle;
Draw();
}
}
);
buttonCreateArmoVehicle.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_drawingArmoVehicle = new DrawingArmoVehicle(
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);
_drawingArmoVehicle.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingArmoVehicle = _drawingArmoVehicle;
Draw();
}
}
);
ActionListener actioListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (_drawingArmoVehicle == null) {
return; return;
_abstractStrategy.SetData(new DrawingObjectTank(_drawingTank), pictureBox.getWidth(), pictureBox.getHeight()); }
comboBoxStrategy.setEnabled(false); switch (((JButton) (e.getSource())).getName()) {
case "up":
_drawingArmoVehicle.MoveTransport(Direction.Up);
break;
case "down":
_drawingArmoVehicle.MoveTransport(Direction.Down);
break;
case "left":
_drawingArmoVehicle.MoveTransport(Direction.Left);
break;
case "right":
_drawingArmoVehicle.MoveTransport(Direction.Right);
break;
}
Draw();
} }
if (_abstractStrategy == null)
return;
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish) {
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
});
ActionListener buttonMoveClickedListener = e -> {
String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) {
case ("buttonUp") -> {
_drawingTank.MoveTransport(Direction.Up);
}
case ("buttonDown") -> {
_drawingTank.MoveTransport(Direction.Down);
}
case ("buttonLeft") -> {
_drawingTank.MoveTransport(Direction.Left);
}
case ("buttonRight") -> {
_drawingTank.MoveTransport(Direction.Right);
}
}
Draw();
}; };
buttonUp.addActionListener(buttonMoveClickedListener); up.addActionListener(actioListener);
buttonDown.addActionListener(buttonMoveClickedListener); down.addActionListener(actioListener);
buttonLeft.addActionListener(buttonMoveClickedListener); left.addActionListener(actioListener);
buttonRight.addActionListener(buttonMoveClickedListener); right.addActionListener(actioListener);
}
public void Draw() { Frame.setSize(1000, 600);
if (_drawingTank == null) { Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return; Frame.setLayout(null);
} canv = new Canvas();
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
Graphics g = pictureBox.getGraphics(); buttonCreateArmoVehicle.setBounds(5, 500, 170, 40);
pictureBox.paint(g); buttonCreateTank.setBounds(185, 500, 170, 40);
_drawingTank.DrawTransport((Graphics2D)g);
}
private void createUIComponents() { up.setBounds(900, 480, 40, 40);
String[] strategiesList = { down.setBounds(900, 520, 40, 40);
"Движение к центру", left.setBounds(860, 520, 40, 40);
"Движение к краю формы" right.setBounds(940, 520, 40, 40);
};
comboBoxStrategy = new JComboBox(strategiesList); ComboBoxStrategy.setBounds(pictureBoxWidth - 150, 20, 150, 20);
buttonStrategysStep.setBounds(pictureBoxWidth - 150, 45, 150, 20);
Frame.add(canv);
Frame.add(buttonCreateArmoVehicle);
Frame.add(buttonCreateTank);
Frame.add(up);
Frame.add(down);
Frame.add(left);
Frame.add(right);
Frame.add(ComboBoxStrategy);
Frame.add(buttonStrategysStep);
Frame.setVisible(true);
} }
} }
class Canvas extends JComponent {
public DrawingArmoVehicle _drawingArmoVehicle;
public Canvas() {
}
public void paintComponent(Graphics g) {
if (_drawingArmoVehicle == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingArmoVehicle.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -1,17 +0,0 @@
import javax.swing.*;
public class FrameTank extends JFrame {
private FormTank _formTank;
public FrameTank() {
super();
setTitle("Танк");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formTank = new FormTank();
setContentPane(_formTank.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
setVisible(true);
}
}

View File

@ -1,5 +1,5 @@
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
new FrameTank(); new FormTank();
} }
} }

View File

@ -1,5 +1,5 @@
public class MoveToBorder extends AbstractStrategy{ public class MoveToBorder extends AbstractStrategy{
protected boolean IsTargetDestinaion() { protected boolean IsTargetDestination() {
var objParams = GetObjectParameters(); var objParams = GetObjectParameters();
if (objParams == null) { if (objParams == null) {
return false; return false;
@ -33,4 +33,4 @@ public class MoveToBorder extends AbstractStrategy{
} }
} }
} }
} }

View File

@ -1,13 +1,13 @@
public class MoveToCenter extends AbstractStrategy { public class MoveToCenter extends AbstractStrategy {
protected boolean IsTargetDestinaion() { protected boolean IsTargetDestination() {
var objParams = GetObjectParameters(); var objParams = GetObjectParameters();
if (objParams == null) { if (objParams == null) {
return false; return false;
} }
return return
Math.abs(objParams.ObjectMiddleHorizontal() - FieldWidth / 2) <= GetStep() Math.abs(objParams.ObjectMiddleHorizontal() - FieldWidth / 2) <= GetStep()
&& &&
Math.abs(objParams.ObjectMiddleVertical() - FieldHeight / 2) <= GetStep(); Math.abs(objParams.ObjectMiddleVertical() - FieldHeight / 2) <= GetStep();
} }
protected void MoveToTarget() { protected void MoveToTarget() {
@ -32,4 +32,4 @@ public class MoveToCenter extends AbstractStrategy {
} }
} }
} }
} }

View File

@ -41,4 +41,4 @@ public class ObjectParameters {
_width = width; _width = width;
_height = height; _height = height;
} }
} }