import javax.swing.*; import java.awt.*; import java.util.Random; public class FormParameterClass extends JComponent { private DrawningBattleship battleship1; private DrawningBattleship battleship2; private DrawningBattleship battleship3; private DrawningBattleship battleship4; private DrawningBattleship battleship5; MyParametrClass parameterClass; public FormParameterClass(){ JFrame form = new JFrame("Экземпляры от параметризованного класса"); form.setSize(900, 500); form.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); form.setLocationRelativeTo(null); Panel statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); statusPanel.setLayout(new FlowLayout()); setLayout(new BorderLayout()); add(statusPanel, BorderLayout.SOUTH); JButton showRandomEntity = new JButton("Создать"); showRandomEntity.addActionListener(e -> { Random random = new Random(); int[] arrayBlocks = {2, 4, 6}; if (parameterClass == null) { parameterClass = new MyParametrClass(5, 5); for (int i = 0; i < 5; i ++) { if (random.nextBoolean()) { parameterClass.Insert(new EntityBattleship(random.nextInt(100), random.nextInt(100), new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); } else { parameterClass.Insert(new EntityLinkor(random.nextInt(100), random.nextInt(100), new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), random.nextBoolean(), random.nextBoolean())); } } for (int i = 0; i < 5; i ++) { int rnd = random.nextInt(3); switch (rnd) { case 0: parameterClass.Insert(new DrawningBlocks(arrayBlocks[random.nextInt(3)], new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); break; case 1: parameterClass.Insert(new DrawningRoundBlocks(arrayBlocks[random.nextInt(3)])); break; case 2: parameterClass.Insert(new DrawningTriangleBlocks(arrayBlocks[random.nextInt(3)])); break; } } } battleship1 = parameterClass.GetDrawningBattleship(); battleship1.SetPosition(200, 200, form.getWidth(), form.getHeight() - 75); battleship2 = parameterClass.GetDrawningBattleship(); battleship2.SetPosition(400, 200, form.getWidth(), form.getHeight() - 75); battleship3 = parameterClass.GetDrawningBattleship(); battleship3.SetPosition(600, 200, form.getWidth(), form.getHeight() - 75); battleship4 = parameterClass.GetDrawningBattleship(); battleship4.SetPosition(300, 300, form.getWidth(), form.getHeight() - 75); battleship5 = parameterClass.GetDrawningBattleship(); battleship5.SetPosition(500, 300, form.getWidth(), form.getHeight() - 75); repaint(); }); statusPanel.add(showRandomEntity); form.getContentPane().add(this); form.setVisible(true); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; if (battleship1 != null) battleship1.DrawTransport(g2); if (battleship2 != null) battleship2.DrawTransport(g2); if (battleship3 != null) battleship3.DrawTransport(g2); if (battleship4 != null) battleship4.DrawTransport(g2); if (battleship5 != null) battleship5.DrawTransport(g2); super.repaint(); } }