diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java index a4f4099..a76d4f4 100644 --- a/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningElectricLocomotive.java @@ -10,12 +10,14 @@ public class DrawningElectricLocomotive extends DrawningLocomotive { Color bodyColor, Color additionalColor, int typeWheels, int countWheels, boolean horns, boolean battery, - int width, int height){ + int width, int height) { super(speed, weight, bodyColor, typeWheels, countWheels, width, height, 120, 70); - if (entityLocomotive != null){ - entityLocomotive = new EntityElectricLocomotive( - speed, weight, bodyColor, additionalColor, horns, battery); - } + entityLocomotive = new EntityElectricLocomotive( + speed, weight, bodyColor, additionalColor, horns, battery); + + } + public DrawningElectricLocomotive(EntityLocomotive entity, IDrawWheels drawingWheels, int width, int height) { + super(entity, drawingWheels, width, height); } @Override public void DrawTransport(Graphics g) { diff --git a/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java b/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java index ef2816a..65f23ff 100644 --- a/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java +++ b/ProjectElectricLocomotive/src/DrawningObjects/DrawningLocomotive.java @@ -85,6 +85,12 @@ public class DrawningLocomotive { entityLocomotive = new EntityLocomotive(speed, weight, bodyColor); } + public DrawningLocomotive(EntityLocomotive entity, IDrawWheels drawWheels, int width, int height) { + entityLocomotive = entity; + wheels = drawWheels; + _pictureWidth = width; + _pictureHeight = height; + } public void SetPosition(int x, int y) { x = Math.min(Math.max(0, x), _pictureWidth - _locomotiveWidth); diff --git a/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java index 2646f71..bfb889d 100644 --- a/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java @@ -13,6 +13,7 @@ public class EntityElectricLocomotive extends EntityLocomotive { Color bodyColor, Color additionalColor, boolean horns, boolean battery) { super(speed, weight, bodyColor); + AdditionalColor = additionalColor; Horns = horns; Battery = battery; } diff --git a/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.form b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.form new file mode 100644 index 0000000..084ae48 --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.form @@ -0,0 +1,26 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.java b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.java new file mode 100644 index 0000000..1f263be --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotiveDop.java @@ -0,0 +1,79 @@ +package Forms; + +import DrawningObjects.*; +import Entities.EntityElectricLocomotive; +import Entities.EntityLocomotive; +import Generics.DopLocomotiveGenericCollection; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.util.Random; + +public class FormGenerateLocomotiveDop extends JFrame { + private Random _random; + private DrawningLocomotive _drawingLocomotive; + private DopLocomotiveGenericCollection _genericDop; + private JButton generateLocomotiveButton; + private JPanel PictureBox; + + public JPanel getPictureBox() { + return PictureBox; + } + public FormGenerateLocomotiveDop(){ + _random = new Random(); + _genericDop = new DopLocomotiveGenericCollection<>(); + PictureBox.setPreferredSize(new Dimension(400, 400)); + generateLocomotiveButton.addActionListener(this::generateLocomotive); + } + + private void addRandomEntity(){ + EntityLocomotive entityLocomotive; + if (_random.nextBoolean()){ + entityLocomotive = new EntityLocomotive( + _random.nextInt(100, 300), + _random.nextInt(1000, 3000), + new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)) + ); + } else { + entityLocomotive = new EntityElectricLocomotive( + _random.nextInt(100, 300), + _random.nextInt(1000, 3000), + new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)), + new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)), + _random.nextBoolean(), + _random.nextBoolean() + ); + } + _genericDop.add(entityLocomotive); + } + private void addRandomWheels(){ + int choice = _random.nextInt(3); + IDrawWheels drawWheels; + if (choice == 0){ + drawWheels = new DrawningWheels(); + } else if (choice == 1){ + drawWheels = new DrawningWheelsStar(); + } else { + drawWheels = new DrawningWheelsPolygon(); + } + drawWheels.setWheelsCount(_random.nextInt(2, 4)); + _genericDop.add(drawWheels); + } + private void generateLocomotive(ActionEvent e){ + addRandomEntity(); + addRandomWheels(); + _drawingLocomotive = _genericDop.GenerateLocomotive(PictureBox.getWidth(), PictureBox.getHeight()); + _drawingLocomotive.SetPosition((PictureBox.getWidth() - _drawingLocomotive.GetWidth()) / 2, + (PictureBox.getHeight() - _drawingLocomotive.GetHeight()) / 2); + Draw(); + } + public void Draw() { + if (_drawingLocomotive == null) { + return; + } + Graphics g = PictureBox.getGraphics(); + PictureBox.paint(g); + _drawingLocomotive.DrawTransport(g); + } +} diff --git a/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java index 62f7177..d185463 100644 --- a/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java +++ b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java @@ -12,9 +12,11 @@ import java.awt.event.ActionListener; public class FormLocomotiveCollection extends JFrame { private final LocomotivesGenericCollection _locomotives; private Canvas canvas; + private FrameGenerateLocomotiveDop _formGenerateLocomotiveDop; private JButton buttonAddLocomotive; private JButton buttonRemoveLocomotive; private JButton buttonRefreshCollection; + private JButton buttonOpenGenerateWindow; private JTextField textBoxNumber; private JPanel buttonBox; @@ -42,6 +44,9 @@ public class FormLocomotiveCollection extends JFrame { buttonRefreshCollection = new JButton("Обновить колекцию"); buttonRefreshCollection.setMargin(new Insets(0, 0, 0, 0)); + buttonOpenGenerateWindow = new JButton("Открыть окно генерации"); + buttonOpenGenerateWindow.setMargin(new Insets(0, 0, 0, 0)); + buttonBox = new JPanel(new GridLayout(0, 1, 0, 10)); JLabel labelBB = new JLabel("Инструменты"); @@ -51,14 +56,17 @@ public class FormLocomotiveCollection extends JFrame { buttonBox.add(buttonRemoveLocomotive); buttonBox.add(buttonRefreshCollection); + buttonBox.setBounds(760, 3, 200, 220); canvas.setBounds(10, 3, 740, 700); + buttonOpenGenerateWindow.setBounds(760, 600, 200, 40); setSize(1000, 700); setTitle("Колекция локомотивов"); setLayout(null); add(buttonBox); + add(buttonOpenGenerateWindow); add(canvas); } void InitializeLogic(){ @@ -122,6 +130,17 @@ public class FormLocomotiveCollection extends JFrame { repaint(); } }); + + buttonOpenGenerateWindow.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_formGenerateLocomotiveDop != null) { + _formGenerateLocomotiveDop.dispose(); + } + _formGenerateLocomotiveDop = new FrameGenerateLocomotiveDop(); + _formGenerateLocomotiveDop.setVisible(true); + } + }); } public FormLocomotiveCollection(){ InitializeComponent(); diff --git a/ProjectElectricLocomotive/src/Forms/FrameGenerateLocomotiveDop.java b/ProjectElectricLocomotive/src/Forms/FrameGenerateLocomotiveDop.java new file mode 100644 index 0000000..6792b87 --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FrameGenerateLocomotiveDop.java @@ -0,0 +1,17 @@ +package Forms; + +import javax.swing.*; + +public class FrameGenerateLocomotiveDop extends JFrame { + public FormGenerateLocomotiveDop formGenerateLocomotiveDop; + public FrameGenerateLocomotiveDop(){ + super(); + setTitle("Генерация локомотивов"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + formGenerateLocomotiveDop = new FormGenerateLocomotiveDop(); + setContentPane(formGenerateLocomotiveDop.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(300, 100); + pack(); + } +} diff --git a/ProjectElectricLocomotive/src/Generics/DopLocomotiveGenericCollection.java b/ProjectElectricLocomotive/src/Generics/DopLocomotiveGenericCollection.java new file mode 100644 index 0000000..5f92817 --- /dev/null +++ b/ProjectElectricLocomotive/src/Generics/DopLocomotiveGenericCollection.java @@ -0,0 +1,45 @@ +package Generics; + +import DrawningObjects.DrawningElectricLocomotive; +import DrawningObjects.DrawningLocomotive; +import DrawningObjects.IDrawWheels; +import Entities.EntityElectricLocomotive; +import Entities.EntityLocomotive; + +import java.util.ArrayList; +import java.util.Random; + +public class DopLocomotiveGenericCollection { + private ArrayList _entitiesStorage; + private ArrayList _drawWheelsStorage; + + public DopLocomotiveGenericCollection(){ + _entitiesStorage = new ArrayList<>(); + _drawWheelsStorage = new ArrayList<>(); + } + public void add(T obj){ + _entitiesStorage.add(obj); + } + public void add(U obj){ + _drawWheelsStorage.add(obj); + } + + public DrawningLocomotive GenerateLocomotive(int pictureWidth, int pictureHeight) { + Random random = new Random(); + if (_entitiesStorage.isEmpty()) { + return null; + } + T entityLocomotive = _entitiesStorage.get(random.nextInt(_entitiesStorage.size())); + U drawingWheels = null; + if (!_drawWheelsStorage.isEmpty()) { + drawingWheels = _drawWheelsStorage.get(random.nextInt(_drawWheelsStorage.size())); + } + DrawningLocomotive drawingLocomotive; + if (entityLocomotive instanceof EntityElectricLocomotive) { + drawingLocomotive = new DrawningElectricLocomotive((EntityLocomotive) entityLocomotive, drawingWheels, pictureWidth, pictureHeight); + } else { + drawingLocomotive = new DrawningLocomotive(entityLocomotive, drawingWheels, pictureWidth, pictureHeight); + } + return drawingLocomotive; + } +} diff --git a/ProjectElectricLocomotive/src/Generics/SetGeneric.java b/ProjectElectricLocomotive/src/Generics/SetGeneric.java index de5521b..5d5748a 100644 --- a/ProjectElectricLocomotive/src/Generics/SetGeneric.java +++ b/ProjectElectricLocomotive/src/Generics/SetGeneric.java @@ -27,7 +27,7 @@ public class SetGeneric { for (int i = fillLineEnd; i > position; --i){ _places[i] = _places[i - 1]; } - _places[fillLineEnd] = obj; + _places[position] = obj; return true; } boolean Insert(T obj){