Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
6f7f3302c1 | |||
b208126772 | |||
b6e9f62b9c | |||
3901e3e00e |
1
.idea/.name
Normal file
1
.idea/.name
Normal file
@ -0,0 +1 @@
|
|||||||
|
DrawingRoadTrain.java
|
@ -3,6 +3,7 @@
|
|||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/Resources" type="java-resource" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
BIN
Resources/ArrowDown.png
Normal file
BIN
Resources/ArrowDown.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
BIN
Resources/ArrowLeft.png
Normal file
BIN
Resources/ArrowLeft.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
BIN
Resources/ArrowRight.png
Normal file
BIN
Resources/ArrowRight.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
BIN
Resources/ArrowUp.png
Normal file
BIN
Resources/ArrowUp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.9 KiB |
@ -1,5 +0,0 @@
|
|||||||
public class Main {
|
|
||||||
public static void main(String[] args){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
8
src/RoadTrain/DirectionType.java
Normal file
8
src/RoadTrain/DirectionType.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package RoadTrain;
|
||||||
|
|
||||||
|
public enum DirectionType {
|
||||||
|
Up,
|
||||||
|
Down,
|
||||||
|
Left,
|
||||||
|
Right
|
||||||
|
}
|
42
src/RoadTrain/DrawingObjects/DrawingRoadTrain.java
Normal file
42
src/RoadTrain/DrawingObjects/DrawingRoadTrain.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package RoadTrain.DrawingObjects;
|
||||||
|
import java.awt.*;
|
||||||
|
import RoadTrain.Entities.*;
|
||||||
|
import RoadTrain.Extra.IDrawingWheels;
|
||||||
|
|
||||||
|
|
||||||
|
public class DrawingRoadTrain extends DrawingTruck{
|
||||||
|
public DrawingRoadTrain(int speed, double weight, Color bodyColor, Color
|
||||||
|
additionalColor, boolean waterContainer, boolean sweepingBrush, int width, int height, int wheelNumber)
|
||||||
|
{
|
||||||
|
super(speed, weight, bodyColor, width, height, wheelNumber);
|
||||||
|
if (entityTruck != null)
|
||||||
|
{
|
||||||
|
entityTruck = new EntityRoadTrain(speed, weight, bodyColor, wheelNumber,
|
||||||
|
additionalColor, waterContainer, sweepingBrush);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public DrawingRoadTrain(EntityRoadTrain truck, IDrawingWheels _wheelDrawing, int width, int height ){
|
||||||
|
super(truck, _wheelDrawing, width, height);
|
||||||
|
if (height < _truckHeight || width < _truckWidth)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void DrawTransport(Graphics g) {
|
||||||
|
if (!(entityTruck instanceof EntityRoadTrain RoadTrain)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
super.DrawTransport(g);
|
||||||
|
if (RoadTrain.GetWaterContainer()){
|
||||||
|
g.setColor(RoadTrain.GetAdditionalColor());
|
||||||
|
g.drawOval(_startPosX + 30, _startPosY, 10, 20);
|
||||||
|
g.fillOval(_startPosX + 30, _startPosY, 10, 20);
|
||||||
|
}
|
||||||
|
if (RoadTrain.GetSweepingBrush())
|
||||||
|
{
|
||||||
|
g.drawLine(_startPosX + 30, _startPosY + 10, _startPosX + 20, _startPosY + 10);
|
||||||
|
g.drawLine(_startPosX + 20, _startPosY + 10, _startPosX + 10, _startPosY + 30);
|
||||||
|
g.drawLine(_startPosX + 17, _startPosY + 30, _startPosX + 3, _startPosY + 30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
152
src/RoadTrain/DrawingObjects/DrawingTruck.java
Normal file
152
src/RoadTrain/DrawingObjects/DrawingTruck.java
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
package RoadTrain.DrawingObjects;
|
||||||
|
import RoadTrain.DirectionType;
|
||||||
|
import RoadTrain.Entities.*;
|
||||||
|
import RoadTrain.Extra.*;
|
||||||
|
import RoadTrain.MovementStraregy.*;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingTruck {
|
||||||
|
public EntityTruck entityTruck;
|
||||||
|
private IDrawingWheels drawingWheels;
|
||||||
|
private int _pictureWidth;
|
||||||
|
private int _pictureHeight;
|
||||||
|
protected int _startPosX;
|
||||||
|
protected int _startPosY;
|
||||||
|
public IMoveableObject GetMoveableObject() { return new DrawingObjectTruck(this);}
|
||||||
|
public int GetPosX() {
|
||||||
|
return _startPosX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetPosY() {
|
||||||
|
return _startPosY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetWidth() {
|
||||||
|
return _truckWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetHeight() {
|
||||||
|
return _truckHeight;
|
||||||
|
}
|
||||||
|
protected int _truckWidth = 80;
|
||||||
|
protected int _truckHeight = 70;
|
||||||
|
public DrawingTruck(EntityTruck truck, IDrawingWheels _wheelDrawing, int width, int height){
|
||||||
|
if (height < _truckHeight || width < _truckWidth)
|
||||||
|
return;
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
entityTruck = truck;
|
||||||
|
drawingWheels = _wheelDrawing;
|
||||||
|
drawingWheels.SetWheelNumber(entityTruck.Numwheel);
|
||||||
|
}
|
||||||
|
public DrawingTruck(int speed, double weight, Color mainColor, int width, int height, int wheelNumber) {
|
||||||
|
if (width < _truckWidth || height < _truckHeight) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
entityTruck = new EntityTruck(speed, weight, mainColor, wheelNumber);
|
||||||
|
Random rand = new Random();
|
||||||
|
drawingWheels = switch (rand.nextInt(0, 3)) {
|
||||||
|
case 0 -> new DrawingWheels();
|
||||||
|
case 1 -> new DrawingOneLineWheels();
|
||||||
|
case 2 -> new DrawingTwoLineWheels();
|
||||||
|
default -> new DrawingWheels();
|
||||||
|
};
|
||||||
|
drawingWheels.SetWheelNumber(wheelNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DrawingTruck(int speed, double weight, Color mainColor,
|
||||||
|
int width, int height, int truckWidth, int truckHeight, int wheelNumber) {
|
||||||
|
if (width < truckWidth || height < truckHeight) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_pictureWidth = width;
|
||||||
|
_pictureHeight = height;
|
||||||
|
_truckWidth = truckWidth;
|
||||||
|
_truckHeight = truckHeight;
|
||||||
|
entityTruck = new EntityTruck(speed, weight, mainColor, wheelNumber);
|
||||||
|
Random rand = new Random();
|
||||||
|
drawingWheels = switch (rand.nextInt(0, 3)) {
|
||||||
|
case 0 -> new DrawingWheels();
|
||||||
|
case 1 -> new DrawingOneLineWheels();
|
||||||
|
case 2 -> new DrawingTwoLineWheels();
|
||||||
|
default -> new DrawingWheels();
|
||||||
|
};
|
||||||
|
drawingWheels.SetWheelNumber(wheelNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetPosition(int x, int y) {
|
||||||
|
if (x < 0)
|
||||||
|
{
|
||||||
|
x = 0;
|
||||||
|
}
|
||||||
|
else if (x + _truckWidth > _pictureWidth)
|
||||||
|
{
|
||||||
|
x = _pictureWidth - _truckWidth;
|
||||||
|
}
|
||||||
|
if (y < 0)
|
||||||
|
{
|
||||||
|
y = 0;
|
||||||
|
}
|
||||||
|
else if (y + _truckHeight > _pictureHeight)
|
||||||
|
{
|
||||||
|
y = _pictureHeight - _truckHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
_startPosX = x;
|
||||||
|
_startPosY = y;
|
||||||
|
}
|
||||||
|
public boolean CanMove(DirectionType direction) {
|
||||||
|
if (entityTruck == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return switch (direction) {
|
||||||
|
case Left -> _startPosX - entityTruck.GetStep() > 0;
|
||||||
|
case Up -> _startPosY - entityTruck.GetStep() > 0;
|
||||||
|
case Right -> _startPosX + _truckWidth + entityTruck.GetStep() < _pictureWidth;
|
||||||
|
case Down -> _startPosY + _truckHeight + entityTruck.GetStep() < _pictureHeight;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MoveTransport(DirectionType direction) {
|
||||||
|
if (!CanMove(direction) || entityTruck == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (direction) {
|
||||||
|
case Left:
|
||||||
|
_startPosX -= (int) entityTruck.GetStep();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Up:
|
||||||
|
_startPosY -= (int) entityTruck.GetStep();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Right:
|
||||||
|
_startPosX += (int) entityTruck.GetStep();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Down:
|
||||||
|
_startPosY += (int) entityTruck.GetStep();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawTransport(Graphics g) {
|
||||||
|
if (entityTruck == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
drawingWheels.drawWheels(g,entityTruck.GetBodyColor(), _startPosX, _startPosY);
|
||||||
|
g.setColor(entityTruck.GetBodyColor());
|
||||||
|
g.drawLine(_startPosX + 20, _startPosY + 20, _startPosX + 70, _startPosY + 20);
|
||||||
|
|
||||||
|
g.drawRect( _startPosX + 60, _startPosY, 10, 20);
|
||||||
|
|
||||||
|
g.fillRect(_startPosX + 60, _startPosY, 10, 20);
|
||||||
|
}
|
||||||
|
}
|
22
src/RoadTrain/Entities/EntityRoadTrain.java
Normal file
22
src/RoadTrain/Entities/EntityRoadTrain.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package RoadTrain.Entities;
|
||||||
|
import java.awt.*;
|
||||||
|
public class EntityRoadTrain extends EntityTruck {
|
||||||
|
private Color AdditionalColor;
|
||||||
|
public Color GetAdditionalColor() { return AdditionalColor; }
|
||||||
|
private boolean WaterContainer;
|
||||||
|
public boolean GetWaterContainer(){
|
||||||
|
return WaterContainer;
|
||||||
|
}
|
||||||
|
private boolean SweepingBrush;
|
||||||
|
public boolean GetSweepingBrush(){
|
||||||
|
return SweepingBrush;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EntityRoadTrain(int speed, double weight, Color bodyColor, int numwheel, Color
|
||||||
|
additionalColor, boolean waterContainer, boolean sweepingBrush) {
|
||||||
|
super(speed, weight, bodyColor, numwheel);
|
||||||
|
AdditionalColor = additionalColor;
|
||||||
|
WaterContainer = waterContainer;
|
||||||
|
SweepingBrush = sweepingBrush;
|
||||||
|
}
|
||||||
|
}
|
33
src/RoadTrain/Entities/EntityTruck.java
Normal file
33
src/RoadTrain/Entities/EntityTruck.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package RoadTrain.Entities;
|
||||||
|
import java.awt.*;
|
||||||
|
public class EntityTruck {
|
||||||
|
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 int Numwheel;
|
||||||
|
|
||||||
|
public double GetStep() {
|
||||||
|
return (double) Speed * 100 / Weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EntityTruck(int speed, double weight, Color bodyColor, int numwheel) {
|
||||||
|
Speed = speed;
|
||||||
|
Weight = weight;
|
||||||
|
BodyColor = bodyColor;
|
||||||
|
Numwheel = numwheel;
|
||||||
|
}
|
||||||
|
}
|
58
src/RoadTrain/Extra/DrawingOneLineWheels.java
Normal file
58
src/RoadTrain/Extra/DrawingOneLineWheels.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package RoadTrain.Extra;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingOneLineWheels implements IDrawingWheels{
|
||||||
|
private WheelNumber wheelNumber;
|
||||||
|
@Override
|
||||||
|
public void SetWheelNumber(int num) {
|
||||||
|
switch (num){
|
||||||
|
case 1:
|
||||||
|
wheelNumber = WheelNumber.One;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
wheelNumber = WheelNumber.Two;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
wheelNumber = WheelNumber.Three;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WheelNumber GetWheelNumber() {
|
||||||
|
return wheelNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawWheels(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
switch (wheelNumber) {
|
||||||
|
case One:
|
||||||
|
drawOneWheel(g, color, startPosX, startPosY);
|
||||||
|
break;
|
||||||
|
case Two:
|
||||||
|
drawTwoWheels(g, color, startPosX, startPosY);
|
||||||
|
break;
|
||||||
|
case Three:
|
||||||
|
drawThreeWheels(g, color, startPosX, startPosY);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void drawOneWheel(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
drawWheel(g,color,startPosX+20,startPosY+20);
|
||||||
|
}
|
||||||
|
private void drawTwoWheels(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
drawOneWheel(g, color, startPosX, startPosY);
|
||||||
|
drawWheel(g,color,startPosX+30,startPosY+20);
|
||||||
|
}
|
||||||
|
private void drawThreeWheels(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
drawTwoWheels(g, color, startPosX, startPosY);
|
||||||
|
drawWheel(g,color,startPosX+60,startPosY+20);
|
||||||
|
}
|
||||||
|
private void drawWheel(Graphics g, Color color, int X, int Y){
|
||||||
|
g.setColor(color);
|
||||||
|
g.drawOval(X,Y,10,10);
|
||||||
|
g.fillOval(X,Y,10,10);
|
||||||
|
g.setColor(Color.black);
|
||||||
|
g.drawLine(X,Y+5,X+10,Y+5);
|
||||||
|
}
|
||||||
|
}
|
59
src/RoadTrain/Extra/DrawingTwoLineWheels.java
Normal file
59
src/RoadTrain/Extra/DrawingTwoLineWheels.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package RoadTrain.Extra;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawingTwoLineWheels implements IDrawingWheels{
|
||||||
|
private WheelNumber wheelNumber;
|
||||||
|
@Override
|
||||||
|
public void SetWheelNumber(int num) {
|
||||||
|
switch (num){
|
||||||
|
case 1:
|
||||||
|
wheelNumber = WheelNumber.One;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
wheelNumber = WheelNumber.Two;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
wheelNumber = WheelNumber.Three;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WheelNumber GetWheelNumber() {
|
||||||
|
return wheelNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drawWheels(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
switch (wheelNumber) {
|
||||||
|
case One:
|
||||||
|
drawOneWheel(g, color, startPosX, startPosY);
|
||||||
|
break;
|
||||||
|
case Two:
|
||||||
|
drawTwoWheels(g, color, startPosX, startPosY);
|
||||||
|
break;
|
||||||
|
case Three:
|
||||||
|
drawThreeWheels(g, color, startPosX, startPosY);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void drawOneWheel(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
drawWheel(g,color,startPosX+20,startPosY+20);
|
||||||
|
}
|
||||||
|
private void drawTwoWheels(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
drawOneWheel(g, color, startPosX, startPosY);
|
||||||
|
drawWheel(g,color,startPosX+30,startPosY+20);
|
||||||
|
}
|
||||||
|
private void drawThreeWheels(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
drawTwoWheels(g, color, startPosX, startPosY);
|
||||||
|
drawWheel(g,color,startPosX+60,startPosY+20);
|
||||||
|
}
|
||||||
|
private void drawWheel(Graphics g, Color color, int X, int Y){
|
||||||
|
g.setColor(color);
|
||||||
|
g.drawOval(X,Y,10,10);
|
||||||
|
g.fillOval(X,Y,10,10);
|
||||||
|
g.setColor(Color.black);
|
||||||
|
g.drawLine(X,Y+5,X+10,Y+5);
|
||||||
|
g.drawLine(X+5, Y,X+5,Y+10);
|
||||||
|
}
|
||||||
|
}
|
67
src/RoadTrain/Extra/DrawingWheels.java
Normal file
67
src/RoadTrain/Extra/DrawingWheels.java
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package RoadTrain.Extra;
|
||||||
|
import java.awt.*;
|
||||||
|
public class DrawingWheels implements IDrawingWheels{
|
||||||
|
private WheelNumber wheelNumber;
|
||||||
|
@Override
|
||||||
|
public void SetWheelNumber(int num){
|
||||||
|
switch (num){
|
||||||
|
case 1:
|
||||||
|
wheelNumber = WheelNumber.One;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
wheelNumber = WheelNumber.Two;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
wheelNumber = WheelNumber.Three;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@Override
|
||||||
|
public WheelNumber GetWheelNumber() {
|
||||||
|
return wheelNumber;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void drawWheels(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
switch (wheelNumber) {
|
||||||
|
case One:
|
||||||
|
drawOneWheel(g, color, startPosX, startPosY);
|
||||||
|
break;
|
||||||
|
case Two:
|
||||||
|
drawTwoWheels(g, color, startPosX, startPosY);
|
||||||
|
break;
|
||||||
|
case Three:
|
||||||
|
drawThreeWheels(g, color, startPosX, startPosY);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void drawOneWheel(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
g.setColor(color);
|
||||||
|
|
||||||
|
g.drawOval(startPosX + 20, startPosY + 20, 10, 10);
|
||||||
|
|
||||||
|
g.fillOval(startPosX + 20, startPosY + 20, 10, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawTwoWheels(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
g.setColor(color);
|
||||||
|
|
||||||
|
g.drawOval(startPosX + 20, startPosY + 20, 10, 10);
|
||||||
|
|
||||||
|
g.drawOval( startPosX + 30, startPosY + 20, 10, 10);
|
||||||
|
|
||||||
|
g.fillOval(startPosX + 20, startPosY + 20, 10, 10);
|
||||||
|
g.fillOval(startPosX + 30, startPosY + 20, 10, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawThreeWheels(Graphics g, Color color, int startPosX, int startPosY) {
|
||||||
|
g.setColor(color);
|
||||||
|
|
||||||
|
g.drawOval(startPosX + 20, startPosY + 20, 10, 10);
|
||||||
|
|
||||||
|
g.drawOval( startPosX + 30, startPosY + 20, 10, 10);
|
||||||
|
g.drawOval(startPosX + 60, startPosY + 20, 10, 10);
|
||||||
|
|
||||||
|
g.fillOval(startPosX + 20, startPosY + 20, 10, 10);
|
||||||
|
g.fillOval(startPosX + 30, startPosY + 20, 10, 10);
|
||||||
|
g.fillOval(startPosX + 60, startPosY + 20, 10, 10);
|
||||||
|
}
|
||||||
|
}
|
71
src/RoadTrain/Extra/GenericDopClass.java
Normal file
71
src/RoadTrain/Extra/GenericDopClass.java
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package RoadTrain.Extra;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
import RoadTrain.Entities.*;
|
||||||
|
import RoadTrain.DrawingObjects.*;
|
||||||
|
|
||||||
|
public class GenericDopClass<T extends EntityTruck, U extends IDrawingWheels> {
|
||||||
|
|
||||||
|
private ArrayList<T> Trucks;
|
||||||
|
private ArrayList<U> Wheels;
|
||||||
|
private int maxCountTrucks;
|
||||||
|
private int countTrucks;
|
||||||
|
private int maxCountWheels;
|
||||||
|
private int countWheels;
|
||||||
|
private Random random;
|
||||||
|
private int _pictureWidth;
|
||||||
|
private int _pictureHeight;
|
||||||
|
public GenericDopClass(int _maxCountTrucks, int _maxCountWheels, int pictureWidth, int pictureHeight){
|
||||||
|
maxCountTrucks = _maxCountTrucks;
|
||||||
|
maxCountWheels = _maxCountWheels;
|
||||||
|
Trucks = new ArrayList<T>(maxCountTrucks);
|
||||||
|
Wheels = new ArrayList<U>(maxCountWheels);
|
||||||
|
countTrucks = 0;
|
||||||
|
countWheels = 0;
|
||||||
|
_pictureWidth = pictureWidth;
|
||||||
|
_pictureHeight = pictureHeight;
|
||||||
|
random = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean add(T train){
|
||||||
|
if (train == null)
|
||||||
|
return false;
|
||||||
|
if (countTrucks > maxCountTrucks)
|
||||||
|
return false;
|
||||||
|
Trucks.add(countTrucks++, train);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean add(U wheel){
|
||||||
|
if (wheel == null)
|
||||||
|
return false;
|
||||||
|
if (countWheels > maxCountWheels)
|
||||||
|
return false;
|
||||||
|
Wheels.add(countWheels++, wheel);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingTruck getDrawingObject(){
|
||||||
|
if (countTrucks == 0 || countWheels == 0)
|
||||||
|
return null;
|
||||||
|
if(Trucks.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
T truck = Trucks.get(random.nextInt(Trucks.size()));
|
||||||
|
U wheel = null;
|
||||||
|
|
||||||
|
if(!Wheels.isEmpty()){
|
||||||
|
wheel = Wheels.get(random.nextInt(Wheels.size()));
|
||||||
|
}
|
||||||
|
DrawingTruck drawingTruck;
|
||||||
|
if (truck instanceof EntityRoadTrain){
|
||||||
|
drawingTruck = new DrawingRoadTrain((EntityRoadTrain)truck, wheel, _pictureWidth, _pictureHeight);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
drawingTruck = new DrawingTruck(truck, wheel, _pictureWidth, _pictureHeight);
|
||||||
|
}
|
||||||
|
return drawingTruck;
|
||||||
|
}
|
||||||
|
}
|
9
src/RoadTrain/Extra/IDrawingWheels.java
Normal file
9
src/RoadTrain/Extra/IDrawingWheels.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package RoadTrain.Extra;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public interface IDrawingWheels {
|
||||||
|
void SetWheelNumber(int number);
|
||||||
|
WheelNumber GetWheelNumber();
|
||||||
|
void drawWheels(Graphics g, Color color, int startPosX, int startPosY);
|
||||||
|
}
|
6
src/RoadTrain/Extra/WheelNumber.java
Normal file
6
src/RoadTrain/Extra/WheelNumber.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package RoadTrain.Extra;
|
||||||
|
public enum WheelNumber {
|
||||||
|
One,
|
||||||
|
Two,
|
||||||
|
Three
|
||||||
|
}
|
65
src/RoadTrain/FormGenericDopClass.java
Normal file
65
src/RoadTrain/FormGenericDopClass.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package RoadTrain;
|
||||||
|
import RoadTrain.DrawingObjects.*;
|
||||||
|
import RoadTrain.Entities.*;
|
||||||
|
import RoadTrain.Extra.*;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JComponent;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
|
public class FormGenericDopClass extends JFrame {
|
||||||
|
static int pictureBoxWidth = 980;
|
||||||
|
static int pictureBoxHeight = 560;
|
||||||
|
public DrawingTruck _drawingTruck;
|
||||||
|
private class Canvas extends JComponent{
|
||||||
|
public Canvas(){
|
||||||
|
}
|
||||||
|
public void paintComponent (Graphics g){
|
||||||
|
if (_drawingTruck == null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.paintComponents (g) ;
|
||||||
|
Graphics2D g2d = (Graphics2D)g;
|
||||||
|
_drawingTruck.SetPosition(50, 50);
|
||||||
|
_drawingTruck.DrawTransport(g2d);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GenericDopClass<EntityTruck, IDrawingWheels> genericDopClass;
|
||||||
|
public FormGenericDopClass(){
|
||||||
|
_drawingTruck = null;
|
||||||
|
Canvas canv = new Canvas();
|
||||||
|
setSize (1000, 600);
|
||||||
|
setLayout(null);
|
||||||
|
canv.setBounds(0,0,pictureBoxWidth, pictureBoxHeight);
|
||||||
|
|
||||||
|
genericDopClass = new GenericDopClass<>(100, 100, pictureBoxWidth, pictureBoxHeight);
|
||||||
|
genericDopClass.add(new EntityTruck(100, 100, Color.BLUE, 1));
|
||||||
|
genericDopClass.add(new EntityTruck(100, 100, Color.RED, 2));
|
||||||
|
genericDopClass.add(new EntityTruck(100, 100, Color.GRAY, 1));
|
||||||
|
genericDopClass.add(new EntityRoadTrain(100, 100, Color.BLUE, 3, Color.ORANGE, true, true));
|
||||||
|
genericDopClass.add(new DrawingOneLineWheels());
|
||||||
|
genericDopClass.add(new DrawingTwoLineWheels());
|
||||||
|
|
||||||
|
JButton createButton = new JButton("Создать");
|
||||||
|
createButton.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e){
|
||||||
|
_drawingTruck = genericDopClass.getDrawingObject();
|
||||||
|
canv.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
createButton.setBounds(pictureBoxWidth/2, pictureBoxHeight-20, 120, 20);
|
||||||
|
|
||||||
|
add(canv);
|
||||||
|
add(createButton);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
125
src/RoadTrain/FormTruckCollection.java
Normal file
125
src/RoadTrain/FormTruckCollection.java
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
package RoadTrain;
|
||||||
|
|
||||||
|
import RoadTrain.MovementStraregy.*;
|
||||||
|
import RoadTrain.Generics.*;
|
||||||
|
import RoadTrain.DrawingObjects.*;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
public class FormTruckCollection {
|
||||||
|
private class Canvas extends JComponent {
|
||||||
|
public TrucksGenericCollection<DrawingTruck, DrawingObjectTruck> _truck;
|
||||||
|
public Canvas() {
|
||||||
|
}
|
||||||
|
public void paintComponent (Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
if (_truck.ShowTrucks() != null) {
|
||||||
|
g.drawImage(_truck.ShowTrucks(), 0, 10, this);
|
||||||
|
}
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Canvas canv;
|
||||||
|
static int pictureBoxWidth = 700;
|
||||||
|
static int pictureBoxHeight = 480;
|
||||||
|
private TrucksGenericCollection<DrawingTruck, DrawingObjectTruck> _truck;
|
||||||
|
public void Draw(){
|
||||||
|
canv.repaint();
|
||||||
|
}
|
||||||
|
FormTruckCollection() {
|
||||||
|
canv = new Canvas();
|
||||||
|
JFrame Frame = new JFrame ("TrucksCollecltion");
|
||||||
|
_truck = new TrucksGenericCollection<DrawingTruck, DrawingObjectTruck>(pictureBoxWidth, pictureBoxHeight);
|
||||||
|
canv._truck = _truck;
|
||||||
|
|
||||||
|
JButton ButtonAddTruck = new JButton("Добавить технику");
|
||||||
|
|
||||||
|
ButtonAddTruck.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
RoadTrainForm form = new RoadTrainForm();
|
||||||
|
form.buttonSelectTruck.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (_truck.Add(form._drawingTruck) != -1) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
Draw();
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
}
|
||||||
|
form.frame.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
JTextField TextBoxNumber = new JTextField();
|
||||||
|
JButton ButtonRemoveTruck = new JButton("Удалить технику");
|
||||||
|
ButtonRemoveTruck.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (char it : TextBoxNumber.getText().toCharArray())
|
||||||
|
if (it < '0' || it > '9') {
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (TextBoxNumber.getText().length() == 0) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pos = Integer.parseInt(TextBoxNumber.getText());
|
||||||
|
if (_truck.remove(pos) != null) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
Draw();
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
JButton ButtonRefreshCollection = new JButton("Обновить коллекцию");
|
||||||
|
ButtonRefreshCollection.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e){
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
JButton FormTruckGenerate = new JButton("Генерировать");
|
||||||
|
FormTruckGenerate.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
FormGenericDopClass formGenericDopClass = new FormGenericDopClass();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Frame.setSize (880, 520);
|
||||||
|
Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
||||||
|
Frame.setLayout(null);
|
||||||
|
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
|
||||||
|
ButtonAddTruck.setBounds(pictureBoxWidth - 50, 10, 170, 30);
|
||||||
|
TextBoxNumber.setBounds(pictureBoxWidth - 50, 50, 170, 20);
|
||||||
|
ButtonRemoveTruck.setBounds(pictureBoxWidth - 50, 80, 170, 30);
|
||||||
|
ButtonRefreshCollection.setBounds(pictureBoxWidth - 50, 120, 170, 30);
|
||||||
|
FormTruckGenerate.setBounds(pictureBoxWidth - 50, 160, 170, 30);
|
||||||
|
Frame.add(canv);
|
||||||
|
Frame.add(ButtonAddTruck);
|
||||||
|
Frame.add(ButtonRemoveTruck);
|
||||||
|
Frame.add(ButtonRefreshCollection);
|
||||||
|
Frame.add(TextBoxNumber);
|
||||||
|
Frame.add(FormTruckGenerate);
|
||||||
|
Frame.setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
63
src/RoadTrain/Generics/SetGeneric.java
Normal file
63
src/RoadTrain/Generics/SetGeneric.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package RoadTrain.Generics;
|
||||||
|
|
||||||
|
public class SetGeneric<T extends Object> {
|
||||||
|
// Массив объектов, которые храним
|
||||||
|
private Object[] _places;
|
||||||
|
|
||||||
|
// Количество объектов в массиве
|
||||||
|
public int Count;
|
||||||
|
|
||||||
|
// Конструктор
|
||||||
|
public SetGeneric(int count) {
|
||||||
|
_places = new Object[count];
|
||||||
|
Count = _places.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Добавление объекта в набор
|
||||||
|
public int Insert(T truck) {
|
||||||
|
int i = 0;
|
||||||
|
for (; i < _places.length; i++) {
|
||||||
|
if (_places[i] == null)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (i == _places.length)
|
||||||
|
return -1;
|
||||||
|
for (; i > 0; i--) {
|
||||||
|
_places[i] = _places[i - 1];
|
||||||
|
}
|
||||||
|
_places[i] = truck;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Добавление объекта в набор на конкретную позицию
|
||||||
|
public boolean Insert(T truck, int position) {
|
||||||
|
if (position < 0 || position >= _places.length)
|
||||||
|
return false;
|
||||||
|
for (; position < _places.length; position++) {
|
||||||
|
if (_places[position] == null)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (position == _places.length)
|
||||||
|
return false;
|
||||||
|
for (; position > 0; position--) {
|
||||||
|
_places[position] = _places[position - 1];
|
||||||
|
}
|
||||||
|
_places[position] = truck;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Удаление объекта из набора с конкретной позиции
|
||||||
|
public boolean Remove(int position) {
|
||||||
|
if (position < 0 || position >= _places.length)
|
||||||
|
return false;
|
||||||
|
_places[position] = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Получение объекта из набора по позиции
|
||||||
|
public T Get(int position) {
|
||||||
|
if (position < 0 || position >= _places.length)
|
||||||
|
return null;
|
||||||
|
return (T) _places[position];
|
||||||
|
}
|
||||||
|
}
|
85
src/RoadTrain/Generics/TrucksGenericCollection.java
Normal file
85
src/RoadTrain/Generics/TrucksGenericCollection.java
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package RoadTrain.Generics;
|
||||||
|
import RoadTrain.DrawingObjects.*;
|
||||||
|
import RoadTrain.MovementStraregy.*;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
public class TrucksGenericCollection<T extends DrawingTruck, U extends IMoveableObject> {
|
||||||
|
// Высота и Ширина окна прорисовки
|
||||||
|
private int _pictureWidth;
|
||||||
|
private int _pictureHeight;
|
||||||
|
|
||||||
|
// Размер занимаемого объектом места (ширина и высота)
|
||||||
|
private int _placeSizeWidth = 180;
|
||||||
|
private int _placeSizeHeight = 90;
|
||||||
|
|
||||||
|
// Набор объектов
|
||||||
|
private SetGeneric<T> _collection;
|
||||||
|
|
||||||
|
// Конструктор
|
||||||
|
public TrucksGenericCollection(int pictureWidth, int pictureHeight) {
|
||||||
|
int width = pictureWidth / _placeSizeWidth;
|
||||||
|
int height = pictureHeight / _placeSizeHeight;
|
||||||
|
_pictureWidth = pictureWidth;
|
||||||
|
_pictureHeight = pictureHeight;
|
||||||
|
_collection = new SetGeneric<T>(width * height);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Перегрузка оператора сложения
|
||||||
|
public int Add(T obj) {
|
||||||
|
if (obj == null) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return _collection.Insert(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Перегрузка оператора вычитания
|
||||||
|
public T remove(int pos) {
|
||||||
|
T obj = _collection.Get(pos);
|
||||||
|
if (obj != null) {
|
||||||
|
_collection.Remove(pos);
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Получение объекта IMoveableObject
|
||||||
|
public U GetU(int pos) {
|
||||||
|
return (U) _collection.Get(pos).GetMoveableObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Вывод всего набора объектов
|
||||||
|
public BufferedImage ShowTrucks() {
|
||||||
|
BufferedImage bitmap = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||||||
|
Graphics2D g = bitmap.createGraphics();
|
||||||
|
DrawBackground(g);
|
||||||
|
DrawObjects(g);
|
||||||
|
g.dispose();
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Метод отрисовки фона
|
||||||
|
private void DrawBackground(Graphics g) {
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) {
|
||||||
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
|
||||||
|
// Линия разметки места
|
||||||
|
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawObjects(Graphics g) {
|
||||||
|
for (int i = 0; i < _collection.Count; i++) {
|
||||||
|
T t = _collection.Get(i);
|
||||||
|
if (t != null) {
|
||||||
|
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
|
||||||
|
if (t instanceof DrawingRoadTrain)
|
||||||
|
((DrawingRoadTrain) t).DrawTransport((Graphics2D) g);
|
||||||
|
else
|
||||||
|
t.DrawTransport((Graphics2D) g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
src/RoadTrain/Main.java
Normal file
7
src/RoadTrain/Main.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package RoadTrain;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args){
|
||||||
|
FormTruckCollection formTruckCollection = new FormTruckCollection();
|
||||||
|
}
|
||||||
|
}
|
93
src/RoadTrain/MovementStraregy/AbstractStrategy.java
Normal file
93
src/RoadTrain/MovementStraregy/AbstractStrategy.java
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
package RoadTrain.MovementStraregy;
|
||||||
|
import RoadTrain.DirectionType;
|
||||||
|
public abstract class AbstractStrategy {
|
||||||
|
private IMoveableObject moveableObject;
|
||||||
|
|
||||||
|
private Status state = Status.NotInit;
|
||||||
|
|
||||||
|
private int fieldWidth;
|
||||||
|
|
||||||
|
protected int GetFieldWidth() {
|
||||||
|
return fieldWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int fieldHeight;
|
||||||
|
|
||||||
|
protected int GetFieldHeight() {
|
||||||
|
return fieldHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Status GetStatus() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetData(IMoveableObject moveableObject, int width, int height) {
|
||||||
|
if (moveableObject == null) {
|
||||||
|
state = Status.NotInit;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state = Status.InProgress;
|
||||||
|
this.moveableObject = moveableObject;
|
||||||
|
fieldWidth = width;
|
||||||
|
fieldHeight = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MakeStep() {
|
||||||
|
if (state != Status.InProgress) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isTargetDestination()) {
|
||||||
|
state = Status.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 != Status.InProgress) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return moveableObject.GetStep();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void MoveToTarget();
|
||||||
|
|
||||||
|
protected abstract boolean isTargetDestination();
|
||||||
|
|
||||||
|
private boolean MoveTo(DirectionType directionType) {
|
||||||
|
if (state != Status.InProgress) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (moveableObject == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (moveableObject.CheckCanMove(directionType)) {
|
||||||
|
moveableObject.MoveObject(directionType);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
39
src/RoadTrain/MovementStraregy/DrawingObjectTruck.java
Normal file
39
src/RoadTrain/MovementStraregy/DrawingObjectTruck.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package RoadTrain.MovementStraregy;
|
||||||
|
import RoadTrain.DirectionType;
|
||||||
|
import RoadTrain.DrawingObjects.*;
|
||||||
|
|
||||||
|
public class DrawingObjectTruck implements IMoveableObject{
|
||||||
|
private DrawingTruck _drawingTruck = null;
|
||||||
|
public DrawingObjectTruck (DrawingTruck drawingTruck)
|
||||||
|
{
|
||||||
|
_drawingTruck = drawingTruck;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public ObjectParameters GetObjectPosition() {
|
||||||
|
if (_drawingTruck == null || _drawingTruck.entityTruck == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new ObjectParameters(_drawingTruck.GetPosX(), _drawingTruck.GetPosY(),
|
||||||
|
_drawingTruck.GetWidth(), _drawingTruck.GetHeight());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int GetStep() {
|
||||||
|
if (_drawingTruck != null && _drawingTruck.entityTruck!=null)
|
||||||
|
return (int)(_drawingTruck.entityTruck.GetStep());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean CheckCanMove(DirectionType direction) {
|
||||||
|
if (_drawingTruck != null)
|
||||||
|
return _drawingTruck.CanMove(direction);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void MoveObject(DirectionType direction) {
|
||||||
|
if (_drawingTruck != null)
|
||||||
|
_drawingTruck.MoveTransport(direction);
|
||||||
|
}
|
||||||
|
}
|
12
src/RoadTrain/MovementStraregy/IMoveableObject.java
Normal file
12
src/RoadTrain/MovementStraregy/IMoveableObject.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package RoadTrain.MovementStraregy;
|
||||||
|
import RoadTrain.DirectionType;
|
||||||
|
|
||||||
|
public interface IMoveableObject{
|
||||||
|
ObjectParameters GetObjectPosition();
|
||||||
|
|
||||||
|
int GetStep();
|
||||||
|
|
||||||
|
boolean CheckCanMove(DirectionType direction);
|
||||||
|
|
||||||
|
void MoveObject(DirectionType direction);
|
||||||
|
}
|
35
src/RoadTrain/MovementStraregy/MoveToBorder.java
Normal file
35
src/RoadTrain/MovementStraregy/MoveToBorder.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package RoadTrain.MovementStraregy;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
39
src/RoadTrain/MovementStraregy/MoveToCenter.java
Normal file
39
src/RoadTrain/MovementStraregy/MoveToCenter.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package RoadTrain.MovementStraregy;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
41
src/RoadTrain/MovementStraregy/ObjectParameters.java
Normal file
41
src/RoadTrain/MovementStraregy/ObjectParameters.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package RoadTrain.MovementStraregy;
|
||||||
|
public class ObjectParameters {
|
||||||
|
private final int _x;
|
||||||
|
|
||||||
|
private final int _y;
|
||||||
|
|
||||||
|
private final int _width;
|
||||||
|
|
||||||
|
private final 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;
|
||||||
|
}
|
||||||
|
}
|
6
src/RoadTrain/MovementStraregy/Status.java
Normal file
6
src/RoadTrain/MovementStraregy/Status.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package RoadTrain.MovementStraregy;
|
||||||
|
public enum Status {
|
||||||
|
NotInit,
|
||||||
|
InProgress,
|
||||||
|
Finish
|
||||||
|
}
|
194
src/RoadTrain/RoadTrainForm.java
Normal file
194
src/RoadTrain/RoadTrainForm.java
Normal file
@ -0,0 +1,194 @@
|
|||||||
|
package RoadTrain;
|
||||||
|
|
||||||
|
import RoadTrain.DrawingObjects.*;
|
||||||
|
import RoadTrain.MovementStraregy.*;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class RoadTrainForm{
|
||||||
|
public DrawingTruck _drawingTruck;
|
||||||
|
private AbstractStrategy _abstractStrategy;
|
||||||
|
public JButton buttonSelectTruck;
|
||||||
|
Canvas canv;
|
||||||
|
public JFrame frame;
|
||||||
|
public void Draw(){
|
||||||
|
canv.repaint();
|
||||||
|
}
|
||||||
|
public RoadTrainForm(){
|
||||||
|
frame = new JFrame("RoadTrain");
|
||||||
|
JButton buttonCreateTruck = new JButton("Создать грузовик");
|
||||||
|
buttonCreateTruck.setFocusPainted(false);
|
||||||
|
buttonCreateTruck.setContentAreaFilled(false);
|
||||||
|
buttonSelectTruck = new JButton("Добавить грузовик");
|
||||||
|
buttonSelectTruck.setFocusPainted(false);
|
||||||
|
buttonSelectTruck.setContentAreaFilled(false);
|
||||||
|
JButton buttonCreateRoadTrain = new JButton("Создать очистительную машину");
|
||||||
|
buttonCreateRoadTrain.setFocusPainted(false);
|
||||||
|
buttonCreateRoadTrain.setContentAreaFilled(false);
|
||||||
|
String[] items = {
|
||||||
|
"Form center",
|
||||||
|
"Form border"
|
||||||
|
};
|
||||||
|
JComboBox comboBoxStrategy = new JComboBox(items);
|
||||||
|
JButton buttonStep = new JButton("Шаг");
|
||||||
|
buttonStep.setFocusPainted(false);
|
||||||
|
buttonStep.setContentAreaFilled(false);
|
||||||
|
JButton buttonUp = new JButton();
|
||||||
|
buttonUp.setFocusPainted(false);
|
||||||
|
buttonUp.setContentAreaFilled(false);
|
||||||
|
buttonUp.setName("up");
|
||||||
|
buttonUp.setIcon(new ImageIcon(((new ImageIcon("Resources/ArrowUp.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
JButton buttonDown = new JButton();
|
||||||
|
buttonDown.setFocusPainted(false);
|
||||||
|
buttonDown.setContentAreaFilled(false);
|
||||||
|
buttonDown.setName("down");
|
||||||
|
buttonDown.setIcon(new ImageIcon(((new ImageIcon("Resources/ArrowDown.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
JButton buttonLeft = new JButton();
|
||||||
|
buttonLeft.setFocusPainted(false);
|
||||||
|
buttonLeft.setContentAreaFilled(false);
|
||||||
|
buttonLeft.setName("left");
|
||||||
|
buttonLeft.setIcon(new ImageIcon(((new ImageIcon("Resources/ArrowLeft.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
JButton buttonRight = new JButton();
|
||||||
|
buttonRight.setFocusPainted(false);
|
||||||
|
buttonRight.setContentAreaFilled(false);
|
||||||
|
buttonRight.setName("right");
|
||||||
|
buttonRight.setIcon(new ImageIcon(((new ImageIcon("Resources/ArrowRight.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
buttonCreateTruck.addActionListener(
|
||||||
|
e -> {
|
||||||
|
Random random = new Random();
|
||||||
|
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||||
|
Color selectedColor = JColorChooser.showDialog(frame, "Выберите цвет", Color.WHITE);
|
||||||
|
if (selectedColor != null)
|
||||||
|
{
|
||||||
|
color = selectedColor;
|
||||||
|
}
|
||||||
|
_drawingTruck = new DrawingTruck(random.nextInt(200) + 100,
|
||||||
|
random.nextInt(2000) + 1000,
|
||||||
|
color, this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3));
|
||||||
|
_drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90));
|
||||||
|
canv._drawingTruck = _drawingTruck;
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
buttonCreateRoadTrain.addActionListener(
|
||||||
|
e -> {
|
||||||
|
Random random = new Random();
|
||||||
|
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||||
|
Color selectedColor = JColorChooser.showDialog(frame, "Выберите цвет", Color.WHITE);
|
||||||
|
if (selectedColor != null)
|
||||||
|
{
|
||||||
|
color = selectedColor;
|
||||||
|
}
|
||||||
|
Color color2 = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||||
|
selectedColor = JColorChooser.showDialog(frame, "Выберите цвет", Color.WHITE);
|
||||||
|
if (selectedColor != null)
|
||||||
|
{
|
||||||
|
color2 = selectedColor;
|
||||||
|
}
|
||||||
|
_drawingTruck = new DrawingRoadTrain(random.nextInt(200) + 100,
|
||||||
|
random.nextInt(2000) + 1000,
|
||||||
|
color, color2, random.nextBoolean(), random.nextBoolean(),
|
||||||
|
this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3));
|
||||||
|
_drawingTruck.SetPosition(random.nextInt(100), random.nextInt(90));
|
||||||
|
canv._drawingTruck = _drawingTruck;
|
||||||
|
Draw();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
ActionListener actionListener = e -> {
|
||||||
|
if (_drawingTruck == null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch ((((JButton)(e.getSource())).getName())){
|
||||||
|
case "up":
|
||||||
|
_drawingTruck.MoveTransport(DirectionType.Up);
|
||||||
|
break;
|
||||||
|
case "down":
|
||||||
|
_drawingTruck.MoveTransport(DirectionType.Down);
|
||||||
|
break;
|
||||||
|
case "left":
|
||||||
|
_drawingTruck.MoveTransport(DirectionType.Left);
|
||||||
|
break;
|
||||||
|
case "right":
|
||||||
|
_drawingTruck.MoveTransport(DirectionType.Right);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Draw();
|
||||||
|
};
|
||||||
|
buttonUp.addActionListener(actionListener);
|
||||||
|
buttonDown.addActionListener(actionListener);
|
||||||
|
buttonLeft.addActionListener(actionListener);
|
||||||
|
buttonRight.addActionListener(actionListener);
|
||||||
|
buttonStep.addActionListener(e -> {
|
||||||
|
if (_drawingTruck == 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 DrawingObjectTruck(_drawingTruck), this.canv.getWidth(), this.canv.getHeight());
|
||||||
|
}
|
||||||
|
if (_abstractStrategy == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
comboBoxStrategy.setEnabled(false);
|
||||||
|
_abstractStrategy.MakeStep();
|
||||||
|
Draw();
|
||||||
|
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||||
|
{
|
||||||
|
comboBoxStrategy.setEnabled(true);
|
||||||
|
_abstractStrategy = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
frame.setSize(910, 500);
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
frame.setLayout(null);
|
||||||
|
canv = new Canvas();
|
||||||
|
canv.setBounds(0, 0, 900, 500);
|
||||||
|
buttonCreateTruck.setBounds(20, 420, 100, 40);
|
||||||
|
buttonCreateRoadTrain.setBounds(140, 420, 100, 40);
|
||||||
|
buttonUp.setBounds(800, 380, 40, 40);
|
||||||
|
buttonDown.setBounds(800, 420, 40, 40);
|
||||||
|
buttonLeft.setBounds(760, 420, 40, 40);
|
||||||
|
buttonRight.setBounds(840, 420, 40, 40);
|
||||||
|
comboBoxStrategy.setBounds(800,10,100,50);
|
||||||
|
buttonStep.setBounds(800,80,100,40);
|
||||||
|
buttonSelectTruck.setBounds(740, 140, 150, 20);
|
||||||
|
frame.add(canv);
|
||||||
|
frame.add(buttonCreateTruck);
|
||||||
|
frame.add(buttonCreateRoadTrain);
|
||||||
|
frame.add(buttonUp);
|
||||||
|
frame.add(buttonDown);
|
||||||
|
frame.add(buttonLeft);
|
||||||
|
frame.add(buttonRight);
|
||||||
|
frame.add(comboBoxStrategy);
|
||||||
|
frame.add(buttonStep);
|
||||||
|
frame.add(buttonSelectTruck);
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
class Canvas extends JComponent{
|
||||||
|
public DrawingTruck _drawingTruck;
|
||||||
|
public Canvas(){}
|
||||||
|
|
||||||
|
public void paintComponent(Graphics g){
|
||||||
|
if (_drawingTruck == null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.paintComponents(g);
|
||||||
|
Graphics2D g2d = (Graphics2D)g;
|
||||||
|
_drawingTruck.DrawTransport(g2d);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user