PIbd-23_Mochalov_D.V._Locom.../FormLocomotiveConfig.java

159 lines
6.5 KiB
Java
Raw Normal View History

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Consumer;
public class FormLocomotiveConfig extends JFrame{
// Рабочие поля
private DrawningLocomotive locomotive;
private final EventListener<DrawningLocomotive> eventListener = new EventListener<>();
//Элементы формы
private JPanel FormPanel;
private JPanel ParametersPanel;
private JPanel CreatePanel;
private JPanel ColorPanel;
private JPanel StatsPanel;
private JLabel SpeedLabel;
private JSpinner SpeedSpinner;
private JLabel WeightLabel;
private JSpinner WeightSpinner;
private JCheckBox PipeCheckBox;
private JCheckBox FuelCheckBox;
private JLabel SimpleObjectLabel;
private JLabel AdvancedObjectLabel;
private JLabel SimpleWheelsLabel;
private JLabel StarWheelsLabel;
private JLabel RoundWheelsLabel;
private JSpinner WheelsCountSpinner;
private JLabel WheelsCountLabel;
private JPanel RedColorPanel;
private JPanel GreenColorPanel;
private JPanel BlueColorPanel;
private JPanel YellowColorPanel;
private JPanel WhiteColorPanel;
private JPanel BlackColorPanel;
private JPanel AquaColorPanel;
private JPanel PurpleColorPanel;
private JLabel MainColorLabel;
private JLabel ExtraColorLabel;
private JPanel ObjectViewPanel;
private JButton AddButton;
private JButton CancelButton;
public FormLocomotiveConfig() {
// Добавили цветные границы
SimpleObjectLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
AdvancedObjectLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
SimpleWheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
RoundWheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
StarWheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
MainColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
ExtraColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// Модели для намериков
SpeedSpinner.setModel(new SpinnerNumberModel(100, 50, 1000, 10));
WeightSpinner.setModel(new SpinnerNumberModel(1000, 1000, 5000, 10));
WheelsCountSpinner.setModel(new SpinnerNumberModel(2, 2, 4, 1));
//Обработчик d&d
var DragDropAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mouseReleased(e);
}
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
Drop((JComponent) e.getSource());
}
};
SimpleObjectLabel.addMouseListener(DragDropAdapter);
AdvancedObjectLabel.addMouseListener(DragDropAdapter);
StarWheelsLabel.addMouseListener(DragDropAdapter);
RoundWheelsLabel.addMouseListener(DragDropAdapter);
StarWheelsLabel.addMouseListener(DragDropAdapter);
RedColorPanel.addMouseListener(DragDropAdapter);
GreenColorPanel.addMouseListener(DragDropAdapter);
BlueColorPanel.addMouseListener(DragDropAdapter);
YellowColorPanel.addMouseListener(DragDropAdapter);
WhiteColorPanel.addMouseListener(DragDropAdapter);
BlackColorPanel.addMouseListener(DragDropAdapter);
AquaColorPanel.addMouseListener(DragDropAdapter);
PurpleColorPanel.addMouseListener(DragDropAdapter);
AddButton.addActionListener(e -> {
eventListener.Emit(locomotive);
dispose();
});
CancelButton.addActionListener(e -> dispose());
// Параметры фрейма
setContentPane(FormPanel);
setSize(800, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
// Drop обработка
public void Drop (JComponent component) {
if (component == null) {
return;
}
if (component instanceof JPanel panel) {
if (MainColorLabel.getMousePosition() != null) {
locomotive.SetColor(panel.getBackground());
locomotive.drawningExtra.SetColor(panel.getBackground());
}
if (ExtraColorLabel.getMousePosition() != null && locomotive instanceof DrawningWarmlyLocomotive warmlyLocomotive) {
warmlyLocomotive.SetExtraColor(panel.getBackground());
}
}
if (component instanceof JLabel label && ObjectViewPanel.getMousePosition() != null) {
int speed = (Integer) SpeedSpinner.getValue();
int weight = (Integer) WeightSpinner.getValue();
int wheelsCount = (Integer) WheelsCountSpinner.getValue();
boolean pipe = PipeCheckBox.isSelected();
boolean fuel = FuelCheckBox.isSelected();
if (label == SimpleObjectLabel) {
locomotive = new DrawningLocomotive(speed, weight, Color.WHITE, wheelsCount);
} else if (label == AdvancedObjectLabel) {
locomotive = new DrawningWarmlyLocomotive(speed, weight, Color.WHITE, wheelsCount, Color.WHITE, pipe, fuel);
} else if (locomotive != null && label == SimpleWheelsLabel) {
locomotive.drawningExtra = new ExtraWheelsDraw(wheelsCount, locomotive.Locomotive.getBodyColor());
locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor());
} else if (locomotive != null && label == StarWheelsLabel) {
locomotive.drawningExtra = new ExtraStarWheelDraw(wheelsCount, locomotive.Locomotive.getBodyColor());
locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor());
} else if (locomotive != null && label == RoundWheelsLabel) {
locomotive.drawningExtra = new ExtraRoundWheelDraw(wheelsCount, locomotive.Locomotive.getBodyColor());
locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor());
}
}
repaint();
}
public void AddListener(Consumer<DrawningLocomotive> listener) {
eventListener.Add(listener);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (locomotive != null) {
g = ObjectViewPanel.getGraphics();
locomotive.SetPosition(20, 20, ObjectViewPanel.getWidth(), ObjectViewPanel.getHeight());
locomotive.DrawTransport((Graphics2D) g);
}
}
}