Compare commits

...

2 Commits

27 changed files with 1133 additions and 0 deletions

11
src/src.iml Normal file
View 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>

View File

@ -0,0 +1,16 @@
package Drawings;
import javax.swing.*;
import java.awt.*;
public class CanvasGasolineTanker extends JComponent{
public DrawingTruck _drawingTruck;
public CanvasGasolineTanker(){}
public void paintComponent(Graphics g) {
if (_drawingTruck == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingTruck.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,13 @@
package Drawings;
public enum DirectionType {
Unknow,
//Вверх
Up,
//Вниз
Down,
//Влево
Left,
//Вправо
Right
}

View File

@ -0,0 +1,48 @@
package Drawings;
import Entities.EntityGasolineTanker;
import Wheels.IDrawingWheels;
import java.awt.*;
public class DrawingGasolineTanker extends DrawingTruck{
private IDrawingWheels drawingWheels;
public DrawingGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean gasTank, boolean signalBeacon, int number) {
super(105,70);
EntityTruck = new EntityGasolineTanker(speed, weight, bodyColor, additionalColor, gasTank, signalBeacon, number);
DrawWheels();
}
@Override
public void DrawTransport(Graphics2D g) {
if (EntityTruck == null || !(EntityTruck instanceof EntityGasolineTanker gasolineTanker) || _startPosX == null || _startPosY == null) {
return;
}
// бензобак
if (gasolineTanker.GasTank) {
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 7, _startPosY + 18, 31, 31);
g.drawOval(_startPosX + 47, _startPosY + 18, 31, 31);
g.drawRect(_startPosX + 20, _startPosY + 20, 45, 30);
g.setColor(gasolineTanker.getAdditionalColor());
g.fillOval(_startPosX + 7, _startPosY + 18, 31, 31);
g.fillOval(_startPosX + 47, _startPosY + 18, 31, 31);
g.fillRect(_startPosX + 20, _startPosY + 18, 45, 30);
}
super.DrawTransport(g); // Обращение к методу DrawTransport базового класса
// сигнальный маяк
if (gasolineTanker.SignalBeacon) {
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 85, _startPosY + 8, 7, 7);
g.setColor(gasolineTanker.getAdditionalColor());
g.fillRect(_startPosX + 85, _startPosY + 8, 7, 7);
}
}
}

View File

