2022-11-18 16:31:49 +04:00
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.*;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
2022-11-18 18:38:30 +04:00
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
|
import java.awt.event.MouseEvent;
|
2022-11-18 16:31:49 +04:00
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
2022-11-18 18:38:30 +04:00
|
|
|
|
public class FormPlaneConfig extends JFrame{
|
2022-11-18 16:31:49 +04:00
|
|
|
|
private JButton ButtonAddObject;
|
|
|
|
|
private JButton ButtonCancel;
|
|
|
|
|
private JPanel GroupBoxConfig;
|
|
|
|
|
private JPanel PanelObject;
|
|
|
|
|
private JPanel GroupBoxColors;
|
|
|
|
|
private JPanel PanelRed;
|
|
|
|
|
private JPanel PanelGreen;
|
|
|
|
|
private JPanel PanelBlue;
|
|
|
|
|
private JPanel PanelYellow;
|
|
|
|
|
private JPanel PanelPurple;
|
|
|
|
|
private JPanel PanelBlack;
|
|
|
|
|
private JPanel PanelGray;
|
|
|
|
|
private JPanel PanelWhite;
|
|
|
|
|
private JLabel LabelSimpleObject;
|
|
|
|
|
private JLabel LabelModifiedObject;
|
|
|
|
|
private JLabel LabelBaseColor;
|
|
|
|
|
private JLabel LabelAddColor;
|
|
|
|
|
private JLabel LabelSpeed;
|
|
|
|
|
private JLabel LabelWeight;
|
|
|
|
|
private JSpinner NumericUpDownWeight;
|
|
|
|
|
private JSpinner NumericUpDownSpeed;
|
|
|
|
|
private JCheckBox CheckBoxAddСompartment;
|
|
|
|
|
private JCheckBox CheckBoxAddEngine;
|
|
|
|
|
private JPanel GroupBoxSelectPlaneWindow;
|
|
|
|
|
private JPanel GroubBoxSelectedCountWindow;
|
|
|
|
|
private JSpinner NumericUpDownCountWindow;
|
|
|
|
|
private JPanel MainPanel;
|
|
|
|
|
private JLabel LabelSimpleWindow;
|
|
|
|
|
private JLabel LabelRectWindow;
|
|
|
|
|
private JLabel LabelTriangleWindow;
|
2022-11-18 18:38:30 +04:00
|
|
|
|
private JPanel PictureBoxPlane;
|
2022-11-18 16:31:49 +04:00
|
|
|
|
|
|
|
|
|
//переменная-выбранный самолёт
|
|
|
|
|
DrawingPlane _plane;
|
|
|
|
|
|
|
|
|
|
//событие
|
|
|
|
|
Consumer<DrawingPlane> EventAddPlane;
|
|
|
|
|
|
2022-11-21 23:56:37 +04:00
|
|
|
|
//объект для контролирования работы DnD
|
|
|
|
|
Object dragObject;
|
|
|
|
|
|
|
|
|
|
//хранит в себе объект иллюминатор
|
|
|
|
|
Object enterObject;
|
|
|
|
|
|
|
|
|
|
public void AddEvent(Consumer<DrawingPlane> ev)
|
|
|
|
|
{
|
2022-11-18 16:31:49 +04:00
|
|
|
|
EventAddPlane = ev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//конструктор
|
|
|
|
|
public FormPlaneConfig()
|
|
|
|
|
{
|
2022-11-18 18:38:30 +04:00
|
|
|
|
super("Самолёт");
|
2022-11-18 16:31:49 +04:00
|
|
|
|
CreateWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Draw(DrawingPlane _plane)
|
|
|
|
|
{
|
2022-11-18 18:38:30 +04:00
|
|
|
|
PictureBoxPlane.removeAll();
|
2022-11-18 16:31:49 +04:00
|
|
|
|
BufferedImage bmp = new BufferedImage(250, 200, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
Graphics gr = bmp.getGraphics();
|
|
|
|
|
|
|
|
|
|
gr.setColor(new Color(238, 238, 238));
|
|
|
|
|
gr.fillRect(0, 0, 250, 200);
|
|
|
|
|
|
|
|
|
|
if (_plane.GetPlane() != null) {
|
|
|
|
|
_plane.DrawTransport(gr);
|
|
|
|
|
JLabel imageOfLogo = new JLabel();
|
2022-11-18 18:38:30 +04:00
|
|
|
|
imageOfLogo.setPreferredSize(PictureBoxPlane.getSize());
|
2022-11-18 16:31:49 +04:00
|
|
|
|
imageOfLogo.setMinimumSize(new Dimension(1, 1));
|
|
|
|
|
imageOfLogo.setIcon(new ImageIcon(bmp));
|
2022-11-18 18:38:30 +04:00
|
|
|
|
PictureBoxPlane.add(imageOfLogo, BorderLayout.CENTER);
|
2022-11-18 16:31:49 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateWindow()
|
|
|
|
|
{
|
|
|
|
|
setPreferredSize(new Dimension(1000, 700));
|
|
|
|
|
getContentPane().add(MainPanel);
|
|
|
|
|
|
|
|
|
|
//рамки у Label
|
|
|
|
|
LabelAddColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
|
|
|
|
|
LabelBaseColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
|
|
|
|
|
LabelModifiedObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
|
|
|
|
|
LabelSimpleObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
|
|
|
|
|
LabelSimpleWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
|
|
|
|
|
LabelRectWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
|
|
|
|
|
LabelTriangleWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
|
|
|
|
|
|
|
|
|
|
//настройка значений у NumericUpDown
|
|
|
|
|
NumericUpDownSpeed.setModel(new SpinnerNumberModel(1000, 1000, 3000, 1));
|
|
|
|
|
NumericUpDownWeight.setModel(new SpinnerNumberModel(750, 100, 1500, 1));
|
|
|
|
|
NumericUpDownCountWindow.setModel(new SpinnerNumberModel(1, 1, 3, 1));
|
|
|
|
|
|
|
|
|
|
MouseAdapter drag = new MouseAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
|
|
setCursor(new Cursor(Cursor.HAND_CURSOR));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
|
|
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
|
|
|
|
Drop((JComponent) e.getSource());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MouseAdapter defCursor = new MouseAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseExited(MouseEvent e) {
|
|
|
|
|
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//настройки курсоров
|
2022-11-18 18:38:30 +04:00
|
|
|
|
PictureBoxPlane.addMouseListener(defCursor);
|
2022-11-18 16:31:49 +04:00
|
|
|
|
LabelBaseColor.addMouseListener(defCursor);
|
|
|
|
|
LabelAddColor.addMouseListener(defCursor);
|
|
|
|
|
|
|
|
|
|
PanelBlack.addMouseListener(drag);
|
|
|
|
|
PanelPurple.addMouseListener(drag);
|
|
|
|
|
PanelGray.addMouseListener(drag);
|
|
|
|
|
PanelGreen.addMouseListener(drag);
|
|
|
|
|
PanelRed.addMouseListener(drag);
|
|
|
|
|
PanelWhite.addMouseListener(drag);
|
|
|
|
|
PanelYellow.addMouseListener(drag);
|
|
|
|
|
PanelBlue.addMouseListener(drag);
|
|
|
|
|
|
|
|
|
|
LabelSimpleObject.addMouseListener(drag);
|
|
|
|
|
LabelModifiedObject.addMouseListener(drag);
|
|
|
|
|
LabelSimpleWindow.addMouseListener(drag);
|
|
|
|
|
LabelRectWindow.addMouseListener(drag);
|
|
|
|
|
LabelTriangleWindow.addMouseListener(drag);
|
|
|
|
|
|
2022-11-24 23:25:21 +04:00
|
|
|
|
//настройка DnD для перетаскивания формы иллюминаторов
|
|
|
|
|
AdditWindowDropObject(LabelSimpleWindow);
|
|
|
|
|
AdditWindowDropObject(LabelRectWindow);
|
|
|
|
|
AdditWindowDropObject(LabelTriangleWindow);
|
|
|
|
|
|
|
|
|
|
AdditWindowDropTarget(PictureBoxPlane);
|
|
|
|
|
|
2022-11-21 23:56:37 +04:00
|
|
|
|
//передача объекта через событие
|
2022-11-18 16:31:49 +04:00
|
|
|
|
ButtonAddObject.addActionListener(e -> {
|
|
|
|
|
EventAddPlane.accept(_plane);
|
|
|
|
|
dispose();
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-21 23:56:37 +04:00
|
|
|
|
//лямбда-выражение для закрытия формы
|
2022-11-18 16:31:49 +04:00
|
|
|
|
ButtonCancel.addActionListener(e -> dispose());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Drop(JComponent dropItem)
|
|
|
|
|
{
|
|
|
|
|
if(dropItem == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(dropItem instanceof JPanel panel)
|
|
|
|
|
{
|
|
|
|
|
if(_plane == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(LabelBaseColor.getMousePosition() != null)
|
|
|
|
|
{
|
|
|
|
|
_plane.SetColor(panel.getBackground());
|
|
|
|
|
Draw(_plane);
|
2022-11-18 18:38:30 +04:00
|
|
|
|
repaint();
|
2022-11-18 16:31:49 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(LabelAddColor.getMousePosition() != null && _plane instanceof DrawingAirbus airbus)
|
|
|
|
|
{
|
2022-11-18 18:45:28 +04:00
|
|
|
|
airbus.SetAddColor(panel.getBackground());
|
2022-11-18 16:31:49 +04:00
|
|
|
|
Draw(_plane);
|
2022-11-18 18:38:30 +04:00
|
|
|
|
repaint();
|
2022-11-18 16:31:49 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-18 18:38:30 +04:00
|
|
|
|
if(dropItem instanceof JLabel label && PictureBoxPlane.getMousePosition() != null)
|
2022-11-18 16:31:49 +04:00
|
|
|
|
{
|
|
|
|
|
int speed = (int)NumericUpDownSpeed.getValue();
|
|
|
|
|
int weight = (int)NumericUpDownWeight.getValue();
|
|
|
|
|
int countWindow = (int)NumericUpDownCountWindow.getValue();
|
|
|
|
|
boolean addCompartment = CheckBoxAddСompartment.isSelected();
|
|
|
|
|
boolean addEngine = CheckBoxAddEngine.isSelected();
|
|
|
|
|
|
|
|
|
|
if(label == LabelSimpleObject)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_plane = new DrawingPlane(speed, weight, Color.WHITE, countWindow);
|
|
|
|
|
Draw(_plane);
|
2022-11-18 18:38:30 +04:00
|
|
|
|
repaint();
|
2022-11-18 16:31:49 +04:00
|
|
|
|
}
|
|
|
|
|
catch(Exception ex) { }
|
|
|
|
|
}
|
|
|
|
|
else if(label == LabelModifiedObject)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_plane = new DrawingAirbus(speed, weight, Color.WHITE, countWindow, Color.WHITE, addCompartment, addEngine);
|
|
|
|
|
Draw(_plane);
|
2022-11-18 18:38:30 +04:00
|
|
|
|
repaint();
|
2022-11-18 16:31:49 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(_plane != null)
|
|
|
|
|
{
|
2022-11-18 18:38:30 +04:00
|
|
|
|
_plane.SetPosition(PictureBoxPlane.getWidth() - 200, PictureBoxPlane.getHeight() - 150, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
|
|
|
|
|
PictureBoxPlane.add(_plane, BorderLayout.CENTER);
|
2022-11-18 16:31:49 +04:00
|
|
|
|
Draw(_plane);
|
2022-11-18 18:38:30 +04:00
|
|
|
|
repaint();
|
|
|
|
|
revalidate();
|
2022-11-18 16:31:49 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-21 23:56:37 +04:00
|
|
|
|
}
|
2022-11-18 16:31:49 +04:00
|
|
|
|
|
2022-11-21 23:56:37 +04:00
|
|
|
|
void AdditWindowDropTarget(JComponent obj)
|
|
|
|
|
{
|
|
|
|
|
obj.addMouseListener(new MouseAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseEntered(MouseEvent e) {super.mouseEntered(e);
|
2022-11-24 23:25:21 +04:00
|
|
|
|
Window_DragEnter(obj);
|
2022-11-21 23:56:37 +04:00
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseExited(MouseEvent e) {super.mouseExited(e);
|
|
|
|
|
DragExit();
|
2022-11-18 16:31:49 +04:00
|
|
|
|
}
|
2022-11-21 23:56:37 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
2022-11-18 16:31:49 +04:00
|
|
|
|
|
2022-11-21 23:56:37 +04:00
|
|
|
|
void AdditWindowDropObject(JComponent obj)
|
|
|
|
|
{
|
|
|
|
|
obj.addMouseListener(new MouseAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void mousePressed(MouseEvent e) {super.mousePressed(e);
|
2022-11-24 23:25:21 +04:00
|
|
|
|
Window_MouseDown(obj);
|
2022-11-21 23:56:37 +04:00
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void mouseReleased(MouseEvent e) {super.mouseReleased(e);
|
2022-11-24 23:25:21 +04:00
|
|
|
|
Window_DragDrop();
|
2022-11-18 16:31:49 +04:00
|
|
|
|
}
|
2022-11-21 23:56:37 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 23:25:21 +04:00
|
|
|
|
void Window_MouseDown(Object sender)
|
2022-11-21 23:56:37 +04:00
|
|
|
|
{
|
|
|
|
|
IAdditionalDrawingObject windows;
|
|
|
|
|
switch (((JLabel)sender).getText()){
|
|
|
|
|
case "Простая":
|
|
|
|
|
windows = new DrawingAirplaneWindow();
|
|
|
|
|
break;
|
|
|
|
|
case "Треугольная":
|
|
|
|
|
windows = new DrawingTriangleAirplaneWindow();
|
|
|
|
|
break;
|
|
|
|
|
case "Квадратная":
|
|
|
|
|
windows = new DrawingRectAirplaneWindow();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enterObject = windows;
|
|
|
|
|
}
|
2022-11-18 18:38:30 +04:00
|
|
|
|
|
2022-11-24 23:25:21 +04:00
|
|
|
|
void Window_DragEnter(Object sender)
|
2022-11-21 23:56:37 +04:00
|
|
|
|
{
|
|
|
|
|
if(enterObject != null && IAdditionalDrawingObject.class.isAssignableFrom(enterObject.getClass()) && _plane != null)
|
|
|
|
|
{
|
|
|
|
|
setCursor(new Cursor(Cursor.HAND_CURSOR));
|
|
|
|
|
dragObject = sender;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 23:25:21 +04:00
|
|
|
|
void Window_DragDrop()
|
2022-11-21 23:56:37 +04:00
|
|
|
|
{
|
|
|
|
|
if(dragObject == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), (IAdditionalDrawingObject)enterObject);
|
|
|
|
|
DragExit();
|
|
|
|
|
enterObject = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragExit()
|
|
|
|
|
{
|
|
|
|
|
if(enterObject != null && dragObject != null)
|
|
|
|
|
{
|
|
|
|
|
setCursor(Cursor.getDefaultCursor());
|
|
|
|
|
Draw(_plane);
|
|
|
|
|
dragObject = null;
|
2022-11-18 16:31:49 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|