import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.util.Random; public class FormMap extends JFrame{ public JPanel Mainpanel; private JButton ButtonCreate; private JButton ButtonLeft; private JButton ButtonUp; private JButton ButtonDown; private JButton ButtonRight; private JPanel PictureBox; private JToolBar StatusStrip; private JButton ButtonModif; private JComboBox comboBoxSelector; private final JLabel JLabelSpeed = new JLabel(); private final JLabel JLabelWeight = new JLabel(); private final JLabel JLabelColor = new JLabel(); private AbstractMap _abstractMap; private void ButtonMove_Click(String name) { Direction dir = Direction.None; switch (name) { case "buttonLeft": dir = Direction.Left; break; case "buttonUp": dir = Direction.Up; break; case "buttonRight": dir = Direction.Right; break; case "buttonDown": dir = Direction.Down; break; } PictureBox.removeAll(); JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(dir))); PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBox.revalidate(); PictureBox.repaint(); } private void SetData(DrawningPlane _plane){ PictureBox.removeAll(); JLabelSpeed.setText("Cкорость: " + _plane.GetPlane().GetSpeed() + " "); JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " "); JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " ")); JLabel imageWithMapAndObject = new JLabel(); imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.CreateMap(PictureBox.getWidth(),PictureBox.getHeight(), new DrawningObjectPlane(_plane)))); PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); PictureBox.revalidate(); } public FormMap() { comboBoxSelector.addItem("Простая карта"); comboBoxSelector.addItem("Вторая карта"); _abstractMap = new SimpleMap(); Box LabelBox = Box.createHorizontalBox(); LabelBox.setMinimumSize(new Dimension(1, 20)); LabelBox.add(JLabelSpeed); LabelBox.add(JLabelWeight); LabelBox.add(JLabelColor); StatusStrip.add(LabelBox); try { Image img = ImageIO.read(FormMap.class.getResource("/Resource/arrowUp.jpg")); ButtonUp.setIcon(new ImageIcon(img)); img = ImageIO.read(FormMap.class.getResource("/Resource/arrowDown.jpg")); ButtonDown.setIcon(new ImageIcon(img)); img = ImageIO.read(FormMap.class.getResource("/Resource/arrowLeft.jpg")); ButtonLeft.setIcon(new ImageIcon(img)); img = ImageIO.read(FormMap.class.getResource("/Resource/arrowRight.jpg")); ButtonRight.setIcon(new ImageIcon(img)); } catch (Exception ex) { System.out.println(ex); } ButtonCreate.addActionListener(e -> { Random random = new Random(); var _plane = new DrawningPlane(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); SetData(_plane); }); ButtonModif.addActionListener(e -> { Random random = new Random(); var _plane = new DrawningAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); SetData(_plane); }); ButtonUp.addActionListener(e -> { ButtonMove_Click("buttonUp"); }); ButtonDown.addActionListener(e -> { ButtonMove_Click("buttonDown"); }); ButtonLeft.addActionListener(e -> { ButtonMove_Click("buttonLeft"); }); ButtonRight.addActionListener(e -> { ButtonMove_Click("buttonRight"); }); comboBoxSelector.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { comboBoxSelector = (JComboBox)e.getSource(); String item = (String)comboBoxSelector.getSelectedItem(); switch (item) { case "Простая карта" -> { _abstractMap = new SimpleMap(); break; } case "Вторая карта" -> { _abstractMap = new MyMap(); break; } } } }); } }