From bc4761841a038570a3a1f0e538a829a1dd4c0470 Mon Sep 17 00:00:00 2001 From: IlyasValiulov <148232695+IlyasValiulov@users.noreply.github.com> Date: Thu, 4 Apr 2024 11:35:29 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=B0=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=963=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AdditionalCollections.java | 61 +++++ .../AbstractCompany.java | 34 +++ .../ICollectionGenericObjects.java | 12 + .../MassiveGenericObjects.java | 75 ++++++ .../ShipPortService.java | 50 ++++ .../DrawingShip/CanvasFormShipCollection.java | 31 +++ .../src/DrawingShip/CanvasWarmlyShip.java | 1 + WarmlyShip/src/DrawingShip/DrawingShip.java | 12 +- .../src/DrawingShip/DrawingWarmlyShip.java | 11 +- WarmlyShip/src/Entities/EntityShip.java | 2 + WarmlyShip/src/Entities/EntityWarmlyShip.java | 2 + WarmlyShip/src/FormAdditionalCollection.java | 137 +++++++++++ WarmlyShip/src/FormShipCollection.java | 220 ++++++++++++++++++ WarmlyShip/src/FormWarmlyShip.java | 59 +---- WarmlyShip/src/Main.java | 3 +- .../src/MovementStrategy/MoveableShip.java | 3 +- .../MovementStrategy/MovementDirection.java | 1 + 17 files changed, 654 insertions(+), 60 deletions(-) create mode 100644 WarmlyShip/src/CollectionAdditionalObjects/AdditionalCollections.java create mode 100644 WarmlyShip/src/CollectionGenericObjects/AbstractCompany.java create mode 100644 WarmlyShip/src/CollectionGenericObjects/ICollectionGenericObjects.java create mode 100644 WarmlyShip/src/CollectionGenericObjects/MassiveGenericObjects.java create mode 100644 WarmlyShip/src/CollectionGenericObjects/ShipPortService.java create mode 100644 WarmlyShip/src/DrawingShip/CanvasFormShipCollection.java create mode 100644 WarmlyShip/src/FormAdditionalCollection.java create mode 100644 WarmlyShip/src/FormShipCollection.java diff --git a/WarmlyShip/src/CollectionAdditionalObjects/AdditionalCollections.java b/WarmlyShip/src/CollectionAdditionalObjects/AdditionalCollections.java new file mode 100644 index 0000000..ef5318a --- /dev/null +++ b/WarmlyShip/src/CollectionAdditionalObjects/AdditionalCollections.java @@ -0,0 +1,61 @@ +package CollectionAdditionalObjects; + +import DiffetentsDrawingDecks.IDifferentDecks; +import DrawingShip.DrawingShip; +import DrawingShip.DrawingWarmlyShip; +import Entities.EntityShip; +import Entities.EntityWarmlyShip; + +import java.lang.reflect.Array; +import java.util.Random; + +public class AdditionalCollections { + public T[] _collectionEntity; + public U[] _collectionDecks; + public AdditionalCollections(int size, Class type1, Class type2) { + _collectionEntity = (T[]) Array.newInstance(type1, size); + _collectionDecks = (U[]) Array.newInstance(type2, size); + CountEntities = size; + CountDecks = size; + } + public int CountEntities; + public int CountDecks; + public int Insert(T entity) { + int index = 0; + while (index < CountEntities) { + if (_collectionEntity[index] == null) + { + _collectionEntity[index] = entity; + return index; + } + ++index; + } + return -1; + } + public int Insert(U decks) { + int index = 0; + while (index < CountDecks) { + if (_collectionDecks[index] == null) + { + _collectionDecks[index] = decks; + return index; + } + ++index; + } + return -1; + } + public DrawingShip CreateAdditionalCollectionShip() { + Random random = new Random(); + if (_collectionEntity == null || _collectionDecks == null) return null; + T entity = _collectionEntity[random.nextInt(CountEntities)]; + U decks = _collectionDecks[random.nextInt(CountDecks)]; + DrawingShip drawingShip = null; + if (entity instanceof EntityWarmlyShip) { + drawingShip = new DrawingWarmlyShip((EntityWarmlyShip) entity, decks); + } + else { + drawingShip = new DrawingShip(entity, decks); + } + return drawingShip; + } +} diff --git a/WarmlyShip/src/CollectionGenericObjects/AbstractCompany.java b/WarmlyShip/src/CollectionGenericObjects/AbstractCompany.java new file mode 100644 index 0000000..676888e --- /dev/null +++ b/WarmlyShip/src/CollectionGenericObjects/AbstractCompany.java @@ -0,0 +1,34 @@ +package CollectionGenericObjects; + +import DrawingShip.DrawingShip; + +import java.awt.*; + +public abstract class AbstractCompany { + protected int _placeSizeWidth = 210; + protected int _placeSizeHeight = 150; + protected int _pictureWidth; + protected int _pictureHeight; + public 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(), (Class) DrawingShip.class); + } + //перегрузка операторов в джаве невозможна + public DrawingShip GetRandomObject() + { + return _collection.Get((int)(Math.random()*GetMaxCount() + 0)); + } + public void SetPosition() + { + SetObjectsPosition(); + } + public abstract void DrawBackgound(Graphics graphics); + protected abstract void SetObjectsPosition(); +} diff --git a/WarmlyShip/src/CollectionGenericObjects/ICollectionGenericObjects.java b/WarmlyShip/src/CollectionGenericObjects/ICollectionGenericObjects.java new file mode 100644 index 0000000..a633cfe --- /dev/null +++ b/WarmlyShip/src/CollectionGenericObjects/ICollectionGenericObjects.java @@ -0,0 +1,12 @@ +package CollectionGenericObjects; + +public interface ICollectionGenericObjects +{ + int getCount(); + void SetMaxCount(int count, Class type); + int Insert(T obj); + int Insert(T obj, int position); + T Remove(int position); + T Get(int position); +} + diff --git a/WarmlyShip/src/CollectionGenericObjects/MassiveGenericObjects.java b/WarmlyShip/src/CollectionGenericObjects/MassiveGenericObjects.java new file mode 100644 index 0000000..063fb44 --- /dev/null +++ b/WarmlyShip/src/CollectionGenericObjects/MassiveGenericObjects.java @@ -0,0 +1,75 @@ +package CollectionGenericObjects; + +import java.lang.reflect.Array; + +public class MassiveGenericObjects implements ICollectionGenericObjects{ + private T[] _collection; + private int Count; + public void SetMaxCount(int size, Class type) { + if (size > 0) { + _collection = (T[]) Array.newInstance(type, size); + Count = size; + } + } + @Override + public int getCount() { + return Count; + } + @Override + public int Insert(T obj) { + int index = 0; + while (index < getCount()) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + ++index; + } + return -1; + } + @Override + public int Insert(T obj, int position) { + if (position >= getCount() || position < 0) + return -1; + if (_collection[position] == null) { + _collection[position] = obj; + return position; + } + int index = position + 1; + while (index < getCount()) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + ++index; + } + index = position - 1; + while (index >= 0) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + --index; + } + return -1; + } + @Override + public T Remove(int position) { + if (position >= getCount() || position < 0) + return null; + T obj = (T) _collection[position]; + _collection[position] = null; + return obj; + } + @Override + public T Get(int position) { + if (position >= getCount() || position < 0) return null; + return (T) _collection[position]; + } +} diff --git a/WarmlyShip/src/CollectionGenericObjects/ShipPortService.java b/WarmlyShip/src/CollectionGenericObjects/ShipPortService.java new file mode 100644 index 0000000..693c0df --- /dev/null +++ b/WarmlyShip/src/CollectionGenericObjects/ShipPortService.java @@ -0,0 +1,50 @@ +package CollectionGenericObjects; + +import DrawingShip.DrawingShip; + +import java.awt.*; + +public class ShipPortService extends AbstractCompany{ + public ShipPortService(int picWidth, int picHeight, ICollectionGenericObjects collection) { + super(picWidth, picHeight, collection); + } + @Override + public void DrawBackgound(Graphics g) { + //рисуем пристань + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + g.setColor(Color.BLACK); + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height + 1; ++j) + { + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, + i * _placeSizeWidth + _placeSizeWidth - 5, j * _placeSizeHeight ); + } + } + } + @Override + protected void SetObjectsPosition() { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = width - 1; + int curHeight = 0; + for (int i = 0; i < (_collection.getCount()); i++) { + if (_collection.Get(i) != null) { + _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(i).SetPosition(_placeSizeWidth * curWidth + 20, curHeight * _placeSizeHeight + 4); + } + if (curWidth > 0) + curWidth--; + else { + curWidth = width - 1; + curHeight++; + } + if (curHeight > height) { + return; + } + } + } +} + diff --git a/WarmlyShip/src/DrawingShip/CanvasFormShipCollection.java b/WarmlyShip/src/DrawingShip/CanvasFormShipCollection.java new file mode 100644 index 0000000..01f9908 --- /dev/null +++ b/WarmlyShip/src/DrawingShip/CanvasFormShipCollection.java @@ -0,0 +1,31 @@ +package DrawingShip; + +import CollectionGenericObjects.AbstractCompany; + +import javax.swing.*; +import java.awt.*; + +public class CanvasFormShipCollection extends JComponent +{ + public AbstractCompany company = null; + public void SetCollectionToCanvas(AbstractCompany company) { + this.company = company; + } + public CanvasFormShipCollection(){}; + public void paintComponent(Graphics g) { + super.paintComponents(g); + if (company == null || company._collection == null) { + return; + } + company.DrawBackgound(g); + for (int i = 0; i < company._collection.getCount(); i++) { + + Graphics2D g2d = (Graphics2D) g; + T obj = (T)company._collection.Get(i); + if (obj instanceof DrawingShip) { + ((DrawingShip) obj).DrawTransport(g2d); + } + } + super.repaint(); + } +} diff --git a/WarmlyShip/src/DrawingShip/CanvasWarmlyShip.java b/WarmlyShip/src/DrawingShip/CanvasWarmlyShip.java index 11725eb..5cc72b3 100644 --- a/WarmlyShip/src/DrawingShip/CanvasWarmlyShip.java +++ b/WarmlyShip/src/DrawingShip/CanvasWarmlyShip.java @@ -16,3 +16,4 @@ public class CanvasWarmlyShip extends JComponent { super.repaint(); } } + diff --git a/WarmlyShip/src/DrawingShip/DrawingShip.java b/WarmlyShip/src/DrawingShip/DrawingShip.java index b52faa1..a81a88c 100644 --- a/WarmlyShip/src/DrawingShip/DrawingShip.java +++ b/WarmlyShip/src/DrawingShip/DrawingShip.java @@ -51,6 +51,16 @@ public class DrawingShip extends JPanel { EntityShip = new EntityShip(speed, weight, bodycolor); SetAmountandTypeDecks(); } + protected DrawingShip(int drawingShipWidth, int drawingShipHeight) { + super(); + this.drawingShipWidth = drawingShipWidth; + this.drawingShipHeight = drawingShipHeight; + } + //добавил + public DrawingShip(EntityShip entity, IDifferentDecks decks) { + EntityShip = entity; + drawingDecks = decks; + } public boolean SetPictureSize(int width, int height) { if (width < drawingShipWidth || height < drawingShipHeight) return false; picture_width = width; @@ -118,7 +128,7 @@ public class DrawingShip extends JPanel { if (EntityShip == null || _StartPosX == null || _StartPosY == null) return; int y = _StartPosY; g.setColor(EntityShip.getBodyColor()); - if (drawingDecks.getNumberOfDecks() != null) { + if (drawingDecks != null && drawingDecks.getNumberOfDecks() != null) { switch (drawingDecks.getNumberOfDecks()) { case OneDeck: drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor()); diff --git a/WarmlyShip/src/DrawingShip/DrawingWarmlyShip.java b/WarmlyShip/src/DrawingShip/DrawingWarmlyShip.java index a3d7ec7..e19ebca 100644 --- a/WarmlyShip/src/DrawingShip/DrawingWarmlyShip.java +++ b/WarmlyShip/src/DrawingShip/DrawingWarmlyShip.java @@ -1,4 +1,6 @@ package DrawingShip; +import DiffetentsDrawingDecks.IDifferentDecks; +import Entities.EntityShip; import Entities.EntityWarmlyShip; import java.awt.*; @@ -8,6 +10,11 @@ public class DrawingWarmlyShip extends DrawingShip { EntityShip = new EntityWarmlyShip(speed, weight, bodycolor, additionalcolor, sheeppipes, fueltank); SetAmountandTypeDecks(); } + //добавил + public DrawingWarmlyShip(EntityWarmlyShip entity, IDifferentDecks decks) { + EntityShip = entity; + drawingDecks = decks; + } @Override public void DrawTransport(Graphics2D g) { if (EntityShip == null || !(EntityShip instanceof EntityWarmlyShip warmlyship) || _StartPosX == null || _StartPosY == null) @@ -22,7 +29,7 @@ public class DrawingWarmlyShip extends DrawingShip { } super.DrawTransport(g); int count_decks = 0; - if (drawingDecks.getNumberOfDecks() != null) { + if (drawingDecks != null && drawingDecks.getNumberOfDecks() != null) { switch (drawingDecks.getNumberOfDecks()) { case OneDeck: count_decks = 1; @@ -35,7 +42,7 @@ public class DrawingWarmlyShip extends DrawingShip { break; } } - if (warmlyship.FuelTank) { + if (warmlyship.getFuelTank()) { g.setColor(warmlyship.getAdditionalColor()); g.fillRect(_StartPosX + 40, _StartPosY + count_decks * 15 + 30, 70, 10); } diff --git a/WarmlyShip/src/Entities/EntityShip.java b/WarmlyShip/src/Entities/EntityShip.java index ba40143..abd45ce 100644 --- a/WarmlyShip/src/Entities/EntityShip.java +++ b/WarmlyShip/src/Entities/EntityShip.java @@ -4,7 +4,9 @@ import java.awt.*; public class EntityShip { private int Speed; + public int getSpeed() {return Speed;} private double Weight; + public double getWeight() {return Weight;} private Color BodyColor; public Color getBodyColor() {return BodyColor;} public double Step; diff --git a/WarmlyShip/src/Entities/EntityWarmlyShip.java b/WarmlyShip/src/Entities/EntityWarmlyShip.java index 8863dcc..80313ad 100644 --- a/WarmlyShip/src/Entities/EntityWarmlyShip.java +++ b/WarmlyShip/src/Entities/EntityWarmlyShip.java @@ -5,7 +5,9 @@ public class EntityWarmlyShip extends EntityShip{ public Color AdditionalColor; public Color getAdditionalColor() {return AdditionalColor;} public boolean ShipPipes; + public boolean getShipPipes() {return ShipPipes;} public boolean FuelTank; + public boolean getFuelTank() {return FuelTank;} public EntityWarmlyShip(int speed, double weight, Color bodyColor, Color additionalcolor, boolean sheeppipes, boolean fueltank) { super(speed, weight, bodyColor); diff --git a/WarmlyShip/src/FormAdditionalCollection.java b/WarmlyShip/src/FormAdditionalCollection.java new file mode 100644 index 0000000..849c2dc --- /dev/null +++ b/WarmlyShip/src/FormAdditionalCollection.java @@ -0,0 +1,137 @@ +import CollectionAdditionalObjects.AdditionalCollections; +import CollectionGenericObjects.AbstractCompany; +import DiffetentsDrawingDecks.DrawingDecksType1; +import DiffetentsDrawingDecks.DrawingDecksType2; +import DiffetentsDrawingDecks.DrawingDecksType3; +import DiffetentsDrawingDecks.IDifferentDecks; +import DrawingShip.DrawingShip; +import DrawingShip.DrawingWarmlyShip; +import DrawingShip.CanvasWarmlyShip; +import Entities.EntityShip; +import Entities.EntityWarmlyShip; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormAdditionalCollection extends JFrame { + public DrawingShip drawingShip = null; + private AbstractCompany company = null; + private CanvasWarmlyShip canvasship = new CanvasWarmlyShip(); + private AdditionalCollections additionalCollection = null; + private Random random = new Random(); + private JButton buttonGenerate = new JButton("Создать"); + private JList listEntity = new JList(); + private JList listDecks = new JList(); + public FormAdditionalCollection() { + setTitle("Random ship"); + setMinimumSize(new Dimension(650,310)); + additionalCollection = new AdditionalCollections(3, + (Class) EntityShip.class, (Class) IDifferentDecks.class); + AddEntities(); + AddDecks(); + buttonGenerate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + drawingShip = additionalCollection.CreateAdditionalCollectionShip(); + drawingShip.SetPictureSize(getWidth(), getHeight()); + drawingShip.SetPosition(50,50); + canvasship._drawingShip = drawingShip; + canvasship.repaint(); + DrawingShip copyShip; + if (drawingShip instanceof DrawingWarmlyShip) + copyShip = new DrawingWarmlyShip((EntityWarmlyShip) drawingShip.EntityShip, drawingShip.drawingDecks); + else + copyShip = new DrawingShip(drawingShip.EntityShip, drawingShip.drawingDecks); + company._collection.Insert(copyShip); + FormShipCollection.canvasShow(); + + String[] data1 = new String[additionalCollection.CountEntities]; + for (int i = 0; i < additionalCollection.CountEntities; i++) { + EntityShip entity = additionalCollection._collectionEntity[i]; + data1[i] = ToString(entity); + } + String[] data2 = new String[additionalCollection.CountDecks]; + for (int i = 0; i < additionalCollection.CountDecks; i++) { + IDifferentDecks decks = additionalCollection._collectionDecks[i]; + data2[i] = ToString(decks); + } + listEntity.setListData(data1); + listDecks.setListData(data2); + } + }); + buttonGenerate.setBounds(450, 10, 100, 50); + add(buttonGenerate); + listEntity.setBounds(10,200,300,60); + listDecks.setBounds(320,200,300,60); + add(listEntity); + add(listDecks); + add(canvasship); + setVisible(true); + } + private String ToString(EntityShip entity) { + String str = ""; + if (entity instanceof EntityWarmlyShip) str += "EntityWarmlyShip "; + else str += "EntityShip "; + str += entity.getBodyColor().toString(); + return str; + } + private String ToString(IDifferentDecks decks) { + if (decks == null || decks.getNumberOfDecks() == null) + return "Dont have decks"; + String str = "Deck "; + if (decks instanceof DrawingDecksType1) str += "Type 1 "; + else if (decks instanceof DrawingDecksType2) str += "Type 2 "; + else str += "Type 3 "; + str += decks.getNumberOfDecks().toString(); + return str; + } + public void AddEntities() { + for (int i = 0; i < additionalCollection.CountEntities; i++) { + random = new Random(); + int speed = random.nextInt(100, 300); + double weight = random.nextInt(1000, 3000); + Color bodycolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); + EntityShip entity; + if (random.nextBoolean()) { + entity = new EntityShip(speed, weight, bodycolor); + } + else { + Color additionalcolor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); + boolean sheeppipes = random.nextBoolean(); + boolean fueltaln = random.nextBoolean(); + entity = new EntityWarmlyShip(speed, weight, bodycolor, + additionalcolor, sheeppipes, fueltaln); + } + additionalCollection.Insert(entity); + } + } + public void AddDecks() { + for (int i = 0; i < additionalCollection.CountDecks; i++) { + random = new Random(); + Integer numberOfDecks = random.nextInt(0, 4); + IDifferentDecks drawingDecks = null; + switch (random.nextInt(0,4)) { + case 1: + drawingDecks = new DrawingDecksType1(); + break; + case 2: + drawingDecks = new DrawingDecksType2(); + break; + case 3: + drawingDecks = new DrawingDecksType3(); + break; + default: + numberOfDecks = null; + break; + } + if (drawingDecks != null) drawingDecks.setNumberOfDecks(numberOfDecks); + additionalCollection.Insert(drawingDecks); + } + } + void setCompany(AbstractCompany company) { + this.company = company; + } +} diff --git a/WarmlyShip/src/FormShipCollection.java b/WarmlyShip/src/FormShipCollection.java new file mode 100644 index 0000000..3b09d7b --- /dev/null +++ b/WarmlyShip/src/FormShipCollection.java @@ -0,0 +1,220 @@ +import CollectionGenericObjects.AbstractCompany; +import CollectionGenericObjects.MassiveGenericObjects; +import CollectionGenericObjects.ShipPortService; +import DrawingShip.CanvasFormShipCollection; +import DrawingShip.DrawingShip; +import DrawingShip.DrawingWarmlyShip; + +import javax.swing.*; +import javax.swing.text.MaskFormatter; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.text.ParseException; +import java.util.Random; + +import static java.lang.Integer.parseInt; + +public class FormShipCollection extends JFrame{ + private String title; + private Dimension dimension; + public static CanvasFormShipCollection _canvasWarmlyShip = new CanvasFormShipCollection(); + private static AbstractCompany _company = null; + private JButton CreateButton = new JButton("Create warmlyship");; + private JButton CreateShipButton = new JButton("Create ship"); + private JButton RemoveButton = new JButton("Remove"); + private JButton GoToCheckButton = new JButton("Check"); + private JButton RandomButton = new JButton("RandomShip"); + private JButton RefreshButton = new JButton("Refresh"); + private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"}); + private JFormattedTextField MaskedTextField; + public FormShipCollection(String title, Dimension dimension) { + this.title = title; + this.dimension = dimension; + } + public static void canvasShow() { + _company.SetPosition(); + _canvasWarmlyShip.SetCollectionToCanvas(_company); + _canvasWarmlyShip.repaint(); + } + private void CreateObject(String typeOfClass) { + if (_company == null) return; + int speed = (int)(Math.random() * 300 + 100); + double weight = (double)(Math.random() * 3000 + 1000); + Color bodyColor = getColor(); + DrawingShip drawingShip; + switch (typeOfClass) { + case "DrawingShip": + drawingShip = new DrawingShip(speed, weight, bodyColor); + break; + case "DrawingWarmlyShip": + Color additionalColor = getColor(); + boolean sheepPipes = new Random().nextBoolean(); + boolean fuelTank = new Random().nextBoolean();; + drawingShip = new DrawingWarmlyShip(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank); + break; + default: return; + } + if (_company._collection.Insert(drawingShip, 0) != -1) { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + canvasShow(); + } + else { + JOptionPane.showMessageDialog(null, "Объект не удалось добавить"); + } + } + public Color getColor() { + Color initializator = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); + Color color = JColorChooser.showDialog(this, "Select a color", initializator); + return color; + } + public void Init() { + setTitle(title); + setMinimumSize(dimension); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + MaskFormatter mask = null; + try { + mask = new MaskFormatter("##"); + mask.setPlaceholder("00"); + } catch (ParseException e) { + throw new RuntimeException(e); + } + + MaskedTextField = new JFormattedTextField(mask); + + ComboBoxCollections.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + switch (ComboBoxCollections.getSelectedItem().toString()) { + case "Хранилище": + _company = new ShipPortService(getWidth()-200, getHeight()-70, new MassiveGenericObjects()); + break; + } + } + }); + + CreateShipButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawingShip"); + } + }); + CreateButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawingWarmlyShip"); + } + }); + + RemoveButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null || MaskedTextField.getText() == null) { + return; + } + int pos = parseInt(MaskedTextField.getText()); + int resultConfirmDialog = JOptionPane.showConfirmDialog(null, + "Удалить", "Удаление", + JOptionPane.YES_NO_OPTION); + if (resultConfirmDialog == JOptionPane.NO_OPTION) return; + if (_company._collection.Remove(pos) != null) { + JOptionPane.showMessageDialog(null, "Объект удален"); + canvasShow(); + } + else { + JOptionPane.showMessageDialog(null, "Объект не удалось удалить"); + } + } + }); + + GoToCheckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null) + { + return; + } + DrawingShip ship = null; + int counter = 100; + while (ship == null) + { + ship = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + if (ship == null) + { + return; + } + FormWarmlyShip form = new FormWarmlyShip("Теплоход", new Dimension(900,565)); + form.Init(ship); + } + }); + + RandomButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null) + { + return; + } + FormAdditionalCollection form = new FormAdditionalCollection(); + form.setCompany(_company); + } + }); + + RefreshButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_company == null) + { + return; + } + canvasShow(); + } + }); + + _canvasWarmlyShip.setBounds(0, 0, getWidth()-200, getHeight()); + ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); + CreateShipButton.setBounds(getWidth()-190, 60, 150, 30); + CreateButton.setBounds(getWidth()-190, 100, 150, 30); + MaskedTextField.setBounds(getWidth()-190,200,150,30); + RemoveButton.setBounds(getWidth()-190, 240, 150, 30); + GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30); + RandomButton.setBounds(getWidth()-190, 320, 150, 30); + RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); + + setSize(dimension.width,dimension.height); + setLayout(null); + add(_canvasWarmlyShip); + add(ComboBoxCollections); + add(CreateShipButton); + add(CreateButton); + add(MaskedTextField); + add(RemoveButton); + add(GoToCheckButton); + add(RandomButton); + add(RefreshButton); + setVisible(true); + + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + _canvasWarmlyShip.setBounds(0, 0, getWidth()-200, getHeight()-70); + ComboBoxCollections.setBounds(getWidth()-190, 10, 150, 20); + CreateShipButton.setBounds(getWidth()-190, 60, 150, 30); + CreateButton.setBounds(getWidth()-190, 100, 150, 30); + MaskedTextField.setBounds(getWidth()-190,200,150,30); + RemoveButton.setBounds(getWidth()-190, 240, 150, 30); + GoToCheckButton.setBounds(getWidth()-190, 280, 150, 30); + RandomButton.setBounds(getWidth()-190, 320, 150, 30); + RefreshButton.setBounds(getWidth()-190, getHeight()-90, 150, 30); + } + }); + } +} diff --git a/WarmlyShip/src/FormWarmlyShip.java b/WarmlyShip/src/FormWarmlyShip.java index b712300..14f7a6f 100644 --- a/WarmlyShip/src/FormWarmlyShip.java +++ b/WarmlyShip/src/FormWarmlyShip.java @@ -1,7 +1,7 @@ +import DrawingShip.CanvasFormShipCollection; import DrawingShip.CanvasWarmlyShip; import DrawingShip.DirectionType; import DrawingShip.DrawingShip; -import DrawingShip.DrawingWarmlyShip; import MovementStrategy.*; import javax.swing.*; @@ -10,15 +10,12 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; -import java.util.Random; public class FormWarmlyShip extends JFrame { private String title; private Dimension dimension; private int Width, Height; public CanvasWarmlyShip canvasWarmlyShip = new CanvasWarmlyShip(); - private JButton CreateButton = new JButton("Create warmlyship");; - private JButton CreateShipButton = new JButton("Create ship"); private JButton UpButton = new JButton(); private JButton DownButton = new JButton();; private JButton LeftButton = new JButton();; @@ -30,44 +27,17 @@ public class FormWarmlyShip extends JFrame { this.title = title; this.dimension = dimension; } - private void CreateObject(String typeOfClass) { - int StartPositionX = (int)(Math.random() * 90 + 10); - int StartPositionY = (int)(Math.random() * 90 + 10); - int speed = (int)(Math.random() * 300 + 100); - double weight = (double)(Math.random() * 3000 + 1000); - Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); - switch (typeOfClass) { - case "DrawingShip": - canvasWarmlyShip._drawingShip = new DrawingShip(speed, weight, bodyColor); - canvasWarmlyShip._drawingShip.SetPictureSize(Width, Height); - canvasWarmlyShip._drawingShip.SetPosition(StartPositionX, StartPositionY); - canvasWarmlyShip.repaint(); - break; - case "DrawingWarmlyShip": - Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; - boolean sheepPipes = new Random().nextBoolean(); - boolean fuelTank = new Random().nextBoolean();; - canvasWarmlyShip._drawingShip = new DrawingWarmlyShip(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank); - canvasWarmlyShip._drawingShip.SetPictureSize(Width, Height); - canvasWarmlyShip._drawingShip.SetPosition(StartPositionX, StartPositionY); - canvasWarmlyShip.repaint(); - break; - default: return; - } - _strategy = null; - ComboBoxStrategy.setEnabled(true); - } - public void Init() { + public void Init(DrawingShip ship) { setTitle(title); setMinimumSize(dimension); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Width = getWidth() - 10; Height = getHeight() - 34; + ComboBoxStrategy.setEnabled(true); _strategy = null; + canvasWarmlyShip._drawingShip = ship; - CreateButton.setName("CREATE"); - CreateShipButton.setName("CREATESHIPBUTTON"); Icon iconUp = new ImageIcon("src\\images\\up.jpg"); UpButton.setIcon(iconUp); UpButton.setName("UP"); @@ -81,20 +51,6 @@ public class FormWarmlyShip extends JFrame { Icon iconRight = new ImageIcon("src\\images\\right.jpg"); RightButton.setIcon(iconRight); - CreateButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - CreateObject("DrawingWarmlyShip"); - } - }); - - CreateShipButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - CreateObject("DrawingShip"); - } - }); - ButtonStrategy.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -141,7 +97,6 @@ public class FormWarmlyShip extends JFrame { boolean result = false; switch ((((JButton)(event.getSource())).getName())) { case "UP": - System.out.println("Кнопка UP нажата"); result = canvasWarmlyShip._drawingShip.MoveTransport(DirectionType.Up); break; case "DOWN": @@ -167,16 +122,12 @@ public class FormWarmlyShip extends JFrame { setSize(dimension.width,dimension.height); setLayout(null); canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 140, 40); - CreateShipButton.setBounds(160, getHeight() - 90, 140, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); - add(CreateButton); - add(CreateShipButton); add(UpButton); add(DownButton); add(RightButton); @@ -193,8 +144,6 @@ public class FormWarmlyShip extends JFrame { if (canvasWarmlyShip._drawingShip != null) canvasWarmlyShip._drawingShip.SetPictureSize(Width, Height); canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 140, 40); - CreateShipButton.setBounds(160, getHeight() - 90, 140, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); diff --git a/WarmlyShip/src/Main.java b/WarmlyShip/src/Main.java index a785b53..e50a0ea 100644 --- a/WarmlyShip/src/Main.java +++ b/WarmlyShip/src/Main.java @@ -1,7 +1,8 @@ import java.awt.*; + public class Main { public static void main(String[] args) { - FormWarmlyShip form = new FormWarmlyShip("Теплоход", new Dimension(700,500)); + FormShipCollection form = new FormShipCollection("Коллекция судов", new Dimension(1100, 648)); form.Init(); } } \ No newline at end of file diff --git a/WarmlyShip/src/MovementStrategy/MoveableShip.java b/WarmlyShip/src/MovementStrategy/MoveableShip.java index c6e4de3..09f7b85 100644 --- a/WarmlyShip/src/MovementStrategy/MoveableShip.java +++ b/WarmlyShip/src/MovementStrategy/MoveableShip.java @@ -1,8 +1,9 @@ package MovementStrategy; -import DrawingShip.CanvasWarmlyShip; +import DrawingShip.CanvasFormShipCollection; import DrawingShip.DirectionType; import DrawingShip.DrawingShip; +import DrawingShip.CanvasWarmlyShip; public class MoveableShip implements IMoveableObjects{ private CanvasWarmlyShip canvas = new CanvasWarmlyShip(); diff --git a/WarmlyShip/src/MovementStrategy/MovementDirection.java b/WarmlyShip/src/MovementStrategy/MovementDirection.java index c52f124..4a0d6b3 100644 --- a/WarmlyShip/src/MovementStrategy/MovementDirection.java +++ b/WarmlyShip/src/MovementStrategy/MovementDirection.java @@ -5,4 +5,5 @@ public enum MovementDirection { Down, Left, Right + }