diff --git a/Project/src/FormPlaneConfig.form b/Project/src/FormPlaneConfig.form index 7566cae..e0549e8 100644 --- a/Project/src/FormPlaneConfig.form +++ b/Project/src/FormPlaneConfig.form @@ -1,6 +1,6 @@
- + @@ -335,16 +335,6 @@ - - - - - - - - - - @@ -397,7 +387,7 @@ - + @@ -406,6 +396,15 @@ + + + + + + + + + diff --git a/Project/src/FormPlaneConfig.java b/Project/src/FormPlaneConfig.java index 2dfcd85..4ca8353 100644 --- a/Project/src/FormPlaneConfig.java +++ b/Project/src/FormPlaneConfig.java @@ -35,7 +35,6 @@ public class FormPlaneConfig extends JFrame{ private JPanel GroubBoxSelectedCountWindow; private JSpinner NumericUpDownCountWindow; private JPanel MainPanel; - private JLabel LabelTypeWindow; private JLabel LabelSimpleWindow; private JLabel LabelRectWindow; private JLabel LabelTriangleWindow; @@ -47,7 +46,14 @@ public class FormPlaneConfig extends JFrame{ //событие Consumer EventAddPlane; - public void AddEvent(Consumer ev){ + //объект для контролирования работы DnD + Object dragObject; + + //хранит в себе объект иллюминатор + Object enterObject; + + public void AddEvent(Consumer ev) + { EventAddPlane = ev; } @@ -86,7 +92,6 @@ public class FormPlaneConfig extends JFrame{ //рамки у Label LabelAddColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); - LabelTypeWindow.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)); @@ -123,7 +128,6 @@ public class FormPlaneConfig extends JFrame{ PictureBoxPlane.addMouseListener(defCursor); LabelBaseColor.addMouseListener(defCursor); LabelAddColor.addMouseListener(defCursor); - LabelTypeWindow.addMouseListener(defCursor); PanelBlack.addMouseListener(drag); PanelPurple.addMouseListener(drag); @@ -140,12 +144,20 @@ public class FormPlaneConfig extends JFrame{ LabelRectWindow.addMouseListener(drag); LabelTriangleWindow.addMouseListener(drag); + //передача объекта через событие ButtonAddObject.addActionListener(e -> { EventAddPlane.accept(_plane); dispose(); }); + //лямбда-выражение для закрытия формы ButtonCancel.addActionListener(e -> dispose()); + + AdditWindowDropObject(LabelSimpleWindow); + AdditWindowDropObject(LabelRectWindow); + AdditWindowDropObject(LabelTriangleWindow); + + AdditWindowDropTarget(PictureBoxPlane); } public void Drop(JComponent dropItem) @@ -215,27 +227,84 @@ public class FormPlaneConfig extends JFrame{ revalidate(); } } - else if(dropItem instanceof JLabel label && LabelTypeWindow.getMousePosition() != null && _plane!=null) + } + + void AdditWindowDropTarget(JComponent obj) + { + obj.addMouseListener(new MouseAdapter() { + @Override + public void mouseEntered(MouseEvent e) {super.mouseEntered(e); + Paddle_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); + Paddle_MouseDown(obj); + } + @Override + public void mouseReleased(MouseEvent e) {super.mouseReleased(e); + Paddle_DragDrop(); + } + }); + } + + void Paddle_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 Paddle_DragEnter(Object sender) + { + if(enterObject != null && IAdditionalDrawingObject.class.isAssignableFrom(enterObject.getClass()) && _plane != null) { - if(label == LabelSimpleWindow) - { - _plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingAirplaneWindow()); - Draw(_plane); - } + setCursor(new Cursor(Cursor.HAND_CURSOR)); + dragObject = sender; + } + } - if(label == LabelRectWindow) - { - _plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingRectAirplaneWindow()); - Draw(_plane); - } + void Paddle_DragDrop() + { + if(dragObject == null) + { + return; + } - if(label == LabelTriangleWindow) - { - _plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingTriangleAirplaneWindow()); - Draw(_plane); - } + _plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), (IAdditionalDrawingObject)enterObject); + DragExit(); + enterObject = null; + } - repaint(); + void DragExit() + { + if(enterObject != null && dragObject != null) + { + setCursor(Cursor.getDefaultCursor()); + Draw(_plane); + dragObject = null; } } } diff --git a/Project/src/IAdditionalDrawingObject.java b/Project/src/IAdditionalDrawingObject.java index cf9f27b..7b47141 100644 --- a/Project/src/IAdditionalDrawingObject.java +++ b/Project/src/IAdditionalDrawingObject.java @@ -1,6 +1,6 @@ import java.awt.*; -public interface IAdditionalDrawingObject +public interface IAdditionalDrawingObject { void SetAddEnum(int airplaneWindow); void DrawAirplaneWindow(Color colorAirplaneWindow, Graphics g, float _startPosX, float _startPosY);