Compare commits

...

23 Commits

Author SHA1 Message Date
f055ae0ad9 Лабораторная работа №5 2024-06-11 01:13:51 +04:00
f6d2369ac6 Лабораторная работа №5 (возможно что-то да работает) 2024-06-10 02:52:12 +04:00
fcb6bf6d11 Лабораторная работа №5 (что-то работает) 2024-06-09 23:09:55 +04:00
ed5df725c2 Лабораторная работа №5 (ничего не работает) 2024-06-09 18:56:43 +04:00
4d1afeaa06 Лабораторная работа №5 (начало) 2024-06-09 14:17:18 +04:00
4d36c0c65c Лабораторная работа №4 (вот бы без конфликтов) 2024-06-03 21:09:22 +04:00
399dd73064 Лабораторная работа №4 (попытка решить конфликт слияния 3) 2024-06-03 19:25:46 +04:00
000a7f87a4 Лабораторная работа №4 (попытка решить конфликт слияния 2) 2024-06-03 19:11:34 +04:00
863d9dddcb Лабораторная работа №4 (попытка решить конфликт слияния) 2024-06-03 19:10:21 +04:00
5ad5018775 Лабораторная работа №4 (вроде готово) 2024-06-03 18:08:25 +04:00
a0fbbbb129 Лабораторная работа №4 (2 часть) 2024-05-12 16:15:45 +04:00
2040af37fc Лабораторная работа №4 (1 часть) 2024-05-12 01:21:57 +04:00
2393344f6d Лабораторная работа №3 (почти готово 3) 2024-05-11 23:50:14 +04:00
85918fccd1 Лабораторная работа №3 (почти готово 2) 2024-05-10 10:31:56 +04:00
d2f3c76202 Лабораторная работа №3 (почти готово) 2024-05-10 10:14:18 +04:00
88daf53b25 Лабораторная работа №3 (часть 2) 2024-05-06 01:12:23 +04:00
654d2e36e0 Лабораторная работа №3 (часть 1) 2024-05-05 00:25:32 +04:00
33272174d0 Лабораторная работа №2 (часть 3) 2024-04-26 17:42:10 +04:00
960d74b512 Лабораторная работа №2 (теперь с различными наследниками интерфейса) 2024-04-17 13:02:19 +04:00
c97de62c51 Лабораторная работа №2 (2 часть) 2024-04-16 18:55:35 +04:00
5a10560b04 Лабораторная работа №2 (1 часть) 2024-04-16 01:53:38 +04:00
27a627d610 Лабораторная работа №1 (2 часть) 2024-03-17 23:22:09 +04:00
bbffa9f730 Лабораторная работа №1 (1 часть) 2024-03-17 18:39:01 +04:00
42 changed files with 2746 additions and 13 deletions

View File

@ -2,7 +2,10 @@
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/ProjectCleaningCar/Resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/ProjectCleaningCar/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

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" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -0,0 +1,44 @@
package CollectionGenericObjects;
import Drawnings.DrawningTruck;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class AbstractCompany {
protected final int _placeSizeWidth = 210;
protected final int _placeSizeHeight = 80;
protected int _pictureWidth;
protected int _pictureHeight;
public ICollectionGenericObjects<DrawningTruck> _collection = null;
private int GetMaxCount() {
return _pictureWidth / _placeSizeWidth * (_pictureHeight / _placeSizeHeight / 2);
};
public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects<DrawningTruck> collection) {
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = collection;
_collection.SetMaxCount(GetMaxCount(), (Class) DrawningTruck.class);
}
public DrawningTruck GetRandomObject() {
Random rnd = new Random();
return _collection.Get(rnd.nextInt(GetMaxCount()));
}
public void Show(Graphics g) {
DrawBackground(g);
SetObjectsPosition();
for (int i = 0; i < _collection.Count(); i++) {
DrawningTruck obj = _collection.Get(i);
if (obj != null) {
obj.DrawTransport(g);
}
}
}
protected abstract void DrawBackground(Graphics g);
protected abstract void SetObjectsPosition();
}

View File

@ -0,0 +1,63 @@
package CollectionGenericObjects;
import Drawnings.CountWheels;
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import Drawnings.IDrawningWheels;
import Entities.EntityCleaningCar;
import Entities.EntityTruck;
import java.lang.reflect.Array;
import java.util.Random;
public class AdditionalCollection <T extends EntityTruck, U extends IDrawningWheels>{
public T[] _collectionEntity;
public U[] _collectionWheels;
public AdditionalCollection(int value, Class<T> type1, Class<U> type2) {
_collectionEntity = (T[]) Array.newInstance(type1, value);
_collectionWheels = (U[]) Array.newInstance(type2, value);
}
public int GetCountEntities() {
return _collectionEntity.length;
}
public int GetCountWheels() {
return _collectionWheels.length;
}
public int Insert(T entity) {
for (int i = 0; i < GetCountEntities(); i++)
{
if (_collectionEntity[i] == null)
{
_collectionEntity[i] = entity;
return i;
}
}
return -1;
}
public int Insert(U decks) {
for (int i = 0; i < GetCountWheels(); i++)
{
if (_collectionWheels[i] == null)
{
_collectionWheels[i] = decks;
return i;
}
}
return -1;
}
public DrawningTruck CreateFromAdditionalCollection() {
Random random = new Random();
if (_collectionEntity == null || _collectionWheels == null) return null;
T entity = _collectionEntity[random.nextInt(GetCountEntities())];
U wheels = _collectionWheels[random.nextInt(GetCountWheels())];
DrawningTruck drawingTruck = null;
if (entity instanceof EntityCleaningCar) {
drawingTruck = new DrawningCleaningCar((EntityCleaningCar) entity, wheels);
}
else {
drawingTruck = new DrawningTruck(entity, wheels);
}
return drawingTruck;
}
}

View File

