5 base
This commit is contained in:
parent
b2efa293ab
commit
c6d17761bf
@ -8,6 +8,11 @@ import java.awt.event.*;
|
|||||||
|
|
||||||
public class DrawingLoco extends DrawingTrain{
|
public class DrawingLoco extends DrawingTrain{
|
||||||
|
|
||||||
|
public void setAdditionalColor(Color color)
|
||||||
|
{
|
||||||
|
((EntityLoco)EntityTrain).AdditionalColor = color;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Инициализация свойств
|
/// Инициализация свойств
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -9,6 +9,11 @@ import java.awt.event.*;
|
|||||||
public class DrawingTrain {
|
public class DrawingTrain {
|
||||||
public IMoveableObject GetMoveableObject() { return new DrawningObjectTrain(this);}
|
public IMoveableObject GetMoveableObject() { return new DrawningObjectTrain(this);}
|
||||||
|
|
||||||
|
public void setBodyColor(Color color)
|
||||||
|
{
|
||||||
|
EntityTrain.BodyColor = color;
|
||||||
|
}
|
||||||
|
|
||||||
protected IWheelDrawing wheelDrawing;
|
protected IWheelDrawing wheelDrawing;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс-сущность
|
/// Класс-сущность
|
||||||
@ -17,11 +22,11 @@ public class DrawingTrain {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ширина окна
|
/// Ширина окна
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected int _pictureWidth;
|
public int _pictureWidth;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Высота окна
|
/// Высота окна
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected int _pictureHeight;
|
public int _pictureHeight;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Левая координата прорисовки локомотива
|
/// Левая координата прорисовки локомотива
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -91,10 +91,10 @@ public class FormTrainCollecltion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormTrainConfig form = new FormTrainConfig();
|
FormTrainConfig form = new FormTrainConfig();
|
||||||
/*form.buttonSelectTrain.addActionListener(
|
form.buttonAdd.addActionListener(
|
||||||
new ActionListener() {
|
new ActionListener() {
|
||||||
public void actionPerformed(ActionEvent e){
|
public void actionPerformed(ActionEvent e){
|
||||||
if (obj.Add(form._drawingTrain) != -1)
|
if (obj.Add(form._train) != -1)
|
||||||
{
|
{
|
||||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||||
System.out.println("Объект добавлен");
|
System.out.println("Объект добавлен");
|
||||||
@ -108,7 +108,7 @@ public class FormTrainCollecltion {
|
|||||||
form.w.dispose();
|
form.w.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);*/
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -26,6 +26,7 @@ public class FormTrainConfig {
|
|||||||
super.repaint();
|
super.repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//класс для перетаскивания типа объекта
|
||||||
private class LabelTransferHandler extends TransferHandler {
|
private class LabelTransferHandler extends TransferHandler {
|
||||||
@Override
|
@Override
|
||||||
public int getSourceActions(JComponent c) {
|
public int getSourceActions(JComponent c) {
|
||||||
@ -37,12 +38,59 @@ public class FormTrainConfig {
|
|||||||
return new StringSelection(((JLabel)c).getText());
|
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{
|
private class LabelMouseAdapter extends MouseAdapter{
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
|
((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 WindowHeight = 700;
|
||||||
final int WindowWidth = 1000;
|
final int WindowWidth = 1000;
|
||||||
final int CanvasHeight = 600;
|
final int CanvasHeight = 600;
|
||||||
@ -50,6 +98,7 @@ public class FormTrainConfig {
|
|||||||
public DrawingTrain _train = null;
|
public DrawingTrain _train = null;
|
||||||
public JButton buttonAdd;
|
public JButton buttonAdd;
|
||||||
public JFrame w;
|
public JFrame w;
|
||||||
|
public Canvas canvas;
|
||||||
public FormTrainConfig(){
|
public FormTrainConfig(){
|
||||||
Border border = BorderFactory.createLineBorder(Color.GRAY);
|
Border border = BorderFactory.createLineBorder(Color.GRAY);
|
||||||
JLabel labelSpeed = new JLabel("Speed");
|
JLabel labelSpeed = new JLabel("Speed");
|
||||||
@ -76,6 +125,10 @@ public class FormTrainConfig {
|
|||||||
colorPanels[5].setBackground(Color.GREEN);
|
colorPanels[5].setBackground(Color.GREEN);
|
||||||
colorPanels[6].setBackground(Color.ORANGE);
|
colorPanels[6].setBackground(Color.ORANGE);
|
||||||
colorPanels[7].setBackground(Color.WHITE);
|
colorPanels[7].setBackground(Color.WHITE);
|
||||||
|
for (var it : colorPanels){
|
||||||
|
it.setTransferHandler(new PanelTransferHandler());
|
||||||
|
it.addMouseListener(new PanelMouseAdapter());
|
||||||
|
}
|
||||||
|
|
||||||
JLabel labelTrain = new JLabel("Train");
|
JLabel labelTrain = new JLabel("Train");
|
||||||
labelTrain.setTransferHandler(new LabelTransferHandler());
|
labelTrain.setTransferHandler(new LabelTransferHandler());
|
||||||
@ -95,12 +148,60 @@ public class FormTrainConfig {
|
|||||||
labelColor.setBorder(border);
|
labelColor.setBorder(border);
|
||||||
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
|
labelColor.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
labelColor.setVerticalAlignment(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");
|
JLabel labelAdditionalColor = new JLabel("Additional color");
|
||||||
labelAdditionalColor.setBorder(border);
|
labelAdditionalColor.setBorder(border);
|
||||||
labelAdditionalColor.setHorizontalAlignment(SwingConstants.CENTER);
|
labelAdditionalColor.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
labelAdditionalColor.setVerticalAlignment(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(
|
canvas.setTransferHandler(
|
||||||
new TransferHandler(){
|
new TransferHandler(){
|
||||||
@Override
|
@Override
|
||||||
|
@ -117,6 +117,8 @@ public class TrainsGenericCollection<T extends DrawingTrain, U extends IMoveable
|
|||||||
{
|
{
|
||||||
if (train != null)
|
if (train != null)
|
||||||
{
|
{
|
||||||
|
train._pictureHeight = _pictureHeight;
|
||||||
|
train._pictureWidth = _pictureWidth;
|
||||||
train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
|
train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
|
||||||
if (train instanceof DrawingLoco)
|
if (train instanceof DrawingLoco)
|
||||||
((DrawingLoco)train).DrawTransport(g);
|
((DrawingLoco)train).DrawTransport(g);
|
||||||
|
Loading…
Reference in New Issue
Block a user