@ -0,0 +1,207 @@
package Drawings;
import Entities.EntityTruck;
import Wheels.DrawingOrnamentSquare;
import Wheels.DrawingOrnamentHeart;
import Wheels.DrawingWheels;
import Wheels.IDrawingWheels;
import javax.swing.*;
import java.awt.*;
public class DrawingTruck extends JPanel{
private IDrawingWheels drawingWheels;
public Entities.EntityTruck EntityTruck;
// Ширина окна
private Integer _pictureWidth;
// Высота окна
private Integer _pictureHeight;
// Левая координата прорисовки бензовоза
protected Integer _startPosX;
// Верхняя координата прорисовки бензовоза
protected Integer _startPosY;
// Ширина прорисовки бензовоза
protected int _drawingTruckWidth = 105;
// Высота прорисовки бензовоза
protected int _drawingTruckHeight = 70;
public Integer GetPosX() {return _startPosX;}
public Integer GetPosY() {return _startPosY;}
public Integer GetWidth() {return _drawingTruckWidth;}
public Integer GetHeight() {return _drawingTruckHeight;}
protected void DrawWheels() {
int number = (int)(Math.random() * 4 + 0);
switch ((int)(Math.random() * 3 + 1)) {
case 1:
drawingWheels = new DrawingWheels();
break;
case 2:
drawingWheels = new DrawingOrnamentSquare();
break;
case 3:
drawingWheels = new DrawingOrnamentHeart();
break;
default:
number = 0;
break;
}
drawingWheels.setNumWheels(number);
}
protected DrawingTruck(){
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public DrawingTruck(int speed, double weight, Color bodyColor, int number){
super();
EntityTruck = new EntityTruck(speed, weight, bodyColor);
DrawWheels();
}
protected DrawingTruck(int drawingTruckWidth, int drawingTruckHeight)
{
_drawingTruckWidth = drawingTruckWidth;
_drawingTruckHeight = drawingTruckHeight;
}
// Установка границ поля
public boolean SetPictureSize(int width, int height)
{
// Проверка "влезает" ли объект в размеры поля
if (_drawingTruckWidth > width || _drawingTruckHeight > height)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_drawingTruckWidth + _startPosX > _pictureWidth)
{
_startPosX = _pictureWidth - _drawingTruckWidth;
}
else if (_startPosX < 0)
{
_startPosX = 0;
}
if (_drawingTruckHeight + _startPosY > _pictureHeight)
{
_startPosY = _pictureHeight - _drawingTruckHeight;
}
else if (_startPosY < 0)
{
_startPosY = 0;
}
}
return true;
}
// Установка позиции
public void SetPosition(int x, int y)
{
if (!(_pictureHeight != null && _pictureWidth != null))
{
return;
}
if (x >= 0 && x + _drawingTruckWidth <= _pictureWidth && y >= 0 && y + _drawingTruckHeight <= _pictureHeight)
{
_startPosX = x;
_startPosY = y;
}
if (x < 0)
{
_startPosX = 0;
}
if (y < 0)
{
_startPosY = 0;
}
if (x + _drawingTruckWidth > _pictureWidth)
{
_startPosX = _drawingTruckWidth - _pictureWidth;
}
if (y + _drawingTruckHeight > _pictureHeight)
{
_startPosY = _drawingTruckHeight - _pictureHeight;
}
}
// Изменение направления
public boolean MoveTransport(DirectionType direction)
{
if (EntityTruck == null || _startPosX == null || _startPosY == null)
{
return false;
}
switch(direction)
{
// влево
case Left:
if (_startPosX - EntityTruck.Step > 0)
{
_startPosX -= (int)EntityTruck.Step;
}
return true;
// вверх
case Up:
if (_startPosY - EntityTruck.Step > 0)
{
_startPosY -= (int)EntityTruck.Step;
}
return true;
// вправо
case Right:
if (_startPosX + EntityTruck.Step + _drawingTruckWidth < _pictureWidth)
{
_startPosX += (int)EntityTruck.Step;
}
return true;
// вниз
case Down:
if (_startPosY + EntityTruck.Step + _drawingTruckHeight < _pictureHeight)
{
_startPosY += (int)EntityTruck.Step;
}
return true;
default:
return false;
}
}
// Прорисовка объекта
public void DrawTransport(Graphics2D g)
{
if (EntityTruck == null || _startPosX == null || _startPosY == null)
{
return;
}
// колеса
drawingWheels.drawWheels(g, Color.BLACK, _startPosX, _startPosY);
// нижняя платформа
g.setColor(EntityTruck.getBodyColor());
g.fillRect(_startPosX + 5, _startPosY + 40, 100, 10);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 5, _startPosY + 40, 100, 10);
// кабина
g.setColor(Color.BLUE);
g.fillRect(_startPosX + 80, _startPosY + 15, 25, 25);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 80, _startPosY + 15, 25, 25);
}
}

View File

@ -0,0 +1,24 @@
package Entities;
import java.awt.*;
public class EntityGasolineTanker extends EntityTruck{
// Дополнительный цвет(для опциональныз элементов)
public Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
// Признак (опция) наличия бака под бензин
public boolean GasTank;
public boolean getGasTank() {return GasTank;}
// Признак (опция) наличия сигнального маяка на кабине
public boolean SignalBeacon;
public boolean getSignalBeacon() {return SignalBeacon;}
public EntityGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean signalBeacon, boolean gasTank, int numWheels){
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
GasTank = gasTank;
SignalBeacon = signalBeacon;
}
}

View File

@ -0,0 +1,25 @@
package Entities;
import java.awt.*;
public class EntityTruck {
// Скорость
private int Speed;
// Вес
private double Weight;
// Основной цвет
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
// Шаг перемещения бензовоза
public double Step;
public EntityTruck(int speed, double weight, Color bodyColor){
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
Step = Speed * 200 / Weight;
}
}

