Правки. Сданная сложная Лаб 3

This commit is contained in:
Данила Мочалов 2022-10-25 16:54:14 +04:00
parent 89ab094b5b
commit 55e0a35928
2 changed files with 47 additions and 56 deletions

View File

@ -2,33 +2,29 @@ import java.lang.reflect.Array;
import java.util.Random; import java.util.Random;
public class EntityWithExtraCreator <T extends EntityLocomotive, U extends IDrawningExtra> { public class EntityWithExtraCreator <T extends EntityLocomotive, U extends IDrawningExtra> {
private T[] entityArr; private final Object[] entityArr;
private U[] extraArr; private final Object[] extraArr;
int entitiesCount = 0; int entitiesCount = 0;
int extraCount = 0; int extraCount = 0;
public EntityWithExtraCreator(int countEntities, int countExtra) { public EntityWithExtraCreator(int countEntities, int countExtra) {
entityArr = (T[]) new EntityLocomotive[countEntities]; entityArr = new Object[countEntities];
extraArr = (U[]) new IDrawningExtra[countExtra]; extraArr = new Object[countExtra];
} }
public int Insert(T entityLocomotive) { public void Insert(T entityLocomotive) {
if(entitiesCount < entityArr.length) { if(entitiesCount < entityArr.length) {
entityArr[entitiesCount] = entityLocomotive; entityArr[entitiesCount] = entityLocomotive;
entitiesCount++; entitiesCount++;
return entitiesCount - 1;
} }
return -1;
} }
public int Insert (U extra) { public void Insert (U extra) {
if(extraCount < extraArr.length) { if(extraCount < extraArr.length) {
extraArr[extraCount] = extra; extraArr[extraCount] = extra;
extraCount++; extraCount++;
return extraCount - 1;
} }
return -1;
} }
public DrawningLocomotive getEntityWithExtra() { public DrawningLocomotive getEntityWithExtra() {
@ -36,8 +32,8 @@ public class EntityWithExtraCreator <T extends EntityLocomotive, U extends IDraw
int getEntityRandomIndex = random.nextInt(entityArr.length); int getEntityRandomIndex = random.nextInt(entityArr.length);
int getExtraRandomIndex = random.nextInt(extraArr.length); int getExtraRandomIndex = random.nextInt(extraArr.length);
EntityLocomotive locomotive = entityArr[getEntityRandomIndex]; EntityLocomotive locomotive = (T)entityArr[getEntityRandomIndex];
IDrawningExtra extra = extraArr[getExtraRandomIndex]; IDrawningExtra extra = (U)extraArr[getExtraRandomIndex];
if (locomotive instanceof EntityWarmlyLocomotive) { if (locomotive instanceof EntityWarmlyLocomotive) {
return new DrawningWarmlyLocomotive(locomotive, extra); return new DrawningWarmlyLocomotive(locomotive, extra);

View File

@ -10,9 +10,8 @@ public class FormEntityWithExtraGallery extends JComponent {
public FormEntityWithExtraGallery() { public FormEntityWithExtraGallery() {
JFrame formFrame = new JFrame("Gallery"); JFrame formFrame = new JFrame("Gallery");
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); formFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
formFrame.setSize(900, 500); formFrame.setSize(900, 500);
formFrame.setVisible(true);
formFrame.setLocationRelativeTo(null); formFrame.setLocationRelativeTo(null);
Panel statusPanel = new Panel(); Panel statusPanel = new Panel();
@ -21,15 +20,15 @@ public class FormEntityWithExtraGallery extends JComponent {
setLayout(new BorderLayout()); setLayout(new BorderLayout());
add(statusPanel, BorderLayout.SOUTH); add(statusPanel, BorderLayout.SOUTH);
Label speedLabel = new Label("Speed: ");
Label weightLabel = new Label("Weight: ");
Label colorLabel = new Label("Color: ");
JButton randomlyFillArraysWithParts = new JButton("Randomly fill arrays with parts");
randomlyFillArraysWithParts.addActionListener(e -> { JButton showRandomEntity = new JButton("Create entity from parts");
showRandomEntity.addActionListener(e -> {
Random random = new Random(); Random random = new Random();
entityWithExtraCreator = new EntityWithExtraCreator<>(100, 100); if (entityWithExtraCreator == null) {
for (int i = 0; i < 100; i ++) { entityWithExtraCreator = new EntityWithExtraCreator<EntityLocomotive, IDrawningExtra>(20, 20);
for (int i = 0; i < 20; i ++) {
if (random.nextBoolean()) { if (random.nextBoolean()) {
entityWithExtraCreator.Insert(new EntityLocomotive(random.nextInt(100), random.nextInt(100), entityWithExtraCreator.Insert(new EntityLocomotive(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))));
@ -41,7 +40,7 @@ public class FormEntityWithExtraGallery extends JComponent {
random.nextBoolean(), random.nextBoolean())); random.nextBoolean(), random.nextBoolean()));
} }
} }
for (int i = 0; i < 100; i ++) { for (int i = 0; i < 20; i ++) {
int extraRand = random.nextInt(3); int extraRand = random.nextInt(3);
switch (extraRand) { switch (extraRand) {
case 0: case 0:
@ -60,13 +59,7 @@ public class FormEntityWithExtraGallery extends JComponent {
break; break;
} }
} }
repaint(); }
});
statusPanel.add(randomlyFillArraysWithParts);
JButton showRandomEntity = new JButton("Create entity from parts");
showRandomEntity.addActionListener(e -> {
if (entityWithExtraCreator == null) return;
_locomotiveFirst = entityWithExtraCreator.getEntityWithExtra(); _locomotiveFirst = entityWithExtraCreator.getEntityWithExtra();
_locomotiveFirst.SetPosition(200, 200, formFrame.getWidth(), formFrame.getHeight() - 75); _locomotiveFirst.SetPosition(200, 200, formFrame.getWidth(), formFrame.getHeight() - 75);
@ -81,6 +74,8 @@ public class FormEntityWithExtraGallery extends JComponent {
formFrame.getContentPane().add(this); formFrame.getContentPane().add(this);
formFrame.setVisible(true);
} }
@Override @Override