From 45096484946ecdcd69fc1eed04abee4ef87c20a8 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 21 Oct 2022 16:04:51 +0400 Subject: [PATCH 01/13] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D0=B5=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20SetLocomo?= =?UTF-8?q?tivesGeneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SetLocomotivesGeneric.java | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 SetLocomotivesGeneric.java diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java new file mode 100644 index 0000000..b74c1e7 --- /dev/null +++ b/SetLocomotivesGeneric.java @@ -0,0 +1,57 @@ +public class SetLocomotivesGeneric +{ + private final T[] _places; + + public int Count() { + return _places.length; + } + + public SetLocomotivesGeneric(int count) { + _places = (T[]) new Object[count]; + } + + public boolean Insert (T locomotive) { + return Insert(locomotive, 0); + } + + public boolean Insert (T locomotive, int position) { + if (position >= _places.length || position < 0) return false; + if (_places[position] == null) { + _places[position] = locomotive; + return true; + } + + int emptyEl = -1; + for (int i = position + 1; i < Count(); i++) + { + if (_places[i] == null) emptyEl = i; + break; + } + if (emptyEl == -1) + { + return false; + } + for (int i = emptyEl; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = locomotive; + return true; + } + + 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]; + } + +} From 95edf9e7d23684bbf836899c5fce4e0cb8867d14 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 21 Oct 2022 16:24:45 +0400 Subject: [PATCH 02/13] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D0=B5=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20MapWithSe?= =?UTF-8?q?tLocomotivesGeneric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MapWithSetLocomotivesGeneric.java | 142 ++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 MapWithSetLocomotivesGeneric.java diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java new file mode 100644 index 0000000..3559e35 --- /dev/null +++ b/MapWithSetLocomotivesGeneric.java @@ -0,0 +1,142 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetLocomotivesGeneric + +{ + /// Ширина окна отрисовки + private final int _pictureWidth; + /// Высота окна отрисовки + private final int _pictureHeight; + /// Размер занимаемого объектом места (ширина) + private final int _placeSizeWidth = 210; + /// Размер занимаемого объектом места (высота) + private final int _placeSizeHeight = 90; + + /// Набор объектов + private final SetLocomotivesGeneric _setLocomotives; + /// Карта + private final U _map; + /// Конструктор + public MapWithSetLocomotivesGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setLocomotives = new SetLocomotivesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + /// Добавление + public boolean Plus(MapWithSetLocomotivesGeneric map, T locomotive) + { + return map._setLocomotives.Insert(locomotive); + } + /// Удаление + public boolean Minus(MapWithSetLocomotivesGeneric map, int position) + { + return map._setLocomotives.Remove(position); + } + + /// Вывод всего набора объектов + public BufferedImage ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + DrawBackground(gr); + DrawLocomotives(gr); + return bmp; + } + + /// Просмотр объекта на карте + public BufferedImage ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setLocomotives.Count(); i++) + { + var locomotive = _setLocomotives.Get(i); + if (locomotive != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + /// Перемещение объекта по крате + public BufferedImage MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + private void Shaking() + { + int j = _setLocomotives.Count() - 1; + for (int i = 0; i < _setLocomotives.Count(); i++) + { + if (_setLocomotives.Get(i) == null) + { + for (; j > i; j--) + { + var locomotive = _setLocomotives.Get(j); + if (locomotive != null) + { + _setLocomotives.Insert(locomotive, i); + _setLocomotives.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + /// Метод отрисовки фона + 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 DrawLocomotives(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = 0; + int curHeight = 0; + + for (int i = 0; i < _setLocomotives.Count(); i++) + { + // установка позиции + if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth, curHeight * _placeSizeHeight, _pictureWidth, _pictureHeight); + if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).DrawningObject(g); + if (curWidth < width) curWidth++; + else + { + curWidth = 0; + curHeight++; + } + } + } + +} From 896285b4c5ce1a5037b28bb290fbcf67dfdb569b Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 21 Oct 2022 16:38:26 +0400 Subject: [PATCH 03/13] =?UTF-8?q?=D0=9A=D0=BD=D0=BE=D0=BF=D0=BA=D0=B0=20Se?= =?UTF-8?q?lect=20=D0=B8=20=D1=81=D0=B2=D0=BE=D0=B9=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=BE=20SelectedLocomotive=20=D0=B2=20FormLocomotive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 2 +- FormLocomotive.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 5a4e57b..e89236f 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -1,7 +1,7 @@ import java.awt.*; import java.util.Random; -class DrawningLocomotive { +public class DrawningLocomotive { public EntityLocomotive Locomotive; public ExtraWheelsDraw extraWheelsDraw; public IDrawningExtra drawningExtra; diff --git a/FormLocomotive.java b/FormLocomotive.java index 7535926..9adefb3 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -5,6 +5,10 @@ import java.util.Random; public class FormLocomotive extends JComponent{ private DrawningLocomotive _locomotive; + private DrawningLocomotive SelectedLocomotive; + public DrawningLocomotive getSelectedLocomotive() { + return SelectedLocomotive; + } public FormLocomotive() { JFrame formFrame = new JFrame("Locomotive"); formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -62,8 +66,15 @@ public class FormLocomotive extends JComponent{ repaint(); }); + JButton selectLocomotiveButton = new JButton("Select"); + selectLocomotiveButton.addActionListener(e -> { + SelectedLocomotive = _locomotive; + JOptionPane.showMessageDialog(formFrame, "OK"); + }); + statusPanel.add(createButton); statusPanel.add(modifiedButton); + statusPanel.add(selectLocomotiveButton); statusPanel.add(speedLabel); statusPanel.add(weightLabel); statusPanel.add(colorLabel); From d91e28ae4a27c21e9687a9b363d0101bb864d572 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 21 Oct 2022 21:27:07 +0400 Subject: [PATCH 04/13] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D0=B5=D0=BD=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=20For?= =?UTF-8?q?mMapWithSetLocomotives,=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B5=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormLocomotive.java | 33 +---- FormMapWithSetLocomotives.java | 209 ++++++++++++++++++++++++++++++ MapWithSetLocomotivesGeneric.java | 62 ++++++--- SetLocomotivesGeneric.java | 7 +- 4 files changed, 265 insertions(+), 46 deletions(-) create mode 100644 FormMapWithSetLocomotives.java diff --git a/FormLocomotive.java b/FormLocomotive.java index 9adefb3..7eca30a 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -1,6 +1,5 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.*; import java.util.Random; public class FormLocomotive extends JComponent{ @@ -9,26 +8,7 @@ public class FormLocomotive extends JComponent{ public DrawningLocomotive getSelectedLocomotive() { return SelectedLocomotive; } - public FormLocomotive() { - JFrame formFrame = new JFrame("Locomotive"); - formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - formFrame.setSize(800, 500); - formFrame.setVisible(true); - formFrame.setLocationRelativeTo(null); - - formFrame.addComponentListener(new ComponentListener() { - @Override - public void componentResized(ComponentEvent e) { - if (_locomotive != null) _locomotive.ChangeBorders(formFrame.getWidth(), formFrame.getHeight()); - repaint(); - } - @Override - public void componentMoved(ComponentEvent e) {} - @Override - public void componentShown(ComponentEvent e) {} - @Override - public void componentHidden(ComponentEvent e) {} - }); + public FormLocomotive(JDialog caller) { Panel statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); @@ -44,7 +24,7 @@ public class FormLocomotive extends JComponent{ createButton.addActionListener(e -> { Random rnd = new Random(); _locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); - _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75); + _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-75); speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() ); @@ -59,7 +39,7 @@ public class FormLocomotive extends JComponent{ new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextBoolean(), rnd.nextBoolean()); - _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75); + _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75); speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() ); @@ -69,7 +49,8 @@ public class FormLocomotive extends JComponent{ JButton selectLocomotiveButton = new JButton("Select"); selectLocomotiveButton.addActionListener(e -> { SelectedLocomotive = _locomotive; - JOptionPane.showMessageDialog(formFrame, "OK"); + //TODO + caller.dispose(); }); statusPanel.add(createButton); @@ -107,8 +88,6 @@ public class FormLocomotive extends JComponent{ statusPanel.add(moveDownButton); statusPanel.add(moveLeftButton); statusPanel.add(moveRightButton); - - formFrame.getContentPane().add(this); } @Override protected void paintComponent(Graphics g) { @@ -119,6 +98,6 @@ public class FormLocomotive extends JComponent{ } public static void main(String[] args) { - new FormMap(); + new FormMapWithSetLocomotives(); } } diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java new file mode 100644 index 0000000..62436e5 --- /dev/null +++ b/FormMapWithSetLocomotives.java @@ -0,0 +1,209 @@ +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; + +public class FormMapWithSetLocomotives extends JComponent { + private BufferedImage bufferImg = null; + /// Объект от класса карты с набором объектов + private MapWithSetLocomotivesGeneric _mapLocomotivesCollectionGeneric; + public FormMapWithSetLocomotives() { + JFrame formFrame = new JFrame("Form Map With SetLocomotives"); + formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + formFrame.setSize(750, 500); + formFrame.setLocationRelativeTo(null); + formFrame.setVisible(true); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new GridLayout(0, 1, 20, 20)); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.EAST); + + // КомбоБокс с картами + String[] maps = { + "Simple Map", + "Spike Map", + "Rail Map" + }; + JComboBox mapSelectComboBox = new JComboBox(maps); + mapSelectComboBox.setEditable(true); + mapSelectComboBox.addActionListener(e -> { + AbstractMap map = null; + String item = (String)mapSelectComboBox.getSelectedItem(); + switch (item) { + case "Simple Map": + map = new SimpleMap(); + break; + case "Spike Map": + map = new SpikeMap(); + break; + case "Rail Map": + map = new RailMap(); + break; + } + if (map != null) + { + _mapLocomotivesCollectionGeneric = new MapWithSetLocomotivesGeneric + (600, 500, map); + } + else + { + _mapLocomotivesCollectionGeneric = null; + } + }); + statusPanel.add(mapSelectComboBox); + + // Кнопка добавления локомотива + JButton addLocomotiveButton = new JButton("Add Locomotive"); + addLocomotiveButton.addActionListener(e -> { + // логика добавления + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + // TODO + + JDialog dialog = new JDialog(formFrame, "Dialog", true); + FormLocomotive formLocomotive = new FormLocomotive(dialog); + dialog.setSize(800, 500); + dialog.setContentPane(formLocomotive); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + + + DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive()); + //TODO + if (_mapLocomotivesCollectionGeneric.Plus(locomotive)) { + JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION); + bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); + repaint(); + } + else { + JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION); + } + }); + statusPanel.add(addLocomotiveButton); + + // Текстовое поле для ввода позиции с маской + JFormattedTextField maskedTextFieldPosition = null; + try { + MaskFormatter positionMask = new MaskFormatter("##"); + maskedTextFieldPosition = new JFormattedTextField(positionMask); + statusPanel.add(maskedTextFieldPosition); + } + catch (ParseException e) { + e.printStackTrace(); + } + + //Кнопка удаления локомотива + JButton deleteLocomotiveButton = new JButton("Delete Locomotive"); + JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition; + deleteLocomotiveButton.addActionListener(e -> { + // логика удаления + if ((String)mapSelectComboBox.getSelectedItem() == null) { + return; + } + //TODO + if (finalMaskedTextFieldPosition == null) { + return; + } + int position = Integer.parseInt(finalMaskedTextFieldPosition.getText()); + if (_mapLocomotivesCollectionGeneric.Minus(position)) { + JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION); + bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); + repaint(); + } + else{ + JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION); + } + }); + statusPanel.add(deleteLocomotiveButton); + + //Кнопка просмотра хранилища + JButton showStorageButton = new JButton("Show Storage"); + showStorageButton.addActionListener(e -> { + // логика просмотра + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); + repaint(); + }); + statusPanel.add(showStorageButton); + + //Кнопка просмотра карты + JButton showOnMapButton = new JButton("Show On Map"); + showOnMapButton.addActionListener(e -> { + // логика просмотра + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + bufferImg = _mapLocomotivesCollectionGeneric.ShowOnMap(); + }); + statusPanel.add(showOnMapButton); + + ActionListener moveButtonListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_mapLocomotivesCollectionGeneric == null) + { + return; + } + String name = e.getActionCommand(); + Direction dir = Direction.None; + switch (name) + { + case "Up": + dir = Direction.Up; + break; + case "Down": + dir = Direction.Down; + break; + case "Left": + dir = Direction.Left; + break; + case "Right": + dir = Direction.Right; + break; + } + bufferImg = _mapLocomotivesCollectionGeneric.MoveObject(dir); + } + }; + + //Кнопки управления + JButton moveDownButton = new JButton("Down"); + moveDownButton.addActionListener(moveButtonListener); + + JButton moveUpButton = new JButton("Up"); + moveUpButton.addActionListener(moveButtonListener); + + JButton moveLeftButton = new JButton("Left"); + moveLeftButton.addActionListener(moveButtonListener); + + JButton moveRightButton = new JButton("Right"); + moveRightButton.addActionListener(moveButtonListener); + + statusPanel.add(moveUpButton); + statusPanel.add(moveDownButton); + statusPanel.add(moveLeftButton); + statusPanel.add(moveRightButton); + + formFrame.getContentPane().add(this); + super.repaint(); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (bufferImg != null) g2.drawImage(bufferImg, 0,0,600,500,null); + super.repaint(); + } + +} diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java index 3559e35..3933913 100644 --- a/MapWithSetLocomotivesGeneric.java +++ b/MapWithSetLocomotivesGeneric.java @@ -29,14 +29,15 @@ public class MapWithSetLocomotivesGeneric } /// Добавление - public boolean Plus(MapWithSetLocomotivesGeneric map, T locomotive) + //TODO + public boolean Plus(T locomotive) { - return map._setLocomotives.Insert(locomotive); + return this._setLocomotives.Insert(locomotive); } /// Удаление - public boolean Minus(MapWithSetLocomotivesGeneric map, int position) + public boolean Minus(int position) { - return map._setLocomotives.Remove(position); + return this._setLocomotives.Remove(position); } /// Вывод всего набора объектов @@ -44,8 +45,8 @@ public class MapWithSetLocomotivesGeneric { BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); Graphics gr = bmp.getGraphics(); - DrawBackground(gr); - DrawLocomotives(gr); + DrawBackground((Graphics2D)gr); + DrawLocomotives((Graphics2D)gr); return bmp; } @@ -101,23 +102,50 @@ public class MapWithSetLocomotivesGeneric } /// Метод отрисовки фона - private void DrawBackground(Graphics g) + private void DrawBackground(Graphics2D g) { - g.setColor(Color.BLACK); - for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + g.setColor(Color.WHITE); + g.fillRect(0,0,600, 500); + for (int j = _placeSizeHeight; j < _pictureHeight; j+= _placeSizeHeight) { - for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) - { //линия разметки места - g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * - _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + //нижняя линия рельс + g.setColor(Color.BLACK); + g.setStroke(new BasicStroke(5)); + + g.drawLine(0, j, _pictureWidth, j); + for (int i = 0; i < _pictureWidth; i+=20) + { + g.drawLine(i, j, i, j + 10); + } + g.drawLine(0, j + 10, _pictureWidth, j + 10); + + //верхняя линия рельс + + g.setColor(Color.GRAY); + + g.drawLine(0, j - 20, _pictureWidth, j - 20); + for (int i = 0; i < _pictureWidth; i += 20) + { + g.drawLine(i, j - 20, i, j - 10); + } + g.drawLine(0, j - 10, _pictureWidth, j - 10); + + //фонари + for (int i = _placeSizeWidth; i < _pictureWidth; i += _placeSizeWidth) + { + g.setColor(Color.BLACK); + g.setStroke(new BasicStroke(10)); + g.drawLine(i, j - _placeSizeHeight + 20, i, j); + g.setColor(Color.YELLOW); + g.setStroke(new BasicStroke(20)); + g.drawLine(i, j - _placeSizeHeight + 18, i, j - _placeSizeHeight + 38); } - g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, - (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); } + g.setStroke(new BasicStroke(2)); } /// Метод прорисовки объектов - private void DrawLocomotives(Graphics g) + private void DrawLocomotives(Graphics2D g) { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; @@ -128,7 +156,7 @@ public class MapWithSetLocomotivesGeneric for (int i = 0; i < _setLocomotives.Count(); i++) { // установка позиции - if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth, curHeight * _placeSizeHeight, _pictureWidth, _pictureHeight); + if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight); if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).DrawningObject(g); if (curWidth < width) curWidth++; else diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java index b74c1e7..5c32864 100644 --- a/SetLocomotivesGeneric.java +++ b/SetLocomotivesGeneric.java @@ -24,8 +24,11 @@ public class SetLocomotivesGeneric int emptyEl = -1; for (int i = position + 1; i < Count(); i++) { - if (_places[i] == null) emptyEl = i; - break; + if (_places[i] == null) + { + emptyEl = i; + break; + } } if (emptyEl == -1) { From b28bb84cf831eaf30a1a946f52098f21f68cfdfb Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 21 Oct 2022 21:53:50 +0400 Subject: [PATCH 05/13] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B2=D1=8B=D0=B1=D0=BE=D1=80=20=D1=86=D0=B2?= =?UTF-8?q?=D0=B5=D1=82=D0=B0=20=D0=BF=D1=80=D0=B8=20=D1=81=D0=BE=D0=B7?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=B8=D0=B8=20=D0=BB=D0=BE=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BE=D1=82=D0=B8=D0=B2=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormLocomotive.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/FormLocomotive.java b/FormLocomotive.java index 7eca30a..ed8a82e 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -23,7 +23,10 @@ public class FormLocomotive extends JComponent{ JButton createButton = new JButton("Create"); createButton.addActionListener(e -> { Random rnd = new Random(); - _locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + + Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + + _locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst); _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-75); speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); @@ -34,9 +37,13 @@ public class FormLocomotive extends JComponent{ JButton modifiedButton = new JButton("Modified"); modifiedButton.addActionListener(e -> { Random rnd = new Random(); + + Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + Color colorSecond = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + _locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + colorFirst, + colorSecond, rnd.nextBoolean(), rnd.nextBoolean()); _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75); From 7611f197fa3081efabb754531b4a95306fed2b7d Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 21 Oct 2022 21:59:22 +0400 Subject: [PATCH 06/13] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=20=D0=B2=D1=81=D0=B5=D0=B9=20=D0=B1=D0=B0=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=BE=D0=B9=20=D1=87=D0=B0=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormMapWithSetLocomotives.java | 4 ++-- MapWithSetLocomotivesGeneric.java | 4 ++-- SetLocomotivesGeneric.java | 19 ++++++++++--------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index 62436e5..a6d8d69 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -77,7 +77,7 @@ public class FormMapWithSetLocomotives extends JComponent { DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive()); //TODO - if (_mapLocomotivesCollectionGeneric.Plus(locomotive)) { + if (_mapLocomotivesCollectionGeneric.Plus(locomotive) != -1) { JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION); bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); repaint(); @@ -112,7 +112,7 @@ public class FormMapWithSetLocomotives extends JComponent { return; } int position = Integer.parseInt(finalMaskedTextFieldPosition.getText()); - if (_mapLocomotivesCollectionGeneric.Minus(position)) { + if (_mapLocomotivesCollectionGeneric.Minus(position) != null) { JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION); bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); repaint(); diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java index 3933913..f92bf87 100644 --- a/MapWithSetLocomotivesGeneric.java +++ b/MapWithSetLocomotivesGeneric.java @@ -30,12 +30,12 @@ public class MapWithSetLocomotivesGeneric /// Добавление //TODO - public boolean Plus(T locomotive) + public int Plus(T locomotive) { return this._setLocomotives.Insert(locomotive); } /// Удаление - public boolean Minus(int position) + public T Minus(int position) { return this._setLocomotives.Remove(position); } diff --git a/SetLocomotivesGeneric.java b/SetLocomotivesGeneric.java index 5c32864..7256512 100644 --- a/SetLocomotivesGeneric.java +++ b/SetLocomotivesGeneric.java @@ -10,15 +10,15 @@ public class SetLocomotivesGeneric _places = (T[]) new Object[count]; } - public boolean Insert (T locomotive) { + public int Insert (T locomotive) { return Insert(locomotive, 0); } - public boolean Insert (T locomotive, int position) { - if (position >= _places.length || position < 0) return false; + public int Insert (T locomotive, int position) { + if (position >= _places.length || position < 0) return -1; if (_places[position] == null) { _places[position] = locomotive; - return true; + return position; } int emptyEl = -1; @@ -32,20 +32,21 @@ public class SetLocomotivesGeneric } if (emptyEl == -1) { - return false; + return -1; } for (int i = emptyEl; i > position; i--) { _places[i] = _places[i - 1]; } _places[position] = locomotive; - return true; + return position; } - public boolean Remove (int position) { - if (position >= _places.length || position < 0) return false; + public T Remove (int position) { + if (position >= _places.length || position < 0) return null; + T result = _places[position]; _places[position] = null; - return true; + return result; } public T Get(int position) From 9acaae8ff2d63f04f0ddca22fe88ee4c809d474a Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 22 Oct 2022 01:15:02 +0400 Subject: [PATCH 07/13] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B4=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81,=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D1=81=D0=BE=D0=B2=D0=BA=D0=B8=20=D0=BA=D0=BE=D0=BB=D0=B5?= =?UTF-8?q?=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 13 +++++----- DrawningWarmlyLocomotive.java | 6 +++++ EntityWithExtraCreator.java | 47 +++++++++++++++++++++++++++++++++++ ExtraRoundWheelDraw.java | 5 +++- ExtraStarWheelDraw.java | 5 +++- FormLocomotive.java | 4 +-- 6 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 EntityWithExtraCreator.java diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index e89236f..3293091 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -3,7 +3,6 @@ import java.util.Random; public class DrawningLocomotive { public EntityLocomotive Locomotive; - public ExtraWheelsDraw extraWheelsDraw; public IDrawningExtra drawningExtra; /// Левая координата отрисовки локомотива protected float _startPosX; @@ -22,13 +21,12 @@ public class DrawningLocomotive { public DrawningLocomotive(int speed, float weight, Color bodyColor) { int randExtra = random.nextInt(2); - extraWheelsDraw = new ExtraWheelsDraw(randExtra, bodyColor); switch (random.nextInt(3)){ case 0: - drawningExtra = new ExtraStarWheelDraw(randExtra); + drawningExtra = new ExtraStarWheelDraw(randExtra, bodyColor); break; case 1: - drawningExtra = new ExtraRoundWheelDraw(randExtra); + drawningExtra = new ExtraRoundWheelDraw(randExtra, bodyColor); break; case 2: drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor); @@ -37,6 +35,11 @@ public class DrawningLocomotive { Locomotive = new EntityLocomotive(speed, weight, bodyColor); } + public DrawningLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) { + drawningExtra = extra; + Locomotive = locomotive; + } + // Новый конструктор protected DrawningLocomotive (int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) { @@ -122,8 +125,6 @@ public class DrawningLocomotive { //дверь g.setColor(Color.BLACK); g.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); - //колеса - extraWheelsDraw.DrawExtra((int)_startPosX, (int)_startPosY, g); //extra drawningExtra.DrawExtra((int)_startPosX, (int)_startPosY, g); //движок diff --git a/DrawningWarmlyLocomotive.java b/DrawningWarmlyLocomotive.java index f7fd094..4019911 100644 --- a/DrawningWarmlyLocomotive.java +++ b/DrawningWarmlyLocomotive.java @@ -5,6 +5,12 @@ public class DrawningWarmlyLocomotive extends DrawningLocomotive{ super(speed, weight, bodyColor, 140, 70); Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); } + + public DrawningWarmlyLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) { + super(locomotive, extra); + Locomotive = locomotive; + } + @Override public void DrawTransport(Graphics2D g) { diff --git a/EntityWithExtraCreator.java b/EntityWithExtraCreator.java new file mode 100644 index 0000000..b037325 --- /dev/null +++ b/EntityWithExtraCreator.java @@ -0,0 +1,47 @@ +import java.util.Random; + +public class EntityWithExtraCreator { + private T[] entityArr; + private U[] extraArr; + + int entitiesCount = 0; + int extraCount = 0; + + public EntityWithExtraCreator(int countEntities, int countExtra) { + entityArr = (T[]) new Object[countEntities]; + extraArr = (U[]) new Object[countExtra]; + } + + public int Insert(T entityLocomotive) { + if(entitiesCount < entityArr.length) { + entityArr[entitiesCount] = entityLocomotive; + entitiesCount++; + return entitiesCount - 1; + } + return -1; + } + + public int Insert (U extra) { + if(extraCount < extraArr.length) { + extraArr[extraCount] = extra; + extraCount++; + return extraCount - 1; + } + return -1; + } + + public DrawningLocomotive getEntityWithExtra() { + Random random = new Random(); + int getEntityRandomIndex = random.nextInt(entityArr.length); + int getExtraRandomIndex = random.nextInt(extraArr.length); + + T locomotive = entityArr[getEntityRandomIndex]; + U extra = extraArr[getExtraRandomIndex]; + + if (locomotive instanceof EntityWarmlyLocomotive) { + return new DrawningWarmlyLocomotive(locomotive, extra); + } + return new DrawningLocomotive(locomotive, extra); + } + +} diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java index 5f28863..e14642b 100644 --- a/ExtraRoundWheelDraw.java +++ b/ExtraRoundWheelDraw.java @@ -2,6 +2,7 @@ import java.awt.*; public class ExtraRoundWheelDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; + private ExtraWheelsDraw extraWheelsDraw; public void setExtraNum(int num) { switch (num) { case 0: { @@ -17,11 +18,13 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{ } } - public ExtraRoundWheelDraw (int num) { + public ExtraRoundWheelDraw (int num, Color bodyColor) { setExtraNum(num); + extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); } public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + extraWheelsDraw.DrawExtra(startPosX, startPosY, g); g.setColor(Color.BLACK); g.fillOval(startPosX + 5, startPosY + 35, 10, 10); g.fillOval(startPosX + 95, startPosY + 35, 10, 10); diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java index dac1eb8..3b415d6 100644 --- a/ExtraStarWheelDraw.java +++ b/ExtraStarWheelDraw.java @@ -2,6 +2,7 @@ import java.awt.*; public class ExtraStarWheelDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; + private ExtraWheelsDraw extraWheelsDraw; public void setExtraNum(int num) { switch (num) { case 0: { @@ -17,11 +18,13 @@ public class ExtraStarWheelDraw implements IDrawningExtra{ } } - public ExtraStarWheelDraw (int num) { + public ExtraStarWheelDraw (int num, Color bodyColor) { setExtraNum(num); + extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); } public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + extraWheelsDraw.DrawExtra(startPosX, startPosY, g); DrawStarOnWheel(startPosX, startPosY + 30, g); DrawStarOnWheel(startPosX + 90, startPosY + 30, g); switch (wheelsCount) { diff --git a/FormLocomotive.java b/FormLocomotive.java index ed8a82e..bab32f4 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -38,8 +38,8 @@ public class FormLocomotive extends JComponent{ modifiedButton.addActionListener(e -> { Random rnd = new Random(); - Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); - Color colorSecond = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); + Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256))); _locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst, From 436407133226afe72e36f667b4dbac7103bda10a Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 22 Oct 2022 16:12:48 +0400 Subject: [PATCH 08/13] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BE=D1=82=D1=80=D0=B8=D1=81=D0=BE=D0=B2=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0.=20=D0=92=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=20?= =?UTF-8?q?=D1=82=D1=80=D0=B5=D0=B1=D1=83=D0=B5=D1=82=D1=81=D1=8F=20=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B5=D0=B5?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EntityWithExtraCreator.java | 12 ++--- FormEntityWithExtraGallery.java | 95 +++++++++++++++++++++++++++++++++ FormMapWithSetLocomotives.java | 11 +++- 3 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 FormEntityWithExtraGallery.java diff --git a/EntityWithExtraCreator.java b/EntityWithExtraCreator.java index b037325..239ec49 100644 --- a/EntityWithExtraCreator.java +++ b/EntityWithExtraCreator.java @@ -1,15 +1,15 @@ import java.util.Random; public class EntityWithExtraCreator { - private T[] entityArr; - private U[] extraArr; + private EntityLocomotive[] entityArr; + private IDrawningExtra[] extraArr; int entitiesCount = 0; int extraCount = 0; public EntityWithExtraCreator(int countEntities, int countExtra) { - entityArr = (T[]) new Object[countEntities]; - extraArr = (U[]) new Object[countExtra]; + entityArr = new EntityLocomotive[countEntities]; + extraArr = new IDrawningExtra[countExtra]; } public int Insert(T entityLocomotive) { @@ -35,8 +35,8 @@ public class EntityWithExtraCreator entityWithExtraCreator; + + public FormEntityWithExtraGallery() { + JFrame formFrame = new JFrame("Gallery"); + formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + formFrame.setSize(900, 500); + formFrame.setVisible(true); + formFrame.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new FlowLayout()); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.SOUTH); + + Label speedLabel = new Label("Speed: "); + Label weightLabel = new Label("Weight: "); + Label colorLabel = new Label("Color: "); + + JButton randomlyFillArraysWithParts = new JButton("Randomly fill arrays with parts"); + randomlyFillArraysWithParts.addActionListener(e -> { + Random random = new Random(); + entityWithExtraCreator = new EntityWithExtraCreator<>(100, 100); + for (int i = 0; i < 100; i ++) { + if (random.nextBoolean()) { + entityWithExtraCreator.Insert(new EntityLocomotive(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + } + else { + entityWithExtraCreator.Insert(new EntityWarmlyLocomotive(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + random.nextBoolean(), random.nextBoolean())); + } + } + for (int i = 0; i < 100; i ++) { + int extraRand = random.nextInt(3); + switch (extraRand) { + case 0: + entityWithExtraCreator.Insert(new ExtraWheelsDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 1: + entityWithExtraCreator.Insert(new ExtraStarWheelDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 2: + entityWithExtraCreator.Insert(new ExtraRoundWheelDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + } + } + repaint(); + }); + statusPanel.add(randomlyFillArraysWithParts); + + JButton showRandomEntity = new JButton("Create entity from parts"); + showRandomEntity.addActionListener(e -> { + if (entityWithExtraCreator == null) return; + _locomotiveFirst = entityWithExtraCreator.getEntityWithExtra(); + _locomotiveFirst.SetPosition(200, 200, formFrame.getWidth(), formFrame.getHeight() - 75); + + _locomotiveSecond = entityWithExtraCreator.getEntityWithExtra(); + _locomotiveSecond.SetPosition(400, 200, formFrame.getWidth(), formFrame.getHeight() - 75); + + _locomotiveThird = entityWithExtraCreator.getEntityWithExtra(); + _locomotiveThird.SetPosition(600, 200, formFrame.getWidth(), formFrame.getHeight() - 75); + repaint(); + }); + statusPanel.add(showRandomEntity); + + + formFrame.getContentPane().add(this); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (_locomotiveFirst != null) _locomotiveFirst.DrawTransport(g2); + if (_locomotiveSecond != null) _locomotiveSecond.DrawTransport(g2); + if (_locomotiveThird != null) _locomotiveThird.DrawTransport(g2); + super.repaint(); + } +} diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index a6d8d69..197144d 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -15,7 +15,7 @@ public class FormMapWithSetLocomotives extends JComponent { formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); formFrame.setSize(750, 500); formFrame.setLocationRelativeTo(null); - formFrame.setVisible(true); + Panel statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); @@ -194,8 +194,15 @@ public class FormMapWithSetLocomotives extends JComponent { statusPanel.add(moveLeftButton); statusPanel.add(moveRightButton); + JButton showGalleryButton = new JButton("Show Gallery"); + showGalleryButton.addActionListener(e -> { + new FormEntityWithExtraGallery(); + }); + statusPanel.add(showGalleryButton); + formFrame.getContentPane().add(this); - super.repaint(); + formFrame.setVisible(true); + //super.repaint(); } @Override From fdd140a179f61ff4abbe3b9cfc23ed53966e2798 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 22 Oct 2022 16:18:22 +0400 Subject: [PATCH 09/13] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D0=B1=D1=83=D0=B5?= =?UTF-8?q?=D1=82=D1=81=D1=8F=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D0=B5=D0=B2,=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6=D0=B5?= =?UTF-8?q?=20FormMap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormLocomotive.java | 1 - 1 file changed, 1 deletion(-) diff --git a/FormLocomotive.java b/FormLocomotive.java index bab32f4..bfdf5eb 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -103,7 +103,6 @@ public class FormLocomotive extends JComponent{ if (_locomotive != null) _locomotive.DrawTransport(g2); super.repaint(); } - public static void main(String[] args) { new FormMapWithSetLocomotives(); } From 89ab094b5b851f5af34af8ca825cb0c1dc6d302f Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Mon, 24 Oct 2022 10:58:46 +0400 Subject: [PATCH 10/13] =?UTF-8?q?=D0=94=D0=B6=D0=B5=D0=BD=D0=B5=D1=80?= =?UTF-8?q?=D0=B8=D0=BA=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BC=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B8=D0=B2=D1=8B=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D1=8E=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20=D1=87=D1=83=D1=82=D1=8C=20=D0=B1=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=BC=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B7=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EntityWithExtraCreator.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/EntityWithExtraCreator.java b/EntityWithExtraCreator.java index 239ec49..04b6d65 100644 --- a/EntityWithExtraCreator.java +++ b/EntityWithExtraCreator.java @@ -1,15 +1,16 @@ +import java.lang.reflect.Array; import java.util.Random; public class EntityWithExtraCreator { - private EntityLocomotive[] entityArr; - private IDrawningExtra[] extraArr; + private T[] entityArr; + private U[] extraArr; int entitiesCount = 0; int extraCount = 0; public EntityWithExtraCreator(int countEntities, int countExtra) { - entityArr = new EntityLocomotive[countEntities]; - extraArr = new IDrawningExtra[countExtra]; + entityArr = (T[]) new EntityLocomotive[countEntities]; + extraArr = (U[]) new IDrawningExtra[countExtra]; } public int Insert(T entityLocomotive) { From 55e0a3592802e1bde7787d6998372034a28e3dd2 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Tue, 25 Oct 2022 16:54:14 +0400 Subject: [PATCH 11/13] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8.=20?= =?UTF-8?q?=D0=A1=D0=B4=D0=B0=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=81=D0=BB=D0=BE?= =?UTF-8?q?=D0=B6=D0=BD=D0=B0=D1=8F=20=D0=9B=D0=B0=D0=B1=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EntityWithExtraCreator.java | 20 ++++---- FormEntityWithExtraGallery.java | 83 ++++++++++++++++----------------- 2 files changed, 47 insertions(+), 56 deletions(-) diff --git a/EntityWithExtraCreator.java b/EntityWithExtraCreator.java index 04b6d65..deda0ef 100644 --- a/EntityWithExtraCreator.java +++ b/EntityWithExtraCreator.java @@ -2,33 +2,29 @@ import java.lang.reflect.Array; import java.util.Random; public class EntityWithExtraCreator { - private T[] entityArr; - private U[] extraArr; + private final Object[] entityArr; + private final Object[] extraArr; int entitiesCount = 0; int extraCount = 0; public EntityWithExtraCreator(int countEntities, int countExtra) { - entityArr = (T[]) new EntityLocomotive[countEntities]; - extraArr = (U[]) new IDrawningExtra[countExtra]; + entityArr = new Object[countEntities]; + extraArr = new Object[countExtra]; } - public int Insert(T entityLocomotive) { + public void Insert(T entityLocomotive) { if(entitiesCount < entityArr.length) { entityArr[entitiesCount] = entityLocomotive; entitiesCount++; - return entitiesCount - 1; } - return -1; } - public int Insert (U extra) { + public void Insert (U extra) { if(extraCount < extraArr.length) { extraArr[extraCount] = extra; extraCount++; - return extraCount - 1; } - return -1; } public DrawningLocomotive getEntityWithExtra() { @@ -36,8 +32,8 @@ public class EntityWithExtraCreator { - Random random = new Random(); - entityWithExtraCreator = new EntityWithExtraCreator<>(100, 100); - for (int i = 0; i < 100; i ++) { - if (random.nextBoolean()) { - entityWithExtraCreator.Insert(new EntityLocomotive(random.nextInt(100), random.nextInt(100), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); - } - else { - entityWithExtraCreator.Insert(new EntityWarmlyLocomotive(random.nextInt(100), random.nextInt(100), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), - random.nextBoolean(), random.nextBoolean())); - } - } - for (int i = 0; i < 100; i ++) { - int extraRand = random.nextInt(3); - switch (extraRand) { - case 0: - entityWithExtraCreator.Insert(new ExtraWheelsDraw(random.nextInt(3), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); - break; - - case 1: - entityWithExtraCreator.Insert(new ExtraStarWheelDraw(random.nextInt(3), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); - break; - - case 2: - entityWithExtraCreator.Insert(new ExtraRoundWheelDraw(random.nextInt(3), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); - break; - } - } - repaint(); - }); - statusPanel.add(randomlyFillArraysWithParts); JButton showRandomEntity = new JButton("Create entity from parts"); showRandomEntity.addActionListener(e -> { - if (entityWithExtraCreator == null) return; + + Random random = new Random(); + if (entityWithExtraCreator == null) { + entityWithExtraCreator = new EntityWithExtraCreator(20, 20); + for (int i = 0; i < 20; i ++) { + if (random.nextBoolean()) { + entityWithExtraCreator.Insert(new EntityLocomotive(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + } + else { + entityWithExtraCreator.Insert(new EntityWarmlyLocomotive(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + random.nextBoolean(), random.nextBoolean())); + } + } + for (int i = 0; i < 20; i ++) { + int extraRand = random.nextInt(3); + switch (extraRand) { + case 0: + entityWithExtraCreator.Insert(new ExtraWheelsDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 1: + entityWithExtraCreator.Insert(new ExtraStarWheelDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 2: + entityWithExtraCreator.Insert(new ExtraRoundWheelDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + } + } + } _locomotiveFirst = entityWithExtraCreator.getEntityWithExtra(); _locomotiveFirst.SetPosition(200, 200, formFrame.getWidth(), formFrame.getHeight() - 75); @@ -81,6 +74,8 @@ public class FormEntityWithExtraGallery extends JComponent { formFrame.getContentPane().add(this); + + formFrame.setVisible(true); } @Override From d66ca977a5fdb975a2e40b30d6e532deb4545c0f Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Tue, 25 Oct 2022 16:59:53 +0400 Subject: [PATCH 12/13] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B1=D0=B5=D0=BB=D1=8B=20=D0=B8=20=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8=D0=B8.=20=D0=A1?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=81=D0=BB=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D0=9B=D0=B0=D0=B1=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormEntityWithExtraGallery.java | 2 -- FormLocomotive.java | 1 - FormMapWithSetLocomotives.java | 10 ---------- MapWithSetLocomotivesGeneric.java | 2 -- 4 files changed, 15 deletions(-) diff --git a/FormEntityWithExtraGallery.java b/FormEntityWithExtraGallery.java index 6398dfc..d2b4dca 100644 --- a/FormEntityWithExtraGallery.java +++ b/FormEntityWithExtraGallery.java @@ -20,8 +20,6 @@ public class FormEntityWithExtraGallery extends JComponent { setLayout(new BorderLayout()); add(statusPanel, BorderLayout.SOUTH); - - JButton showRandomEntity = new JButton("Create entity from parts"); showRandomEntity.addActionListener(e -> { diff --git a/FormLocomotive.java b/FormLocomotive.java index bfdf5eb..8d7b5ee 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -56,7 +56,6 @@ public class FormLocomotive extends JComponent{ JButton selectLocomotiveButton = new JButton("Select"); selectLocomotiveButton.addActionListener(e -> { SelectedLocomotive = _locomotive; - //TODO caller.dispose(); }); diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index 197144d..d988495 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -16,7 +16,6 @@ public class FormMapWithSetLocomotives extends JComponent { formFrame.setSize(750, 500); formFrame.setLocationRelativeTo(null); - Panel statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); statusPanel.setLayout(new GridLayout(0, 1, 20, 20)); @@ -65,18 +64,13 @@ public class FormMapWithSetLocomotives extends JComponent { { return; } - // TODO - JDialog dialog = new JDialog(formFrame, "Dialog", true); FormLocomotive formLocomotive = new FormLocomotive(dialog); dialog.setSize(800, 500); dialog.setContentPane(formLocomotive); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); - - DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive()); - //TODO if (_mapLocomotivesCollectionGeneric.Plus(locomotive) != -1) { JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION); bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); @@ -98,7 +92,6 @@ public class FormMapWithSetLocomotives extends JComponent { catch (ParseException e) { e.printStackTrace(); } - //Кнопка удаления локомотива JButton deleteLocomotiveButton = new JButton("Delete Locomotive"); JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition; @@ -122,7 +115,6 @@ public class FormMapWithSetLocomotives extends JComponent { } }); statusPanel.add(deleteLocomotiveButton); - //Кнопка просмотра хранилища JButton showStorageButton = new JButton("Show Storage"); showStorageButton.addActionListener(e -> { @@ -135,7 +127,6 @@ public class FormMapWithSetLocomotives extends JComponent { repaint(); }); statusPanel.add(showStorageButton); - //Кнопка просмотра карты JButton showOnMapButton = new JButton("Show On Map"); showOnMapButton.addActionListener(e -> { @@ -202,7 +193,6 @@ public class FormMapWithSetLocomotives extends JComponent { formFrame.getContentPane().add(this); formFrame.setVisible(true); - //super.repaint(); } @Override diff --git a/MapWithSetLocomotivesGeneric.java b/MapWithSetLocomotivesGeneric.java index f92bf87..d6bd589 100644 --- a/MapWithSetLocomotivesGeneric.java +++ b/MapWithSetLocomotivesGeneric.java @@ -29,7 +29,6 @@ public class MapWithSetLocomotivesGeneric } /// Добавление - //TODO public int Plus(T locomotive) { return this._setLocomotives.Insert(locomotive); @@ -39,7 +38,6 @@ public class MapWithSetLocomotivesGeneric { return this._setLocomotives.Remove(position); } - /// Вывод всего набора объектов public BufferedImage ShowSet() { From 80e51d1ecff80e8e333b752899d1a83235a72bd9 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Tue, 25 Oct 2022 17:03:00 +0400 Subject: [PATCH 13/13] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B8=D0=B5=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B1=D0=B5=D0=BB=D1=8B=20=D0=B8=20=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8=D0=B8=20=D0=A7=D0=90?= =?UTF-8?q?=D0=A1=D0=A2=D0=AC=202.=20=D0=A1=D0=B4=D0=B0=D0=BD=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B0=D1=8F=20=D0=9B?= =?UTF-8?q?=D0=B0=D0=B1=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormEntityWithExtraGallery.java | 1 - FormMapWithSetLocomotives.java | 1 - 2 files changed, 2 deletions(-) diff --git a/FormEntityWithExtraGallery.java b/FormEntityWithExtraGallery.java index d2b4dca..80f05ec 100644 --- a/FormEntityWithExtraGallery.java +++ b/FormEntityWithExtraGallery.java @@ -70,7 +70,6 @@ public class FormEntityWithExtraGallery extends JComponent { }); statusPanel.add(showRandomEntity); - formFrame.getContentPane().add(this); formFrame.setVisible(true); diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index d988495..318e071 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -100,7 +100,6 @@ public class FormMapWithSetLocomotives extends JComponent { if ((String)mapSelectComboBox.getSelectedItem() == null) { return; } - //TODO if (finalMaskedTextFieldPosition == null) { return; }