@ -0,0 +1,51 @@
package CollectionGenericObjects;
import Drawnings.DrawningTruck;
import java.awt.*;
public class AutoParkService extends AbstractCompany {
public AutoParkService(int picWidth, int picHeight, ICollectionGenericObjects<DrawningTruck> collection) {
super(picWidth, picHeight, collection);
}
@Override
protected void DrawBackground(Graphics g) {
g.setColor(Color.black);
((Graphics2D) g).setStroke(new BasicStroke(4));
for (int i = 0; i < _pictureHeight / _placeSizeHeight / 2; i++)
{
g.drawLine(0, i * _placeSizeHeight * 2, _pictureWidth / _placeSizeWidth * _placeSizeWidth, i * _placeSizeHeight * 2);
for (int j = 0; j < _pictureWidth / _placeSizeWidth + 1; ++j)
{
g.drawLine(j * _placeSizeWidth, i * _placeSizeHeight * 2, j * _placeSizeWidth, i * _placeSizeHeight * 2 + _placeSizeHeight + 20);
}
}
((Graphics2D) g).setStroke(new BasicStroke(1));
}
@Override
protected void SetObjectsPosition() {
int nowWidth = 0;
int nowHeight = 0;
for (int i = 0; i < _collection.Count(); i++)
{
if (nowHeight > _pictureHeight / _placeSizeHeight)
{
return;
}
if (_collection.Get(i) != null)
{
_collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight);
_collection.Get(i).SetPosition(_placeSizeWidth * nowWidth + 30, nowHeight * _placeSizeHeight * 2 + 20);
}
if (nowWidth < _pictureWidth / _placeSizeWidth - 1) nowWidth++;
else
{
nowWidth = 0;
nowHeight++;
}
}
}
}

View File

@ -0,0 +1,7 @@
package CollectionGenericObjects;
public enum CollectionType {
None,
Massive,
List;
}

View File

@ -0,0 +1,10 @@
package CollectionGenericObjects;
public interface ICollectionGenericObjects<T> {
int Count();
void SetMaxCount(int value, Class<T> type);
int Insert(T obj);
int Insert(T obj, int position);
T Remove(int position);
T Get(int position);
}

View File

@ -0,0 +1,56 @@
package CollectionGenericObjects;
import java.util.ArrayList;
import java.util.List;
public class ListGenericObjects<T> implements ICollectionGenericObjects<T> {
private List<T> _collection;
private int _maxCount;
@Override
public int Count() {
return _collection.size();
}
@Override
public void SetMaxCount(int value, Class<T> type) {
if (value > 0) {
_maxCount = value;
}
}
public ListGenericObjects() {
_collection = new ArrayList<T>();
}
@Override
public int Insert(T obj) {
if (Count() == _maxCount) return -1;
_collection.add(obj);
return Count();
}
@Override
public int Insert(T obj, int position) {
if (Count() == _maxCount) return -1;
if (position > Count() || position < 0) return -1;
_collection.add(position, obj);
return 0;
}
@Override
public T Remove(int position) {
if (position >= _maxCount || position < 0) {
return null;
}
T myObj = _collection.get(position);
_collection.remove(position);
return myObj;
}
@Override
public T Get(int position) {
if (position >= Count() || position < 0) {
return null;
}
return _collection.get(position);
}
}

View File

@ -0,0 +1,72 @@
package CollectionGenericObjects;
import java.lang.reflect.Array;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
private T[] _collection;
@Override
public void SetMaxCount(int value, Class<T> type) {
if (value > 0) {
if (_collection == null)
_collection = (T[]) Array.newInstance(type, value);
}
}
@Override
public int Count() {
return _collection.length;
}
@Override
public int Insert(T obj) {
for (int i = 0; i < Count(); i++)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
return -1;
}
@Override
public int Insert(T obj, int position) {
if (position >= Count() || position < 0) return -1;
if (_collection[position] == null)
{
_collection[position] = obj;
return position;
}
int temp = position + 1;
while(temp < Count())
{
if (_collection[temp] == null)
{
_collection[temp] = obj;
return temp;
}
++temp;
}
temp = position - 1;
while(temp >= 0)
{
if (_collection[temp] == null)
{
_collection[temp] = obj;
return temp;
}
--temp;
}
return -1;
}
@Override
public T Remove(int position) {
if (position >= Count() || position < 0) return null;
T myObject = (T) _collection[position];
_collection[position] = null;
return myObject;
}
@Override
public T Get(int position) {
if (position < 0 || position >= Count()) return null;
return (T) _collection[position];
}
}

View File

@ -0,0 +1,45 @@
package CollectionGenericObjects;
import java.util.*;
public class StorageCollection<T> {
private Map<String, ICollectionGenericObjects<T>> _storages;
public Set<String> Keys() {
return _storages.keySet();
}
public StorageCollection() {
_storages = new HashMap<>();
}
public void AddCollection(String name, CollectionType collectionType) {
if (name == null || _storages.containsKey(name)) {
return;
}
switch (collectionType) {
case None:
return;
case Massive:
_storages.put(name, new MassiveGenericObjects<T>());
return;
case List:
_storages.put(name, new ListGenericObjects<T>());
return;
}
}
public void DelCollection(String name) {
if (_storages.containsKey(name)) {
_storages.remove(name);
}
}
public ICollectionGenericObjects<T> GetCollectionObject(String name) {
if (_storages.containsKey(name)) {
return _storages.get(name);
}
return null;
}
public T Remove(String name, int position){
if(_storages.containsKey(name))
return _storages.get(name).Remove(position);
return null;
}
}

View File

@ -0,0 +1,14 @@
package Drawnings;
public enum CountWheels {
One(1),
Two(2),
Three(3);
private final int count;
CountWheels(int count) {
this.count = count;
}
public int getCountWheels() {
return count;
}
}

View File

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

View File

@ -0,0 +1,36 @@
package Drawnings;
import java.awt.*;
public class DrawningCircleWheels implements IDrawningWheels {
private CountWheels countWheels;
@Override
public void setCountWheels(int numWheels) {
for (CountWheels value : CountWheels.values()) {
if (value.getCountWheels() == numWheels) {
countWheels = value;
return;
}
}
}
@Override
public CountWheels getNumberOfWheels() {
return countWheels;
}
@Override
public void DrawCleaningCarWheels(Graphics g, Color bodyColor, Color additionalColor, int startPosX, int startPosY) {
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < countWheels.getCountWheels(); i++) {
g2d.setColor(bodyColor);
g2d.fillOval(startPosX + 10 + 40 * i, startPosY + 50, 20, 20);
g2d.setColor(additionalColor);
g2d.fillOval(startPosX + 15 + 40 * i, startPosY + 55, 10, 10);
g2d.setColor(Color.black);
g2d.drawOval(startPosX + 10 + 40 * i, startPosY + 50, 19, 19);
g2d.drawOval(startPosX + 15 + 40 * i, startPosY + 55, 9, 9);
}
}
}

