diff --git a/src/Drawnings/DrawningAirbus.java b/src/Drawnings/DrawningAirbus.java index a8fc97e..6a2790f 100644 --- a/src/Drawnings/DrawningAirbus.java +++ b/src/Drawnings/DrawningAirbus.java @@ -16,6 +16,11 @@ public class DrawningAirbus { private int _airbusWidth = 124; private int _airbusHeight = 44; + // Получение объекта IMoveableObject из объекта DrawningCar; + public IMoveableObject GetMoveableObject() { + return new DrawningObjectAirbus(this); + } + public int GetPosX() { return _startPosX; } public int GetPosY() { return _startPosY; } public int GetWidth() { return _airbusWidth; } diff --git a/src/Drawnings/DrawningPortholesCircle.java b/src/Drawnings/DrawningPortholesCircle.java index 5de565e..6044515 100644 --- a/src/Drawnings/DrawningPortholesCircle.java +++ b/src/Drawnings/DrawningPortholesCircle.java @@ -5,13 +5,13 @@ import Entities.*; public class DrawningPortholesCircle implements IDrawningPortholes { private CountPortholes _porthole; - - public CountPortholes getCount() - { - return _porthole; + public int Count; + public int getCount() { + return Count; } public void SetCount (int count) { - switch (count) { + Count = count; + switch (Count) { case 10: _porthole = CountPortholes.Ten; break; diff --git a/src/Drawnings/IDrawningPortholes.java b/src/Drawnings/IDrawningPortholes.java index 6ac3162..1072010 100644 --- a/src/Drawnings/IDrawningPortholes.java +++ b/src/Drawnings/IDrawningPortholes.java @@ -4,7 +4,7 @@ import java.awt.*; import Entities.CountPortholes; public interface IDrawningPortholes { - public CountPortholes getCount(); + public int getCount(); public void SetCount (int count); public void Draw (Graphics2D g, int _startPosX, int _startPoxY); } \ No newline at end of file diff --git a/src/FormAirbus.java b/src/FormAirbus.java index 78cf068..2539d48 100644 --- a/src/FormAirbus.java +++ b/src/FormAirbus.java @@ -9,7 +9,7 @@ public class FormAirbus extends JFrame { private int width; private int height; - private DrawningAirbus _drawningAirbus; + public DrawningAirbus _drawningAirbus; private AbstractStrategy _abstractStrategy; private Canvas canvas; @@ -24,6 +24,7 @@ public class FormAirbus extends JFrame { private JButton buttonCreateAirbus; private JButton buttonCreatePlane; + public JButton buttonSelectAirbus; private JButton buttonUp; private JButton buttonDown; private JButton buttonRight; @@ -53,6 +54,9 @@ public class FormAirbus extends JFrame { buttonCreatePlane = new JButton("Создать самолёт"); buttonCreatePlane.setMargin(new Insets(0, 0, 0, 0)); + buttonSelectAirbus = new JButton("Выбрать"); + buttonSelectAirbus.setMargin(new Insets(0, 0, 0, 0)); + buttonUp = new JButton(); buttonUp.setName("up"); buttonUp.setIcon(new ImageIcon("images\\KeyUp.png")); @@ -73,8 +77,9 @@ public class FormAirbus extends JFrame { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); - buttonCreateAirbus.setBounds(12, 355, 146, 33); - buttonCreatePlane.setBounds(182, 355, 146, 33); + buttonCreateAirbus.setBounds(12, 355, 130, 25); + buttonCreatePlane.setBounds(buttonCreateAirbus.getX()+140, 355, 130, 25); + buttonSelectAirbus. setBounds(buttonCreatePlane.getX()+140, 355, 130, 25); labelCount.setBounds(42, 405, 240, 20); fieldCount.setBounds(240, 407, 48, 20); @@ -93,6 +98,7 @@ public class FormAirbus extends JFrame { add(buttonCreateAirbus); add(buttonCreatePlane); + add(buttonSelectAirbus); add(labelCount); add(fieldCount); add(labelStrategy); @@ -119,6 +125,15 @@ public class FormAirbus extends JFrame { ActionListener buttonCreateAirbusListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + Random rand = new Random(); + // базовый цвет + Color color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)); + Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", Color.BLACK); + if (selectedColor != null) + { + color = selectedColor; + } + int countPortholes; try { @@ -134,9 +149,9 @@ public class FormAirbus extends JFrame { countPortholes = 10; } - Random rand = new Random(); - _drawningAirbus = new DrawningAirbus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, - new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), + _drawningAirbus = new DrawningAirbus( + rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + color, countPortholes, canvas.getWidth(), canvas.getHeight()); @@ -149,6 +164,23 @@ public class FormAirbus extends JFrame { ActionListener buttonCreatePlaneListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + Random rand = new Random(); + // базовый цвет + Color color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)); + Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", Color.BLACK); + if (selectedColor != null) + { + color = selectedColor; + } + + // доп цвет + Color additionalColor = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)); + selectedColor = JColorChooser.showDialog(null, "Выберите доп. цвет", Color.GRAY); + if (selectedColor != null) + { + additionalColor = selectedColor; + } + int countPortholes; try { @@ -164,11 +196,11 @@ public class FormAirbus extends JFrame { countPortholes = 10; } - Random rand = new Random(); - _drawningAirbus = new DrawningPlane(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, - new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), + _drawningAirbus = new DrawningPlane( + rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + color, countPortholes, - new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), + additionalColor, rand.nextBoolean(), rand.nextBoolean(), canvas.getWidth(), canvas.getHeight()); diff --git a/src/FormAirbusCollection.java b/src/FormAirbusCollection.java new file mode 100644 index 0000000..da76684 --- /dev/null +++ b/src/FormAirbusCollection.java @@ -0,0 +1,156 @@ +import Drawnings.DrawningAirbus; +import Generics.AirbusGenericCollection; +import MovementStrategy.DrawningObjectAirbus; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.*; + +public class FormAirbusCollection extends JFrame { + + Canvas canvas; + + static int pictureBoxWidth = 650; + static int pictureBoxHeight = 460; + JButton buttonAddAirbus; + JButton buttonDeleteAirbus; + JButton buttonUpdate; + // открыть форму генерации + private JButton buttonGenerateAirbus; + + JTextField textFieldNumber = new JTextField(); + private AirbusGenericCollection _airbus; + + FormAirbusCollection() { + canvas = new Canvas(); + + JFrame frame = new JFrame("Коллекция аэробусов"); + _airbus = new AirbusGenericCollection (pictureBoxWidth, pictureBoxHeight); + canvas._airbus = _airbus; + + buttonAddAirbus = new JButton("Добавить аэробус"); + buttonAddAirbus.setMargin(new Insets(0, 0, 0, 0)); + textFieldNumber = new JTextField(); + buttonDeleteAirbus = new JButton("Удалить аэробус"); + buttonDeleteAirbus.setMargin(new Insets(0, 0, 0, 0)); + buttonUpdate = new JButton("Обновить"); + buttonUpdate.setMargin(new Insets(0, 0, 0, 0)); + buttonGenerateAirbus = new JButton("Форма генерации"); + buttonGenerateAirbus.setMargin(new Insets(0,0,0,0)); + + setSize(800,500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(null); + + buttonAddAirbus.setBounds(pictureBoxWidth, 20, 120, 25); + textFieldNumber.setBounds(pictureBoxWidth, buttonAddAirbus.getY()+30, 120, 25); + buttonDeleteAirbus.setBounds(pictureBoxWidth, textFieldNumber.getY()+30, 120, 25); + buttonUpdate.setBounds(pictureBoxWidth, buttonDeleteAirbus.getY()+30, 120, 25); + buttonGenerateAirbus.setBounds(pictureBoxWidth, buttonUpdate.getY()+30, 120, 25); + canvas.setBounds(0,0,pictureBoxWidth, pictureBoxHeight); + + add(canvas); + add(buttonAddAirbus); + add(buttonDeleteAirbus); + add(buttonUpdate); + add(buttonGenerateAirbus); + add(textFieldNumber); + setVisible(true); + + // логика формы + buttonAddAirbus.addActionListener(AddAirbusListener); + buttonDeleteAirbus.addActionListener(DeleteAirbusListener); + buttonUpdate.addActionListener(UpdateAirbusListener); + buttonGenerateAirbus.addActionListener(OpenGenerationForm); + } + + ActionListener OpenGenerationForm = new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) { + FormGenerationAirbus form = new FormGenerationAirbus(); + dispose(); + } + }; + + ActionListener AddAirbusListener = new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) { + FormAirbus form = new FormAirbus(); + // выбор аэробуса на второй форме + form.buttonSelectAirbus.addActionListener( + new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + DrawningAirbus selectedAirbus = form._drawningAirbus; + form.dispose(); + if (selectedAirbus != null) { + if (_airbus.Add(selectedAirbus) != -1) { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + _airbus.ShowAirbus(); + canvas.repaint(); + } else { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); + canvas.repaint(); + } + } + } + } + ); + } + }; + + ActionListener DeleteAirbusListener = new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) { + int pos; + try { + pos = Integer.parseInt(textFieldNumber.getText()); + } catch (Exception ex) { + JOptionPane.showMessageDialog(null, "Ошибка ввода", "Ошибка", JOptionPane.ERROR_MESSAGE); + return; + } + Object[] options= {"да", "нет"}; + if (JOptionPane.showOptionDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Да", "Нет"}, "Да") + == JOptionPane.NO_OPTION) + { + return; + } + if (_airbus.Remove(pos) != false) + { + _airbus.ShowAirbus(); + canvas.repaint(); + JOptionPane.showMessageDialog(null, "Объект удален"); + } else { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); + } + } + }; + + ActionListener UpdateAirbusListener = new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) { + _airbus.ShowAirbus(); + canvas.repaint(); + } + }; + + class Canvas extends JComponent { + public AirbusGenericCollection _airbus; + + public Canvas() { + } + + public void paintComponent(Graphics g) { + super.paintComponents(g); + if (_airbus.ShowAirbus() != null) { + g.drawImage(_airbus.ShowAirbus(), 0, 0, this); + } + super.repaint(); + } + } +} \ No newline at end of file diff --git a/src/FormGenerationAirbus.java b/src/FormGenerationAirbus.java new file mode 100644 index 0000000..03117cf --- /dev/null +++ b/src/FormGenerationAirbus.java @@ -0,0 +1,91 @@ +import Drawnings.*; +import Entities.EntityAirbus; +import Entities.EntityPlane; +import Generics.AirbusGenericCollection; +import Generics.GenericParametrized; +import MovementStrategy.DrawningObjectAirbus; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; +import javax.swing.*; + +public class FormGenerationAirbus extends JFrame { + Canvas canvas; + GenericParametrized generic; + DrawningAirbus _airbus; + private int _width = 800; + private int _height = 500; + + JButton buttonGenerate; + + FormGenerationAirbus() { + JFrame frame = new JFrame("Генерация аэробусов"); + setSize(800,500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(null); + + generic = new GenericParametrized<>(50, 50, _width, _height); + + canvas = new FormGenerationAirbus.Canvas(); + canvas.setBounds(0 , 0, _width, _height); + + buttonGenerate = new JButton("Сгенерировать аэробус"); + buttonGenerate.setMargin(new Insets(0, 0, 0, 0)); + buttonGenerate.setBounds(600, 20, 170, 25); + + add(canvas); + add(buttonGenerate); + setVisible(true); + + buttonGenerate.addActionListener(GenerateAirbus); + } + + ActionListener GenerateAirbus = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rand = new Random(); + // генерация сущности + EntityAirbus entity; + if (rand.nextBoolean()) { + entity = new EntityAirbus( + rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256))); + } else { + entity = new EntityPlane(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)), + new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)), + rand.nextBoolean(), rand.nextBoolean()); + } + generic.addAirbus(entity); + // генерация иллюминаторов + IDrawningPortholes potholes; + int n = rand.nextInt(3); + if (n == 0) + potholes = new DrawningPortholesCircle(); + else if (n == 1) + potholes = new DrawningPortholesHeart(); + else + potholes = new DrawningPortholesSquare(); + potholes.SetCount(rand.nextInt(1,4)*10); + generic.addPortholes(potholes); + + // отрисовка + _airbus = generic.randomDrawingObject(); + _airbus.SetPosition(100, 100); + canvas.repaint(); + } + }; + + class Canvas extends JComponent { + public Canvas() {} + public void paintComponent(Graphics g) { + super.paintComponents(g); + Graphics2D g2d = (Graphics2D)g; + if (_airbus != null) + _airbus.DrawTransport(g2d); + super.repaint(); + } + } +} diff --git a/src/Generics/AirbusGenericCollection.java b/src/Generics/AirbusGenericCollection.java new file mode 100644 index 0000000..285399f --- /dev/null +++ b/src/Generics/AirbusGenericCollection.java @@ -0,0 +1,104 @@ +package Generics; + +import java.awt.*; +import java.awt.image.BufferedImage; + +import Drawnings.DrawningAirbus; +import MovementStrategy.IMoveableObject; + +public class AirbusGenericCollection { + private int _pictureWidth; + private int _pictureHeight; + private int _placeSizeWidth = 124; + private int _placeSizeHeight = 44; + private SetGeneric _collection; + + public AirbusGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + + _collection = new SetGeneric(width * height); + } + // сложение + public int Add(T obj) + { + if (obj == null) + return -1; + return _collection.Insert(obj); + } + // вычитание + public boolean Remove(int pos) + { + if (_collection.Get(pos) == null) + return false; + return _collection.Remove(pos); + } + + // получение объекта IMoveableObjecr + public U GetU(int pos) + { + if (_collection.Get(pos) != null) + return (U)_collection.Get(pos).GetMoveableObject(); + return null; + } + + // вывод всего набора + public BufferedImage ShowAirbus() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D gr = bmp.createGraphics(); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + + // прорисовка фона + private void DrawBackground(Graphics gr) + { + gr.setColor(Color.BLACK); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + // линия разметки + gr.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + gr.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + } + + // прорисовка объекта + private void DrawObjects(Graphics2D gr) + { + // координаты + int x = _pictureWidth / _placeSizeWidth - 1; + int y = _pictureHeight / _placeSizeHeight - 1; + + for (int i = 0; i < _collection.Count; ++i) + { + DrawningAirbus _airbus = _collection.Get(i); + if (_airbus == null) + { + --x; + if (x < 0) + { + x = _pictureWidth / _placeSizeWidth - 1; + --y; + } + continue; + } + _airbus.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y); + _airbus.DrawTransport(gr); + --x; + // новая строка + if (x < 0) + { + x = _pictureWidth / _placeSizeWidth - 1; + --y; + } + } + } +} diff --git a/src/Generics/GenericParametrized.java b/src/Generics/GenericParametrized.java new file mode 100644 index 0000000..0767a63 --- /dev/null +++ b/src/Generics/GenericParametrized.java @@ -0,0 +1,76 @@ +package Generics; + +import java.util.ArrayList; +import java.util.Random; +import Entities.*; +import Drawnings.*; + +public class GenericParametrized { + + private ArrayList _airbus; + private ArrayList _portholes; + private int CountAirbus; + private int CountPortholes; + private int MaxCountAirbus; + private int MaxCountPortoles; + private int _pictureWidth; + private int _pictureHeight; + + public GenericParametrized(int maxCountAirbus, int maxCountPortoles, int pictureWidth, int pictureHeight) + { + MaxCountAirbus = maxCountAirbus; + CountAirbus = 0; + MaxCountPortoles = maxCountPortoles; + CountPortholes = 0; + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + _airbus = new ArrayList(); + _portholes = new ArrayList(); + } + + public boolean addAirbus(T airbus) + { + if (airbus == null || CountAirbus >= MaxCountAirbus) + return false; + _airbus.add(airbus); + CountAirbus++; + return true; + } + + public boolean addPortholes(U porthole) + { + if (porthole == null || CountPortholes >= MaxCountPortoles) + return false; + _portholes.add(porthole); + CountPortholes++; + return true; + } + + public DrawningAirbus randomDrawingObject() + { + Random rand = new Random(); + if (CountAirbus == 0 || CountPortholes == 0) + return null; + int ar = rand.nextInt(CountAirbus); + int port = rand.nextInt(CountPortholes); + DrawningAirbus drawningAirbus; + if (_airbus.get(ar) instanceof EntityPlane) + drawningAirbus = new DrawningPlane( + _airbus.get(ar).getSpeed(), + _airbus.get(ar).getWeight(), + _airbus.get(ar).getBodyColor(), + _portholes.get(port).getCount(), + ((EntityPlane)_airbus.get(ar)).getAdditionalColor(), + ((EntityPlane)_airbus.get(ar)).IsCompartment(), + ((EntityPlane)_airbus.get(ar)).IsAdditionalEngine(), + _pictureWidth, _pictureHeight); + else + drawningAirbus = new DrawningAirbus( + _airbus.get(ar).getSpeed(), + _airbus.get(ar).getWeight(), + _airbus.get(ar).getBodyColor(), + _portholes.get(port).getCount(), + _pictureWidth, _pictureHeight); + return drawningAirbus; + } +} diff --git a/src/Generics/SetGeneric.java b/src/Generics/SetGeneric.java new file mode 100644 index 0000000..05dd4d0 --- /dev/null +++ b/src/Generics/SetGeneric.java @@ -0,0 +1,67 @@ +package Generics; + +public class SetGeneric { + + // список объектов + private Object[] _places; + // кол-во объектов + public int Count; + + public SetGeneric(int count) + { + _places = new Object[count]; + Count = _places.length; + } + + // Добавление объекта в набор + public int Insert(T airbus) + { + return Insert(airbus, 0); + } + + // Добавление объекта в набор на конкретную позицию + public int Insert(T airbus, int position) + { + if (position < 0 || position >= Count) + { + return -1; + } + int pos = position; + if (_places[position] != null) { + for (int i = position + 1; i < Count; ++i) { + if (_places[i] == null) { + pos = i; + break; + } + } + if (pos == Count-1) + return -1; + for (int i = pos; i > position; --i) { + _places[i] = _places[i - 1]; + } + } + _places[position] = airbus; + return position; + } + + // Удаление объекта из набора с конкретной позиции + public boolean Remove(int position) + { + if (position < 0 || position >= Count) + { + return false; + } + _places[position] = null; + return true; + } + + // Получение объекта из набора по позиции + public T Get(int position) + { + if (position < 0 || position >= Count) + { + return null; + } + return (T)_places[position]; + } +} diff --git a/src/Main.java b/src/Main.java index ba66689..7a82767 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,6 @@ public class Main { public static void main(String[] args) { - new FormAirbus(); + new FormAirbusCollection(); } } \ No newline at end of file