View File

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormGasolineTanker">
<grid id="27dc6" layout-manager="GridLayoutManager" row-count="3" column-count="5" 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="945" height="439"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="6cce4" class="javax.swing.JButton" binding="buttonCreateTruck">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать грузовик"/>
</properties>
</component>
<vspacer id="b7eca">
<constraints>
<grid row="0" column="0" row-span="2" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="bdb21" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="resources/Right.jpg"/>
<text value=""/>
</properties>
</component>
<component id="c59ad" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="resources/Left.jpg"/>
<text value=""/>
</properties>
</component>
<component id="7c97d" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="resources/Down.jpg"/>
<text value=""/>
</properties>
</component>
<component id="10d3d" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="3" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="resources/Up.jpg"/>
<text value=""/>
</properties>
</component>
<component id="62c53" class="javax.swing.JButton" binding="buttonCreateGasolineTanker">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать бензовоз"/>
</properties>
</component>
<component id="fabd7" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="9" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="К центру"/>
<item value="К краю"/>
</model>
</properties>
</component>
<component id="2bd6d" class="javax.swing.JButton" binding="buttonStrategy">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="5" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,207 @@
import Drawings.CanvasGasolineTanker;
import Drawings.DirectionType;
import Drawings.DrawingGasolineTanker;
import Drawings.DrawingTruck;
import MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormGasolineTanker extends JFrame{
private int Width;
private int Height;
private CanvasGasolineTanker canvasGasolineTanker = new CanvasGasolineTanker();
private JButton buttonCreateTruck = new JButton("Создать грузовик");
private JButton buttonRight = new JButton();
private JButton buttonLeft = new JButton();
private JButton buttonDown = new JButton();
private JButton buttonUp = new JButton();
private AbstractStrategy _strategy;
private JButton buttonCreateGasolineTanker = new JButton("Создать бензовоз");
private JComboBox comboBoxStrategy = new JComboBox(new String[] {"К центру", "К краю"});
private JButton buttonStrategy = new JButton("Шаг");
private void CreateObject(String typeOfClass){
int StartPositionX = 10 + (int)(Math.random() *((100-10) +1));
int StartPositionY = 10 + (int)(Math.random() *((100-10) +1));
int speed = 10 + (int)(Math.random() *((100-10) +1));
double weight = 1000 + (double)(Math.random() *((5000-1000) +1));
Color bodyColor = new Color((int) (Math.random() * ((256) + 1)), (int) (Math.random() * ((256) + 1)), (int) (Math.random() * ((256) + 1)));
int number = new Random().nextInt(3)+1;
switch (typeOfClass){
case "DrawingTruck":
canvasGasolineTanker._drawingTruck = new DrawingTruck(speed, weight, bodyColor, number);
canvasGasolineTanker._drawingTruck.SetPictureSize(Width,Height);
canvasGasolineTanker._drawingTruck.SetPosition(StartPositionX, StartPositionY);
canvasGasolineTanker.repaint();
break;
case "DrawingGasolineTanker":
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
boolean gasTank = new Random().nextBoolean();
boolean signalBeacon = new Random().nextBoolean();
canvasGasolineTanker._drawingTruck = new DrawingGasolineTanker(speed, weight, bodyColor, additionalColor, signalBeacon, gasTank, number);
canvasGasolineTanker._drawingTruck.SetPictureSize(Width,Height);
canvasGasolineTanker._drawingTruck.SetPosition(StartPositionX, StartPositionY);
canvasGasolineTanker.repaint();
break;
default: return;
}
_strategy = null;
comboBoxStrategy.setEnabled(true);
}
public void Init(){
setTitle("Бензовоз");
setSize(900,600);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Width = getWidth();
Height = getHeight();
_strategy = null;
buttonCreateTruck.setName("CREATETRUCK");
buttonCreateGasolineTanker.setName("CREATEGASOLINETANKER");
buttonUp.setName("buttonUp");
Icon iconUp = new ImageIcon("src\\src\\resources\\Up.jpg");
buttonUp.setIcon(iconUp);
buttonDown.setName("buttonDown");
Icon iconDown = new ImageIcon("src\\src\\resources\\Down.jpg");
buttonDown.setIcon(iconDown);
buttonLeft.setName("buttonLeft");
Icon iconLeft = new ImageIcon("src\\src\\resources\\Left.jpg");
buttonLeft.setIcon(iconLeft);
buttonRight.setName("buttonRight");
Icon iconRight = new ImageIcon("src\\src\\resources\\Right.jpg");
buttonRight.setIcon(iconRight);
buttonCreateTruck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingTruck");
}
});
buttonCreateGasolineTanker.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawingGasolineTanker");
}
});
buttonStrategy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (canvasGasolineTanker._drawingTruck == null) return;
if (comboBoxStrategy.isEnabled()) {
int ind = comboBoxStrategy.getSelectedIndex();
switch (ind) {
case 0:
_strategy = new MoveToCenter();
break;
case 1:
_strategy = new MoveToBorder();
break;
default:
_strategy = null;
break;
};
if (_strategy == null) {
return;
}
_strategy.SetData(new MoveableTruck(canvasGasolineTanker._drawingTruck), Width, Height);
}
if (_strategy == null) {
return;
}
comboBoxStrategy.setEnabled(false);
_strategy.MakeStep();
if (_strategy.GetStatus() == StrategyStatus.Finish) {
comboBoxStrategy.setEnabled(true);
_strategy = null;
}
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasGasolineTanker._drawingTruck == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "buttonUp":
result = canvasGasolineTanker._drawingTruck.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result = canvasGasolineTanker._drawingTruck.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result = canvasGasolineTanker._drawingTruck.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result = canvasGasolineTanker._drawingTruck.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasGasolineTanker.repaint();
}
}
};
buttonUp.addActionListener(actionListener);
buttonDown.addActionListener(actionListener);
buttonLeft.addActionListener(actionListener);
buttonRight.addActionListener(actionListener);
setLayout(null);
canvasGasolineTanker.setBounds(0,0, getWidth(), getHeight());
buttonCreateTruck.setBounds(10, getHeight() - 90, 150, 40);
buttonCreateGasolineTanker.setBounds(170, getHeight() - 90, 150, 40);
buttonUp.setBounds(getWidth() - 200, getHeight() - 160, 45, 45);
buttonDown.setBounds(getWidth() - 140, getHeight() - 100, 45, 45);
buttonRight.setBounds(getWidth() - 80, getHeight() - 100, 45, 45);
buttonLeft.setBounds(getWidth() - 200, getHeight() - 100, 45, 45);
comboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
buttonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
add(buttonCreateTruck);
add(buttonCreateGasolineTanker);
add(buttonUp);
add(buttonDown);
add(buttonRight);
add(buttonLeft);
add(canvasGasolineTanker);
add(buttonStrategy);
add(comboBoxStrategy);
setVisible(true);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasGasolineTanker._drawingTruck != null)
canvasGasolineTanker._drawingTruck.SetPictureSize(Width, Height);
canvasGasolineTanker.setBounds(0,0, getWidth(), getHeight());
buttonCreateTruck.setBounds(10, getHeight() - 90, 150, 40);
buttonCreateGasolineTanker.setBounds(170, getHeight() - 90, 150, 40);
buttonUp.setBounds(getWidth() - 140, getHeight() - 160, 45, 45);
buttonDown.setBounds(getWidth() - 140, getHeight() - 100, 45, 45);
buttonRight.setBounds(getWidth() - 80, getHeight() - 100, 45, 45);
buttonLeft.setBounds(getWidth() - 200, getHeight() - 100, 45, 45);
comboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
buttonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
}
});
}
}

