From 436407133226afe72e36f667b4dbac7103bda10a Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 22 Oct 2022 16:12:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BE=D1=82=D1=80=D0=B8=D1=81=D0=BE=D0=B2=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8=D1=82=D0=B5=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?.=20=D0=92=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=20=D1=82?= =?UTF-8?q?=D1=80=D0=B5=D0=B1=D1=83=D0=B5=D1=82=D1=81=D1=8F=20=D1=83=D1=82?= =?UTF-8?q?=D0=BE=D1=87=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B5=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EntityWithExtraCreator.java | 12 ++--- FormEntityWithExtraGallery.java | 95 +++++++++++++++++++++++++++++++++ FormMapWithSetLocomotives.java | 11 +++- 3 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 FormEntityWithExtraGallery.java diff --git a/EntityWithExtraCreator.java b/EntityWithExtraCreator.java index b037325..239ec49 100644 --- a/EntityWithExtraCreator.java +++ b/EntityWithExtraCreator.java @@ -1,15 +1,15 @@ import java.util.Random; public class EntityWithExtraCreator { - 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 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(); + } +} diff --git a/FormMapWithSetLocomotives.java b/FormMapWithSetLocomotives.java index a6d8d69..197144d 100644 --- a/FormMapWithSetLocomotives.java +++ b/FormMapWithSetLocomotives.java @@ -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