diff --git a/Project/src/Direction.java b/Project/src/Direction.java index e2d98d9..aab36d9 100644 --- a/Project/src/Direction.java +++ b/Project/src/Direction.java @@ -8,7 +8,7 @@ public enum Direction private final int DirectionCode; - private Direction(int directionCode) + Direction(int directionCode) { this.DirectionCode = directionCode; } diff --git a/Project/src/DrawingAirbus.java b/Project/src/DrawingAirbus.java index 0a46bb8..e5cf23d 100644 --- a/Project/src/DrawingAirbus.java +++ b/Project/src/DrawingAirbus.java @@ -2,13 +2,20 @@ import java.awt.*; public class DrawingAirbus extends DrawingPlane { - //Инициализаци свойств + //Конструктор public DrawingAirbus(int speed, int weight, Color corpusColor, Color addColor, boolean addCompartment, boolean addEngine) { super(speed, weight, corpusColor, 70, 30); Plane = new EntityAirbus(speed, weight, corpusColor, addColor, addCompartment, addEngine); } + //Второй конструктор + public DrawingAirbus(EntityPlane plane, IAdditionalDrawingObject addWindows) + { + super(plane, addWindows); + Plane = plane; + } + @Override public void DrawTransport(Graphics g) { diff --git a/Project/src/DrawingEntities.java b/Project/src/DrawingEntities.java new file mode 100644 index 0000000..e40059c --- /dev/null +++ b/Project/src/DrawingEntities.java @@ -0,0 +1,82 @@ +import java.lang.reflect.Array; +import java.util.Random; + +public class DrawingEntities +{ + public T[] _arrPlane; + public U[] _arrWindow; + int countArrPlane = 0; + int countArrWindows = 0; + String _x_index; + String _y_index; + + //конструктор + public DrawingEntities(int sizeArr, Class objectT, Class objectU) + { + final T[] arrPlane = (T[]) Array.newInstance(objectT, sizeArr); + _arrPlane = arrPlane; + + final U[] arrWindod = (U[]) Array.newInstance(objectU, sizeArr); + _arrWindow = arrWindod; + } + + //добавить сущность-самолёт + public int Insert(T plane) + { + if(countArrPlane < _arrPlane.length) + { + _arrPlane[countArrPlane] = plane; + countArrPlane++; + + return countArrPlane - 1; + } + + return -1; + } + + //добавить кол-во и тип иллюминаторов + public int Insert(U addElement) + { + if(countArrWindows < _arrWindow.length) + { + _arrWindow[countArrWindows] = addElement; + countArrWindows++; + + return countArrWindows - 1; + } + + return -1; + } + + //фиксирование индексов + public void SetIndex(int indexFirst, int indexSecond) + { + _x_index = Integer.toString(indexFirst); + _y_index = Integer.toString(indexSecond); + } + + public DrawingPlane CreatePlane() + { + Random rnd = new Random(); + int indexPlane = 0; + int indexArrWindow = 0; + + if(countArrPlane - 1 != 0 && countArrWindows - 1 != 0) + { + indexPlane = rnd.nextInt(0, countArrPlane - 1); + indexArrWindow = rnd.nextInt(0, countArrWindows - 1); + } + + T plane = (T)_arrPlane[indexPlane]; + U windows = (U)_arrWindow[indexArrWindow]; + + SetIndex(indexPlane, indexArrWindow); + + if(plane instanceof EntityAirbus) + { + return new DrawingAirbus(plane, windows); + } + + return new DrawingPlane(plane, windows); + } +} diff --git a/Project/src/DrawingPlane.java b/Project/src/DrawingPlane.java index 9a3a4d9..74c5382 100644 --- a/Project/src/DrawingPlane.java +++ b/Project/src/DrawingPlane.java @@ -65,6 +65,13 @@ public class DrawingPlane extends JPanel _airbusHeight = planeHeight; } + //второй конструктор + public DrawingPlane(EntityPlane plane, IAdditionalDrawingObject countWindow) + { + Plane = plane; + _airplaneWindow = countWindow; + } + //установка координат позиции самолёта public void SetPosition(int x, int y, int width, int height) { @@ -135,20 +142,15 @@ public class DrawingPlane extends JPanel Graphics2D g2d = (Graphics2D)g; - //корпус + //заливка корпуса g2d.setColor(Plane.GetColor()); g2d.fillRect((int)_startPosX, (int)_startPosY + 10, 40, 10); - //заливает фигуру - g2d.setPaint(Color.BLACK); - //крыло + g2d.setColor(Color.BLACK); g2d.fillRect((int)_startPosX + 10, (int)_startPosY + 15, 20, 2); g2d.fillRect((int)_startPosX + 12, (int)_startPosY + 17, 16, 2); - //заднее поперечное крыло - g2d.fillOval((int)_startPosX - 3, (int)_startPosY + 7, 10, 5); - //задние шасси g2d.fillRect((int)_startPosX + 10, (int)_startPosY + 21, 2, 2); g2d.fillRect((int)_startPosX + 12, (int)_startPosY + 23, 4, 4); @@ -158,15 +160,37 @@ public class DrawingPlane extends JPanel g2d.fillRect((int)_startPosX + 35, (int)_startPosY + 21, 2, 2); g2d.fillRect((int)_startPosX + 35, (int)_startPosY + 23, 4, 4); - //хвостовое крыло + //заливка хвостового крыла + g2d.setColor(Plane.GetColor()); + int[] xCoordFirst = {(int)_startPosX, (int)_startPosX, (int)_startPosX + 10}; + int[] yCoordFirst = {(int)_startPosY + 10, (int)_startPosY, (int)_startPosY + 10}; + g2d.fillPolygon(xCoordFirst, yCoordFirst, xCoordFirst.length); + + //заливка носа самолёта + int[] xCoordSecond = {(int)_startPosX + 40, (int)_startPosX + 47, (int)_startPosX + 40}; + int[] yCoordSecond = {(int)_startPosY + 15, (int)_startPosY + 15, (int)_startPosY + 10}; + g2d.fillPolygon(xCoordSecond, yCoordSecond, xCoordSecond.length); + + int[] xCoordThird = {(int)_startPosX + 40, (int)_startPosX + 50, (int)_startPosX + 40}; + int[] yCoordThird = {(int)_startPosY + 15, (int)_startPosY + 15, (int)_startPosY + 20}; + g2d.fillPolygon(xCoordThird, yCoordThird, xCoordThird.length); + + //контур хвостового крыла + g2d.setColor(Color.BLACK); g2d.drawLine((int)_startPosX, (int)_startPosY + 10, (int)_startPosX, (int)_startPosY); g2d.drawLine((int)_startPosX, (int)_startPosY, (int)_startPosX + 10, (int)_startPosY + 10); - //нос самолёта - g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 15, (int)_startPosX + 50, (int)_startPosY + 16); + //отрисовка контура самолёта + g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 15, (int)_startPosX + 50, (int)_startPosY + 15); g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 10, (int)_startPosX + 47, (int)_startPosY + 15); g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 20, (int)_startPosX + 50, (int)_startPosY + 15); + //контур корпуса + g2d.drawRect((int)_startPosX, (int)_startPosY + 10, 40, 10); + + //заднее поперечное крыло + g2d.fillOval((int)_startPosX - 3, (int)_startPosY + 7, 10, 5); + //отрисовка иллюминаторов _airplaneWindow.DrawAirplaneWindow(Plane.GetColor(), g, _startPosX, _startPosY); } diff --git a/Project/src/EntityPlane.java b/Project/src/EntityPlane.java index d8e678d..2e3ba1f 100644 --- a/Project/src/EntityPlane.java +++ b/Project/src/EntityPlane.java @@ -4,13 +4,15 @@ public class EntityPlane { //скорость public int Speed; - public int GetSpeed(){ + public int GetSpeed() + { return Speed; } //вес public float Weight; - public float GetWeight(){ + public float GetWeight() + { return Weight; } diff --git a/Project/src/FormMap.form b/Project/src/FormMap.form deleted file mode 100644 index 0eeac40..0000000 --- a/Project/src/FormMap.form +++ /dev/null @@ -1,127 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/Project/src/FormMap.java b/Project/src/FormMap.java deleted file mode 100644 index e08377f..0000000 --- a/Project/src/FormMap.java +++ /dev/null @@ -1,201 +0,0 @@ -import javax.imageio.ImageIO; -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.image.BufferedImage; -import java.util.Random; - -public class FormMap extends JFrame -{ - public JPanel MainPanel; - private JButton ButtonCreate; - private JButton ButtonCreateModif; - private JButton ButtonLeft; - private JButton ButtonDown; - private JButton ButtonRight; - private JButton ButtonUp; - private JToolBar StatusStrip; - private JPanel PictureBoxPlane; - private JComboBox ComboBoxSelectorMap; - private JLabel LabelSpeed = new JLabel(); - private JLabel LabelWeight = new JLabel(); - private JLabel LabelColor = new JLabel(); - - protected DrawingPlane _plane; - private AbstractMap _abstractMap; - private Random rnd = new Random(); - private BufferedImage bufferImg = null; - - public void Draw() - { - if (bufferImg == null) - { - return; - } - - PictureBoxPlane.getGraphics().drawImage(bufferImg, 0, 0, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), null); - } - - public void SetData(DrawingPlane _plane) - { - PictureBoxPlane.removeAll(); - - LabelSpeed.setText("Скорость: " + _plane.GetPlane().GetSpeed() + " "); - LabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " "); - LabelColor.setText("Цвет: r = " + _plane.GetPlane().GetColor().getRed() + " g = " + _plane.GetPlane().GetColor().getGreen() + - " b = " + _plane.GetPlane().GetColor().getBlue()); - - if (_abstractMap != null) - { - bufferImg = _abstractMap.CreateMap(PictureBoxPlane.getWidth(), - PictureBoxPlane.getHeight(), new DrawningObjectPlane(_plane)); - } - - PictureBoxPlane.revalidate(); - } - - public FormMap() - { - _abstractMap = new SimpleMap(); - - //создание строки отображения скорости, веса и цвета объекта - Box LableBox = Box.createHorizontalBox(); - LableBox.setMinimumSize(new Dimension(1, 20)); - LableBox.add(LabelSpeed); - LableBox.add(LabelWeight); - LableBox.add(LabelColor); - StatusStrip.add(LableBox); - - try - { - Image img = ImageIO.read(getClass().getResource("resourses/Up.png")); - ButtonUp.setIcon(new ImageIcon(img)); - img = ImageIO.read(getClass().getResource("resourses/Left.png")); - ButtonLeft.setIcon(new ImageIcon(img)); - img = ImageIO.read(getClass().getResource("resourses/Down.png")); - ButtonDown.setIcon(new ImageIcon(img)); - img = ImageIO.read(getClass().getResource("resourses/Right.png")); - ButtonRight.setIcon(new ImageIcon(img)); - } - catch (Exception ex) - { - System.out.println(ex.getMessage()); - } - - ButtonCreate.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - Random rnd = new Random(); - var plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); - plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), - PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); - SetData(plane); - Draw(); - } - }); - - ButtonUp.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - PictureBoxPlane.removeAll(); - - JLabel imageWithMapAndObject = new JLabel(); - imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); - imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject((Direction.Up)))); - - PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); - PictureBoxPlane.revalidate(); - PictureBoxPlane.repaint(); - Draw(); - } - }); - - ButtonLeft.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - PictureBoxPlane.removeAll(); - - JLabel imageWithMapAndObject = new JLabel(); - imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); - imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject((Direction.Left)))); - - PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); - PictureBoxPlane.revalidate(); - PictureBoxPlane.repaint(); - Draw(); - } - }); - - ButtonDown.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - PictureBoxPlane.removeAll(); - - JLabel imageWithMapAndObject = new JLabel(); - imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); - imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject((Direction.Down)))); - - PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); - PictureBoxPlane.revalidate(); - PictureBoxPlane.repaint(); - Draw(); - } - }); - - ButtonRight.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - PictureBoxPlane.removeAll(); - - JLabel imageWithMapAndObject = new JLabel(); - imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); - imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject((Direction.Right)))); - - PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); - PictureBoxPlane.revalidate(); - PictureBoxPlane.repaint(); - Draw(); - } - }); - - ButtonCreateModif.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - _plane = new DrawingAirbus(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - 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()); - _plane.SetPosition(rnd.nextInt(100, 500), rnd.nextInt(10, 100), - PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); - SetData(_plane); - Draw(); - } - }); - - ComboBoxSelectorMap.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - ComboBoxSelectorMap = (JComboBox)e.getSource(); - String item = (String)ComboBoxSelectorMap.getSelectedItem(); - switch(item) - { - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Буря в пустыне": - _abstractMap = new DesertStormMap(); - break; - case "Звёздные войны": - _abstractMap = new StarWarsMap(); - break; - } - } - }); - } -} diff --git a/Project/src/FormMapWithSetPlanesGeneric.form b/Project/src/FormMapWithSetPlanesGeneric.form new file mode 100644 index 0000000..0604884 --- /dev/null +++ b/Project/src/FormMapWithSetPlanesGeneric.form @@ -0,0 +1,189 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Project/src/FormMapWithSetPlanesGeneric.java b/Project/src/FormMapWithSetPlanesGeneric.java new file mode 100644 index 0000000..0bf2364 --- /dev/null +++ b/Project/src/FormMapWithSetPlanesGeneric.java @@ -0,0 +1,272 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.image.BufferedImage; + +public class FormMapWithSetPlanesGeneric { + public JPanel MainPanel; + private JPanel PictureBoxPlane; + private JPanel ButtonGroupPanel; + private JComboBox ComboBoxSelectorMap; + private JButton ButtonLeft; + private JButton ButtonDown; + private JButton ButtonRight; + private JButton ButtonUp; + private JButton ButtonShowOnMap; + private JButton ButtonShowStorage; + private JButton ButtonAddPlane; + private JTextField MaskedTextBoxPosition; + private JButton ButtonRemovePlane; + + //объект от класса карты с набором объектов + private MapWithSetPlanesGeneric _mapPlanesCollectionGeneric; + + //обновление отрисовки + public void UpdateWindow(BufferedImage bmp) + { + PictureBoxPlane.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(bmp)); + PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); + PictureBoxPlane.revalidate(); + } + + public FormMapWithSetPlanesGeneric() + { + //загрузка изображений для кнопок + try + { + Image img = ImageIO.read(getClass().getResource("resourses/Up.png")); + ButtonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(getClass().getResource("resourses/Left.png")); + ButtonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(getClass().getResource("resourses/Down.png")); + ButtonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(getClass().getResource("resourses/Right.png")); + ButtonRight.setIcon(new ImageIcon(img)); + } + catch (Exception ex) + { + System.out.println(ex.getMessage()); + } + + //добавление самолёта + ButtonAddPlane.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_mapPlanesCollectionGeneric == null) + { + return; + } + + FormPlane form = new FormPlane(); + form.setSize(700,400); + form.setVisible(true); + form.setModal(true); + DrawningObjectPlane plane = new DrawningObjectPlane(form.GetSelectedShip()); + + if(_mapPlanesCollectionGeneric.Add(plane) != -1) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + UpdateWindow(_mapPlanesCollectionGeneric.ShowSet()); + } + else + { + JOptionPane.showMessageDialog(null,"Не удалось добавить объект"); + } + } + }); + + //удаление самолёта + ButtonRemovePlane.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (MaskedTextBoxPosition.getText().isEmpty()) + { + return; + } + + int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление", + JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); + + if(result==JOptionPane.NO_OPTION) + { + return; + } + + int pos = Integer.parseInt(MaskedTextBoxPosition.getText()); + + if(_mapPlanesCollectionGeneric.Delete(pos) != null) + { + JOptionPane.showMessageDialog(null,"Объект удалён"); + UpdateWindow(_mapPlanesCollectionGeneric.ShowSet()); + } + else + { + JOptionPane.showMessageDialog(null,"Не удалось удалить объект"); + } + } + }); + + //показ хранилища + ButtonShowStorage.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_mapPlanesCollectionGeneric == null) + { + return; + } + + UpdateWindow(_mapPlanesCollectionGeneric.ShowSet()); + } + }); + + //показ карты + ButtonShowOnMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_mapPlanesCollectionGeneric == null) + { + return; + } + + UpdateWindow(_mapPlanesCollectionGeneric.ShowOnMap()); + } + }); + + ButtonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_mapPlanesCollectionGeneric == null) + { + return; + } + + PictureBoxPlane.removeAll(); + + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Up)))); + + PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); + PictureBoxPlane.revalidate(); + PictureBoxPlane.repaint(); + } + }); + + ButtonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_mapPlanesCollectionGeneric == null) + { + return; + } + + PictureBoxPlane.removeAll(); + + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Left)))); + + PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); + PictureBoxPlane.revalidate(); + PictureBoxPlane.repaint(); + } + }); + + ButtonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_mapPlanesCollectionGeneric == null) + { + return; + } + + PictureBoxPlane.removeAll(); + + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Down)))); + + PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); + PictureBoxPlane.revalidate(); + PictureBoxPlane.repaint(); + } + }); + + ButtonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_mapPlanesCollectionGeneric == null) + { + return; + } + + PictureBoxPlane.removeAll(); + + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(PictureBoxPlane.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapPlanesCollectionGeneric.MoveObject((Direction.Right)))); + + PictureBoxPlane.add(imageWithMapAndObject, BorderLayout.CENTER); + PictureBoxPlane.revalidate(); + PictureBoxPlane.repaint(); + } + }); + + //выпадающий список с вариантами карт + ComboBoxSelectorMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + AbstractMap map = null; + + ComboBoxSelectorMap = (JComboBox)e.getSource(); + String item = (String)ComboBoxSelectorMap.getSelectedItem(); + switch(item) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Буря в пустыне": + map = new DesertStormMap(); + break; + case "Звёздные войны": + map = new StarWarsMap(); + break; + } + + if(map != null) + { + _mapPlanesCollectionGeneric = new MapWithSetPlanesGeneric( + PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), map); + } + else + { + _mapPlanesCollectionGeneric = null; + } + } + }); + + + MaskedTextBoxPosition.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + char c = e.getKeyChar(); + + if ( ((c < '0') || (c > '9')) || MaskedTextBoxPosition.getText().length() >= 2) { + e.consume(); + } + } + }); + } +} diff --git a/Project/src/FormParam.form b/Project/src/FormParam.form new file mode 100644 index 0000000..90819ad --- /dev/null +++ b/Project/src/FormParam.form @@ -0,0 +1,77 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Project/src/FormParam.java b/Project/src/FormParam.java new file mode 100644 index 0000000..b8b89d1 --- /dev/null +++ b/Project/src/FormParam.java @@ -0,0 +1,128 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class FormParam extends JFrame +{ + public JPanel MainPanel; + private JButton ButtonAddPlane; + private JPanel PictureBoxPlane; + private JButton ButtonCreateModif; + private JLabel LabelInfo; + private JLabel LabelSpeed = new JLabel(); + private JLabel LabelWeight = new JLabel(); + private JLabel LabelColor = new JLabel(); + private JToolBar StatusStrip; + + private DrawingEntities _drawingEntities; + + private IAdditionalDrawingObject SetData() + { + Random rnd = new Random(); + int r = rnd.nextInt(3); + + if(r == 0) + { + return new DrawingAirplaneWindow(); + } + + if(r == 1) + { + return new DrawingTriangleAirplaneWindow(); + } + + return new DrawingRectAirplaneWindow(); + } + + private void Draw(DrawingPlane _plane) + { + PictureBoxPlane.removeAll(); + Random rnd = new Random(); + BufferedImage bmp = new BufferedImage(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + gr.setColor(new Color(238, 238, 238)); + gr.fillRect(0, 0, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + + if(_plane != null) + { + _plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), + PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + _plane.DrawTransport(gr); + LabelSpeed.setText("Скорость: " + _plane.GetPlane().GetSpeed() + " "); + LabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " "); + LabelColor.setText("Цвет: r = " + _plane.GetPlane().GetColor().getRed() + " g = " + _plane.GetPlane().GetColor().getGreen() + + " b = " + _plane.GetPlane().GetColor().getBlue()); + JLabel imageOfLogo = new JLabel(); + imageOfLogo.setPreferredSize(PictureBoxPlane.getSize()); + imageOfLogo.setMinimumSize(new Dimension(1, 1)); + imageOfLogo.setIcon(new ImageIcon(bmp)); + PictureBoxPlane.add(imageOfLogo, BorderLayout.CENTER); + } + + validate(); + } + + public FormParam() + { + //создание строки отображения скорости, веса и цвета объекта + Box LableBox = Box.createHorizontalBox(); + LableBox.setMinimumSize(new Dimension(1, 20)); + LableBox.add(LabelSpeed); + LableBox.add(LabelWeight); + LableBox.add(LabelColor); + StatusStrip.add(LableBox); + + _drawingEntities = new DrawingEntities<>(10, EntityPlane.class, IAdditionalDrawingObject.class); + + ButtonAddPlane.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + Color colorCorpus = JColorChooser.showDialog(null, "Выбор цвета", null); + EntityPlane plane = new EntityPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), colorCorpus); + IAdditionalDrawingObject addWindows = SetData(); + int countWindows = rnd.nextInt(1, 4); + addWindows.SetAddEnum(countWindows); + + if((_drawingEntities.Insert(plane) != -1) && (_drawingEntities.Insert(addWindows) != -1)) + { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + Draw(_drawingEntities.CreatePlane()); + LabelInfo.setText(_drawingEntities._x_index + " " + _drawingEntities._y_index); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + }); + + ButtonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Выбор цвета", null); + Color colorSecond = JColorChooser.showDialog(null, "Выбор цвета", null); + EntityAirbus airbus = new EntityAirbus(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + colorFirst, colorSecond, rnd.nextBoolean(), rnd.nextBoolean()); + IAdditionalDrawingObject addWindows = SetData(); + int countWindows = rnd.nextInt(1, 4); + addWindows.SetAddEnum(countWindows); + + if((_drawingEntities.Insert(airbus) != -1) && (_drawingEntities.Insert(addWindows) != -1)) + { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + Draw(_drawingEntities.CreatePlane()); + LabelInfo.setText(_drawingEntities._x_index + " " + _drawingEntities._y_index); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + }); + } +} diff --git a/Project/src/FormPlane.form b/Project/src/FormPlane.form index 9f4bf18..294e38b 100644 --- a/Project/src/FormPlane.form +++ b/Project/src/FormPlane.form @@ -1,16 +1,16 @@
- + - + - + @@ -23,7 +23,7 @@ - + @@ -34,38 +34,21 @@ - - - - - - - - - + + + + - - - - - - - - - - - - - + @@ -73,7 +56,7 @@ - + @@ -86,7 +69,7 @@ - + @@ -97,14 +80,46 @@ - + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Project/src/FormPlane.java b/Project/src/FormPlane.java index 27b4444..cf07032 100644 --- a/Project/src/FormPlane.java +++ b/Project/src/FormPlane.java @@ -1,16 +1,12 @@ import javax.imageio.ImageIO; import javax.swing.*; 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.awt.event.*; +import java.awt.image.BufferedImage; import java.util.Random; -public class FormPlane +public class FormPlane extends JDialog { - protected DrawingPlane plane; - public JPanel MainPanel; private JButton ButtonCreate; private JButton ButtonLeft; @@ -19,12 +15,54 @@ public class FormPlane private JButton ButtonUp; private JToolBar StatusStrip; private JPanel PictureBoxPlane; + private JButton ButtonCreateModif; + private JButton ButtonSelectPlane; private JLabel LabelSpeed = new JLabel(); private JLabel LabelWeight = new JLabel(); private JLabel LabelColor = new JLabel(); + protected DrawingPlane plane; + protected DrawingPlane _selectedPlane; + + public DrawingPlane GetSelectedShip() + { + return _selectedPlane; + } + + //метод отрисовки + public void Draw(DrawingPlane _plane) { + PictureBoxPlane.removeAll(); + BufferedImage bmp = new BufferedImage(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + + gr.setColor(new Color(238, 238, 238)); + gr.fillRect(0, 0, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + + if (plane.GetPlane() != null) { + _plane.DrawTransport(gr); + JLabel imageOfLogo = new JLabel(); + imageOfLogo.setPreferredSize(PictureBoxPlane.getSize()); + imageOfLogo.setMinimumSize(new Dimension(1, 1)); + imageOfLogo.setIcon(new ImageIcon(bmp)); + PictureBoxPlane.add(imageOfLogo, BorderLayout.CENTER); + } + + validate(); + } + + //создание всплывающего окна public FormPlane() { + super(new Frame("Airbus")); + CreateWindow(); + setModal(true); + getContentPane().add(MainPanel); + } + + public void CreateWindow() + { + setModal(true); + //создание строки отображения скорости, веса и цвета объекта Box LableBox = Box.createHorizontalBox(); LableBox.setMinimumSize(new Dimension(1, 20)); @@ -49,30 +87,60 @@ public class FormPlane System.out.println(ex.getMessage()); } + _selectedPlane = plane; ButtonCreate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Random rnd = new Random(); + Color colorSimple = JColorChooser.showDialog(null, "Выберите цвет", null); + plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + colorSimple); plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + LabelSpeed.setText("Скорость: " + plane.GetPlane().GetSpeed() + " "); LabelWeight.setText("Вес: " + plane.GetPlane().GetWeight() + " "); LabelColor.setText("Цвет: r = " + plane.GetPlane().GetColor().getRed() + " g = " + plane.GetPlane().GetColor().getGreen() + " b = " + plane.GetPlane().GetColor().getBlue()); - Draw(); + + Draw(plane); } }); - //обновление размеров формы - MainPanel.addComponentListener(new ComponentAdapter() { + ButtonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + Color colorSimple = JColorChooser.showDialog(null, "Выберите цвет", null); + Color colorModif = JColorChooser.showDialog(null, "Выберите цвет", null); + + plane = new DrawingAirbus(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + colorSimple, colorModif, rnd.nextBoolean(), rnd.nextBoolean()); + plane.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), + PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + + LabelSpeed.setText("Скорость: " + plane.GetPlane().GetSpeed() + " "); + LabelWeight.setText("Вес: " + plane.GetPlane().GetWeight() + " "); + LabelColor.setText("Цвет: r = " + plane.GetPlane().GetColor().getRed() + " g = " + plane.GetPlane().GetColor().getGreen() + + " b = " + plane.GetPlane().GetColor().getBlue()); + + Draw(plane); + } + }); + + PictureBoxPlane.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { super.componentResized(e); - plane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); - Draw(); + + if(plane != null) + { + plane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + PictureBoxPlane.revalidate(); + Draw(plane); + } } }); @@ -80,7 +148,8 @@ public class FormPlane @Override public void actionPerformed(ActionEvent e) { plane.MoveTransport(Direction.Up); - Draw(); + PictureBoxPlane.revalidate(); + Draw(plane); } }); @@ -88,7 +157,8 @@ public class FormPlane @Override public void actionPerformed(ActionEvent e) { plane.MoveTransport(Direction.Left); - Draw(); + PictureBoxPlane.revalidate(); + Draw(plane); } }); @@ -96,7 +166,8 @@ public class FormPlane @Override public void actionPerformed(ActionEvent e) { plane.MoveTransport(Direction.Down); - Draw(); + PictureBoxPlane.revalidate(); + Draw(plane); } }); @@ -104,18 +175,18 @@ public class FormPlane @Override public void actionPerformed(ActionEvent e) { plane.MoveTransport(Direction.Right); - Draw(); + PictureBoxPlane.revalidate(); + Draw(plane); + } + }); + + //кнопка подтверждение выбора + ButtonSelectPlane.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _selectedPlane = plane; + dispose(); } }); } - - public void Draw() - { - if(plane.GetPlane() == null) - { - return; - } - - plane.DrawTransport(PictureBoxPlane.getGraphics()); - } } diff --git a/Project/src/Main.java b/Project/src/Main.java index 6387f7b..2641f29 100644 --- a/Project/src/Main.java +++ b/Project/src/Main.java @@ -4,12 +4,12 @@ public class Main { public static void main(String[] args) { - JFrame frame = new JFrame("Airbus"); - frame.setContentPane(new FormMap().MainPanel); + JFrame frame = new JFrame("Аэробус"); + frame.setContentPane(new FormParam().MainPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(500, 200); frame.pack(); - frame.setSize(1000, 500); + frame.setSize(800, 500); frame.setVisible(true); } } diff --git a/Project/src/MapWithSetPlanesGeneric.java b/Project/src/MapWithSetPlanesGeneric.java new file mode 100644 index 0000000..acfdb66 --- /dev/null +++ b/Project/src/MapWithSetPlanesGeneric.java @@ -0,0 +1,205 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetPlanesGeneric +{ + //ширина окна отрисовки + private final int _pictureWidth; + + //высота окна отрисовки + private final int _pictureHeight; + + //размер занимаемого объектом места (ширина) + private final int _placeSizeWidth = 210; + + //размер занимаемого объектом места (высота) + private final int _placeSizeHeight = 90; + + //набор объектов + private final SetPlanesGeneric _setPlanes; + + //карта + private final U _map; + + //конструктор + public MapWithSetPlanesGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setPlanes = new SetPlanesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int Add(T plane) + { + return _setPlanes.Insert(plane); + } + + public T Delete(int position) + { + return _setPlanes.Remove(position); + } + + //вывод всего набора объектов + public BufferedImage ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + DrawBackground(gr); + DrawPlanes(gr); + return bmp; + } + + //просмотр объекта на карте + public BufferedImage ShowOnMap() + { + Shaking(); + + for(int i = 0; i < _setPlanes.Count(); i++) + { + var plane = _setPlanes.Get(i); + + if(plane != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, plane); + } + } + + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + //пеермещение объекта по карте + public BufferedImage MoveObject(Direction direction) + { + if(_map != null) + { + return _map.MoveObject(direction); + } + + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + //"взламываем" набор, чтобы все элементы оказались в начале + private void Shaking() + { + int j = _setPlanes.Count() - 1; + + for (int i = 0; i < _setPlanes.Count(); i++) + { + if (_setPlanes.Get(i) == null) + { + for (; j > i; j--) + { + var plane = _setPlanes.Get(j); + + if (plane != null) + { + _setPlanes.Insert(plane, i); + _setPlanes.Remove(j); + break; + } + } + + if (j <= i) + { + return; + } + } + } + } + + //метод отрисовки фона + public void DrawBackground(Graphics g) + { + Graphics2D g2d = (Graphics2D) g; + + //заливаем область в цвет бетона + g2d.setPaint(Color.LIGHT_GRAY); + g2d.fillRect(0, 0, _pictureWidth, _pictureHeight); + + g2d.setStroke(new BasicStroke(3)); + g2d.setColor(Color.BLACK); + + for(int i = 0; i < _pictureWidth / _placeSizeWidth - 1; i++) + { + //линия разметки места + for(int j = 2; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g2d.drawLine(i * _placeSizeWidth + 5, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 5, j * _placeSizeHeight); + } + + g2d.drawLine(i * _placeSizeWidth + 5, _placeSizeHeight * 2, i * _placeSizeWidth + 5, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + + //отрисовка разметки взлётной полосы + g2d.setColor(Color.DARK_GRAY); + g2d.fillRect(_placeSizeWidth * 2 + 35, 0, 175, _pictureHeight); + + g2d.setStroke(new BasicStroke(5)); + g2d.setColor(Color.WHITE); + g2d.drawLine(_placeSizeWidth * 2 + 210, 0, _placeSizeWidth * 2 + 210, _pictureHeight); + g2d.drawLine(_placeSizeWidth * 2 + 35, 0, _placeSizeWidth * 2 + 35, _pictureHeight); + g2d.drawLine(_placeSizeWidth * 2 + 215, 0, _placeSizeWidth * 2 + 215, _pictureHeight); + g2d.drawLine(_placeSizeWidth * 2 + 30, 0, _placeSizeWidth * 2 + 30, _pictureHeight); + + g2d.setStroke(new BasicStroke(5)); + g2d.setColor(Color.WHITE); + for (int i = 0; i < _pictureHeight / _placeSizeHeight; ++i) + { + g2d.drawLine(_placeSizeWidth * 2 + 125, 20 + i * _placeSizeHeight, _placeSizeWidth * 2 + 125, (i + 1) * _placeSizeHeight - 20); + } + + g2d.setColor(new Color(0xFF, 0x45, 0x00)); + for(int i = 0; i < _pictureHeight / 20; i++) + { + g2d.drawLine(_placeSizeWidth * 2 + 15, 20 + i * _placeSizeHeight / 2, _placeSizeWidth * 2 + 15, (i + 1) * _placeSizeHeight / 2 - 20); + g2d.drawLine(_placeSizeWidth * 3 + 20, 20 + i * _placeSizeHeight / 2, _placeSizeWidth * 3 + 20, (i + 1) * _placeSizeHeight / 2 - 20); + } + + //отрисовка сочков + for(int i = 1; i < 6; i++) + { + g2d.setColor(new Color(0xFF, 0xB6, 0xC1)); + int[] xPoint = {(i * 70 - 10) + 45, i * 70 - 10, i * 70 - 10}; + int[] yPoint = {30, 50, 10}; + g.fillPolygon(xPoint, yPoint, xPoint.length); + + g2d.setStroke(new BasicStroke(3)); + g2d.setColor(Color.BLACK); + g2d.drawLine(i * 70 - 10, 10, i * 70 - 10, 80); + g2d.drawLine(i * 70 - 10, 10, (i * 70 - 10) + 45, 30); + g2d.drawLine(i * 70 - 10, 50, (i * 70 - 10) + 45, 30); + } + } + + //метод прорисовки объеков + public void DrawPlanes(Graphics g) + { + int position = 0; + int currentWidth = 1; + int currentHeight = 5; + + for (int i = 0; i < _setPlanes.Count(); i++) + { + if(_setPlanes.Get(i) != null) + { + _setPlanes.Get(i).SetObject(currentWidth * _placeSizeWidth + 20, currentHeight * _placeSizeHeight + 20, _pictureWidth, _pictureHeight); + _setPlanes.Get(i).DrawningObject(g); + } + + if(position % 2 == 0) + { + position++; + currentWidth--; + } + else + { + position = 0; + currentWidth = 1; + currentHeight--; + } + } + } +} diff --git a/Project/src/SetPlanesGeneric.java b/Project/src/SetPlanesGeneric.java new file mode 100644 index 0000000..7d7794c --- /dev/null +++ b/Project/src/SetPlanesGeneric.java @@ -0,0 +1,99 @@ +public class SetPlanesGeneric +{ + //массив объектов, которые храним + private final Object[] _places; + + //количество объектов в массиве + public int Count() + { + return _places.length; + } + + //конструктор + public SetPlanesGeneric(int count) + { + _places = new Object[count]; + } + + //добавление объекта в набор + public int Insert(T plane) + { + return Insert(plane, 0); + } + + //добавление объекта в набор на конкретную позицию + public int Insert(T plane, int position) + { + //проверка на корректность значения индекса + if (position >= _places.length || position < 0) + { + return -1; + } + + //проверка ячейки на пустоту + if (_places[position] == null) + { + _places[position] = plane; + + return position; + } + + //поиск первой свободной ячейки + int _emptyPositionIndex = -1; + for (int i = position + 1; i < Count(); i++) + { + if (_places[i] == null) + { + _emptyPositionIndex = i; + break; + } + } + + //есла пустых ячеек нет + if (_emptyPositionIndex < 0) + { + return -1; + } + + //сдвиг объектов + for (int i = _emptyPositionIndex; i > position; i--) + { + _places[i] = _places[i - 1]; + } + + _places[position] = plane; + + return position; + } + + //удаление объекта из набора с конкретной позиции + public T Remove(int position) + { + // проверка позиции + if (position >= _places.length || position < 0) + { + return null; + } + + // удаление объекта из массива, присовив элементу массива значение null + T temp = (T)_places[position]; + _places[position] = null; + + return temp; + } + + //получение объекта из набора по позиции + public T Get(int position) + { + if (position >= _places.length || position < 0) + { + return null; + } + else if (_places[position] == null) + { + return null; + } + + return (T)_places[position]; + } +}