7
src/src/Main.java Normal file
View File

@ -0,0 +1,7 @@
import java.awt.*;
public class Main{
public static void main(String[] args){
FormGasolineTanker formGasolineTanker = new FormGasolineTanker();
formGasolineTanker.Init();
}
}

View File

@ -0,0 +1,63 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObjects _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
public int FieldWidth;
public int 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 (IsTargetDestinaion()) {
_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 Integer GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestinaion();
private boolean MoveTo(MovementDirection movementDirection)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
boolean stateTryMoveObject = _moveableObject.TryMoveObject(movementDirection);
if (stateTryMoveObject)
{
return stateTryMoveObject;
}
return false;
}
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
// только объявляются методы
public interface IMoveableObjects {
ObjectParameters GetObjectPosition();
int GetStep();
boolean TryMoveObject(MovementDirection direction);
}

View File

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

View File

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

View File

@ -0,0 +1,44 @@
package MovementStrategy;
import Drawings.CanvasGasolineTanker;
import Drawings.DirectionType;
import Drawings.DrawingTruck;
public class MoveableTruck implements IMoveableObjects{
private CanvasGasolineTanker canvas = new CanvasGasolineTanker();
public MoveableTruck(DrawingTruck drawingTruck)
{
canvas._drawingTruck = drawingTruck;
}
@Override
public ObjectParameters GetObjectPosition() {
if (canvas._drawingTruck == null || canvas._drawingTruck.EntityTruck == null ||
canvas._drawingTruck.GetPosX() == null || canvas._drawingTruck.GetPosY() == null)
{
return null;
}
return new ObjectParameters(canvas._drawingTruck.GetPosX(), canvas._drawingTruck.GetPosY(),
canvas._drawingTruck.GetWidth(), canvas._drawingTruck.GetHeight());
}
@Override
public int GetStep() {
return (int)(canvas._drawingTruck.EntityTruck.Step);
}
@Override
public boolean TryMoveObject(MovementDirection direction) {
if (canvas._drawingTruck == null || canvas._drawingTruck.EntityTruck == null)
{
return false;
}
return canvas._drawingTruck.MoveTransport(GetDirectionType(direction));
}
private static DirectionType GetDirectionType(MovementDirection direction)
{
switch (direction) {
case Left: return DirectionType.Left;
case Right: return DirectionType.Right;
case Up: return DirectionType.Up;
case Down: return DirectionType.Down;
default: return DirectionType.Unknow;
}
}
}

