154 lines
6.2 KiB
Java
154 lines
6.2 KiB
Java
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);
|
|
}
|
|
}
|
|
}
|