import javax.swing.*; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Random; public class FormMap extends JFrame { private JPanel artilleryPane; private JLabel speedLabel; private JLabel weightLabel; private JLabel colorLabel; private JPanel pictureBox; private JButton buttonUp; private JButton buttonDown; private JButton buttonLeft; private JButton buttonRight; private JButton createButton; private JButton createAdvancedButton; private JComboBox comboBoxSelectorMap; private AbstractMap _abstractMap; private Image bufferedImage; public FormMap() { this.setTitle("Artillery"); this.setContentPane(artilleryPane); _abstractMap = new SimpleMap(); createButton.addActionListener(e -> { Random rnd = new Random(); var artillery = new DrawingArtillery( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextInt(4, 6 + 1) ); setData(artillery); }); buttonLeft.setForeground(new Color(0, 0, 0, 0)); buttonRight.setForeground(new Color(0, 0, 0, 0)); buttonUp.setForeground(new Color(0, 0, 0, 0)); buttonDown.setForeground(new Color(0, 0, 0, 0)); buttonLeft.addActionListener(e -> { if (_abstractMap != null) { bufferedImage = _abstractMap.moveObject(Direction.Left); repaint(); } }); buttonRight.addActionListener(e -> { if (_abstractMap != null) { bufferedImage = _abstractMap.moveObject(Direction.Right); repaint(); } }); buttonUp.addActionListener(e -> { if (_abstractMap != null) { bufferedImage = _abstractMap.moveObject(Direction.Up); repaint(); } }); buttonDown.addActionListener(e -> { if (_abstractMap != null) { bufferedImage = _abstractMap.moveObject(Direction.Down); repaint(); } }); createAdvancedButton.addActionListener(e -> { Random rnd = new Random(); var artillery = new DrawingAdvancedArtillery( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextInt(4, 6 + 1), new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextBoolean(), rnd.nextBoolean() ); setData(artillery); }); comboBoxSelectorMap.addItemListener(e -> { if (e.getStateChange() == ItemEvent.SELECTED) { switch (e.getItem().toString()) { case "Простая карта" -> _abstractMap = new SimpleMap(); case "Лесная карта" -> _abstractMap = new ForestMap(); } } }); } private void setData(DrawingArtillery artillery) { Random rnd = new Random(); artillery.setPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight()); speedLabel.setText(String.format("Скорость: %d", artillery.artillery.getSpeed())); weightLabel.setText(String.format("Вес: %f", artillery.artillery.getWeight())); colorLabel.setText(String.format("Цвет: %x", artillery.getArtillery().getBodyColor().getRGB())); bufferedImage = _abstractMap.createMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawingObjectArtillery(artillery)); repaint(); } @Override public void paint(Graphics g) { super.paint(g); if (bufferedImage != null) { pictureBox.paintComponents(bufferedImage.getGraphics()); pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null); } } }