import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.function.Consumer; public class FormAircraftConfig implements Form { private JPanel mainPane; private JSpinner spinnerSpeed; private JSpinner spinnerWeight; private JPanel colorsPane; private JCheckBox checkboxDopWings; private JCheckBox checkboxRockets; private JSpinner spinnerEnginesCount; private JButton buttonAdd; private JButton buttonCancel; private JPanel panelRed; private JPanel panelYellow; private JPanel panelGreen; private JPanel panelBlue; private JPanel panelWhite; private JPanel panelGray; private JPanel panelBlack; private JPanel panelPurple; private JPanel panelBaseColor; private JPanel panelDopColor; private JPanel panelDraw; private JLabel labelBase; private JLabel labelAdvanced; private JLabel labelTruncatedEngine; private JLabel labelWavyEngine; private JLabel labelBaseEngine; private JFrame jframe = getFrame(); private DrawingAircraft aircraft; private boolean isMousePressed = false; private Canvas canv = new Canvas(this); private ArrayList> listeners = new ArrayList<>(); private JFrame getFrame() { JFrame frame = new JFrame(); frame.setVisible(true); frame.setBounds(300, 100, 800, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); return frame; } public void addListener(Consumer l) { listeners.add(l); } public void run() { jframe.add(mainPane); panelDraw.add(canv); buttonAdd.addActionListener(e -> { jframe.dispose(); for(var l : listeners) l.accept(aircraft); }); buttonCancel.addActionListener(e -> jframe.dispose()); MouseAdapter mouseHandler = new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { super.mouseEntered(e); jframe.setCursor(new Cursor(Cursor.HAND_CURSOR)); } @Override public void mouseExited(MouseEvent e) { super.mouseExited(e); if(!isMousePressed) jframe.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } @Override public void mousePressed(MouseEvent e) { super.mousePressed(e); isMousePressed = true; jframe.setCursor(new Cursor(Cursor.HAND_CURSOR)); } @Override public void mouseReleased(MouseEvent e) { super.mouseReleased(e); isMousePressed = false; jframe.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); drop((JComponent) e.getSource()); canv.repaint(); } }; spinnerSpeed.setModel(new SpinnerNumberModel(100, 70, 200, 1)); spinnerWeight.setModel(new SpinnerNumberModel(700, 600, 1500, 1)); spinnerEnginesCount.setModel(new SpinnerNumberModel(4, 2, 6, 2)); panelRed.addMouseListener(mouseHandler); panelYellow.addMouseListener(mouseHandler); panelGreen.addMouseListener(mouseHandler); panelBlue.addMouseListener(mouseHandler); panelWhite.addMouseListener(mouseHandler); panelGray.addMouseListener(mouseHandler); panelBlack.addMouseListener(mouseHandler); panelPurple.addMouseListener(mouseHandler); labelBase.addMouseListener(mouseHandler); labelAdvanced.addMouseListener(mouseHandler); labelTruncatedEngine.addMouseListener(mouseHandler); labelWavyEngine.addMouseListener(mouseHandler); labelBaseEngine.addMouseListener(mouseHandler); } private void drop(JComponent elem) { if(elem instanceof JPanel panel) { if(aircraft == null) return; if(panelBaseColor.getMousePosition() != null) { aircraft.setColor(panel.getBackground()); aircraft.getEngines().setColor(panel.getBackground()); } if(panelDopColor.getMousePosition() != null && aircraft instanceof DrawingModernAircraft advanced) { advanced.setDopColor(panel.getBackground()); } } Point test = panelDraw.getMousePosition(); if(elem instanceof JLabel label && test != null) { int speed = (int)spinnerSpeed.getValue(); int weight = (int)spinnerWeight.getValue(); int enginesCount = (int)spinnerEnginesCount.getValue(); if(label == labelBase) { aircraft = new DrawingAircraft(speed, weight, Color.WHITE); aircraft.SetPosition(20, 40, canv.getWidth(), canv.getHeight()); aircraft.getEngines().setCount(enginesCount); aircraft.getEngines().setColor(Color.WHITE); } if(label == labelAdvanced) { aircraft = new DrawingModernAircraft(speed, weight, Color.WHITE, Color.BLACK, checkboxDopWings.isSelected(), checkboxRockets.isSelected()); aircraft.SetPosition(10, 10, canv.getWidth(), canv.getHeight()); aircraft.getEngines().setCount(enginesCount); aircraft.getEngines().setColor(Color.WHITE); } if(aircraft == null) return; if(label == labelBaseEngine) { aircraft.setEngines(new DrawingEngines(enginesCount, aircraft.getColor())); } if(label == labelTruncatedEngine) { aircraft.setEngines(new DrawingTruncatedEngines(enginesCount, aircraft.getColor())); } if(label == labelWavyEngine) { aircraft.setEngines(new DrawingWavyEngines(enginesCount, aircraft.getColor())); } } } @Override public void Draw(Graphics2D g) { if(aircraft == null) return; aircraft.DrawTransport(g); } }