diff --git a/ProjectCatamaran/src/CollectionGenericObjects/AbstractCompany.java b/ProjectCatamaran/src/CollectionGenericObjects/AbstractCompany.java new file mode 100644 index 0000000..c5644b0 --- /dev/null +++ b/ProjectCatamaran/src/CollectionGenericObjects/AbstractCompany.java @@ -0,0 +1,61 @@ +package CollectionGenericObjects; + +import Drawnings.DrawningBoat; + +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 final int _pictureWidth; + protected final int _pictureHeight; + protected ICollectionGenericObjects _collection = null; + private int maxCount = getMaxCount(); + public int getMaxCount() { + return _pictureWidth * _pictureHeight / (_placeSizeWidth*_placeSizeHeight); + } + public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects collection) { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.setMaxCount(maxCount); + } + public boolean add(AbstractCompany company ,DrawningBoat boat) { + if (company != null && company._collection != null) { + return company._collection.insert(boat); + } + return false; + } + + public static boolean remove(AbstractCompany company, int position) { + if (company != null && company._collection != null) { + return company._collection.remove(position); + } + return false; + } + public DrawningBoat getRandomObject() { + Random rnd = new Random(); + return _collection.get(rnd.nextInt(getMaxCount())); + } + public BufferedImage show() { + BufferedImage bitmap = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = bitmap.createGraphics(); + + drawBackground(g2d); + + setObjectsPosition(); + for (int i = 0; i < (_collection != null ? _collection.getCount() : 0); ++i) { + DrawningBoat obj = _collection.get(i); + obj.drawBoat(g2d); + } + + g2d.dispose(); + return bitmap; + } + protected abstract void drawBackground(Graphics2D g2d); + protected abstract void setObjectsPosition(); + + +} diff --git a/ProjectCatamaran/src/CollectionGenericObjects/BoatSharingService.java b/ProjectCatamaran/src/CollectionGenericObjects/BoatSharingService.java new file mode 100644 index 0000000..590887f --- /dev/null +++ b/ProjectCatamaran/src/CollectionGenericObjects/BoatSharingService.java @@ -0,0 +1,58 @@ +package CollectionGenericObjects; + +import Drawnings.DrawningBoat; + +import java.awt.*; +import java.util.ArrayList; +import java.util.List; + +public class BoatSharingService extends AbstractCompany { + private List locCoord = new ArrayList<>(); + private int numRows, numCols; + public BoatSharingService(int picWidth, int picHeight, ICollectionGenericObjects collection) { + super(picWidth, picHeight, collection); + } + @Override + protected void drawBackground(Graphics2D g) { + Color backgroundColor = new Color(135, 206, 235); + + g.setColor(backgroundColor); + g.fillRect(0, 0, _pictureWidth, _pictureHeight); + + g.setColor(new Color(165, 42, 42)); // Brown + int offsetX = 10, offsetY = -12; + int x = 1 + offsetX, y = _pictureHeight - _placeSizeHeight + offsetY; + int numRows = 0; + while (y >= 0) { + int numCols = 0; + while (x + _placeSizeWidth <= _pictureWidth) { + numCols++; + g.drawLine(x, y, x + _placeSizeWidth / 2, y); + g.drawLine(x, y, x, y + _placeSizeHeight + 4); + locCoord.add(new Point(x, y)); + x += _placeSizeWidth + 2; + } + numRows++; + x = 1 + offsetX; + y -= _placeSizeHeight + 5 + offsetY; + } + + } + + @Override + protected void setObjectsPosition() { + if (locCoord == null || _collection == null) { + return; + } + int row = numRows - 1, col = numCols; + for (int i=0;i< _collection.getCount(); i++, col--) { + _collection.get(i).setPictureSize(_pictureWidth, _pictureHeight); + _collection.get(i).setPosition((int)locCoord.get(row*numCols - col).getX() + 5, + (int)locCoord.get(row*numCols - col).getY() + 5); + if (col == 1) { + col = numCols + 1; + row--; + } + } + } +} diff --git a/ProjectCatamaran/src/CollectionGenericObjects/ICollectionGenericObjects.java b/ProjectCatamaran/src/CollectionGenericObjects/ICollectionGenericObjects.java new file mode 100644 index 0000000..0100136 --- /dev/null +++ b/ProjectCatamaran/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -0,0 +1,10 @@ +package CollectionGenericObjects; + +public interface ICollectionGenericObjects { + int getCount(); + void setMaxCount(int maxCount); + boolean insert(T obj); + boolean insert(T obj, int position); + boolean remove(int position); + T get(int position); +} diff --git a/ProjectCatamaran/src/CollectionGenericObjects/MassiveGenericObjects.java b/ProjectCatamaran/src/CollectionGenericObjects/MassiveGenericObjects.java new file mode 100644 index 0000000..fcdb365 --- /dev/null +++ b/ProjectCatamaran/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -0,0 +1,79 @@ +package CollectionGenericObjects; + +import java.sql.Array; +import java.util.Arrays; + +public class MassiveGenericObjects implements ICollectionGenericObjects { + private T[] _collection; + public MassiveGenericObjects() { + _collection = (T[]) new Object[getCount()]; + + } + @Override + public int getCount() { + return _collection.length; + } + @Override + public boolean insert(T obj) { + for (int i = 0; i < getCount(); i++) { + if (_collection[i] == null) { + _collection[i] = obj; + return true; + } + } + return false; + } + @Override + public boolean insert(T obj, int position) { + if (position < 0 || position >= getCount()) { + return false; + } + if (_collection[position] == null) { + _collection[position] = obj; + return true; + } + for (int i = position + 1;i < getCount(); i++) { + if (_collection[i] == null) { + _collection[i] = obj; + return true; + } + } + for (int i = position - 1;i >= 0; i--) { + if (_collection[i] == null) { + _collection[i] = obj; + return true; + } + } + return false; + } + + @Override + public boolean remove(int position) { + if (position < 0 || position >= getCount()) { + return false; + } + _collection[position] = null; + return true; + } + + @Override + public T get(int position) { + if (position >= 0 && position < getCount()) { + return _collection[position]; + } + else { + return null; + } + } + + @Override + public void setMaxCount(int value) { + if (value > 0) { + if (_collection.length > 0) { + _collection = Arrays.copyOf(_collection, value); + } else { + _collection = (T[]) new Object[value]; + } + } + } +} diff --git a/ProjectCatamaran/src/Drawnings/DrawningCatamaran.java b/ProjectCatamaran/src/Drawnings/DrawningCatamaran.java index 221a0f3..199546f 100644 --- a/ProjectCatamaran/src/Drawnings/DrawningCatamaran.java +++ b/ProjectCatamaran/src/Drawnings/DrawningCatamaran.java @@ -1,6 +1,4 @@ package Drawnings; - -import Drawnings.DrawningBoat; import Entities.*; import java.awt.*; @@ -12,12 +10,6 @@ public class DrawningCatamaran extends DrawningBoat { EntityBoat = new EntityCatamaran(speed, weight, bodyColor, additionalColor, floaters, sail); } - - - - - - private void drawFloater(int y0, Color additionalColor, Graphics2D g2d) { g2d.setColor(additionalColor); @@ -75,11 +67,6 @@ public class DrawningCatamaran extends DrawningBoat { g2d.drawPolygon(sailPolygon); g2d.setColor(EntityBoat.getAdditionalColor()); g2d.fillPolygon(sailPolygon); - - - - } - } } diff --git a/ProjectCatamaran/src/Drawnings/PaddlesCount.java b/ProjectCatamaran/src/Drawnings/PaddlesCount.java index ae60f0e..f9ff5de 100644 --- a/ProjectCatamaran/src/Drawnings/PaddlesCount.java +++ b/ProjectCatamaran/src/Drawnings/PaddlesCount.java @@ -4,7 +4,6 @@ public enum PaddlesCount { One(1), Two(2), Three(3); - final private int EnumNumber; PaddlesCount(int enumNumber) { EnumNumber = enumNumber; diff --git a/ProjectCatamaran/src/FormBoatCollection.java b/ProjectCatamaran/src/FormBoatCollection.java new file mode 100644 index 0000000..059c67f --- /dev/null +++ b/ProjectCatamaran/src/FormBoatCollection.java @@ -0,0 +1,134 @@ +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.BoatSharingService; +import CollectionGenericObjects.MassiveGenericObjects; +import Drawnings.DrawningBoat; +import Drawnings.DrawningCatamaran; + +import javax.swing.*; +import javax.swing.text.NumberFormatter; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.text.NumberFormat; +import java.util.Random; + +public class FormBoatCollection extends JFrame { + private AbstractCompany _company; + private JPanel pictureBox, instrumentsBox; + private JComboBox comboBoxSelectorCompany; + private JButton buttonAddBoat, buttonAddCatamaran, buttonRemoveBoat, buttonGoToCheck, buttonRefresh; + private JTextField textBoxPosition; + + public FormBoatCollection() { + setTitle("Коллекция лодок"); + setSize(900, 500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + instrumentsBox = new JPanel(); + instrumentsBox.setLayout(new GridLayout(0,1)); + + comboBoxSelectorCompany = new JComboBox<>(); + comboBoxSelectorCompany.addItem("Хранилище"); + comboBoxSelectorCompany.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String selectedItem = Integer.toString(comboBoxSelectorCompany.getSelectedIndex()); + switch (selectedItem) { + case "Хранилище": + _company = new BoatSharingService(pictureBox.getWidth(), pictureBox.getHeight(), + new MassiveGenericObjects()); + break; + + + } + } + }); + instrumentsBox.add(comboBoxSelectorCompany); + + buttonAddBoat = new JButton("Добавить лодку"); + buttonAddBoat.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + createObject("Drawnings.DrawningBoat"); + } + }); + instrumentsBox.add(buttonAddBoat); + + buttonAddCatamaran = new JButton("Добавить катамаран"); + buttonAddCatamaran.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + createObject("Drawnings.DrawningCatamaran"); + } + }); + instrumentsBox.add(buttonAddCatamaran); + + NumberFormat format = NumberFormat.getInstance(); + NumberFormatter formatter = new NumberFormatter(format); + formatter.setValueClass(Integer.class); + formatter.setMinimum(0); + formatter.setMaximum(100); + formatter.setAllowsInvalid(false); + formatter.setCommitsOnValidEdit(true); + + textBoxPosition = new JFormattedTextField(formatter); + textBoxPosition.setPreferredSize(buttonAddBoat.getPreferredSize()); + + instrumentsBox.add(textBoxPosition); + + buttonRemoveBoat = new JButton("Удалить объект"); + buttonRemoveBoat.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (textBoxPosition.getText() == null || textBoxPosition.getText().isEmpty()) { + return; + } + int result = JOptionPane.showConfirmDialog(null, "Удалить объект?", + "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); + if (result == JOptionPane.NO_OPTION) { + return; + } + int pos = Integer.valueOf(textBoxPosition.getText()); + if (_company.remove(_company, pos)) { + JOptionPane.showMessageDialog(null,"Объект удален"); + + } + else { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); + } + + } + }); + instrumentsBox.add(buttonRemoveBoat); + + pictureBox = new JPanel(); + getContentPane().add(instrumentsBox, BorderLayout.EAST); + getContentPane().add(pictureBox); // Добавляем pictureBox в центр + setVisible(true); + } + + private void createObject(String type) { + if (_company == null) { + return; + } + DrawningBoat _drawningBoat; + Random random = new Random(); + switch (type) { + case "Drawnings.DrawningBoat": + _drawningBoat = new DrawningBoat(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3)); + break; + case "Drawnings.DrawningCatamaran": + _drawningBoat = new DrawningCatamaran(random.nextInt(100 - 30) + 30, random.nextInt(500 - 100) + 100, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextInt(3), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + random.nextBoolean(), random.nextBoolean()); + break; + default: + return; + } + if (_company.add(_company, _drawningBoat)) { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + //pictureBox.setIcon(new ImageIcon(_company.show())); + } + else { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } +} diff --git a/ProjectCatamaran/src/Main.java b/ProjectCatamaran/src/Main.java index 843e4bb..76196ab 100644 --- a/ProjectCatamaran/src/Main.java +++ b/ProjectCatamaran/src/Main.java @@ -2,13 +2,14 @@ import javax.swing.*; public class Main { public static void main(String[] args) { - JFrame.setDefaultLookAndFeelDecorated(false); - JFrame frame = new JFrame("Катамаран"); - frame.setContentPane(new FormCatamaran().PanelWrapper); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setLocation(500, 200); - frame.pack(); - frame.setSize(700, 500); - frame.setVisible(true); +// JFrame.setDefaultLookAndFeelDecorated(false); +// JFrame frame = new JFrame("Катамаран"); +// frame.setContentPane(new FormBoatCollection()); +// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); +// frame.setLocation(500, 200); +// frame.pack(); +// frame.setSize(700, 500); +// frame.setVisible(true); + new FormBoatCollection(); } } \ No newline at end of file diff --git a/ProjectCatamaran/src/MovementStrategy/AbstractStrategy.java b/ProjectCatamaran/src/MovementStrategy/AbstractStrategy.java index e6bb51f..17cd800 100644 --- a/ProjectCatamaran/src/MovementStrategy/AbstractStrategy.java +++ b/ProjectCatamaran/src/MovementStrategy/AbstractStrategy.java @@ -7,7 +7,6 @@ public abstract class AbstractStrategy { protected int FieldHeight; public StrategyStatus GetStatus() { return _state; } - // Изменить статус, установить поля public void SetData(IMoveableObject moveableObject, int width, int height) { if (moveableObject == null) @@ -21,7 +20,6 @@ public abstract class AbstractStrategy { FieldHeight = height; } - // сделать шаг public void MakeStep() { if (_state != StrategyStatus.InProgress) @@ -36,13 +34,11 @@ public abstract class AbstractStrategy { 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 int GetStep() @@ -53,13 +49,10 @@ public abstract class AbstractStrategy { } return _moveableObject.GetStep(); } - // перемещение protected abstract void MoveToTarget(); - // достигнута ли цель protected abstract boolean IsTargetDestination(); - // попытка перемещения по направлению private boolean MoveTo(MovementDirection directionType) { if (_state != StrategyStatus.InProgress) diff --git a/ProjectCatamaran/src/MovementStrategy/MoveToBorder.java b/ProjectCatamaran/src/MovementStrategy/MoveToBorder.java index 24ed166..bca1e56 100644 --- a/ProjectCatamaran/src/MovementStrategy/MoveToBorder.java +++ b/ProjectCatamaran/src/MovementStrategy/MoveToBorder.java @@ -20,17 +20,29 @@ public class MoveToBorder extends AbstractStrategy { if (objParams == null) { return; } - - var diffX = FieldWidth - objParams.ObjectMiddleHorizontal(); - if (Math.abs(diffX) > GetStep()) { - - MoveRight(); - + int diffX = objParams.RightBorder() - FieldWidth; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } } - var diffY = FieldHeight - objParams.ObjectMiddleVertical(); - if (Math.abs(diffY) > GetStep()) { - - MoveDown(); + int diffY = objParams.DownBorder() - FieldHeight; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } } } diff --git a/ProjectCatamaran/src/MovementStrategy/MoveableBoat.java b/ProjectCatamaran/src/MovementStrategy/MoveableBoat.java index dfa19ea..73e7d7c 100644 --- a/ProjectCatamaran/src/MovementStrategy/MoveableBoat.java +++ b/ProjectCatamaran/src/MovementStrategy/MoveableBoat.java @@ -1,7 +1,5 @@ package MovementStrategy; import Drawnings.*; -import Entities.EntityBoat; - public class MoveableBoat implements IMoveableObject { private DrawningBoat _boat = null; diff --git a/ProjectCatamaran/src/MovementStrategy/ObjectParameters.java b/ProjectCatamaran/src/MovementStrategy/ObjectParameters.java index a741f5a..9260415 100644 --- a/ProjectCatamaran/src/MovementStrategy/ObjectParameters.java +++ b/ProjectCatamaran/src/MovementStrategy/ObjectParameters.java @@ -11,7 +11,6 @@ public class ObjectParameters { 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; }