LabWork05: Лабораторная готова, возможен рефакторинг

This commit is contained in:
Safgerd 2022-12-04 00:19:21 +04:00
parent 8a33beaab2
commit 5a2ef44c5c
10 changed files with 564 additions and 21 deletions

View File

@ -91,4 +91,8 @@ public class DrawningCrossRollers implements IDrawningRollers{
g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6);
g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6);
} }
public void setColor(Color color) {
this.colorRollers = color;
}
} }

View File

@ -67,4 +67,8 @@ public class DrawningRollers implements IDrawningRollers {
g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6);
g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6);
} }
public void setColor(Color color) {
this.colorRollers = color;
}
} }

View File

@ -122,4 +122,8 @@ public class DrawningSquaredRollers implements IDrawningRollers {
g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6);
g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6);
} }
public void setColor(Color color) {
this.colorRollers = color;
}
} }

View File

@ -58,4 +58,15 @@ public class DrawningTrackedVehicle extends DrawningTracktor {
g.drawRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10); 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());
}
} }

View File

@ -27,6 +27,10 @@ public class DrawningTracktor {
drawningRollers = rollers; drawningRollers = rollers;
} }
public IDrawningRollers getRollers() {
return drawningRollers;
}
protected DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers, int tracktorWidth, int tracktorHeight){ protected DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers, int tracktorWidth, int tracktorHeight){
this(speed, weight, bodyColor, countRollers); this(speed, weight, bodyColor, countRollers);
_tracktorWidth = tracktorWidth; _tracktorWidth = tracktorWidth;
@ -174,4 +178,12 @@ public class DrawningTracktor {
// Top - 2 // Top - 2
// Bottom - 3 // Bottom - 3
} }
public void setColor(Color color) {
Tracktor = new EntityTracktor(Tracktor.getSpeed(), Tracktor.getWeight(), color);
}
public void setRollers(IDrawningRollers rollers) {
drawningRollers = rollers;
}
} }

16
EventListener.java Normal file
View File

@ -0,0 +1,16 @@
import java.util.ArrayList;
import java.util.function.Consumer;
public class EventListener<T> {
private final ArrayList<Consumer<T>> listeners = new ArrayList<>();
public void addListener(Consumer<T> listener) {
listeners.add(listener);
}
public void emit(T tracktor) {
for (var listener : listeners) {
listener.accept(tracktor);
}
}
}

View File

