import javax.swing.*; import java.awt.*; import java.util.Random; public class FormShipDisplay extends JFrame { private static final Random rnd = new Random(); private JPanel contentPane; private JButton buttonRefresh; private JPanel pictureBox; private IDrawingObject first; private IDrawingObject second; private IDrawingObject third; private final EntityWithDecks storage; public FormShipDisplay() { setTitle("Корабли"); setContentPane(contentPane); storage = new EntityWithDecks<>(20); for(int i = 0; i < 20; i++) { var base_color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); var dop_color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); if (rnd.nextBoolean()) { storage.add(new EntityMotorShip( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), base_color, dop_color, rnd.nextBoolean(), rnd.nextBoolean() )); } else { storage.add(new EntityShip( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), base_color )); } storage.add(DecksType.random(rnd.nextInt(0, 3), base_color)); } buttonRefresh.addActionListener(e -> { first = storage.constructShip(); second = storage.constructShip(); third = storage.constructShip(); first.setObject(0, 10, pictureBox.getWidth(), pictureBox.getHeight()); second.setObject(90, 10, pictureBox.getWidth(), pictureBox.getHeight()); third.setObject(190, 10, pictureBox.getWidth(), pictureBox.getHeight()); repaint(); }); } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2g = (Graphics2D) pictureBox.getGraphics(); if (first != null && second != null && third != null) { first.drawingObject(g2g); second.drawingObject(g2g); third.drawingObject(g2g); } } }