PIbd-21_Putintsev_D.M._Road.../src/RoadTrain/FormTruckConfig.java

404 lines
17 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package RoadTrain;
import RoadTrain.DrawingObjects.*;
import RoadTrain.Extra.*;
import RoadTrain.Entities.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.util.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.*;
import java.io.IOException;
public class FormTruckConfig {
private class Canvas extends JComponent {
public Canvas() {
}
public void paintComponent(Graphics g) {
if (_Truck == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_Truck.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 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, IOException {
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(((JPanel) 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 class WheelTransferable implements Transferable {
private IDrawingWheels wheelDrawing;
private static final DataFlavor wheelDrawingDataFlavor = new DataFlavor(IDrawingWheels.class, "Рисование колес");
public WheelTransferable(IDrawingWheels wheelDrawing) {
this.wheelDrawing = wheelDrawing;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{wheelDrawingDataFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(wheelDrawingDataFlavor);
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return wheelDrawing;
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
private class ComponentWheel extends JComponent {
public IDrawingWheels wheelDrawing;
public ComponentWheel(IDrawingWheels _wheelDrawing) {
wheelDrawing = _wheelDrawing;
this.addMouseListener(
new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
((ComponentWheel) e.getComponent()).getTransferHandler().exportAsDrag(((ComponentWheel) e.getComponent()), e, TransferHandler.COPY);
}
}
);
this.setTransferHandler(
new TransferHandler() {
@Override
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
@Override
protected Transferable createTransferable(JComponent c) {
return new WheelTransferable(((ComponentWheel) c).wheelDrawing);
}
}
);
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
wheelDrawing.drawWheels(g2d, Color.BLUE, -20, -20);
super.repaint();
}
}
final int WindowHeight = 700;
final int WindowWidth = 800;
final int CanvasHeight = 600;
final int CanvasWidth = 600;
public DrawingTruck _Truck = null;
public JButton buttonAdd;
public JFrame w;
public Canvas canvas;
public FormTruckConfig() {
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(1, 1, 3, 1);
JSpinner numericWheelNum = new JSpinner(spinnerModel3);
JCheckBox checkBoxWaterContainer = new JCheckBox("Бак с водой");
JCheckBox checkBoxSweepingBrush = 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);
for (var it : colorPanels) {
it.setTransferHandler(new PanelTransferHandler());
it.addMouseListener(new PanelMouseAdapter());
}
JLabel labelTruck = new JLabel("Грузовик");
labelTruck.setTransferHandler(new LabelTransferHandler());
labelTruck.addMouseListener(new LabelMouseAdapter());
labelTruck.setBorder(border);
labelTruck.setHorizontalAlignment(SwingConstants.CENTER);
labelTruck.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelRoadtrain = new JLabel("Очист.машина");
labelRoadtrain.setTransferHandler(new LabelTransferHandler());
labelRoadtrain.addMouseListener(new LabelMouseAdapter());
labelRoadtrain.setBorder(border);
labelRoadtrain.setHorizontalAlignment(SwingConstants.CENTER);
labelRoadtrain.setVerticalAlignment(SwingConstants.CENTER);
JLabel labelColor = new JLabel("Цвет");
labelColor.setBorder(border);
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
labelColor.setVerticalAlignment(SwingConstants.CENTER);
labelColor.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_Truck == null)
return false;
_Truck.entityTruck.SetBodyColor(color);
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
JLabel labelAdditionalColor = new JLabel("Доп.Цвет");
labelAdditionalColor.setBorder(border);
labelAdditionalColor.setHorizontalAlignment(SwingConstants.CENTER);
labelAdditionalColor.setVerticalAlignment(SwingConstants.CENTER);
labelAdditionalColor.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_Truck == null)
return false;
if (!(_Truck instanceof DrawingRoadTrain))
return false;
((EntityRoadTrain) _Truck.entityTruck).SetAdditionalColor(color);
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
JLabel labelWheel = new JLabel("Колеса");
labelWheel.setBorder(border);
labelWheel.setHorizontalAlignment(SwingConstants.CENTER);
labelWheel.setVerticalAlignment(SwingConstants.CENTER);
labelWheel.setTransferHandler(
new TransferHandler() {
@Override
public boolean canImport(TransferHandler.TransferSupport support) {
return support.isDataFlavorSupported(WheelTransferable.wheelDrawingDataFlavor);
}
@Override
public boolean importData(TransferHandler.TransferSupport support) {
if (canImport(support)) {
try {
IDrawingWheels wheelDrawing = (IDrawingWheels) support.getTransferable().getTransferData(WheelTransferable.wheelDrawingDataFlavor);
if (_Truck == null)
return false;
wheelDrawing.SetWheelNumber(_Truck.entityTruck.Numwheel);
_Truck.drawingWheels = wheelDrawing;
canvas.repaint();
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
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 "Грузовик":
_Truck = new DrawingTruck((int) numericSpeed.getValue(), (int) numericWeight.getValue(), Color.WHITE, CanvasWidth, CanvasHeight, (int) numericWheelNum.getValue());
break;
case "Очист.машина":
_Truck = new DrawingRoadTrain((int) numericSpeed.getValue(), (int) numericWeight.getValue(), Color.WHITE, Color.BLACK, checkBoxWaterContainer.isSelected(), checkBoxSweepingBrush.isSelected(), CanvasWidth, CanvasHeight, (int) numericWheelNum.getValue());
break;
}
canvas.repaint();
return true;
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
return false;
}
}
);
buttonAdd = new JButton("Добавить");
JButton buttonCancel = new JButton("Отменить");
ComponentWheel componentWheels = new ComponentWheel(new DrawingWheels());
ComponentWheel componentWheelsOneLine = new ComponentWheel(new DrawingOneLineWheels());
ComponentWheel componentWheelsTwoLine = new ComponentWheel(new DrawingTwoLineWheels());
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);
checkBoxWaterContainer.setBounds(10, 100, 120, 20);
checkBoxSweepingBrush.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);
}
componentWheels.setBounds(10, 470, 10, 10);
componentWheelsOneLine.setBounds(50, 470, 10, 10);
componentWheelsTwoLine.setBounds(90, 470, 10, 10);
labelTruck.setBounds(10, 500, 50, 30);
labelRoadtrain.setBounds(70, 500, 50, 30);
labelColor.setBounds(WindowWidth - CanvasWidth, 10, CanvasWidth / 3, 30);
labelAdditionalColor.setBounds(WindowWidth - CanvasWidth + CanvasWidth / 3, 10, CanvasWidth / 3, 30);
labelWheel.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(checkBoxWaterContainer);
w.add(checkBoxSweepingBrush);
for (var it : colorPanels)
w.add(it);
w.add(labelTruck);
w.add(labelRoadtrain);
w.add(labelColor);
w.add(labelAdditionalColor);
w.add(labelWheel);
w.add(canvas);
w.add(buttonAdd);
w.add(buttonCancel);
w.add(componentWheels);
w.add(componentWheelsOneLine);
w.add(componentWheelsTwoLine);
w.setVisible(true);
buttonCancel.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
w.dispose();
}
}
);
}
}