lab2 ready

This commit is contained in:
Maxim Boyko 2024-03-28 10:46:56 +04:00
parent 738d5f9a46
commit 64bbb4ea6f
22 changed files with 734 additions and 267 deletions

View File

@ -1,6 +0,0 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -1,184 +0,0 @@
import java.awt.*;
import java.util.Random;
public class DrawningElectroTrans {
private EntityElectroTrans entityElectroTrans;
public EntityElectroTrans getEntityElectroTrans() {
return entityElectroTrans;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
private Integer _startPosX;
private Integer _startPosY;
private final int _drawingElectroTransWidth = 120;
private final int _drawingElectroTransHeight = 70;
public DrawningElectroTransWheels drawningElectroTransWheels;
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean floaters, boolean sail) {
entityElectroTrans = new EntityElectroTrans();
entityElectroTrans.Init(speed, weight, bodyColor, additionalColor, floaters, sail);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
_pictureHeight = null;
drawningElectroTransWheels = new DrawningElectroTransWheels();
Random random = new Random();
int paddlesCount = random.nextInt(2, 5);
drawningElectroTransWheels.setEnumNumber(paddlesCount);
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;
_startPosX = x;
_startPosY = y;
if (_drawingElectroTransWidth + x > _pictureWidth || x < 0) {
_startPosX = 0;
}
if (_drawingElectroTransHeight + y > _pictureHeight || y < 0) {
_startPosY = 0;
}
}
public boolean setPictureSize(int width, int height) {
if (_drawingElectroTransHeight > height || _drawingElectroTransWidth > width)
return false;
_pictureHeight = height;
_pictureWidth = width;
if (_startPosX != null && _startPosY != null) {
if (_startPosX + _drawingElectroTransWidth > width)
_startPosX = width - _drawingElectroTransWidth;
if (_startPosY + _drawingElectroTransHeight > height)
_startPosY = height - _drawingElectroTransHeight;
}
return true;
}
public boolean moveTransport(DirectionType direction) {
if (entityElectroTrans == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case Left:
if (_startPosX - entityElectroTrans.Step() > 0)
_startPosX -= (int) entityElectroTrans.Step();
return true;
case Up:
if (_startPosY - entityElectroTrans.Step() > 0)
_startPosY -= (int) entityElectroTrans.Step();
return true;
case Right:
if (_startPosX + entityElectroTrans.Step() < _pictureWidth - _drawingElectroTransWidth)
_startPosX += (int) entityElectroTrans.Step();
return true;
case Down:
if (_startPosY + entityElectroTrans.Step() < _pictureHeight - _drawingElectroTransHeight)
_startPosY += (int) entityElectroTrans.Step();
return true;
default:
return false;
}
}
public void drawElectroTrans(Graphics g) {
if (entityElectroTrans == null || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
Point[] electroTransBorders = new Point[]{
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 10, _startPosY + 10),
new Point(_startPosX + 70, _startPosY + 10),
new Point(_startPosX + 80, _startPosY + 30),
new Point(_startPosX + 80, _startPosY + 50),
new Point(_startPosX, _startPosY + 50),
};
Polygon electroTransPolygon = new Polygon();
for (Point point : electroTransBorders)
electroTransPolygon.addPoint(point.x, point.y);
g2d.setColor(entityElectroTrans.getBodyColor());
g2d.drawPolygon(electroTransPolygon);
Point[] electroTransGlass = new Point[]{
new Point(_startPosX + 2, _startPosY + 30),
new Point(_startPosX + 10, _startPosY + 13),
new Point(_startPosX + 70, _startPosY + 13),
new Point(_startPosX + 78, _startPosY + 30),
};
Polygon electroTransGlassPolygon = new Polygon();
for (Point point : electroTransGlass)
electroTransGlassPolygon.addPoint(point.x, point.y);
g2d.setColor(entityElectroTrans.getBodyColor());
g2d.drawPolygon(electroTransGlassPolygon);
Point[] electroTransHorns;
if (entityElectroTrans.getHorns()) {
electroTransHorns = new Point[]{
new Point(_startPosX + 40, _startPosY + 10),
new Point(_startPosX + 20, _startPosY),
new Point(_startPosX + 60, _startPosY),
};
} else {
electroTransHorns = new Point[]{
new Point(_startPosX + 40, _startPosY + 7),
new Point(_startPosX + 20, _startPosY + 7),
new Point(_startPosX + 60, _startPosY + 7),
};
}
Polygon electroTransHornsPolygon = new Polygon();
for (Point point : electroTransHorns)
electroTransHornsPolygon.addPoint(point.x, point.y);
g2d.setColor(entityElectroTrans.getAdditionalColor());
g2d.drawPolygon(electroTransHornsPolygon);
if (entityElectroTrans.getBattery()) {
Point[] electroTransBattery = new Point[]{
new Point(_startPosX + 25, _startPosY + 32),
new Point(_startPosX + 25, _startPosY + 36),
new Point(_startPosX + 22, _startPosY + 36),
new Point(_startPosX + 22, _startPosY + 40),
new Point(_startPosX + 25, _startPosY + 40),
new Point(_startPosX + 25, _startPosY + 46),
new Point(_startPosX + 58, _startPosY + 46),
new Point(_startPosX + 58, _startPosY + 32),
};
Polygon electroTransBatteryPolygon = new Polygon();
for (Point point : electroTransBattery)
electroTransBatteryPolygon.addPoint(point.x, point.y);
g2d.setColor(entityElectroTrans.getAdditionalColor());
g2d.fillPolygon(electroTransBatteryPolygon);
}
drawningElectroTransWheels.drawElectroTransWheels(g, entityElectroTrans.getAdditionalColor(), _startPosX, _startPosY);
}
}

