161 lines
6.4 KiB
Java
161 lines
6.4 KiB
Java
|
import javax.swing.*;
|
||
|
import java.awt.*;
|
||
|
import java.awt.event.FocusEvent;
|
||
|
import java.awt.event.MouseAdapter;
|
||
|
import java.awt.event.MouseEvent;
|
||
|
import java.awt.event.WindowEvent;
|
||
|
import java.util.function.Consumer;
|
||
|
|
||
|
public class FormArtilleryConfig extends JFrame {
|
||
|
|
||
|
private final EventListener<DrawingArtillery> eventHandler = new EventListener<>();
|
||
|
private DrawingArtillery artillery;
|
||
|
private JComponent draggedComponent;
|
||
|
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 checkBoxWeapon;
|
||
|
private JCheckBox checkBoxSalvoBattery;
|
||
|
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 FormArtilleryConfig() {
|
||
|
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 form = this;
|
||
|
var dragAdapter = new MouseAdapter() {
|
||
|
@Override
|
||
|
public void mousePressed(MouseEvent e) {
|
||
|
super.mouseReleased(e);
|
||
|
form.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
||
|
draggedComponent = (JComponent) e.getSource();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void mouseReleased(MouseEvent e) {
|
||
|
super.mouseReleased(e);
|
||
|
dispatchDrop();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// Задание обработчиков
|
||
|
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(artillery);
|
||
|
form.dispose();
|
||
|
});
|
||
|
buttonCancel.addActionListener(e -> {
|
||
|
form.dispose();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public void addListener(Consumer<DrawingArtillery> listener) {
|
||
|
eventHandler.addListener(listener);
|
||
|
}
|
||
|
|
||
|
public void dispatchDrop() {
|
||
|
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
||
|
if (draggedComponent == null) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (draggedComponent instanceof JPanel panel) {
|
||
|
if (colorLabel.getMousePosition() != null) {
|
||
|
artillery.setColor(panel.getBackground());
|
||
|
artillery.getRollers().setColor(panel.getBackground());
|
||
|
}
|
||
|
if (additionalColorLabel.getMousePosition() != null && artillery instanceof DrawingAdvancedArtillery advanced) {
|
||
|
advanced.setDopColor(panel.getBackground());
|
||
|
}
|
||
|
}
|
||
|
if (draggedComponent instanceof JLabel label && pictureBox.getMousePosition() != null) {
|
||
|
int speed = (Integer) speedSpinner.getValue();
|
||
|
int weight = (Integer) weightSpinner.getValue();
|
||
|
int rollersCount = (Integer) rollersSpinner.getValue();
|
||
|
boolean weapon = checkBoxWeapon.isSelected();
|
||
|
boolean salvoBattery = checkBoxSalvoBattery.isSelected();
|
||
|
|
||
|
if (label == baseLabel) {
|
||
|
artillery = new DrawingArtillery(speed, weight, Color.WHITE, rollersCount);
|
||
|
} else if (label == advancedLabel) {
|
||
|
artillery = new DrawingAdvancedArtillery(speed, weight, Color.WHITE, rollersCount, Color.WHITE, weapon, salvoBattery);
|
||
|
} else if (artillery != null && label == baseRollersLabel) {
|
||
|
artillery.setRollers(new DrawingRollers(rollersCount, artillery.getArtillery().getBodyColor()));
|
||
|
} else if (artillery != null && label == crossRollersLabel) {
|
||
|
artillery.setRollers(new DrawingCrossRollers(rollersCount, artillery.getArtillery().getBodyColor()));
|
||
|
} else if (artillery != null && label == squaredRollersLabel) {
|
||
|
artillery.setRollers(new DrawingSquaredRollers(rollersCount, artillery.getArtillery().getBodyColor()));
|
||
|
}
|
||
|
}
|
||
|
draggedComponent = null;
|
||
|
repaint();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void paint(Graphics g) {
|
||
|
super.paint(g);
|
||
|
if (artillery != null) {
|
||
|
g = pictureBox.getGraphics();
|
||
|
artillery.setPosition(60, 75, pictureBox.getWidth(), pictureBox.getHeight());
|
||
|
artillery.drawTransport((Graphics2D) g);
|
||
|
}
|
||
|
}
|
||
|
}
|