From 789fe7165d988145c4174c4cb6beac22806d8dab Mon Sep 17 00:00:00 2001 From: Timourka Date: Sun, 5 Nov 2023 20:05:09 +0400 Subject: [PATCH] hard 5 laba --- laba1Loco/FormTrainCollecltion.java | 2 +- laba1Loco/FormTrainConfig.java | 118 +++++++++++++++++++++++++++- 2 files changed, 117 insertions(+), 3 deletions(-) diff --git a/laba1Loco/FormTrainCollecltion.java b/laba1Loco/FormTrainCollecltion.java index cc3ef8f..a134a55 100644 --- a/laba1Loco/FormTrainCollecltion.java +++ b/laba1Loco/FormTrainCollecltion.java @@ -94,7 +94,7 @@ public class FormTrainCollecltion { form.buttonAdd.addActionListener( new ActionListener() { 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); System.out.println("Объект добавлен"); diff --git a/laba1Loco/FormTrainConfig.java b/laba1Loco/FormTrainConfig.java index 1b58c73..5329872 100644 --- a/laba1Loco/FormTrainConfig.java +++ b/laba1Loco/FormTrainConfig.java @@ -91,15 +91,83 @@ public class FormTrainConfig { ((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 = 1000; + 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"); @@ -160,6 +228,8 @@ public class FormTrainConfig { if (canImport(support)) { try { Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor); + if (_train == null) + return false; _train.setBodyColor(color); canvas.repaint(); return true; @@ -188,6 +258,8 @@ public class FormTrainConfig { if (canImport(support)) { try { Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor); + if (_train == null) + return false; if (!(_train instanceof DrawingLoco)) return false; ((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.setTransferHandler( new TransferHandler(){ @@ -236,6 +338,10 @@ public class FormTrainConfig { 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); @@ -249,11 +355,15 @@ public class FormTrainConfig { 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*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); buttonAdd.setBounds(WindowWidth-CanvasWidth, 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(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);