Лабораторная работа №2 (1 часть)

This commit is contained in:
DjonniStorm 2024-04-16 01:53:38 +04:00
parent 27a627d610
commit 5a10560b04
19 changed files with 620 additions and 222 deletions

View File

@ -1,140 +0,0 @@
import java.awt.*;
import java.util.Random;
public class DrawningCleaningCar {
private EntityCleaningCar EntityCleaningCar;
public EntityCleaningCar getEntityCleaningCar() {
return EntityCleaningCar;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
private Integer _startPosX;
private Integer _startPosY;
private final Integer _drawningCarWidth = 138;
private final Integer _drawningCarHeight = 68;
public DrawningWheels drawningWheels;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashlight) {
EntityCleaningCar = new EntityCleaningCar();
EntityCleaningCar.Init(speed, weight, bodyColor, additionalColor, tank, sweepingBrush, flashlight);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
drawningWheels = new DrawningWheels();
Random random = new Random();
drawningWheels.setCountWheels(random.nextInt(1, 4));
}
public boolean SetPictureSize(int width, int height) {
if (width < _drawningCarWidth || height < _drawningCarHeight) {return false;}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX + _drawningCarWidth > _pictureWidth)
{
_startPosX = -_drawningCarWidth + _pictureWidth;
}
else if (_startPosX < 0)
{
_startPosX = 0;
}
if (_startPosY + _drawningCarHeight > _pictureHeight)
{
_startPosY = -_drawningCarHeight + _pictureHeight;
}
else if (_startPosY < 0)
{
_startPosY = 0;
}
}
return true;
}
public void SetPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null) return;
if (x + _drawningCarWidth > _pictureWidth) {
_startPosX = _pictureWidth - _drawningCarWidth;
} else if (x < 0) {
_startPosX = 0;
}
else {
_startPosX = x;
}
if (y + _drawningCarHeight > _pictureHeight) {
_startPosY = _pictureHeight - _drawningCarHeight;
} else if (y < 0) {
_startPosY = 0;
} else {
_startPosY = y;
}
}
public boolean MoveTransport(DirectionType directionType) {
if (EntityCleaningCar == null || _startPosX == null || _startPosY == null) {
return false;
}
switch (directionType) {
case Left:
if (_startPosX - EntityCleaningCar.Step() > 0) {
_startPosX -= (int) EntityCleaningCar.Step();
}
return true;
case Right:
if (_startPosX + _drawningCarWidth + EntityCleaningCar.Step() < _pictureWidth) {
_startPosX += (int) EntityCleaningCar.Step();
}
return true;
case Up:
if (_startPosY - EntityCleaningCar.Step() > 0) {
_startPosY -= (int) EntityCleaningCar.Step();
}
return true;
case Down:
if (_startPosY + _drawningCarHeight + EntityCleaningCar.Step() < _pictureHeight) {
_startPosY += (int) EntityCleaningCar.Step();
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics g) {
if (EntityCleaningCar == null || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
if (EntityCleaningCar.getTank()) {
g2d.setColor(EntityCleaningCar.getAdditionalColor());
g2d.fillRect(_startPosX, _startPosY, 100, 38);
g2d.setColor(Color.black);
g2d.drawRect(_startPosX, _startPosY, 100, 38);
g2d.setColor(EntityCleaningCar.getBodyColor());
g2d.fillRect(_startPosX, _startPosY + 20, 100, 10);
}
if (EntityCleaningCar.getSweepingBrush()) {
g2d.setColor(EntityCleaningCar.getAdditionalColor());
g2d.fillOval(_startPosX + 120, _startPosY + 50, 20, 20);
g2d.setColor(EntityCleaningCar.getBodyColor());
g2d.fillOval(_startPosX + 125, _startPosY + 55, 10, 10);
g2d.setColor(Color.black);
g2d.drawOval(_startPosX + 120, _startPosY + 50, 19, 19);
g2d.drawOval(_startPosX + 125, _startPosY + 55, 9, 9);
g2d.drawLine(_startPosX + 122, _startPosY + 48, _startPosX + 130, _startPosY + 60);
}
if (EntityCleaningCar.getFlashLight()) {
g2d.setColor(EntityCleaningCar.getAdditionalColor());
g2d.fillRect(_startPosX + 108, _startPosY + 3, 5, 8);
}
g2d.setColor(EntityCleaningCar.getBodyColor());
g2d.fillRect(_startPosX, _startPosY + 40, 122, 10);
g2d.fillRect(_startPosX + 102, _startPosY + 10, 20, 30);
drawningWheels.DrawCleaningCarWheels(g, EntityCleaningCar.getBodyColor(), EntityCleaningCar.getAdditionalColor(), _startPosX, _startPosY);
g2d.setColor(Color.black);
g2d.drawRect(_startPosX, _startPosY + 40, 122, 10);
g2d.drawRect(_startPosX + 102, _startPosY + 10, 20, 30);
g2d.fillRect(_startPosX + 112, _startPosY + 15, 10, 12);
}
}

View File

@ -1,3 +1,5 @@
package Drawnings;
public enum CountWheels {
One(1),
Two(2),

View File

@ -1,6 +1,9 @@
package Drawnings;
public enum DirectionType {
Up,
Down,
Left,
Right
Right,
Unknown
}

View File

@ -0,0 +1,62 @@
package Drawnings;
import java.awt.*;
import java.util.Random;
import Entities.*;
public class DrawningCleaningCar extends DrawningTruck {
public DrawningWheels drawningWheels;
public DrawningCleaningCar(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashlight) {
super(138,68);
EntityTruck = new EntityCleaningCar(speed, weight, bodyColor, additionalColor, tank, sweepingBrush, flashlight);
drawningWheels = new DrawningWheels();
Random random = new Random();
drawningWheels.setCountWheels(random.nextInt(1, 4));
}
@Override
public void DrawTransport(Graphics g) {
super.DrawTransport(g);
if (EntityTruck == null || !(EntityTruck instanceof EntityCleaningCar cleaningCar) || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
if (cleaningCar.getTank()) {
g2d.setColor(cleaningCar.getAdditionalColor());
g2d.fillRect(_startPosX, _startPosY, 100, 38);
g2d.setColor(Color.black);
g2d.drawRect(_startPosX, _startPosY, 100, 38);
g2d.setColor(cleaningCar.getBodyColor());
g2d.fillRect(_startPosX, _startPosY + 20, 100, 10);
}
if (cleaningCar.getSweepingBrush()) {
g2d.setColor(cleaningCar.getAdditionalColor());
g2d.fillOval(_startPosX + 120, _startPosY + 50, 20, 20);
g2d.setColor(cleaningCar.getBodyColor());
g2d.fillOval(_startPosX + 125, _startPosY + 55, 10, 10);
g2d.setColor(Color.black);
g2d.drawOval(_startPosX + 120, _startPosY + 50, 19, 19);
g2d.drawOval(_startPosX + 125, _startPosY + 55, 9, 9);
g2d.drawLine(_startPosX + 122, _startPosY + 48, _startPosX + 130, _startPosY + 60);
}
if (cleaningCar.getFlashLight()) {
g2d.setColor(cleaningCar.getAdditionalColor());
g2d.fillRect(_startPosX + 108, _startPosY + 3, 5, 8);
}
// g2d.setColor(EntityCleaningCar.getBodyColor());
// g2d.fillRect(_startPosX, _startPosY + 40, 122, 10);
// g2d.fillRect(_startPosX + 102, _startPosY + 10, 20, 30);
drawningWheels.DrawCleaningCarWheels(g, cleaningCar.getBodyColor(), cleaningCar.getAdditionalColor(), _startPosX, _startPosY);
g2d.setColor(Color.black);
//
// g2d.drawRect(_startPosX, _startPosY + 40, 122, 10);
// g2d.drawRect(_startPosX + 102, _startPosY + 10, 20, 30);
//
// g2d.fillRect(_startPosX + 112, _startPosY + 15, 10, 12);
}
}

View File

@ -0,0 +1,135 @@
package Drawnings;
import Entities.*;
import java.awt.*;
import java.util.Random;
public class DrawningTruck {
protected EntityTruck EntityTruck;
public EntityTruck getEntityTruck() {
return EntityTruck;
}
private Integer _pictureWidth;
private Integer _pictureHeight;
protected Integer _startPosX;
protected Integer _startPosY;
private Integer _drawningCarWidth = 138;
private Integer _drawningCarHeight = 68;
public DrawningWheels drawningWheels;
public int GetPosX(){return _startPosX;}
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _drawningCarWidth;}
public int GetHeight(){return _drawningCarHeight;}
private DrawningTruck() {
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public DrawningTruck(int speed, double weight, Color bodyColor) {
this();
EntityTruck = new EntityTruck(speed, weight, bodyColor);
drawningWheels = new DrawningWheels();
Random random = new Random();
drawningWheels.setCountWheels(random.nextInt(1, 4));
}
protected DrawningTruck(Integer drawningCarWidth, Integer drawningCarHeight) {
this();
_drawningCarWidth = drawningCarWidth;
_drawningCarHeight = drawningCarHeight;
}
public boolean SetPictureSize(int width, int height) {
if (width < _drawningCarWidth || height < _drawningCarHeight) {return false;}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX + _drawningCarWidth > _pictureWidth)
{
_startPosX = -_drawningCarWidth + _pictureWidth;
}
else if (_startPosX < 0)
{
_startPosX = 0;
}
if (_startPosY + _drawningCarHeight > _pictureHeight)
{
_startPosY = -_drawningCarHeight + _pictureHeight;
}
else if (_startPosY < 0)
{
_startPosY = 0;
}
}
return true;
}
public void SetPosition(int x, int y) {
if (_pictureHeight == null || _pictureWidth == null) return;
if (x + _drawningCarWidth > _pictureWidth) {
_startPosX = _pictureWidth - _drawningCarWidth;
} else if (x < 0) {
_startPosX = 0;
}
else {
_startPosX = x;
}
if (y + _drawningCarHeight > _pictureHeight) {
_startPosY = _pictureHeight - _drawningCarHeight;
} else if (y < 0) {
_startPosY = 0;
} else {
_startPosY = y;
}
}
public boolean MoveTransport(DirectionType directionType) {
if (EntityTruck == null || _startPosX == null || _startPosY == null) {
return false;
}
switch (directionType) {
case Left:
if (_startPosX - EntityTruck.Step() > 0) {
_startPosX -= (int) EntityTruck.Step();
}
return true;
case Right:
if (_startPosX + _drawningCarWidth + EntityTruck.Step() < _pictureWidth) {
_startPosX += (int) EntityTruck.Step();
}
return true;
case Up:
if (_startPosY - EntityTruck.Step() > 0) {
_startPosY -= (int) EntityTruck.Step();
}
return true;
case Down:
if (_startPosY + _drawningCarHeight + EntityTruck.Step() < _pictureHeight) {
_startPosY += (int) EntityTruck.Step();
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics g) {
if (EntityTruck == null || _startPosX == null || _startPosY == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(EntityTruck.getBodyColor());
g2d.fillRect(_startPosX, _startPosY + 40, 122, 10);
g2d.fillRect(_startPosX + 102, _startPosY + 10, 20, 30);
// drawningWheels.DrawCleaningCarWheels(g, EntityTruck.getBodyColor(), EntityCleaningCar.getAdditionalColor(), _startPosX, _startPosY);
g2d.setColor(Color.black);
g2d.drawRect(_startPosX, _startPosY + 40, 122, 10);
g2d.drawRect(_startPosX + 102, _startPosY + 10, 20, 30);
g2d.fillRect(_startPosX + 112, _startPosY + 15, 10, 12);
}
}

View File

@ -1,3 +1,7 @@
package Drawnings;
import Drawnings.CountWheels;
import java.awt.*;
public class DrawningWheels {

View File

@ -0,0 +1,23 @@
package Entities;
import java.awt.*;
public class EntityCleaningCar extends EntityTruck {
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
private boolean Tank;
public boolean getTank() {return Tank;}
private boolean SweepingBrush;
public boolean getSweepingBrush() {return SweepingBrush;}
private boolean FlashLight;
public boolean getFlashLight() {return FlashLight;}
public EntityCleaningCar(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashLight) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Tank = tank;
SweepingBrush = sweepingBrush;
FlashLight = flashLight;
}
}

View File

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

View File

@ -1,45 +0,0 @@
import java.awt.*;
public class EntityCleaningCar {
private int Speed;
public int getSpeed() {
return Speed;
}
private double Weight;
public double getWeight() {
return Weight;
}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
private boolean Tank;
public boolean getTank() {
return Tank;
}
private boolean SweepingBrush;
public boolean getSweepingBrush() {
return SweepingBrush;
}
private boolean FlashLight;
public boolean getFlashLight() {
return FlashLight;
}
public double Step() {
return Speed * 200 / Weight;
}
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashLight) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Tank = tank;
SweepingBrush = sweepingBrush;
FlashLight = flashLight;
}
}

View File

@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormCleaningCar">
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="4" 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="601" height="395"/>
<xy x="20" y="20" width="714" height="441"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="fb60" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="6" fill="0" indent="0" use-parent-layout="false">
<grid row="3" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="6" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="62" height="62"/>
<preferred-size width="62" height="62"/>
<maximum-size width="62" height="62"/>
@ -26,36 +26,60 @@
</component>
<vspacer id="cd599">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="3" col-span="5" vsize-policy="7" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="3587c" class="javax.swing.JButton" binding="buttonCreate">
<component id="3587c" class="javax.swing.JButton" binding="buttonCreateCleaningCar">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="100" height="60"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<text value="Создать"/>
<text value="Создать уборочную машину"/>
</properties>
</component>
<component id="2822e" class="javax.swing.JButton" binding="buttonUp">
<component id="1fedd" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="6" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="62" height="62"/>
<preferred-size width="62" height="62"/>
<maximum-size width="62" height="62"/>
<grid row="0" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="60677" class="javax.swing.JButton" binding="buttonStrategyStep">
<constraints>
<grid row="1" column="7" 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>
<vspacer id="77b33">
<constraints>
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="7d444" class="javax.swing.JButton" binding="buttonCreateTruck">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="120" height="60"/>
</grid>
</constraints>
<properties>
<icon value="Arrow_Up.png"/>
<text value=""/>
<horizontalAlignment value="0"/>
<text value="Создать грузовик"/>
</properties>
</component>
<hspacer id="121e4">
<constraints>
<grid row="3" column="2" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="100" height="2"/>
</grid>
</constraints>
</hspacer>
<component id="62bbe" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="10" fill="0" indent="0" use-parent-layout="false">
<grid row="3" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="62" height="62"/>
<preferred-size width="62" height="62"/>
<maximum-size width="62" height="62"/>
@ -66,9 +90,22 @@
<text value=""/>
</properties>
</component>
<component id="2822e" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="6" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="62" height="62"/>
<preferred-size width="62" height="62"/>
<maximum-size width="62" height="62"/>
</grid>
</constraints>
<properties>
<icon value="Arrow_Up.png"/>
<text value=""/>
</properties>
</component>
<component id="b50ef" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="6" fill="0" indent="0" use-parent-layout="false">
<grid row="3" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="6" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="62" height="62"/>
<preferred-size width="62" height="62"/>
<maximum-size width="62" height="62"/>

View File

@ -1,3 +1,8 @@
import Drawnings.DirectionType;
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
@ -7,15 +12,20 @@ import java.util.List;
import java.util.LinkedList;
public class FormCleaningCar extends JFrame {
protected DrawningCleaningCar _drawningCleaningCar = new DrawningCleaningCar();
protected DrawningTruck _drawningTruck;
JPanel pictureBox;
private JButton buttonCreate;
private JButton buttonCreateCleaningCar;
private JButton buttonRight;
private JButton buttonLeft;
private JButton buttonDown;
private JButton buttonUp;
private JButton buttonCreateTruck;
private JComboBox comboBoxStrategy;
private JButton buttonStrategyStep;
private List<JComponent> controls;
private AbstractStrategy _strategy;
public FormCleaningCar() {
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
@ -23,19 +33,17 @@ public class FormCleaningCar extends JFrame {
buttonRight.setName("buttonRight");
InitializeControlsRepaintList();
buttonCreate.addActionListener(new ActionListener() {
// _strategy = null;
buttonCreateCleaningCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_drawningCleaningCar.Init(random.nextInt(100, 300), random.nextInt(1000, 3000),
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(),
random.nextBoolean());
_drawningCleaningCar.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight());
_drawningCleaningCar.SetPosition(random.nextInt(0, 200), random.nextInt(0, 200));
Draw();
CreateObject("DrawningTruck");
}
});
buttonCreateTruck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawningCleaningCar");
}
});
ActionListener buttonMoveClickedListener = new ActionListener() {
@ -46,19 +54,19 @@ public class FormCleaningCar extends JFrame {
switch (buttonName) {
case "buttonUp": {
result = _drawningCleaningCar.MoveTransport(DirectionType.Up);
result = _drawningTruck.MoveTransport(DirectionType.Up);
}
break;
case "buttonDown": {
result = _drawningCleaningCar.MoveTransport(DirectionType.Down);
result = _drawningTruck.MoveTransport(DirectionType.Down);
}
break;
case "buttonLeft": {
result = _drawningCleaningCar.MoveTransport(DirectionType.Left);
result = _drawningTruck.MoveTransport(DirectionType.Left);
}
break;
case "buttonRight": {
result = _drawningCleaningCar.MoveTransport(DirectionType.Right);
result = _drawningTruck.MoveTransport(DirectionType.Right);
}
break;
@ -68,19 +76,81 @@ public class FormCleaningCar extends JFrame {
}
};
buttonStrategyStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawningTruck == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) {
case 0:
_strategy = new MoveToCenter();
break;
case 1:
_strategy = new MoveToBorder();
break;
default:
_strategy = null;
break;
}
if (_strategy == null) {
return;
}
_strategy.SetData(new MoveableTruck(_drawningTruck), pictureBox.getWidth(), pictureBox.getHeight());
}
if (_strategy == null) {
return;
}
comboBoxStrategy.setEnabled(false);
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish) {
comboBoxStrategy.setEnabled(true);
_strategy = null;
}
}
});
buttonRight.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonUp.addActionListener(buttonMoveClickedListener);
comboBoxStrategy.addItem("К центру");
comboBoxStrategy.addItem("К краю");
}
private void CreateObject(String type) {
Random random = new Random();
switch (type) {
case "DrawningTruck":
_drawningTruck = new DrawningTruck(random.nextInt(100, 300), random.nextInt(100, 300),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
break;
case "DrawningCleaningCar":
_drawningTruck = new DrawningCleaningCar(random.nextInt(100, 300), random.nextInt(100, 300),
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(), random.nextBoolean());
break;
default: return;
}
_drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight());
_drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(0, 100));
_strategy = null;
comboBoxStrategy.setEnabled(true);
Draw();
}
private void Draw() {
if (_drawningCleaningCar.getEntityCleaningCar() == null ||
if (_drawningTruck.getEntityTruck() == null ||
pictureBox.getWidth() == 0 || pictureBox.getHeight() == 0)
return;
Graphics g = pictureBox.getGraphics();
g.setColor(pictureBox.getBackground());
g.fillRect(0,0, pictureBox.getWidth(), pictureBox.getHeight());
_drawningCleaningCar.DrawTransport(g);
_drawningTruck.DrawTransport(g);
RepaintControls();
@ -92,10 +162,15 @@ public class FormCleaningCar extends JFrame {
}
private void InitializeControlsRepaintList() {
controls = new LinkedList<>();
controls.add(buttonCreate);
controls.add(buttonCreateCleaningCar);
controls.add(buttonCreateTruck);
controls.add(buttonUp);
controls.add(buttonDown);
controls.add(buttonLeft);
controls.add(buttonRight);
controls.add(comboBoxStrategy);
controls.add(buttonStrategyStep);
}
}

View File

@ -0,0 +1,55 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObjects _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
protected int FieldWidth;
public int getFieldWidth() {return FieldWidth;}
protected int FieldHeight;
public int getFieldHeight() {return FieldHeight;}
public StrategyStatus GetStatus() {return _state;}
public void SetData(IMoveableObjects moveableObjects, int width, int height) {
if (moveableObjects == null) {
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObjects;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep() {
if (_state != StrategyStatus.InProgress) {
return;
}
if (IsTargetDestination()) {
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() { return MoveTo(MovementDirection.Left); }
protected boolean MoveRight() { return MoveTo(MovementDirection.Right); }
protected boolean MoveUp() { return MoveTo(MovementDirection.Up); }
protected boolean MoveDown() { return MoveTo(MovementDirection.Down); }
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestination();
protected Integer GetStep() {
if (_state != StrategyStatus.InProgress) {
return null;
}
return _moveableObject.GetStep();
}
private boolean MoveTo(MovementDirection directionType) {
if (_state != StrategyStatus.InProgress) {
return false;
}
return _moveableObject.TryMoveObject(directionType);
}
}

View File

@ -0,0 +1,9 @@
package MovementStrategy;
public interface IMoveableObjects {
ObjectParameters GetObjectPosition();
int GetStep();
boolean TryMoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,37 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean IsTargetDestination() {
ObjectParameters objParameters = GetObjectParameters();
if (objParameters == null) {
return false;
}
return objParameters.RightBorder() + GetStep() >= FieldWidth
&& objParameters.DownBorder() + GetStep() >= FieldHeight;
}
@Override
protected void MoveToTarget() {
ObjectParameters objParameters = GetObjectParameters();
if (objParameters == null) {
return;
}
int diffX = objParameters.RightBorder();
if (Math.abs(diffX) > GetStep()) {
if (diffX < 0) {
MoveLeft();
} else {
MoveRight();
}
}
int diffY = objParameters.DownBorder();
if (Math.abs(diffY) > GetStep()) {
if (diffY < 0) {
MoveUp();
} else {
MoveDown();
}
}
}
}

View File

@ -0,0 +1,37 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean IsTargetDestination() {
ObjectParameters objParameters = GetObjectParameters();
if (objParameters == null) {
return false;
}
return objParameters.ObjectMiddleHorizontal() - GetStep() <= FieldWidth / 2 && objParameters.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2
&& objParameters.ObjectMiddleVertical() - GetStep() <= FieldHeight / 2 && objParameters.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2;
}
@Override
protected void MoveToTarget() {
ObjectParameters objParameters = GetObjectParameters();
if (objParameters == null) {
return;
}
int diffX = objParameters.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep()) {
if (diffX > 0) {
MoveLeft();
} else {
MoveRight();
}
}
int diffY = objParameters.ObjectMiddleHorizontal() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
} else {
MoveDown();
}
}
}
}

