hard 5 laba

This commit is contained in:
Timourka 2023-11-05 20:05:09 +04:00
parent c6d17761bf
commit 789fe7165d
2 changed files with 117 additions and 3 deletions

View File

@ -94,7 +94,7 @@ public class FormTrainCollecltion {
form.buttonAdd.addActionListener( form.buttonAdd.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
if (obj.Add(form._train) != -1) if (obj != null && obj.Add(form._train) != -1)
{ {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект добавлен"); System.out.println("Объект добавлен");

View File

@ -91,15 +91,83 @@ public class FormTrainConfig {
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY); ((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 WindowHeight = 700;
final int WindowWidth = 1000; final int WindowWidth = 800;
final int CanvasHeight = 600; final int CanvasHeight = 600;
final int CanvasWidth = 600; final int CanvasWidth = 600;
public DrawingTrain _train = null; public DrawingTrain _train = null;
public JButton buttonAdd; public JButton buttonAdd;
public JFrame w; public JFrame w;
public Canvas canvas; 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");
JLabel labelWeight = new JLabel("Weight"); JLabel labelWeight = new JLabel("Weight");
@ -160,6 +228,8 @@ public class FormTrainConfig {
if (canImport(support)) { if (canImport(support)) {
try { try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor); Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_train == null)
return false;
_train.setBodyColor(color); _train.setBodyColor(color);
canvas.repaint(); canvas.repaint();
return true; return true;
@ -188,6 +258,8 @@ public class FormTrainConfig {
if (canImport(support)) { if (canImport(support)) {
try { try {
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor); Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
if (_train == null)
return false;
if (!(_train instanceof DrawingLoco)) if (!(_train instanceof DrawingLoco))
return false; return false;
((DrawingLoco)_train).setAdditionalColor(color); ((DrawingLoco)_train).setAdditionalColor(color);
@ -201,6 +273,36 @@ public class FormTrainConfig {
} }
); );
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 = new Canvas();
canvas.setTransferHandler( canvas.setTransferHandler(
new TransferHandler(){ new TransferHandler(){
@ -236,6 +338,10 @@ public class FormTrainConfig {
buttonAdd = new JButton("Add"); buttonAdd = new JButton("Add");
JButton buttonCancel = new JButton("Cancel"); 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); labelSpeed.setBounds(10,10,40,20);
labelWeight.setBounds(10,40,40,20); labelWeight.setBounds(10,40,40,20);
labelWheelNum.setBounds(10,70,40,20); labelWheelNum.setBounds(10,70,40,20);
@ -249,11 +355,15 @@ public class FormTrainConfig {
colorPanels[i].setBounds(10,200+i/2*60,50,50); colorPanels[i].setBounds(10,200+i/2*60,50,50);
colorPanels[i+1].setBounds(70,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); labelTrain.setBounds(10,500 ,50,30);
labelLoco.setBounds(70,500 ,50,30); labelLoco.setBounds(70,500 ,50,30);
labelColor.setBounds(WindowWidth-CanvasWidth, 10, CanvasWidth/3, 30); labelColor.setBounds(WindowWidth-CanvasWidth, 10, CanvasWidth/3, 30);
labelAdditionalColor.setBounds(WindowWidth-CanvasWidth + CanvasWidth*2/3, 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); canvas.setBounds(WindowWidth-CanvasWidth, 50, CanvasWidth, CanvasHeight);
buttonAdd.setBounds(WindowWidth-CanvasWidth, CanvasHeight+60, CanvasWidth/3, 30); buttonAdd.setBounds(WindowWidth-CanvasWidth, CanvasHeight+60, CanvasWidth/3, 30);
buttonCancel.setBounds(WindowWidth-CanvasWidth + CanvasWidth*2/3, CanvasHeight+60, CanvasWidth/3, 30); buttonCancel.setBounds(WindowWidth-CanvasWidth + CanvasWidth*2/3, CanvasHeight+60, CanvasWidth/3, 30);
@ -276,9 +386,13 @@ public class FormTrainConfig {
w.add(labelLoco); w.add(labelLoco);
w.add(labelColor); w.add(labelColor);
w.add(labelAdditionalColor); w.add(labelAdditionalColor);
w.add(labelWheel);
w.add(canvas); w.add(canvas);
w.add(buttonAdd); w.add(buttonAdd);
w.add(buttonCancel); w.add(buttonCancel);
w.add(componentWheelSimple);
w.add(componentWheelBalls);
w.add(componentWheelDavidStar);
w.setVisible(true); w.setVisible(true);