From dcdcdaaa20f2c6450a4f888c05fc47a48600a4ba Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Tue, 29 Nov 2022 16:50:40 +0300 Subject: [PATCH 1/2] lab 5 full --- DrawingAircraft.java | 15 ++ DrawingEngines.java | 5 + DrawingModernAircraft.java | 5 + DrawingTruncatedEngines.java | 5 + DrawingWavyEngines.java | 5 + FormAircraftConfig.form | 478 +++++++++++++++++++++++++++++++++++ FormAircraftConfig.java | 171 +++++++++++++ FormMapWithSetAircrafts.java | 29 ++- IDrawingEngines.java | 1 + 9 files changed, 713 insertions(+), 1 deletion(-) create mode 100644 FormAircraftConfig.form create mode 100644 FormAircraftConfig.java diff --git a/DrawingAircraft.java b/DrawingAircraft.java index a007097..e55b23b 100644 --- a/DrawingAircraft.java +++ b/DrawingAircraft.java @@ -51,6 +51,21 @@ class DrawingAircraft _airFighterHeight = airFighterHeight; } + public void setEngines(IDrawingEngines engines) { + drawingEngines = engines; + } + + public IDrawingEngines getEngines() { + return drawingEngines; + } + + public void setColor(Color color) { + AirFighter.BodyColor = color; + } + + public Color getColor() { + return AirFighter.BodyColor; + } public void SetPosition(int x, int y, int width, int height) { diff --git a/DrawingEngines.java b/DrawingEngines.java index 8e186ad..c52e809 100644 --- a/DrawingEngines.java +++ b/DrawingEngines.java @@ -16,6 +16,11 @@ public class DrawingEngines implements IDrawingEngines { else enginesCount = EnginesCount.Four; } + @Override + public void setColor(Color color) { + this.color = color; + } + public void draw(Graphics2D g, int startPosX, int startPosY) { g.setPaint(color); g.fillOval(startPosX + 80, startPosY + 10, 30, 15); diff --git a/DrawingModernAircraft.java b/DrawingModernAircraft.java index 09d50c6..d52ec04 100644 --- a/DrawingModernAircraft.java +++ b/DrawingModernAircraft.java @@ -12,6 +12,11 @@ public class DrawingModernAircraft extends DrawingAircraft super(speed, weight, bodyColor, 195, 166); AirFighter = new EntityModernAircraft(speed, weight, bodyColor, dopColor, dopWings, rockets); } + + public void setDopColor(Color color) { + ((EntityModernAircraft)AirFighter).DopColor = color; + } + @Override public void DrawTransport(Graphics2D g) { diff --git a/DrawingTruncatedEngines.java b/DrawingTruncatedEngines.java index b63c02f..a0ee8cb 100644 --- a/DrawingTruncatedEngines.java +++ b/DrawingTruncatedEngines.java @@ -16,6 +16,11 @@ public class DrawingTruncatedEngines implements IDrawingEngines { else enginesCount = EnginesCount.Four; } + @Override + public void setColor(Color color) { + this.color = color; + } + public void draw(Graphics2D g, int startPosX, int startPosY) { g.setPaint(color); g.fillArc(startPosX + 90, startPosY + 10, 30, 15, 90, 180); diff --git a/DrawingWavyEngines.java b/DrawingWavyEngines.java index cae2701..ff84219 100644 --- a/DrawingWavyEngines.java +++ b/DrawingWavyEngines.java @@ -16,6 +16,11 @@ public class DrawingWavyEngines implements IDrawingEngines { else enginesCount = EnginesCount.Four; } + @Override + public void setColor(Color color) { + this.color = color; + } + private void drawEngine(Graphics2D g, int x, int y) { g.setColor(color); g.fillRect(x, y, 21, 10); diff --git a/FormAircraftConfig.form b/FormAircraftConfig.form new file mode 100644 index 0000000..efd3811 --- /dev/null +++ b/FormAircraftConfig.form @@ -0,0 +1,478 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FormAircraftConfig.java b/FormAircraftConfig.java new file mode 100644 index 0000000..db32445 --- /dev/null +++ b/FormAircraftConfig.java @@ -0,0 +1,171 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.function.Consumer; + +public class FormAircraftConfig implements Form { + private JPanel mainPane; + private JSpinner spinnerSpeed; + private JSpinner spinnerWeight; + private JPanel colorsPane; + private JCheckBox checkboxDopWings; + private JCheckBox checkboxRockets; + private JSpinner spinnerEnginesCount; + private JButton buttonAdd; + private JButton buttonCancel; + private JPanel panelRed; + private JPanel panelYellow; + private JPanel panelGreen; + private JPanel panelBlue; + private JPanel panelWhite; + private JPanel panelGray; + private JPanel panelBlack; + private JPanel panelPurple; + private JPanel panelBaseColor; + private JPanel panelDopColor; + private JPanel panelDraw; + private JLabel labelBase; + private JLabel labelAdvanced; + private JLabel labelTruncatedEngine; + private JLabel labelWavyEngine; + private JLabel labelBaseEngine; + + private JFrame jframe = getFrame(); + private DrawingAircraft aircraft; + private boolean isMousePressed = false; + private Canvas canv = new Canvas(this); + private ArrayList> listeners = new ArrayList<>(); + + private JFrame getFrame() { + JFrame frame = new JFrame(); + frame.setVisible(true); + frame.setBounds(300, 100, 800, 500); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + return frame; + } + + public void addListener(Consumer l) { + listeners.add(l); + } + + public void run() { + jframe.add(mainPane); + panelDraw.add(canv); + + buttonAdd.addActionListener(e -> { + jframe.dispose(); + for(var l : listeners) l.accept(aircraft); + }); + + buttonCancel.addActionListener(e -> jframe.dispose()); + + MouseAdapter mouseHandler = new MouseAdapter() { + @Override + public void mouseEntered(MouseEvent e) { + super.mouseEntered(e); + jframe.setCursor(new Cursor(Cursor.HAND_CURSOR)); + } + + @Override + public void mouseExited(MouseEvent e) { + super.mouseExited(e); + if(!isMousePressed) jframe.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + } + + @Override + public void mousePressed(MouseEvent e) { + super.mousePressed(e); + isMousePressed = true; + jframe.setCursor(new Cursor(Cursor.HAND_CURSOR)); + } + + @Override + public void mouseReleased(MouseEvent e) { + super.mouseReleased(e); + isMousePressed = false; + jframe.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); + drop((JComponent) e.getSource()); + canv.repaint(); + } + }; + + spinnerSpeed.setModel(new SpinnerNumberModel(100, 70, 200, 1)); + spinnerWeight.setModel(new SpinnerNumberModel(700, 600, 1500, 1)); + spinnerEnginesCount.setModel(new SpinnerNumberModel(4, 2, 6, 2)); + + panelRed.addMouseListener(mouseHandler); + panelYellow.addMouseListener(mouseHandler); + panelGreen.addMouseListener(mouseHandler); + panelBlue.addMouseListener(mouseHandler); + panelWhite.addMouseListener(mouseHandler); + panelGray.addMouseListener(mouseHandler); + panelBlack.addMouseListener(mouseHandler); + panelPurple.addMouseListener(mouseHandler); + + labelBase.addMouseListener(mouseHandler); + labelAdvanced.addMouseListener(mouseHandler); + + labelTruncatedEngine.addMouseListener(mouseHandler); + labelWavyEngine.addMouseListener(mouseHandler); + labelBaseEngine.addMouseListener(mouseHandler); + } + + private void drop(JComponent elem) { + if(elem instanceof JPanel panel) { + if(aircraft == null) return; + if(panelBaseColor.getMousePosition() != null) { + aircraft.setColor(panel.getBackground()); + aircraft.getEngines().setColor(panel.getBackground()); + } + + if(panelDopColor.getMousePosition() != null && aircraft instanceof DrawingModernAircraft advanced) { + advanced.setDopColor(panel.getBackground()); + } + } + + Point test = panelDraw.getMousePosition(); + + if(elem instanceof JLabel label && test != null) { + int speed = (int)spinnerSpeed.getValue(); + int weight = (int)spinnerWeight.getValue(); + int enginesCount = (int)spinnerEnginesCount.getValue(); + + if(label == labelBase) { + aircraft = new DrawingAircraft(speed, weight, Color.WHITE); + aircraft.SetPosition(20, 40, canv.getWidth(), canv.getHeight()); + + aircraft.getEngines().setCount(enginesCount); + aircraft.getEngines().setColor(Color.WHITE); + } + + if(label == labelAdvanced) { + aircraft = new DrawingModernAircraft(speed, weight, Color.WHITE, Color.BLACK, + checkboxDopWings.isSelected(), checkboxRockets.isSelected()); + + aircraft.SetPosition(10, 10, canv.getWidth(), canv.getHeight()); + aircraft.getEngines().setCount(enginesCount); + aircraft.getEngines().setColor(Color.WHITE); + } + + if(aircraft == null) return; + + if(label == labelBaseEngine) { + aircraft.setEngines(new DrawingEngines(enginesCount, aircraft.getColor())); + } + if(label == labelTruncatedEngine) { + aircraft.setEngines(new DrawingTruncatedEngines(enginesCount, aircraft.getColor())); + } + if(label == labelWavyEngine) { + aircraft.setEngines(new DrawingWavyEngines(enginesCount, aircraft.getColor())); + } + } + } + + @Override + public void Draw(Graphics2D g) { + if(aircraft == null) return; + aircraft.DrawTransport(g); + } +} diff --git a/FormMapWithSetAircrafts.java b/FormMapWithSetAircrafts.java index 1ed93a5..6f5ec15 100644 --- a/FormMapWithSetAircrafts.java +++ b/FormMapWithSetAircrafts.java @@ -108,7 +108,7 @@ public class FormMapWithSetAircrafts implements Form { } }); - buttonAddAircraft.addActionListener(e -> { + /*buttonAddAircraft.addActionListener(e -> { if (listBoxMaps.getSelectedIndex() == -1) { return; @@ -135,6 +135,33 @@ public class FormMapWithSetAircrafts implements Form { { JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект"); } + });*/ + + + buttonAddAircraft.addActionListener(e -> { + if (listBoxMaps.getSelectedIndex() == -1) + { + return; + } + + FormAircraftConfig formConfig = new FormAircraftConfig(); + formConfig.run(); + + formConfig.addListener(drawingAircraft -> { + if(drawingAircraft == null) return; + + DrawingObjectAircraft aircraft = new DrawingObjectAircraft(drawingAircraft); + if (_mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).addAircraft(aircraft) != -1) + { + JOptionPane.showMessageDialog(jFrame, "Объект добавлен"); + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet(); + canv.repaint(); + } + else + { + JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект"); + } + }); }); buttonRemoveAircraft.addActionListener(e -> { diff --git a/IDrawingEngines.java b/IDrawingEngines.java index 04c08e6..27685c6 100644 --- a/IDrawingEngines.java +++ b/IDrawingEngines.java @@ -2,5 +2,6 @@ import java.awt.*; public interface IDrawingEngines { void setCount(int count); + void setColor(Color color); void draw(Graphics2D g, int x, int y); } -- 2.25.1 From 015b2df6647d99375c26aa50f91092337ed15554 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Tue, 29 Nov 2022 17:03:05 +0300 Subject: [PATCH 2/2] refactoring --- FormMapWithSetAircrafts.java | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/FormMapWithSetAircrafts.java b/FormMapWithSetAircrafts.java index 6f5ec15..48d25f3 100644 --- a/FormMapWithSetAircrafts.java +++ b/FormMapWithSetAircrafts.java @@ -108,36 +108,6 @@ public class FormMapWithSetAircrafts implements Form { } }); - /*buttonAddAircraft.addActionListener(e -> { - if (listBoxMaps.getSelectedIndex() == -1) - { - return; - } - - FormAircraft dialog = new FormAircraft(); - dialog.run(); - dialog.setSize(800, 500); - dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - - dialog.setVisible(true); - - if (dialog.getSelectedAircraft() == null) return; - - DrawingObjectAircraft aircraft = new DrawingObjectAircraft(dialog.getSelectedAircraft()); - if (_mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).addAircraft(aircraft) != -1) - { - JOptionPane.showMessageDialog(jFrame, "Объект добавлен"); - img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet(); - canv.repaint(); - } - else - { - JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект"); - } - });*/ - - buttonAddAircraft.addActionListener(e -> { if (listBoxMaps.getSelectedIndex() == -1) { -- 2.25.1