Работает Drag&Drop

This commit is contained in:
Никита Потапов 2023-12-05 11:32:33 +04:00
parent be38dbc017
commit b8906c0325
6 changed files with 620 additions and 166 deletions

View File

@ -201,5 +201,10 @@ public class DrawingPlane extends JPanel {
return new DrawingObjectPlane(this);
}
public void SetDrawingBounds(int width, int height) {
_pictureWidth = width;
_pictureHeight = height;
}
;
}

View File

@ -16,4 +16,8 @@ public class EntityPlane {
Weight = weight;
BodyColor = bodyColor;
}
public void SetBodyColor(Color color) {
BodyColor = color;
}
}

View File

@ -14,4 +14,8 @@ public class EntityStormtrooper extends EntityPlane {
Rockets = rockets;
Bombs = bombs;
}
public void SetAdditionalColor(Color color) {
AdditionalColor = color;
}
}

View File

@ -97,34 +97,44 @@ public class FormPlaneCollection {
}
public void buttonAddPlaneClicked(ActionEvent e) {
FrameStormtrooper frameConfig = new FrameStormtrooper();
FormPlaneConfig formConfig = new FormPlaneConfig();
frameConfig.setContentPane(formConfig.panelWrapper);
formConfig.buttonApply.addActionListener(ev -> {
AddPlaneListener(formConfig.selectedPlane);
frameConfig.dispose();
});
formConfig.buttonCancel.addActionListener(ev -> {
frameConfig.dispose();
});
frameConfig.setVisible(true);
}
public void AddPlaneListener(DrawingPlane plane) {
if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
plane.SetDrawingBounds(PictureBoxCollection.getWidth(), PictureBoxCollection.getHeight());
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
if (obj == null) {
return;
}
FrameStormtrooper frameStormtrooper = new FrameStormtrooper();
frameStormtrooper.setVisible(true);
frameStormtrooper._formStromtrooper.buttonSelectPlane.addActionListener(ev -> {
SelectedPlane = frameStormtrooper._formStromtrooper._drawingPlane;
frameStormtrooper.dispose();
if (SelectedPlane != null) {
if (obj.Add(SelectedPlane) > -1) {
refreshPictureBox();
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Объект добавлен",
"Успех",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Не удалось добавить объект",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}
SelectedPlane = plane;
if (SelectedPlane != null) {
if (obj.Add(SelectedPlane) > -1) {
refreshPictureBox();
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Объект добавлен",
"Успех",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Не удалось добавить объект",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}
});
}
}
public void buttonRemovePlaneClicked(ActionEvent e) {

View File

@ -0,0 +1,339 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectStormtrooper.FormPlaneConfig">
<grid id="27dc6" binding="panelWrapper" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="33" y="106" width="1116" height="569"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="4095a" binding="panelParams" layout-manager="GridLayoutManager" row-count="6" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="58a35" class="javax.swing.JLabel" binding="labelSpeed">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость"/>
</properties>
</component>
<hspacer id="baa2">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="2d1d6" class="javax.swing.JLabel" binding="labelWeight">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес"/>
</properties>
</component>
<component id="85fac" class="javax.swing.JSpinner" binding="spinnerSpeed">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="8ffa1" class="javax.swing.JSpinner" binding="spinnerWeight">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="efbfc" class="javax.swing.JCheckBox" binding="checkBoxRockets">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Ракеты"/>
<text value="Ракеты"/>
</properties>
</component>
<component id="6cca0" class="javax.swing.JCheckBox" binding="checkBoxBombs">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<label value="Бомбы"/>
<text value="Бомбы"/>
</properties>
</component>
<component id="75e45" class="javax.swing.JButton" binding="buttonApply">
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить"/>
</properties>
</component>
<component id="85e09" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="5" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Отмена"/>
</properties>
</component>
<component id="b7976" class="javax.swing.JLabel" binding="labelEnginesCount">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Двигателей"/>
</properties>
</component>
<component id="eacb0" class="javax.swing.JSpinner" binding="spinnerEnginesCount">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="2bd5a" class="javax.swing.JLabel" binding="labelPlane">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Простой"/>
</properties>
</component>
<component id="1764d" class="javax.swing.JLabel" binding="labelStormtrooper">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Продвинутый"/>
</properties>
</component>
</children>
</grid>
<grid id="b5fd1" binding="panelShow" custom-create="true" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="4ecd9">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="503d1" binding="panelLabelColorTargetBackground" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="60b6e" class="javax.swing.JLabel" binding="labelColorTarget" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Цвет"/>
</properties>
</component>
</children>
</grid>
<grid id="c761e" binding="panelLabelAddColorTargetBackground" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="1625" class="javax.swing.JLabel" binding="labelAddColorTarget" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Доп. цвет"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="d6d47" binding="panelColors" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="30f6a" binding="panelColorRed" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="25" height="25"/>
<preferred-size width="25" height="25"/>
</grid>
</constraints>
<properties>
<background color="-65536"/>
<foreground color="-65536"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="1d871" binding="panelColorGreen" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="25" height="25"/>
<preferred-size width="25" height="25"/>
</grid>
</constraints>
<properties>
<background color="-16711936"/>
<foreground color="-16711936"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="e8ff7" binding="panelColorBlue" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="25" height="25"/>
<preferred-size width="25" height="25"/>
</grid>
</constraints>
<properties>
<background color="-16776961"/>
<foreground color="-16776961"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="cbc25" binding="panelColorYellow" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="25" height="25"/>
<preferred-size width="25" height="25"/>
</grid>
</constraints>
<properties>
<background color="-256"/>
<foreground color="-256"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="55334" binding="panelColorBlack" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="25" height="25"/>
<preferred-size width="25" height="25"/>
</grid>
</constraints>
<properties>
<background color="-16777216"/>
<foreground color="-16777216"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="ef939" binding="panelColorPink" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="25" height="25"/>
<preferred-size width="25" height="25"/>
</grid>
</constraints>
<properties>
<background color="-1729845"/>
<foreground color="-1729845"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="1b9f4" binding="panelColorOrange" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="25" height="25"/>
<preferred-size width="25" height="25"/>
</grid>
</constraints>
<properties>
<background color="-1732047"/>
<foreground color="-1732047"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="dd803" binding="panelColorPurple" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="25" height="25"/>
<preferred-size width="25" height="25"/>
</grid>
</constraints>
<properties>
<background color="-1767480"/>
<foreground color="-1767480"/>
</properties>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
<grid id="74e84" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c8143" class="javax.swing.JLabel" binding="labelEllipseEngines">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Эллипс"/>
</properties>
</component>
<component id="1344b" class="javax.swing.JLabel" binding="labelSimpleEngines">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Обычный"/>
</properties>
</component>
<component id="c1e0e" class="javax.swing.JLabel" binding="labelPyramidEngines">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Пирамид"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -1,28 +1,208 @@
package ProjectStormtrooper;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.MouseEvent;
import java.io.IOException;
public class FormPlaneConfig {
public DrawingPlane selectedPlane;
private JPanel panelColorRed;
private JPanel panelColorGreen;
private JPanel panelColorBlue;
private JPanel panelColorYellow;
private JPanel panelColorBlack;
private JPanel panelColorPink;
private JPanel panelColorOrange;
private JPanel panelColorPurple;
public JPanel panelWrapper;
private JPanel panelParams;
private JComponent panelShow;
private JPanel panelColors;
private JSpinner spinnerSpeed;
private JSpinner spinnerWeight;
private JLabel labelSpeed;
private JLabel labelWeight;
private JCheckBox checkBoxRockets;
private JCheckBox checkBoxBombs;
private JLabel labelColorTarget;
private JLabel labelAddColorTarget;
public JButton buttonApply;
public JButton buttonCancel;
private JLabel labelEllipseEngines;
private JLabel labelSimpleEngines;
private JLabel labelPyramidEngines;
private JLabel labelEnginesCount;
private JSpinner spinnerEnginesCount;
private JLabel labelPlane;
private JLabel labelStormtrooper;
private JPanel panelLabelColorTargetBackground;
private JPanel panelLabelAddColorTargetBackground;
public FormPlaneConfig() {
SpinnerModel numSpeed = new SpinnerNumberModel(100, 100, 1000, 1);
spinnerSpeed.setModel(numSpeed);
spinnerWeight.setModel(numSpeed);
labelPlane.setBorder(new EmptyBorder(20, 40, 20, 40));
labelStormtrooper.setBorder(new EmptyBorder(20, 40, 20, 40));
labelPlane.setTransferHandler(new LabelTransferHandler());
labelPlane.addMouseListener(new LabelMouseAdapter());
labelStormtrooper.setTransferHandler(new LabelTransferHandler());
labelStormtrooper.addMouseListener(new LabelMouseAdapter());
labelEllipseEngines.setTransferHandler(new LabelTransferHandler());
labelEllipseEngines.addMouseListener(new LabelMouseAdapter());
labelPyramidEngines.setTransferHandler(new LabelTransferHandler());
labelPyramidEngines.addMouseListener(new LabelMouseAdapter());
labelSimpleEngines.setTransferHandler(new LabelTransferHandler());
labelSimpleEngines.addMouseListener(new LabelMouseAdapter());
JPanel[] colorPanels = {
panelColorBlack,
panelColorBlue,
panelColorGreen,
panelColorPink,
panelColorPurple,
panelColorOrange,
panelColorRed,
panelColorYellow
};
for (var panelColor : colorPanels) {
panelColor.setTransferHandler(new PanelTransferHandler());
panelColor.addMouseListener(new PanelMouseAdapter());
}
panelLabelColorTargetBackground.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (selectedPlane == null) return false;
selectedPlane.EntityPlane.SetBodyColor(color);
panelLabelColorTargetBackground.setBackground(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
}
);
panelLabelAddColorTargetBackground.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
panelLabelAddColorTargetBackground.setBackground(color);
if (selectedPlane == null) return false;
if (!(selectedPlane instanceof DrawingStormtrooper))
return false;
((EntityStormtrooper) selectedPlane.EntityPlane).SetAdditionalColor(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
return false;
}
}
);
Canvas canvas = new Canvas();
panelShow.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "Простой" -> selectedPlane = new DrawingPlane(
(int) spinnerSpeed.getValue(),
(int) spinnerWeight.getValue(),
Color.WHITE,
panelShow.getWidth(),
panelShow.getHeight()
);
case "Продвинутый" -> selectedPlane = new DrawingStormtrooper(
(int) spinnerSpeed.getValue(),
(int) spinnerWeight.getValue(),
Color.WHITE,
Color.BLACK,
checkBoxRockets.isSelected(),
checkBoxBombs.isSelected(),
panelShow.getWidth(),
panelShow.getHeight()
);
}
if (selectedPlane != null) {
selectedPlane.SetPosition((panelShow.getWidth() - selectedPlane.GetWidth()) / 2, (panelShow.getHeight() - selectedPlane.GetHeight()) / 2);
panelLabelColorTargetBackground.setBackground(null);
panelLabelAddColorTargetBackground.setBackground(null);
} else {
return false;
}
String dataEngines = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
IDrawingEngines iDrawingEngines = null;
int enginesCount = (int) spinnerEnginesCount.getValue();
switch (dataEngines) {
case "Эллипс" -> {
iDrawingEngines = new DrawingEnginesEllipse();
}
case "Обычный" -> {
iDrawingEngines = new DrawingEnginesSimple();
}
case "Пирамид" -> {
iDrawingEngines = new DrawingEnginesPyramid();
}
}
if (iDrawingEngines != null) {
selectedPlane._drawingEngines = iDrawingEngines;
selectedPlane.SetEnginesCount(enginesCount);
}
panelShow.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
}
private class Canvas extends JComponent {
public Canvas() {
}
public void paintComponent(Graphics g) {
if (_plane == null) {
if (selectedPlane == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_plane.DrawTransport(g2d);
selectedPlane.DrawTransport(g2d);
super.repaint();
}
}
@ -39,6 +219,46 @@ public class FormPlaneConfig {
}
}
private class ColorTransferable implements Transferable {
private Color color;
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
public ColorTransferable(Color color) {
this.color = color;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{colorDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return colorDataFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return color;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private class PanelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new ColorTransferable(c.getBackground());
}
}
private class LabelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
@ -46,144 +266,16 @@ public class FormPlaneConfig {
}
}
final int WindowHeight = 700;
final int WindowWidth = 1000;
final int CanvasHeight = 600;
final int CanvasWidth = 600;
public DrawingPlane _plane = null;
public JButton buttonAdd;
public JFrame w;
public FormPlaneConfig() {
Border border = BorderFactory.createLineBorder(Color.GRAY);
JLabel labelSpeed = new JLabel("Скорость");
JLabel labelWeight = new JLabel("Вес");
JLabel labelWheelNum = new JLabel("Двигатели");
SpinnerModel spinnerModel = new SpinnerNumberModel(100, 100, 1000, 1);
JSpinner numericSpeed = new JSpinner(spinnerModel);
SpinnerModel spinnerModel2 = new SpinnerNumberModel(100, 100, 1000, 1);
JSpinner numericWeight = new JSpinner(spinnerModel2);
SpinnerModel spinnerModel3 = new SpinnerNumberModel(2, 2, 4, 1);
JSpinner numericWheelNum = new JSpinner(spinnerModel3);
JCheckBox checkBoxRockets = new JCheckBox("Ракеты");
JCheckBox checkBoxBombs = new JCheckBox("Бомбы");
JPanel[] colorPanels = {
new JPanel(), new JPanel(), new JPanel(), new JPanel(),
new JPanel(), new JPanel(), new JPanel(), new JPanel(),
};
colorPanels[0].setBackground(Color.BLACK);
colorPanels[1].setBackground(Color.BLUE);
colorPanels[2].setBackground(Color.GRAY);
colorPanels[3].setBackground(Color.YELLOW);
colorPanels[4].setBackground(Color.RED);
colorPanels[5].setBackground(Color.GREEN);
colorPanels[6].setBackground(Color.ORANGE);
colorPanels[7].setBackground(Color.WHITE);
JLabel labelTrain = new JLabel("Самолет");
labelTrain.setTransferHandler(new LabelTransferHandler());
labelTrain.addMouseListener(new LabelMouseAdapter());
labelTrain.setBorder(border);
labelTrain.setHorizontalAlignment(SwingConstants.CENTER);
labelTrain.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelLoco = new JLabel("Штурмовик");
labelLoco.setTransferHandler(new LabelTransferHandler());
labelLoco.addMouseListener(new LabelMouseAdapter());
labelLoco.setBorder(border);
labelLoco.setHorizontalAlignment(SwingConstants.CENTER);
labelLoco.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelColor = new JLabel("Цвет");
labelColor.setBorder(border);
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
labelColor.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelAdditionalColor = new JLabel("Доп. цвет");
labelAdditionalColor.setBorder(border);
labelAdditionalColor.setHorizontalAlignment(SwingConstants.CENTER);
labelAdditionalColor.setVerticalAlignment(SwingConstants.CENTER);
Canvas canvas = new Canvas();
canvas.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
switch (data) {
case "Самолет":
_plane = new DrawingPlane((int)numericSpeed.getValue(), (double)numericWeight.getValue(), Color.WHITE, CanvasWidth, CanvasHeight);
break;
case "Штурмовик":
_plane = new DrawingStormtrooper((int)numericSpeed.getValue(), (double)numericWeight.getValue(), Color.WHITE, Color.WHITE, checkBoxRockets.isSelected(), checkBoxBombs.isSelected(), CanvasWidth, CanvasHeight);
break;
}
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
buttonAdd = new JButton("Добавить");
JButton buttonCancel = new JButton("Отмена");
labelSpeed.setBounds(10, 10, 40, 20);
labelWeight.setBounds(10, 40, 40, 20);
labelWheelNum.setBounds(10, 70, 40, 20);
numericSpeed.setBounds(55, 10, 80, 20);
numericWeight.setBounds(55, 40, 80, 20);
numericWheelNum.setBounds(55, 70, 80, 20);
checkBoxRockets.setBounds(10, 100, 120, 20);
checkBoxBombs.setBounds(10, 130, 120, 20);
for (int i = 0; i < colorPanels.length; i += 2) {
colorPanels[i].setBounds(10, 200 + i / 2 * 60, 50, 50);
colorPanels[i + 1].setBounds(70, 200 + i / 2 * 60, 50, 50);
private class PanelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
labelTrain.setBounds(10, 500, 50, 30);
labelLoco.setBounds(70, 500, 50, 30);
}
labelColor.setBounds(WindowWidth - CanvasWidth, 10, CanvasWidth / 3, 30);
labelAdditionalColor.setBounds(WindowWidth - CanvasWidth + CanvasWidth * 2 / 3, 10, CanvasWidth / 3, 30);
canvas.setBounds(WindowWidth - CanvasWidth, 50, CanvasWidth, CanvasHeight);
buttonAdd.setBounds(WindowWidth - CanvasWidth, CanvasHeight + 60, CanvasWidth / 3, 30);
buttonCancel.setBounds(WindowWidth - CanvasWidth + CanvasWidth * 2 / 3, CanvasHeight + 60, CanvasWidth / 3, 30);
w = new JFrame();
w.setSize(WindowWidth + 20, WindowHeight + 40);
w.setLayout(null);
w.add(labelSpeed);
w.add(labelWeight);
w.add(labelWheelNum);
w.add(numericSpeed);
w.add(numericWeight);
w.add(numericWheelNum);
w.add(checkBoxRockets);
w.add(checkBoxBombs);
for (var it : colorPanels)
w.add(it);
w.add(labelTrain);
w.add(labelLoco);
w.add(labelColor);
w.add(labelAdditionalColor);
w.add(canvas);
w.add(buttonAdd);
w.add(buttonCancel);
w.setVisible(true);
buttonCancel.addActionListener(
e -> w.dispose()
);
private void createUIComponents() {
panelShow = new Canvas();
labelColorTarget = new JLabel("Цвет");
labelAddColorTarget = new JLabel("Доп. цвет");
}
}