Правки. Сданная сложная Лаб 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;
public class EntityWithExtraCreator <T extends EntityLocomotive, U extends IDrawningExtra> {
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 <T extends EntityLocomotive, U extends IDraw
int getEntityRandomIndex = random.nextInt(entityArr.length);
int getExtraRandomIndex = random.nextInt(extraArr.length);
EntityLocomotive locomotive = entityArr[getEntityRandomIndex];
IDrawningExtra extra = extraArr[getExtraRandomIndex];
EntityLocomotive locomotive = (T)entityArr[getEntityRandomIndex];
IDrawningExtra extra = (U)extraArr[getExtraRandomIndex];
if (locomotive instanceof EntityWarmlyLocomotive) {
return new DrawningWarmlyLocomotive(locomotive, extra);

View File

@ -10,9 +10,8 @@ public class FormEntityWithExtraGallery extends JComponent {
public FormEntityWithExtraGallery() {
JFrame formFrame = new JFrame("Gallery");
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
formFrame.setSize(900, 500);
formFrame.setVisible(true);
formFrame.setLocationRelativeTo(null);
Panel statusPanel = new Panel();
@ -21,52 +20,46 @@ public class FormEntityWithExtraGallery extends JComponent {
setLayout(new BorderLayout());
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 -> {
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<EntityLocomotive, IDrawningExtra>(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