View File

@ -0,0 +1,51 @@
package Drawnings;
import java.awt.*;
import Entities.*;
public class DrawningCleaningCar extends DrawningTruck {
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);
super.SetWheelsType();
}
public DrawningCleaningCar(EntityCleaningCar cleaningCar, IDrawningWheels drawningWheels) {
EntityTruck = cleaningCar;
this.drawningWheels = drawningWheels;
}
@Override
public void DrawTransport(Graphics 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);
}
super.DrawTransport(g);
}
}

View File

@ -0,0 +1,41 @@
package Drawnings;
import java.awt.*;
public class DrawningRhombWheels implements IDrawningWheels {
private CountWheels countWheels;
@Override
public void setCountWheels(int numWheels) {
for (CountWheels value : CountWheels.values()) {
if (value.getCountWheels() == numWheels) {
countWheels = value;
return;
}
}
}
@Override
public CountWheels getNumberOfWheels() {
return countWheels;
}
@Override
public void DrawCleaningCarWheels(Graphics g, Color bodyColor, Color additionalColor, int startPosX, int startPosY) {
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < countWheels.getCountWheels(); i++) {
g2d.setColor(bodyColor);
int[] xPoints = {startPosX + 20 + 40 * i, startPosX + 10 + 40 * i, startPosX + 20 + 40 * i,
startPosX + 30 + 40 * i, startPosX + 20 + 40 * i};
int[] yPoints = {startPosY + 50, startPosY + 60, startPosY + 70,
startPosY + 60, startPosY + 50};
g2d.fillPolygon(xPoints, yPoints, 5);
g2d.setColor(additionalColor);
int[] xPoints2 = {startPosX + 20 + 40 * i, startPosX + 13 + 40 * i, startPosX + 20 + 40 * i,
startPosX + 27 + 40 * i, startPosX + 20 + 40 * i};
int[] yPoints2 = {startPosY + 52, startPosY + 60, startPosY + 68,
startPosY + 60, startPosY + 52};
g2d.fillPolygon(xPoints2, yPoints2, 5);
g2d.drawPolyline(xPoints, yPoints, 5);
}
}
}

View File

@ -0,0 +1,41 @@
package Drawnings;
import java.awt.*;
public class DrawningTriangleWheels implements IDrawningWheels {
private CountWheels countWheels;
@Override
public void setCountWheels(int numWheels) {
for (CountWheels value : CountWheels.values()) {
if (value.getCountWheels() == numWheels) {
countWheels = value;
return;
}
}
}
@Override
public CountWheels getNumberOfWheels() {
return countWheels;
}
@Override
public void DrawCleaningCarWheels(Graphics g, Color bodyColor, Color additionalColor, int startPosX, int startPosY) {
Graphics2D g2d = (Graphics2D) g;
for (int i = 0; i < countWheels.getCountWheels(); i++) {
g2d.setColor(bodyColor);
int[] xPoints = {startPosX + 10 + 40 * i, startPosX + 20 + 40 * i, startPosX + 30 + 40 * i };
int[] yPoints = {startPosY + 51, startPosY + 50 + 20, startPosY + 51};
g2d.fillPolygon(xPoints, yPoints, 3);
g2d.drawPolygon(xPoints, yPoints, 3);
g2d.setColor(additionalColor);
int[] xPoints2 = {startPosX + 13 + 40 * i, startPosX + 20 + 40 * i, startPosX + 27 + 40 * i };
int[] yPoints2 = {startPosY + 53, startPosY + 50 + 15, startPosY + 53};
g2d.fillPolygon(xPoints2, yPoints2, 3);
g2d.drawPolygon(xPoints2, yPoints2, 3);
g2d.drawPolyline(xPoints, yPoints, 3);
}
}
}

View File

