Готовая 2 усложненная лабораторная

This commit is contained in:
Даниил Путинцев 2023-11-12 00:17:43 +04:00
parent 3901e3e00e
commit b6e9f62b9c
25 changed files with 806 additions and 233 deletions

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
DrawingRoadTrain.java

View File

@ -3,6 +3,7 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<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" />
</component>
</project>

View File

@ -1,83 +0,0 @@
import java.awt.*;
public class DrawingRoadTrain {
private EntityRoadTrain entityRoadTrain;
private DrawingWheels drawingWheels;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int _trainWidth = 90;
private final int _trainHeight = 70;
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean waterContainer, boolean sweepingBrush, int width, int height, int wheelnumber){
if (width < _trainWidth || height <_trainHeight){
return false;
}
_pictureWidth = width;
_pictureHeight = height;
entityRoadTrain = new EntityRoadTrain();
entityRoadTrain.Init(speed, weight, bodyColor, additionalColor, waterContainer, sweepingBrush);
drawingWheels = new DrawingWheels();
drawingWheels.SetWheelNumber(wheelnumber);
return true;
}
public void SetPosition(int x, int y){
if (x < 0 || x + _trainWidth > _pictureWidth){
x = 200;
}
if (y < 0 || y + _trainHeight > _pictureHeight){
y = 200;
}
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionType direction){
if (entityRoadTrain == null){
return;
}
switch (direction){
case Left:
if (_startPosX - entityRoadTrain.GetStep() > 0){
_startPosX -= (int)entityRoadTrain.GetStep();
}
break;
case Up:
if (_startPosY - entityRoadTrain.GetStep() > 0){
_startPosY -= (int)entityRoadTrain.GetStep();
}
break;
case Right:
if (_startPosX + entityRoadTrain.GetStep() + _trainWidth < _pictureWidth){
_startPosX += (int)entityRoadTrain.GetStep();
}
break;
case Down:
if (_startPosY + entityRoadTrain.GetStep() + _trainHeight < _pictureHeight){
_startPosY += (int)entityRoadTrain.GetStep();
}
break;
}
}
public void DrawTransport(Graphics g){
if (entityRoadTrain == null){
return;
}
if (entityRoadTrain.GetWaterContainer()){
g.setColor(entityRoadTrain.GetAdditionalColor());
g.drawOval(_startPosX + 30, _startPosY, 10, 20);
g.fillOval(_startPosX + 30, _startPosY, 10, 20);
}
if (entityRoadTrain.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);
}
drawingWheels.drawWheels(g,entityRoadTrain.GetBodyColor(), _startPosX, _startPosY);
g.setColor(entityRoadTrain.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);
}
}

View File

@ -1,34 +0,0 @@
import java.awt.*;
public class EntityRoadTrain {
private int Speed;
private double Weight;
private Color BodyColor;
public Color GetBodyColor(){
return BodyColor;
}
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 double Step;
public double GetStep() {
return (double)Speed * 100 / Weight;
}
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean waterContainer, boolean sweepingBrush)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
WaterContainer = waterContainer;
SweepingBrush = sweepingBrush;
}
}

View File

@ -1,3 +1,4 @@
package RoadTrain;
public enum DirectionType {
Up,
Down,

View File

@ -0,0 +1,36 @@
package RoadTrain.DrawingObjects;
import java.awt.*;
import RoadTrain.Entities.*;
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,
additionalColor, waterContainer, sweepingBrush);
}
}
@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);
}
}
}

View File

@ -0,0 +1,141 @@
package RoadTrain.DrawingObjects;
import RoadTrain.DirectionType;
import RoadTrain.Entities.*;
import RoadTrain.Extra.*;
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 int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _truckWidth;
}
public int GetHeight() {
return _truckHeight;
}
private int _truckWidth = 80;
private int _truckHeight = 70;
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);
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);
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);
}
}

View 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, Color
additionalColor, boolean waterContainer, boolean sweepingBrush) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
WaterContainer = waterContainer;
SweepingBrush = sweepingBrush;
}
}

View File

@ -0,0 +1,31 @@
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 double GetStep() {
return (double) Speed * 100 / Weight;
}
public EntityTruck(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View 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);
}
}

