import javax.swing.*; import java.awt.*; import java.awt.event.ItemEvent; import java.util.Random; public class FormMap extends JFrame { private JPanel ContentPanel; private JButton buttonCreate; private JLabel speedLabel; private JLabel weightLabel; private JLabel colorLabel; private JButton buttonLeft; private JButton buttonDown; private JButton buttonRight; private JButton buttonUp; private JPanel pictureBox; private JButton buttonCreateModif; private JComboBox comboBoxMapSelector; private AbstractMap _abstractMap; private Image imageBuffer; public FormMap(){ setTitle("Трактор"); setContentPane(ContentPanel); setSize(800, 500); _abstractMap = new SimpleMap(); // Обработка нажатия кнопки "Создать" buttonCreate.addActionListener(e->{ Random rnd = new Random(); var _tracktor = new DrawningTracktor( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), rnd.nextInt(3,8) ); setData(_tracktor); }); // Обработка нажатия кнопки "Модификация" buttonCreateModif.addActionListener(e->{ Random rnd = new Random(); var _tracktor = new DrawningTrackedVehicle( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), rnd.nextInt(3,8), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), rnd.nextBoolean(), rnd.nextBoolean() ); setData(_tracktor); }); buttonUp.addActionListener(e->{ if (_abstractMap != null){ imageBuffer = _abstractMap.MoveObject(Direction.Up); repaint(); } }); buttonLeft.addActionListener(e->{ if (_abstractMap != null){ imageBuffer = _abstractMap.MoveObject(Direction.Left); repaint(); } }); buttonDown.addActionListener(e->{ if (_abstractMap != null){ imageBuffer = _abstractMap.MoveObject(Direction.Down); repaint(); } }); buttonRight.addActionListener(e->{ if (_abstractMap != null){ imageBuffer = _abstractMap.MoveObject(Direction.Right); repaint(); } }); comboBoxMapSelector.addItemListener(e->{ if (e.getStateChange()== ItemEvent.SELECTED){ switch (e.getItem().toString()) { case "Простая карта" -> _abstractMap = new SimpleMap(); case "Свалка карта" -> _abstractMap = new DumpMap(); } repaint(); } }); } private void setData(DrawningTracktor _tracktor) { Random rnd = new Random(); _tracktor.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight()); speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed()); weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight()); colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor())); imageBuffer = _abstractMap.CreateMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawningObjectExcavator(_tracktor)); repaint(); } @Override public void paint(Graphics g){ super.paint(g); Graphics2D g2d = (Graphics2D)pictureBox.getGraphics(); if (imageBuffer != null){ pictureBox.paintComponents(imageBuffer.getGraphics()); g2d.drawImage(imageBuffer,0,0,null); } } }