From 1924451a6378f7d34a4b0803ee3d426757123aa8 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 5 Dec 2023 20:49:29 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=203=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B7=D0=B0=20=D0=BD=D0=B0=20=D1=8F=D0=B2=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningObjects/DrawningLocomotive.java | 6 +- .../src/Forms/FormElectricLocomotive.java | 45 ++++-- .../src/Forms/FormLocomotiveCollection.java | 132 ++++++++++++++++++ .../LocomotivesGenericCollection.java | 79 +++++++++++ .../src/Generics/SetGeneric.java | 50 +++++++ ProjectElectricLocomotive/src/Main.java | 3 +- 6 files changed, 301 insertions(+), 14 deletions(-) create mode 100644 ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java create mode 100644 ProjectElectricLocomotive/src/Generics/LocomotivesGenericCollection.java create mode 100644 ProjectElectricLocomotive/src/Generics/SetGeneric.java diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java index 204954a..ef2816a 100644 --- a/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java @@ -2,6 +2,7 @@ package DrawningObjects; import Entities.EntityLocomotive; import MovementStrategy.DirectionType; +import MovementStrategy.IMoveableObject; import java.awt.*; @@ -96,8 +97,6 @@ public class DrawningLocomotive { if (entityLocomotive == null) { return; } - - g.clearRect(0, 0, _pictureWidth, _pictureHeight); // корпус g.setColor(entityLocomotive.BodyColor); @@ -136,6 +135,9 @@ public class DrawningLocomotive { _pictureWidth = width; _pictureHeight = height; } + public IMoveableObject GetMoveableObject(){ + return (IMoveableObject) this; + } public boolean CanMove(DirectionType direction) { if (entityLocomotive == null) { diff --git a/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java b/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java index afb49a1..3956cf9 100644 --- a/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java @@ -15,6 +15,7 @@ public class FormElectricLocomotive extends JFrame { private Canvas canvas; private JButton buttonCreateElectricLocomotive; private JButton buttonCreateLocomotive; + public JButton buttonSelectLocomotive; private JButton buttonUp; private JButton buttonRight; private JButton buttonDown; @@ -28,7 +29,12 @@ public class FormElectricLocomotive extends JFrame { { return; } - _drawningLocomotive.DrawTransport(canvas.getGraphics()); + Graphics g = canvas.getGraphics(); + g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); + _drawningLocomotive.DrawTransport(g); + } + public DrawningLocomotive GetSelectedLocomotive(){ + return _drawningLocomotive; } private void InitializeComponent(){ buttonCreateElectricLocomotive = new JButton("Создать Электропоезд"); @@ -37,6 +43,9 @@ public class FormElectricLocomotive extends JFrame { buttonCreateLocomotive = new JButton("Создать локомотив"); buttonCreateLocomotive.setMargin(new Insets(0, 0, 0, 0)); + buttonSelectLocomotive = new JButton("Выбор"); + buttonSelectLocomotive.setMargin(new Insets(0, 0, 0, 0)); + buttonStep = new JButton("шаг"); buttonStep.setName("шаг"); @@ -85,12 +94,14 @@ public class FormElectricLocomotive extends JFrame { setSize(1000, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setTitle("Рисование локомотивов"); setLayout(null); canvas = new Canvas(); canvas.setBounds(0, 0, 980, 560); buttonCreateElectricLocomotive.setBounds(10, 470, 200, 40); buttonCreateLocomotive.setBounds(10, 520, 200, 40); + buttonSelectLocomotive.setBounds(770, 520, 200, 40); buttonUp.setBounds(50, 380, 40, 40); buttonDown.setBounds(50, 420, 40, 40); buttonRight.setBounds(90, 420, 40, 40); @@ -100,9 +111,9 @@ public class FormElectricLocomotive extends JFrame { comboBoxStrategy.setBounds(770, 10, 200, 30); comboBoxWheels.setBounds(220, 520, 250, 40); - add(buttonCreateElectricLocomotive); add(buttonCreateLocomotive); + add(buttonSelectLocomotive); add(buttonUp); add(buttonDown); add(buttonRight); @@ -127,14 +138,22 @@ public class FormElectricLocomotive extends JFrame { _drawWheels.setWheelsCount(countWheels); System.out.println(e.getActionCommand()); Random random = new Random(); + Color baseColor = JColorChooser.showDialog(null,"Основной цвет", Color.RED); + if (baseColor == null){ + baseColor = new Color(random.nextInt(0, 256), + random.nextInt(0, 256), + random.nextInt(0, 256)); + } + Color addColor = JColorChooser.showDialog(null,"Дополнительный цвет", Color.RED); + if (addColor == null){ + addColor = new Color(random.nextInt(0, 256), + random.nextInt(0, 256), + random.nextInt(0, 256)); + } _drawningLocomotive = new DrawningElectricLocomotive( random.nextInt(100, 300), random.nextInt(1000, 3000), - 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)), + baseColor, + addColor, comboBoxWheels.getSelectedIndex(), countWheels, random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1, canvas.getWidth(), canvas.getHeight() @@ -156,11 +175,15 @@ public class FormElectricLocomotive extends JFrame { } System.out.println(e.getActionCommand()); Random random = new Random(); + Color baseColor = JColorChooser.showDialog(null,"Основной цвет", Color.RED); + if (baseColor == null){ + baseColor = new Color(random.nextInt(0, 256), + random.nextInt(0, 256), + random.nextInt(0, 256)); + } _drawningLocomotive = new DrawningLocomotive( random.nextInt(100, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(0, 256), - random.nextInt(0, 256), - random.nextInt(0, 256)), + baseColor, comboBoxWheels.getSelectedIndex(), countWheels, canvas.getWidth(), canvas.getHeight() ); diff --git a/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java new file mode 100644 index 0000000..62f7177 --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java @@ -0,0 +1,132 @@ +package Forms; + +import DrawningObjects.DrawningLocomotive; +import Generics.LocomotivesGenericCollection; +import MovementStrategy.DrawningObjectLocomotive; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class FormLocomotiveCollection extends JFrame { + private final LocomotivesGenericCollection _locomotives; + private Canvas canvas; + private JButton buttonAddLocomotive; + private JButton buttonRemoveLocomotive; + private JButton buttonRefreshCollection; + private JTextField textBoxNumber; + private JPanel buttonBox; + + public void repaint(){ + + if (_locomotives == null) + return; + Graphics g = canvas.getGraphics(); + g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); + canvas.setBackground(getBackground()); + g.drawImage(_locomotives.ShowLocomotives(), 0, 0, canvas); + } + private void InitializeComponent(){ + canvas = new Canvas(); + + + buttonAddLocomotive = new JButton("Добавить локомотив"); + buttonAddLocomotive.setMargin(new Insets(0, 0, 0, 0)); + + textBoxNumber = new JTextField(); + + buttonRemoveLocomotive = new JButton("Удалить локомотив"); + buttonRemoveLocomotive.setMargin(new Insets(0, 0, 0, 0)); + + buttonRefreshCollection = new JButton("Обновить колекцию"); + buttonRefreshCollection.setMargin(new Insets(0, 0, 0, 0)); + + buttonBox = new JPanel(new GridLayout(0, 1, 0, 10)); + JLabel labelBB = new JLabel("Инструменты"); + + buttonBox.add(labelBB); + buttonBox.add(buttonAddLocomotive); + buttonBox.add(textBoxNumber); + buttonBox.add(buttonRemoveLocomotive); + buttonBox.add(buttonRefreshCollection); + + buttonBox.setBounds(760, 3, 200, 220); + canvas.setBounds(10, 3, 740, 700); + + setSize(1000, 700); + setTitle("Колекция локомотивов"); + setLayout(null); + + add(buttonBox); + add(canvas); + } + void InitializeLogic(){ + buttonAddLocomotive.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + FormElectricLocomotive form = new FormElectricLocomotive(); + form.setVisible(true); + form.buttonSelectLocomotive.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (_locomotives.Add(form.GetSelectedLocomotive()) != -1){ + JOptionPane.showMessageDialog(null, + "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Объект добавлен"); + repaint(); + } + else { + JOptionPane.showMessageDialog(null, + "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось добавить объект"); + } + form.dispose(); + } + } + ); + } + }); + buttonRemoveLocomotive.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (JOptionPane.showConfirmDialog(null, + "Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) + { + int pos = 0; + try { + pos = Integer.parseInt(textBoxNumber.getText()); + } + catch (Exception ex){ + System.out.println(ex.toString()); + pos = 0; + } + if (_locomotives.Sub(pos) != null) + { + JOptionPane.showMessageDialog(null, + "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + repaint(); + } + else + { + JOptionPane.showMessageDialog(null, + "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + } + repaint(); + } + }); + buttonRefreshCollection.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + repaint(); + } + }); + } + public FormLocomotiveCollection(){ + InitializeComponent(); + _locomotives = new LocomotivesGenericCollection<>(canvas.getWidth(), canvas.getHeight()); + InitializeLogic(); + setVisible(true); + } +} diff --git a/ProjectElectricLocomotive/src/Generics/LocomotivesGenericCollection.java b/ProjectElectricLocomotive/src/Generics/LocomotivesGenericCollection.java new file mode 100644 index 0000000..b20a6a6 --- /dev/null +++ b/ProjectElectricLocomotive/src/Generics/LocomotivesGenericCollection.java @@ -0,0 +1,79 @@ +package Generics; + +import DrawningObjects.DrawningLocomotive; +import MovementStrategy.IMoveableObject; + +import java.awt.*; +import java.awt.image.BufferedImage; + +public class LocomotivesGenericCollection { + private int _pictureWidth; + private int _pictureHeight; + private final int _placeSizeWidth = 210; + private final int _placeSizeHeight = 90; + private SetGeneric _collection; + public LocomotivesGenericCollection(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; + } + obj.SetPictureSize(_pictureWidth, _pictureHeight); + if (_collection.Insert(obj)) { + return 1; + } + return -1; + } + public T Sub(int pos){ + T obj = _collection.Get(pos); + _collection.Remove(pos); + return obj; + } + public U GetU(int pos){ + return (U) (_collection.Get(pos)).GetMoveableObject(); + } + public Image ShowLocomotives() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics g = bmp.getGraphics(); + DrawBackground(g); + DrawObjects(g); + return bmp; + } + private void DrawBackground(Graphics g){ + g.setColor(Color.BLACK); + g.setFont(new Font("myFont", Font.PLAIN, 3)); + for (int i = 0; i < _pictureHeight / _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) { + int ColumnCount = _pictureWidth / _placeSizeWidth; + int RowsCount = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count(); ++i) { + DrawningLocomotive drawningLocomotive = _collection.Get(i); + if (drawningLocomotive == null) { + continue; + } + drawningLocomotive.SetPosition( + (i % ColumnCount) * _placeSizeWidth, + ((RowsCount - (i / ColumnCount) - 1) * _placeSizeHeight)); + drawningLocomotive.DrawTransport(g); + } + } +} diff --git a/ProjectElectricLocomotive/src/Generics/SetGeneric.java b/ProjectElectricLocomotive/src/Generics/SetGeneric.java new file mode 100644 index 0000000..de5521b --- /dev/null +++ b/ProjectElectricLocomotive/src/Generics/SetGeneric.java @@ -0,0 +1,50 @@ +package Generics; + +import DrawningObjects.DrawningLocomotive; + +public class SetGeneric { + private T[] _places; + public int Count(){ + return _places.length; + } + public SetGeneric(int count){ + _places = (T[])new DrawningLocomotive[count]; + } + boolean Insert(T obj, int position){ + if (position >= _places.length || position < 0){ + return false; + } + int fillLineEnd = _places.length; + for (int i = position; i < _places.length; ++i){ + if (_places[i] == null){ + fillLineEnd = i; + break; + } + } + if (fillLineEnd == _places.length){ + return false; + } + for (int i = fillLineEnd; i > position; --i){ + _places[i] = _places[i - 1]; + } + _places[fillLineEnd] = obj; + return true; + } + boolean Insert(T obj){ + return Insert(obj, 0); + } + public boolean Remove(int position){ + if (position >= _places.length || position < 0) + { + return false; + } + _places[position] = null; + return true; + } + public T Get(int position){ + if (position >= _places.length || position < 0){ + return null; + } + return _places[position]; + } +} diff --git a/ProjectElectricLocomotive/src/Main.java b/ProjectElectricLocomotive/src/Main.java index e47628f..6c31352 100644 --- a/ProjectElectricLocomotive/src/Main.java +++ b/ProjectElectricLocomotive/src/Main.java @@ -2,6 +2,7 @@ import Forms.*; public class Main { public static void main(String[] args) { - FormElectricLocomotive form = new FormElectricLocomotive(); + + FormLocomotiveCollection form = new FormLocomotiveCollection(); } } \ No newline at end of file -- 2.25.1 From 5b1b1f22846a48555568213d89e2c1b6445e7a57 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 12 Dec 2023 15:21:05 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D1=91?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningElectricLocomotive.java | 12 +-- .../DrawningObjects/DrawningLocomotive.java | 6 ++ .../Entities/EntityElectricLocomotive.java | 1 + .../src/Forms/FormGenerateLocomotiveDop.form | 26 ++++++ .../src/Forms/FormGenerateLocomotiveDop.java | 79 +++++++++++++++++++ .../src/Forms/FormLocomotiveCollection.java | 19 +++++ .../src/Forms/FrameGenerateLocomotiveDop.java | 17 ++++ .../DopLocomotiveGenericCollection.java | 45 +++++++++++ .../src/Generics/SetGeneric.java | 2 +- 9 files changed, 201 insertions(+), 6 deletions(-) create mode 100644 ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.form create mode 100644 ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.java create mode 100644 ProjectElectricLocomotive/src/Forms/FrameGenerateLocomotiveDop.java create mode 100644 ProjectElectricLocomotive/src/Generics/DopLocomotiveGenericCollection.java diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java index a4f4099..a76d4f4 100644 --- a/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java @@ -10,12 +10,14 @@ public class DrawningElectricLocomotive extends DrawningLocomotive { Color bodyColor, Color additionalColor, int typeWheels, int countWheels, boolean horns, boolean battery, - int width, int height){ + int width, int height) { super(speed, weight, bodyColor, typeWheels, countWheels, width, height, 120, 70); - if (entityLocomotive != null){ - entityLocomotive = new EntityElectricLocomotive( - speed, weight, bodyColor, additionalColor, horns, battery); - } + entityLocomotive = new EntityElectricLocomotive( + speed, weight, bodyColor, additionalColor, horns, battery); + + } + public DrawningElectricLocomotive(EntityLocomotive entity, IDrawWheels drawingWheels, int width, int height) { + super(entity, drawingWheels, width, height); } @Override public void DrawTransport(Graphics g) { diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java index ef2816a..65f23ff 100644 --- a/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java @@ -85,6 +85,12 @@ public class DrawningLocomotive { entityLocomotive = new EntityLocomotive(speed, weight, bodyColor); } + public DrawningLocomotive(EntityLocomotive entity, IDrawWheels drawWheels, int width, int height) { + entityLocomotive = entity; + wheels = drawWheels; + _pictureWidth = width; + _pictureHeight = height; + } public void SetPosition(int x, int y) { x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth); diff --git a/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java index 2646f71..bfb889d 100644 --- a/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java @@ -13,6 +13,7 @@ public class EntityElectricLocomotive extends EntityLocomotive { Color bodyColor, Color additionalColor, boolean horns, boolean battery) { super(speed, weight, bodyColor); + AdditionalColor = additionalColor; Horns = horns; Battery = battery; } diff --git a/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.form b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.form new file mode 100644 index 0000000..084ae48 --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.form @@ -0,0 +1,26 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.java b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.java new file mode 100644 index 0000000..1f263be --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.java @@ -0,0 +1,79 @@ +package Forms; + +import DrawningObjects.*; +import Entities.EntityElectricLocomotive; +import Entities.EntityLocomotive; +import Generics.DopLocomotiveGenericCollection; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.util.Random; + +public class FormGenerateLocomotiveDop extends JFrame { + private Random _random; + private DrawningLocomotive _drawingLocomotive; + private DopLocomotiveGenericCollection _genericDop; + private JButton generateLocomotiveButton; + private JPanel PictureBox; + + public JPanel getPictureBox() { + return PictureBox; + } + public FormGenerateLocomotiveDop(){ + _random = new Random(); + _genericDop = new DopLocomotiveGenericCollection<>(); + PictureBox.setPreferredSize(new Dimension(400, 400)); + generateLocomotiveButton.addActionListener(this::generateLocomotive); + } + + private void addRandomEntity(){ + EntityLocomotive entityLocomotive; + if (_random.nextBoolean()){ + entityLocomotive = new EntityLocomotive( + _random.nextInt(100, 300), + _random.nextInt(1000, 3000), + new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)) + ); + } else { + entityLocomotive = new EntityElectricLocomotive( + _random.nextInt(100, 300), + _random.nextInt(1000, 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() + ); + } + _genericDop.add(entityLocomotive); + } + private void addRandomWheels(){ + int choice = _random.nextInt(3); + IDrawWheels drawWheels; + if (choice == 0){ + drawWheels = new DrawningWheels(); + } else if (choice == 1){ + drawWheels = new DrawningWheelsStar(); + } else { + drawWheels = new DrawningWheelsPolygon(); + } + drawWheels.setWheelsCount(_random.nextInt(2, 4)); + _genericDop.add(drawWheels); + } + private void generateLocomotive(ActionEvent e){ + addRandomEntity(); + addRandomWheels(); + _drawingLocomotive = _genericDop.GenerateLocomotive(PictureBox.getWidth(), PictureBox.getHeight()); + _drawingLocomotive.SetPosition((PictureBox.getWidth() - _drawingLocomotive.GetWidth()) / 2, + (PictureBox.getHeight() - _drawingLocomotive.GetHeight()) / 2); + Draw(); + } + public void Draw() { + if (_drawingLocomotive == null) { + return; + } + Graphics g = PictureBox.getGraphics(); + PictureBox.paint(g); + _drawingLocomotive.DrawTransport(g); + } +} diff --git a/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java index 62f7177..d185463 100644 --- a/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java +++ b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java @@ -12,9 +12,11 @@ import java.awt.event.ActionListener; public class FormLocomotiveCollection extends JFrame { private final LocomotivesGenericCollection _locomotives; private Canvas canvas; + private FrameGenerateLocomotiveDop _formGenerateLocomotiveDop; private JButton buttonAddLocomotive; private JButton buttonRemoveLocomotive; private JButton buttonRefreshCollection; + private JButton buttonOpenGenerateWindow; private JTextField textBoxNumber; private JPanel buttonBox; @@ -42,6 +44,9 @@ public class FormLocomotiveCollection extends JFrame { buttonRefreshCollection = new JButton("Обновить колекцию"); buttonRefreshCollection.setMargin(new Insets(0, 0, 0, 0)); + buttonOpenGenerateWindow = new JButton("Открыть окно генерации"); + buttonOpenGenerateWindow.setMargin(new Insets(0, 0, 0, 0)); + buttonBox = new JPanel(new GridLayout(0, 1, 0, 10)); JLabel labelBB = new JLabel("Инструменты"); @@ -51,14 +56,17 @@ public class FormLocomotiveCollection extends JFrame { buttonBox.add(buttonRemoveLocomotive); buttonBox.add(buttonRefreshCollection); + buttonBox.setBounds(760, 3, 200, 220); canvas.setBounds(10, 3, 740, 700); + buttonOpenGenerateWindow.setBounds(760, 600, 200, 40); setSize(1000, 700); setTitle("Колекция локомотивов"); setLayout(null); add(buttonBox); + add(buttonOpenGenerateWindow); add(canvas); } void InitializeLogic(){ @@ -122,6 +130,17 @@ public class FormLocomotiveCollection extends JFrame { repaint(); } }); + + buttonOpenGenerateWindow.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_formGenerateLocomotiveDop != null) { + _formGenerateLocomotiveDop.dispose(); + } + _formGenerateLocomotiveDop = new FrameGenerateLocomotiveDop(); + _formGenerateLocomotiveDop.setVisible(true); + } + }); } public FormLocomotiveCollection(){ InitializeComponent(); diff --git a/ProjectElectricLocomotive/src/Forms/FrameGenerateLocomotiveDop.java b/ProjectElectricLocomotive/src/Forms/FrameGenerateLocomotiveDop.java new file mode 100644 index 0000000..6792b87 --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FrameGenerateLocomotiveDop.java @@ -0,0 +1,17 @@ +package Forms; + +import javax.swing.*; + +public class FrameGenerateLocomotiveDop extends JFrame { + public FormGenerateLocomotiveDop formGenerateLocomotiveDop; + public FrameGenerateLocomotiveDop(){ + super(); + setTitle("Генерация локомотивов"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + formGenerateLocomotiveDop = new FormGenerateLocomotiveDop(); + setContentPane(formGenerateLocomotiveDop.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(300, 100); + pack(); + } +} diff --git a/ProjectElectricLocomotive/src/Generics/DopLocomotiveGenericCollection.java b/ProjectElectricLocomotive/src/Generics/DopLocomotiveGenericCollection.java new file mode 100644 index 0000000..5f92817 --- /dev/null +++ b/ProjectElectricLocomotive/src/Generics/DopLocomotiveGenericCollection.java @@ -0,0 +1,45 @@ +package Generics; + +import DrawningObjects.DrawningElectricLocomotive; +import DrawningObjects.DrawningLocomotive; +import DrawningObjects.IDrawWheels; +import Entities.EntityElectricLocomotive; +import Entities.EntityLocomotive; + +import java.util.ArrayList; +import java.util.Random; + +public class DopLocomotiveGenericCollection { + private ArrayList _entitiesStorage; + private ArrayList _drawWheelsStorage; + + public DopLocomotiveGenericCollection(){ + _entitiesStorage = new ArrayList<>(); + _drawWheelsStorage = new ArrayList<>(); + } + public void add(T obj){ + _entitiesStorage.add(obj); + } + public void add(U obj){ + _drawWheelsStorage.add(obj); + } + + public DrawningLocomotive GenerateLocomotive(int pictureWidth, int pictureHeight) { + Random random = new Random(); + if (_entitiesStorage.isEmpty()) { + return null; + } + T entityLocomotive = _entitiesStorage.get(random.nextInt(_entitiesStorage.size())); + U drawingWheels = null; + if (!_drawWheelsStorage.isEmpty()) { + drawingWheels = _drawWheelsStorage.get(random.nextInt(_drawWheelsStorage.size())); + } + DrawningLocomotive drawingLocomotive; + if (entityLocomotive instanceof EntityElectricLocomotive) { + drawingLocomotive = new DrawningElectricLocomotive((EntityLocomotive) entityLocomotive, drawingWheels, pictureWidth, pictureHeight); + } else { + drawingLocomotive = new DrawningLocomotive(entityLocomotive, drawingWheels, pictureWidth, pictureHeight); + } + return drawingLocomotive; + } +} diff --git a/ProjectElectricLocomotive/src/Generics/SetGeneric.java b/ProjectElectricLocomotive/src/Generics/SetGeneric.java index de5521b..5d5748a 100644 --- a/ProjectElectricLocomotive/src/Generics/SetGeneric.java +++ b/ProjectElectricLocomotive/src/Generics/SetGeneric.java @@ -27,7 +27,7 @@ public class SetGeneric { for (int i = fillLineEnd; i > position; --i){ _places[i] = _places[i - 1]; } - _places[fillLineEnd] = obj; + _places[position] = obj; return true; } boolean Insert(T obj){ -- 2.25.1