import javax.swing.*; import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.Random; public class FormArtillery extends JFrame { private JPanel artilleryPane; private JLabel speedLabel; private JLabel weightLabel; private JLabel colorLabel; private JPanel pictureBox; private JButton createButton; private JButton buttonUp; private JButton buttonDown; private JButton buttonLeft; private JButton buttonRight; private DrawingArtillery _artillery; public FormArtillery() { this.setTitle("Artillery"); this.setContentPane(artilleryPane); createButton.addActionListener(e -> { Random rnd = new Random(); _artillery = new DrawingArtillery(); _artillery.Init( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color( rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), rnd.nextInt(4, 7) ); _artillery.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); speedLabel.setText(String.format("Скорость: %s", _artillery.getArtillery().getSpeed())); weightLabel.setText(String.format("Вес: %s", _artillery.getArtillery().getWeight())); colorLabel.setText(String.format("Цвет: %x", _artillery.getArtillery().getBodyColor().getRGB())); repaint(); }); pictureBox.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { if (_artillery != null) _artillery.changeBorders(pictureBox.getWidth(), pictureBox.getHeight()); repaint(); } }); buttonLeft.addActionListener(e -> { if (_artillery != null) _artillery.moveTransport(Direction.Left); repaint(); }); buttonRight.addActionListener(e -> { if (_artillery != null) _artillery.moveTransport(Direction.Right); repaint(); }); buttonUp.addActionListener(e -> { if (_artillery != null) _artillery.moveTransport(Direction.Up); repaint(); }); buttonDown.addActionListener(e -> { if (_artillery != null) _artillery.moveTransport(Direction.Down); repaint(); }); } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) artilleryPane.getGraphics(); if (_artillery != null) { _artillery.drawTransport(g2d); } } }