View File

@ -0,0 +1,71 @@
package Drawnings;
import Entities.EntityElectroTrans;
import java.awt.*;
public class DrawningElectroTrans extends DrawningTrans {
private EntityElectroTrans entityElectroTrans;
public DrawningElectroTrans(int speed, float weight, Color bodyColor, int paddlesType, Color additionalColor, boolean horns, boolean battery) {
super(speed, weight, bodyColor, paddlesType, 110, 60);
entityElectroTrans = new EntityElectroTrans(speed, weight, bodyColor, additionalColor, horns, battery);
}
public void drawTrans(Graphics g) {
if (entityElectroTrans == null || _startPosX == null || _startPosY == null) {
return;
}
super.drawTrans(g);
Graphics2D g2d = (Graphics2D) g;
Point[] electroTransHorns;
if (entityElectroTrans.getHorns()) {
electroTransHorns = new Point[]{
new Point(_startPosX + 40, _startPosY + 10),
new Point(_startPosX + 20, _startPosY),
new Point(_startPosX + 60, _startPosY),
};
} else {
electroTransHorns = new Point[]{
new Point(_startPosX + 40, _startPosY + 7),
new Point(_startPosX + 20, _startPosY + 7),
new Point(_startPosX + 60, _startPosY + 7),
};
}
Polygon electroTransHornsPolygon = new Polygon();
for (Point point : electroTransHorns)
electroTransHornsPolygon.addPoint(point.x, point.y);
g2d.setColor(entityElectroTrans.getAdditionalColor());
g2d.drawPolygon(electroTransHornsPolygon);
if (entityElectroTrans.getBattery()) {
Point[] electroTransBattery = new Point[]{
new Point(_startPosX + 25, _startPosY + 32),
new Point(_startPosX + 25, _startPosY + 36),
new Point(_startPosX + 22, _startPosY + 36),
new Point(_startPosX + 22, _startPosY + 40),
new Point(_startPosX + 25, _startPosY + 40),
new Point(_startPosX + 25, _startPosY + 46),
new Point(_startPosX + 58, _startPosY + 46),
new Point(_startPosX + 58, _startPosY + 32),
};
Polygon electroTransBatteryPolygon = new Polygon();
for (Point point : electroTransBattery)
electroTransBatteryPolygon.addPoint(point.x, point.y);
g2d.setColor(entityElectroTrans.getAdditionalColor());
g2d.fillPolygon(electroTransBatteryPolygon);
}
}
}

View File

