2022-11-06 20:33:15 +04:00
|
|
|
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 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 dragAdapter = new MouseAdapter() {
|
|
|
|
@Override
|
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
|
super.mouseReleased(e);
|
2022-11-06 21:56:53 +04:00
|
|
|
setCursor(new Cursor(Cursor.HAND_CURSOR));
|
2022-11-06 20:33:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
|
super.mouseReleased(e);
|
2022-11-07 15:32:44 +04:00
|
|
|
dispatchDrop((JComponent) e.getSource());
|
2022-11-06 20:33:15 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Задание обработчиков
|
|
|
|
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);
|
2022-11-06 21:56:53 +04:00
|
|
|
dispose();
|
2022-11-06 20:33:15 +04:00
|
|
|
});
|
2022-11-06 21:56:53 +04:00
|
|
|
buttonCancel.addActionListener(e -> dispose());
|
2022-11-06 20:33:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addListener(Consumer<DrawingArtillery> listener) {
|
|
|
|
eventHandler.addListener(listener);
|
|
|
|
}
|
|
|
|
|
2022-11-07 15:32:44 +04:00
|
|
|
public void dispatchDrop(JComponent droppedComponent) {
|
2022-11-06 20:33:15 +04:00
|
|
|
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
2022-11-07 15:32:44 +04:00
|
|
|
if (droppedComponent == null) {
|
2022-11-06 20:33:15 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-07 15:32:44 +04:00
|
|
|
if (droppedComponent instanceof JPanel panel) {
|
2022-11-06 20:33:15 +04:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 15:32:44 +04:00
|
|
|
if (droppedComponent instanceof JLabel label && pictureBox.getMousePosition() != null) {
|
2022-11-06 20:33:15 +04:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 15:32:44 +04:00
|
|
|
|
2022-11-06 20:33:15 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|