PIbd-21_Potapov_N.S._Stormt.../ProjectStormtrooper/FormPlaneConfig.java

334 lines
13 KiB
Java

package ProjectStormtrooper;
import javax.swing.*;
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 TransferHandler(){
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new EngineTransferable(new DrawingEnginesEllipse());
}
}
);
labelEllipseEngines.addMouseListener(new LabelMouseAdapter());
labelPyramidEngines.setTransferHandler(
new TransferHandler(){
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new EngineTransferable(new DrawingEnginesPyramid());
}
}
);
labelPyramidEngines.addMouseListener(new LabelMouseAdapter());
labelSimpleEngines.setTransferHandler(
new TransferHandler(){
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new EngineTransferable(new DrawingEnginesSimple());
}
}
);
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;
}
}
);
panelShow.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.stringFlavor) || support.isDataFlavorSupported(EngineTransferable.engineDrawingDataFlavor);
}
@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;
}
} catch (UnsupportedFlavorException | IOException e) {
}
try {
IDrawingEngines drawingEngines = (IDrawingEngines) support.getTransferable().getTransferData(EngineTransferable.engineDrawingDataFlavor);
int enginesCount = (int) spinnerEnginesCount.getValue();
selectedPlane._drawingEngines = drawingEngines;
selectedPlane.SetEnginesCount(enginesCount);
}
catch (UnsupportedFlavorException | IOException e) {
}
panelShow.repaint();
return true;
}
return false;
}
}
);
}
private class Canvas extends JComponent {
public Canvas() {
}
public void paintComponent(Graphics g) {
if (selectedPlane == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
selectedPlane.DrawTransport(g2d);
super.repaint();
}
}
private class LabelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new StringSelection(((JLabel) c).getText());
}
}
private class EngineTransferable implements Transferable {
private IDrawingEngines drawingEngines;
private static final DataFlavor engineDrawingDataFlavor = new DataFlavor(IDrawingEngines.class, "Engine Drawing");
public EngineTransferable(IDrawingEngines drawingEngines) {
this.drawingEngines = drawingEngines;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{engineDrawingDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(engineDrawingDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
if (isDataFlavorSupported(flavor)) {
return drawingEngines;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
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) {
((JLabel) e.getComponent()).getTransferHandler().exportAsDrag(((JLabel) e.getComponent()), e, TransferHandler.COPY);
}
}
private class PanelMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
((JPanel) e.getComponent()).getTransferHandler().exportAsDrag(((JPanel) e.getComponent()), e, TransferHandler.COPY);
}
}
private void createUIComponents() {
panelShow = new Canvas();
labelColorTarget = new JLabel("Цвет");
labelAddColorTarget = new JLabel("Доп. цвет");
}
}