From 654d2e36e059b53f8808ed3f1babc8bae15b11d9 Mon Sep 17 00:00:00 2001 From: DjonniStorm Date: Sun, 5 May 2024 00:25:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=963=20(=D1=87=D0=B0=D1=81=D1=82=D1=8C=201)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/OOP_JAVA.iml | 5 +- .../AbstractCompany.java | 48 ++++++ .../AutoParkService.java | 50 ++++++ .../ICollectionGenericObjects.java | 10 ++ .../MassiveGenericObjects.java | 86 ++++++++++ ProjectCleaningCar/src/FormCleaningCar.form | 22 --- ProjectCleaningCar/src/FormCleaningCar.java | 54 ++---- .../src/FormCleaningCarCollection.form | 103 ++++++++++++ .../src/FormCleaningCarCollection.java | 157 ++++++++++++++++++ ProjectCleaningCar/src/Main.java | 20 ++- 10 files changed, 484 insertions(+), 71 deletions(-) create mode 100644 ProjectCleaningCar/src/CollectionGenericObjects/AbstractCompany.java create mode 100644 ProjectCleaningCar/src/CollectionGenericObjects/AutoParkService.java create mode 100644 ProjectCleaningCar/src/CollectionGenericObjects/ICollectionGenericObjects.java create mode 100644 ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java create mode 100644 ProjectCleaningCar/src/FormCleaningCarCollection.form create mode 100644 ProjectCleaningCar/src/FormCleaningCarCollection.java diff --git a/.idea/OOP_JAVA.iml b/.idea/OOP_JAVA.iml index d6ebd48..cfddbf8 100644 --- a/.idea/OOP_JAVA.iml +++ b/.idea/OOP_JAVA.iml @@ -2,7 +2,10 @@ - + + + + diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/AbstractCompany.java b/ProjectCleaningCar/src/CollectionGenericObjects/AbstractCompany.java new file mode 100644 index 0000000..bbe1090 --- /dev/null +++ b/ProjectCleaningCar/src/CollectionGenericObjects/AbstractCompany.java @@ -0,0 +1,48 @@ +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 _collection = null; + private int GetMaxCount() { + return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); + }; + + public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects collection) { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount(GetMaxCount()); + } + public DrawningTruck GetRandomObject() { + Random rnd = new Random(); + return _collection.Get(rnd.nextInt(GetMaxCount())); + } + + public JPanel Show(JPanel panel) { + BufferedImage bitmap = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics2D g = bitmap.createGraphics(); + panel.paint(g); + DrawBackground(g); + SetObjectsPosition(); + for (int i = 0; i < _collection.Count(); i++) { + DrawningTruck obj = _collection.Get(i); + if (obj != null) { + obj.DrawTransport(g); + } + } + return panel; + } + + protected abstract void DrawBackground(Graphics g); + protected abstract void SetObjectsPosition(); +} \ No newline at end of file diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/AutoParkService.java b/ProjectCleaningCar/src/CollectionGenericObjects/AutoParkService.java new file mode 100644 index 0000000..2c6dc1b --- /dev/null +++ b/ProjectCleaningCar/src/CollectionGenericObjects/AutoParkService.java @@ -0,0 +1,50 @@ +package CollectionGenericObjects; + +import Drawnings.DrawningTruck; + +import java.awt.*; + +public class AutoParkService extends AbstractCompany { + public AutoParkService(int picWidth, int picHeight, ICollectionGenericObjects collection) { + super(picWidth, picHeight, collection); + } + @Override + protected void DrawBackground(Graphics g) { +// Pen pen = new Pen(Color.Black, 4f); + g.setColor(Color.black); + 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); + } + } + } + + @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++; + } + } + } +} diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/ICollectionGenericObjects.java b/ProjectCleaningCar/src/CollectionGenericObjects/ICollectionGenericObjects.java new file mode 100644 index 0000000..7078d5b --- /dev/null +++ b/ProjectCleaningCar/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -0,0 +1,10 @@ +package CollectionGenericObjects; + +public interface ICollectionGenericObjects { + int Count(); + void SetMaxCount(int value); + int Insert(T obj); + int Insert(T obj, int position); + T Remove(int position); + T Get(int position); +} diff --git a/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java b/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java new file mode 100644 index 0000000..e7160b5 --- /dev/null +++ b/ProjectCleaningCar/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -0,0 +1,86 @@ +package CollectionGenericObjects; + +import java.lang.reflect.Array; +import java.util.ArrayList; + +public class MassiveGenericObjects implements ICollectionGenericObjects{ + private ArrayList _collection; + @Override + public int Count() { + return _collection.size(); + } + + @Override + public void SetMaxCount(int value) { + if (value > 0) { + if (!_collection.isEmpty()) { + ArrayList tmp = new ArrayList<>(value); + tmp.addAll(0, _collection); + _collection = tmp; + for (int i = 0; ; i++) { + _collection.add(null); + } + } else { + _collection = new ArrayList<>(value); + for (int i = 0; i < value; i++) { + _collection.add(null); + } + } + } + } + + public MassiveGenericObjects() { + _collection = new ArrayList<>(); + } + + @Override + public int Insert(T obj) { + for (int i = 0; i < Count(); i++) { + if (_collection.get(i) == null) { + _collection.add(i, obj); + return i; + } + } + return -1; + } + + @Override + public int Insert(T obj, int position) { + if (position >= Count() || position < 0) return -1; + if (_collection.get(position) == null) { + _collection.add(position, obj); + return position; + } + int tmp = position + 1; + while (tmp < Count()) { + if (_collection.get(tmp) == null) { + _collection.add(tmp, obj); + return tmp; + } + ++tmp; + } + tmp = position - 1; + while (tmp >= 0) { + if (_collection.get(tmp) == null) { + _collection.add(tmp, obj); + return tmp; + } + --tmp; + } + return -1; + } + + @Override + public T Remove(int position) { + if (position >= Count() || position < 0) return null; + T myObject = _collection.get(position); + _collection.add(position, null); + return myObject; + } + + @Override + public T Get(int position) { + if (position < 0 || position >= Count()) return null; + return _collection.get(position); + } +} diff --git a/ProjectCleaningCar/src/FormCleaningCar.form b/ProjectCleaningCar/src/FormCleaningCar.form index 67d9a46..bc93552 100644 --- a/ProjectCleaningCar/src/FormCleaningCar.form +++ b/ProjectCleaningCar/src/FormCleaningCar.form @@ -29,17 +29,6 @@ - - - - - - - - - - - @@ -61,17 +50,6 @@ - - - - - - - - - - - diff --git a/ProjectCleaningCar/src/FormCleaningCar.java b/ProjectCleaningCar/src/FormCleaningCar.java index ce14f50..1548679 100644 --- a/ProjectCleaningCar/src/FormCleaningCar.java +++ b/ProjectCleaningCar/src/FormCleaningCar.java @@ -13,38 +13,37 @@ import java.util.LinkedList; public class FormCleaningCar extends JFrame { protected DrawningTruck _drawningTruck; - JPanel pictureBox; - private JButton buttonCreateCleaningCar; + private JPanel pictureBox; private JButton buttonRight; private JButton buttonLeft; private JButton buttonDown; private JButton buttonUp; - private JButton buttonCreateTruck; private JComboBox comboBoxStrategy; private JButton buttonStrategyStep; private List controls; private AbstractStrategy _strategy; + public void SetTruck(DrawningTruck truck) { + _drawningTruck = truck; + _drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight()); + comboBoxStrategy.setEnabled(true); + _strategy = null; + Draw(); + } public FormCleaningCar() { + setSize(1000, 600); + setLocationRelativeTo(null); + setTitle("Project Cleaning Car"); + add(pictureBox); + setVisible(true); + pictureBox.setSize(getWidth(), getHeight()); buttonUp.setName("buttonUp"); buttonDown.setName("buttonDown"); buttonLeft.setName("buttonLeft"); buttonRight.setName("buttonRight"); comboBoxStrategy.setEnabled(false); InitializeControlsRepaintList(); - buttonCreateCleaningCar.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - CreateObject("DrawningCleaningCar"); - } - }); - buttonCreateTruck.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - CreateObject("DrawningTruck"); - } - }); ActionListener buttonMoveClickedListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -120,28 +119,6 @@ public class FormCleaningCar extends JFrame { comboBoxStrategy.addItem("К центру"); comboBoxStrategy.addItem("К краю"); } - private void CreateObject(String type) { - Random random = new Random(); - switch (type) { - case "DrawningTruck": - _drawningTruck = new DrawningTruck(random.nextInt(100, 300), random.nextDouble(100, 3000), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); - break; - case "DrawningCleaningCar": - _drawningTruck = new DrawningCleaningCar(random.nextInt(100, 300), random.nextDouble(100, 3000), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); - break; - default: return; - } - _drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight()); - _drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(0, 100)); - - _strategy = null; - comboBoxStrategy.setEnabled(true); - Draw(); - } private void Draw() { if (_drawningTruck.getEntityTruck() == null || pictureBox.getWidth() == 0 || pictureBox.getHeight() == 0) @@ -150,7 +127,6 @@ public class FormCleaningCar extends JFrame { g.setColor(pictureBox.getBackground()); g.fillRect(0,0, pictureBox.getWidth(), pictureBox.getHeight()); _drawningTruck.DrawTransport(g); - RepaintControls(); } @@ -161,8 +137,6 @@ public class FormCleaningCar extends JFrame { } private void InitializeControlsRepaintList() { controls = new LinkedList<>(); - controls.add(buttonCreateCleaningCar); - controls.add(buttonCreateTruck); controls.add(buttonUp); controls.add(buttonDown); diff --git a/ProjectCleaningCar/src/FormCleaningCarCollection.form b/ProjectCleaningCar/src/FormCleaningCarCollection.form new file mode 100644 index 0000000..0c760ca --- /dev/null +++ b/ProjectCleaningCar/src/FormCleaningCarCollection.form @@ -0,0 +1,103 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectCleaningCar/src/FormCleaningCarCollection.java b/ProjectCleaningCar/src/FormCleaningCarCollection.java new file mode 100644 index 0000000..51f124a --- /dev/null +++ b/ProjectCleaningCar/src/FormCleaningCarCollection.java @@ -0,0 +1,157 @@ +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.AutoParkService; +import CollectionGenericObjects.MassiveGenericObjects; +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.awt.image.BufferedImage; +import java.text.ParseException; +import java.util.Random; + +import static java.lang.Integer.parseInt; + +public class FormCleaningCarCollection extends JFrame{ + private AbstractCompany _company = null; + private JPanel panel; + private JPanel tools; + private JLabel toolsNameLabel; + private JComboBox comboBoxSelectorCompany; + private JButton buttonAddTruck; + private JButton buttonAddCleaningCar; + private JPanel pictureBox; + private JFormattedTextField maskedTextBox; + private JButton buttonDel; + private JButton buttonGoToCheck; + private JButton buttonRefresh; + public FormCleaningCarCollection() { + setTitle("Коллекция уборочных машин"); + setSize(1000, 600); + setLocation(200, 100); + setDefaultCloseOperation(EXIT_ON_CLOSE); + add(panel); + + + + 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"); + //это intellij + } 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) { + switch (comboBoxSelectorCompany.getSelectedItem().toString()) { + case "Автопарк": + _company = new AutoParkService(pictureBox.getWidth(), pictureBox.getHeight(), new MassiveGenericObjects()); + System.out.println(pictureBox + "/" + pictureBox.getWidth() + "/" + pictureBox.getHeight()); + + break; + } + System.out.println(comboBoxSelectorCompany.getSelectedItem().toString()); + } + }); + + //ButtonAddTruck_Click + buttonAddTruck.addActionListener(e -> CreateObject("DrawningTruck")); + //ButtonAddCleaningCar_Click + buttonAddCleaningCar.addActionListener(e -> CreateObject("DrawningCleaningCar")); + //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()); + if (_company._collection.Remove(pos) != null) { + JOptionPane.showMessageDialog(null, "Объект удалён"); + + + } else { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); + } + break; + case 1: + 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; + + } + }); + } + private void CreateObject(String type) { + if (_company == null) return; + Random random = new Random(); + DrawningTruck drawningTruck; + switch (type) { + case "DrawningTruck": + drawningTruck = new DrawningTruck(random.nextInt(100, 300), random.nextDouble(100, 3000), + GetColor(random)); + break; + case "DrawningCleaningCar": + drawningTruck = new DrawningCleaningCar(random.nextInt(100, 300), random.nextDouble(100, 3000), + GetColor(random), GetColor(random), + random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); + break; + default: return; + } + if (_company._collection.Insert(drawningTruck, 0) != -1) { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + pictureBox = _company.Show(pictureBox); + repaint(); + } else { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + private Color GetColor(Random random) { + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color colorChooser = JColorChooser.showDialog(this, "Выберите цвет", color); + return colorChooser; + } +} diff --git a/ProjectCleaningCar/src/Main.java b/ProjectCleaningCar/src/Main.java index 76b742d..66d8817 100644 --- a/ProjectCleaningCar/src/Main.java +++ b/ProjectCleaningCar/src/Main.java @@ -1,13 +1,17 @@ import javax.swing.*; +import java.awt.*; + class Main { public static void main(String[] args) { - JFrame.setDefaultLookAndFeelDecorated(false); - JFrame frame = new JFrame("Project Cleaning Car"); - frame.setContentPane(new FormCleaningCar().pictureBox); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setLocation(200, 200); - frame.pack(); - frame.setSize(1000, 600); - frame.setVisible(true); +// JFrame.setDefaultLookAndFeelDecorated(false); +// JFrame frame = new JFrame("Project Cleaning Car"); +// frame.setContentPane(new FormCleaningCarCollection().pictureBox); +// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); +// frame.setLocation(200, 200); +// frame.pack(); +// frame.setSize(1000, 600); +// frame.setVisible(true); + FormCleaningCarCollection form = new FormCleaningCarCollection(); + form.setVisible(true); } } \ No newline at end of file