408 lines
16 KiB
Java
408 lines
16 KiB
Java
package laba1Loco;
|
|
|
|
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 FormTrainConfig {
|
|
private class Canvas extends JComponent{
|
|
public Canvas(){
|
|
}
|
|
public void paintComponent (Graphics g){
|
|
if (_train == null){
|
|
return;
|
|
}
|
|
super.paintComponents (g) ;
|
|
Graphics2D g2d = (Graphics2D)g;
|
|
_train.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);
|
|
}
|
|
}
|
|
|
|
//что бы wheel'ы можно было тоскать
|
|
private class WheelTransferable implements Transferable {
|
|
private IWheelDrawing wheelDrawing;
|
|
private static final DataFlavor wheelDrawingDataFlavor = new DataFlavor(IWheelDrawing.class, "Wheel Drawing");
|
|
|
|
public WheelTransferable(IWheelDrawing 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);
|
|
}
|
|
}
|
|
}
|
|
//для отрисовки wheel'ов, которые можно взять и потащить
|
|
private class ComponentWheel extends JComponent{
|
|
public IWheelDrawing wheelDrawing;
|
|
|
|
public ComponentWheel(IWheelDrawing _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.Draw(-3,-34,false,Color.BLACK,g2d);
|
|
super.repaint();
|
|
}
|
|
}
|
|
|
|
final int WindowHeight = 700;
|
|
final int WindowWidth = 800;
|
|
final int CanvasHeight = 600;
|
|
final int CanvasWidth = 600;
|
|
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");
|
|
JLabel labelWeight = new JLabel("Weight");
|
|
JLabel labelWheelNum = new JLabel("Wheel");
|
|
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(2, 2, 4, 1);
|
|
JSpinner numericWheelNum = new JSpinner(spinnerModel3);
|
|
JCheckBox checkBoxTube = new JCheckBox("Smoke tube");
|
|
JCheckBox checkBoxFuelTank = new JCheckBox("Fuel tank");
|
|
JCheckBox checkBoxLocoLine = new JCheckBox("Loco line");
|
|
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 labelTrain = new JLabel("Train");
|
|
labelTrain.setTransferHandler(new LabelTransferHandler());
|
|
labelTrain.addMouseListener(new LabelMouseAdapter());
|
|
labelTrain.setBorder(border);
|
|
labelTrain.setHorizontalAlignment(SwingConstants.CENTER);
|
|
labelTrain.setVerticalAlignment(SwingConstants.CENTER);
|
|
|
|
JLabel labelLoco = new JLabel("Loco");
|
|
labelLoco.setTransferHandler(new LabelTransferHandler());
|
|
labelLoco.addMouseListener(new LabelMouseAdapter());
|
|
labelLoco.setBorder(border);
|
|
labelLoco.setHorizontalAlignment(SwingConstants.CENTER);
|
|
labelLoco.setVerticalAlignment(SwingConstants.CENTER);
|
|
|
|
JLabel labelColor = new JLabel("Color");
|
|
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 (_train == null)
|
|
return false;
|
|
_train.EntityTrain.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);
|
|
}
|
|
|
|
@Override
|
|
public boolean importData(TransferHandler.TransferSupport support) {
|
|
if (canImport(support)) {
|
|
try {
|
|
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
|
if (_train == null)
|
|
return false;
|
|
if (!(_train instanceof DrawingLoco))
|
|
return false;
|
|
((EntityLoco)_train.EntityTrain).setAdditionalColor(color);
|
|
return true;
|
|
} catch (UnsupportedFlavorException | IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
);
|
|
|
|
JLabel labelWheel = new JLabel("Wheel");
|
|
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 {
|
|
IWheelDrawing wheelDrawing = (IWheelDrawing) support.getTransferable().getTransferData(WheelTransferable.wheelDrawingDataFlavor);
|
|
if (_train == null)
|
|
return false;
|
|
wheelDrawing.setNumWheel(_train.EntityTrain.numWheel);
|
|
_train.wheelDrawing = 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 "Train":
|
|
_train = new DrawingTrain((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, (int)numericWheelNum.getValue(), CanvasWidth,CanvasHeight);
|
|
break;
|
|
case "Loco":
|
|
_train = new DrawingLoco((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, (int)numericWheelNum.getValue(), Color.BLACK, checkBoxTube.isSelected(), checkBoxFuelTank.isSelected(), checkBoxLocoLine.isSelected(), CanvasWidth,CanvasHeight);
|
|
break;
|
|
}
|
|
canvas.repaint();
|
|
return true;
|
|
} catch (UnsupportedFlavorException | IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
);
|
|
|
|
buttonAdd = new JButton("Add");
|
|
JButton buttonCancel = new JButton("Cancel");
|
|
|
|
ComponentWheel componentWheelSimple = new ComponentWheel(new WheelDrawingSimple());
|
|
ComponentWheel componentWheelBalls = new ComponentWheel(new WheelDrawingBalls());
|
|
ComponentWheel componentWheelDavidStar = new ComponentWheel(new WheelDrawingDavidStar());
|
|
|
|
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);
|
|
checkBoxTube.setBounds(10,100,120,20);
|
|
checkBoxFuelTank.setBounds(10,130,120,20);
|
|
checkBoxLocoLine.setBounds(10,160,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);
|
|
}
|
|
componentWheelSimple.setBounds(10,470,8,8);
|
|
componentWheelBalls.setBounds(30,470,8,8);
|
|
componentWheelDavidStar.setBounds(50,470,8,8);
|
|
labelTrain.setBounds(10,500 ,50,30);
|
|
labelLoco.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(checkBoxTube);
|
|
w.add(checkBoxFuelTank);
|
|
w.add(checkBoxLocoLine);
|
|
for (var it : colorPanels)
|
|
w.add(it);
|
|
w.add(labelTrain);
|
|
w.add(labelLoco);
|
|
w.add(labelColor);
|
|
w.add(labelAdditionalColor);
|
|
w.add(labelWheel);
|
|
w.add(canvas);
|
|
w.add(buttonAdd);
|
|
w.add(buttonCancel);
|
|
w.add(componentWheelSimple);
|
|
w.add(componentWheelBalls);
|
|
w.add(componentWheelDavidStar);
|
|
|
|
w.setVisible(true);
|
|
|
|
buttonCancel.addActionListener(
|
|
new ActionListener() {
|
|
public void actionPerformed(ActionEvent e){
|
|
w.dispose();
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}
|