diff --git a/Project/src/DrawingAirbus.java b/Project/src/DrawingAirbus.java index 8dab024..5d8009b 100644 --- a/Project/src/DrawingAirbus.java +++ b/Project/src/DrawingAirbus.java @@ -2,11 +2,14 @@ import java.awt.*; public class DrawingAirbus extends DrawingPlane { + private Color addColor; + //Конструктор - public DrawingAirbus(int speed, int weight, Color corpusColor, Color addColor, boolean addCompartment, boolean addEngine) + public DrawingAirbus(int speed, int weight, Color corpusColor, int countWindow, Color addColor, boolean addCompartment, boolean addEngine) { - super(speed, weight, corpusColor, 70, 30); + super(speed, weight, corpusColor, countWindow, 70, 30); Plane = new EntityAirbus(speed, weight, corpusColor, addColor, addCompartment, addEngine); + this.addColor = addColor; } //Второй конструктор @@ -16,6 +19,22 @@ public class DrawingAirbus extends DrawingPlane Plane = plane; } + public void SetAddColor(Color color) + { + var temp = (EntityAirbus) Plane; + Plane = new EntityAirbus(temp.GetSpeed(), temp.GetWeight(), temp.GetColor(), color, + temp.AddCompartment(), temp.AddEngine()); + addColor = color; + } + + @Override + public void SetColor(Color color) + { + var temp = (EntityAirbus) Plane; + addColor = addColor == null ? Color.WHITE : addColor; + Plane = new EntityAirbus(temp.GetSpeed(), temp.GetWeight(), color, addColor, temp.AddCompartment(), temp.AddEngine()); + } + @Override public void DrawTransport(Graphics g) { @@ -35,21 +54,23 @@ public class DrawingAirbus extends DrawingPlane _startPosY -= 5; //дополнительный пассажирский отсек - if (airbus.AddСompartment) + if (airbus.AddCompartment()) { - g2d.setPaint(airbus.AddColor); + g2d.setPaint(airbus.AddColor()); g2d.fillRect((int)_startPosX + 30, (int)_startPosY + 12, 14, 3); g2d.setPaint(Color.BLACK); g2d.drawRect((int)_startPosX + 30, (int)_startPosY + 12, 14, 3); } //дополнительный двигатель - if (airbus.AddEngine) + if (airbus.AddEngine()) { - g2d.setPaint(airbus.AddColor); + g2d.setPaint(airbus.AddColor()); g2d.fillOval((int)_startPosX + 24, (int)_startPosY + 22, 10, 5); g2d.setPaint(Color.BLACK); g2d.drawOval((int)_startPosX + 24, (int)_startPosY + 22, 10, 5); } + + super.repaint(); } } diff --git a/Project/src/DrawingPlane.java b/Project/src/DrawingPlane.java index fe40eed..c3c1822 100644 --- a/Project/src/DrawingPlane.java +++ b/Project/src/DrawingPlane.java @@ -5,42 +5,6 @@ import java.util.Random; public class DrawingPlane extends JPanel { - protected EntityPlane Plane; //класс-сущность - public IAdditionalDrawingObject _airplaneWindow; //для дополнительной отрисовки - - public void SetAddEnum() - { - Random rnd = new Random(); - int numbEnum = rnd.nextInt(1, 4); - _airplaneWindow.SetAddEnum(numbEnum); - } - - public void SetFormEnum() - { - Random rnd = new Random(); - int numbEnum = rnd.nextInt(1, 4); - - if(numbEnum == 1) - { - _airplaneWindow = new DrawingAirplaneWindow(); - } - - if(numbEnum == 2) - { - _airplaneWindow = new DrawingTriangleAirplaneWindow(); - } - - if(numbEnum == 3) - { - _airplaneWindow = new DrawingRectAirplaneWindow(); - } - } - - public EntityPlane GetPlane() - { - return Plane; - } - protected float _startPosX; //левая координата отрисовки protected float _startPosY; //верхняя координата отрисовки private Integer _pictureWidth = null; //ширина окна отрисовки @@ -48,18 +12,52 @@ public class DrawingPlane extends JPanel protected int _airbusWidth = 50; //ширина отрисовки самолёта protected int _airbusHeight = 16; //высота отрисовки самолёта - //конструктор - public DrawingPlane(int speed, float weight, Color corpusColor) + protected EntityPlane Plane; //класс-сущность + public IAdditionalDrawingObject _airplaneWindow; //для дополнительной отрисовки + + public void SetColor(Color color) { - Plane = new EntityPlane(speed, weight, corpusColor); - SetFormEnum(); - SetAddEnum(); + Plane = new EntityPlane(Plane.GetSpeed(), Plane.GetWeight(), color); + } + + public void SetTypeWindow(int countWindow, IAdditionalDrawingObject window) + { + _airplaneWindow = window; + _airplaneWindow.SetAddEnum(countWindow); + } + + public EntityPlane GetPlane() + { + return Plane; } //конструктор - public DrawingPlane(int speed, float weight, Color corpusColor, int planeWidth, int planeHeight) + public DrawingPlane(int speed, float weight, Color corpusColor, int countWindow) { - this(speed, weight, corpusColor); + Random rnd = new Random(); + int randomTypeWindow = rnd.nextInt(3); + Plane = new EntityPlane(speed, weight, corpusColor); + + if(randomTypeWindow == 1) + { + _airplaneWindow = new DrawingAirplaneWindow(); + } + else if(randomTypeWindow == 2) + { + _airplaneWindow = new DrawingRectAirplaneWindow(); + } + else + { + _airplaneWindow = new DrawingTriangleAirplaneWindow(); + } + + _airplaneWindow.SetAddEnum(countWindow); + } + + //конструктор + public DrawingPlane(int speed, float weight, Color corpusColor, int countWindow, int planeWidth, int planeHeight) + { + this(speed, weight, corpusColor, countWindow); _airbusWidth = planeWidth; _airbusHeight = planeHeight; } diff --git a/Project/src/EntityAirbus.java b/Project/src/EntityAirbus.java index 10cfcfe..af6603c 100644 --- a/Project/src/EntityAirbus.java +++ b/Project/src/EntityAirbus.java @@ -3,20 +3,32 @@ import java.awt.*; public class EntityAirbus extends EntityPlane { //Дополнительный цвет - public Color AddColor; + public Color addColor; + public Color AddColor() + { + return addColor; + } //Признак наличия дополнительно пассажирского отсека - public boolean AddСompartment; + public boolean addСompartment; + public boolean AddCompartment() + { + return addСompartment; + } //Признак наличия дополнительных двигателей - public boolean AddEngine; + public boolean addEngine; + public boolean AddEngine() + { + return addEngine; + } //Инициализация свойств public EntityAirbus(int speed, float weight, Color corpusColor, Color addColor, boolean addCompartment, boolean addEngine) { super(speed, weight, corpusColor); - AddColor = addColor; - AddСompartment = addCompartment; - AddEngine = addEngine; + this.addColor = addColor; + this.addСompartment = addCompartment; + this.addEngine = addEngine; } } diff --git a/Project/src/FormMapWithSetPlanesGeneric.java b/Project/src/FormMapWithSetPlanesGeneric.java index cc74529..88b52b3 100644 --- a/Project/src/FormMapWithSetPlanesGeneric.java +++ b/Project/src/FormMapWithSetPlanesGeneric.java @@ -10,7 +10,7 @@ import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.util.HashMap; -public class FormMapWithSetPlanesGeneric { +public class FormMapWithSetPlanesGeneric extends JFrame{ public JPanel MainPanel; private JPanel PictureBoxPlane; private JPanel GroupBoxTools; @@ -44,6 +44,7 @@ public class FormMapWithSetPlanesGeneric { public FormMapWithSetPlanesGeneric() { + super("Хранилище"); CreateWindow(); _mapsCollection = new MapsCollection(730, 650); ComboBoxSelectorMap.removeAllItems(); @@ -186,30 +187,36 @@ public class FormMapWithSetPlanesGeneric { } }); - //добавление самолёта + //добавление самолёта с использованием делегата ButtonAddPlane.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(ListBoxMaps.getSelectedIndex() == -1) - { - return; - } + FormPlaneConfig form = new FormPlaneConfig(); - FormPlane form = new FormPlane(); - form.setSize(700,400); + form.AddEvent(newPlane ->{ + if(ListBoxMaps.getSelectedIndex() == -1) + { + return; + } + + if(newPlane != null) + { + DrawningObjectPlane plane = new DrawningObjectPlane(newPlane); + + if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Add(plane) != -1) + { + JOptionPane.showMessageDialog(null, "Объект добавлен"); + UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + } + }); + + form.setSize(975, 360); form.setVisible(true); - form.setModal(true); - DrawningObjectPlane plane = new DrawningObjectPlane(form.GetSelectedShip()); - - if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Add(plane) != -1) - { - JOptionPane.showMessageDialog(null,"Объект добавлен"); - UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet()); - } - else - { - JOptionPane.showMessageDialog(null,"Не удалось добавить объект"); - } } }); @@ -359,7 +366,8 @@ public class FormMapWithSetPlanesGeneric { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); - if ((c < '0') || (c > '9')) { + if ((c < '0') || (c > '9')) + { e.consume(); } } diff --git a/Project/src/FormPlane.java b/Project/src/FormPlane.java index dabac37..5700e0a 100644 --- a/Project/src/FormPlane.java +++ b/Project/src/FormPlane.java @@ -134,7 +134,7 @@ public class FormPlane extends JDialog Color colorSimple = JColorChooser.showDialog(null, "Выберите цвет", null); _plane = new DrawingPlane(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - colorSimple); + colorSimple, rnd.nextInt(3)); SetData(); @@ -156,7 +156,7 @@ public class FormPlane extends JDialog Color colorModif = JColorChooser.showDialog(null, "Выберите цвет", null); _plane = new DrawingAirbus(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - colorSimple, colorModif, rnd.nextBoolean(), rnd.nextBoolean()); + colorSimple, rnd.nextInt(3), colorModif, rnd.nextBoolean(), rnd.nextBoolean()); SetData(); diff --git a/Project/src/FormPlaneConfig.form b/Project/src/FormPlaneConfig.form new file mode 100644 index 0000000..e0549e8 --- /dev/null +++ b/Project/src/FormPlaneConfig.form @@ -0,0 +1,417 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Project/src/FormPlaneConfig.java b/Project/src/FormPlaneConfig.java new file mode 100644 index 0000000..6a6cce7 --- /dev/null +++ b/Project/src/FormPlaneConfig.java @@ -0,0 +1,311 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.image.BufferedImage; +import java.util.function.Consumer; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.function.Consumer; + +public class FormPlaneConfig extends JFrame{ + private JButton ButtonAddObject; + private JButton ButtonCancel; + private JPanel GroupBoxConfig; + private JPanel PanelObject; + private JPanel GroupBoxColors; + private JPanel PanelRed; + private JPanel PanelGreen; + private JPanel PanelBlue; + private JPanel PanelYellow; + private JPanel PanelPurple; + private JPanel PanelBlack; + private JPanel PanelGray; + private JPanel PanelWhite; + private JLabel LabelSimpleObject; + private JLabel LabelModifiedObject; + private JLabel LabelBaseColor; + private JLabel LabelAddColor; + private JLabel LabelSpeed; + private JLabel LabelWeight; + private JSpinner NumericUpDownWeight; + private JSpinner NumericUpDownSpeed; + private JCheckBox CheckBoxAddСompartment; + private JCheckBox CheckBoxAddEngine; + private JPanel GroupBoxSelectPlaneWindow; + private JPanel GroubBoxSelectedCountWindow; + private JSpinner NumericUpDownCountWindow; + private JPanel MainPanel; + private JLabel LabelSimpleWindow; + private JLabel LabelRectWindow; + private JLabel LabelTriangleWindow; + private JPanel PictureBoxPlane; + + //переменная-выбранный самолёт + DrawingPlane _plane; + + //событие + Consumer EventAddPlane; + + //объект для контролирования работы DnD + Object dragObject; + + //хранит в себе объект иллюминатор + Object enterObject; + + public void AddEvent(Consumer ev) + { + EventAddPlane = ev; + } + + //конструктор + public FormPlaneConfig() + { + super("Самолёт"); + CreateWindow(); + } + + public void Draw(DrawingPlane _plane) + { + PictureBoxPlane.removeAll(); + BufferedImage bmp = new BufferedImage(250, 200, BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + + gr.setColor(new Color(238, 238, 238)); + gr.fillRect(0, 0, 250, 200); + + 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 void CreateWindow() + { + setPreferredSize(new Dimension(1000, 700)); + getContentPane().add(MainPanel); + + //рамки у Label + LabelAddColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); + LabelBaseColor.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); + LabelModifiedObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); + LabelSimpleObject.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); + LabelSimpleWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); + LabelRectWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); + LabelTriangleWindow.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); + + //настройка значений у NumericUpDown + NumericUpDownSpeed.setModel(new SpinnerNumberModel(1000, 1000, 3000, 1)); + NumericUpDownWeight.setModel(new SpinnerNumberModel(750, 100, 1500, 1)); + NumericUpDownCountWindow.setModel(new SpinnerNumberModel(1, 1, 3, 1)); + + MouseAdapter drag = new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + setCursor(new Cursor(Cursor.HAND_CURSOR)); + } + + @Override + public void mouseReleased(MouseEvent e) { + setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + Drop((JComponent) e.getSource()); + } + }; + + MouseAdapter defCursor = new MouseAdapter() { + @Override + public void mouseExited(MouseEvent e) { + setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + } + }; + + //настройки курсоров + PictureBoxPlane.addMouseListener(defCursor); + LabelBaseColor.addMouseListener(defCursor); + LabelAddColor.addMouseListener(defCursor); + + PanelBlack.addMouseListener(drag); + PanelPurple.addMouseListener(drag); + PanelGray.addMouseListener(drag); + PanelGreen.addMouseListener(drag); + PanelRed.addMouseListener(drag); + PanelWhite.addMouseListener(drag); + PanelYellow.addMouseListener(drag); + PanelBlue.addMouseListener(drag); + + LabelSimpleObject.addMouseListener(drag); + LabelModifiedObject.addMouseListener(drag); + LabelSimpleWindow.addMouseListener(drag); + LabelRectWindow.addMouseListener(drag); + LabelTriangleWindow.addMouseListener(drag); + + //настройка DnD для перетаскивания формы иллюминаторов + AdditWindowDropObject(LabelSimpleWindow); + AdditWindowDropObject(LabelRectWindow); + AdditWindowDropObject(LabelTriangleWindow); + + AdditWindowDropTarget(PictureBoxPlane); + + //передача объекта через событие + ButtonAddObject.addActionListener(e -> { + EventAddPlane.accept(_plane); + dispose(); + }); + + //лямбда-выражение для закрытия формы + ButtonCancel.addActionListener(e -> dispose()); + } + + public void Drop(JComponent dropItem) + { + if(dropItem == null) + { + return; + } + + if(dropItem instanceof JPanel panel) + { + if(_plane == null) + { + return; + } + + if(LabelBaseColor.getMousePosition() != null) + { + _plane.SetColor(panel.getBackground()); + Draw(_plane); + repaint(); + } + + if(LabelAddColor.getMousePosition() != null && _plane instanceof DrawingAirbus airbus) + { + airbus.SetAddColor(panel.getBackground()); + Draw(_plane); + repaint(); + } + } + + if(dropItem instanceof JLabel label && PictureBoxPlane.getMousePosition() != null) + { + int speed = (int)NumericUpDownSpeed.getValue(); + int weight = (int)NumericUpDownWeight.getValue(); + int countWindow = (int)NumericUpDownCountWindow.getValue(); + boolean addCompartment = CheckBoxAddСompartment.isSelected(); + boolean addEngine = CheckBoxAddEngine.isSelected(); + + if(label == LabelSimpleObject) + { + try + { + _plane = new DrawingPlane(speed, weight, Color.WHITE, countWindow); + Draw(_plane); + repaint(); + } + catch(Exception ex) { } + } + else if(label == LabelModifiedObject) + { + try + { + _plane = new DrawingAirbus(speed, weight, Color.WHITE, countWindow, Color.WHITE, addCompartment, addEngine); + Draw(_plane); + repaint(); + } + catch (Exception ex) { }; + } + + if(_plane != null) + { + _plane.SetPosition(PictureBoxPlane.getWidth() - 200, PictureBoxPlane.getHeight() - 150, PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + PictureBoxPlane.add(_plane, BorderLayout.CENTER); + Draw(_plane); + repaint(); + revalidate(); + } + } + } + + void AdditWindowDropTarget(JComponent obj) + { + obj.addMouseListener(new MouseAdapter() { + @Override + public void mouseEntered(MouseEvent e) {super.mouseEntered(e); + Window_DragEnter(obj); + } + @Override + public void mouseExited(MouseEvent e) {super.mouseExited(e); + DragExit(); + } + }); + } + + void AdditWindowDropObject(JComponent obj) + { + obj.addMouseListener(new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) {super.mousePressed(e); + Window_MouseDown(obj); + } + @Override + public void mouseReleased(MouseEvent e) {super.mouseReleased(e); + Window_DragDrop(); + } + }); + } + + void Window_MouseDown(Object sender) + { + IAdditionalDrawingObject windows; + switch (((JLabel)sender).getText()){ + case "Простая": + windows = new DrawingAirplaneWindow(); + break; + case "Треугольная": + windows = new DrawingTriangleAirplaneWindow(); + break; + case "Квадратная": + windows = new DrawingRectAirplaneWindow(); + break; + default: + return; + } + + enterObject = windows; + } + + void Window_DragEnter(Object sender) + { + if(enterObject != null && IAdditionalDrawingObject.class.isAssignableFrom(enterObject.getClass()) && _plane != null) + { + setCursor(new Cursor(Cursor.HAND_CURSOR)); + dragObject = sender; + } + } + + void Window_DragDrop() + { + if(dragObject == null) + { + return; + } + + _plane.SetTypeWindow((int)NumericUpDownCountWindow.getValue(), (IAdditionalDrawingObject)enterObject); + DragExit(); + enterObject = null; + } + + void DragExit() + { + if(enterObject != null && dragObject != null) + { + setCursor(Cursor.getDefaultCursor()); + Draw(_plane); + dragObject = null; + } + } +} diff --git a/Project/src/IAdditionalDrawingObject.java b/Project/src/IAdditionalDrawingObject.java index cf9f27b..7b47141 100644 --- a/Project/src/IAdditionalDrawingObject.java +++ b/Project/src/IAdditionalDrawingObject.java @@ -1,6 +1,6 @@ import java.awt.*; -public interface IAdditionalDrawingObject +public interface IAdditionalDrawingObject { void SetAddEnum(int airplaneWindow); void DrawAirplaneWindow(Color colorAirplaneWindow, Graphics g, float _startPosX, float _startPosY);