@ -0,0 +1,157 @@
package Drawnings;
import Entities.*;
import java.awt.*;
import java.util.Random;
public class DrawningTruck {
public 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 IDrawningWheels drawningWheels;
public int GetPosX(){return _startPosX;}
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _drawningCarWidth;}
public int GetHeight(){return _drawningCarHeight;}
protected DrawningTruck() {
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public DrawningTruck(EntityTruck truck, IDrawningWheels drawningWheels) {
EntityTruck = truck;
this.drawningWheels = drawningWheels;
}
protected void SetWheelsType() {
Random random = new Random();
switch (random.nextInt(1, 4)) {
case 1:
drawningWheels = new DrawningCircleWheels();
break;
case 2:
drawningWheels = new DrawningRhombWheels();
break;
case 3:
drawningWheels = new DrawningTriangleWheels();
break;
default:
return;
}
drawningWheels.setCountWheels(random.nextInt(1, 4));
}
public DrawningTruck(int speed, double weight, Color bodyColor) {
this();
EntityTruck = new EntityTruck(speed, weight, bodyColor);
// для дополнительных элементов
SetWheelsType();
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(), Color.BLACK, _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,10 @@
package Drawnings;
import java.awt.*;
public interface IDrawningWheels {
void setCountWheels(int numWheels);
CountWheels getNumberOfWheels();
void DrawCleaningCarWheels(Graphics g, Color bodyColor, Color additionalColor, int startPosX, int startPosY);
}

View File

@ -0,0 +1,27 @@
package Entities;
import java.awt.*;
public class EntityCleaningCar extends EntityTruck {
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
public void setAdditionalColor(Color color) {AdditionalColor = color;}
private boolean Tank;
public boolean getTank() {return Tank;}
public void setTank(boolean tank) {Tank = tank;}
private boolean SweepingBrush;
public boolean getSweepingBrush() {return SweepingBrush;}
public void setSweepingBrush(boolean sweepingBrush) {SweepingBrush = sweepingBrush;}
private boolean FlashLight;
public boolean getFlashLight() {return FlashLight;}
public void setFlashLight(boolean flashLight) {FlashLight = 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,29 @@
package Entities;
import java.awt.*;
public class EntityTruck {
private int Speed;
public int getSpeed() {
return Speed;
}
public void setSpeed(int speed) {Speed = speed;}
private double Weight;
public double getWeight() {
return Weight;
}
public void setWeight(double weight) {Weight = weight;}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
public void setBodyColor(Color color) {BodyColor = color;}
public double Step() {
return Speed * 100 / Weight;
}
public EntityTruck(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormAdditionalCollection">
<grid id="27dc6" binding="defaultPanel" layout-manager="GridLayoutManager" row-count="3" column-count="3" 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="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="6ae24">
<constraints>
<grid row="2" column="1" 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="d0d65" class="javax.swing.JList" binding="listEntity">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
<maximum-size width="-1" height="200"/>
</grid>
</constraints>
<properties/>
</component>
<component id="3d508" class="javax.swing.JList" binding="listWheels">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
<maximum-size width="-1" height="200"/>
</grid>
</constraints>
<properties/>
</component>
<component id="6d472" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="2" 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="-1" height="100"/>
<maximum-size width="200" height="100"/>
</grid>
</constraints>
<properties>
<font name="Roboto Light" size="28" style="1"/>
<text value="Создать"/>
</properties>
</component>
<grid id="d47fe" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" 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="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<vspacer id="ff720">
<constraints>
<grid row="0" column="2" 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>
</form>

View File

@ -0,0 +1,133 @@
import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.AdditionalCollection;
import Drawnings.*;
import Entities.EntityCleaningCar;
import Entities.EntityTruck;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormAdditionalCollection extends JFrame {
private DrawningTruck _drawningTruck;
private IDrawningWheels _wheels;
private AbstractCompany _company;
private JPanel defaultPanel;
private JButton buttonCreate;
private JList<String> listEntity;
private JList<String> listWheels;
private JPanel pictureBox;
private AdditionalCollection<EntityTruck, IDrawningWheels> additionalCollection;
public FormAdditionalCollection() {
setTitle("Дополнительная форма");
setLocation(200, 100);
pack();
setVisible(true);
add(defaultPanel);
setSize(600, 400);
additionalCollection = new AdditionalCollection<>(3, (Class) EntityTruck.class, (Class) IDrawningWheels.class);
Random random = new Random();
addEntities(random);
addWheels(random);
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
_drawningTruck = additionalCollection.CreateFromAdditionalCollection();
_drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight());
_drawningTruck.SetPosition(90, 90);
repaint();
DrawningTruck truck;
repaint();
if (_drawningTruck instanceof DrawningCleaningCar)
truck = new DrawningCleaningCar((EntityCleaningCar) _drawningTruck.getEntityTruck(), _drawningTruck.drawningWheels);
else
truck = new DrawningTruck(_drawningTruck.getEntityTruck(), _drawningTruck.drawningWheels);
_company._collection.Insert(truck);
FormCleaningCarCollection.Show();
String[] data1 = new String[additionalCollection.GetCountEntities()];
for (int i = 0; i < additionalCollection.GetCountEntities(); i++) {
EntityTruck entity = additionalCollection._collectionEntity[i];
data1[i] = ToString(entity);
}
String[] data2 = new String[additionalCollection.GetCountWheels()];
for (int i = 0; i < additionalCollection.GetCountWheels(); i++) {
IDrawningWheels wheels = additionalCollection._collectionWheels[i];
data2[i] = ToString(wheels);
}
listEntity.setListData(data1);
listWheels.setListData(data2);
}
});
}
private String ToString(EntityTruck entity) {
String str = "";
if (entity instanceof EntityCleaningCar) str += "EntityCleaningCar ";
else str += "EntityTruck ";
str += entity.getBodyColor().toString();
return str;
}
private String ToString(IDrawningWheels wheels) {
if (wheels == null || wheels.getNumberOfWheels() == null)
return "Don't have wheels";
String str = "Wheels: ";
if (wheels instanceof DrawningCircleWheels) str += "Circle ";
else if (wheels instanceof DrawningRhombWheels) str += "Rhomb ";
else str += "Triangle ";
str += wheels.getNumberOfWheels().toString();
return str;
}
private void addEntities(Random rnd) {
for (int i = 0; i < additionalCollection.GetCountEntities(); i++) {
EntityTruck entityTruck;
if (rnd.nextBoolean()) {
entityTruck = new EntityTruck(rnd.nextInt(100, 300), rnd.nextInt(1000, 3000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
}
else {
entityTruck = new EntityCleaningCar(rnd.nextInt(100, 300), rnd.nextInt(1000, 3000),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextBoolean(), rnd.nextBoolean(), rnd.nextBoolean());
}
additionalCollection.Insert(entityTruck);
}
}
private void addWheels(Random rnd) {
for (int i = 0; i < additionalCollection.GetCountWheels(); i++) {
int countWheels;
switch (countWheels = rnd.nextInt(1, 4)) {
case 1:
_wheels = new DrawningCircleWheels();
break;
case 2:
_wheels = new DrawningRhombWheels();
break;
case 3:
_wheels = new DrawningTriangleWheels();
break;
default:
_wheels = null;
break;
}
if (_wheels != null) _wheels.setCountWheels(countWheels);
additionalCollection.Insert(_wheels);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_drawningTruck == null) return;
Graphics e = pictureBox.getGraphics();
e.setColor(pictureBox.getBackground());
e.fillRect(0, 0, pictureBox.getWidth(), pictureBox.getHeight());
_drawningTruck.DrawTransport(g);
}
public void SetCompany(AbstractCompany company) {
_company = company;
}
}

View File

@ -0,0 +1,101 @@
<?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="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="714" height="441"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="fb60" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<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"/>
</grid>
</constraints>
<properties>
<enabled value="true"/>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<icon value="Arrow_Right.png"/>
<text value=""/>
</properties>
</component>
<vspacer id="cd599">
<constraints>
<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="1fedd" class="javax.swing.JComboBox" binding="comboBoxStrategy">
<constraints>
<grid row="0" column="6" row-span="1" col-span="2" 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="6" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="100" height="20"/>
</grid>
</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>
<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="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"/>
</grid>
</constraints>
<properties>
<icon value="Arrow_Down.png"/>
<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="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"/>
</grid>
</constraints>
<properties>
<icon value="Arrow_Left.png"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,159 @@
import Drawnings.DirectionType;
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.List;
import java.util.LinkedList;
public class FormCleaningCar extends JFrame {
private DrawningTruck _drawningTruck;
private JPanel pictureBox;
private JButton buttonRight;
private JButton buttonLeft;
private JButton buttonDown;
private JButton buttonUp;
private JComboBox comboBoxStrategy;
private JButton buttonStrategyStep;
private List<JComponent> controls;
private AbstractStrategy _strategy;
@Override
public void paint(Graphics g) {
super.paint(g);
if (_drawningTruck.getEntityTruck() == null ||
pictureBox.getWidth() == 0 || pictureBox.getHeight() == 0)
return;
Graphics e = pictureBox.getGraphics();
e.setColor(pictureBox.getBackground());
e.fillRect(0,0, pictureBox.getWidth(), pictureBox.getHeight());
_drawningTruck.DrawTransport(e);
RepaintControls();
}
public void SetTruck(DrawningTruck truck) {
_drawningTruck = truck;
_drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight());
comboBoxStrategy.setEnabled(true);
_strategy = null;
repaint();
}
public FormCleaningCar() {
setSize(1000, 600);
setLocationRelativeTo(null);
setTitle("Project Cleaning Car");
add(pictureBox);
setVisible(true);
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
comboBoxStrategy.setEnabled(false);
InitializeControlsRepaintList();
ActionListener buttonMoveClickedListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String buttonName = ((JButton) e.getSource()).getName();
boolean result = false;
switch (buttonName) {
case "buttonUp": {
result = _drawningTruck.MoveTransport(DirectionType.Up);
}
break;
case "buttonDown": {
result = _drawningTruck.MoveTransport(DirectionType.Down);
}
break;
case "buttonLeft": {
result = _drawningTruck.MoveTransport(DirectionType.Left);
}
break;
case "buttonRight": {
result = _drawningTruck.MoveTransport(DirectionType.Right);
}
break;
}
if (result)
Draw();
}
};
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 Draw() {
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());
_drawningTruck.DrawTransport(g);
RepaintControls();
}
private void RepaintControls() {
for (JComponent control : controls) {
control.repaint();
}
}
private void InitializeControlsRepaintList() {
controls = new LinkedList<>();
controls.add(buttonUp);
controls.add(buttonDown);
controls.add(buttonLeft);
controls.add(buttonRight);
controls.add(comboBoxStrategy);
controls.add(buttonStrategyStep);
}
}

View File

@ -0,0 +1,195 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormCleaningCarCollection">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="2" 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="981" height="661"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="ed6cf" binding="tools" layout-manager="GridLayoutManager" row-count="4" 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="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none" title="Инструменты:"/>
<children>
<component id="f051c" class="javax.swing.JComboBox" binding="comboBoxSelectorCompany">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<maximum-size width="-1" height="70"/>
</grid>
</constraints>
<properties>
<toolTipText value="" noi18n="true"/>
</properties>
</component>
<grid id="b3851" binding="panelStorage" layout-manager="GridLayoutManager" row-count="6" column-count="2" 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="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="14367" class="javax.swing.JLabel" binding="labelCollectionName">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Название коллекции:"/>
</properties>
</component>
<component id="7a65d" class="javax.swing.JTextField" binding="textBoxCollectionName">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="2293" class="javax.swing.JRadioButton" binding="radioButtonMassive">
<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>
<component id="264bb" class="javax.swing.JRadioButton" binding="radioButtonList">
<constraints>
<grid row="2" column="1" 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>
<component id="65912" class="javax.swing.JButton" binding="buttonCollectionAdd">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить коллекцию"/>
</properties>
</component>
<component id="df305" class="javax.swing.JButton" binding="buttonCollectionDel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить коллекцию"/>
</properties>
</component>
<component id="1ff49" class="javax.swing.JList" binding="listBoxCollection">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
<component id="2b76c" class="javax.swing.JButton" binding="buttonCreateCompany">
<constraints>
<grid row="2" 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>
<grid id="ceabb" binding="panelCompanyTools" layout-manager="GridLayoutManager" row-count="8" 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="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="false"/>
</properties>
<border type="none"/>
<children>
<component id="5a95" class="javax.swing.JButton" binding="buttonAddTruck">
<constraints>
<grid row="0" 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>
<component id="8fa47" class="javax.swing.JFormattedTextField" binding="maskedTextBox">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="50" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="af87b" class="javax.swing.JButton" binding="buttonDel">
<constraints>
<grid row="2" 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>
<component id="7c328" class="javax.swing.JButton" binding="buttonGoToCheck">
<constraints>
<grid row="4" 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>
<component id="44aef" class="javax.swing.JButton" binding="buttonRefresh">
<constraints>
<grid row="5" 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>
<component id="5f2f8" class="javax.swing.JButton" binding="buttonAdditionalCollection">
<constraints>
<grid row="6" 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>
<component id="b5aef" class="javax.swing.JButton" binding="buttonResurrected">
<constraints>
<grid row="7" 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>
<component id="bf6d" class="javax.swing.JButton" binding="buttonIndex">
<constraints>
<grid row="3" 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>
</children>
</grid>
</children>
</grid>
<grid id="abc4c" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" 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="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="700" height="-1"/>
<preferred-size width="310" height="24"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,274 @@
import CollectionGenericObjects.*;
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Random;
import static java.lang.Integer.parseInt;
public class FormCleaningCarCollection extends JFrame{
private AbstractCompany _company = null;
private StorageCollection<DrawningTruck> _storageCollection;
private LinkedList<DrawningTruck> _resurrected;
private JPanel panel;
private JPanel tools;
private JComboBox comboBoxSelectorCompany;
private JButton buttonAddTruck;
private JPanel pictureBox;
private JFormattedTextField maskedTextBox;
private JButton buttonDel;
private JButton buttonGoToCheck;
private JButton buttonRefresh;
private JButton buttonAdditionalCollection;
private JPanel panelStorage;
private JLabel labelCollectionName;
private JTextField textBoxCollectionName;
private JRadioButton radioButtonMassive;
private JRadioButton radioButtonList;
private JButton buttonCollectionAdd;
private JList listBoxCollection;
private JButton buttonCollectionDel;
private JButton buttonCreateCompany;
private JPanel panelCompanyTools;
private JButton buttonResurrected;
private JButton buttonIndex;
public FormCleaningCarCollection() {
setTitle("Коллекция уборочных машин");
setSize(1000, 600);
setLocation(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(panel);
_storageCollection = new StorageCollection<>();
_resurrected = new LinkedList<>();
tools.setBackground(new Color(220, 220, 220));
comboBoxSelectorCompany.addItem(new String(""));
comboBoxSelectorCompany.addItem(new String("Автопарк"));
//mask
MaskFormatter maskFormatter = null;
try {
maskFormatter = new MaskFormatter("##");
maskFormatter.setPlaceholder("00");
} catch (ParseException e) {
throw new RuntimeException(e);
}
MaskFormatter finalMaskFormatter = maskFormatter;
maskedTextBox.setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() {
@Override
public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
return finalMaskFormatter;
}
});
Init();
}
private void Init() {
//ComboBoxSelectorCompany_SelectedIndexChanged
comboBoxSelectorCompany.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panelCompanyTools.setEnabled(false);
for (Component i : panelCompanyTools.getComponents()) {
i.setBackground(Color.GRAY);
}
}
});
//ButtonAddTruck_Click
buttonAddTruck.addActionListener(e -> {
FormTruckConfig form = new FormTruckConfig();
form.setVisible(true);
form.SetCompany(_company);
});
//ButtonDelTruck_Click
buttonDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null || maskedTextBox.getText() == null) return;
switch (JOptionPane.showConfirmDialog(null, "Удалить?", "Удаление", JOptionPane.YES_NO_OPTION)) {
case 0:
int pos = parseInt(maskedTextBox.getText());
DrawningTruck truck = _company._collection.Remove(pos);
if (truck != null) {
_resurrected.addFirst(truck);
JOptionPane.showMessageDialog(null, "Объект удалён");
repaint();
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
break;
case 1:
default:
return;
}
}
});
//ButtonGoToCheck_Click
buttonGoToCheck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null) return;
DrawningTruck truck = _company.GetRandomObject();
int counter = 100;
while (truck == null) {
truck = _company.GetRandomObject();
counter--;
if (counter <= 0) break;
}
if (truck == null) return;
FormCleaningCar formCleaningCar = new FormCleaningCar();
formCleaningCar.SetTruck(truck);
}
});
//ButtonRefresh_Click
buttonRefresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null) return;
repaint();
}
});
buttonAdditionalCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null) {
return;
}
FormAdditionalCollection formAdditionalCollection = new FormAdditionalCollection();
formAdditionalCollection.SetCompany(_company);
}
});
//ButtonCollectionAdd_Click
buttonCollectionAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (textBoxCollectionName.getText().isEmpty() || (!radioButtonMassive.isSelected() && !radioButtonList.isSelected())) {
JOptionPane.showMessageDialog(null, "Не все данные заполнены");
return;
}
CollectionType collectionType = CollectionType.None;
if (radioButtonMassive.isSelected()) {
collectionType = CollectionType.Massive;
} else if (radioButtonList.isSelected()) {
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxCollectionName.getText(), collectionType);
RefreshListBoxItems();
}
});
//ButtonCollectionDel_Click
buttonCollectionDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) {
JOptionPane.showMessageDialog(null, "Коллекция не выбрана");
return;
}
switch (JOptionPane.showConfirmDialog(null, "Удалить коллекцию?", "Удаление", JOptionPane.YES_NO_OPTION)) {
case 1:
return;
case 0:
_storageCollection.DelCollection(listBoxCollection.getSelectedValue().toString());
break;
}
_storageCollection.DelCollection(listBoxCollection.getSelectedValue().toString());
RefreshListBoxItems();
}
});
//ButtonCreateCompany_Click
buttonCreateCompany.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) {
JOptionPane.showMessageDialog(null, "Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawningTruck> collection = _storageCollection.GetCollectionObject(listBoxCollection.getSelectedValue().toString());
if (collection == null) {
JOptionPane.showMessageDialog(null, "Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.getSelectedItem().toString()) {
case "Автопарк":
_company = new AutoParkService(pictureBox.getWidth(), pictureBox.getHeight(), collection);
break;
}
panelCompanyTools.setEnabled(true);
for (Component i : panelCompanyTools.getComponents()) {
i.setBackground(buttonCreateCompany.getBackground());
}
RefreshListBoxItems();
}
});
buttonResurrected.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_resurrected.peek() != null) {
FormCleaningCar form = new FormCleaningCar();
form.SetTruck(_resurrected.pop());
}
}
});
buttonIndex.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxCollection.getSelectedValue() == null || listBoxCollection.getSelectedIndex() < 0
|| _company == null || maskedTextBox.getText() == null) return;
int pos = parseInt(maskedTextBox.getText());
switch (JOptionPane.showConfirmDialog(null, "Удалить?", "Удаление", JOptionPane.YES_NO_OPTION)) {
case 0:
DrawningTruck truck = _storageCollection.Remove(listBoxCollection.getSelectedValue().toString(), pos);
if (truck != null) {
_resurrected.addFirst(truck);
JOptionPane.showMessageDialog(null, "Объект удалён");
repaint();
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
break;
case 1:
default:
return;
}
}
});
}
private void RefreshListBoxItems()
{
listBoxCollection.removeAll();
DefaultListModel<String> list = new DefaultListModel<String>();
for (String name : _storageCollection.Keys()) {
if (!Objects.equals(name, ""))
{
list.addElement(name);
}
}
listBoxCollection.setModel(list);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_company == null) return;
Graphics e = pictureBox.getGraphics();
e.setColor(pictureBox.getBackground());
e.fillRect(0,0, pictureBox.getWidth(), pictureBox.getHeight());
_company.Show(e);
}
public static void Show() {for (Frame i : Frame.getFrames()) {if (!i.isActive()) {i.repaint();}}}
}