View 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);
}
}

View File

@ -1,7 +1,8 @@
package RoadTrain.Extra;
import java.awt.*;
public class DrawingWheels {
public class DrawingWheels implements IDrawingWheels{
private WheelNumber wheelNumber;
@Override
public void SetWheelNumber(int num){
switch (num){
case 1:
@ -14,6 +15,11 @@ public class DrawingWheels {
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:
@ -39,6 +45,7 @@ public class DrawingWheels {
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);
@ -49,6 +56,7 @@ public class DrawingWheels {
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);

View 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);
}

View File

@ -1,3 +1,4 @@
package RoadTrain.Extra;
public enum WheelNumber {
One,
Two,

View File

@ -1,3 +1,4 @@
package RoadTrain;
public class Main {
public static void main(String[] args){
RoadTrainForm RoadTrainForm = new RoadTrainForm();

View 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;
}
}

View 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);
}
}

View 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);
}

View 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();
}
}
}
}

View 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();
}
}
}
}

View 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;
}
}

View File

@ -0,0 +1,6 @@
package RoadTrain.MovementStraregy;
public enum Status {
NotInit,
InProgress,
Finish
}

View File

@ -0,0 +1,169 @@
package RoadTrain;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import RoadTrain.DrawingObjects.*;
import RoadTrain.MovementStraregy.*;
public class RoadTrainForm{
private DrawingTruck _drawingTruck;
private AbstractStrategy _abstractStrategy;
Canvas canv;
public void Draw(){
canv.repaint();
}
public RoadTrainForm(){
JFrame frame = new JFrame("RoadTrain");
JButton buttonCreateTruck = new JButton("Создать грузовик");
buttonCreateTruck.setFocusPainted(false);
buttonCreateTruck.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();
_drawingTruck = new DrawingTruck(random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
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();
_drawingTruck = new DrawingRoadTrain(random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256)), new Color(random.nextInt(256)), 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);
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.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();
}
}
}

View File

@ -1,112 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class RoadTrainForm{
private DrawingRoadTrain _drawingRoadTrain;
Canvas canv;
public void Draw(){
canv.repaint();
}
public RoadTrainForm(){
JFrame frame = new JFrame("RoadTrain");
JButton buttonCreate = new JButton("Создать");
buttonCreate.setFocusPainted(false);
buttonCreate.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)));
buttonCreate.addActionListener(
e -> {
System.out.println(e.getActionCommand());
Random random = new Random();
_drawingRoadTrain = new DrawingRoadTrain();
_drawingRoadTrain.Init(
random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(), this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3));
_drawingRoadTrain.SetPosition(random.nextInt(100), random.nextInt(90));
canv._drawingRoadTrain = _drawingRoadTrain;
Draw();
}
);
ActionListener actionListener = e -> {
System.out.println(((JButton)(e.getSource())).getName());
if (_drawingRoadTrain == null){
return;
}
switch ((((JButton)(e.getSource())).getName())){
case "up":
_drawingRoadTrain.MoveTransport(DirectionType.Up);
break;
case "down":
_drawingRoadTrain.MoveTransport(DirectionType.Down);
break;
case "left":
_drawingRoadTrain.MoveTransport(DirectionType.Left);
break;
case "right":
_drawingRoadTrain.MoveTransport(DirectionType.Right);
break;
}
Draw();
};
buttonUp.addActionListener(actionListener);
buttonDown.addActionListener(actionListener);
buttonLeft.addActionListener(actionListener);
buttonRight.addActionListener(actionListener);
frame.setSize(900, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
canv = new Canvas();
canv.setBounds(0, 0, 900, 500);
buttonCreate.setBounds(20, 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);
frame.add(canv);
frame.add(buttonCreate);
frame.add(buttonUp);
frame.add(buttonDown);
frame.add(buttonLeft);
frame.add(buttonRight);
frame.setVisible(true);
}
class Canvas extends JComponent{
public DrawingRoadTrain _drawingRoadTrain;
public Canvas(){}
public void paintComponent(Graphics g){
if (_drawingRoadTrain == null){
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
_drawingRoadTrain.DrawTransport(g2d);
super.repaint();
}
}
}