From d781237090cd133ce520b03c1fa53f89fd3d74e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 18 Nov 2023 22:32:49 +0400 Subject: [PATCH] done --- src/DoubleDeckerBus/ColorTransferable.java | 35 ++ .../DrawningObjects/DrawningBus.java | 21 +- .../DrawningDoubleDeckerBus.java | 4 + src/DoubleDeckerBus/FormBusCollection.java | 19 +- src/DoubleDeckerBus/FormBusConfig.java | 327 ++++++++++++++++++ src/DoubleDeckerBus/IDrawComponent.java | 37 ++ src/DoubleDeckerBus/LabelMouseAdapter.java | 12 + src/DoubleDeckerBus/LabelTransferHandler.java | 17 + src/DoubleDeckerBus/PanelMouseAdapter.java | 12 + src/DoubleDeckerBus/PanelTransferHandler.java | 16 + src/DoubleDeckerBus/iDrawTransferable.java | 36 ++ 11 files changed, 528 insertions(+), 8 deletions(-) create mode 100644 src/DoubleDeckerBus/ColorTransferable.java create mode 100644 src/DoubleDeckerBus/FormBusConfig.java create mode 100644 src/DoubleDeckerBus/IDrawComponent.java create mode 100644 src/DoubleDeckerBus/LabelMouseAdapter.java create mode 100644 src/DoubleDeckerBus/LabelTransferHandler.java create mode 100644 src/DoubleDeckerBus/PanelMouseAdapter.java create mode 100644 src/DoubleDeckerBus/PanelTransferHandler.java create mode 100644 src/DoubleDeckerBus/iDrawTransferable.java diff --git a/src/DoubleDeckerBus/ColorTransferable.java b/src/DoubleDeckerBus/ColorTransferable.java new file mode 100644 index 0000000..666f45e --- /dev/null +++ b/src/DoubleDeckerBus/ColorTransferable.java @@ -0,0 +1,35 @@ +package DoubleDeckerBus; + +import java.awt.*; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.io.IOException; + +public class ColorTransferable implements Transferable { + private Color color; + public 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); + } + } +} diff --git a/src/DoubleDeckerBus/DrawningObjects/DrawningBus.java b/src/DoubleDeckerBus/DrawningObjects/DrawningBus.java index be8d928..efaa3e7 100644 --- a/src/DoubleDeckerBus/DrawningObjects/DrawningBus.java +++ b/src/DoubleDeckerBus/DrawningObjects/DrawningBus.java @@ -14,8 +14,9 @@ public class DrawningBus { public int _pictureHeight; protected int _startPosX; protected int _startPosY; - protected int _busWidth = 110; - protected int _busHeight = 70; + public int _busWidth = 110; + public int _busHeight = 70; + public int _doorNumb; protected IDraw DrawningDoor; public EntityBus EntityBus() { @@ -69,6 +70,18 @@ public class DrawningBus { DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6)); } + public void ChangeIDraw(IDraw obj) { + DrawningDoor = obj; + obj.ChangeDoorsNumber(_doorNumb); + obj.ChangeX(_startPosX); + obj.ChangeY(_startPosY); + } + + public void ChangeDoorsNumb(int numb) { + _doorNumb = numb; + DrawningDoor.ChangeDoorsNumber(numb); + } + public void SetPosition(int x, int y){ if (EntityBus == null) { return; @@ -159,6 +172,10 @@ public class DrawningBus { DrawningDoor.ChangeY(_startPosY); } + public void ChangeColor(Color col){ + EntityBus.BodyColor = col; + } + public void DrawTransport(Graphics2D g2d) { if (EntityBus == null) { return; diff --git a/src/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.java b/src/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.java index da850a5..8d348ea 100644 --- a/src/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.java +++ b/src/DoubleDeckerBus/DrawningObjects/DrawningDoubleDeckerBus.java @@ -15,6 +15,10 @@ public class DrawningDoubleDeckerBus extends DrawningBus { } } + public void ChangeAddColor(Color col) { + ((EntityDoubleDeckerBus)EntityBus).AdditionalColor = col; + } + @Override public void DrawTransport(Graphics2D g2d) { diff --git a/src/DoubleDeckerBus/FormBusCollection.java b/src/DoubleDeckerBus/FormBusCollection.java index ede8d42..fa61436 100644 --- a/src/DoubleDeckerBus/FormBusCollection.java +++ b/src/DoubleDeckerBus/FormBusCollection.java @@ -124,22 +124,29 @@ public class FormBusCollection { return; } TheBusesGenericCollection _bus = _storage.Get(listBoxStorages.getSelectedValue()); - FormDoubleDeckerBus form = new FormDoubleDeckerBus(); - form.buttonSelect.addActionListener(new ActionListener() { + FormBusConfig form = new FormBusConfig(); + form.addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_bus.Insert(form.SelectedBus())) + if (_bus.Insert(form._bus)) { JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); - form.SelectedBus()._pictureWidth = pictureBoxWidth; - form.SelectedBus()._pictureHeight = pictureBoxHeight; + form._bus._pictureWidth = pictureBoxWidth; + form._bus._pictureHeight = pictureBoxHeight; Draw(); } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); } - form.BusFrame.dispose(); + form.frameConfig.dispose(); + Draw(); + } + }); + form.cancelButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + form.frameConfig.dispose(); Draw(); } }); diff --git a/src/DoubleDeckerBus/FormBusConfig.java b/src/DoubleDeckerBus/FormBusConfig.java new file mode 100644 index 0000000..3240c04 --- /dev/null +++ b/src/DoubleDeckerBus/FormBusConfig.java @@ -0,0 +1,327 @@ +package DoubleDeckerBus; + +import DoubleDeckerBus.DrawningObjects.*; + +import javax.swing.*; +import java.awt.*; +import java.awt.datatransfer.*; +import java.awt.event.*; +import java.io.IOException; + +public class FormBusConfig { + public JFrame frameConfig; + public JButton addButton; + public JButton cancelButton; + public DrawningBus _bus; + int _pictureBoxWidth = 218; + int _pictureBoxHeight = 190; + + public FormBusConfig(){ + frameConfig = new JFrame(); + addButton = new JButton("Добавить"); + cancelButton = new JButton("Отмена"); + addButton.setBounds(555,262,94,29); + cancelButton.setBounds(679,262,94,29); + Canvas canvas = new Canvas(); + canvas.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + canvas.setBounds(555, 65, _pictureBoxWidth, _pictureBoxHeight); + JLabel iDrawLabel = new JLabel("Орнамент"); + JLabel colorLabel = new JLabel("Цвет"); + DrawningBus toGetSize = new DrawningBus(0,0, null, 0, 0); + IDrawComponent baseDoor = new IDrawComponent(new DrawningDoor(toGetSize._busWidth - toGetSize._busWidth / 10, + toGetSize._busHeight, + _pictureBoxWidth / 2 -toGetSize._busWidth / 2, + _pictureBoxHeight / 2 -toGetSize._busHeight / 2)); + IDrawComponent ovalDoor = new IDrawComponent(new DrawningDoorOval(toGetSize._busWidth - toGetSize._busWidth / 10, + toGetSize._busHeight, + _pictureBoxWidth / 2 -toGetSize._busWidth / 2, + _pictureBoxHeight / 2 -toGetSize._busHeight / 2)); + IDrawComponent triangleDoor = new IDrawComponent(new DrawningDoorTriangle(toGetSize._busWidth - toGetSize._busWidth / 10, + toGetSize._busHeight, + _pictureBoxWidth / 2 -toGetSize._busWidth / 2, + _pictureBoxHeight / 2 -toGetSize._busHeight / 2)); + baseDoor.setLayout(new GridLayout(1,1)); + ovalDoor.setLayout(new GridLayout(1,1)); + triangleDoor.setLayout(new GridLayout(1,1)); + iDrawLabel.setHorizontalAlignment(SwingConstants.CENTER); + iDrawLabel.setVerticalAlignment(SwingConstants.CENTER); + colorLabel.setHorizontalAlignment(SwingConstants.CENTER); + colorLabel.setVerticalAlignment(SwingConstants.CENTER); + JLabel baseLabel = new JLabel("Прямоугольные"); + JLabel ovalLabel = new JLabel("Овальные"); + JLabel triangleLabel = new JLabel("Треугольные"); + baseLabel.setHorizontalAlignment(SwingConstants.CENTER); + baseLabel.setVerticalAlignment(SwingConstants.CENTER); + ovalLabel.setHorizontalAlignment(SwingConstants.CENTER); + ovalLabel.setVerticalAlignment(SwingConstants.CENTER); + triangleLabel.setHorizontalAlignment(SwingConstants.CENTER); + triangleLabel.setVerticalAlignment(SwingConstants.CENTER); + + baseDoor.add(baseLabel); + ovalDoor.add(ovalLabel); + triangleDoor.add(triangleLabel); + baseDoor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + ovalDoor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + triangleDoor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + JLabel addColorLabel = new JLabel("Доп цвет"); + addColorLabel.setHorizontalAlignment(SwingConstants.CENTER); + addColorLabel.setVerticalAlignment(SwingConstants.CENTER); + colorLabel.setBounds(555, 20, 70, 33); + addColorLabel.setBounds(629, 20, 70, 33); + iDrawLabel.setBounds(703, 20, 70, 33); + colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + addColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + iDrawLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + JCheckBox checkSecondFloor = new JCheckBox("Наличие 2 этажа"); + JCheckBox checkLadder = new JCheckBox("Наличие лестницы"); + JCheckBox checkLineBetweenFloor = new JCheckBox("Наличие разделительной линии"); + checkSecondFloor.setBounds(6, 122, 159, 24); + checkLadder.setBounds(6, 152, 145, 24); + checkLineBetweenFloor.setBounds(6, 182, 131, 24); + JLabel simpleLabel = new JLabel("Простой"); + JLabel advancedLabel = new JLabel("Продвинутый"); + simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); + simpleLabel.setBounds(171,169, 120, 50); + simpleLabel.setHorizontalAlignment(SwingConstants.CENTER); + simpleLabel.setVerticalAlignment(SwingConstants.CENTER); + advancedLabel.setBounds(297,169, 120, 50); + advancedLabel.setHorizontalAlignment(SwingConstants.CENTER); + advancedLabel.setVerticalAlignment(SwingConstants.CENTER); + baseDoor.setBounds(171,229, 120, 50); + ovalDoor.setBounds(297,229, 120, 50); + triangleDoor.setBounds(423,229, 120, 50); + JLabel speedLabel = new JLabel ("Скорость"); + JLabel weightLabel = new JLabel ("Вес"); + JPanel colorPanel = new JPanel(); + colorPanel.setBounds(171, 23, 234,143); + SpinnerNumberModel speedSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0); + SpinnerNumberModel weightSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0); + SpinnerNumberModel doorNumbSpinnerModel = new SpinnerNumberModel(2, 2, 4, 1.0); + JSpinner wheelNumbSpinner = new JSpinner(doorNumbSpinnerModel); + JSpinner speedSpinner = new JSpinner(speedSpinnerModel); + JSpinner weightSpinner = new JSpinner(weightSpinnerModel); + speedSpinner.setBounds(6, 46, 150, 27); + speedLabel.setBounds(6, 23, 73, 20); + weightSpinner.setBounds(6, 99, 150, 27); + weightLabel.setBounds(6, 76, 33, 20); + wheelNumbSpinner.setBounds(6, 200, 150, 27); + + JPanel redPanel = new JPanel(); + JPanel greenPanel = new JPanel(); + JPanel bluePanel = new JPanel(); + JPanel yellowPanel = new JPanel(); + JPanel whitePanel = new JPanel(); + JPanel grayPanel = new JPanel(); + JPanel blackPanel = new JPanel(); + JPanel purplePanel = new JPanel(); + + redPanel.setTransferHandler(new PanelTransferHandler()); + greenPanel.setTransferHandler(new PanelTransferHandler()); + bluePanel.setTransferHandler(new PanelTransferHandler()); + yellowPanel.setTransferHandler(new PanelTransferHandler()); + whitePanel.setTransferHandler(new PanelTransferHandler()); + grayPanel.setTransferHandler(new PanelTransferHandler()); + blackPanel.setTransferHandler(new PanelTransferHandler()); + purplePanel.setTransferHandler(new PanelTransferHandler()); + + redPanel.addMouseListener(new PanelMouseAdapter()); + greenPanel.addMouseListener(new PanelMouseAdapter()); + bluePanel.addMouseListener(new PanelMouseAdapter()); + yellowPanel.addMouseListener(new PanelMouseAdapter()); + whitePanel.addMouseListener(new PanelMouseAdapter()); + grayPanel.addMouseListener(new PanelMouseAdapter()); + blackPanel.addMouseListener(new PanelMouseAdapter()); + purplePanel.addMouseListener(new PanelMouseAdapter()); + + redPanel.setName("Красный"); + greenPanel.setName("Зелёный"); + bluePanel.setName("Синий"); + yellowPanel.setName("Жёлтый"); + whitePanel.setName("Белый"); + grayPanel.setName("Серый"); + blackPanel.setName("Чёрный"); + purplePanel.setName("Фиолетовый"); + + simpleLabel.setTransferHandler(new LabelTransferHandler()); + simpleLabel.addMouseListener(new LabelMouseAdapter()); + + advancedLabel.setTransferHandler(new LabelTransferHandler()); + advancedLabel.addMouseListener(new LabelMouseAdapter()); + + redPanel.setBackground(Color.RED); + greenPanel.setBackground(Color.GREEN); + bluePanel.setBackground(Color.BLUE); + yellowPanel.setBackground(Color.YELLOW); + whitePanel.setBackground(Color.WHITE); + grayPanel.setBackground(Color.GRAY); + blackPanel.setBackground(Color.BLACK); + purplePanel.setBackground(Color.PINK); + + colorPanel.setLayout(new GridLayout(2, 4)); + + colorPanel.add(redPanel); + colorPanel.add(greenPanel); + colorPanel.add(bluePanel); + colorPanel.add(yellowPanel); + colorPanel.add(whitePanel); + colorPanel.add(grayPanel); + colorPanel.add(blackPanel); + colorPanel.add(purplePanel); + + + frameConfig.add(colorLabel); + frameConfig.add(addColorLabel); + frameConfig.add(iDrawLabel); + frameConfig.setLayout(null); + frameConfig.setSize(818, 350); + frameConfig.add(speedLabel); + frameConfig.add(speedSpinner); + frameConfig.add(weightLabel); + frameConfig.add(weightSpinner); + frameConfig.add(simpleLabel); + frameConfig.add(advancedLabel); + frameConfig.add(checkSecondFloor); + frameConfig.add(checkLadder); + frameConfig.add(checkLineBetweenFloor); + frameConfig.add(canvas); + frameConfig.add(addButton); + frameConfig.add(cancelButton); + frameConfig.add(wheelNumbSpinner); + frameConfig.setVisible(true); + frameConfig.add(colorPanel); + frameConfig.add(baseDoor); + frameConfig.add(ovalDoor); + frameConfig.add(triangleDoor); + colorPanel.setVisible(true); + baseDoor.setVisible(true); + ovalDoor.setVisible(true); + triangleDoor.setVisible(true); + + 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 { + Object speedObj = speedSpinner.getValue(); + Number speedNumb = (Number) speedObj; + int speed = speedNumb.intValue(); + + Object weightObj = weightSpinner.getValue(); + Number weightNumb = (Number) weightObj; + int weight = weightNumb.intValue(); + + Object doorNumbObj = wheelNumbSpinner.getValue(); + Number doorNumb = (Number) doorNumbObj; + int doorsNumb = doorNumb.intValue(); + + String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor); + switch (data) { + case "Простой": + _bus = new DrawningBus(speed, weight, Color.WHITE, _pictureBoxWidth, _pictureBoxHeight); + _bus.ChangeDoorsNumb(doorsNumb); + break; + case "Продвинутый": + _bus = new DrawningDoubleDeckerBus(speed, weight, Color.WHITE, Color.BLACK, + doorsNumb, _pictureBoxWidth, _pictureBoxHeight, + checkSecondFloor.isSelected(), checkLadder.isSelected(), checkLineBetweenFloor.isSelected()); + _bus.ChangeDoorsNumb(doorsNumb); + break; + } + _bus.SetPosition(_pictureBoxWidth / 2 -_bus._busWidth / 2, + _pictureBoxHeight / 2 -_bus._busHeight / 2); + + canvas.DrawningBus = _bus; + canvas.repaint(); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + + iDrawLabel.setTransferHandler( + new TransferHandler(){ + @Override + public boolean canImport(TransferHandler.TransferSupport support) { + return support.isDataFlavorSupported(iDrawTransferable.iDrawDataFlavor); + } + @Override + public boolean importData(TransferHandler.TransferSupport support) { + if (canImport(support)) { + try { + IDraw obj = (IDraw) support.getTransferable().getTransferData(iDrawTransferable.iDrawDataFlavor); + if (_bus == null) + return false; + _bus.ChangeIDraw(obj); + canvas.repaint(); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + + colorLabel.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 (_bus == null) + return false; + _bus.ChangeColor(color); + canvas.repaint(); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + + addColorLabel.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 (_bus == null || !(_bus instanceof DrawningDoubleDeckerBus)) + return false; + ((DrawningDoubleDeckerBus)_bus).ChangeAddColor(color); + canvas.repaint(); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + } +} \ No newline at end of file diff --git a/src/DoubleDeckerBus/IDrawComponent.java b/src/DoubleDeckerBus/IDrawComponent.java new file mode 100644 index 0000000..683072e --- /dev/null +++ b/src/DoubleDeckerBus/IDrawComponent.java @@ -0,0 +1,37 @@ +package DoubleDeckerBus; + +import DoubleDeckerBus.DrawningObjects.IDraw; + +import javax.swing.*; +import java.awt.datatransfer.Transferable; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class IDrawComponent extends JComponent { + public IDraw obj; + + public IDrawComponent(IDraw obj){ + this.obj = obj; + this.addMouseListener( + new MouseAdapter(){ + @Override + public void mousePressed(MouseEvent e) { + ((IDrawComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawComponent)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 iDrawTransferable(((IDrawComponent)c).obj); + } + } + ); + } +} diff --git a/src/DoubleDeckerBus/LabelMouseAdapter.java b/src/DoubleDeckerBus/LabelMouseAdapter.java new file mode 100644 index 0000000..5d8c9c0 --- /dev/null +++ b/src/DoubleDeckerBus/LabelMouseAdapter.java @@ -0,0 +1,12 @@ +package DoubleDeckerBus; + +import javax.swing.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class LabelMouseAdapter extends MouseAdapter { + @Override + public void mousePressed(MouseEvent e) { + ((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY); + } +} diff --git a/src/DoubleDeckerBus/LabelTransferHandler.java b/src/DoubleDeckerBus/LabelTransferHandler.java new file mode 100644 index 0000000..1decc9b --- /dev/null +++ b/src/DoubleDeckerBus/LabelTransferHandler.java @@ -0,0 +1,17 @@ +package DoubleDeckerBus; + +import javax.swing.*; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; + +public 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()); + } +} \ No newline at end of file diff --git a/src/DoubleDeckerBus/PanelMouseAdapter.java b/src/DoubleDeckerBus/PanelMouseAdapter.java new file mode 100644 index 0000000..858cc8e --- /dev/null +++ b/src/DoubleDeckerBus/PanelMouseAdapter.java @@ -0,0 +1,12 @@ +package DoubleDeckerBus; + +import javax.swing.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class PanelMouseAdapter extends MouseAdapter { + @Override + public void mousePressed(MouseEvent e) { + ((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY); + } +} diff --git a/src/DoubleDeckerBus/PanelTransferHandler.java b/src/DoubleDeckerBus/PanelTransferHandler.java new file mode 100644 index 0000000..c2a103a --- /dev/null +++ b/src/DoubleDeckerBus/PanelTransferHandler.java @@ -0,0 +1,16 @@ +package DoubleDeckerBus; + +import javax.swing.*; +import java.awt.datatransfer.Transferable; + +public 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()); + } +} diff --git a/src/DoubleDeckerBus/iDrawTransferable.java b/src/DoubleDeckerBus/iDrawTransferable.java new file mode 100644 index 0000000..2208171 --- /dev/null +++ b/src/DoubleDeckerBus/iDrawTransferable.java @@ -0,0 +1,36 @@ +package DoubleDeckerBus; + +import DoubleDeckerBus.DrawningObjects.IDraw; + +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.io.IOException; + +public class iDrawTransferable implements Transferable { + private IDraw iDrawObject; + public static final DataFlavor iDrawDataFlavor = new DataFlavor(IDraw.class, "IDraw"); + + public iDrawTransferable(IDraw iDrawObject) { + this.iDrawObject = iDrawObject; + } + + @Override + public DataFlavor[] getTransferDataFlavors() { + return new DataFlavor[]{iDrawDataFlavor}; + } + + @Override + public boolean isDataFlavorSupported(DataFlavor flavor) { + return iDrawDataFlavor.equals(flavor); + } + + @Override + public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { + if (isDataFlavorSupported(flavor)) { + return iDrawObject; + } else { + throw new UnsupportedFlavorException(flavor); + } + } +}