View File

@ -0,0 +1,351 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormTruckConfig">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="1" column-count="2" 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="828" height="281"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="50bbc" binding="GroupBoxConfig" layout-manager="GridLayoutManager" row-count="7" column-count="6" 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="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false">
<minimum-size width="600" height="-1"/>
<preferred-size width="600" height="-1"/>
<maximum-size width="600" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none" title="Параметры:"/>
<children>
<component id="9090b" class="javax.swing.JLabel" binding="labelSimpleObject">
<constraints>
<grid row="6" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="2" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="90" height="40"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<text value="Простой"/>
<toolTipText value="simple"/>
<verticalAlignment value="0"/>
</properties>
</component>
<vspacer id="f3150">
<constraints>
<grid row="0" column="3" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="bef18" class="javax.swing.JLabel" binding="labelModifiedObject">
<constraints>
<grid row="6" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="2" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="90" height="40"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="Продвинутый"/>
<toolTipText value="modified"/>
</properties>
</component>
<component id="4ddc3" class="javax.swing.JLabel" binding="labelSpeed">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость:"/>
</properties>
</component>
<component id="e70e0" class="javax.swing.JSpinner" binding="spinnerSpeed">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="20"/>
<preferred-size width="70" height="20"/>
<maximum-size width="70" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="32f" class="javax.swing.JLabel" binding="labelWeight">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес:"/>
</properties>
</component>
<component id="532dd" class="javax.swing.JSpinner" binding="spinnerWeight">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="20"/>
<preferred-size width="70" height="20"/>
<maximum-size width="70" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="77aaa" class="javax.swing.JCheckBox" binding="checkBoxTank">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия бака"/>
</properties>
</component>
<component id="189fd" class="javax.swing.JCheckBox" binding="checkBoxSweepingBrush">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия щётки"/>
</properties>
</component>
<component id="69dcd" class="javax.swing.JCheckBox" binding="checkBoxFlashLight">
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия маячка"/>
</properties>
</component>
<grid id="59a96" binding="groupBoxColors" layout-manager="GridLayoutManager" row-count="2" column-count="4" 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="4" row-span="6" col-span="2" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="300" height="-1"/>
<preferred-size width="300" height="-1"/>
</grid>
</constraints>
<properties/>
<border type="none" title="Цвета"/>
<children>
<grid id="dbd1a" binding="panelRed" layout-manager="GridLayoutManager" row-count="1" 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="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-65536"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="5cf79" binding="panelGreen" layout-manager="GridLayoutManager" row-count="1" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-16711936"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="229b8" binding="panelBlue" layout-manager="GridLayoutManager" row-count="1" 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="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-16776961"/>
<foreground color="-12828863"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="89383" binding="panelYellow" layout-manager="GridLayoutManager" row-count="1" 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="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-5376"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="ed3d3" binding="panelWhite" layout-manager="GridLayoutManager" row-count="1" 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="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-1"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="eca1b" binding="panelGray" layout-manager="GridLayoutManager" row-count="1" 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="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-11316397"/>
<foreground color="-12828863"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="4beef" binding="panelBlack" layout-manager="GridLayoutManager" row-count="1" 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="1" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-16777216"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="c6721" binding="panelPurple" layout-manager="GridLayoutManager" row-count="1" 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="1" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-4910849"/>
</properties>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
<component id="8a3dc" class="javax.swing.JLabel" binding="labelCountWheels">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Число колёс:"/>
</properties>
</component>
<component id="23c65" class="javax.swing.JSpinner" binding="spinnerCountWheels">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="70" height="20"/>
<preferred-size width="20" height="20"/>
<maximum-size width="70" height="20"/>
</grid>
</constraints>
<properties/>
</component>
<component id="ac4ad" class="javax.swing.JLabel" binding="labelTriangleWheels">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="30"/>
</grid>
</constraints>
<properties>
<text value="Треугольник"/>
<toolTipText value="Triangle"/>
</properties>
</component>
<component id="d0d6" class="javax.swing.JLabel" binding="labelCircleWheels">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="2"/>
<text value="Круг"/>
<toolTipText value="Circle"/>
<verticalTextPosition value="0"/>
</properties>
</component>
<component id="7ba83" class="javax.swing.JLabel" binding="labelRhombWheels">
<constraints>
<grid row="6" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="70" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<text value="Ромб"/>
<toolTipText value="Rhomb"/>
</properties>
</component>
</children>
</grid>
<grid id="292f4" binding="panelRight" layout-manager="GridLayoutManager" row-count="3" column-count="2" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="line"/>
<children>
<grid id="193d6" binding="panelObject" layout-manager="GridLayoutManager" row-count="1" 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="1" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="line"/>
<children/>
</grid>
<component id="6a122" class="javax.swing.JButton" binding="buttonAdd">
<constraints>
<grid row="2" 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>
<component id="896cf" class="javax.swing.JButton" binding="buttonCancel">
<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>
<component id="f5ca6" class="javax.swing.JLabel" binding="labelAdditionalColor">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="60" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="Доп. цвет"/>
</properties>
</component>
<component id="b6369" class="javax.swing.JLabel" binding="labelBodyColor">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="60" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="Цвет"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,356 @@
import CollectionGenericObjects.AbstractCompany;
import Drawnings.*;
import Entities.EntityCleaningCar;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
public class FormTruckConfig extends JFrame {
private DrawningTruck _truck;
private AbstractCompany _company;
private JPanel panel;
private JPanel GroupBoxConfig;
private JLabel labelSimpleObject;
private JLabel labelModifiedObject;
private JLabel labelSpeed;
private JSpinner spinnerSpeed;
private JSpinner spinnerWeight;
private JLabel labelWeight;
private JCheckBox checkBoxTank;
private JCheckBox checkBoxSweepingBrush;
private JCheckBox checkBoxFlashLight;
private JPanel groupBoxColors;
private JPanel panelRed;
private JPanel panelGreen;
private JPanel panelBlue;
private JPanel panelYellow;
private JPanel panelWhite;
private JPanel panelGray;
private JPanel panelBlack;
private JPanel panelPurple;
private JPanel panelRight;
private JButton buttonAdd;
private JButton buttonCancel;
private JPanel panelObject;
private JLabel labelBodyColor;
private JLabel labelAdditionalColor;
private JSpinner spinnerCountWheels;
private JLabel labelCountWheels;
private JLabel labelTriangleWheels;
private JLabel labelCircleWheels;
private JLabel labelRhombWheels;
public FormTruckConfig() {
setTitle("Создание объекта");
add(panel);
setMinimumSize(new Dimension(900, 300));
setLocationRelativeTo(null);
buttonCancel.addActionListener(e -> this.dispose());
Init();
}
protected void Init() {
labelSimpleObject.setBorder(new LineBorder(Color.BLACK, 2));
labelModifiedObject.setBorder(new LineBorder(Color.BLACK, 2));
labelCircleWheels.setBorder(new LineBorder(Color.BLACK, 2));
labelRhombWheels.setBorder(new LineBorder(Color.BLACK, 2));
labelTriangleWheels.setBorder(new LineBorder(Color.BLACK, 2));
spinnerSpeed.setModel(new SpinnerNumberModel(100, 1, 1000, 1));
spinnerWeight.setModel(new SpinnerNumberModel(100, 1, 1000, 1));
spinnerCountWheels.setModel(new SpinnerNumberModel(1, 1, 3, 1));
labelBodyColor.setBorder(new LineBorder(Color.BLACK));
labelAdditionalColor.setBorder(new LineBorder(Color.BLACK));
MouseAdapter labelObjectsMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
};
TransferHandler labelObjectsTransferHandler = new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel) c).getText());
}
};
labelSimpleObject.addMouseListener(labelObjectsMouseDown);
labelSimpleObject.setTransferHandler(labelObjectsTransferHandler);
labelModifiedObject.addMouseListener(labelObjectsMouseDown);
labelModifiedObject.setTransferHandler(labelObjectsTransferHandler);
ArrayList<JLabel> IDrawningWheelsLabels = new ArrayList<>();
IDrawningWheelsLabels.add(labelTriangleWheels);
IDrawningWheelsLabels.add(labelCircleWheels);
IDrawningWheelsLabels.add(labelRhombWheels);
for (JLabel i : IDrawningWheelsLabels) {
i.addMouseListener(labelObjectsMouseDown);
i.setTransferHandler(new TransferHandler(){
@Override
public int getSourceActions(JComponent c) {return TransferHandler.COPY;}
@Override
protected Transferable createTransferable(JComponent c) {
IDrawningWheels wheels;
switch (i.getToolTipText()) {
case "Triangle":
wheels = new DrawningTriangleWheels();
break;
case "Circle":
wheels = new DrawningCircleWheels();
break;
case "Rhomb":
wheels = new DrawningRhombWheels();
break;
default: return null;
}
return new WheelsTransferable(wheels);
}
});
}
panelObject.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor)
|| support.isDataFlavorSupported(WheelsTransferable.wheelsDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "Простой":
_truck = new DrawningTruck((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.WHITE);
break;
case "Продвинутый":
_truck = new DrawningCleaningCar((int) spinnerSpeed.getValue(), (int) spinnerWeight.getValue(),
Color.WHITE, Color.BLACK,
checkBoxTank.isSelected(), checkBoxSweepingBrush.isSelected(), checkBoxFlashLight.isSelected());
break;
}
if (_truck != null) {
_truck.SetPictureSize(panelObject.getWidth(), panelObject.getHeight());
_truck.SetPosition(60,30);
_truck.drawningWheels.setCountWheels((int)spinnerCountWheels.getValue());
}
else return false;
}
catch (UnsupportedFlavorException | IOException e) {}
try {
IDrawningWheels wheels =
(IDrawningWheels) support.getTransferable().getTransferData(WheelsTransferable.wheelsDataFlavor);
_truck.drawningWheels = wheels;
_truck.drawningWheels.setCountWheels((int) spinnerCountWheels.getValue());
}catch (UnsupportedFlavorException | IOException e) {}
repaint();
return true;
}
return false;
}
});
ArrayList<JPanel> colorList = new ArrayList<>();
colorList.add(panelWhite);colorList.add(panelBlack);
colorList.add(panelPurple); colorList.add(panelBlue); colorList.add(panelGreen);
colorList.add(panelGray); colorList.add(panelRed); colorList.add(panelYellow);
MouseAdapter colorMouseDown = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
};
TransferHandler colorTransferHandler = new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(((JPanel)c).getBackground());
}
};
for (JPanel color : colorList) {
color.addMouseListener(colorMouseDown);
color.setTransferHandler(colorTransferHandler);
}
labelBodyColor.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_truck == null) return false;
_truck.EntityTruck.setBodyColor(color);
repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
labelAdditionalColor.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
if (!(_truck instanceof DrawningCleaningCar)) return false;
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_truck == null) return false;
if (_truck.EntityTruck instanceof EntityCleaningCar cleaningCar) {
cleaningCar.setAdditionalColor(color);
repaint();
return true;
}
return false;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
});
//SetTruck
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null || _truck == null) {
return;
}
if (_company._collection.Insert(_truck, 0) != -1) {
FormCleaningCarCollection.Show();
JOptionPane.showMessageDialog(null, "Объект добавлен");
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
dispose();
}
});
spinnerCountWheels.addChangeListener(e -> {
if (_truck == null) return;
_truck.drawningWheels.setCountWheels((int)spinnerCountWheels.getValue());
repaint();
});
checkBoxTank.addActionListener(e -> {
if (_truck == null) return;
if (_truck.EntityTruck instanceof EntityCleaningCar) {
((EntityCleaningCar) _truck.EntityTruck).setTank(checkBoxTank.isSelected());
repaint();
}
});
checkBoxSweepingBrush.addActionListener(e -> {
if (_truck == null) return;
if (_truck.EntityTruck instanceof EntityCleaningCar) {
((EntityCleaningCar) _truck.EntityTruck).setSweepingBrush(checkBoxSweepingBrush.isSelected());
repaint();
}
});
checkBoxFlashLight.addActionListener(e -> {
if (_truck == null) return;
if (_truck.EntityTruck instanceof EntityCleaningCar) {
((EntityCleaningCar) _truck.EntityTruck).setFlashLight(checkBoxFlashLight.isSelected());
repaint();
}
});
spinnerWeight.addChangeListener(e -> {
if (_truck == null) return;
_truck.EntityTruck.setSpeed((int)spinnerSpeed.getValue());
});
spinnerWeight.addChangeListener(e -> {
if (_truck == null) return;
_truck.EntityTruck.setWeight((int)spinnerWeight.getValue());
});
}
public void SetCompany(AbstractCompany company) {
_company = company;
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_truck == null) return;
Graphics e = panelObject.getGraphics();
e.setColor(panelObject.getBackground());
e.fillRect(0,0, panelObject.getWidth(), panelObject.getHeight());
_truck.DrawTransport(e);
}
private class ColorTransferable implements Transferable {
private Color color;
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
public ColorTransferable(Color color) {
this.color = color;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{colorDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return colorDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private class WheelsTransferable implements Transferable {
private IDrawningWheels wheels;
private static final DataFlavor wheelsDataFlavor = new DataFlavor(IDrawningWheels.class, "Wheels");
public WheelsTransferable(IDrawningWheels wheels) {
this.wheels = wheels;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{wheelsDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(wheelsDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return wheels;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
}

View File

@ -1,15 +1,7 @@
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
class Main {
public static void main(String[] args) {
FormCleaningCarCollection form = new FormCleaningCarCollection();
form.setVisible(true);
}
}

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.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep()) {
if (diffY > 0) {
MoveUp();
} else {
MoveDown();
}
}
}
}

View File

@ -0,0 +1,41 @@
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
}