From 506a4eb234df123f1ede895fe4d16e9a0ce8bf28 Mon Sep 17 00:00:00 2001 From: BoiledMilk123 Date: Tue, 4 Jun 2024 23:53:41 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=B7=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B8?= =?UTF-8?q?=D0=BB=20labwork03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectElectricLocomotive/Main.java | 6 +- .../AbstractCompany.java | 73 ++++++++++ .../ICollectionGenericObjects.java | 18 +++ .../LocomotiveDepotService.java | 57 ++++++++ .../MassiveGenericObjects.java | 74 ++++++++++ .../SpecialGenerateLocomotive.java | 76 +++++++++++ .../src/Drawnings/DrawingOrnamentWheels.java | 2 +- .../src/Drawnings/DrawingWheels.java | 4 +- .../DrawningSecondOrnamentWheels.java | 2 +- .../{Drawnings => Forms}/DrawingField.java | 44 ++---- .../src/Forms/DrawingGenerateLocomotive.java | 84 ++++++++++++ .../Forms/DrawingLocomotiveCollection.java | 123 +++++++++++++++++ .../{ => Forms}/FormElectricLocomotive.java | 29 ++-- .../src/Forms/FormGenerateLocomotive.java | 111 +++++++++++++++ .../src/Forms/FormLocomotiveCollection.java | 129 ++++++++++++++++++ 15 files changed, 772 insertions(+), 60 deletions(-) create mode 100644 ProjectElectricLocomotive/src/CollectionGenericObjects/AbstractCompany.java create mode 100644 ProjectElectricLocomotive/src/CollectionGenericObjects/ICollectionGenericObjects.java create mode 100644 ProjectElectricLocomotive/src/CollectionGenericObjects/LocomotiveDepotService.java create mode 100644 ProjectElectricLocomotive/src/CollectionGenericObjects/MassiveGenericObjects.java create mode 100644 ProjectElectricLocomotive/src/CollectionGenericObjects/SpecialGenerateLocomotive.java rename ProjectElectricLocomotive/src/{Drawnings => Forms}/DrawingField.java (63%) create mode 100644 ProjectElectricLocomotive/src/Forms/DrawingGenerateLocomotive.java create mode 100644 ProjectElectricLocomotive/src/Forms/DrawingLocomotiveCollection.java rename ProjectElectricLocomotive/src/{ => Forms}/FormElectricLocomotive.java (83%) create mode 100644 ProjectElectricLocomotive/src/Forms/FormGenerateLocomotive.java create mode 100644 ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java diff --git a/ProjectElectricLocomotive/Main.java b/ProjectElectricLocomotive/Main.java index e6973ff..af234d3 100644 --- a/ProjectElectricLocomotive/Main.java +++ b/ProjectElectricLocomotive/Main.java @@ -1,10 +1,12 @@ package ProjectElectricLocomotive; -import ProjectElectricLocomotive.src.*; +import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive; +import ProjectElectricLocomotive.src.Forms.FormElectricLocomotive; +import ProjectElectricLocomotive.src.Forms.FormLocomotiveCollection; public class Main { public static void main(String[] args) { - new FormElectricLocomotive(); + new FormLocomotiveCollection(); } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/AbstractCompany.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/AbstractCompany.java new file mode 100644 index 0000000..006929e --- /dev/null +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/AbstractCompany.java @@ -0,0 +1,73 @@ +package ProjectElectricLocomotive.src.CollectionGenericObjects; + +import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive; + +import javax.swing.*; +import java.awt.image.BufferedImage; +import java.awt.*; +import java.util.Optional; +import java.util.Random; + +public abstract class AbstractCompany +{ + protected final int _placeSizeWidth = 100; + + protected final int _placeSizeHeight = 65; + + protected int _pictureWidth; + + protected int _pictureHeight; + + protected ICollectionGenericObjects _collection = null; + + private int getMaxCount() { + return _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); + } + + public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.setMaxCount(getMaxCount()); + } + + public int add(DrawingLocomotive ship) { + if (_collection == null) { + return -1; + } + return _collection.insert(ship); + } + + public DrawingLocomotive remove(AbstractCompany company, int position) { + if(company._collection == null) return null; + return company._collection.remove(position); + } + + + public DrawingLocomotive GetRandomObjects() + { + if(_collection == null) return null; + Random rnd = new Random(); + int maxCount = getMaxCount(); + return _collection.get(rnd.nextInt(maxCount)); + } + public BufferedImage show(JLabel pictureBox) { + BufferedImage bitmap = new BufferedImage(pictureBox.getWidth(), pictureBox.getHeight(), BufferedImage.TYPE_INT_ARGB); + Graphics2D graphics = bitmap.createGraphics(); + DrawBackground(graphics); + SetObjectsPosition(); + for (int i = 0; i < (_collection != null ? _collection.getCount() : 0); ++i) { + if(_collection == null || _collection.get(i) == null) continue; + DrawingLocomotive obj = _collection.get(i); + obj.DrawTransport(graphics); + } + graphics.dispose(); + return bitmap; + } + + + protected abstract void DrawBackground(Graphics graphics); + + protected abstract void SetObjectsPosition(); +} diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/ICollectionGenericObjects.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/ICollectionGenericObjects.java new file mode 100644 index 0000000..c5356c1 --- /dev/null +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -0,0 +1,18 @@ +package ProjectElectricLocomotive.src.CollectionGenericObjects; + +import java.util.Optional; + +public interface ICollectionGenericObjects +{ + int getCount(); + + void setMaxCount(int maxCount); + + int insert(T obj); + + int insert(T obj, int position); + + T remove(int position); + + T get(int position); +} diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/LocomotiveDepotService.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/LocomotiveDepotService.java new file mode 100644 index 0000000..4eb6a8b --- /dev/null +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/LocomotiveDepotService.java @@ -0,0 +1,57 @@ +package ProjectElectricLocomotive.src.CollectionGenericObjects; + +import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive; + +import java.awt.*; +import java.util.ArrayList; +import java.util.List; +import java.util.AbstractMap; + +public class LocomotiveDepotService extends AbstractCompany +{ + private List> locCoord = new ArrayList<>(); + private int countInRow; + + public LocomotiveDepotService(int picWidth, int picHeight, ICollectionGenericObjects collection) + { + super(picWidth, picHeight, collection); + } + @Override + protected void DrawBackground(Graphics g) + { + Color brown = new Color(52, 22, 22); + g.setColor(brown); + int x = 1, y = 0; + while (y + _placeSizeHeight <= _pictureHeight) { + int count = 0; + while (x + _placeSizeWidth <= _pictureWidth) { + count++; + g.drawLine(x, y, x + _placeSizeWidth, y); + g.drawLine(x, y, x, y + _placeSizeHeight); + g.drawLine(x, y + _placeSizeHeight, x + _placeSizeWidth, y + _placeSizeHeight); + locCoord.add(new AbstractMap.SimpleEntry<>(x, y)); + x += _placeSizeWidth + 2; + } + countInRow = count; + x = 1; + y += _placeSizeHeight + 5; + } + } + + @Override + protected void SetObjectsPosition() { + if (locCoord == null || _collection == null) return; + int row = 1, col = 1; + for (int i = 0; i < _collection.getCount(); i++, col++) + { + if(_collection.get(i) == null) continue; + _collection.get(i).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.get(i).SetPosition(locCoord.get(row * countInRow - col).getKey() + 5, locCoord.get(row * countInRow - col).getValue() + 5); + if (col == countInRow) + { + col = 0; + row++; + } + } + } +} diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/MassiveGenericObjects.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/MassiveGenericObjects.java new file mode 100644 index 0000000..11b7612 --- /dev/null +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -0,0 +1,74 @@ +package ProjectElectricLocomotive.src.CollectionGenericObjects; + +import java.util.Optional; + +public class MassiveGenericObjects implements ICollectionGenericObjects +{ + private T[] _collection; + + @Override + public int getCount(){ + return _collection.length; + } + + @Override + public void setMaxCount(int maxCount) { + if (maxCount > 0) { + _collection = (T[]) new Object[maxCount]; + } + } + + public MassiveGenericObjects() { + _collection = (T[]) new Object[0]; // Create an empty array of type T + } + @Override + public int insert(T obj) { + return insert(obj, 0); + } + + @Override + public int insert(T obj, int position) { + for(int i = position; i < getCount(); i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + for(int i = position - 1; i >= 0; --i) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + return -1; + } + + @Override + public T remove(int position) { + if (position < 0 || position >= getCount()) return null; + if (_collection[position] != null) + { + T obj = _collection[position]; + _collection[position] = null; + return obj; + + } + return null; + } + + @Override + public T get(int position) { + if(position >= 0 && position < getCount()) + { + return _collection[position]; + } + else + { + return null; + } + } +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/SpecialGenerateLocomotive.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/SpecialGenerateLocomotive.java new file mode 100644 index 0000000..4256d97 --- /dev/null +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/SpecialGenerateLocomotive.java @@ -0,0 +1,76 @@ +package ProjectElectricLocomotive.src.CollectionGenericObjects; + +import ProjectElectricLocomotive.src.Drawnings.*; +import ProjectElectricLocomotive.src.Entities.EntityElectricLocomotive; +import ProjectElectricLocomotive.src.Entities.EntityLocomotive; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class SpecialGenerateLocomotive +{ + public List entityArray = new ArrayList(); + public List wheelsArray = new ArrayList(); + + public void add(T element) { + entityArray.add(element); + } + + public void add(U element) { + wheelsArray.add(element); + } + + public String getEn(int ind) { + if(entityArray.get(ind) instanceof EntityElectricLocomotive) { + EntityElectricLocomotive entity = (EntityElectricLocomotive)entityArray.get(ind); + return "EntityWarmlyShip{" + + "speed=" + entity.GetSpeed() + + ", weight=" + entity.GetWeight() + + ", bodyColor=" + entity.GetBodyColor() + + ", AdditionalColor=" + entity.GetAdditionalColor() + + ", Horns=" + entity.GetElectricHorns() + + ", BatteryPlacement=" + entity.GetBatteryPlacement() + + ", step=" + entity.GetStep() + + '}'; + } + else{ + return "EntityShip{" + + "speed=" + entityArray.get(ind).GetSpeed() + + ", weight=" + entityArray.get(ind).GetWeight() + + ", bodyColor=" + entityArray.get(ind).GetBodyColor() + + ", step=" + entityArray.get(ind).GetStep() + + '}'; + } + } + public String getDec(int ind) + { + if(wheelsArray.get(ind) instanceof DrawingOrnamentWheels) + { + return "DrawingOrnamentWheels - " + ((DrawingOrnamentWheels) wheelsArray.get(ind))._wheels.GetCountWheels(); + } + else if(wheelsArray.get(ind) instanceof DrawingWheels) + { + return "DrawingWheels - " + ((DrawingWheels) wheelsArray.get(ind))._wheels.GetCountWheels(); + } + else + { + return "DrawingSecondOrnamentWheels - " + ((DrawningSecondOrnamentWheels) wheelsArray.get(ind))._wheels.GetCountWheels(); + } + } + public DrawingLocomotive createDrawingObject() { + Random rnd = new Random(); + T _objectFromArray = entityArray.get(rnd.nextInt(entityArray.size())); + DrawingLocomotive _locomotiveCreated; + if (_objectFromArray instanceof EntityElectricLocomotive) { + DrawingElectricLocomotive _electricLocomotive = new DrawingElectricLocomotive(_objectFromArray.GetSpeed(), _objectFromArray.GetWeight(), _objectFromArray.GetBodyColor(), ((EntityElectricLocomotive) _objectFromArray).GetAdditionalColor(), ((EntityElectricLocomotive) _objectFromArray).GetElectricHorns(), ((EntityElectricLocomotive) _objectFromArray).GetBatteryPlacement()); + _electricLocomotive._ornament = wheelsArray.get(rnd.nextInt(wheelsArray.size())); + _locomotiveCreated = _electricLocomotive; + } else { + _locomotiveCreated = new DrawingLocomotive(_objectFromArray.GetSpeed(), _objectFromArray.GetWeight(), _objectFromArray.GetBodyColor()); + } + return _locomotiveCreated; + } + + +} diff --git a/ProjectElectricLocomotive/src/Drawnings/DrawingOrnamentWheels.java b/ProjectElectricLocomotive/src/Drawnings/DrawingOrnamentWheels.java index a7377a7..0f15146 100644 --- a/ProjectElectricLocomotive/src/Drawnings/DrawingOrnamentWheels.java +++ b/ProjectElectricLocomotive/src/Drawnings/DrawingOrnamentWheels.java @@ -6,7 +6,7 @@ import java.awt.*; public class DrawingOrnamentWheels implements IDrawingOrnament { - private CountWheels _wheels; + public CountWheels _wheels; @Override public void SetCountWheels(int count) { diff --git a/ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java b/ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java index 42c20e9..91f3435 100644 --- a/ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java +++ b/ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java @@ -5,7 +5,7 @@ import ProjectElectricLocomotive.src.CountWheels; import java.awt.*; public class DrawingWheels implements IDrawingOrnament { - private CountWheels _wheels; + public CountWheels _wheels; @Override public void SetCountWheels(int count) { @@ -19,6 +19,8 @@ public class DrawingWheels implements IDrawingOrnament } } + + @Override public void DrawWheels(Graphics g,int _startPosX, int _startPosY) { diff --git a/ProjectElectricLocomotive/src/Drawnings/DrawningSecondOrnamentWheels.java b/ProjectElectricLocomotive/src/Drawnings/DrawningSecondOrnamentWheels.java index cc5ded6..445118e 100644 --- a/ProjectElectricLocomotive/src/Drawnings/DrawningSecondOrnamentWheels.java +++ b/ProjectElectricLocomotive/src/Drawnings/DrawningSecondOrnamentWheels.java @@ -6,7 +6,7 @@ import java.awt.*; public class DrawningSecondOrnamentWheels implements IDrawingOrnament { - private CountWheels _wheels; + public CountWheels _wheels; @Override public void SetCountWheels(int count) { diff --git a/ProjectElectricLocomotive/src/Drawnings/DrawingField.java b/ProjectElectricLocomotive/src/Forms/DrawingField.java similarity index 63% rename from ProjectElectricLocomotive/src/Drawnings/DrawingField.java rename to ProjectElectricLocomotive/src/Forms/DrawingField.java index 15d1bba..8b9d84e 100644 --- a/ProjectElectricLocomotive/src/Drawnings/DrawingField.java +++ b/ProjectElectricLocomotive/src/Forms/DrawingField.java @@ -1,14 +1,12 @@ -package ProjectElectricLocomotive.src.Drawnings; +package ProjectElectricLocomotive.src.Forms; import ProjectElectricLocomotive.src.DirectionType; -import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive; -import ProjectElectricLocomotive.src.FormElectricLocomotive; +import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive; import ProjectElectricLocomotive.src.MovementStrategy.*; import javax.swing.*; import java.awt.*; -import java.util.Random; public class DrawingField extends JPanel { private final FormElectricLocomotive field; @@ -17,6 +15,14 @@ public class DrawingField extends JPanel { public DrawingField(FormElectricLocomotive field) { this.field = field; } + + public void setLocomotive(DrawingLocomotive locomotive) { + _drawingLocomotive = locomotive; + _drawingLocomotive.SetPictureSize(getWidth(),getHeight()); + field.comboBoxStrategy.setEnabled(true); + _strategy = null; + } + @Override protected void paintComponent(Graphics g) { @@ -43,36 +49,6 @@ public class DrawingField extends JPanel { if (_drawingLocomotive == null) return; _drawingLocomotive.MoveTransport(DirectionType.Left); } - public void CreateObject(String type){ - Random rnd = new Random(); - switch (type) - { - case "DrawningLocomotive": - _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), - rnd.nextInt(1000, 3000), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); - break; - case "DrawningElectricLocomotive": - _drawingLocomotive = new DrawingElectricLocomotive(rnd.nextInt(100, 300), - rnd.nextInt(1000, 3000), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - rnd.nextBoolean(), - rnd.nextBoolean()); - break; - default: - return; - } - _drawingLocomotive.SetPictureSize(getWidth(),getHeight()); - _drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); - } - - public void CreateButtonElectricLocomotiveAction(){ - CreateObject("DrawningElectricLocomotive"); - } - public void CreateButtonLocomotiveAction(){ - CreateObject("DrawningLocomotive"); - } public void ButtonStrategyStepAction() { diff --git a/ProjectElectricLocomotive/src/Forms/DrawingGenerateLocomotive.java b/ProjectElectricLocomotive/src/Forms/DrawingGenerateLocomotive.java new file mode 100644 index 0000000..39bc686 --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/DrawingGenerateLocomotive.java @@ -0,0 +1,84 @@ +package ProjectElectricLocomotive.src.Forms; + +import ProjectElectricLocomotive.src.CollectionGenericObjects.SpecialGenerateLocomotive; +import ProjectElectricLocomotive.src.Drawnings.*; +import ProjectElectricLocomotive.src.Entities.EntityElectricLocomotive; +import ProjectElectricLocomotive.src.Entities.EntityLocomotive; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawingGenerateLocomotive extends JPanel +{ + private final FormGenerateLocomotive field; + protected SpecialGenerateLocomotive _generateLocomotive; + private DrawingLocomotive _locomotive = null; + + public void setGenerateLocomotive() + { + _generateLocomotive = new SpecialGenerateLocomotive(); + for(int i = 0; i < 3; i++) + { + Random rnd = new Random(); + EntityLocomotive _EntityLocomotive; + int shipNum = rnd.nextInt(2); + switch (shipNum){ + case 1: + _EntityLocomotive = new EntityLocomotive(rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + break; + default: + _EntityLocomotive = new EntityElectricLocomotive(rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), + rnd.nextBoolean()); + break; + } + _generateLocomotive.add(_EntityLocomotive); + + IDrawingOrnament _wheels; + int varDecks = rnd.nextInt(3); + switch (varDecks) + { + case 0: + _wheels = new DrawingWheels(); + break; + case 1: + _wheels = new DrawingOrnamentWheels(); + break; + default: + _wheels = new DrawningSecondOrnamentWheels(); + break; + } + _wheels.SetCountWheels(rnd.nextInt(1,4)); + _generateLocomotive.add(_wheels); + } + } + @Override + protected void paintComponent(Graphics g) + { + if (_locomotive == null) return; + _locomotive.DrawTransport(g); + //repaint(); + } + public DrawingGenerateLocomotive(FormGenerateLocomotive field) { + this.field = field; + setGenerateLocomotive(); + + } + public void createNewObject() + { + _locomotive = _generateLocomotive.createDrawingObject(); + _locomotive.SetPictureSize(field.getWidth(), field.getHeight()); + _locomotive.SetPosition(field.panelLocomotive.getWidth()/2 - 50, field.getHeight()/2 - 50); + } + public void insertCollection(){ + if(_locomotive == null) return; + field._collectionForm.insertNewLocomotive(_locomotive); + //createNewObject(); + } +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/src/Forms/DrawingLocomotiveCollection.java b/ProjectElectricLocomotive/src/Forms/DrawingLocomotiveCollection.java new file mode 100644 index 0000000..3ecdbb8 --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/DrawingLocomotiveCollection.java @@ -0,0 +1,123 @@ +package ProjectElectricLocomotive.src.Forms; + +import ProjectElectricLocomotive.src.CollectionGenericObjects.AbstractCompany; +import ProjectElectricLocomotive.src.CollectionGenericObjects.LocomotiveDepotService; +import ProjectElectricLocomotive.src.CollectionGenericObjects.MassiveGenericObjects; +import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive; +import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawingLocomotiveCollection extends JPanel +{ + private final FormLocomotiveCollection field; + private AbstractCompany _company = null; + + public DrawingLocomotiveCollection(FormLocomotiveCollection field) { + this.field = field; + } + void comboBoxSelectorCompany_SelectedIndexChanged(String text) + { + switch (text) + { + case "Хранилище": + _company = new LocomotiveDepotService(field.pictureBox.getWidth(), field.pictureBox.getHeight(), new MassiveGenericObjects()); + break; + } + } + + public void insertNewLocomotive(DrawingLocomotive _drawingLocomotive) + { + if (_company == null) return; + if (_company.add(_drawingLocomotive) != -1) + { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox))); + } + else + { + JOptionPane.showMessageDialog(null, "Объект не удалось добавить"); + } + } + private void CreateObject(String type) + { + if (_company == null) return; + DrawingLocomotive _drawingLocomotive; + Random rnd = new Random(); + switch (type) + { + case "DrawningLocomotive": + _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + getColor(rnd)); + break; + case "DrawningElectricLocomotive": + _drawingLocomotive = new DrawingElectricLocomotive(rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + getColor(rnd), + getColor(rnd), + rnd.nextBoolean(), + rnd.nextBoolean()); + break; + default: + return; + } + insertNewLocomotive(_drawingLocomotive); + } + private static Color getColor(Random random) { + Color initialColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); + Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", initialColor); + if (selectedColor != null) { + return selectedColor; + } else { + return initialColor; + } + } + + public void CreateButtonElectricLocomotiveAction(){ + CreateObject("DrawningElectricLocomotive"); + } + public void CreateButtonLocomotiveAction(){ + CreateObject("DrawningLocomotive"); + } + public void ButtonCreateSpecialLocomotive_Click() + { + FormGenerateLocomotive form = new FormGenerateLocomotive(this); + } + public void ButtonRemoveLocomotive_Click(JFormattedTextField maskedTextField) + { + if (maskedTextField.getText().isEmpty() || _company == null) return; + int pos = Integer.parseInt(maskedTextField.getText()); + int choice = JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); + if (choice == JOptionPane.NO_OPTION) return; + if (_company.remove(_company, pos) != null) { + JOptionPane.showMessageDialog(null, "Объект удален"); + field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox))); + } else { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект"); + } + } + protected void ButtonGoToCheck_Click() + { + if (_company == null) return; + DrawingLocomotive locomotive = null; + int counter = 100; + while (locomotive == null) + { + locomotive = _company.GetRandomObjects(); + counter--; + if(counter <= 0) break; + } + if (locomotive == null) return; + FormElectricLocomotive form = new FormElectricLocomotive(locomotive); + + } + protected void ButtonRefresh_Click() + { + if (_company == null) return; + field.pictureBox.setIcon(new ImageIcon(_company.show(field.pictureBox))); + } +} + diff --git a/ProjectElectricLocomotive/src/FormElectricLocomotive.java b/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java similarity index 83% rename from ProjectElectricLocomotive/src/FormElectricLocomotive.java rename to ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java index 5326b18..6d24bc5 100644 --- a/ProjectElectricLocomotive/src/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/Forms/FormElectricLocomotive.java @@ -1,6 +1,7 @@ -package ProjectElectricLocomotive.src; +package ProjectElectricLocomotive.src.Forms; -import ProjectElectricLocomotive.src.Drawnings.DrawingField; +import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive; +import ProjectElectricLocomotive.src.Forms.DrawingField; import javax.swing.*; import java.awt.*; @@ -26,29 +27,26 @@ public class FormElectricLocomotive extends JFrame{ JButton ButtonStep = new JButton("Шаг"); - JButton ButtonCreateElectricLocomotive=new JButton("CreateElectricLocomotive"); - JButton ButtonCreateLocomotive=new JButton("CreateLocomotive"); - public JComboBox comboBoxStrategy = new JComboBox<>(); - JButton ButtonUp=new JButton("Up"); + JButton ButtonUp=new JButton(); - JButton ButtonDown=new JButton("Down"); + JButton ButtonDown=new JButton(); JButton ButtonRight=new JButton("Right"); JButton ButtonLeft=new JButton("Left"); - public FormElectricLocomotive(){ - super("Electric Locomotive"); + public FormElectricLocomotive(DrawingLocomotive _drawingLocomotive){ + super("Тесты"); setSize(900,500); Width = getWidth(); Height = getHeight(); setBackground(Color.WHITE); ShowWindow(); + field.setLocomotive(_drawingLocomotive); RefreshWindow(); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } @@ -103,17 +101,6 @@ public class FormElectricLocomotive extends JFrame{ CreatePanel.setLayout(new FlowLayout()); CreatePanel.setBackground(new Color(0,0,0,0)); - CreatePanel.add(ButtonCreateLocomotive); - ButtonCreateLocomotive.addActionListener(e->{ - field.CreateButtonLocomotiveAction(); - repaint(); - }); - - CreatePanel.add(ButtonCreateElectricLocomotive); - ButtonCreateElectricLocomotive.addActionListener(e->{ - field.CreateButtonElectricLocomotiveAction(); - repaint(); - }); comboBoxStrategy.setPreferredSize(new Dimension(80, 40)); comboBoxStrategy.addItem("К центру"); comboBoxStrategy.addItem("К краю"); diff --git a/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotive.java b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotive.java new file mode 100644 index 0000000..6fad6c4 --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FormGenerateLocomotive.java @@ -0,0 +1,111 @@ +package ProjectElectricLocomotive.src.Forms; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; + +public class FormGenerateLocomotive extends JFrame +{ + protected int Width; + private int Height; + + protected JPanel panelLocomotive; + private JPanel panelInfoLocomotive; + private JPanel panelButtonManage; + private JLabel labelInfoLocomotive; + private JPanel panelInfoWheels; + private JLabel labelInfoWheels; + private JButton createNewObject; + private JButton sendObject; + + protected DrawingLocomotiveCollection _collectionForm; + DrawingGenerateLocomotive field = new DrawingGenerateLocomotive(this); + + + public FormGenerateLocomotive(DrawingLocomotiveCollection _collection){ + super("Коллекция локомотивов"); + _collectionForm = _collection; + setSize(900,500); + Width = getWidth(); + Height = getHeight(); + initComponents(); + RefreshWindow(); + setVisible(true); + field.createNewObject(); + } + private void initComponents() + { + panelLocomotive = new JPanel(); + panelInfoLocomotive = new JPanel(); + panelInfoWheels = new JPanel(); + labelInfoLocomotive = new JLabel(); + labelInfoWheels = new JLabel(); + panelButtonManage = new JPanel(); + createNewObject = new JButton("Сгенерировать"); + sendObject = new JButton("Отправить"); + + panelLocomotive.setBorder(BorderFactory.createTitledBorder("Объект")); + panelInfoLocomotive.setBorder(BorderFactory.createTitledBorder("Информация о кораблях")); + panelInfoWheels.setBorder(BorderFactory.createTitledBorder("Информация о палубах")); + panelButtonManage.setLayout(new GridLayout(1, 2)); + + labelInfoLocomotive.setVerticalAlignment(SwingConstants.TOP); + labelInfoLocomotive.setPreferredSize(new Dimension(Width/3, Height - 40)); + + labelInfoWheels.setVerticalAlignment(SwingConstants.TOP); + + + createNewObject.setPreferredSize(new Dimension(130, 30)); + sendObject.setPreferredSize(new Dimension(130, 30)); + + StringBuilder sbE = new StringBuilder(); + StringBuilder sbD = new StringBuilder(); + for(int i = 0; i < 3; i++){ + sbE.append(field._generateLocomotive.getEn(i)).append("
").append("
"); + sbD.append(field._generateLocomotive.getDec(i)).append("
").append("
"); + } + labelInfoLocomotive.setText("" + sbE.toString() + ""); + labelInfoWheels.setText("" + sbD.toString() + ""); + + createNewObject.addActionListener(e->{ + field.createNewObject(); + repaint(); + }); + + sendObject.addActionListener(e->{ + field.insertCollection(); + //repaint(); + }); + + panelLocomotive.add(panelButtonManage); + panelInfoLocomotive.add(labelInfoLocomotive); + panelInfoWheels.add(labelInfoWheels); + panelButtonManage.add(createNewObject); + panelButtonManage.add(sendObject); + add(panelLocomotive); + add(panelInfoLocomotive); + add(panelInfoWheels); + + add(field); + + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + Width = getWidth(); + Height = getHeight(); + repaint(); + RefreshWindow(); + } + }); + } + public void RefreshWindow(){ + field.setLayout(new FlowLayout()); + panelLocomotive.setBounds(0, 0, Width/3, Height/3 - 40); + panelInfoLocomotive.setBounds(Width/3, 0, Width/3, Height - 40); + panelInfoWheels.setBounds(2 * Width/3, 0, Width/3, Height - 40); + panelButtonManage.setBounds(10,10,panelLocomotive.getWidth() - 10, panelLocomotive.getHeight() / 6); + } +} diff --git a/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java new file mode 100644 index 0000000..78341d9 --- /dev/null +++ b/ProjectElectricLocomotive/src/Forms/FormLocomotiveCollection.java @@ -0,0 +1,129 @@ +package ProjectElectricLocomotive.src.Forms; + + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import javax.swing.text.MaskFormatter; +import java.text.ParseException; +public class FormLocomotiveCollection extends JFrame { + protected int Width; + private int Height; + private JPanel groupBoxTools; + private JButton buttonAddElectricLocomotive; + private JButton buttonAddLocomotive; + private JComboBox comboBoxSelectorCompany; + private JButton buttonRefresh; + private JButton buttonGoToCheck; + private JButton buttonRemoveLocomotive; + private JButton buttonCreateSpecialLocomotive; + private JFormattedTextField maskedTextBoxPosition; + protected JLabel pictureBox; + + DrawingLocomotiveCollection field = new DrawingLocomotiveCollection(this); + + public FormLocomotiveCollection(){ + super("Коллекция локомотивов"); + setSize(900,500); + Width = getWidth(); + Height = getHeight(); + initComponents(); + RefreshWindow(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + private void initComponents() { + groupBoxTools = new JPanel(); + buttonAddElectricLocomotive = new JButton("Добавление электровоза"); + buttonAddLocomotive = new JButton("Добавление локомотива"); + comboBoxSelectorCompany = new JComboBox<>(); + buttonRefresh = new JButton("Обновить"); + buttonGoToCheck = new JButton("Передать на тесты"); + buttonRemoveLocomotive = new JButton("Удалить локомотив"); + buttonCreateSpecialLocomotive = new JButton("Сгенерировать локомотив"); + maskedTextBoxPosition = createMaskedTextField(); + pictureBox = new JLabel(); + + comboBoxSelectorCompany.addActionListener(e -> { + field.comboBoxSelectorCompany_SelectedIndexChanged((String) comboBoxSelectorCompany.getSelectedItem()); + }); + comboBoxSelectorCompany.addItem(""); + comboBoxSelectorCompany.addItem("Хранилище"); + + buttonAddLocomotive.addActionListener(e->{ + field.CreateButtonLocomotiveAction(); + repaint(); + }); + + buttonAddElectricLocomotive.addActionListener(e->{ + field.CreateButtonElectricLocomotiveAction(); + repaint(); + }); + buttonRemoveLocomotive.addActionListener(e->{ + field.ButtonRemoveLocomotive_Click(maskedTextBoxPosition); + repaint(); + }); + buttonGoToCheck.addActionListener(e->{ + field.ButtonGoToCheck_Click(); + repaint(); + }); + buttonRefresh.addActionListener(e->{ + field.ButtonRefresh_Click(); + repaint(); + }); + buttonCreateSpecialLocomotive.addActionListener(e->{ + field.ButtonCreateSpecialLocomotive_Click(); + repaint(); + }); + + + groupBoxTools.setBorder(BorderFactory.createTitledBorder("Инструменты")); + groupBoxTools.setLayout(new GridLayout(8, 1)); + groupBoxTools.add(comboBoxSelectorCompany); + groupBoxTools.add(buttonAddLocomotive); + groupBoxTools.add(buttonAddElectricLocomotive); + groupBoxTools.add(maskedTextBoxPosition); + groupBoxTools.add(buttonRemoveLocomotive); + groupBoxTools.add(buttonGoToCheck); + groupBoxTools.add(buttonCreateSpecialLocomotive); + + groupBoxTools.add(buttonRefresh); + + add(groupBoxTools); + add(pictureBox); + add(field); + + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + Width = getWidth(); + Height = getHeight(); + repaint(); + RefreshWindow(); + + } + }); + + + } + + private JFormattedTextField createMaskedTextField() { + try { + MaskFormatter formatter = new MaskFormatter("##"); + formatter.setPlaceholderCharacter('_'); + JFormattedTextField maskedTextField = new JFormattedTextField(formatter); + maskedTextField.setFocusLostBehavior(JFormattedTextField.PERSIST); + return maskedTextField; + } catch (ParseException e) { + e.printStackTrace(); + return new JFormattedTextField(); + } + } + public void RefreshWindow(){ + field.setLayout(new FlowLayout()); + groupBoxTools.setBounds(Width - 210, 0, 190, Height - 40); + pictureBox.setBounds(0,0,Width - 210, Height - 40); + } +} -- 2.25.1 From 03efd44060e76ce9f1c6a9f7c9d3fa3295e38b43 Mon Sep 17 00:00:00 2001 From: BoiledMilk123 Date: Wed, 5 Jun 2024 09:00:52 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B7=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B8?= =?UTF-8?q?=D0=BB=20labwork03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LocomotiveDepotService.java | 15 +++++++-------- .../SpecialGenerateLocomotive.java | 4 ++-- .../src/Forms/FormGenerateLocomotive.java | 4 ++-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/LocomotiveDepotService.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/LocomotiveDepotService.java index 4eb6a8b..9bec88c 100644 --- a/ProjectElectricLocomotive/src/CollectionGenericObjects/LocomotiveDepotService.java +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/LocomotiveDepotService.java @@ -41,17 +41,16 @@ public class LocomotiveDepotService extends AbstractCompany @Override protected void SetObjectsPosition() { if (locCoord == null || _collection == null) return; - int row = 1, col = 1; - for (int i = 0; i < _collection.getCount(); i++, col++) - { - if(_collection.get(i) == null) continue; + int row = countInRow, col = 1; + for (int i = 0; i < _collection.getCount(); i++, col++) { + if (_collection.get(i) == null) continue; _collection.get(i).SetPictureSize(_pictureWidth, _pictureHeight); - _collection.get(i).SetPosition(locCoord.get(row * countInRow - col).getKey() + 5, locCoord.get(row * countInRow - col).getValue() + 5); - if (col == countInRow) - { + _collection.get(i).SetPosition(locCoord.get((row - 1) * countInRow + (col - 1)).getKey() + 5, locCoord.get((row - 1) * countInRow + (col - 1)).getValue() + 5); + if (col == countInRow) { col = 0; - row++; + row--; } } } + } diff --git a/ProjectElectricLocomotive/src/CollectionGenericObjects/SpecialGenerateLocomotive.java b/ProjectElectricLocomotive/src/CollectionGenericObjects/SpecialGenerateLocomotive.java index 4256d97..8c51498 100644 --- a/ProjectElectricLocomotive/src/CollectionGenericObjects/SpecialGenerateLocomotive.java +++ b/ProjectElectricLocomotive/src/CollectionGenericObjects/SpecialGenerateLocomotive.java @@ -24,7 +24,7 @@ public class SpecialGenerateLocomotive