Правки по работе DnD по передаче объектов.

This commit is contained in:
Programmist73 2022-11-21 23:43:44 +04:00
parent 5d93c8bf38
commit 55364bb619
3 changed files with 102 additions and 34 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormPlaneConfig">
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="1067" height="400"/>
@ -335,16 +335,6 @@
<text value="Доп. цвет"/>
</properties>
</component>
<component id="da776" class="javax.swing.JLabel" binding="LabelTypeWindow">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="Форма"/>
</properties>
</component>
<grid id="f0d81" binding="PictureBoxPlane" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -397,7 +387,7 @@
</grid>
</constraints>
</vspacer>
<hspacer id="296b8">
<hspacer id="f542d">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="5" height="-1"/>
@ -406,6 +396,15 @@
</grid>
</constraints>
</hspacer>
<hspacer id="2e630">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="5" height="-1"/>
<preferred-size width="5" height="-1"/>
<maximum-size width="5" height="-1"/>
</grid>
</constraints>
</hspacer>
</children>
</grid>
<buttonGroups>

View File

@ -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<DrawingPlane> EventAddPlane;
public void AddEvent(Consumer<DrawingPlane> ev){
//объект для контролирования работы DnD
Object dragObject;
//хранит в себе объект иллюминатор
Object enterObject;
public void AddEvent(Consumer<DrawingPlane> 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;
}
}
}

View File

@ -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);