Добавил стратегии
This commit is contained in:
parent
a5d0649974
commit
eaef3c6307
@ -1,58 +0,0 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingField extends JPanel {
|
||||
private final FormModernMonorail field;
|
||||
DrawingModernMonorail _monorail;
|
||||
public DrawingField(FormModernMonorail field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 =(Graphics2D)g;
|
||||
if (_monorail !=null)
|
||||
_monorail.DrawTransport(g2);
|
||||
else return;
|
||||
}
|
||||
|
||||
public void UpButtonAction(){
|
||||
if (_monorail !=null)
|
||||
_monorail.MoveTransport(DirectionType.Up);
|
||||
return;
|
||||
}
|
||||
|
||||
public void DownButtonAction(){
|
||||
if (_monorail !=null)
|
||||
_monorail.MoveTransport(DirectionType.Down);
|
||||
return;
|
||||
}
|
||||
|
||||
public void RightButtonAction(){
|
||||
if (_monorail !=null)
|
||||
_monorail.MoveTransport(DirectionType.Right);
|
||||
return;
|
||||
}
|
||||
|
||||
public void LeftButtonAction(){
|
||||
if (_monorail !=null)
|
||||
_monorail.MoveTransport(DirectionType.Left);
|
||||
return;
|
||||
}
|
||||
|
||||
public void CreateButtonAction(){
|
||||
Random rnd=new Random();
|
||||
_monorail =new DrawingModernMonorail();
|
||||
_monorail.SetPictureSize(getWidth(),getHeight());
|
||||
_monorail.Initialization(rnd.nextInt(50)+10,rnd.nextInt(100)+500,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());
|
||||
_monorail.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10,getWidth(),getHeight());
|
||||
}
|
||||
|
||||
public void ResizeField(){
|
||||
if (_monorail !=null)
|
||||
_monorail.SetPictureSize(getWidth(),getHeight());
|
||||
else return;
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package Scripts;
|
||||
|
||||
public enum CountWheels {
|
||||
Two(2),
|
||||
Three(3),
|
@ -1,3 +1,5 @@
|
||||
package Scripts;
|
||||
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
68
ProjectMonorail/src/Scripts/DrawingField.java
Normal file
68
ProjectMonorail/src/Scripts/DrawingField.java
Normal file
@ -0,0 +1,68 @@
|
||||
package Scripts;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingField extends JPanel {
|
||||
private final FormModernMonorail field;
|
||||
private DrawingMonorail _drawingMonorail;
|
||||
public DrawingField(FormModernMonorail field) {
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
public DrawingMonorail getDrawingMonorail() {return _drawingMonorail;}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 =(Graphics2D)g;
|
||||
if (_drawingMonorail !=null)
|
||||
_drawingMonorail.DrawTransport(g2);
|
||||
else return;
|
||||
}
|
||||
|
||||
public void UpButtonAction(){
|
||||
if (_drawingMonorail !=null)
|
||||
_drawingMonorail.MoveTransport(DirectionType.Up);
|
||||
return;
|
||||
}
|
||||
|
||||
public void DownButtonAction(){
|
||||
if (_drawingMonorail !=null)
|
||||
_drawingMonorail.MoveTransport(DirectionType.Down);
|
||||
return;
|
||||
}
|
||||
|
||||
public void RightButtonAction(){
|
||||
if (_drawingMonorail !=null)
|
||||
_drawingMonorail.MoveTransport(DirectionType.Right);
|
||||
return;
|
||||
}
|
||||
|
||||
public void LeftButtonAction(){
|
||||
if (_drawingMonorail !=null)
|
||||
_drawingMonorail.MoveTransport(DirectionType.Left);
|
||||
return;
|
||||
}
|
||||
|
||||
public void CreateButtonAction_Monorail(){
|
||||
Random rnd=new Random();
|
||||
_drawingMonorail = new DrawingMonorail(rnd.nextInt(50)+10,rnd.nextInt(100)+500,new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)), null, null);
|
||||
_drawingMonorail.SetPictureSize(getWidth(),getHeight());
|
||||
_drawingMonorail.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10,getWidth(),getHeight());
|
||||
}
|
||||
|
||||
public void CreateButtonAction_ModernMonorail(){
|
||||
Random rnd=new Random();
|
||||
_drawingMonorail = new DrawingModernMonorail(rnd.nextInt(50)+10,rnd.nextInt(100)+500,new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)), new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256)), rnd.nextBoolean(), true);
|
||||
_drawingMonorail.SetPictureSize(getWidth(),getHeight());
|
||||
_drawingMonorail.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10,getWidth(),getHeight());
|
||||
}
|
||||
|
||||
public void ResizeField(){
|
||||
if (_drawingMonorail !=null)
|
||||
_drawingMonorail.SetPictureSize(getWidth(),getHeight());
|
||||
else return;
|
||||
}
|
||||
}
|
124
ProjectMonorail/src/Scripts/DrawingModernMonorail.java
Normal file
124
ProjectMonorail/src/Scripts/DrawingModernMonorail.java
Normal file
@ -0,0 +1,124 @@
|
||||
package Scripts;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingModernMonorail extends DrawingMonorail {
|
||||
public DrawingModernMonorail(int speed, float weight, Color bodyColor, Color additionalColor, boolean monorailTrack, boolean cabin)
|
||||
{
|
||||
super(speed, weight, bodyColor, 190, 80);
|
||||
|
||||
_entityMonorail = new EntityModernMonorail(speed, weight, bodyColor, additionalColor, monorailTrack, cabin);
|
||||
_wheels = new DrawingWheels();
|
||||
Random rnd = new Random();
|
||||
_wheels.SetCountWheels(2 + rnd.nextInt(0, 3));
|
||||
_wheelsSeed = rnd.nextInt(0, 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (! (_entityMonorail instanceof EntityModernMonorail)) return;
|
||||
|
||||
super.DrawTransport(g);
|
||||
|
||||
EntityModernMonorail modernMonorail = (EntityModernMonorail)_entityMonorail;
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
|
||||
g.setColor(modernMonorail.getAdditionalColor());
|
||||
//Окна
|
||||
g.drawRect(_startPositionX + 14, _startPositionY + 2, 5, 6);
|
||||
g.drawRect(_startPositionX + 21, _startPositionY + 2, 5, 6);
|
||||
g.drawRect(_startPositionX + 70, _startPositionY + 2, 5, 6);
|
||||
|
||||
g.setColor(modernMonorail.getBodyColor());
|
||||
|
||||
if (modernMonorail.getMonorailTrack()) {
|
||||
if (modernMonorail.getCabin()) {
|
||||
g.fillRect(_startPositionX, _startPositionY + 35, 170, 5);
|
||||
}
|
||||
else {
|
||||
g.fillRect(_startPositionX, _startPositionY + 35, 86, 5);
|
||||
}
|
||||
}
|
||||
|
||||
//Колеса
|
||||
int[] coordinatesX = new int[_wheels.getCountWheels()];
|
||||
coordinatesX[0] = _startPositionX + 10;
|
||||
coordinatesX[1] = _startPositionX + 65;
|
||||
|
||||
if (coordinatesX.length == 4) {
|
||||
coordinatesX[2] = _startPositionX + 25;
|
||||
coordinatesX[3] = _startPositionX + 50;
|
||||
}
|
||||
else if (coordinatesX.length == 3) {
|
||||
Random rnd = new Random();
|
||||
if (_wheelsSeed == 1) {
|
||||
coordinatesX[2] = _startPositionX + 25;
|
||||
}
|
||||
else {
|
||||
coordinatesX[2] = _startPositionX + 50;
|
||||
}
|
||||
}
|
||||
|
||||
_wheels.DrawWheels(g2, coordinatesX, _startPositionY + 30, modernMonorail.getAdditionalColor());
|
||||
|
||||
if (modernMonorail.getCabin()) {
|
||||
int offset = 170;
|
||||
|
||||
g.setColor(modernMonorail.getBodyColor());
|
||||
//Кузов монорельса
|
||||
g.drawLine(_startPositionX - 8 + offset, _startPositionY + 10, _startPositionX - 13 + offset, _startPositionY);
|
||||
g.drawLine(_startPositionX - 13 + offset, _startPositionY, _startPositionX - 80 + offset, _startPositionY);
|
||||
g.drawLine(_startPositionX - 80 + offset, _startPositionY, _startPositionX - 80 + offset, _startPositionY + 25);
|
||||
g.drawLine(_startPositionX - 80 + offset, _startPositionY + 25, _startPositionX - 8 + offset, _startPositionY + 25);
|
||||
g.drawLine(_startPositionX - 8 + offset, _startPositionY + 25, _startPositionX - 8 + offset, _startPositionY + 10);
|
||||
g.drawLine(_startPositionX - 8 + offset, _startPositionY + 10, _startPositionX - 30 + offset, _startPositionY + 10);
|
||||
g.drawLine(_startPositionX - 39 + offset, _startPositionY + 10, _startPositionX - 80 + offset, _startPositionY + 10);
|
||||
|
||||
//Дверь
|
||||
g.drawRect(_startPositionX - 39 + offset, _startPositionY + 5, 9, 15);
|
||||
|
||||
//Окна
|
||||
g.setColor(modernMonorail.getAdditionalColor());
|
||||
g.drawRect(_startPositionX - 20 + offset, _startPositionY + 2, 5, 6);
|
||||
g.drawRect(_startPositionX - 27 + offset, _startPositionY + 2, 5, 6);
|
||||
g.drawRect(_startPositionX - 76 + offset, _startPositionY + 2, 5, 6);
|
||||
|
||||
//Связка монорельса
|
||||
g.setColor(_entityMonorail.getBodyColor());
|
||||
g.fillRect(_startPositionX - 87 + offset, _startPositionY + 2, 7, 22);
|
||||
|
||||
//Нижняя часть монорельса
|
||||
int[] xPoly_LeftSide = new int[]{_startPositionX + offset, _startPositionX - 8 + offset, _startPositionX - 72 + offset, _startPositionX - 36 + offset, _startPositionX - 36 + offset, _startPositionX - 15 + offset, _startPositionX - 15 + offset, _startPositionX + offset};
|
||||
int[] yPoly_LeftSide = new int[] {_startPositionY + 30, _startPositionY + 25, _startPositionY + 25, _startPositionY + 25, _startPositionY + 32, _startPositionY + 32, _startPositionY + 35, _startPositionY + 32};
|
||||
g.fillPolygon(xPoly_LeftSide, yPoly_LeftSide, xPoly_LeftSide.length);
|
||||
|
||||
int[] xPoly_RightSide = new int[]{_startPositionX - 86 + offset, _startPositionX - 80 + offset, _startPositionX - 50 + offset, _startPositionX - 50 + offset, _startPositionX - 69 + offset, _startPositionX - 79 + offset, _startPositionX - 86 + offset};
|
||||
int[] yPoly_RightSide = new int[] {_startPositionY + 30, _startPositionY + 25, _startPositionY+ 25, _startPositionY + 32, _startPositionY + 32 , _startPositionY + 35, _startPositionY + 32};
|
||||
g.fillPolygon(xPoly_RightSide, yPoly_RightSide, xPoly_RightSide.length);
|
||||
g.fillRect(_startPositionX - 80 + offset, _startPositionY + 25, 70, 3);
|
||||
|
||||
//Колеса
|
||||
coordinatesX = new int[_wheels.getCountWheels()];
|
||||
coordinatesX[0] = _startPositionX - 20 + offset;
|
||||
coordinatesX[1] = _startPositionX - 75 + offset;
|
||||
|
||||
if (coordinatesX.length == 4) {
|
||||
coordinatesX[2] = _startPositionX - 35 + offset;
|
||||
coordinatesX[3] = _startPositionX - 60 + offset;
|
||||
}
|
||||
else if (coordinatesX.length == 3) {
|
||||
Random rnd = new Random();
|
||||
if (_wheelsSeed == 1) {
|
||||
coordinatesX[2] = _startPositionX - 35 + offset;
|
||||
}
|
||||
else {
|
||||
coordinatesX[2] = _startPositionX - 60 + offset;
|
||||
}
|
||||
}
|
||||
|
||||
_wheels.DrawWheels(g2, coordinatesX , _startPositionY + 30, modernMonorail.getAdditionalColor());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +1,51 @@
|
||||
package Scripts;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingModernMonorail {
|
||||
private EntityModernMonorail _entityMonorail;
|
||||
private DrawingWheels _wheels;
|
||||
public class DrawingMonorail {
|
||||
protected EntityMonorail _entityMonorail;
|
||||
protected DrawingWheels _wheels;
|
||||
|
||||
private Integer _pictureWidth = null;
|
||||
private Integer _pictureHeight = null;
|
||||
|
||||
private int _startPositionX;
|
||||
private int _startPositionY;
|
||||
protected int _startPositionX;
|
||||
protected int _startPositionY;
|
||||
|
||||
private int _drawningMonorailWidth = 100;
|
||||
private final int _drawningMonorailHeight = 40;
|
||||
private int _drawningMonorailHeight = 80;
|
||||
|
||||
public EntityModernMonorail getMonorail() {return _entityMonorail;}
|
||||
public EntityMonorail getMonorail() {return _entityMonorail;}
|
||||
public DrawingWheels getWheels() {return _wheels;}
|
||||
private int _wheelsSeed;
|
||||
protected int _wheelsSeed;
|
||||
|
||||
public void Initialization(int speed, float weight, Color bodyColor, Color additionalColor, boolean monorailTrack, boolean cabin)
|
||||
public int GetPositionX() { return _startPositionX;}
|
||||
|
||||
public int GetPositionY() {return _startPositionY;}
|
||||
|
||||
public int GetWidth() { return _drawningMonorailWidth;}
|
||||
|
||||
public int GetHeight() { return _drawningMonorailHeight;}
|
||||
|
||||
public DrawingMonorail(int speed, float weight, Color bodyColor, Integer drawningMonorailWidth, Integer drawningMonorailHeight)
|
||||
{
|
||||
_entityMonorail = new EntityModernMonorail();
|
||||
_entityMonorail.Initialization(speed, weight, bodyColor, additionalColor, monorailTrack, cabin);
|
||||
this(drawningMonorailWidth, drawningMonorailHeight);
|
||||
|
||||
_entityMonorail = new EntityMonorail(speed, weight, bodyColor);
|
||||
_wheels = new DrawingWheels();
|
||||
Random rnd = new Random();
|
||||
if (_entityMonorail.getCabin()) _drawningMonorailWidth = 190;
|
||||
_wheels.SetCountWheels(2 + rnd.nextInt(0, 3));
|
||||
_wheelsSeed = rnd.nextInt(0, 2);
|
||||
}
|
||||
|
||||
protected DrawingMonorail(Integer drawningMonorailWidth, Integer drawningMonorailHeight) {
|
||||
if(drawningMonorailWidth == null || drawningMonorailHeight == null) return;
|
||||
|
||||
_drawningMonorailWidth = drawningMonorailWidth;
|
||||
_drawningMonorailHeight = drawningMonorailHeight;
|
||||
}
|
||||
|
||||
public boolean SetPictureSize(int width, int height)
|
||||
{
|
||||
if (_drawningMonorailWidth > width || _drawningMonorailHeight > height) return false;
|
||||
@ -108,6 +125,18 @@ public class DrawingModernMonorail {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean CanMove(DirectionType direction) {
|
||||
if (_entityMonorail == null) {
|
||||
return false;
|
||||
}
|
||||
return switch (direction) {
|
||||
case Left -> _startPositionX - _entityMonorail.Step > 0;
|
||||
case Up -> _startPositionY - _entityMonorail.Step > 0;
|
||||
case Right -> _startPositionX + _drawningMonorailWidth + _entityMonorail.Step < _pictureWidth;
|
||||
case Down -> _startPositionY + _drawningMonorailHeight + _entityMonorail.Step < _pictureHeight;
|
||||
};
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (_startPositionX < 0 || _startPositionY < 0 || _pictureHeight== null || _pictureWidth== null) return;
|
||||
@ -128,7 +157,6 @@ public class DrawingModernMonorail {
|
||||
g.drawRect(_startPositionX + 30, _startPositionY + 5, 9, 15);
|
||||
|
||||
//Окна
|
||||
g.setColor(_entityMonorail.getAdditionalColor());
|
||||
g.drawRect(_startPositionX + 14, _startPositionY + 2, 5, 6);
|
||||
g.drawRect(_startPositionX + 21, _startPositionY + 2, 5, 6);
|
||||
g.drawRect(_startPositionX + 70, _startPositionY + 2, 5, 6);
|
||||
@ -148,15 +176,6 @@ public class DrawingModernMonorail {
|
||||
|
||||
g.fillRect(_startPositionX + 8, _startPositionY + 25, 70, 3);
|
||||
|
||||
if (_entityMonorail.getMonorailTrack()) {
|
||||
if (_entityMonorail.getCabin()) {
|
||||
g.fillRect(_startPositionX, _startPositionY + 35, 170, 5);
|
||||
}
|
||||
else {
|
||||
g.fillRect(_startPositionX, _startPositionY + 35, 86, 5);
|
||||
}
|
||||
}
|
||||
|
||||
//Колеса
|
||||
int[] coordinatesX = new int[_wheels.getCountWheels()];
|
||||
coordinatesX[0] = _startPositionX + 10;
|
||||
@ -176,64 +195,6 @@ public class DrawingModernMonorail {
|
||||
}
|
||||
}
|
||||
|
||||
_wheels.DrawWheels(g2, coordinatesX, _startPositionY + 30, _entityMonorail.getAdditionalColor());
|
||||
|
||||
if (_entityMonorail.getCabin()) {
|
||||
int offset = 170;
|
||||
|
||||
//Кузов монорельса
|
||||
g.drawLine(_startPositionX - 8 + offset, _startPositionY + 10, _startPositionX - 13 + offset, _startPositionY);
|
||||
g.drawLine(_startPositionX - 13 + offset, _startPositionY, _startPositionX - 80 + offset, _startPositionY);
|
||||
g.drawLine(_startPositionX - 80 + offset, _startPositionY, _startPositionX - 80 + offset, _startPositionY + 25);
|
||||
g.drawLine(_startPositionX - 80 + offset, _startPositionY + 25, _startPositionX - 8 + offset, _startPositionY + 25);
|
||||
g.drawLine(_startPositionX - 8 + offset, _startPositionY + 25, _startPositionX - 8 + offset, _startPositionY + 10);
|
||||
g.drawLine(_startPositionX - 8 + offset, _startPositionY + 10, _startPositionX - 30 + offset, _startPositionY + 10);
|
||||
g.drawLine(_startPositionX - 39 + offset, _startPositionY + 10, _startPositionX - 80 + offset, _startPositionY + 10);
|
||||
|
||||
//Дверь
|
||||
g.drawRect(_startPositionX - 39 + offset, _startPositionY + 5, 9, 15);
|
||||
|
||||
//Окна
|
||||
g.setColor(_entityMonorail.getAdditionalColor());
|
||||
g.drawRect(_startPositionX - 20 + offset, _startPositionY + 2, 5, 6);
|
||||
g.drawRect(_startPositionX - 27 + offset, _startPositionY + 2, 5, 6);
|
||||
g.drawRect(_startPositionX - 76 + offset, _startPositionY + 2, 5, 6);
|
||||
|
||||
//Связка монорельса
|
||||
g.setColor(_entityMonorail.getBodyColor());
|
||||
g.fillRect(_startPositionX - 87 + offset, _startPositionY + 2, 7, 22);
|
||||
|
||||
//Нижняя часть монорельса
|
||||
xPoly_LeftSide = new int[]{_startPositionX + offset, _startPositionX - 8 + offset, _startPositionX - 72 + offset, _startPositionX - 36 + offset, _startPositionX - 36 + offset, _startPositionX - 15 + offset, _startPositionX - 15 + offset, _startPositionX + offset};
|
||||
yPoly_LeftSide = new int[] {_startPositionY + 30, _startPositionY + 25, _startPositionY + 25, _startPositionY + 25, _startPositionY + 32, _startPositionY + 32, _startPositionY + 35, _startPositionY + 32};
|
||||
g.fillPolygon(xPoly_LeftSide, yPoly_LeftSide, xPoly_LeftSide.length);
|
||||
|
||||
xPoly_RightSide = new int[]{_startPositionX - 86 + offset, _startPositionX - 80 + offset, _startPositionX - 50 + offset, _startPositionX - 50 + offset, _startPositionX - 69 + offset, _startPositionX - 79 + offset, _startPositionX - 86 + offset};
|
||||
yPoly_RightSide = new int[] {_startPositionY + 30, _startPositionY + 25, _startPositionY+ 25, _startPositionY + 32, _startPositionY + 32 , _startPositionY + 35, _startPositionY + 32};
|
||||
g.fillPolygon(xPoly_RightSide, yPoly_RightSide, xPoly_RightSide.length);
|
||||
g.fillRect(_startPositionX - 80 + offset, _startPositionY + 25, 70, 3);
|
||||
|
||||
//Колеса
|
||||
coordinatesX = new int[_wheels.getCountWheels()];
|
||||
coordinatesX[0] = _startPositionX - 20 + offset;
|
||||
coordinatesX[1] = _startPositionX - 75 + offset;
|
||||
|
||||
if (coordinatesX.length == 4) {
|
||||
coordinatesX[2] = _startPositionX - 35 + offset;
|
||||
coordinatesX[3] = _startPositionX - 60 + offset;
|
||||
}
|
||||
else if (coordinatesX.length == 3) {
|
||||
Random rnd = new Random();
|
||||
if (_wheelsSeed == 1) {
|
||||
coordinatesX[2] = _startPositionX - 35 + offset;
|
||||
}
|
||||
else {
|
||||
coordinatesX[2] = _startPositionX - 60 + offset;
|
||||
}
|
||||
}
|
||||
|
||||
_wheels.DrawWheels(g2, coordinatesX , _startPositionY + 30, _entityMonorail.getAdditionalColor());
|
||||
}
|
||||
|
||||
_wheels.DrawWheels(g2, coordinatesX, _startPositionY + 30, _entityMonorail.getBodyColor());
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package Scripts;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingWheels {
|
@ -1,29 +1,19 @@
|
||||
package Scripts;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityModernMonorail {
|
||||
public float Step;
|
||||
private int _speed;
|
||||
private float _weight;
|
||||
private Color _bodyColor;
|
||||
public class EntityModernMonorail extends EntityMonorail {
|
||||
private Color _additionalColor;
|
||||
private boolean _monorailTrack;
|
||||
private boolean _cabin;
|
||||
|
||||
public int getSpeed() {
|
||||
return _speed;
|
||||
}
|
||||
public float getWeight() {
|
||||
return _weight;
|
||||
}
|
||||
public Color getBodyColor() {
|
||||
return _bodyColor;
|
||||
}
|
||||
public Color getAdditionalColor() {return _additionalColor;}
|
||||
public boolean getMonorailTrack() {return _monorailTrack;}
|
||||
public boolean getCabin() {return _cabin;}
|
||||
|
||||
public void Initialization(int speed, float weight, Color bodyColor, Color additionalColor, boolean monorailTrack, boolean cabin){
|
||||
public EntityModernMonorail(int speed, float weight, Color bodyColor, Color additionalColor, boolean monorailTrack, boolean cabin){
|
||||
super(speed, weight, bodyColor);
|
||||
Random rnd = new Random();
|
||||
_speed = speed <= 0 ? rnd.nextInt(50)+10 : speed;
|
||||
_weight = weight <= 0 ? rnd.nextInt(100)+500 : weight;
|
30
ProjectMonorail/src/Scripts/EntityMonorail.java
Normal file
30
ProjectMonorail/src/Scripts/EntityMonorail.java
Normal file
@ -0,0 +1,30 @@
|
||||
package Scripts;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityMonorail {
|
||||
public float Step;
|
||||
protected int _speed;
|
||||
protected float _weight;
|
||||
protected Color _bodyColor;
|
||||
|
||||
public int getSpeed() {
|
||||
return _speed;
|
||||
}
|
||||
public float getWeight() {
|
||||
return _weight;
|
||||
}
|
||||
public Color getBodyColor() {
|
||||
return _bodyColor;
|
||||
}
|
||||
|
||||
public EntityMonorail(int speed, float weight, Color bodyColor){
|
||||
Random rnd = new Random();
|
||||
_speed = speed <= 0 ? rnd.nextInt(50)+10 : speed;
|
||||
_weight = weight <= 0 ? rnd.nextInt(100)+500 : weight;
|
||||
_bodyColor = bodyColor;
|
||||
|
||||
Step = _speed * 100/ (int)_weight;
|
||||
}
|
||||
}
|
@ -1,3 +1,7 @@
|
||||
package Scripts;
|
||||
|
||||
import Scripts.MovementStratagy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
@ -16,7 +20,8 @@ public class FormModernMonorail extends JFrame{
|
||||
|
||||
private DrawingField field = new DrawingField(this);
|
||||
|
||||
private JButton ButtonCreate=new JButton("Create Monorail");
|
||||
private JButton ButtonCreateMonorail = new JButton("Create Monorail");
|
||||
private JButton ButtonCreateModernMonorail = new JButton("Create Modern Monorail");
|
||||
|
||||
private Icon _iconUp = new ImageIcon("Resource\\Arrows\\ArrowUp.png");
|
||||
private Icon _iconDown = new ImageIcon("Resource\\Arrows\\ArrowDown.png");
|
||||
@ -28,6 +33,15 @@ public class FormModernMonorail extends JFrame{
|
||||
private JButton ButtonRight=new JButton(_iconRight);
|
||||
private JButton ButtonLeft=new JButton(_iconLeft);
|
||||
|
||||
private JPanel StrategyPanel = new JPanel();
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
private String[] _strategyItems = {
|
||||
"ToCenter",
|
||||
"ToBorder"
|
||||
};
|
||||
private JComboBox ComboBoxStrategy = new JComboBox(_strategyItems);
|
||||
private JButton ButtonStep = new JButton("Step");
|
||||
|
||||
public FormModernMonorail(){
|
||||
super("Modern Monorail");
|
||||
setSize(600,480);
|
||||
@ -87,20 +101,63 @@ public class FormModernMonorail extends JFrame{
|
||||
DimentionPanel.add(DPanel);
|
||||
add(DimentionPanel);
|
||||
|
||||
CreatePanel.setLayout(new FlowLayout());
|
||||
CreatePanel.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
|
||||
CreatePanel.setBackground(new Color(0,0,0,0));
|
||||
CreatePanel.add(ButtonCreate);
|
||||
ButtonCreate.addActionListener(e->{
|
||||
field.CreateButtonAction();
|
||||
CreatePanel.add(ButtonCreateMonorail);
|
||||
CreatePanel.add(ButtonCreateModernMonorail);
|
||||
|
||||
ButtonCreateModernMonorail.addActionListener(e->{
|
||||
field.CreateButtonAction_ModernMonorail();
|
||||
repaint();
|
||||
});
|
||||
|
||||
ButtonCreateMonorail.addActionListener(e->{
|
||||
field.CreateButtonAction_Monorail();
|
||||
repaint();
|
||||
});
|
||||
|
||||
StrategyPanel.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
|
||||
StrategyPanel.setBackground(new Color(0,0,0,0));
|
||||
StrategyPanel.add(ButtonStep);
|
||||
StrategyPanel.add(ComboBoxStrategy);
|
||||
|
||||
ButtonStep.addActionListener(e -> {
|
||||
if (field.getDrawingMonorail() == null) {
|
||||
return;
|
||||
}
|
||||
if (ComboBoxStrategy.isEnabled()) {
|
||||
_abstractStrategy = switch (ComboBoxStrategy.getSelectedIndex()) {
|
||||
case 0 -> new MoveToCenter();
|
||||
case 1 -> new MoveToBorder();
|
||||
default -> null;
|
||||
};
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.SetData(new MoveableMonorail(field.getDrawingMonorail()), Width, Height);
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ComboBoxStrategy.setEnabled(false);
|
||||
_abstractStrategy.MakeStep();
|
||||
repaint();
|
||||
if (_abstractStrategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
ComboBoxStrategy.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
});
|
||||
|
||||
BottomPanel.setLayout(new FlowLayout());
|
||||
BottomPanel.setBackground(new Color(0,0,0,0));
|
||||
|
||||
BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS));
|
||||
BottomAndCreatePanel.setBackground(new Color(0,0,0,0));
|
||||
BottomAndCreatePanel.add(CreatePanel);
|
||||
BottomAndCreatePanel.add(StrategyPanel);
|
||||
BottomAndCreatePanel.add(BottomPanel);
|
||||
|
||||
add(BottomAndCreatePanel);
|
@ -0,0 +1,93 @@
|
||||
package Scripts.MovementStratagy;
|
||||
|
||||
import Scripts.DirectionType;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject moveableObject;
|
||||
|
||||
private StrategyStatus state = StrategyStatus.NotInit;
|
||||
|
||||
private int fieldWidth;
|
||||
|
||||
protected int GetFieldWidth() {
|
||||
return fieldWidth;
|
||||
}
|
||||
|
||||
private int fieldHeight;
|
||||
|
||||
protected int GetFieldHeight() {
|
||||
return 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;
|
||||
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(DirectionType.Left);
|
||||
}
|
||||
|
||||
protected boolean MoveRight() {
|
||||
return MoveTo(DirectionType.Right);
|
||||
}
|
||||
|
||||
protected boolean MoveUp() { return MoveTo(DirectionType.Up); }
|
||||
|
||||
protected boolean MoveDown() {
|
||||
return MoveTo(DirectionType.Down);
|
||||
}
|
||||
|
||||
protected ObjectParameters GetObjectParameters() {
|
||||
if (moveableObject == null) {
|
||||
return null;
|
||||
}
|
||||
return moveableObject.GetObjectPosition();
|
||||
}
|
||||
|
||||
protected Integer GetStep() {
|
||||
if (state != StrategyStatus.InProgress) {
|
||||
return null;
|
||||
}
|
||||
return moveableObject.GetStep();
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
protected abstract boolean isTargetDestination();
|
||||
|
||||
private boolean MoveTo(DirectionType direction) {
|
||||
if (state != StrategyStatus.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (moveableObject == null) {
|
||||
return false;
|
||||
}
|
||||
if (moveableObject.CanMoveObject(direction)) {
|
||||
moveableObject.MoveObject(direction);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package Scripts.MovementStratagy;
|
||||
|
||||
import Scripts.DirectionType;
|
||||
|
||||
public interface IMoveableObject{
|
||||
ObjectParameters GetObjectPosition();
|
||||
|
||||
int GetStep();
|
||||
|
||||
boolean CanMoveObject(DirectionType direction);
|
||||
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package Scripts.MovementStratagy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy {
|
||||
@Override
|
||||
protected boolean isTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder() <= GetFieldWidth() &&
|
||||
objParams.RightBorder() + GetStep() >= GetFieldWidth() &&
|
||||
objParams.DownBorder() <= GetFieldHeight() &&
|
||||
objParams.DownBorder() + GetStep() >= GetFieldHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth();
|
||||
if (Math.abs(diffX) > GetStep()) {
|
||||
if (diffX < 0) {
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical() - GetFieldHeight();
|
||||
if (Math.abs(diffY) > GetStep()) {
|
||||
if (diffY < 0) {
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package Scripts.MovementStratagy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy {
|
||||
@Override
|
||||
protected boolean isTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal() <= GetFieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal() + GetStep() >= GetFieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleVertical() <= GetFieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical() + GetStep() >= GetFieldHeight() / 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth() / 2;
|
||||
if (Math.abs(diffX) > GetStep()) {
|
||||
if (diffX > 0) {
|
||||
MoveLeft();
|
||||
} else {
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical() - GetFieldHeight() / 2;
|
||||
if (Math.abs(diffY) > GetStep()) {
|
||||
if (diffY > 0) {
|
||||
MoveUp();
|
||||
} else {
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package Scripts.MovementStratagy;
|
||||
|
||||
import Scripts.DirectionType;
|
||||
import Scripts.DrawingMonorail;
|
||||
|
||||
public class MoveableMonorail implements IMoveableObject {
|
||||
private DrawingMonorail _drawingMonorail = null;
|
||||
public MoveableMonorail(DrawingMonorail drawingMonorail)
|
||||
{
|
||||
_drawingMonorail = drawingMonorail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectParameters GetObjectPosition() {
|
||||
if (_drawingMonorail == null || _drawingMonorail.getMonorail() == null) {
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_drawingMonorail.GetPositionX(), _drawingMonorail.GetPositionY(),
|
||||
_drawingMonorail.GetWidth(), _drawingMonorail.GetHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int GetStep() {
|
||||
if (_drawingMonorail != null && _drawingMonorail.getMonorail()!=null)
|
||||
return (int)(_drawingMonorail.getMonorail().Step);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean CanMoveObject(DirectionType direction) {
|
||||
if (_drawingMonorail != null)
|
||||
return _drawingMonorail.CanMove(direction);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void MoveObject(DirectionType direction) {
|
||||
if (_drawingMonorail != null)
|
||||
_drawingMonorail.MoveTransport(direction);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package Scripts.MovementStratagy;
|
||||
|
||||
public class ObjectParameters {
|
||||
private final int _x;
|
||||
|
||||
private final int _y;
|
||||
|
||||
private final int _width;
|
||||
|
||||
private final int _height;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package Scripts.MovementStratagy;
|
||||
|
||||
public enum StrategyStatus {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package Scripts;
|
||||
|
||||
public class Program {
|
||||
public static void main(String[] args){
|
||||
new FormModernMonorail();
|
Loading…
Reference in New Issue
Block a user