View File

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

View File

@ -0,0 +1,27 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
public int LeftBorder = _x;
public int TopBorder = _y;
public int RightBorder = _x + _width;
public int DownBorder = _y + _height;
public int ObjectMiddleHorizontal = _x + _width / 2;
public int ObjectMiddleVertical = _y + _height / 2;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
LeftBorder = _x;
TopBorder = _y;
RightBorder = _x + _width;
DownBorder = _y + _height;
ObjectMiddleHorizontal = _x + _width / 2;
ObjectMiddleVertical = _y + _height / 2;
}
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public enum StrategyStatus {
NotInit,
InProgress,
Finish
}

View File

@ -0,0 +1,82 @@
package Wheels;
import java.awt.*;
public class DrawingOrnamentHeart implements IDrawingWheels{
private NumWheels numWheels;
@Override
public NumWheels getNumWheels() {
return numWheels;
}
@Override
public void setNumWheels(int number) {
switch (number) {
case 1:
numWheels = NumWheels.Two;
break;
case 2:
numWheels = NumWheels.Three;
break;
case 3:
numWheels = NumWheels.Four;
break;
default:
numWheels = NumWheels.Three;
}
}
@Override
public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numWheels) {
case Two:
drawTwoWheels(g2D, color, _startPosX, _startPosY);
break;
case Three:
drawThreeWheels(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourWheels(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
drawOrnament(g2D, _startPosX + 5, _startPosY + 50);
drawOrnament(g2D, _startPosX + 85, _startPosY + 50);
}
private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 25, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
drawOrnament(g2D, _startPosX + 5, _startPosY + 50);
drawOrnament(g2D, _startPosX + 25, _startPosY + 50);
drawOrnament(g2D, _startPosX + 85, _startPosY + 50);
}
private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 25, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 65, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
drawOrnament(g2D, _startPosX + 5, _startPosY + 50);
drawOrnament(g2D, _startPosX + 25, _startPosY + 50);
drawOrnament(g2D, _startPosX + 65, _startPosY + 50);
drawOrnament(g2D, _startPosX + 85, _startPosY + 50);
}
private void drawOrnament(Graphics2D g2D, int _startPosX, int _startPosY) {
int[] X = {_startPosX + 2, _startPosX + 5, _startPosX + 9,_startPosX + 12, _startPosX + 15, _startPosX + 15, _startPosX + 9, _startPosX + 2};
int[] Y = {_startPosY + 5, _startPosY + 5, _startPosY+ 8, _startPosY + 5, _startPosY + 5, _startPosY + 10, _startPosY + 15, _startPosY + 10};
g2D.setColor(Color.GREEN);
g2D.fillPolygon(X, Y, X.length);
g2D.setColor(Color.BLACK);
g2D.drawPolygon(X, Y, X.length);
}
}

