312 lines
10 KiB
Java
312 lines
10 KiB
Java
import javax.swing.*;
|
||
import java.awt.*;
|
||
import java.awt.event.*;
|
||
import java.awt.image.BufferedImage;
|
||
import java.util.function.Consumer;
|
||
import java.awt.event.MouseAdapter;
|
||
import java.awt.event.MouseEvent;
|
||
import java.util.function.Consumer;
|
||
|
||
public class FormPlaneConfig extends JFrame{
|
||
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;
|
||
private JPanel PictureBoxPlane;
|
||
|
||
//переменная-выбранный самолёт
|
||
DrawingPlane _plane;
|
||
|
||
//событие
|
||
Consumer<DrawingPlane> EventAddPlane;
|
||
|
||
//объект для контролирования работы DnD
|
||
Object dragObject;
|
||
|
||
//хранит в себе объект иллюминатор
|
||
Object enterObject;
|
||
|
||
public void AddEvent(Consumer<DrawingPlane> ev)
|
||
{
|
||
EventAddPlane = ev;
|
||
}
|
||
|
||
//конструктор
|
||
public FormPlaneConfig()
|
||
{
|
||
super("Самолёт");
|
||
CreateWindow();
|
||
}
|
||
|
||
public void Draw(DrawingPlane _plane)
|
||
{
|
||
PictureBoxPlane.removeAll();
|
||
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();
|
||
imageOfLogo.setPreferredSize(PictureBoxPlane.getSize());
|
||
imageOfLogo.setMinimumSize(new Dimension(1, 1));
|
||
imageOfLogo.setIcon(new ImageIcon(bmp));
|
||
PictureBoxPlane.add(imageOfLogo, BorderLayout.CENTER);
|
||
}
|
||
|
||
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));
|
||
}
|
||
};
|
||
|
||
//настройки курсоров
|
||
PictureBoxPlane.addMouseListener(defCursor);
|
||
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);
|
||
|
||
//настройка DnD для перетаскивания формы иллюминаторов
|
||
AdditWindowDropObject(LabelSimpleWindow);
|
||
AdditWindowDropObject(LabelRectWindow);
|
||
AdditWindowDropObject(LabelTriangleWindow);
|
||
|
||
AdditWindowDropTarget(PictureBoxPlane);
|
||
|
||
//передача объекта через событие
|
||
ButtonAddObject.addActionListener(e -> {
|
||
EventAddPlane.accept(_plane);
|
||
dispose();
|
||
});
|
||
|
||
//лямбда-выражение для закрытия формы
|
||
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);
|
||
repaint();
|
||
}
|
||
|
||
if(LabelAddColor.getMousePosition() != null && _plane instanceof DrawingAirbus airbus)
|
||
{
|
||
airbus.SetAddColor(panel.getBackground());
|
||
Draw(_plane);
|
||
repaint();
|
||
}
|
||
}
|
||
|
||
if(dropItem instanceof JLabel label && PictureBoxPlane.getMousePosition() != null)
|
||
{
|
||
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);
|
||
repaint();
|
||
}
|
||
catch(Exception ex) { }
|
||
}
|
||
else if(label == LabelModifiedObject)
|
||
{
|
||
try
|
||
{
|
||
_plane = new DrawingAirbus(speed, weight, Color.WHITE, countWindow, Color.WHITE, addCompartment, addEngine);
|
||
Draw(_plane);
|
||
repaint();
|
||
}
|
||
catch (Exception ex) { };
|
||
}
|
||
|
||
if(_plane != null)
|
||
{
|
||
_plane.SetPosition(PictureBoxPlane.getWidth() - 200, PictureBoxPlane.getHeight() - 150, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight());
|
||
PictureBoxPlane.add(_plane, BorderLayout.CENTER);
|
||
Draw(_plane);
|
||
repaint();
|
||
revalidate();
|
||
}
|
||
}
|
||
}
|
||
|
||
void AdditWindowDropTarget(JComponent obj)
|
||
{
|
||
obj.addMouseListener(new MouseAdapter() {
|
||
@Override
|
||
public void mouseEntered(MouseEvent e) {super.mouseEntered(e);
|
||
Window_DragEnter(obj);
|
||
}
|
||
@Override
|
||
public void mouseExited(MouseEvent e) {super.mouseExited(e);
|
||
DragExit();
|
||
}
|
||
});
|
||
}
|
||
|
||
void AdditWindowDropObject(JComponent obj)
|
||
{
|
||
obj.addMouseListener(new MouseAdapter() {
|
||
@Override
|
||
public void mousePressed(MouseEvent e) {super.mousePressed(e);
|
||
Window_MouseDown(obj);
|
||
}
|
||
@Override
|
||
public void mouseReleased(MouseEvent e) {super.mouseReleased(e);
|
||
Window_DragDrop();
|
||
}
|
||
});
|
||
}
|
||
|
||
void Window_MouseDown(Object sender)
|
||
{
|
||
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;
|
||
}
|
||
|
||
void Window_DragEnter(Object sender)
|
||
{
|
||
if(enterObject != null && IAdditionalDrawingObject.class.isAssignableFrom(enterObject.getClass()) && _plane != null)
|
||
{
|
||
setCursor(new Cursor(Cursor.HAND_CURSOR));
|
||
dragObject = sender;
|
||
}
|
||
}
|
||
|
||
void Window_DragDrop()
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
}
|