Добавлена форма для отрисовки объектов дополнительного класса. Возможно требуется уточнение ее реализации...

This commit is contained in:
Данила Мочалов 2022-10-22 16:12:48 +04:00
parent 9acaae8ff2
commit 4364071332
3 changed files with 110 additions and 8 deletions

View File

@ -1,15 +1,15 @@
import java.util.Random;
public class EntityWithExtraCreator <T extends EntityLocomotive, U extends IDrawningExtra> {
private T[] entityArr;
private U[] extraArr;
private EntityLocomotive[] entityArr;
private IDrawningExtra[] extraArr;
int entitiesCount = 0;
int extraCount = 0;
public EntityWithExtraCreator(int countEntities, int countExtra) {
entityArr = (T[]) new Object[countEntities];
extraArr = (U[]) new Object[countExtra];
entityArr = new EntityLocomotive[countEntities];
extraArr = new IDrawningExtra[countExtra];
}
public int Insert(T entityLocomotive) {
@ -35,8 +35,8 @@ public class EntityWithExtraCreator <T extends EntityLocomotive, U extends IDraw
int getEntityRandomIndex = random.nextInt(entityArr.length);
int getExtraRandomIndex = random.nextInt(extraArr.length);
T locomotive = entityArr[getEntityRandomIndex];
U extra = extraArr[getExtraRandomIndex];
EntityLocomotive locomotive = entityArr[getEntityRandomIndex];
IDrawningExtra extra = extraArr[getExtraRandomIndex];
if (locomotive instanceof EntityWarmlyLocomotive) {
return new DrawningWarmlyLocomotive(locomotive, extra);

View File

@ -0,0 +1,95 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FormEntityWithExtraGallery extends JComponent {
private DrawningLocomotive _locomotiveFirst;
private DrawningLocomotive _locomotiveSecond;
private DrawningLocomotive _locomotiveThird;
EntityWithExtraCreator<EntityLocomotive, IDrawningExtra> entityWithExtraCreator;
public FormEntityWithExtraGallery() {
JFrame formFrame = new JFrame("Gallery");
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formFrame.setSize(900, 500);
formFrame.setVisible(true);
formFrame.setLocationRelativeTo(null);
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new FlowLayout());
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;
_locomotiveFirst = entityWithExtraCreator.getEntityWithExtra();
_locomotiveFirst.SetPosition(200, 200, formFrame.getWidth(), formFrame.getHeight() - 75);
_locomotiveSecond = entityWithExtraCreator.getEntityWithExtra();
_locomotiveSecond.SetPosition(400, 200, formFrame.getWidth(), formFrame.getHeight() - 75);
_locomotiveThird = entityWithExtraCreator.getEntityWithExtra();
_locomotiveThird.SetPosition(600, 200, formFrame.getWidth(), formFrame.getHeight() - 75);
repaint();
});
statusPanel.add(showRandomEntity);
formFrame.getContentPane().add(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (_locomotiveFirst != null) _locomotiveFirst.DrawTransport(g2);
if (_locomotiveSecond != null) _locomotiveSecond.DrawTransport(g2);
if (_locomotiveThird != null) _locomotiveThird.DrawTransport(g2);
super.repaint();
}
}

View File

@ -15,7 +15,7 @@ public class FormMapWithSetLocomotives extends JComponent {
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formFrame.setSize(750, 500);
formFrame.setLocationRelativeTo(null);
formFrame.setVisible(true);
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
@ -194,8 +194,15 @@ public class FormMapWithSetLocomotives extends JComponent {
statusPanel.add(moveLeftButton);
statusPanel.add(moveRightButton);
JButton showGalleryButton = new JButton("Show Gallery");
showGalleryButton.addActionListener(e -> {
new FormEntityWithExtraGallery();
});
statusPanel.add(showGalleryButton);
formFrame.getContentPane().add(this);
super.repaint();
formFrame.setVisible(true);
//super.repaint();
}
@Override