From fd07441ba6fb40c9f6f5b79b79eb398fb11302d2 Mon Sep 17 00:00:00 2001 From: russell Date: Fri, 1 Dec 2023 17:39:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=203?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawingObjects/DrawingDumpTruck.java | 9 ++ .../DrawingObjects/DrawingTruck.java | 18 +++- src/DumpTruck/FrameDop.java | 22 +++++ src/DumpTruck/FrameDumpTruck.java | 2 +- src/DumpTruck/FrameTruckCollection.java | 17 ++++ src/DumpTruck/Generics/GenericDop.java | 61 ++++++++++++ src/DumpTruck/Generics/SetGeneric.java | 54 +++++++++++ .../Generics/TrucksGenericCollection.java | 70 ++++++++++++++ src/DumpTruck/Main.java | 2 +- src/DumpTruck/PictureBoxCollection.java | 94 +++++++++++++++++++ src/DumpTruck/PictureBoxDop.java | 50 ++++++++++ src/DumpTruck/PictureBoxDumpTruck.java | 50 +++++++--- 12 files changed, 431 insertions(+), 18 deletions(-) create mode 100644 src/DumpTruck/FrameDop.java create mode 100644 src/DumpTruck/FrameTruckCollection.java create mode 100644 src/DumpTruck/Generics/GenericDop.java create mode 100644 src/DumpTruck/Generics/SetGeneric.java create mode 100644 src/DumpTruck/Generics/TrucksGenericCollection.java create mode 100644 src/DumpTruck/PictureBoxCollection.java create mode 100644 src/DumpTruck/PictureBoxDop.java diff --git a/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java b/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java index c5cc009..854b028 100644 --- a/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java +++ b/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java @@ -1,5 +1,6 @@ package DumpTruck.DrawingObjects; import DumpTruck.Entities.*; +import DumpTruck.Wheels.IDrawingWheels; import java.awt.*; @@ -12,6 +13,14 @@ public class DrawingDumpTruck extends DrawingTruck { setEntityTruck(new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor)); } } + public DrawingDumpTruck(EntityDumpTruck entityDumpTruck, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels) + { + super(entityDumpTruck, width, height, wheelNumber, iDrawingWheels); + if (getEntityTruck() != null) + { + setEntityTruck(entityDumpTruck); + } + } @Override public void DrawTransport(Graphics2D g2D) { diff --git a/src/DumpTruck/DrawingObjects/DrawingTruck.java b/src/DumpTruck/DrawingObjects/DrawingTruck.java index aadd45f..efe6923 100644 --- a/src/DumpTruck/DrawingObjects/DrawingTruck.java +++ b/src/DumpTruck/DrawingObjects/DrawingTruck.java @@ -5,6 +5,7 @@ import java.util.Random; import DumpTruck.Entities.*; import DumpTruck.DirectionType; +import DumpTruck.MovementStrategy.*; import DumpTruck.Wheels.*; public class DrawingTruck { @@ -42,8 +43,11 @@ public class DrawingTruck { public int getHeight() { return _truckHeight; } + public IMoveableObject GetMoveableObject() { + return new DrawingObjectTruck(this); + } - public DrawingTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber) + public DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int wheelNumber) { if (height < _truckHeight || width < _truckWidth) { return; @@ -78,6 +82,18 @@ public class DrawingTruck { }; drawingWheels.setWheelNumber(wheelNumber); } + public DrawingTruck(EntityTruck entityTruck, int width, int height, int wheelNumber, IDrawingWheels iDrawingWheels) + { + if (height < _truckHeight || width < _truckWidth) { + return; + } + _pictureWidth = width; + _pictureHeight = height; + this.entityTruck = entityTruck; + Random random = new Random(); + drawingWheels = iDrawingWheels; + drawingWheels.setWheelNumber(wheelNumber); + } public void SetPosition(int x, int y) { diff --git a/src/DumpTruck/FrameDop.java b/src/DumpTruck/FrameDop.java new file mode 100644 index 0000000..ef7ad2e --- /dev/null +++ b/src/DumpTruck/FrameDop.java @@ -0,0 +1,22 @@ +package DumpTruck; + +import DumpTruck.Entities.*; +import DumpTruck.Generics.*; +import DumpTruck.Wheels.*; + +import javax.swing.*; +import java.awt.*; + +public class FrameDop extends JFrame { + + private PictureBoxDop pictureBoxDop; + + public FrameDop() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + pictureBoxDop = new PictureBoxDop(); + add(pictureBoxDop); + pack(); + setLocationRelativeTo(null); + setVisible(true); + } +} \ No newline at end of file diff --git a/src/DumpTruck/FrameDumpTruck.java b/src/DumpTruck/FrameDumpTruck.java index e7f69d6..ba61745 100644 --- a/src/DumpTruck/FrameDumpTruck.java +++ b/src/DumpTruck/FrameDumpTruck.java @@ -4,7 +4,7 @@ import javax.swing.*; public class FrameDumpTruck extends JFrame { - private PictureBoxDumpTruck pictureBoxDumpTruck; + public PictureBoxDumpTruck pictureBoxDumpTruck; public FrameDumpTruck() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); diff --git a/src/DumpTruck/FrameTruckCollection.java b/src/DumpTruck/FrameTruckCollection.java new file mode 100644 index 0000000..c8f1ceb --- /dev/null +++ b/src/DumpTruck/FrameTruckCollection.java @@ -0,0 +1,17 @@ +package DumpTruck; + +import javax.swing.*; + +public class FrameTruckCollection extends JFrame { + + private PictureBoxCollection pictureBoxCollection; + + public FrameTruckCollection() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + pictureBoxCollection = new PictureBoxCollection(); + add(pictureBoxCollection); + pack(); + setLocationRelativeTo(null); + setVisible(true); + } +} \ No newline at end of file diff --git a/src/DumpTruck/Generics/GenericDop.java b/src/DumpTruck/Generics/GenericDop.java new file mode 100644 index 0000000..cb3116c --- /dev/null +++ b/src/DumpTruck/Generics/GenericDop.java @@ -0,0 +1,61 @@ +package DumpTruck.Generics; + +import DumpTruck.Entities.*; +import DumpTruck.Wheels.*; +import DumpTruck.DrawingObjects.*; + +import java.util.Random; + +public class GenericDop { + + private T[] Trucks; + private U[] Wheels; + private int maxTruckAmount; + private int truckAmount; + private int maxWheelAmount ; + private int wheelAmount; + private Random random; + private int _pictureWidth; + private int _pictureHeight; + public GenericDop(int maxTruckAmount, int maxWheelAmount, int pictureWidth, int pictureHeight){ + this.maxTruckAmount = maxTruckAmount; + this.maxWheelAmount = maxWheelAmount; + Trucks = (T[]) new EntityTruck[maxTruckAmount]; + Wheels = (U[]) new IDrawingWheels[maxWheelAmount]; + truckAmount = 0; + wheelAmount = 0; + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + random = new Random(); + } + + public boolean add(T truck){ + if (truck == null || truckAmount > maxTruckAmount) + return false; + Trucks[truckAmount] = truck; + truckAmount++; + return true; + } + + public boolean add(U wheel){ + if (wheel == null || wheelAmount > maxWheelAmount) + return false; + Wheels[wheelAmount] = wheel; + wheelAmount++; + return true; + } + + public DrawingTruck DrawingTruckDop(){ + if (truckAmount == 0 || wheelAmount == 0) + return null; + T truck = Trucks[random.nextInt(truckAmount)]; + DrawingTruck drawingTruck; + if (truck instanceof EntityDumpTruck){ + drawingTruck = new DrawingDumpTruck((EntityDumpTruck)truck, _pictureWidth, _pictureHeight, random.nextInt(2, 5), Wheels[random.nextInt(wheelAmount)]); + } + else{ + drawingTruck = new DrawingTruck(truck, _pictureWidth, _pictureHeight, random.nextInt(2, 5), Wheels[random.nextInt(wheelAmount)]); + } + return drawingTruck; + } +} \ No newline at end of file diff --git a/src/DumpTruck/Generics/SetGeneric.java b/src/DumpTruck/Generics/SetGeneric.java new file mode 100644 index 0000000..aeeaeb2 --- /dev/null +++ b/src/DumpTruck/Generics/SetGeneric.java @@ -0,0 +1,54 @@ +package DumpTruck.Generics; + +public class SetGeneric { + private Object[] _places; + + public int Count() {return _places.length;} + + public SetGeneric(int count) { + _places = new Object[count]; + } + + public int Insert(T truck) { + return Insert(truck, 0); + } + + public int Insert(T truck, int position) { + if (position < 0 || position >= Count()) + return -1; + if (_places[position] == null) + { + _places[position] = truck; + return position; + } + int index = -1; + for (int i = position; i < Count(); i++) + { + if (_places[i] == null) + { + index = i; + break; + } + } + if (index < 0) return -1; + for (int i = index; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = truck; + return position; + } + + public boolean Remove(int position) { + if (position < 0 || position >= Count()) + return false; + _places[position] = null; + return true; + } + + public T Get(int position) { + if (position < 0 || position >= Count()) + return null; + return (T) _places[position]; + } +} \ No newline at end of file diff --git a/src/DumpTruck/Generics/TrucksGenericCollection.java b/src/DumpTruck/Generics/TrucksGenericCollection.java new file mode 100644 index 0000000..228fbec --- /dev/null +++ b/src/DumpTruck/Generics/TrucksGenericCollection.java @@ -0,0 +1,70 @@ +package DumpTruck.Generics; + +import DumpTruck.DrawingObjects.*; +import DumpTruck.MovementStrategy.*; + +import java.awt.*; +import java.awt.image.BufferedImage; + +public class TrucksGenericCollection{ + private int _pictureWidth; + private int _pictureHeight; + + private int _placeSizeWidth = 180; + private int _placeSizeHeight = 100; + + private SetGeneric _collection; + public TrucksGenericCollection(int pictureWidth, int pictureHeight) { + int width = pictureWidth / _placeSizeWidth; + int height = pictureHeight / _placeSizeHeight; + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + _collection = new SetGeneric(width * height); + } + public int Add(T obj) { + if (obj == null) { + return -1; + } + return _collection.Insert(obj); + } + public boolean remove(int pos) { + T obj = _collection.Get(pos); + if (obj != null) { + _collection.Remove(pos); + } + return false; + } + public U GetU(int pos) { + return (U) _collection.Get(pos).GetMoveableObject(); + } + public BufferedImage ShowTrucks() { + BufferedImage bitmap = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = bitmap.createGraphics(); + DrawBackground(g); + DrawObjects(g); + g.dispose(); + return bitmap; + } + private void DrawBackground(Graphics g) { + g.setColor(Color.BLACK); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + private void DrawObjects(Graphics g) { + for (int i = 0; i < _collection.Count(); i++) + { + T obj = _collection.Get(i); + if (obj != null) + { + obj.SetPosition(i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, i / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); + obj.DrawTransport((Graphics2D) g); + } + } + } +} \ No newline at end of file diff --git a/src/DumpTruck/Main.java b/src/DumpTruck/Main.java index be46a71..e5297f8 100644 --- a/src/DumpTruck/Main.java +++ b/src/DumpTruck/Main.java @@ -2,6 +2,6 @@ package DumpTruck; public class Main { public static void main(String[] args) { - new FrameDumpTruck(); + new FrameTruckCollection(); } } \ No newline at end of file diff --git a/src/DumpTruck/PictureBoxCollection.java b/src/DumpTruck/PictureBoxCollection.java new file mode 100644 index 0000000..f7b7251 --- /dev/null +++ b/src/DumpTruck/PictureBoxCollection.java @@ -0,0 +1,94 @@ +package DumpTruck; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.util.Random; +import DumpTruck.DrawingObjects.*; +import DumpTruck.Generics.*; +import DumpTruck.MovementStrategy.*; + +public class PictureBoxCollection extends JPanel { + public TrucksGenericCollection _trucks; + private JLabel labelTools; + private JButton buttonAddTruck, buttonDeleteTruck, buttonRefreshCollection, buttonShowDop; + private JTextField textFieldNumber; + public PictureBoxCollection() { + setLayout(null); + setBounds(0, 0, 800, 450); + _trucks = new TrucksGenericCollection<>(this.getWidth() - 200, this.getHeight()); + labelTools = new JLabel("Инструменты"); + labelTools.setBounds(660, 10, 150, 30); + add(labelTools); + buttonAddTruck = new JButton("Добавить грузовик"); + buttonAddTruck.setFocusable(false); + buttonAddTruck.setBounds(620, 50, 150, 30); + buttonAddTruck.addActionListener(e -> { + FrameDumpTruck frameDumpTruck = new FrameDumpTruck(); + frameDumpTruck.pictureBoxDumpTruck.buttonSelectTruck.addActionListener(e1 -> { + if (_trucks.Add(frameDumpTruck.pictureBoxDumpTruck.drawingTruck) != -1) { + JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + frameDumpTruck.dispose(); + repaint(); + } + ); + }); + add(buttonAddTruck); + + textFieldNumber = new JTextField(); + textFieldNumber.setBounds(620, 100, 150, 30); + add(textFieldNumber); + + buttonDeleteTruck = new JButton("Удалить грузовик"); + buttonDeleteTruck.setFocusable(false); + buttonDeleteTruck.setBounds(620, 150, 150, 30); + buttonDeleteTruck.addActionListener(e -> { + if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { + return; + } + for (char it : textFieldNumber.getText().toCharArray()) + if (it < '0' || it > '9') { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + if (textFieldNumber.getText().length() == 0) { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + + int pos = Integer.parseInt(textFieldNumber.getText()); + if (_trucks.remove(pos)) { + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + repaint(); + }); + add(buttonDeleteTruck); + + buttonRefreshCollection = new JButton("Обновить коллекцию"); + buttonRefreshCollection.setFocusable(false); + buttonRefreshCollection.setBounds(620, 200, 150, 30); + buttonRefreshCollection.addActionListener(e -> repaint()); + add(buttonRefreshCollection); + + buttonShowDop = new JButton("Показать доп"); + buttonShowDop.setFocusable(false); + buttonShowDop.setBounds(620, 250, 150, 30); + buttonShowDop.addActionListener(e -> new FrameDop()); + add(buttonShowDop); + + setPreferredSize(new Dimension(800, 450)); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + g2d.drawImage(_trucks.ShowTrucks(), 0, 0, null); + } +} diff --git a/src/DumpTruck/PictureBoxDop.java b/src/DumpTruck/PictureBoxDop.java new file mode 100644 index 0000000..50b184e --- /dev/null +++ b/src/DumpTruck/PictureBoxDop.java @@ -0,0 +1,50 @@ +package DumpTruck; + +import DumpTruck.DrawingObjects.DrawingTruck; +import DumpTruck.Entities.*; +import DumpTruck.Generics.*; +import DumpTruck.Wheels.*; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class PictureBoxDop extends JPanel { + GenericDop genericDop; + private JButton buttonCreate; + public DrawingTruck drawingTruck; + + public PictureBoxDop(){ + setLayout(null); + setBounds(0, 0, 800, 450); + genericDop = new GenericDop<>(100, 100, this.getWidth(), this.getHeight()); + genericDop.add(new EntityTruck(100, 100, Color.BLUE)); + genericDop.add(new EntityTruck(100, 100, Color.RED)); + genericDop.add(new EntityTruck(100, 100, Color.GREEN)); + genericDop.add(new EntityDumpTruck(100, 100, Color.BLUE, true, true, Color.BLACK, Color.RED)); + genericDop.add(new EntityDumpTruck(100, 100, Color.GREEN, true, true, Color.MAGENTA, Color.YELLOW)); + genericDop.add(new DrawingWheels()); + genericDop.add(new DrawingWheelsCircles()); + genericDop.add(new DrawingWheelsStar()); + + buttonCreate = new JButton("Создать грузовик"); + buttonCreate.setFocusable(false); + buttonCreate.setBounds(12, 415, 150, 30); + buttonCreate.addActionListener(e -> { + drawingTruck = genericDop.DrawingTruckDop(); + repaint(); + }); + add(buttonCreate); + setPreferredSize(new Dimension(800, 450)); + } + @Override + protected void paintComponent(Graphics g) { + if (drawingTruck == null) { + return; + } + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + drawingTruck.SetPosition(100, 100); + drawingTruck.DrawTransport(g2d); + } +} diff --git a/src/DumpTruck/PictureBoxDumpTruck.java b/src/DumpTruck/PictureBoxDumpTruck.java index 1ef2a15..8a94cc1 100644 --- a/src/DumpTruck/PictureBoxDumpTruck.java +++ b/src/DumpTruck/PictureBoxDumpTruck.java @@ -8,7 +8,7 @@ import DumpTruck.DrawingObjects.*; import DumpTruck.MovementStrategy.*; public class PictureBoxDumpTruck extends JPanel { - private DrawingTruck drawingTruck; + public DrawingTruck drawingTruck; private AbstractStrategy abstractStrategy; @@ -24,6 +24,7 @@ public class PictureBoxDumpTruck extends JPanel { private JButton buttonCreateTruck; private JComboBox comboBoxStrategy; private JButton buttonStep; + public JButton buttonSelectTruck; public PictureBoxDumpTruck() { setLayout(null); @@ -35,15 +36,15 @@ public class PictureBoxDumpTruck extends JPanel { buttonCreateTruck.addActionListener(e -> { Random random = new Random(); + Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + bodyColor = selectedColor; + } drawingTruck = new DrawingTruck((random.nextInt(200, 300)), random.nextInt(1000, 3000), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), - random.nextInt(0, 256)), - random.nextBoolean(), random.nextBoolean(), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), - random.nextInt(0, 256)), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), - random.nextInt(0, 256)), + bodyColor, this.getWidth(), this.getHeight(), random.nextInt(2, 5)); drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); repaint(); @@ -56,20 +57,39 @@ public class PictureBoxDumpTruck extends JPanel { buttonCreateDumpTruck.addActionListener(e -> { Random random = new Random(); + Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + bodyColor = selectedColor; + } + Color dumpBoxColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + dumpBoxColor = selectedColor; + } + Color tentColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE); + if (selectedColor != null) + { + tentColor = selectedColor; + } drawingTruck = new DrawingDumpTruck((random.nextInt(200, 300)), random.nextInt(1000, 3000), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), - random.nextInt(0, 256)), - random.nextBoolean(), random.nextBoolean(), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), - random.nextInt(0, 256)), - new Color(random.nextInt(0, 256), random.nextInt(0, 256), - random.nextInt(0, 256)), + bodyColor, + random.nextBoolean(), true, + tentColor, dumpBoxColor, this.getWidth(), this.getHeight(), random.nextInt(2, 5)); drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); repaint(); }); + buttonSelectTruck = new JButton("Выбрать"); + buttonSelectTruck.setFocusable(false); + buttonSelectTruck.setBounds(350, 415, 150, 30); + add(buttonSelectTruck); + ActionListener buttonMoveListener = e -> { if (drawingTruck == null) { -- 2.25.1