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