diff --git a/laba1Loco/DrawingLoco.java b/laba1Loco/DrawingLoco.java
index 0a03b88..e5de16d 100644
--- a/laba1Loco/DrawingLoco.java
+++ b/laba1Loco/DrawingLoco.java
@@ -8,6 +8,11 @@ import java.awt.event.*;
public class DrawingLoco extends DrawingTrain{
+ public void setAdditionalColor(Color color)
+ {
+ ((EntityLoco)EntityTrain).AdditionalColor = color;
+ }
+
///
/// Инициализация свойств
///
diff --git a/laba1Loco/DrawingTrain.java b/laba1Loco/DrawingTrain.java
index db5143c..b17607b 100644
--- a/laba1Loco/DrawingTrain.java
+++ b/laba1Loco/DrawingTrain.java
@@ -9,6 +9,11 @@ import java.awt.event.*;
public class DrawingTrain {
public IMoveableObject GetMoveableObject() { return new DrawningObjectTrain(this);}
+ public void setBodyColor(Color color)
+ {
+ EntityTrain.BodyColor = color;
+ }
+
protected IWheelDrawing wheelDrawing;
///
/// Класс-сущность
@@ -17,11 +22,11 @@ public class DrawingTrain {
///
/// Ширина окна
///
- protected int _pictureWidth;
+ public int _pictureWidth;
///
/// Высота окна
///
- protected int _pictureHeight;
+ public int _pictureHeight;
///
/// Левая координата прорисовки локомотива
///
diff --git a/laba1Loco/FormTrainCollecltion.java b/laba1Loco/FormTrainCollecltion.java
index 78e561f..cc3ef8f 100644
--- a/laba1Loco/FormTrainCollecltion.java
+++ b/laba1Loco/FormTrainCollecltion.java
@@ -91,10 +91,10 @@ public class FormTrainCollecltion {
}
FormTrainConfig form = new FormTrainConfig();
- /*form.buttonSelectTrain.addActionListener(
+ form.buttonAdd.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
- if (obj.Add(form._drawingTrain) != -1)
+ if (obj.Add(form._train) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект добавлен");
@@ -108,7 +108,7 @@ public class FormTrainCollecltion {
form.w.dispose();
}
}
- );*/
+ );
}
}
);
diff --git a/laba1Loco/FormTrainConfig.java b/laba1Loco/FormTrainConfig.java
index 3890232..1b58c73 100644
--- a/laba1Loco/FormTrainConfig.java
+++ b/laba1Loco/FormTrainConfig.java
@@ -26,6 +26,7 @@ public class FormTrainConfig {
super.repaint();
}
}
+ //класс для перетаскивания типа объекта
private class LabelTransferHandler extends TransferHandler {
@Override
public int getSourceActions(JComponent c) {
@@ -36,13 +37,60 @@ public class FormTrainConfig {
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);
+ }
+ }
final int WindowHeight = 700;
final int WindowWidth = 1000;
final int CanvasHeight = 600;
@@ -50,6 +98,7 @@ public class FormTrainConfig {
public DrawingTrain _train = null;
public JButton buttonAdd;
public JFrame w;
+ public Canvas canvas;
public FormTrainConfig(){
Border border = BorderFactory.createLineBorder(Color.GRAY);
JLabel labelSpeed = new JLabel("Speed");
@@ -76,6 +125,10 @@ public class FormTrainConfig {
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 labelTrain = new JLabel("Train");
labelTrain.setTransferHandler(new LabelTransferHandler());
@@ -95,12 +148,60 @@ public class FormTrainConfig {
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);
+ _train.setBodyColor(color);
+ canvas.repaint();
+ return true;
+ } catch (UnsupportedFlavorException | IOException e) {
+ e.printStackTrace();
+ }
+ }
+ return false;
+ }
+ }
+ );
+
JLabel labelAdditionalColor = new JLabel("Additional color");
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);
+ }
- Canvas canvas = new Canvas();
+ @Override
+ public boolean importData(TransferHandler.TransferSupport support) {
+ if (canImport(support)) {
+ try {
+ Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
+ if (!(_train instanceof DrawingLoco))
+ return false;
+ ((DrawingLoco)_train).setAdditionalColor(color);
+ return true;
+ } catch (UnsupportedFlavorException | IOException e) {
+ e.printStackTrace();
+ }
+ }
+ return false;
+ }
+ }
+ );
+
+ canvas = new Canvas();
canvas.setTransferHandler(
new TransferHandler(){
@Override
diff --git a/laba1Loco/TrainsGenericCollection.java b/laba1Loco/TrainsGenericCollection.java
index 12461bc..5063084 100644
--- a/laba1Loco/TrainsGenericCollection.java
+++ b/laba1Loco/TrainsGenericCollection.java
@@ -117,6 +117,8 @@ public class TrainsGenericCollection