@ -0,0 +1,27 @@
package Drawnings;
import java.awt.*;
public class DrawningRectWheels implements IDrawWheels {
private WheelsCount wheelsCount;
@Override
public void setNumber(int wheelCount) {
for (WheelsCount value : WheelsCount.values()) {
if (value.getEnumNumber() == wheelCount) {
wheelsCount = value;
return;
}
}
}
@Override
public void drawWheels(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int wheelDistance = 100 / wheelsCount.getEnumNumber();
for (int i = 0; i < wheelsCount.getEnumNumber(); i++) {
g2d.drawRect(_startX + 5 + i * wheelDistance, _startY + 46, 8, 8);
}
}
}

View File

@ -0,0 +1,149 @@
package Drawnings;
import Entities.*;
import MovementStrategy.*;
import java.awt.*;
import java.util.Random;
public class DrawningTrans {
private final EntityTrans entityTran;
public EntityTrans getEntityTrans() {
return entityTran;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
protected Integer _startPosX;
protected Integer _startPosY;
private int _drawingTransWidth = 110;
private int _drawingTransHeight = 60;
public int GetPosX(){return _startPosX;}
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _drawingTransWidth;}
public int GetHeight(){return _drawingTransHeight;}
private IDrawWheels drawWheels;
public DrawningTrans(int speed, float weight, Color bodyColor, int wheelsType) {
entityTran = new EntityTrans(speed, weight, bodyColor);
_startPosY = null;
_startPosX = null;
_pictureWidth = null;
_pictureHeight = null;
switch (wheelsType) {
case 0:
drawWheels = new DrawningWheels();
break;
case 1:
drawWheels = new DrawningTriangleWheels();
break;
case 2:
drawWheels = new DrawningRectWheels();
break;
}
Random random = new Random();
int wheelsCount = random.nextInt(2, 4);
System.out.print(wheelsCount);
drawWheels.setNumber(wheelsCount);
}
protected DrawningTrans(int speed, float weight, Color bodyColor, int wheelsType, int transWidth, int transHeight) {
this(speed, weight, bodyColor, wheelsType);
_drawingTransHeight = transHeight;
_drawingTransWidth = transWidth;
}
public void setPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null)
return;
_startPosX = x;
_startPosY = y;
if (_drawingTransWidth + x > _pictureWidth || x < 0) {
_startPosX = 0;
}
if (_drawingTransHeight + y > _pictureHeight || y < 0) {
_startPosY = 0;
}
}
public boolean setPictureSize(int width, int height) {
if (_drawingTransHeight > height || _drawingTransWidth > width)
return false;
_pictureHeight = height;
_pictureWidth = width;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX + _drawingTransWidth > width)
_startPosX = width - _drawingTransWidth;
if (_startPosY + _drawingTransHeight > height)
_startPosY = height - _drawingTransHeight;
}
return true;
}
public boolean moveTransport(MovementDirection direction) {
if (entityTran == null || _pictureWidth == null || _pictureHeight == null)
return false;
switch (direction) {
case MovementDirection.Left:
if (_startPosX - entityTran.getStep() > 0)
_startPosX -= (int) entityTran.getStep();
return true;
case MovementDirection.Up:
if (_startPosY - entityTran.getStep() > 0)
_startPosY -= (int) entityTran.getStep();
return true;
case MovementDirection.Right:
if (_startPosX + entityTran.getStep() < _pictureWidth - _drawingTransWidth)
_startPosX += (int) entityTran.getStep();
return true;
case MovementDirection.Down:
if (_startPosY + entityTran.getStep() < _pictureHeight - _drawingTransHeight)
_startPosY += (int) entityTran.getStep();
return true;
default:
return false;
}
}
public void drawTrans(Graphics g) {
if (entityTran == null || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
Point[] electroTransBorders = new Point[]{
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 10, _startPosY + 10),
new Point(_startPosX + 70, _startPosY + 10),
new Point(_startPosX + 80, _startPosY + 30),
new Point(_startPosX + 80, _startPosY + 50),
new Point(_startPosX, _startPosY + 50),
};
Polygon electroTransPolygon = new Polygon();
for (Point point : electroTransBorders)
electroTransPolygon.addPoint(point.x, point.y);
g2d.setColor(entityTran.getBodyColor());
g2d.drawPolygon(electroTransPolygon);
Point[] electroTransGlass = new Point[]{
new Point(_startPosX + 2, _startPosY + 30),
new Point(_startPosX + 10, _startPosY + 13),
new Point(_startPosX + 70, _startPosY + 13),
new Point(_startPosX + 78, _startPosY + 30),
};
Polygon electroTransGlassPolygon = new Polygon();
for (Point point : electroTransGlass)
electroTransGlassPolygon.addPoint(point.x, point.y);
g2d.setColor(entityTran.getBodyColor());
g2d.drawPolygon(electroTransGlassPolygon);
drawWheels.drawWheels(g2d, entityTran.getBodyColor(), _startPosX, _startPosY);
}
}