@ -89,19 +89,14 @@ public class FormMapWithSetTracktor extends JFrame {
buttonAddTracktor.addActionListener(e -> { buttonAddTracktor.addActionListener(e -> {
FormTracktorConfig form = new FormTracktorConfig();
form.addListener(tracktor -> {
if (listBoxMaps.getSelectedIndex() == -1) { if (listBoxMaps.getSelectedIndex() == -1) {
return; return;
} }
if (tracktor != null) {
FormTracktor dialog = new FormTracktor(); DrawningObjectExcavator objectTracktor = new DrawningObjectExcavator(tracktor);
dialog.setSize(800, 500); if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addTracktor(objectTracktor) != -1)
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); JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
@ -113,6 +108,8 @@ public class FormMapWithSetTracktor extends JFrame {
} }
} }
}); });
form.setVisible(true);
});
buttonRemoveTracktor.addActionListener(e -> { buttonRemoveTracktor.addActionListener(e -> {
String text = textBoxPosition.getText(); String text = textBoxPosition.getText();

341
FormTracktorConfig.form Normal file
View File

@ -0,0 +1,341 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormTracktorConfig">
<grid id="27dc6" binding="contentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="773" height="382"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="fdfc8" binding="toolsPanel" layout-manager="GridLayoutManager" row-count="7" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none" title="Параметры"/>
<children>
<component id="6962a" class="javax.swing.JLabel" binding="speedLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="27572"/>
<text value="Скорость:"/>
</properties>
</component>
<component id="27572" class="javax.swing.JSpinner" binding="speedSpinner">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="871d8" class="javax.swing.JLabel" binding="weightLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="8e1be"/>
<text value="Вес:"/>
</properties>
</component>
<component id="8e1be" class="javax.swing.JSpinner" binding="weightSpinner">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="39e94" class="javax.swing.JCheckBox" binding="checkBoxBucket">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия ковша"/>
</properties>
</component>
<component id="9d114" class="javax.swing.JCheckBox" binding="checkBoxSupports">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Признак наличия опорных стоек"/>
</properties>
</component>
<grid id="2044c" binding="colorsPanel" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="2" row-span="5" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none" title="Цвета"/>
<children>
<grid id="784a6" binding="redPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="60" height="60"/>
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-65536"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="abece" binding="greenPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="60" height="60"/>
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-16711936"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="b2eb9" binding="bluePanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="60" height="60"/>
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-16776961"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="482a4" binding="yellowPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="60" height="60"/>
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-723926"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="1692f" binding="whitePanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="60" height="60"/>
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-1"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="aaf16" binding="grayPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="60" height="60"/>
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-10527145"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="d1a3c" binding="blackPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="60" height="60"/>
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-16777216"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="e81e" binding="purplePanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="60" height="60"/>
<maximum-size width="60" height="60"/>
</grid>
</constraints>
<properties>
<background color="-5173091"/>
</properties>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
<grid id="f974a" binding="rollersPanel" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="0" row-span="1" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none" title="Катки"/>
<children>
<component id="a92b0" class="javax.swing.JLabel" binding="rollersLabel">
<constraints>
<grid row="0" column="0" row-span="3" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="be4e0"/>
<text value="Количество катков:"/>
</properties>
</component>
<component id="be4e0" class="javax.swing.JSpinner" binding="rollersSpinner">
<constraints>
<grid row="0" column="1" row-span="3" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<maximum-size width="120" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="9f9ab" class="javax.swing.JLabel" binding="baseRollersLabel">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="100" height="38"/>
<maximum-size width="100" height="38"/>
</grid>
</constraints>
<properties>
<font size="16"/>
<text value="Простые"/>
</properties>
</component>
<component id="ea1ae" class="javax.swing.JLabel" binding="crossRollersLabel">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="120" height="38"/>
<maximum-size width="120" height="38"/>
</grid>
</constraints>
<properties>
<font size="16"/>
<text value="С крестиками"/>
</properties>
</component>
<component id="15683" class="javax.swing.JLabel" binding="squaredRollersLabel">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="120" height="38"/>
<maximum-size width="120" height="38"/>
</grid>
</constraints>
<properties>
<font size="16"/>
<text value="С квадратами"/>
</properties>
</component>
</children>
</grid>
<component id="9f748" class="javax.swing.JLabel" binding="baseLabel">
<constraints>
<grid row="5" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="100" height="38"/>
<maximum-size width="100" height="38"/>
</grid>
</constraints>
<properties>
<background color="-4720538"/>
<font size="16"/>
<text value="Простой"/>
</properties>
</component>
<component id="7569d" class="javax.swing.JLabel" binding="advancedLabel">
<constraints>
<grid row="5" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="100" height="38"/>
<maximum-size width="120" height="38"/>
</grid>
</constraints>
<properties>
<font size="16"/>
<text value="Продвинутый"/>
</properties>
</component>
</children>
</grid>
<grid id="1a87d" binding="workspacePanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none" title="Предпоказ"/>
<children>
<component id="18903" class="javax.swing.JLabel" binding="colorLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="100" height="38"/>
<maximum-size width="100" height="38"/>
</grid>
</constraints>
<properties>
<font size="16"/>
<text value="Цвет"/>
</properties>
</component>
<component id="fc724" class="javax.swing.JLabel" binding="additionalColorLabel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="100" height="38"/>
<maximum-size width="100" height="38"/>
</grid>
</constraints>
<properties>
<font size="16"/>
<text value="Доп. цвет"/>
</properties>
</component>
<grid id="47b0f" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="200" height="200"/>
<maximum-size width="200" height="200"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
<component id="df1e6" class="javax.swing.JButton" binding="buttonAdd">
<constraints>
<grid row="1" column="1" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить"/>
</properties>
</component>
<component id="22a26" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="1" column="2" row-span="2" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Отмена"/>
</properties>
</component>
</children>
</grid>
</form>

153
FormTracktorConfig.java Normal file
View File

@ -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<DrawningTracktor> 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<DrawningTracktor> 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);
}
}
}

View File

@ -2,5 +2,6 @@ import java.awt.*;
public interface IDrawningRollers { public interface IDrawningRollers {
void setRollersCount(int count); void setRollersCount(int count);
void setColor(Color color);
void DrawRollers(Graphics2D g, float _startPosX, float _startPosY); void DrawRollers(Graphics2D g, float _startPosX, float _startPosY);
} }