View File

@ -0,0 +1,38 @@
package MovementStrategy;
import Drawnings.DirectionType;
import Drawnings.DrawningTruck;
public class MoveableTruck implements IMoveableObjects {
private DrawningTruck _truck = null;
public MoveableTruck(DrawningTruck drawningTruck) {
_truck = drawningTruck;
}
public ObjectParameters GetObjectPosition() {
if (_truck == null || _truck.getEntityTruck() == null) {
return null;
}
return new ObjectParameters(_truck.GetPosX(), _truck.GetPosY(), _truck.GetWidth(), _truck.GetHeight());
}
public int GetStep() {return (int)(_truck.getEntityTruck().Step());}
public boolean TryMoveObject(MovementDirection direction) {
if (_truck == null || _truck.getEntityTruck() == null) {
return false;
}
return _truck.MoveTransport(GetDirectionType(direction));
}
private static DirectionType GetDirectionType(MovementDirection direction) {
switch (direction) {
case MovementDirection.Up -> {return DirectionType.Up;}
case MovementDirection.Right -> {return DirectionType.Right;}
case MovementDirection.Down -> {return DirectionType.Down;}
case MovementDirection.Left -> {return DirectionType.Left;}
default -> {
return DirectionType.Unknown;
}
}
}
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public enum MovementDirection {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,25 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
public int LeftBorder() {return _x;}
public int TopBorder() {return _y;}
public int RightBorder() {return _x + _width;}
public int DownBorder() {return _y + _height;}
public int 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,7 @@
package MovementStrategy;
public enum StrategyStatus {
NotInit,
InProgress,
Finish
}