View File

@ -0,0 +1,29 @@
package Drawnings;
import java.awt.*;
public class DrawningTriangleWheels implements IDrawWheels {
private WheelsCount wheelsCount;
@Override
public void setNumber(int wheelCount) {
for (WheelsCount value : WheelsCount.values()) {
if (value.getEnumNumber() == wheelCount) {
wheelsCount = value;
return;
}
}
}
@Override
public void drawWheels(Graphics2D g2d, Color color, int _startX, int _startY) {
g2d.setColor(color);
g2d.setStroke(new BasicStroke(4));
int wheelDistance = 100 / wheelsCount.getEnumNumber();
for (int i = 0; i < wheelsCount.getEnumNumber(); i++) {
g2d.drawLine(_startX + 5 + i * wheelDistance - 4, (int) _startY + 44, _startX + 5 + i * wheelDistance + 4, (int) _startY + 44);
g2d.drawLine(_startX + 5 + i * wheelDistance + 4, (int) _startY + 44, _startX + 5 + i * wheelDistance, (int) _startY + 48);
g2d.drawLine(_startX + 5 + i * wheelDistance, (int) _startY + 48, _startX + 5 + i * wheelDistance - 4, (int) _startY + 44);
}
}
}

View File

@ -1,9 +1,12 @@
package Drawnings;
import java.awt.*; import java.awt.*;
public class DrawningElectroTransWheels { public class DrawningWheels implements IDrawWheels {
private WheelsCount wheelsCount; private WheelsCount wheelsCount;
public void setEnumNumber(int wheelCount) { @Override
public void setNumber(int wheelCount) {
for (WheelsCount value : WheelsCount.values()) { for (WheelsCount value : WheelsCount.values()) {
if (value.getEnumNumber() == wheelCount) { if (value.getEnumNumber() == wheelCount) {
wheelsCount = value; wheelsCount = value;
@ -12,13 +15,13 @@ public class DrawningElectroTransWheels {
} }
} }
public void drawElectroTransWheels(Graphics g, Color color, float startPosX, float startPosY) { @Override
public void drawWheels(Graphics2D g2d, Color color, int _startX, int _startY) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(color); g2d.setColor(color);
g2d.setStroke(new BasicStroke(4)); g2d.setStroke(new BasicStroke(4));
int wheelDistance = 100 / wheelsCount.getEnumNumber();
for (int i = 0; i < wheelsCount.getEnumNumber(); i++) { for (int i = 0; i < wheelsCount.getEnumNumber(); i++) {
g2d.drawOval((int) startPosX + (70 / 4) * (i + 1) + -4, (int) startPosY + 46, 8, 8); g2d.drawOval(_startX + 5 + i * wheelDistance, _startY + 46, 8, 8);
} }
} }
} }

View File

@ -0,0 +1,8 @@
package Drawnings;
import java.awt.*;
public interface IDrawWheels {
void setNumber(int x);
void drawWheels(Graphics2D graphics2D, Color color, int _startX, int _startY);
}

View File

@ -1,3 +1,5 @@
package Drawnings;
public enum WheelsCount { public enum WheelsCount {
Two(2), Two(2),
Three(3), Three(3),

View File

@ -1,7 +1,17 @@
package Entities;
import java.awt.*; import java.awt.*;
public class EntityElectroTrans { public class EntityElectroTrans extends EntityTrans {
private int Speed; private int Speed;
public EntityElectroTrans(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean battery) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Horns = horns;
Battery = battery;
}
public int getSpeed() { public int getSpeed() {
return Speed; return Speed;
} }
@ -30,12 +40,4 @@ public class EntityElectroTrans {
public boolean getBattery() { public boolean getBattery() {
return Battery; return Battery;
} }
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean battery) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Horns = horns;
Battery = battery;
}
} }

View File

@ -0,0 +1,28 @@
package Entities;
import java.awt.*;
public class EntityTrans {
private int Speed;
public int getSpeed() {
return Speed;
}
private double Weight;
public double getWeight() {
return Weight;
}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
private double Step;
public double getStep() {
return Speed*100/Weight;
}
public EntityTrans(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -8,61 +8,55 @@
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <children>
<grid id="1ff9a" binding="PictureBox" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="1ff9a" binding="PictureBox" layout-manager="GridLayoutManager" row-count="6" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties/> <properties>
<toolTipText value=""/>
</properties>
<border type="none"/> <border type="none"/>
<children> <children>
<component id="a8c2" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="2" column="0" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<vspacer id="7b40c"> <vspacer id="7b40c">
<constraints> <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"/> <grid row="2" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<preferred-size width="88" height="14"/>
</grid>
</constraints> </constraints>
</vspacer> </vspacer>
<component id="3dc0" class="javax.swing.JButton" binding="buttonDown"> <hspacer id="e0b21">
<constraints> <constraints>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"> <grid row="4" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" 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>
<icon value="arrowDown.png"/>
<text value=""/>
</properties>
</component>
<hspacer id="abaeb">
<constraints>
<grid row="2" column="1" row-span="2" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</hspacer> </hspacer>
<component id="ccb2a" class="javax.swing.JButton" binding="buttonUp"> <component id="a8c2" class="javax.swing.JButton" binding="buttonCreateElectroTrans">
<constraints> <constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"> <grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/> <minimum-size width="150" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="150" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="150" height="30"/>
</grid> </grid>
</constraints> </constraints>
<properties> <properties>
<icon value="arrowUp.png"/> <text value="Создать электропоезд"/>
<text value=""/> </properties>
</component>
<component id="79b3e" class="javax.swing.JButton" binding="buttonCreateTrans">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="30"/>
<preferred-size width="150" height="30"/>
<maximum-size width="150" height="30"/>
</grid>
</constraints>
<properties>
<text value="Создать поезд"/>
</properties> </properties>
</component> </component>
<component id="fe25b" class="javax.swing.JButton" binding="buttonLeft"> <component id="fe25b" class="javax.swing.JButton" binding="buttonLeft">
<constraints> <constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"> <grid row="5" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -73,9 +67,17 @@
<text value=""/> <text value=""/>
</properties> </properties>
</component> </component>
<component id="3eabd" class="javax.swing.JButton" binding="buttonStrategyStep">
<constraints>
<grid row="1" column="3" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
<component id="86d84" class="javax.swing.JButton" binding="buttonRight"> <component id="86d84" class="javax.swing.JButton" binding="buttonRight">
<constraints> <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"> <grid row="5" 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"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -86,6 +88,42 @@
<text value=""/> <text value=""/>
</properties> </properties>
</component> </component>
<component id="3dc0" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="5" 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>
<icon value="arrowDown.png"/>
<text value=""/>
</properties>
</component>
<component id="ccb2a" class="javax.swing.JButton" binding="buttonUp">
<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>
<icon value="arrowUp.png"/>
<text value=""/>
</properties>
</component>
<component id="988c8" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="2" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="9" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<actionCommand value=""/>
<doubleBuffered value="false"/>
<toolTipText value=""/>
</properties>
</component>
</children> </children>
</grid> </grid>
</children> </children>

View File

@ -1,3 +1,7 @@
import Drawnings.DrawningElectroTrans;
import Drawnings.DrawningTrans;
import MovementStrategy.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@ -7,16 +11,46 @@ import java.util.Random;
import java.util.List; import java.util.List;
public class FormElectroTrans extends JFrame { public class FormElectroTrans extends JFrame {
protected DrawningElectroTrans _drawningElectroTrans = new DrawningElectroTrans(); protected DrawningTrans _drawningTrans;
JPanel PanelWrapper; JPanel PanelWrapper;
private JPanel PictureBox; private JPanel PictureBox;
private JButton buttonCreate; private JButton buttonCreateElectroTrans;
private JButton buttonCreateTrans;
private JButton buttonRight; private JButton buttonRight;
private JButton buttonDown; private JButton buttonDown;
private JButton buttonLeft; private JButton buttonLeft;
private JButton buttonUp; private JButton buttonUp;
private JComboBox comboBoxStrategy;
private JButton buttonStrategyStep;
private AbstractStrategy _strategy;
private List<JComponent> controls; private List<JComponent> controls;
private void createObject(String type) {
Random random = new Random();
switch (type) {
case "Drawnings.DrawningTrans":
_drawningTrans = new DrawningTrans(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3));
break;
case "Drawnings.DrawningElectroTrans":
_drawningTrans = new DrawningElectroTrans(random.nextInt(70 - 30) + 30, random.nextInt(500 - 100) + 100,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean());
break;
default:
return;
}
_drawningTrans.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
_drawningTrans.setPosition(random.nextInt(25, 100),
random.nextInt(25, 100));
_strategy = null;
comboBoxStrategy.setEnabled(true);
Draw();
}
public FormElectroTrans() { public FormElectroTrans() {
buttonUp.setName("buttonUp"); buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown"); buttonDown.setName("buttonDown");
@ -25,25 +59,19 @@ public class FormElectroTrans extends JFrame {
InitializeControlsRepaintList(); InitializeControlsRepaintList();
buttonCreate.addActionListener(new ActionListener() { buttonCreateElectroTrans.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
_drawningElectroTrans = new DrawningElectroTrans(); createObject("Drawnings.DrawningElectroTrans");
Random random = new Random();
_drawningElectroTrans.Init(random.nextInt(30, 100),
random.nextInt(100, 500),
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() );
_drawningElectroTrans.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
_drawningElectroTrans.setPosition(random.nextInt(25, 100),
random.nextInt(25, 100));
Draw();
} }
}); });
buttonCreateTrans.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createObject("Drawnings.DrawningTrans");
}
});
ActionListener buttonMoveClickedListener = new ActionListener() { ActionListener buttonMoveClickedListener = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -52,26 +80,25 @@ public class FormElectroTrans extends JFrame {
switch (buttonName) { switch (buttonName) {
case "buttonUp": { case "buttonUp": {
result = _drawningElectroTrans.moveTransport(DirectionType.Up); result = _drawningTrans.moveTransport(MovementDirection.Up);
} }
break; break;
case "buttonDown": { case "buttonDown": {
result = _drawningElectroTrans.moveTransport(DirectionType.Down); result = _drawningTrans.moveTransport(MovementDirection.Down);
} }
break; break;
case "buttonLeft": { case "buttonLeft": {
result = _drawningElectroTrans.moveTransport(DirectionType.Left); result = _drawningTrans.moveTransport(MovementDirection.Left);
} }
break; break;
case "buttonRight": { case "buttonRight": {
result = _drawningElectroTrans.moveTransport(DirectionType.Right); result = _drawningTrans.moveTransport(MovementDirection.Right);
} }
break; break;
} }
if (result) if (result)
Draw(); Draw();
} }
}; };
buttonRight.addActionListener(buttonMoveClickedListener); buttonRight.addActionListener(buttonMoveClickedListener);
@ -79,9 +106,49 @@ public class FormElectroTrans extends JFrame {
buttonLeft.addActionListener(buttonMoveClickedListener); buttonLeft.addActionListener(buttonMoveClickedListener);
buttonUp.addActionListener(buttonMoveClickedListener); buttonUp.addActionListener(buttonMoveClickedListener);
comboBoxStrategy.addItem("К центру");
comboBoxStrategy.addItem("К краю");
buttonStrategyStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawningTrans == null)
return;
if (comboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) {
case 0:
_strategy = new MoveToCenter();
break;
case 1:
_strategy = new MoveToBorder();
break;
default:
_strategy = null;
break;
}
if (_strategy == null) {
return;
}
_strategy.SetData(new MoveableTrans(_drawningTrans), PictureBox.getWidth(), PictureBox.getHeight());
}
if (_strategy == null) {
return;
}
_strategy.MakeStep();
Draw();
comboBoxStrategy.setEnabled(false);
if (_strategy.GetStatus() == StrategyStatus.Finish) {
comboBoxStrategy.setEnabled(true);
_strategy = null;
}
}
});
} }
private void Draw() { private void Draw() {
if (_drawningElectroTrans.getEntityElectroTrans() == null) if (_drawningTrans.getEntityTrans() == null)
return; return;
if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) { if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) {
return; return;
@ -89,10 +156,9 @@ public class FormElectroTrans extends JFrame {
Graphics g = PictureBox.getGraphics(); Graphics g = PictureBox.getGraphics();
g.setColor(PictureBox.getBackground()); g.setColor(PictureBox.getBackground());
g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight()); g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight());
_drawningElectroTrans.drawElectroTrans(g); _drawningTrans.drawTrans(g);
RepaintControls(); RepaintControls();
} }
private void RepaintControls() { private void RepaintControls() {
for (JComponent control : controls) { for (JComponent control : controls) {
@ -103,11 +169,14 @@ public class FormElectroTrans extends JFrame {
private void InitializeControlsRepaintList() { private void InitializeControlsRepaintList() {
controls = new LinkedList<>(); controls = new LinkedList<>();
controls.add(buttonCreate); controls.add(buttonCreateElectroTrans);
controls.add(buttonCreateTrans);
controls.add(buttonUp); controls.add(buttonUp);
controls.add(buttonDown); controls.add(buttonDown);
controls.add(buttonLeft); controls.add(buttonLeft);
controls.add(buttonRight); controls.add(buttonRight);
controls.add(comboBoxStrategy);
controls.add(buttonStrategyStep);
} }

View File

@ -5,7 +5,7 @@ import javax.swing.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(false); JFrame.setDefaultLookAndFeelDecorated(false);
JFrame frame = new JFrame("Катамаран"); JFrame frame = new JFrame("Элетроаоезд");
frame.setContentPane(new FormElectroTrans().PanelWrapper); frame.setContentPane(new FormElectroTrans().PanelWrapper);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200); frame.setLocation(500, 200);

View File

@ -0,0 +1,66 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
protected int FieldWidth;
protected int FieldHeight;
public StrategyStatus GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_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); }
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
protected int GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return 0;
}
return _moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestination();
private boolean MoveTo(MovementDirection directionType)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
if (_moveableObject.TryMoveObject(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public interface IMoveableObject {
ObjectParameters GetObjectPosition();
int GetStep();
boolean TryMoveObject(MovementDirection direction);
void MoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,47 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.RightBorder() <= FieldWidth &&
objParams.RightBorder() + GetStep() >= FieldWidth &&
objParams.DownBorder() <= FieldHeight &&
objParams.DownBorder() + GetStep() >= FieldHeight;
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
int diffX = objParams.RightBorder() - FieldWidth;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.DownBorder() - FieldHeight;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,46 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy {
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return (objParams.ObjectMiddleHorizontal() - GetStep() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical() - GetStep() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2);
}
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,24 @@
package MovementStrategy;
import Drawnings.DrawningTrans;
public class MoveableTrans implements IMoveableObject {
private DrawningTrans _trans = null;
public MoveableTrans(DrawningTrans drawningTrans)
{
_trans = drawningTrans;
}
public ObjectParameters GetObjectPosition()
{
if (_trans == null || _trans.getEntityTrans() == null)
{
return null;
}
return new ObjectParameters(_trans.GetPosX(), _trans.GetPosY(), _trans.GetWidth(), _trans.GetHeight());
}
public int GetStep() { return (int) _trans.getEntityTrans().getStep(); }
public boolean TryMoveObject(MovementDirection direction) { return _trans.moveTransport(direction); }
public void MoveObject(MovementDirection direction) { _trans.moveTransport(direction); }
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public enum MovementDirection {
Up,
Down,
Left,
Right;
}

View File

@ -0,0 +1,25 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
public int LeftBorder() { return _x; }
public int TopBorder() { return _y; }
public int RightBorder() { return _x + _width; }
public int DownBorder() { return _y + _height; }
public int ObjectMiddleHorizontal () { return _x + _width / 2; }
public int ObjectMiddleVertical () { return _y + _height / 2; }
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public enum StrategyStatus {
NotInit,
InProgress,
Finish
}