View File

@ -0,0 +1,82 @@
package Wheels;
import java.awt.*;
public class DrawingOrnamentSquare implements IDrawingWheels {
private NumWheels numWheels;
@Override
public NumWheels getNumWheels() {
return numWheels;
}
@Override
public void setNumWheels(int number) {
switch (number) {
case 1:
numWheels = NumWheels.Two;
break;
case 2:
numWheels = NumWheels.Three;
break;
case 3:
numWheels = NumWheels.Four;
break;
default:
numWheels = NumWheels.Three;
}
}
@Override
public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numWheels) {
case Two:
drawTwoWheels(g2D, color, _startPosX, _startPosY);
break;
case Three:
drawThreeWheels(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourWheels(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
drawOrnament(g2D, _startPosX + 5, _startPosY + 50);
drawOrnament(g2D, _startPosX + 85, _startPosY + 50);
}
private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 25, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
drawOrnament(g2D, _startPosX + 5, _startPosY + 50);
drawOrnament(g2D, _startPosX + 25, _startPosY + 50);
drawOrnament(g2D, _startPosX + 85, _startPosY + 50);
}
private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 25, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 65, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
drawOrnament(g2D, _startPosX + 5, _startPosY + 50);
drawOrnament(g2D, _startPosX + 25, _startPosY + 50);
drawOrnament(g2D, _startPosX + 65, _startPosY + 50);
drawOrnament(g2D, _startPosX + 85, _startPosY + 50);
}
private void drawOrnament(Graphics2D g2D, int _startPosX, int _startPosY) {
g2D.setColor(Color.ORANGE);
g2D.fillRect(_startPosX+5, _startPosY+4, 9, 9);
g2D.setColor(Color.BLACK);
g2D.drawRect(_startPosX+5, _startPosY+4, 9, 9);
}
}

View File

@ -0,0 +1,65 @@
package Wheels;
import java.awt.*;
public class DrawingWheels implements IDrawingWheels{
private NumWheels numWheels;
@Override
public NumWheels getNumWheels() {
return numWheels;
}
@Override
public void setNumWheels(int number) {
switch (number) {
case 1:
numWheels = NumWheels.Two;
break;
case 2:
numWheels = NumWheels.Three;
break;
case 3:
numWheels = NumWheels.Four;
break;
default:
numWheels = NumWheels.Three;
}
}
@Override
public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numWheels) {
case Two:
drawTwoWheels(g2D, color, _startPosX, _startPosY);
break;
case Three:
drawThreeWheels(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourWheels(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
}
private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 25, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
}
private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
g2D.fillOval(_startPosX + 5, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 25, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 65, _startPosY + 50, 20, 20);
g2D.fillOval(_startPosX + 85, _startPosY + 50, 20, 20);
}
}

View File

@ -0,0 +1,7 @@
package Wheels;
import java.awt.*;
public interface IDrawingWheels {
void setNumWheels(int number);
NumWheels getNumWheels();
void drawWheels(Graphics2D g2d, Color color, int _startPosX, int _startPosY); //
}

View File

@ -0,0 +1,7 @@
package Wheels;
public enum NumWheels {
Two,
Three,
Four
}

BIN
src/src/resources/Down.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

BIN
src/src/resources/Left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

BIN
src/src/resources/Right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

BIN
src/src/resources/Up.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB