PIbd-12 Ulybin A.A. Monorail Hard Lab02 #2
@ -1,3 +1,5 @@
|
||||
package Drawings;
|
||||
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
54
ProjectMonorail/src/Drawings/DrawingMonorail.java
Normal file
54
ProjectMonorail/src/Drawings/DrawingMonorail.java
Normal file
@ -0,0 +1,54 @@
|
||||
package Drawings;
|
||||
|
||||
import Entities.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingMonorail extends DrawingTrain{
|
||||
EntityMonorail entityMonorail;
|
||||
|
||||
public DrawingMonorail(int speed, double weight, Color bodyColor, int wheelsType, Color additionalColor, boolean rail, boolean secondCarriage) {
|
||||
super(speed, weight, bodyColor, wheelsType, 150, 40);
|
||||
entityMonorail = new EntityMonorail(speed, weight, bodyColor, additionalColor, rail, secondCarriage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTrain(Graphics g) {
|
||||
if (entityMonorail == null || !(entityMonorail instanceof EntityMonorail) || _startPosX == null || _startPosY == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.DrawTrain(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
if (entityMonorail.getRail()) {
|
||||
g2d.setColor(entityMonorail.getAdditionalColor());
|
||||
if (entityMonorail.getSecondCarriage()){
|
||||
g2d.fillRect(_startPosX, _startPosY + 40, 150, 3);
|
||||
}
|
||||
else{
|
||||
g2d.fillRect(_startPosX, _startPosY + 40, 90, 3);
|
||||
}
|
||||
}
|
||||
|
||||
if (entityMonorail.getSecondCarriage()) {
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(_startPosX + 95, _startPosY, 55, 30);
|
||||
g2d.setColor(entityMonorail.getAdditionalColor());
|
||||
g2d.fillRect(_startPosX + 96, _startPosY + 1, 54, 29);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillRect(_startPosX + 85, _startPosY + 5, 10, 23);
|
||||
g2d.drawRect(_startPosX + 105, _startPosY + 5, 35, 10);
|
||||
g2d.setColor(Color.CYAN);
|
||||
g2d.fillRect(_startPosX + 106, _startPosY + 6, 34, 9);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(_startPosX + 105, _startPosY + 5, 10, 23);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(_startPosX + 106, _startPosY + 16, 9, 12);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(_startPosX + 100, _startPosY + 30, 10, 10);
|
||||
g2d.drawOval(_startPosX + 110, _startPosY + 30, 10, 10);
|
||||
g2d.drawOval(_startPosX + 130, _startPosY + 30, 10, 10);
|
||||
g2d.drawOval(_startPosX + 140, _startPosY + 30, 10, 10);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package Drawings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingMonorailWheels {
|
||||
public class DrawingMonorailWheels implements IDrawingWheels{
|
||||
private WheelsCount _wheelsCount;
|
||||
|
||||
public void setEnumNumber(int wheelsCount){
|
51
ProjectMonorail/src/Drawings/DrawingMonorailWheelsPlus.java
Normal file
51
ProjectMonorail/src/Drawings/DrawingMonorailWheelsPlus.java
Normal file
@ -0,0 +1,51 @@
|
||||
package Drawings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingMonorailWheelsPlus implements IDrawingWheels{
|
||||
private WheelsCount _wheelsCount;
|
||||
public void setEnumNumber(int wheelsCount){
|
||||
for (WheelsCount value : WheelsCount.values()){
|
||||
if (value.getEnumNumber() == wheelsCount){
|
||||
_wheelsCount = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void drawWheels(Graphics2D g2d, Color color, int x, int y){
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(x, y + 30, 10, 10);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillOval(x, y + 30, 10, 10);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(x + 4, y + 30, 2, 10);
|
||||
g2d.drawRect(x, y + 34, 10, 2);
|
||||
g2d.setColor(color);
|
||||
g2d.fillRect(x + 5, y + 31, 1, 9);
|
||||
g2d.fillRect(x + 1, y + 35, 9, 1);
|
||||
}
|
||||
|
||||
public void drawMonorailWheels(Graphics g, Color color, int startPosX, int startPosY){
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
switch (_wheelsCount){
|
||||
case Two:
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY);
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY);
|
||||
break;
|
||||
case Three:
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY);
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY);
|
||||
drawWheels(g2d, color, startPosX + 30, startPosY);
|
||||
break;
|
||||
case Four:
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY);
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY);
|
||||
drawWheels(g2d, color, startPosX + 30, startPosY);
|
||||
drawWheels(g2d, color, startPosX + 50, startPosY);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package Drawings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingMonorailWheelsSquare implements IDrawingWheels{
|
||||
private WheelsCount _wheelsCount;
|
||||
public void setEnumNumber(int wheelsCount){
|
||||
for (WheelsCount value : WheelsCount.values()){
|
||||
if (value.getEnumNumber() == wheelsCount){
|
||||
_wheelsCount = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void drawWheels(Graphics2D g2d, Color color, int x, int y){
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(x, y + 30, 10, 10);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillOval(x, y + 30, 10, 10);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(x + 2, y + 32, 6, 6);
|
||||
g2d.setColor(color);
|
||||
g2d.fillRect(x + 3, y + 33, 5, 5);
|
||||
}
|
||||
|
||||
public void drawMonorailWheels(Graphics g, Color color, int startPosX, int startPosY){
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
switch (_wheelsCount){
|
||||
case Two:
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY);
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY);
|
||||
break;
|
||||
case Three:
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY);
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY);
|
||||
drawWheels(g2d, color, startPosX + 30, startPosY);
|
||||
break;
|
||||
case Four:
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY);
|
||||
drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY);
|
||||
drawWheels(g2d, color, startPosX + 30, startPosY);
|
||||
drawWheels(g2d, color, startPosX + 50, startPosY);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,34 +1,53 @@
|
||||
package Drawings;
|
||||
import Entities.EntityTrain;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingMonorail {
|
||||
private EntityMonorail entityMonorail;
|
||||
public EntityMonorail getEntityMonorail(){return entityMonorail;}
|
||||
private DrawingMonorailWheels drawingMonorailWheels;
|
||||
public class DrawingTrain {
|
||||
private EntityTrain entityTrain;
|
||||
public EntityTrain getEntityTrain(){return entityTrain;}
|
||||
private IDrawingWheels drawingWheels;
|
||||
|
||||
private Integer _pictureWidth;
|
||||
|
||||
private Integer _pictureHeight;
|
||||
protected Integer _startPosX;
|
||||
protected Integer _startPosY;
|
||||
private int _drawingTrainWidth = 100;
|
||||
private int _drawingTrainHeight = 40;
|
||||
public int getPosX(){return _startPosX;}
|
||||
public int getPosY(){return _startPosY;}
|
||||
public int getTrainWidth(){return _drawingTrainWidth;}
|
||||
public int getTrainHeight(){return _drawingTrainHeight;}
|
||||
|
||||
private Integer _startPosX;
|
||||
|
||||
private Integer _startPosY;
|
||||
|
||||
private final int _drawingTrainWidth = 150;
|
||||
|
||||
private final int _drawingTrainHeight = 40;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean rail, boolean secondCarriage){
|
||||
entityMonorail = new EntityMonorail();
|
||||
entityMonorail.Init(speed, weight, bodyColor, additionalColor, rail, secondCarriage);
|
||||
public DrawingTrain(int speed, double weight, Color bodyColor, int wheelsType){
|
||||
entityTrain = new EntityTrain(speed, weight, bodyColor);
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
drawingMonorailWheels = new DrawingMonorailWheels();
|
||||
switch(wheelsType){
|
||||
case 0:
|
||||
drawingWheels = new DrawingMonorailWheels();
|
||||
break;
|
||||
case 1:
|
||||
drawingWheels = new DrawingMonorailWheelsPlus();
|
||||
break;
|
||||
case 2:
|
||||
drawingWheels = new DrawingMonorailWheelsSquare();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
Random rand = new Random();
|
||||
int wheelsCount = rand.nextInt(2, 5);
|
||||
drawingMonorailWheels.setEnumNumber(wheelsCount);
|
||||
drawingWheels.setEnumNumber(wheelsCount);
|
||||
}
|
||||
|
||||
public DrawingTrain(int speed, double weight, Color bodyColor, int wheelsType, int drawingTrainWidth, int drawingTrainHeight){
|
||||
this(speed, weight, bodyColor, wheelsType);
|
||||
_drawingTrainWidth = drawingTrainWidth;
|
||||
_drawingTrainHeight = drawingTrainHeight;
|
||||
}
|
||||
|
||||
public boolean SetPictureSize(int width, int height){
|
||||
@ -66,41 +85,41 @@ public class DrawingMonorail {
|
||||
}
|
||||
|
||||
public boolean MoveTransport(DirectionType direction){
|
||||
if (entityMonorail == null || _startPosX == null || _startPosY == null){
|
||||
if (entityTrain == null || _startPosX == null || _startPosY == null){
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (direction){
|
||||
case Up:
|
||||
if (_startPosY - entityMonorail.Step() > 0){
|
||||
_startPosY -= (int) entityMonorail.Step();
|
||||
case DirectionType.Up:
|
||||
if (_startPosY - entityTrain.Step() > 0){
|
||||
_startPosY -= (int) entityTrain.Step();
|
||||
}
|
||||
return true;
|
||||
case Down:
|
||||
if (_startPosY + entityMonorail.Step() + _drawingTrainHeight < _pictureHeight){
|
||||
_startPosY += (int) entityMonorail.Step();
|
||||
case DirectionType.Down:
|
||||
if (_startPosY + entityTrain.Step() + _drawingTrainHeight < _pictureHeight){
|
||||
_startPosY += (int) entityTrain.Step();
|
||||
}
|
||||
return true;
|
||||
case Left:
|
||||
if (_startPosX - entityMonorail.Step() > 0){
|
||||
_startPosX -= (int) entityMonorail.Step();
|
||||
case DirectionType.Left:
|
||||
if (_startPosX - entityTrain.Step() > 0){
|
||||
_startPosX -= (int) entityTrain.Step();
|
||||
}
|
||||
return true;
|
||||
case Right:
|
||||
if (_startPosX + entityMonorail.Step() + _drawingTrainWidth < _pictureWidth){
|
||||
_startPosX += (int) entityMonorail.Step();
|
||||
case DirectionType.Right:
|
||||
if (_startPosX + entityTrain.Step() + _drawingTrainWidth < _pictureWidth){
|
||||
_startPosX += (int) entityTrain.Step();
|
||||
}
|
||||
return true;
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawMonorail(Graphics g) {
|
||||
if (entityMonorail == null || _startPosX == null || _startPosY == null) {
|
||||
public void DrawTrain(Graphics g) {
|
||||
if (entityTrain == null || _startPosX == null || _startPosY == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
drawingMonorailWheels.drawMonorailWheels(g, entityMonorail.getAdditionalColor(), _startPosX, _startPosY);
|
||||
drawingWheels.drawMonorailWheels(g, entityTrain.getBodyColor(), _startPosX, _startPosY);
|
||||
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
@ -117,7 +136,7 @@ public class DrawingMonorail {
|
||||
monorailPolygon.addPoint(point.x, point.y);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.draw(monorailPolygon);
|
||||
g2d.setColor(entityMonorail.getBodyColor());
|
||||
g2d.setColor(entityTrain.getBodyColor());
|
||||
g2d.fill(monorailPolygon);
|
||||
|
||||
Point[][] carts = new Point[][] {
|
||||
@ -139,7 +158,6 @@ public class DrawingMonorail {
|
||||
new Point(_startPosX + 85, _startPosY + 30)}
|
||||
};
|
||||
|
||||
|
||||
g2d.setColor(Color.BLACK);
|
||||
for (int i = 0; i < 4; i++){
|
||||
Polygon cartPolygon = new Polygon();
|
||||
@ -157,36 +175,5 @@ public class DrawingMonorail {
|
||||
g2d.fillRect(_startPosX + 56, _startPosY + 6, 26, 6);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(_startPosX + 41, _startPosY + 8, 9, 20);
|
||||
|
||||
|
||||
if (entityMonorail.getRail()) {
|
||||
g2d.setColor(entityMonorail.getAdditionalColor());
|
||||
if (entityMonorail.getSecondCarriage()){
|
||||
g2d.fillRect(_startPosX, _startPosY + 40, 150, 3);
|
||||
}
|
||||
else{
|
||||
g2d.fillRect(_startPosX, _startPosY + 40, 90, 3);
|
||||
}
|
||||
}
|
||||
if (entityMonorail.getSecondCarriage()) {
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(_startPosX + 95, _startPosY, 55, 30);
|
||||
g2d.setColor(entityMonorail.getAdditionalColor());
|
||||
g2d.fillRect(_startPosX + 96, _startPosY + 1, 54, 29);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillRect(_startPosX + 85, _startPosY + 5, 10, 23);
|
||||
g2d.drawRect(_startPosX + 105, _startPosY + 5, 35, 10);
|
||||
g2d.setColor(Color.CYAN);
|
||||
g2d.fillRect(_startPosX + 106, _startPosY + 6, 34, 9);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawRect(_startPosX + 105, _startPosY + 5, 10, 23);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(_startPosX + 106, _startPosY + 16, 9, 12);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(_startPosX + 100, _startPosY + 30, 10, 10);
|
||||
g2d.drawOval(_startPosX + 110, _startPosY + 30, 10, 10);
|
||||
g2d.drawOval(_startPosX + 130, _startPosY + 30, 10, 10);
|
||||
g2d.drawOval(_startPosX + 140, _startPosY + 30, 10, 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
8
ProjectMonorail/src/Drawings/IDrawingWheels.java
Normal file
8
ProjectMonorail/src/Drawings/IDrawingWheels.java
Normal file
@ -0,0 +1,8 @@
|
||||
package Drawings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingWheels {
|
||||
void setEnumNumber(int x);
|
||||
void drawMonorailWheels(Graphics g, Color color, int startX, int startY);
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package Drawings;
|
||||
|
||||
public enum WheelsCount {
|
||||
Two(2),
|
||||
Three(3),
|
21
ProjectMonorail/src/Entities/EntityMonorail.java
Normal file
21
ProjectMonorail/src/Entities/EntityMonorail.java
Normal file
@ -0,0 +1,21 @@
|
||||
package Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityMonorail extends EntityTrain {
|
||||
private Color AdditionalColor;
|
||||
public Color getAdditionalColor(){return AdditionalColor;};
|
||||
|
||||
private boolean Rail;
|
||||
public boolean getRail(){return Rail;}
|
||||
|
||||
private boolean SecondCarriage;
|
||||
public boolean getSecondCarriage(){return SecondCarriage;}
|
||||
|
||||
public EntityMonorail(int speed, double weight, Color bodyColor, Color additionalColor, boolean rail, boolean secondCarriage) {
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor = additionalColor;
|
||||
Rail = rail;
|
||||
SecondCarriage = secondCarriage;
|
||||
}
|
||||
}
|
22
ProjectMonorail/src/Entities/EntityTrain.java
Normal file
22
ProjectMonorail/src/Entities/EntityTrain.java
Normal file
@ -0,0 +1,22 @@
|
||||
package Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityTrain {
|
||||
private int Speed;
|
||||
public int getSpeed(){return Speed;}
|
||||
|
||||
private double Weight;
|
||||
public double getWeight(){return Weight;}
|
||||
|
||||
private Color BodyColor;
|
||||
public Color getBodyColor(){return BodyColor;};
|
||||
|
||||
public double Step(){return Speed*100/Weight;}
|
||||
|
||||
public EntityTrain(int speed, double weight, Color bodyColor){
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityMonorail {
|
||||
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 Color AdditionalColor;
|
||||
public Color getAdditionalColor(){return AdditionalColor;};
|
||||
|
||||
private boolean Rail;
|
||||
public boolean getRail(){return Rail;}
|
||||
|
||||
private boolean SecondCarriage;
|
||||
public boolean getSecondCarriage(){return SecondCarriage;}
|
||||
|
||||
public double Step(){return Speed*100/Weight;}
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean rail, boolean secondCarriage){
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Rail = rail;
|
||||
SecondCarriage = secondCarriage;
|
||||
}
|
||||
}
|
@ -1,33 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMonorail">
|
||||
<grid id="27dc6" binding="PictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<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="1222" height="655"/>
|
||||
<xy x="6" y="11" width="1222" height="655"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<maximumSize width="1222" height="655"/>
|
||||
<minimumSize width="1222" height="655"/>
|
||||
<maximumSize width="9999999" height="9999999"/>
|
||||
<minimumSize width="10" height="10"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="22152" class="javax.swing.JButton" binding="buttonCreate">
|
||||
<component id="22152" class="javax.swing.JButton" binding="buttonCreateMonorail">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<doubleBuffered value="false"/>
|
||||
<text value="Создать"/>
|
||||
<text value="Создать Монорельс"/>
|
||||
</properties>
|
||||
</component>
|
||||
<vspacer id="15f80">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
<component id="dacaf" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
|
||||
<grid row="4" column="5" 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="50" height="50"/>
|
||||
<preferred-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
@ -39,7 +34,7 @@
|
||||
</component>
|
||||
<component id="7c40b" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
|
||||
<grid row="4" column="4" 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="50" height="50"/>
|
||||
<preferred-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
@ -52,7 +47,7 @@
|
||||
</component>
|
||||
<component id="c146d" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
|
||||
<grid row="4" column="3" 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="50" height="50"/>
|
||||
<preferred-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
@ -65,7 +60,7 @@
|
||||
</component>
|
||||
<component id="209bb" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
|
||||
<grid row="3" column="4" 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="50" height="50"/>
|
||||
<preferred-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
@ -76,16 +71,40 @@
|
||||
<text value="↑"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="98210">
|
||||
<component id="2194f" class="javax.swing.JButton" binding="buttonCreateTrain">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<doubleBuffered value="false"/>
|
||||
<text value="Создать Поезд"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="99f56">
|
||||
<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="e878d">
|
||||
<component id="9ff70" class="javax.swing.JComboBox" binding="comboBoxStrategy">
|
||||
<constraints>
|
||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
<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>
|
||||
<vspacer id="89201">
|
||||
<constraints>
|
||||
<grid row="2" column="4" 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="b010c" class="javax.swing.JButton" binding="buttonStrategyStep">
|
||||
<constraints>
|
||||
<grid row="1" column="4" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<label value="Шаг"/>
|
||||
<text value="Шаг"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
||||
|
@ -1,3 +1,6 @@
|
||||
import Drawings.*;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
@ -7,16 +10,43 @@ import java.util.Random;
|
||||
import java.util.List;
|
||||
|
||||
public class FormMonorail extends JFrame {
|
||||
protected DrawingMonorail _drawingMonorail = new DrawingMonorail();
|
||||
protected DrawingTrain _drawingTrain;
|
||||
private AbstractStrategy _strategy;
|
||||
JPanel PictureBox;
|
||||
private JButton buttonCreate;
|
||||
private JButton buttonCreateMonorail;
|
||||
private JButton buttonCreateTrain;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonUp;
|
||||
private JComboBox comboBoxStrategy;
|
||||
private JButton buttonStrategyStep;
|
||||
|
||||
private List<JComponent> controls;
|
||||
|
||||
public void createObject(String obj){
|
||||
Random rand = new Random();
|
||||
switch(obj){
|
||||
case "Train":
|
||||
_drawingTrain = new DrawingTrain(rand.nextInt(100, 300), rand.nextDouble(1000, 3000),
|
||||
new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), rand.nextInt(0, 3));
|
||||
break;
|
||||
case "Monorail":
|
||||
_drawingTrain = new DrawingMonorail(rand.nextInt(100, 300), rand.nextDouble(1000, 3000),
|
||||
new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), rand.nextInt(0, 3),
|
||||
new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)),
|
||||
rand.nextBoolean(), rand.nextBoolean());
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
_drawingTrain.SetPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
|
||||
_drawingTrain.SetPosition(rand.nextInt(50), rand.nextInt(50));
|
||||
_strategy = null;
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
Draw();
|
||||
}
|
||||
|
||||
public FormMonorail() {
|
||||
buttonUp.setName("buttonUp");
|
||||
buttonDown.setName("buttonDown");
|
||||
@ -25,64 +55,76 @@ public class FormMonorail extends JFrame {
|
||||
|
||||
InitializeControlsRepaintList();
|
||||
|
||||
buttonCreate.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
_drawingMonorail = new DrawingMonorail();
|
||||
Random random = new Random();
|
||||
buttonCreateMonorail.addActionListener(e -> createObject("Monorail"));
|
||||
|
||||
_drawingMonorail.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());
|
||||
_drawingMonorail.SetPictureSize(PictureBox.getWidth(), PictureBox.getHeight());
|
||||
_drawingMonorail.SetPosition(random.nextInt(25, 100),
|
||||
random.nextInt(25, 100));
|
||||
buttonCreateTrain.addActionListener(e -> createObject("Train"));
|
||||
ActionListener buttonMoveClickedListener = e -> {
|
||||
String buttonName = ((JButton) e.getSource()).getName();
|
||||
boolean result = false;
|
||||
|
||||
switch (buttonName) {
|
||||
case "buttonUp": {
|
||||
result = _drawingTrain.MoveTransport(DirectionType.Up);
|
||||
}
|
||||
break;
|
||||
case "buttonDown": {
|
||||
result = _drawingTrain.MoveTransport(DirectionType.Down);
|
||||
}
|
||||
break;
|
||||
case "buttonLeft": {
|
||||
result = _drawingTrain.MoveTransport(DirectionType.Left);
|
||||
}
|
||||
break;
|
||||
case "buttonRight": {
|
||||
result = _drawingTrain.MoveTransport(DirectionType.Right);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
if (result)
|
||||
Draw();
|
||||
|
||||
}
|
||||
});
|
||||
ActionListener buttonMoveClickedListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String buttonName = ((JButton) e.getSource()).getName();
|
||||
boolean result = false;
|
||||
|
||||
switch (buttonName) {
|
||||
case "buttonUp": {
|
||||
result = _drawingMonorail.MoveTransport(DirectionType.Up);
|
||||
}
|
||||
break;
|
||||
case "buttonDown": {
|
||||
result = _drawingMonorail.MoveTransport(DirectionType.Down);
|
||||
}
|
||||
break;
|
||||
case "buttonLeft": {
|
||||
result = _drawingMonorail.MoveTransport(DirectionType.Left);
|
||||
}
|
||||
break;
|
||||
case "buttonRight": {
|
||||
result = _drawingMonorail.MoveTransport(DirectionType.Right);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
if (result)
|
||||
Draw();
|
||||
|
||||
}
|
||||
};
|
||||
buttonRight.addActionListener(buttonMoveClickedListener);
|
||||
buttonDown.addActionListener(buttonMoveClickedListener);
|
||||
buttonLeft.addActionListener(buttonMoveClickedListener);
|
||||
buttonUp.addActionListener(buttonMoveClickedListener);
|
||||
|
||||
comboBoxStrategy.addItem("К Центру");
|
||||
comboBoxStrategy.addItem("К Краю");
|
||||
buttonStrategyStep.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_drawingTrain == 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 MovableTrain(_drawingTrain), 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() {
|
||||
if (_drawingMonorail.getEntityMonorail() == null)
|
||||
if (_drawingTrain.getEntityTrain() == null)
|
||||
return;
|
||||
if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) {
|
||||
return;
|
||||
@ -90,7 +132,7 @@ public class FormMonorail extends JFrame {
|
||||
Graphics g = PictureBox.getGraphics();
|
||||
g.setColor(PictureBox.getBackground());
|
||||
g.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight());
|
||||
_drawingMonorail.DrawMonorail(g);
|
||||
_drawingTrain.DrawTrain(g);
|
||||
|
||||
RepaintControls();
|
||||
|
||||
@ -105,7 +147,8 @@ public class FormMonorail extends JFrame {
|
||||
|
||||
private void InitializeControlsRepaintList() {
|
||||
controls = new LinkedList<>();
|
||||
controls.add(buttonCreate);
|
||||
controls.add(buttonCreateMonorail);
|
||||
controls.add(buttonCreateTrain);
|
||||
controls.add(buttonUp);
|
||||
controls.add(buttonDown);
|
||||
controls.add(buttonLeft);
|
||||
|
61
ProjectMonorail/src/MovementStrategy/AbstractStrategy.java
Normal file
61
ProjectMonorail/src/MovementStrategy/AbstractStrategy.java
Normal file
@ -0,0 +1,61 @@
|
||||
package MovementStrategy;
|
||||
|
||||
import Drawings.DirectionType;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _movableObject;
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
protected int FieldWidth;
|
||||
protected int FieldHeight;
|
||||
public StrategyStatus GetStatus() { return _state; }
|
||||
|
||||
public void SetData(IMoveableObject movableObject, int width, int height) {
|
||||
if (movableObject == null)
|
||||
{
|
||||
_state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = StrategyStatus.InProgress;
|
||||
_movableObject = movableObject;
|
||||
FieldHeight = height;
|
||||
FieldWidth = width;
|
||||
}
|
||||
|
||||
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() { return _movableObject.GetObjectPosition(); }
|
||||
|
||||
protected int GetStep() {
|
||||
if(_state != StrategyStatus.InProgress) {
|
||||
return 0;
|
||||
}
|
||||
return _movableObject.GetStep();
|
||||
}
|
||||
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract boolean IsTargetDestination();
|
||||
private boolean MoveTo(DirectionType directionType) {
|
||||
if (_state != StrategyStatus.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (_movableObject.TryMoveObject(directionType)) {
|
||||
_movableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
10
ProjectMonorail/src/MovementStrategy/IMoveableObject.java
Normal file
10
ProjectMonorail/src/MovementStrategy/IMoveableObject.java
Normal file
@ -0,0 +1,10 @@
|
||||
package MovementStrategy;
|
||||
|
||||
import Drawings.DirectionType;
|
||||
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean TryMoveObject(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
30
ProjectMonorail/src/MovementStrategy/MovableTrain.java
Normal file
30
ProjectMonorail/src/MovementStrategy/MovableTrain.java
Normal file
@ -0,0 +1,30 @@
|
||||
package MovementStrategy;
|
||||
|
||||
import Drawings.DirectionType;
|
||||
import Drawings.DrawingTrain;
|
||||
|
||||
public class MovableTrain implements IMoveableObject {
|
||||
private DrawingTrain _train = null;
|
||||
public MovableTrain(DrawingTrain train){
|
||||
_train = train;
|
||||
}
|
||||
|
||||
public ObjectParameters GetObjectPosition() {
|
||||
if (_train == null || _train.getEntityTrain() == null){
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_train.getPosX(), _train.getPosY(), _train.getTrainWidth(), _train.getTrainHeight());
|
||||
}
|
||||
|
||||
public int GetStep() {
|
||||
return (int) _train.getEntityTrain().Step();
|
||||
}
|
||||
|
||||
public boolean TryMoveObject(DirectionType direction) {
|
||||
return _train.MoveTransport(direction);
|
||||
}
|
||||
|
||||
public void MoveObject(DirectionType direction) {
|
||||
_train.MoveTransport(direction);
|
||||
}
|
||||
}
|
27
ProjectMonorail/src/MovementStrategy/MoveToBorder.java
Normal file
27
ProjectMonorail/src/MovementStrategy/MoveToBorder.java
Normal file
@ -0,0 +1,27 @@
|
||||
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()){
|
||||
MoveRight();
|
||||
}
|
||||
int diffY = objParams.DownBorder() - FieldHeight;
|
||||
if (Math.abs(diffY) > GetStep()){
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
39
ProjectMonorail/src/MovementStrategy/MoveToCenter.java
Normal file
39
ProjectMonorail/src/MovementStrategy/MoveToCenter.java
Normal file
@ -0,0 +1,39 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy {
|
||||
protected boolean IsTargetDestination(){
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMidHorizontal() - GetStep() <= FieldWidth / 2 &&
|
||||
objParams.ObjectMidHorizontal() + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMidVertical() - GetStep() <= FieldHeight / 2 &&
|
||||
objParams.ObjectMidVertical() + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
|
||||
protected void MoveToTarget(){
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.ObjectMidHorizontal() - FieldWidth / 2;
|
||||
if (Math.abs(diffX) > GetStep()){
|
||||
if (diffX > 0){
|
||||
MoveLeft();
|
||||
}
|
||||
else{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.ObjectMidVertical() - FieldHeight / 2;
|
||||
if (Math.abs(diffY) > GetStep()){
|
||||
if (diffY > 0){
|
||||
MoveUp();
|
||||
}
|
||||
else{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum MovementDirection {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
23
ProjectMonorail/src/MovementStrategy/ObjectParameters.java
Normal file
23
ProjectMonorail/src/MovementStrategy/ObjectParameters.java
Normal file
@ -0,0 +1,23 @@
|
||||
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 ObjectMidHorizontal(){return _x + _width / 2;};
|
||||
public int ObjectMidVertical(){return _y + _height / 2;};
|
||||
|
||||
public ObjectParameters(int x, int y, int width, int height){
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
7
ProjectMonorail/src/MovementStrategy/StrategyStatus.java
Normal file
7
ProjectMonorail/src/MovementStrategy/StrategyStatus.java
Normal file
@ -0,0 +1,7 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum StrategyStatus {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user