This commit is contained in:
Timourka 2023-11-05 16:29:18 +04:00
parent b2efa293ab
commit c6d17761bf
5 changed files with 120 additions and 7 deletions

View File

@ -8,6 +8,11 @@ import java.awt.event.*;
public class DrawingLoco extends DrawingTrain{
public void setAdditionalColor(Color color)
{
((EntityLoco)EntityTrain).AdditionalColor = color;
}
/// <summary>
/// Инициализация свойств
/// </summary>

View File

@ -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;
/// <summary>
/// Класс-сущность
@ -17,11 +22,11 @@ public class DrawingTrain {
/// <summary>
/// Ширина окна
/// </summary>
protected int _pictureWidth;
public int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
protected int _pictureHeight;
public int _pictureHeight;
/// <summary>
/// Левая координата прорисовки локомотива
/// </summary>

View File

@ -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();
}
}
);*/
);
}
}
);

View File

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

View File

@ -117,6 +117,8 @@ public class TrainsGenericCollection<T extends DrawingTrain, U extends IMoveable
{
if (train != null)
{
train._pictureHeight = _pictureHeight;
train._pictureWidth = _pictureWidth;
train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (train instanceof DrawingLoco)
((DrawingLoco)train).DrawTransport(g);