import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.beans.Expression; import java.rmi.server.ExportException; import java.util.concurrent.CountDownLatch; import java.util.function.Consumer; public class FormPlaneConfig extends JDialog{ private JButton ButtonAddObject; private JButton ButtonCancel; private JPanel GroupBoxConfig; private JPanel PanelObject; private JPanel GroupBoxColors; private JPanel PanelRed; private JPanel PanelGreen; private JPanel PanelBlue; private JPanel PanelYellow; private JPanel PanelPurple; private JPanel PanelBlack; private JPanel PanelGray; private JPanel PanelWhite; private JLabel LabelSimpleObject; private JLabel LabelModifiedObject; private JLabel LabelBaseColor; private JLabel LabelAddColor; private JPanel PictureBoxObject; private JLabel LabelSpeed; private JLabel LabelWeight; private JSpinner NumericUpDownWeight; private JSpinner NumericUpDownSpeed; private JCheckBox CheckBoxAddСompartment; private JCheckBox CheckBoxAddEngine; private JPanel GroupBoxSelectPlaneWindow; private JPanel GroubBoxSelectedCountWindow; private JSpinner NumericUpDownCountWindow; private JPanel MainPanel; private JLabel LabelTypeWindow; private JLabel LabelSimpleWindow; private JLabel LabelRectWindow; private JLabel LabelTriangleWindow; //переменная-выбранный самолёт DrawingPlane _plane; //событие Consumer EventAddPlane; public void AddEvent(Consumer ev){ EventAddPlane = ev; } //конструктор public FormPlaneConfig() { super(new Frame("Аэробус")); CreateWindow(); setModal(true); getContentPane().add(MainPanel); } public void Draw(DrawingPlane _plane) { PictureBoxObject.removeAll(); BufferedImage bmp = new BufferedImage(250, 200, BufferedImage.TYPE_INT_RGB); Graphics gr = bmp.getGraphics(); gr.setColor(new Color(238, 238, 238)); gr.fillRect(0, 0, 250, 200); if (_plane.GetPlane() != null) { _plane.DrawTransport(gr); JLabel imageOfLogo = new JLabel(); imageOfLogo.setPreferredSize(PictureBoxObject.getSize()); imageOfLogo.setMinimumSize(new Dimension(1, 1)); imageOfLogo.setIcon(new ImageIcon(bmp)); PictureBoxObject.add(imageOfLogo, BorderLayout.CENTER); } validate(); } public void CreateWindow() { setPreferredSize(new Dimension(1000, 700)); getContentPane().add(MainPanel); //рамки у Label LabelAddColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelTypeWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelBaseColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelModifiedObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelSimpleObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelSimpleWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelRectWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); LabelTriangleWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); //настройка значений у NumericUpDown NumericUpDownSpeed.setModel(new SpinnerNumberModel(1000, 1000, 3000, 1)); NumericUpDownWeight.setModel(new SpinnerNumberModel(750, 100, 1500, 1)); NumericUpDownCountWindow.setModel(new SpinnerNumberModel(1, 1, 3, 1)); MouseAdapter drag = new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { setCursor(new Cursor(Cursor.HAND_CURSOR)); } @Override public void mouseReleased(MouseEvent e) { setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); Drop((JComponent) e.getSource()); } }; MouseAdapter defCursor = new MouseAdapter() { @Override public void mouseExited(MouseEvent e) { setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } }; //настройки курсоров PictureBoxObject.addMouseListener(defCursor); LabelBaseColor.addMouseListener(defCursor); LabelAddColor.addMouseListener(defCursor); LabelTypeWindow.addMouseListener(defCursor); PanelBlack.addMouseListener(drag); PanelPurple.addMouseListener(drag); PanelGray.addMouseListener(drag); PanelGreen.addMouseListener(drag); PanelRed.addMouseListener(drag); PanelWhite.addMouseListener(drag); PanelYellow.addMouseListener(drag); PanelBlue.addMouseListener(drag); LabelSimpleObject.addMouseListener(drag); LabelModifiedObject.addMouseListener(drag); LabelSimpleWindow.addMouseListener(drag); LabelRectWindow.addMouseListener(drag); LabelTriangleWindow.addMouseListener(drag); ButtonAddObject.addActionListener(e -> { EventAddPlane.accept(_plane); dispose(); }); ButtonCancel.addActionListener(e -> dispose()); } public void Drop(JComponent dropItem) { if(dropItem == null) { return; } if(dropItem instanceof JPanel panel) { if(_plane == null) { return; } if(LabelBaseColor.getMousePosition() != null) { _plane.SetColor(panel.getBackground()); Draw(_plane); } if(LabelAddColor.getMousePosition() != null && _plane instanceof DrawingAirbus airbus) { airbus.SetAddColor(panel.getBackground()); Draw(_plane); } } if(dropItem instanceof JLabel label && PictureBoxObject.getMousePosition() != null) { int speed = (int)NumericUpDownSpeed.getValue(); int weight = (int)NumericUpDownWeight.getValue(); int countWindow = (int)NumericUpDownCountWindow.getValue(); boolean addCompartment = CheckBoxAddСompartment.isSelected(); boolean addEngine = CheckBoxAddEngine.isSelected(); if(label == LabelSimpleObject) { try { PictureBoxObject.remove(_plane); _plane = new DrawingPlane(speed, weight, Color.WHITE, countWindow); Draw(_plane); } catch(Exception ex) { } } else if(label == LabelModifiedObject) { try { PictureBoxObject.remove(_plane); _plane = new DrawingAirbus(speed, weight, Color.WHITE, countWindow, Color.WHITE, addCompartment, addEngine); Draw(_plane); } catch (Exception ex) { }; } if(_plane != null) { _plane.SetPosition(PictureBoxObject.getWidth() - 200, PictureBoxObject.getHeight() - 150, PictureBoxObject.getWidth(), PictureBoxObject.getHeight()); PictureBoxObject.add(_plane, BorderLayout.CENTER); Draw(_plane); } } else if(dropItem instanceof JLabel label && LabelTypeWindow.getMousePosition() != null && _plane != null) { if(label == LabelSimpleWindow) { _plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingAirplaneWindow()); Draw(_plane); } if(label == LabelRectWindow) { _plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingRectAirplaneWindow()); Draw(_plane); } if(label == LabelTriangleWindow) { _plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), new DrawingTriangleAirplaneWindow()); Draw(_plane); } } } }