From 55e0a3592802e1bde7787d6998372034a28e3dd2 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Tue, 25 Oct 2022 16:54:14 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8.=20=D0=A1?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=81=D0=BB=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D0=9B=D0=B0=D0=B1=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EntityWithExtraCreator.java | 20 ++++---- FormEntityWithExtraGallery.java | 83 ++++++++++++++++----------------- 2 files changed, 47 insertions(+), 56 deletions(-) diff --git a/EntityWithExtraCreator.java b/EntityWithExtraCreator.java index 04b6d65..deda0ef 100644 --- a/EntityWithExtraCreator.java +++ b/EntityWithExtraCreator.java @@ -2,33 +2,29 @@ import java.lang.reflect.Array; import java.util.Random; public class EntityWithExtraCreator { - private T[] entityArr; - private U[] extraArr; + private final Object[] entityArr; + private final Object[] extraArr; int entitiesCount = 0; int extraCount = 0; public EntityWithExtraCreator(int countEntities, int countExtra) { - entityArr = (T[]) new EntityLocomotive[countEntities]; - extraArr = (U[]) new IDrawningExtra[countExtra]; + entityArr = new Object[countEntities]; + extraArr = new Object[countExtra]; } - public int Insert(T entityLocomotive) { + public void Insert(T entityLocomotive) { if(entitiesCount < entityArr.length) { entityArr[entitiesCount] = entityLocomotive; entitiesCount++; - return entitiesCount - 1; } - return -1; } - public int Insert (U extra) { + public void Insert (U extra) { if(extraCount < extraArr.length) { extraArr[extraCount] = extra; extraCount++; - return extraCount - 1; } - return -1; } public DrawningLocomotive getEntityWithExtra() { @@ -36,8 +32,8 @@ public class EntityWithExtraCreator { - Random random = new Random(); - entityWithExtraCreator = new EntityWithExtraCreator<>(100, 100); - for (int i = 0; i < 100; i ++) { - if (random.nextBoolean()) { - entityWithExtraCreator.Insert(new EntityLocomotive(random.nextInt(100), random.nextInt(100), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); - } - else { - entityWithExtraCreator.Insert(new EntityWarmlyLocomotive(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 < 100; i ++) { - int extraRand = random.nextInt(3); - switch (extraRand) { - case 0: - entityWithExtraCreator.Insert(new ExtraWheelsDraw(random.nextInt(3), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); - break; - - case 1: - entityWithExtraCreator.Insert(new ExtraStarWheelDraw(random.nextInt(3), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); - break; - - case 2: - entityWithExtraCreator.Insert(new ExtraRoundWheelDraw(random.nextInt(3), - new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); - break; - } - } - repaint(); - }); - statusPanel.add(randomlyFillArraysWithParts); JButton showRandomEntity = new JButton("Create entity from parts"); showRandomEntity.addActionListener(e -> { - if (entityWithExtraCreator == null) return; + + Random random = new Random(); + if (entityWithExtraCreator == null) { + entityWithExtraCreator = new EntityWithExtraCreator(20, 20); + for (int i = 0; i < 20; i ++) { + if (random.nextBoolean()) { + entityWithExtraCreator.Insert(new EntityLocomotive(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + } + else { + entityWithExtraCreator.Insert(new EntityWarmlyLocomotive(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 < 20; i ++) { + int extraRand = random.nextInt(3); + switch (extraRand) { + case 0: + entityWithExtraCreator.Insert(new ExtraWheelsDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 1: + entityWithExtraCreator.Insert(new ExtraStarWheelDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 2: + entityWithExtraCreator.Insert(new ExtraRoundWheelDraw(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + } + } + } _locomotiveFirst = entityWithExtraCreator.getEntityWithExtra(); _locomotiveFirst.SetPosition(200, 200, formFrame.getWidth(), formFrame.getHeight() - 75); @@ -81,6 +74,8 @@ public class FormEntityWithExtraGallery extends JComponent { formFrame.getContentPane().add(this); + + formFrame.setVisible(true); } @Override