From 5a2ef44c5cf8c63a4923103f69ce5aed821bf05c Mon Sep 17 00:00:00 2001 From: Safgerd Date: Sun, 4 Dec 2022 00:19:21 +0400 Subject: [PATCH] =?UTF-8?q?LabWork05:=20=D0=9B=D0=B0=D0=B1=D0=BE=D1=80?= =?UTF-8?q?=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D0=B3=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0,=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningCrossRollers.java | 4 + DrawningRollers.java | 4 + DrawningSquaredRollers.java | 4 + DrawningTrackedVehicle.java | 11 ++ DrawningTracktor.java | 12 ++ EventListener.java | 16 ++ FormMapWithSetTracktor.java | 39 ++--- FormTracktorConfig.form | 341 ++++++++++++++++++++++++++++++++++++ FormTracktorConfig.java | 153 ++++++++++++++++ IDrawningRollers.java | 1 + 10 files changed, 564 insertions(+), 21 deletions(-) create mode 100644 EventListener.java create mode 100644 FormTracktorConfig.form create mode 100644 FormTracktorConfig.java diff --git a/DrawningCrossRollers.java b/DrawningCrossRollers.java index 22bd26e..8e2bbef 100644 --- a/DrawningCrossRollers.java +++ b/DrawningCrossRollers.java @@ -91,4 +91,8 @@ public class DrawningCrossRollers implements IDrawningRollers{ g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); } + + public void setColor(Color color) { + this.colorRollers = color; + } } diff --git a/DrawningRollers.java b/DrawningRollers.java index 748c25f..100f1b7 100644 --- a/DrawningRollers.java +++ b/DrawningRollers.java @@ -67,4 +67,8 @@ public class DrawningRollers implements IDrawningRollers { g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); } + + public void setColor(Color color) { + this.colorRollers = color; + } } diff --git a/DrawningSquaredRollers.java b/DrawningSquaredRollers.java index 192dbe9..dcae709 100644 --- a/DrawningSquaredRollers.java +++ b/DrawningSquaredRollers.java @@ -122,4 +122,8 @@ public class DrawningSquaredRollers implements IDrawningRollers { g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); } + + public void setColor(Color color) { + this.colorRollers = color; + } } diff --git a/DrawningTrackedVehicle.java b/DrawningTrackedVehicle.java index 085c5e5..4ea5426 100644 --- a/DrawningTrackedVehicle.java +++ b/DrawningTrackedVehicle.java @@ -58,4 +58,15 @@ public class DrawningTrackedVehicle extends DrawningTracktor { g.drawRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10); } } + + @Override + public void setColor(Color color) { + var tmp = (EntityTrackedVehicle) Tracktor; + Tracktor = new EntityTrackedVehicle(tmp.getSpeed(), tmp.getWeight(), color, tmp.getDopColor(), tmp.getBucket(), tmp.getSupports()); + } + + public void setDopColor(Color color) { + var tmp = (EntityTrackedVehicle) Tracktor; + Tracktor = new EntityTrackedVehicle(tmp.getSpeed(), tmp.getWeight(), tmp.getBodyColor(), color, tmp.getBucket(), tmp.getSupports()); + } } diff --git a/DrawningTracktor.java b/DrawningTracktor.java index 8354d8f..df94f9a 100644 --- a/DrawningTracktor.java +++ b/DrawningTracktor.java @@ -27,6 +27,10 @@ public class DrawningTracktor { drawningRollers = rollers; } + public IDrawningRollers getRollers() { + return drawningRollers; + } + protected DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers, int tracktorWidth, int tracktorHeight){ this(speed, weight, bodyColor, countRollers); _tracktorWidth = tracktorWidth; @@ -174,4 +178,12 @@ public class DrawningTracktor { // Top - 2 // Bottom - 3 } + + public void setColor(Color color) { + Tracktor = new EntityTracktor(Tracktor.getSpeed(), Tracktor.getWeight(), color); + } + + public void setRollers(IDrawningRollers rollers) { + drawningRollers = rollers; + } } diff --git a/EventListener.java b/EventListener.java new file mode 100644 index 0000000..f939e3f --- /dev/null +++ b/EventListener.java @@ -0,0 +1,16 @@ +import java.util.ArrayList; +import java.util.function.Consumer; + +public class EventListener { + private final ArrayList> listeners = new ArrayList<>(); + + public void addListener(Consumer listener) { + listeners.add(listener); + } + + public void emit(T tracktor) { + for (var listener : listeners) { + listener.accept(tracktor); + } + } +} \ No newline at end of file diff --git a/FormMapWithSetTracktor.java b/FormMapWithSetTracktor.java index 49a6848..334733b 100644 --- a/FormMapWithSetTracktor.java +++ b/FormMapWithSetTracktor.java @@ -89,29 +89,26 @@ public class FormMapWithSetTracktor extends JFrame { buttonAddTracktor.addActionListener(e -> { - if (listBoxMaps.getSelectedIndex() == -1) { - return; - } - - FormTracktor dialog = new FormTracktor(); - dialog.setSize(800, 500); - dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - dialog.setVisible(true); - - if (dialog.getSelectedTracktor() != null) { - DrawningObjectExcavator tracktor = new DrawningObjectExcavator(dialog.getSelectedTracktor()); - if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addTracktor(tracktor) != -1) - { - JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); - repaint(); + FormTracktorConfig form = new FormTracktorConfig(); + form.addListener(tracktor -> { + if (listBoxMaps.getSelectedIndex() == -1) { + return; } - else - { - JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE); + if (tracktor != null) { + DrawningObjectExcavator objectTracktor = new DrawningObjectExcavator(tracktor); + if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addTracktor(objectTracktor) != -1) + { + JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); + repaint(); + } + else + { + JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE); + } } - } + }); + form.setVisible(true); }); buttonRemoveTracktor.addActionListener(e -> { diff --git a/FormTracktorConfig.form b/FormTracktorConfig.form new file mode 100644 index 0000000..1a736aa --- /dev/null +++ b/FormTracktorConfig.form @@ -0,0 +1,341 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FormTracktorConfig.java b/FormTracktorConfig.java new file mode 100644 index 0000000..1c78b67 --- /dev/null +++ b/FormTracktorConfig.java @@ -0,0 +1,153 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.function.Consumer; + +public class FormTracktorConfig extends JFrame { + + private final EventListener eventHandler = new EventListener<>(); + private DrawningTracktor _tracktor; + private JPanel contentPanel; + private JPanel toolsPanel; + private JPanel workspacePanel; + private JButton buttonAdd; + private JButton buttonCancel; + private JLabel colorLabel; + private JLabel additionalColorLabel; + private JPanel pictureBox; + private JLabel speedLabel; + private JSpinner speedSpinner; + private JLabel weightLabel; + private JSpinner weightSpinner; + private JCheckBox checkBoxBucket; + private JCheckBox checkBoxSupports; + private JPanel colorsPanel; + private JPanel redPanel; + private JPanel greenPanel; + private JPanel bluePanel; + private JPanel yellowPanel; + private JPanel whitePanel; + private JPanel grayPanel; + private JPanel blackPanel; + private JPanel purplePanel; + private JPanel rollersPanel; + private JLabel rollersLabel; + private JSpinner rollersSpinner; + private JLabel baseLabel; + private JLabel advancedLabel; + private JLabel baseRollersLabel; + private JLabel crossRollersLabel; + private JLabel squaredRollersLabel; + + public FormTracktorConfig() { + this.setTitle("Создание объекта"); + this.setSize(840, 480); + this.setContentPane(contentPanel); + this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); + + // Создание границ + colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + additionalColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + pictureBox.setBorder(BorderFactory.createDashedBorder(Color.BLACK)); + baseLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + baseRollersLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + crossRollersLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + squaredRollersLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); + + // Задание ограничений + rollersSpinner.setModel(new SpinnerNumberModel(4, 4, 6, 1)); + speedSpinner.setModel(new SpinnerNumberModel(100, 100, 1000, 1)); + weightSpinner.setModel(new SpinnerNumberModel(100, 100, 1000, 1)); + + // Создание обработчиков + var dragAdapter = new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + super.mouseReleased(e); + setCursor(new Cursor(Cursor.HAND_CURSOR)); + } + + @Override + public void mouseReleased(MouseEvent e) { + super.mouseReleased(e); + dispatchDrop((JComponent) e.getSource()); + } + }; + + // Задание обработчиков + redPanel.addMouseListener(dragAdapter); + greenPanel.addMouseListener(dragAdapter); + bluePanel.addMouseListener(dragAdapter); + yellowPanel.addMouseListener(dragAdapter); + whitePanel.addMouseListener(dragAdapter); + grayPanel.addMouseListener(dragAdapter); + blackPanel.addMouseListener(dragAdapter); + purplePanel.addMouseListener(dragAdapter); + + baseLabel.addMouseListener(dragAdapter); + advancedLabel.addMouseListener(dragAdapter); + baseRollersLabel.addMouseListener(dragAdapter); + crossRollersLabel.addMouseListener(dragAdapter); + squaredRollersLabel.addMouseListener(dragAdapter); + + buttonAdd.addActionListener(e -> { + eventHandler.emit(_tracktor); + dispose(); + }); + buttonCancel.addActionListener(e -> dispose()); + } + + public void addListener(Consumer listener) { + eventHandler.addListener(listener); + } + + public void dispatchDrop(JComponent droppedComponent) { + setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + if (droppedComponent == null) { + return; + } + + if (droppedComponent instanceof JPanel panel) { + if (colorLabel.getMousePosition() != null) { + _tracktor.setColor(panel.getBackground()); + _tracktor.getRollers().setColor(panel.getBackground()); + } + if (additionalColorLabel.getMousePosition() != null && _tracktor instanceof DrawningTrackedVehicle _trackedVehicle) { + _trackedVehicle.setDopColor(panel.getBackground()); + } + } + if (droppedComponent instanceof JLabel label && pictureBox.getMousePosition() != null) { + int speed = (Integer) speedSpinner.getValue(); + int weight = (Integer) weightSpinner.getValue(); + int rollersCount = (Integer) rollersSpinner.getValue(); + boolean bucket = checkBoxBucket.isSelected(); + boolean supports = checkBoxSupports.isSelected(); + + if (label == baseLabel) { + _tracktor = new DrawningTracktor(speed, weight, Color.WHITE, rollersCount); + } else if (label == advancedLabel) { + _tracktor = new DrawningTrackedVehicle(speed, weight, Color.WHITE, rollersCount, Color.WHITE, bucket, supports); + } else if (_tracktor != null && label == baseRollersLabel) { + _tracktor.setRollers(new DrawningRollers(rollersCount, _tracktor.getTracktor().getBodyColor())); + } else if (_tracktor != null && label == crossRollersLabel) { + _tracktor.setRollers(new DrawningCrossRollers(rollersCount, _tracktor.getTracktor().getBodyColor())); + } else if (_tracktor != null && label == squaredRollersLabel) { + _tracktor.setRollers(new DrawningSquaredRollers(rollersCount, _tracktor.getTracktor().getBodyColor())); + } + } + + repaint(); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + if (_tracktor != null) { + g = pictureBox.getGraphics(); + _tracktor.SetPosition(60, 75, pictureBox.getWidth(), pictureBox.getHeight()); + _tracktor.DrawTransport((Graphics2D) g); + } + } +} diff --git a/IDrawningRollers.java b/IDrawningRollers.java index 7a739d2..511c22c 100644 --- a/IDrawningRollers.java +++ b/IDrawningRollers.java @@ -2,5 +2,6 @@ import java.awt.*; public interface IDrawningRollers { void setRollersCount(int count); + void setColor(Color color); void DrawRollers(Graphics2D g, float _startPosX, float _startPosY); }