Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
7b0ffff8ac | ||
|
aad0dc3598 | ||
|
b54203910b | ||
|
b1d21a1556 | ||
|
35c8287d32 |
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
11
.idea/PIbd-21_Kryukov_A_I_Excavator_HARD.iml
Normal file
11
.idea/PIbd-21_Kryukov_A_I_Excavator_HARD.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/PIbd-21_Kryukov_A_I_Excavator_HARD.iml" filepath="$PROJECT_DIR$/.idea/PIbd-21_Kryukov_A_I_Excavator_HARD.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
95
src/AbstractStrategy.java
Normal file
95
src/AbstractStrategy.java
Normal file
@ -0,0 +1,95 @@
|
||||
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(Direction.Left);
|
||||
}
|
||||
|
||||
protected boolean moveRight() {
|
||||
return moveTo(Direction.Right);
|
||||
}
|
||||
|
||||
protected boolean moveUp() {
|
||||
return moveTo(Direction.Up);
|
||||
}
|
||||
|
||||
protected boolean moveDown() {
|
||||
return moveTo(Direction.Down);
|
||||
}
|
||||
|
||||
protected ObjectParameters getObjectParameters() {
|
||||
if (moveableObject == null) {
|
||||
return null;
|
||||
}
|
||||
return moveableObject.getObjectPosition();
|
||||
}
|
||||
|
||||
protected Integer getStep() {
|
||||
if (state != Status.InProgress) {
|
||||
return null;
|
||||
}
|
||||
if (moveableObject == null) {
|
||||
return null;
|
||||
}
|
||||
return moveableObject.getStep();
|
||||
}
|
||||
|
||||
protected abstract void moveToTarget();
|
||||
|
||||
protected abstract boolean isTargetDestination();
|
||||
|
||||
private boolean moveTo(Direction direction) {
|
||||
if (state != Status.InProgress) {
|
||||
return false;
|
||||
}
|
||||
if (moveableObject == null) {
|
||||
return false;
|
||||
}
|
||||
if (moveableObject.checkCanMove(direction)) {
|
||||
moveableObject.moveObject(direction);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
9
src/Direction.java
Normal file
9
src/Direction.java
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
|
||||
|
94
src/DrawningCrossRollers.java
Normal file
94
src/DrawningCrossRollers.java
Normal file
@ -0,0 +1,94 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningCrossRollers implements IDrawningRollers{
|
||||
private RollersCount rollersCount;
|
||||
private Color colorRollers;
|
||||
|
||||
public void setRollersCount(int count){
|
||||
switch (count) {
|
||||
case 4 -> rollersCount = RollersCount.Four;
|
||||
case 5 -> rollersCount = RollersCount.Five;
|
||||
case 6 -> rollersCount = RollersCount.Six;
|
||||
default -> rollersCount = RollersCount.Four;
|
||||
}
|
||||
}
|
||||
|
||||
public DrawningCrossRollers(int count, Color colorRollers){
|
||||
setRollersCount(count);
|
||||
this.colorRollers = colorRollers;
|
||||
}
|
||||
|
||||
public void DrawRollers(Graphics2D g, float _startPosX, float _startPosY){
|
||||
Color penColor = Color.BLACK;
|
||||
Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers;
|
||||
|
||||
// Крупные катки - всегда
|
||||
// 1
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
|
||||
// вертикальное перекрестие
|
||||
g.fillRect((int)_startPosX + 14, (int)_startPosY + 65, 5, 14);
|
||||
// горизонтальное перекрестие
|
||||
g.fillRect((int)_startPosX + 10, (int)_startPosY + 69, 13, 5);
|
||||
// 2
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
|
||||
// вертикальное перекрестие
|
||||
g.fillRect((int)_startPosX + 92, (int)_startPosY + 65, 5, 14);
|
||||
// горизонтальное перекрестие
|
||||
g.fillRect((int)_startPosX + 88, (int)_startPosY + 69, 13, 5);
|
||||
|
||||
// Малые катки - всегда
|
||||
// 1
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
|
||||
// вертикальное перекрестие
|
||||
g.fillRect((int)_startPosX + 46, (int)_startPosY + 60, 1, 3);
|
||||
// горизонтальное перекрестие
|
||||
g.fillRect((int)_startPosX + 45, (int)_startPosY + 61, 3, 1);
|
||||
// 2
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
|
||||
// вертикальное перекрестие
|
||||
g.fillRect((int)_startPosX + 64, (int)_startPosY + 60, 1, 3);
|
||||
// горизонтальное перекрестие
|
||||
g.fillRect((int)_startPosX + 63, (int)_startPosY + 61, 3, 1);
|
||||
|
||||
// Средние катки - не всегда
|
||||
switch (rollersCount){
|
||||
case Six:
|
||||
// 1
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
|
||||
// вертикальное перекрестие
|
||||
g.fillRect((int)_startPosX + 37, (int)_startPosY + 75, 2, 6);
|
||||
// горизонтальное перекрестие
|
||||
g.fillRect((int)_startPosX + 35, (int)_startPosY + 77, 6, 2);
|
||||
case Five:
|
||||
// 2
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
|
||||
// вертикальное перекрестие
|
||||
g.fillRect((int)_startPosX + 72, (int)_startPosY + 75, 2, 6);
|
||||
// горизонтальное перекрестие
|
||||
g.fillRect((int)_startPosX + 70, (int)_startPosY + 77, 6, 2);
|
||||
}
|
||||
|
||||
// Центры крупных катков
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6);
|
||||
g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6);
|
||||
}
|
||||
}
|
69
src/DrawningExcavator.java
Normal file
69
src/DrawningExcavator.java
Normal file
@ -0,0 +1,69 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningExcavator extends DrawningTracktor {
|
||||
|
||||
public DrawningExcavator(int speed, double weight, Color bodyColor, boolean bucket, boolean supports, Color bucketColor , Color supportsColor, int width, int height, int rollersCount)
|
||||
{
|
||||
super(speed, weight, bodyColor, width, height, 110, 87, rollersCount);
|
||||
if (getEntityTracktor() != null)
|
||||
{
|
||||
setEntityTracktor(new EntityExcavator(speed, weight, bodyColor,bucketColor,supportsColor, bucket, supports));
|
||||
}
|
||||
}
|
||||
public DrawningExcavator(EntityExcavator entityExcavator, int width, int height, int countRollers, IDrawningRollers iDrawningRollers){
|
||||
super(entityExcavator,width,height,countRollers,iDrawningRollers);
|
||||
if(getEntityTracktor() != null){
|
||||
setEntityTracktor(entityExcavator);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics2D g){
|
||||
if (!(getEntityTracktor() instanceof EntityExcavator excavator))
|
||||
{
|
||||
return;
|
||||
}
|
||||
Color pen;
|
||||
Color supportsBrush = excavator.getSupportsColor();
|
||||
Color bucketBrush = excavator.getBucketColor();
|
||||
if (excavator.getBucket())
|
||||
{
|
||||
pen = excavator.getBucketColor();
|
||||
g.setStroke(new BasicStroke(5));
|
||||
g.setColor(pen);
|
||||
g.drawLine((int)_startPosX + 1, (int)_startPosY + 90, (int)_startPosX + 15, (int)_startPosY + 70);
|
||||
g.drawLine((int)_startPosX + 15, (int)_startPosY + 72, (int)_startPosX + 15, (int)_startPosY + 50);
|
||||
g.drawLine((int)_startPosX + 15, (int)_startPosY + 52, (int)_startPosX + 10, (int)_startPosY + 45);
|
||||
g.drawLine((int)_startPosX + 15, (int)_startPosY + 60, (int)_startPosX + 40, (int)_startPosY + 50);
|
||||
g.setStroke(new BasicStroke(1));
|
||||
}
|
||||
_startPosX += 20;
|
||||
_startPosY += 5;
|
||||
super.DrawTransport(g);
|
||||
_startPosX -= 20;
|
||||
_startPosY -= 5;
|
||||
if (excavator.getSupports())
|
||||
{
|
||||
pen = Color.BLACK;
|
||||
g.setColor(supportsBrush);
|
||||
g.fillRect((int)_startPosX + 100, (int)_startPosY + 50, 10, 42);
|
||||
g.setColor(pen);
|
||||
g.drawRect((int)_startPosX + 100, (int)_startPosY + 50, 10, 42);
|
||||
|
||||
g.setColor(supportsBrush);
|
||||
g.fillRect((int)_startPosX + 90, (int)_startPosY + 82, 30, 10);
|
||||
g.setColor(pen);
|
||||
g.drawRect((int)_startPosX + 90, (int)_startPosY + 82, 30, 10);
|
||||
|
||||
g.setColor(supportsBrush);
|
||||
g.fillRect((int)_startPosX + 45, (int)_startPosY + 50, 10, 42);
|
||||
g.setColor(pen);
|
||||
g.drawRect((int)_startPosX + 45, (int)_startPosY + 50, 10, 42);
|
||||
|
||||
g.setColor(supportsBrush);
|
||||
g.fillRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10);
|
||||
g.setColor(pen);
|
||||
g.drawRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10);
|
||||
}
|
||||
}
|
||||
}
|
37
src/DrawningObjectExcavator.java
Normal file
37
src/DrawningObjectExcavator.java
Normal file
@ -0,0 +1,37 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningObjectExcavator implements IMoveableObject{
|
||||
private DrawningTracktor _tracktor = null;
|
||||
|
||||
public DrawningObjectExcavator(DrawningTracktor tracktor) {
|
||||
this._tracktor = tracktor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectParameters getObjectPosition(){
|
||||
if(_tracktor == null || _tracktor.getEntityTracktor() == null){
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_tracktor.getPosX(), _tracktor.getPosY(),_tracktor.getWidth(), _tracktor.getHeight());
|
||||
}
|
||||
@Override
|
||||
public int getStep() {
|
||||
if (_tracktor == null) {
|
||||
return 0;
|
||||
}
|
||||
return (int)((_tracktor.getEntityTracktor() != null)? _tracktor.getEntityTracktor().getStep() : 0);
|
||||
}
|
||||
@Override
|
||||
public boolean checkCanMove(Direction direction){
|
||||
if(_tracktor == null){
|
||||
return false;
|
||||
}
|
||||
return _tracktor.CanMove(direction);
|
||||
}
|
||||
@Override
|
||||
public void moveObject(Direction direction) {
|
||||
if (_tracktor != null) {
|
||||
_tracktor.MoveTransport(direction);
|
||||
}
|
||||
}
|
||||
}
|
68
src/DrawningRollers.java
Normal file
68
src/DrawningRollers.java
Normal file
@ -0,0 +1,68 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningRollers implements IDrawningRollers {
|
||||
private RollersCount rollersCount;
|
||||
private Color colorRollers;
|
||||
|
||||
public RollersCount getRollersCount(){
|
||||
return rollersCount;
|
||||
}
|
||||
@Override
|
||||
public void setRollersCount(int count){
|
||||
switch (count) {
|
||||
case 4 -> rollersCount = RollersCount.Four;
|
||||
case 5 -> rollersCount = RollersCount.Five;
|
||||
case 6 -> rollersCount = RollersCount.Six;
|
||||
default -> rollersCount = RollersCount.Four;
|
||||
}
|
||||
}
|
||||
public DrawningRollers(int count, Color colorRollers){
|
||||
setRollersCount(count);
|
||||
this.colorRollers = colorRollers;
|
||||
}
|
||||
|
||||
public void DrawRollers(Graphics2D g, float _startPosX, float _startPosY){
|
||||
Color penColor = Color.BLACK;
|
||||
Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers;
|
||||
|
||||
// Крупные катки - всегда
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
|
||||
|
||||
// Малые катки - всегда
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
|
||||
|
||||
// Средние катки - не всегда
|
||||
switch (rollersCount){
|
||||
case Six:
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
|
||||
case Five:
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
|
||||
}
|
||||
|
||||
// Центры крупных катков
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6);
|
||||
g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6);
|
||||
}
|
||||
}
|
125
src/DrawningSquaredRollers.java
Normal file
125
src/DrawningSquaredRollers.java
Normal file
@ -0,0 +1,125 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningSquaredRollers implements IDrawningRollers {
|
||||
private RollersCount rollersCount;
|
||||
private Color colorRollers;
|
||||
|
||||
public void setRollersCount(int count){
|
||||
switch (count) {
|
||||
case 4 -> rollersCount = RollersCount.Four;
|
||||
case 5 -> rollersCount = RollersCount.Five;
|
||||
case 6 -> rollersCount = RollersCount.Six;
|
||||
default -> rollersCount = RollersCount.Four;
|
||||
}
|
||||
}
|
||||
|
||||
public DrawningSquaredRollers(int count, Color colorRollers){
|
||||
setRollersCount(count);
|
||||
this.colorRollers = colorRollers;
|
||||
}
|
||||
|
||||
public void DrawRollers(Graphics2D g, float _startPosX, float _startPosY){
|
||||
Color penColor = Color.BLACK;
|
||||
Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers;
|
||||
|
||||
// Крупные катки - всегда
|
||||
// Узор для больших катков
|
||||
Polygon bigRomb = new Polygon(
|
||||
new int[]{(int)_startPosX + 5, (int)_startPosX + 16, (int)_startPosX + 27, (int)_startPosX + 16},
|
||||
new int[]{(int)_startPosY + 71, (int)_startPosY + 60, (int)_startPosY + 71, (int)_startPosY + 82},
|
||||
4
|
||||
);
|
||||
Polygon bigCube = new Polygon(
|
||||
new int[]{(int)_startPosX + 10, (int)_startPosX + 22, (int)_startPosX + 22, (int)_startPosX + 10},
|
||||
new int[]{(int)_startPosY + 65, (int)_startPosY + 65, (int)_startPosY + 77, (int)_startPosY + 77},
|
||||
4
|
||||
);
|
||||
// 1
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
|
||||
g.drawPolygon(bigRomb);
|
||||
g.drawPolygon(bigCube);
|
||||
// Сдвиг
|
||||
bigRomb.translate(78,0);
|
||||
bigCube.translate(78,0);
|
||||
// 2
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
|
||||
g.drawPolygon(bigRomb);
|
||||
g.drawPolygon(bigCube);
|
||||
|
||||
// Малые катки - всегда
|
||||
// Узор
|
||||
Polygon smallRomb = new Polygon(
|
||||
new int[]{(int)_startPosX + 43, (int)_startPosX + 46, (int)_startPosX + 49, (int)_startPosX + 46},
|
||||
new int[]{(int)_startPosY + 61, (int)_startPosY + 58, (int)_startPosY + 61, (int)_startPosY + 64},
|
||||
4
|
||||
);
|
||||
Polygon smallCube = new Polygon(
|
||||
new int[]{(int)_startPosX + 44, (int)_startPosX + 48, (int)_startPosX + 48, (int)_startPosX + 44},
|
||||
new int[]{(int)_startPosY + 59, (int)_startPosY + 63, (int)_startPosY + 63, (int)_startPosY + 59},
|
||||
4
|
||||
);
|
||||
// 1
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
|
||||
g.drawPolygon(smallRomb);
|
||||
g.drawPolygon(smallCube);
|
||||
// Сдвиг
|
||||
smallRomb.translate(18,0);
|
||||
smallCube.translate(18,0);
|
||||
// 2
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
|
||||
g.drawPolygon(smallRomb);
|
||||
g.drawPolygon(smallCube);
|
||||
|
||||
// Средние катки - не всегда
|
||||
// Узор
|
||||
Polygon middleRomb = new Polygon(
|
||||
new int[]{(int)_startPosX + 33, (int)_startPosX + 38, (int)_startPosX + 43, (int)_startPosX + 38},
|
||||
new int[]{(int)_startPosY + 78, (int)_startPosY + 73, (int)_startPosY + 78, (int)_startPosY + 83},
|
||||
4
|
||||
);
|
||||
Polygon middleCube = new Polygon(
|
||||
new int[]{(int)_startPosX + 35, (int)_startPosX + 41, (int)_startPosX + 41, (int)_startPosX + 35},
|
||||
new int[]{(int)_startPosY + 75, (int)_startPosY + 75, (int)_startPosY + 81, (int)_startPosY + 81},
|
||||
4
|
||||
);
|
||||
switch (rollersCount){
|
||||
case Six:
|
||||
// 1
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
|
||||
g.drawPolygon(middleRomb);
|
||||
g.drawPolygon(middleCube);
|
||||
|
||||
case Five:
|
||||
// Сдвиг
|
||||
middleRomb.translate(35,0);
|
||||
middleCube.translate(35,0);
|
||||
// 2
|
||||
g.setColor(mainColor);
|
||||
g.fillOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
|
||||
g.drawPolygon(middleRomb);
|
||||
g.drawPolygon(middleCube);
|
||||
}
|
||||
|
||||
// Центры крупных катков
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6);
|
||||
g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6);
|
||||
}
|
||||
}
|
205
src/DrawningTracktor.java
Normal file
205
src/DrawningTracktor.java
Normal file
@ -0,0 +1,205 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
public class DrawningTracktor {
|
||||
private EntityTracktor entityTracktor; // Класс-сущность
|
||||
public EntityTracktor getEntityTracktor(){
|
||||
return entityTracktor;
|
||||
}
|
||||
protected void setEntityTracktor(EntityTracktor entityTracktor){
|
||||
this.entityTracktor = entityTracktor;
|
||||
}
|
||||
public IMoveableObject GetMoveableObject(){
|
||||
return new DrawningObjectExcavator(this);
|
||||
}
|
||||
protected int _startPosX; // Левая координата отрисовки трактора
|
||||
protected int _startPosY; // Верхняя кооридната отрисовки трактора
|
||||
private Integer _pictureWidth; // Ширина окна отрисовки
|
||||
private Integer _pictureHeight; // Высота окна отрисовки
|
||||
protected int _tracktorWidth = 110; // Ширина отрисовки трактора
|
||||
protected int _tracktorHeight = 87; // Высота отрисовки трактора
|
||||
public int getPosX(){
|
||||
return _startPosX;
|
||||
}
|
||||
public int getPosY(){
|
||||
return _startPosY;
|
||||
}
|
||||
public int getWidth(){
|
||||
return _tracktorWidth;
|
||||
}
|
||||
public int getHeight(){
|
||||
return _tracktorHeight;
|
||||
}
|
||||
private IDrawningRollers drawningRollers;
|
||||
|
||||
// Инициализация свойств
|
||||
public DrawningTracktor(int speed, double weight, Color bodyColor,int width,int height, int countRollers)
|
||||
{
|
||||
if(height < _tracktorHeight || width < _tracktorWidth ){
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
entityTracktor = new EntityTracktor(speed, weight,bodyColor);
|
||||
Random random = new Random();
|
||||
drawningRollers = switch (random.nextInt(0, 3)) {
|
||||
case 0 -> new DrawningRollers(random.nextInt(0,3), bodyColor);
|
||||
case 1 -> new DrawningSquaredRollers(random.nextInt(0,3), bodyColor);
|
||||
case 2 -> new DrawningCrossRollers(random.nextInt(0,3), bodyColor);
|
||||
default -> new DrawningRollers(random.nextInt(0,3), bodyColor);
|
||||
};
|
||||
drawningRollers.setRollersCount((countRollers));
|
||||
|
||||
}
|
||||
protected DrawningTracktor(int speed, double weight, Color bodyColor, int width, int height, int tracktorWidth,int tracktorHeight, int countRollers){
|
||||
if(width < tracktorWidth || height < tracktorHeight)
|
||||
return;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_tracktorWidth = tracktorWidth;
|
||||
_pictureHeight = tracktorHeight;
|
||||
entityTracktor = new EntityTracktor(speed,weight,bodyColor);
|
||||
Random random = new Random();
|
||||
drawningRollers = switch (random.nextInt(0, 3)) {
|
||||
case 0 -> new DrawningRollers(random.nextInt(0,3), bodyColor);
|
||||
case 1 -> new DrawningSquaredRollers(random.nextInt(0,3), bodyColor);
|
||||
case 2 -> new DrawningCrossRollers(random.nextInt(0,3), bodyColor);
|
||||
default -> new DrawningRollers(random.nextInt(0,3), bodyColor);
|
||||
};
|
||||
drawningRollers.setRollersCount(countRollers);
|
||||
}
|
||||
public DrawningTracktor (EntityTracktor entityTracktor, int width, int height , int rollersCount, IDrawningRollers iDrawningRollers){
|
||||
if(height < _tracktorHeight || width < _tracktorWidth){
|
||||
return;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
this.entityTracktor = entityTracktor;
|
||||
Random random = new Random();
|
||||
drawningRollers = iDrawningRollers;
|
||||
drawningRollers.setRollersCount(rollersCount);
|
||||
}
|
||||
public boolean CanMove(Direction direction){
|
||||
if(entityTracktor == null)
|
||||
return false;
|
||||
return switch (direction){
|
||||
case Left -> _startPosY - entityTracktor.getStep() > 0;
|
||||
case Up -> _startPosY - entityTracktor.getStep() > 0;
|
||||
case Right -> _startPosX + _tracktorHeight + entityTracktor.getStep() < _pictureWidth;
|
||||
case Down -> _startPosX + _tracktorWidth + entityTracktor.getStep() < _pictureHeight;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
// Установка позиции Трактора
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x + _tracktorWidth > _pictureWidth){
|
||||
x = 0;
|
||||
}
|
||||
if( x < 0 || y + _tracktorHeight > _pictureHeight || y < 0)
|
||||
{
|
||||
y = 0;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
|
||||
// Изменение направления перемещения
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (!CanMove(direction) || entityTracktor == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
// вправо
|
||||
case Right:
|
||||
if (_startPosX + _tracktorWidth + entityTracktor.getStep() < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)entityTracktor.getStep();
|
||||
}
|
||||
break;
|
||||
//влево
|
||||
case Left:
|
||||
if (_startPosX - entityTracktor.getStep() > 0)
|
||||
{
|
||||
_startPosX -= (int)entityTracktor.getStep();
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case Up:
|
||||
if (_startPosY - entityTracktor.getStep() > 0)
|
||||
{
|
||||
_startPosY -= (int)entityTracktor.getStep();
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case Down:
|
||||
if (_startPosY + _tracktorHeight + entityTracktor.getStep() < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)entityTracktor.getStep();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Отрисовка Трактора
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Color penColor = Color.BLACK;
|
||||
|
||||
// корпус
|
||||
Color br = entityTracktor!=null ? entityTracktor.getBodyColor() : Color.GRAY ;
|
||||
g.setColor(br);
|
||||
g.fillRect((int)_startPosX + 10, (int)_startPosY + 30, 90, 25);
|
||||
g.setColor(penColor);
|
||||
g.drawRect((int)_startPosX + 10, (int)_startPosY + 30, 90, 25);
|
||||
|
||||
// окно
|
||||
g.setColor(Color.CYAN);
|
||||
g.fillRect((int)_startPosX + 65, (int)_startPosY + 1, 30, 29);
|
||||
g.setColor(penColor);
|
||||
g.drawRect((int)_startPosX + 65, (int)_startPosY + 1, 30, 29);
|
||||
|
||||
// труба
|
||||
g.setColor(Color.RED);
|
||||
g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 20);
|
||||
|
||||
// гусеница
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillOval((int)_startPosX + 1, (int)_startPosY + 57, 20, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 1, (int)_startPosY + 57, 20, 20);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillOval((int)_startPosX + 1, (int)_startPosY + 65, 20, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 1, (int)_startPosY + 65, 20, 20);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillOval((int)_startPosX + 90, (int)_startPosY + 57, 20, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 90, (int)_startPosY + 57, 20, 20);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillOval((int)_startPosX + 90, (int)_startPosY + 65, 20, 20);
|
||||
g.setColor(penColor);
|
||||
g.drawOval((int)_startPosX + 90, (int)_startPosY + 65, 20, 20);
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
g.fillRect((int)_startPosX + 10, (int)_startPosY + 57, 90, 30);
|
||||
g.fillRect((int)_startPosX + 1, (int)_startPosY + 65, 110, 10);
|
||||
g.setColor(penColor);
|
||||
g.drawLine((int)_startPosX + 10, (int)_startPosY + 57, (int)_startPosX + 100, (int)_startPosY + 57);
|
||||
g.drawLine((int)_startPosX + 10, (int)_startPosY + 86, (int)_startPosX + 100, (int)_startPosY + 86);
|
||||
g.drawLine((int)_startPosX + 1, (int)_startPosY + 65, (int)_startPosX + 1, (int)_startPosY + 75);
|
||||
g.drawLine((int)_startPosX + 110, (int)_startPosY + 65, (int)_startPosX + 110, (int)_startPosY + 75);
|
||||
|
||||
drawningRollers.DrawRollers(g,_startPosX, _startPosY);
|
||||
}
|
||||
}
|
33
src/EntityExcavator.java
Normal file
33
src/EntityExcavator.java
Normal file
@ -0,0 +1,33 @@
|
||||
import java.awt.*;
|
||||
public class EntityExcavator extends EntityTracktor{
|
||||
// Дополнительный цвет
|
||||
private Color BucketColor;
|
||||
private Color SupportsColor;
|
||||
// Признак наличия ковша
|
||||
private boolean bucket;
|
||||
// Признак наличия опор
|
||||
private boolean supports;
|
||||
|
||||
public EntityExcavator(int speed, double weight, Color bodyColor, Color bucketColor, Color supportsColor, boolean bucket, boolean supports){
|
||||
super(speed, weight, bodyColor);
|
||||
this.BucketColor = bucketColor;
|
||||
this.SupportsColor = supportsColor;
|
||||
this.bucket = bucket;
|
||||
this.supports = supports;
|
||||
}
|
||||
|
||||
public Color getBucketColor(){
|
||||
return BucketColor;
|
||||
}
|
||||
public Color getSupportsColor(){
|
||||
return SupportsColor;
|
||||
}
|
||||
|
||||
public boolean getBucket(){
|
||||
return bucket;
|
||||
}
|
||||
|
||||
public boolean getSupports(){
|
||||
return supports;
|
||||
}
|
||||
}
|
28
src/EntityTracktor.java
Normal file
28
src/EntityTracktor.java
Normal file
@ -0,0 +1,28 @@
|
||||
import java.awt.*;
|
||||
// Класс-сущность "Трактор"
|
||||
public class EntityTracktor {
|
||||
private int Speed;
|
||||
private double Weight;
|
||||
private Color BodyColor;
|
||||
|
||||
public int getSpeed() {
|
||||
return Speed;
|
||||
}
|
||||
public double getWeight(){
|
||||
return Weight;
|
||||
}
|
||||
public Color getBodyColor(){
|
||||
return BodyColor;
|
||||
}
|
||||
public double getStep(){
|
||||
return (double) Speed * 100 / Weight;
|
||||
}
|
||||
|
||||
// Инициализация полей объекта-класса Трактора
|
||||
public EntityTracktor(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
125
src/FormTracktor.form
Normal file
125
src/FormTracktor.form
Normal file
@ -0,0 +1,125 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormTracktor">
|
||||
<grid id="27dc6" binding="ContentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="8" 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="661" height="428"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="eb389" class="javax.swing.JButton" binding="buttonCreate">
|
||||
<constraints>
|
||||
<grid row="1" 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>
|
||||
<text value="Создать"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="7a016">
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<grid id="2fbf8" binding="pictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="8" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<hspacer id="dc061">
|
||||
<constraints>
|
||||
<grid row="0" column="0" 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="15753">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
</children>
|
||||
</grid>
|
||||
<component id="5a9f4" class="javax.swing.JButton" binding="buttonLeft">
|
||||
<constraints>
|
||||
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Resources/left.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="3c09f" class="javax.swing.JButton" binding="buttonDown">
|
||||
<constraints>
|
||||
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Resources/down'.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="6d42a" class="javax.swing.JButton" binding="buttonRight">
|
||||
<constraints>
|
||||
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Resources/right.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="82038" class="javax.swing.JButton" binding="buttonUp">
|
||||
<constraints>
|
||||
<grid row="1" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="30" height="30"/>
|
||||
<maximum-size width="30" height="30"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<icon value="Resources/up.png"/>
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="a11cc" class="javax.swing.JButton" binding="buttonCreateModif">
|
||||
<constraints>
|
||||
<grid row="1" 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>
|
||||
<text value="Модификация"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="f577" class="javax.swing.JComboBox" binding="comboBoxStrategy">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<model>
|
||||
<item value=""/>
|
||||
<item value="0"/>
|
||||
<item value="1"/>
|
||||
</model>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="a453" class="javax.swing.JButton" binding="buttonMakeStep">
|
||||
<constraints>
|
||||
<grid row="2" 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>
|
||||
<text value="Шаг"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
<inspectionSuppressions>
|
||||
<suppress inspection="NoLabelFor"/>
|
||||
</inspectionSuppressions>
|
||||
</form>
|
130
src/FormTracktor.java
Normal file
130
src/FormTracktor.java
Normal file
@ -0,0 +1,130 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormTracktor extends JFrame {
|
||||
private JPanel ContentPanel;
|
||||
private JButton buttonCreate;
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonUp;
|
||||
public JPanel pictureBox;
|
||||
private JButton buttonCreateModif;
|
||||
private JComboBox comboBoxStrategy;
|
||||
private JButton buttonMakeStep;
|
||||
private AbstractStrategy abstractStrategy;
|
||||
private DrawningTracktor _tracktor;
|
||||
private DrawningExcavator _excavator;
|
||||
|
||||
public FormTracktor(){
|
||||
setTitle("Трактор");
|
||||
setContentPane(ContentPanel);
|
||||
setSize(800, 500);
|
||||
|
||||
// Обработка нажатия кнопки "Создать"
|
||||
buttonCreate.addActionListener(e->{
|
||||
Random rnd = new Random();
|
||||
_tracktor = new DrawningTracktor(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
|
||||
pictureBox.getWidth(),
|
||||
pictureBox.getHeight(),
|
||||
rnd.nextInt(3,8)
|
||||
);
|
||||
setData();
|
||||
});
|
||||
|
||||
// Обработка нажатия кнопки "Модификация"
|
||||
buttonCreateModif.addActionListener(e->{
|
||||
Random rnd = new Random();
|
||||
_tracktor = new DrawningExcavator(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
|
||||
rnd.nextBoolean(),
|
||||
rnd.nextBoolean(),
|
||||
new Color(rnd.nextInt(0,256),rnd.nextInt(0,256),rnd.nextInt(0,256)),
|
||||
new Color(rnd.nextInt(0,256),rnd.nextInt(0,256),rnd.nextInt(0,256)),
|
||||
pictureBox.getWidth(),
|
||||
pictureBox.getHeight(),
|
||||
rnd.nextInt(3,8)
|
||||
);
|
||||
setData();
|
||||
});
|
||||
|
||||
buttonUp.addActionListener(e->{
|
||||
if (_tracktor != null){
|
||||
_tracktor.MoveTransport(Direction.Up);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
buttonLeft.addActionListener(e->{
|
||||
if (_tracktor != null){
|
||||
_tracktor.MoveTransport(Direction.Left);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
buttonDown.addActionListener(e->{
|
||||
if (_tracktor != null){
|
||||
_tracktor.MoveTransport(Direction.Down);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
buttonRight.addActionListener(e->{
|
||||
if (_tracktor != null){
|
||||
_tracktor.MoveTransport(Direction.Right);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
buttonMakeStep.addActionListener(e -> {
|
||||
if (_tracktor == null ) {
|
||||
return;
|
||||
}
|
||||
if(comboBoxStrategy.isEditable()) {
|
||||
abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()){
|
||||
case 0 -> new MoveToCenter();
|
||||
case 1 -> new MoveToBorder();
|
||||
default -> null;
|
||||
};
|
||||
if(abstractStrategy == null){
|
||||
return;
|
||||
}
|
||||
abstractStrategy.setData(new DrawningObjectExcavator(_excavator),this.getWidth(),this.getHeight());
|
||||
}
|
||||
if(abstractStrategy == null){
|
||||
return;
|
||||
}
|
||||
comboBoxStrategy.setEditable(false);
|
||||
abstractStrategy.makeStep();
|
||||
repaint();
|
||||
if(abstractStrategy.getStatus() == Status.Finish){
|
||||
comboBoxStrategy.setEditable(true);
|
||||
abstractStrategy = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setData() {
|
||||
Random rnd = new Random();
|
||||
_tracktor.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g){
|
||||
super.paint(g);
|
||||
Graphics2D g2d = (Graphics2D)pictureBox.getGraphics();
|
||||
if (_tracktor != null){
|
||||
_tracktor.DrawTransport(g2d);
|
||||
}
|
||||
}
|
||||
}
|
15
src/FrameDop.java
Normal file
15
src/FrameDop.java
Normal file
@ -0,0 +1,15 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class FrameDop extends JFrame {
|
||||
|
||||
private PictureBoxDop pictureBoxDop;
|
||||
|
||||
public FrameDop() {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
pictureBoxDop = new PictureBoxDop();
|
||||
add(pictureBoxDop);
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
15
src/FrameExcavator.java
Normal file
15
src/FrameExcavator.java
Normal file
@ -0,0 +1,15 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class FrameExcavator extends JFrame {
|
||||
|
||||
public PictureBoxExcavator pictureBoxExcavator;
|
||||
|
||||
public FrameExcavator() {
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
pictureBoxExcavator = new PictureBoxExcavator();
|
||||
add(pictureBoxExcavator);
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
12
src/FrameTruckCollection.java
Normal file
12
src/FrameTruckCollection.java
Normal file
@ -0,0 +1,12 @@
|
||||
import javax.swing.*;
|
||||
public class FrameTruckCollection extends JFrame {
|
||||
private PictureBoxCollection pictureBoxCollection;
|
||||
public FrameTruckCollection(){
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
pictureBoxCollection = new PictureBoxCollection();
|
||||
add(pictureBoxCollection);
|
||||
pack();
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
55
src/GenericDop.java
Normal file
55
src/GenericDop.java
Normal file
@ -0,0 +1,55 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class GenericDop<T extends EntityTracktor, U extends IDrawningRollers> {
|
||||
|
||||
private T[] Tracktors;
|
||||
private U[] Rollers;
|
||||
private int maxTracktorAmount;
|
||||
private int tracktorAmount;
|
||||
private int maxRollersAmount ;
|
||||
private int rollersAmount;
|
||||
private Random random;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
public GenericDop(int maxTracktorAmount, int maxRollersAmount, int pictureWidth, int pictureHeight){
|
||||
this.maxTracktorAmount = maxTracktorAmount;
|
||||
this.maxRollersAmount = maxRollersAmount;
|
||||
Tracktors = (T[]) new EntityTracktor[maxTracktorAmount];
|
||||
Rollers = (U[]) new IDrawningRollers[maxRollersAmount];
|
||||
tracktorAmount = 0;
|
||||
rollersAmount = 0;
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
public boolean add(T tracktor){
|
||||
if (tracktor == null || tracktorAmount > maxTracktorAmount)
|
||||
return false;
|
||||
Tracktors[tracktorAmount] = tracktor;
|
||||
tracktorAmount++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean add(U roller){
|
||||
if (roller == null || rollersAmount > maxRollersAmount)
|
||||
return false;
|
||||
Rollers[rollersAmount] = roller;
|
||||
rollersAmount++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public DrawningTracktor DrawingTracktorDop(){
|
||||
if (tracktorAmount == 0 || rollersAmount == 0)
|
||||
return null;
|
||||
T tracktor = Tracktors[random.nextInt(tracktorAmount)];
|
||||
DrawningTracktor drawingTracktor;
|
||||
if (tracktor instanceof EntityExcavator){
|
||||
drawingTracktor = new DrawningExcavator((EntityExcavator) tracktor, _pictureWidth, _pictureHeight, random.nextInt(2, 5), Rollers[random.nextInt(rollersAmount)]);
|
||||
}
|
||||
else{
|
||||
drawingTracktor = new DrawningTracktor(tracktor, _pictureWidth, _pictureHeight, random.nextInt(2, 5), Rollers[random.nextInt(rollersAmount)]);
|
||||
}
|
||||
return drawingTracktor;
|
||||
}
|
||||
}
|
6
src/IDrawningRollers.java
Normal file
6
src/IDrawningRollers.java
Normal file
@ -0,0 +1,6 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawningRollers {
|
||||
void setRollersCount(int count);
|
||||
void DrawRollers(Graphics2D g, float _startPosX, float _startPosY);
|
||||
}
|
6
src/IMoveableObject.java
Normal file
6
src/IMoveableObject.java
Normal file
@ -0,0 +1,6 @@
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters getObjectPosition();
|
||||
int getStep();
|
||||
boolean checkCanMove(Direction direction);
|
||||
void moveObject(Direction direction);
|
||||
}
|
27
src/MoveToBorder.java
Normal file
27
src/MoveToBorder.java
Normal file
@ -0,0 +1,27 @@
|
||||
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();
|
||||
}
|
||||
}
|
35
src/MoveToCenter.java
Normal file
35
src/MoveToCenter.java
Normal file
@ -0,0 +1,35 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
32
src/ObjectParameters.java
Normal file
32
src/ObjectParameters.java
Normal file
@ -0,0 +1,32 @@
|
||||
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){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
|
90
src/PictureBoxCollection.java
Normal file
90
src/PictureBoxCollection.java
Normal file
@ -0,0 +1,90 @@
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Random;
|
||||
|
||||
public class PictureBoxCollection extends JPanel {
|
||||
public TracktorsGenericCollection<DrawningTracktor,DrawningObjectExcavator> _traktors;
|
||||
private JLabel labelTools;
|
||||
private JButton buttonAddTracktor, buttonDeleteTracktor, buttonRefreshCollection, buttonShowDop;
|
||||
private JTextField textFieldNumber;
|
||||
public PictureBoxCollection() {
|
||||
setLayout(null);
|
||||
setBounds(0, 0, 800, 450);
|
||||
_traktors = new TracktorsGenericCollection<>(this.getWidth() - 200, this.getHeight());
|
||||
labelTools = new JLabel("Инструменты");
|
||||
labelTools.setBounds(660, 10, 150, 30);
|
||||
add(labelTools);
|
||||
buttonAddTracktor = new JButton("Добавить экскаватор");
|
||||
buttonAddTracktor.setFocusable(false);
|
||||
buttonAddTracktor.setBounds(620, 50, 150, 30);
|
||||
buttonAddTracktor.addActionListener(e -> {
|
||||
FrameExcavator frameExcavator = new FrameExcavator();
|
||||
frameExcavator.pictureBoxExcavator.buttonSelectTracktor.addActionListener(e1 -> {
|
||||
if (_traktors.Add(frameExcavator.pictureBoxExcavator.drawingTracktor) != -1) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
frameExcavator.dispose();
|
||||
repaint();
|
||||
}
|
||||
);
|
||||
});
|
||||
add(buttonAddTracktor);
|
||||
|
||||
textFieldNumber = new JTextField();
|
||||
textFieldNumber.setBounds(620, 100, 150, 30);
|
||||
add(textFieldNumber);
|
||||
|
||||
buttonDeleteTracktor = new JButton("Удалить");
|
||||
buttonDeleteTracktor.setFocusable(false);
|
||||
buttonDeleteTracktor.setBounds(620, 150, 150, 30);
|
||||
buttonDeleteTracktor.addActionListener(e -> {
|
||||
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
|
||||
return;
|
||||
}
|
||||
for (char it : textFieldNumber.getText().toCharArray())
|
||||
if (it < '0' || it > '9') {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
if (textFieldNumber.getText().length() == 0) {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
int pos = Integer.parseInt(textFieldNumber.getText());
|
||||
if (_traktors.remove(pos)) {
|
||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
repaint();
|
||||
});
|
||||
add(buttonDeleteTracktor);
|
||||
|
||||
buttonRefreshCollection = new JButton("Обновить коллекцию");
|
||||
buttonRefreshCollection.setFocusable(false);
|
||||
buttonRefreshCollection.setBounds(620, 200, 150, 30);
|
||||
buttonRefreshCollection.addActionListener(e -> repaint());
|
||||
add(buttonRefreshCollection);
|
||||
|
||||
buttonShowDop = new JButton("Показать доп");
|
||||
buttonShowDop.setFocusable(false);
|
||||
buttonShowDop.setBounds(620, 250, 150, 30);
|
||||
buttonShowDop.addActionListener(e -> new FrameDop());
|
||||
add(buttonShowDop);
|
||||
|
||||
setPreferredSize(new Dimension(800, 450));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.drawImage(_traktors.ShowTracktors(), 0, 0, null);
|
||||
}
|
||||
}
|
44
src/PictureBoxDop.java
Normal file
44
src/PictureBoxDop.java
Normal file
@ -0,0 +1,44 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class PictureBoxDop extends JPanel {
|
||||
GenericDop<EntityTracktor, IDrawningRollers> genericDop;
|
||||
private JButton buttonCreate;
|
||||
public DrawningTracktor drawingTracktor;
|
||||
|
||||
public PictureBoxDop(){
|
||||
setLayout(null);
|
||||
Random random = new Random();
|
||||
setBounds(0, 0, 800, 450);
|
||||
genericDop = new GenericDop<>(100, 100, this.getWidth(), this.getHeight());
|
||||
genericDop.add(new EntityTracktor(100, 100, Color.BLUE));
|
||||
genericDop.add(new EntityTracktor(100, 100, Color.RED));
|
||||
genericDop.add(new EntityTracktor(100, 100, Color.GREEN));
|
||||
genericDop.add(new EntityExcavator(100, 100, Color.BLUE, Color.BLACK, Color.RED,true, true));
|
||||
genericDop.add(new EntityExcavator(100, 100, Color.GREEN, Color.MAGENTA, Color.YELLOW,true, true));
|
||||
genericDop.add(new DrawningRollers(random.nextInt(4,6),Color.blue));
|
||||
genericDop.add(new DrawningCrossRollers(random.nextInt(4,6),Color.blue));
|
||||
genericDop.add(new DrawningSquaredRollers(random.nextInt(4,6),Color.blue));
|
||||
|
||||
buttonCreate = new JButton("Создать");
|
||||
buttonCreate.setFocusable(false);
|
||||
buttonCreate.setBounds(12, 415, 150, 30);
|
||||
buttonCreate.addActionListener(e -> {
|
||||
drawingTracktor = genericDop.DrawingTracktorDop();
|
||||
repaint();
|
||||
});
|
||||
add(buttonCreate);
|
||||
setPreferredSize(new Dimension(800, 450));
|
||||
}
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
if (drawingTracktor == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
drawingTracktor.SetPosition(100, 100);
|
||||
drawingTracktor.DrawTransport(g2d);
|
||||
}
|
||||
}
|
209
src/PictureBoxExcavator.java
Normal file
209
src/PictureBoxExcavator.java
Normal file
@ -0,0 +1,209 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class PictureBoxExcavator extends JPanel {
|
||||
public DrawningTracktor drawingTracktor;
|
||||
|
||||
private AbstractStrategy abstractStrategy;
|
||||
|
||||
private JButton buttonLeft;
|
||||
|
||||
private JButton buttonUp;
|
||||
|
||||
private JButton buttonRight;
|
||||
|
||||
private JButton buttonDown;
|
||||
|
||||
private JButton buttonCreateExcavator;
|
||||
private JButton buttonCreateTracktor;
|
||||
private JComboBox comboBoxStrategy;
|
||||
private JButton buttonStep;
|
||||
public JButton buttonSelectTracktor;
|
||||
|
||||
public PictureBoxExcavator() {
|
||||
setLayout(null);
|
||||
setBounds(0, 0, 800, 450);
|
||||
buttonCreateTracktor = new JButton("Создать Трактор");
|
||||
buttonCreateTracktor.setFocusable(false);
|
||||
buttonCreateTracktor.setBounds(12, 415, 150, 30);
|
||||
add(buttonCreateTracktor);
|
||||
|
||||
buttonCreateTracktor.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
|
||||
if (selectedColor != null)
|
||||
{
|
||||
bodyColor = selectedColor;
|
||||
}
|
||||
drawingTracktor = new DrawningTracktor((random.nextInt(200, 300)),
|
||||
random.nextInt(1000, 3000),
|
||||
bodyColor,
|
||||
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||||
drawingTracktor.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonCreateExcavator = new JButton("Создать экскаватор");
|
||||
buttonCreateExcavator.setFocusable(false);
|
||||
buttonCreateExcavator.setBounds(180, 415, 150, 30);
|
||||
add(buttonCreateExcavator);
|
||||
|
||||
buttonCreateExcavator.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
|
||||
if (selectedColor != null)
|
||||
{
|
||||
bodyColor = selectedColor;
|
||||
}
|
||||
Color bucketColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
|
||||
if (selectedColor != null)
|
||||
{
|
||||
bucketColor = selectedColor;
|
||||
}
|
||||
Color suppotsColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
|
||||
if (selectedColor != null)
|
||||
{
|
||||
suppotsColor = selectedColor;
|
||||
}
|
||||
|
||||
drawingTracktor = new DrawningExcavator((random.nextInt(200, 300)),
|
||||
random.nextInt(1000, 3000),
|
||||
bodyColor,
|
||||
random.nextBoolean(), true,
|
||||
bucketColor,suppotsColor,
|
||||
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||||
drawingTracktor.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonSelectTracktor = new JButton("Выбрать");
|
||||
buttonSelectTracktor.setFocusable(false);
|
||||
buttonSelectTracktor.setBounds(350, 415, 150, 30);
|
||||
add(buttonSelectTracktor);
|
||||
|
||||
ActionListener buttonMoveListener = e -> {
|
||||
if (drawingTracktor == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
String buttonName = ((JButton) e.getSource()).getName();
|
||||
|
||||
switch (buttonName) {
|
||||
case ("buttonUp"):
|
||||
drawingTracktor.MoveTransport(Direction.Up);
|
||||
break;
|
||||
case ("buttonDown"):
|
||||
drawingTracktor.MoveTransport(Direction.Down);
|
||||
break;
|
||||
case ("buttonLeft"):
|
||||
drawingTracktor.MoveTransport(Direction.Left);
|
||||
break;
|
||||
case ("buttonRight"):
|
||||
drawingTracktor.MoveTransport(Direction.Right);
|
||||
break;
|
||||
}
|
||||
repaint();
|
||||
};
|
||||
buttonLeft = new JButton();
|
||||
buttonLeft.setName("buttonLeft");
|
||||
buttonLeft.setFocusable(false);
|
||||
buttonLeft.setPreferredSize(new Dimension(30, 30));
|
||||
buttonLeft.setIcon(new ImageIcon("Resources/left.png"));
|
||||
buttonLeft.addActionListener(buttonMoveListener);
|
||||
buttonLeft.setBounds(686, 408, 30, 30);
|
||||
|
||||
add(buttonLeft);
|
||||
|
||||
buttonRight = new JButton();
|
||||
buttonRight.setName("buttonRight");
|
||||
buttonRight.setFocusable(false);
|
||||
buttonRight.setPreferredSize(new Dimension(30, 30));
|
||||
buttonRight.setIcon(new ImageIcon("Resources/right.png"));
|
||||
buttonRight.addActionListener(buttonMoveListener);
|
||||
buttonRight.setBounds(758, 408, 30, 30);
|
||||
|
||||
add(buttonRight);
|
||||
|
||||
buttonDown = new JButton();
|
||||
buttonDown.setName("buttonDown");
|
||||
buttonDown.setFocusable(false);
|
||||
buttonDown.setPreferredSize(new Dimension(30, 30));
|
||||
buttonDown.setIcon(new ImageIcon("Resources/down'.png"));
|
||||
buttonDown.addActionListener(buttonMoveListener);
|
||||
buttonDown.setBounds(722, 408, 30, 30);
|
||||
|
||||
add(buttonDown);
|
||||
|
||||
buttonUp = new JButton();
|
||||
buttonUp.setName("buttonUp");
|
||||
buttonUp.setFocusable(false);
|
||||
buttonUp.setPreferredSize(new Dimension(30, 30));
|
||||
buttonUp.setIcon(new ImageIcon("Resources/up.png"));
|
||||
buttonUp.addActionListener(buttonMoveListener);
|
||||
buttonUp.setBounds(722, 372, 30, 30);
|
||||
|
||||
add(buttonUp);
|
||||
|
||||
String[] items = {
|
||||
"0",
|
||||
"1"
|
||||
};
|
||||
comboBoxStrategy = new JComboBox(items);
|
||||
comboBoxStrategy.setBounds(667, 10, 120, 25);
|
||||
|
||||
buttonStep = new JButton("Шаг");
|
||||
buttonStep.setFocusable(false);
|
||||
buttonStep.setBounds(710, 40, 75, 30);
|
||||
|
||||
buttonStep.addActionListener(e -> {
|
||||
if (drawingTracktor == 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 DrawningObjectExcavator(drawingTracktor), this.getWidth(), this.getHeight());
|
||||
}
|
||||
if (abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
abstractStrategy.makeStep();
|
||||
repaint();
|
||||
if (abstractStrategy.getStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
abstractStrategy = null;
|
||||
}
|
||||
});
|
||||
|
||||
add(comboBoxStrategy);
|
||||
add(buttonStep);
|
||||
setPreferredSize(new Dimension(800, 450));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
if (drawingTracktor == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
drawingTracktor.DrawTransport(g2d);
|
||||
}
|
||||
}
|
6
src/Program.java
Normal file
6
src/Program.java
Normal file
@ -0,0 +1,6 @@
|
||||
import javax.swing.*;
|
||||
public class Program {
|
||||
public static void main(String[] args){
|
||||
new FrameTruckCollection();
|
||||
}
|
||||
}
|
BIN
src/Resources/down'.png
Normal file
BIN
src/Resources/down'.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
BIN
src/Resources/left.png
Normal file
BIN
src/Resources/left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.0 KiB |
BIN
src/Resources/right.png
Normal file
BIN
src/Resources/right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
BIN
src/Resources/up.png
Normal file
BIN
src/Resources/up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
5
src/RollersCount.java
Normal file
5
src/RollersCount.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum RollersCount {
|
||||
Four,
|
||||
Five,
|
||||
Six
|
||||
}
|
17
src/RollersType.java
Normal file
17
src/RollersType.java
Normal file
@ -0,0 +1,17 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public enum RollersType {
|
||||
Standard,
|
||||
Squared,
|
||||
Cross;
|
||||
|
||||
public static IDrawningRollers random(int rollersCount, Color bodyColor) {
|
||||
return switch (new Random().nextInt(RollersType.values().length)) {
|
||||
case 0 -> new DrawningRollers(rollersCount, bodyColor);
|
||||
case 1 -> new DrawningSquaredRollers(rollersCount, bodyColor);
|
||||
case 2 -> new DrawningCrossRollers(rollersCount, bodyColor);
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
}
|
45
src/SetGeneric.java
Normal file
45
src/SetGeneric.java
Normal file
@ -0,0 +1,45 @@
|
||||
public class SetGeneric<T extends Object> {
|
||||
private Object[] _places;
|
||||
public int Count(){return _places.length;}
|
||||
public SetGeneric(int count){
|
||||
_places = new Object[count];
|
||||
}
|
||||
public int Insert(T tracktor){
|
||||
return Insert(tracktor,0);
|
||||
}
|
||||
public int Insert(T tracktor, int position){
|
||||
if(position < 0 || position >= Count())
|
||||
return -1;
|
||||
if (_places[position] == null){
|
||||
_places[position] = tracktor;
|
||||
return position;
|
||||
}
|
||||
int index = -1;
|
||||
for(int i = position; i < Count();i++){
|
||||
if(_places[i] == null){
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(index < 0)
|
||||
return -1;
|
||||
for(int i = index; i > position; i--){
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[position] = tracktor;
|
||||
return position;
|
||||
}
|
||||
public boolean Remove(int position){
|
||||
if(position < 0 || position >= Count()){
|
||||
return false;
|
||||
}
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
public T Get(int position){
|
||||
if(position < 0 || position >= Count()){
|
||||
return null;
|
||||
}
|
||||
return (T) _places[position];
|
||||
}
|
||||
}
|
5
src/Status.java
Normal file
5
src/Status.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
56
src/TracktorsGenericCollection.java
Normal file
56
src/TracktorsGenericCollection.java
Normal file
@ -0,0 +1,56 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
public class TracktorsGenericCollection<T extends DrawningTracktor, U extends IMoveableObject>{
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
private final int _placeSizeWidth = 160;
|
||||
private final int _placeSizeHeight = 140;
|
||||
private SetGeneric<T> _collection;
|
||||
public TracktorsGenericCollection(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 boolean remove(int pos){
|
||||
T obj = _collection.Get(pos);
|
||||
if(obj != null)
|
||||
_collection.Remove(pos);
|
||||
return false;
|
||||
}
|
||||
public U GetU(int pos){
|
||||
return (U) _collection.Get(pos).GetMoveableObject();
|
||||
}
|
||||
public BufferedImage ShowTracktors(){
|
||||
BufferedImage bitmap = new BufferedImage(_pictureWidth,_pictureHeight,BufferedImage.TYPE_4BYTE_ABGR);
|
||||
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 obj = _collection.Get(i);
|
||||
if(obj != null){
|
||||
obj.SetPosition(i %(_pictureWidth/ _placeSizeWidth)*_placeSizeWidth,i/(_pictureWidth/_placeSizeWidth) * _placeSizeHeight);
|
||||
obj.DrawTransport((Graphics2D) g);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user