diff --git a/Hydroplane/src/main/java/org/example/DrawingHydroplane.java b/Hydroplane/src/main/java/org/example/DrawingHydroplane.java index 3e0870e..3c26202 100644 --- a/Hydroplane/src/main/java/org/example/DrawingHydroplane.java +++ b/Hydroplane/src/main/java/org/example/DrawingHydroplane.java @@ -16,6 +16,7 @@ public class DrawingHydroplane extends DrawingPlane{ super(plane, _windowDrawing, width, height); if (height < _pictureHeight || width < _pictureWidth) return; + _EntityPlane = plane; } public void SetPosition(int x, int y) @@ -50,7 +51,7 @@ public class DrawingHydroplane extends DrawingPlane{ g.drawLine(_startPosX + 125, _startPosY + 55, _startPosX + 125, _startPosY + 70); g.drawLine(_startPosX + 130, _startPosY + 55, _startPosX + 130, _startPosY + 70); - windowDrawing.Draw(_startPosX, _startPosY, ((EntityHydroplane)_EntityPlane).AdditionalColor, g); +// windowDrawing.Draw(_startPosX, _startPosY, ((EntityHydroplane)_EntityPlane).AdditionalColor, g); if (((EntityHydroplane)_EntityPlane).Bobber) { g.fillPolygon( diff --git a/Hydroplane/src/main/java/org/example/DrawingPlane.java b/Hydroplane/src/main/java/org/example/DrawingPlane.java index 52ddea1..f6724a5 100644 --- a/Hydroplane/src/main/java/org/example/DrawingPlane.java +++ b/Hydroplane/src/main/java/org/example/DrawingPlane.java @@ -11,9 +11,9 @@ public class DrawingPlane{ public EntityPlane _EntityPlane; - protected int _pictureWidth; + public int _pictureWidth; - protected int _pictureHeight; + public int _pictureHeight; protected int _startPosX; diff --git a/Hydroplane/src/main/java/org/example/EntityHydroplane.java b/Hydroplane/src/main/java/org/example/EntityHydroplane.java index 4a87141..b7ce4bc 100644 --- a/Hydroplane/src/main/java/org/example/EntityHydroplane.java +++ b/Hydroplane/src/main/java/org/example/EntityHydroplane.java @@ -6,6 +6,10 @@ public class EntityHydroplane extends EntityPlane{ public Color AdditionalColor; + public void setAdditionalColor(Color color) { + AdditionalColor = color; + } + public boolean Boat; public boolean Bobber; diff --git a/Hydroplane/src/main/java/org/example/EntityPlane.java b/Hydroplane/src/main/java/org/example/EntityPlane.java index 7ad0d23..ae33b6a 100644 --- a/Hydroplane/src/main/java/org/example/EntityPlane.java +++ b/Hydroplane/src/main/java/org/example/EntityPlane.java @@ -10,6 +10,10 @@ public class EntityPlane{ public Color BodyColor; + public void setBodyColor(Color color) { + BodyColor = color; + } + public double Step; public int numWindow; diff --git a/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java b/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java index 98d1ef2..6ced848 100644 --- a/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java +++ b/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java @@ -94,11 +94,11 @@ public class FormPlaneCollecltion { return; } - FormHydroplane form = new FormHydroplane(); - form.buttonSelectPlane.addActionListener( + FormPlaneConfig form = new FormPlaneConfig(); + form.buttonAdd.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { - if (obj.Add(form._drawingPlane) != -1) + if (obj != null && obj.Add(form._plane) != -1) { JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); System.out.println("Объект добавлен"); diff --git a/Hydroplane/src/main/java/org/example/FormPlaneConfig.java b/Hydroplane/src/main/java/org/example/FormPlaneConfig.java new file mode 100644 index 0000000..49d296e --- /dev/null +++ b/Hydroplane/src/main/java/org/example/FormPlaneConfig.java @@ -0,0 +1,405 @@ +package org.example; + +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 FormPlaneConfig { + private class Canvas extends JComponent{ + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_plane == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _plane.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); + } + } + + //что бы window'ы можно было тоскать + private class WindowTransferable implements Transferable { + private IWindowDrawing windowDrawing; + private static final DataFlavor windowDrawingDataFlavor = new DataFlavor(IWindowDrawing.class, "Window Drawing"); + + public WindowTransferable(IWindowDrawing windowDrawing) { + this.windowDrawing = windowDrawing; + } + + @Override + public DataFlavor[] getTransferDataFlavors() { + return new DataFlavor[]{windowDrawingDataFlavor}; + } + + @Override + public boolean isDataFlavorSupported(DataFlavor flavor) { + return flavor.equals(windowDrawingDataFlavor); + } + + @Override + public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { + if (isDataFlavorSupported(flavor)) { + return windowDrawing; + } else { + throw new UnsupportedFlavorException(flavor); + } + } + } + //для отрисовки window'ов, которые можно взять и потащить + private class ComponentWindow extends JComponent{ + public IWindowDrawing windowDrawing; + + public ComponentWindow(IWindowDrawing _windowDrawing){ + windowDrawing = _windowDrawing; + this.addMouseListener( + new MouseAdapter(){ + @Override + public void mousePressed(MouseEvent e) { + ((ComponentWindow)e.getComponent()).getTransferHandler().exportAsDrag(((ComponentWindow)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 WindowTransferable(((ComponentWindow)c).windowDrawing); + } + } + ); + } + + public void paintComponent (Graphics g){ + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + windowDrawing.setNumWindow(10); // ??? + windowDrawing.Draw(-35, -28, Color.black, g2d); + super.repaint(); + } + } + + final int WindowHeight = 700; + final int WindowWidth = 800; + final int CanvasHeight = 600; + final int CanvasWidth = 600; + public DrawingPlane _plane = null; + public JButton buttonAdd; + public JFrame w; + public Canvas canvas; + + public FormPlaneConfig(){ + + Border border = BorderFactory.createLineBorder(Color.GRAY); + JLabel labelSpeed = new JLabel("Speed"); + JLabel labelWeight = new JLabel("Weight"); + JLabel labelWindowNum = new JLabel("Window"); + SpinnerModel spinnerModel = new SpinnerNumberModel(100, 100, 1000, 50); + JSpinner numericSpeed = new JSpinner(spinnerModel); + SpinnerModel spinnerModel2 = new SpinnerNumberModel(100, 100, 1000, 50); + JSpinner numericWeight = new JSpinner(spinnerModel2); + SpinnerModel spinnerModel3 = new SpinnerNumberModel(10, 10, 30, 10); + JSpinner numericWindowNum = new JSpinner(spinnerModel3); + JCheckBox checkBoxBoat = new JCheckBox("Boat"); + JCheckBox checkBoxBobber = new JCheckBox("Bobber"); + 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 labelPlane = new JLabel("Plane"); + labelPlane.setTransferHandler(new LabelTransferHandler()); + labelPlane.addMouseListener(new LabelMouseAdapter()); + labelPlane.setBorder(border); + labelPlane.setHorizontalAlignment(SwingConstants.CENTER); + labelPlane.setVerticalAlignment(SwingConstants.CENTER); + + JLabel labelHydroplane = new JLabel("Hydroplane"); + labelHydroplane.setTransferHandler(new LabelTransferHandler()); + labelHydroplane.addMouseListener(new LabelMouseAdapter()); + labelHydroplane.setBorder(border); + labelHydroplane.setHorizontalAlignment(SwingConstants.CENTER); + labelHydroplane.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 (_plane == null) + return false; + _plane._EntityPlane.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 (_plane == null) + return false; + if (!(_plane instanceof DrawingHydroplane)) + return false; + ((EntityHydroplane)_plane._EntityPlane).setAdditionalColor(color); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + + JLabel labelWindow = new JLabel("Window"); + labelWindow.setBorder(border); + labelWindow.setHorizontalAlignment(SwingConstants.CENTER); + labelWindow.setVerticalAlignment(SwingConstants.CENTER); + labelWindow.setTransferHandler( + new TransferHandler(){ + @Override + public boolean canImport(TransferHandler.TransferSupport support) { + return support.isDataFlavorSupported(WindowTransferable.windowDrawingDataFlavor); + } + + @Override + public boolean importData(TransferHandler.TransferSupport support) { + if (canImport(support)) { + try { + IWindowDrawing windowDrawing = (IWindowDrawing) support.getTransferable().getTransferData(WindowTransferable.windowDrawingDataFlavor); + if (_plane == null) + return false; + windowDrawing.setNumWindow(_plane._EntityPlane.numWindow); + _plane.windowDrawing = windowDrawing; + 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 "Plane": + _plane = new DrawingPlane((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, (int)numericWindowNum.getValue(), CanvasWidth,CanvasHeight); + break; + case "Hydroplane": + _plane = new DrawingHydroplane((int)numericSpeed.getValue(), (int)numericWeight.getValue(), Color.WHITE, Color.BLACK, checkBoxBoat.isSelected(), checkBoxBobber.isSelected(), (int)numericWindowNum.getValue(), CanvasWidth,CanvasHeight); + break; + } + canvas.repaint(); + return true; + } catch (UnsupportedFlavorException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + } + ); + + buttonAdd = new JButton("Add"); + JButton buttonCancel = new JButton("Cancel"); + + ComponentWindow componentWindowOval = new ComponentWindow(new WindowDrawing()); + ComponentWindow componentWindowRect = new ComponentWindow(new WindowDrawingRect()); + ComponentWindow componentWindowTringle = new ComponentWindow(new WindowDrawingTringle()); + + labelSpeed.setBounds(10,10,40,20); + labelWeight.setBounds(10,40,40,20); + labelWindowNum.setBounds(10,70,40,20); + numericSpeed.setBounds(55,10,80,20); + numericWeight.setBounds(55,40,80,20); + numericWindowNum.setBounds(55,70,80,20); + checkBoxBoat.setBounds(10,100,120,20); + checkBoxBobber.setBounds(10,130,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); + } + componentWindowOval.setBounds(10,470,8,8); // 10,470,8,8 + componentWindowRect.setBounds(30,470,8,8); //30,470,8,8 + componentWindowTringle.setBounds(50,470,8,8); + labelPlane.setBounds(10,500 ,50,30); + labelHydroplane.setBounds(70,500 ,50,30); + + labelColor.setBounds(WindowWidth-CanvasWidth, 10, CanvasWidth/3, 30); + labelAdditionalColor.setBounds(WindowWidth-CanvasWidth + CanvasWidth/3, 10, CanvasWidth/3, 30); + labelWindow.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(labelWindowNum); + w.add(numericSpeed); + w.add(numericWeight); + w.add(numericWindowNum); + w.add(checkBoxBoat); + w.add(checkBoxBobber); + for (var it : colorPanels) + w.add(it); + w.add(labelPlane); + w.add(labelHydroplane); + w.add(labelColor); + w.add(labelAdditionalColor); + w.add(labelWindow); + w.add(canvas); + w.add(buttonAdd); + w.add(buttonCancel); + w.add(componentWindowOval); + w.add(componentWindowRect); + w.add(componentWindowTringle); + + w.setVisible(true); + + buttonCancel.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + w.dispose(); + } + } + ); + } +} diff --git a/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java b/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java index 6578e48..2399050 100644 --- a/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java +++ b/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java @@ -80,6 +80,8 @@ public class PlanesGenericCollection