From 8d9e0c9eb81f96db4206a5a12418c9d92876f9e5 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Tue, 15 Nov 2022 17:20:34 +0300 Subject: [PATCH] lab 3 full --- AircraftMixer.java | 50 +++++++++ Canvas.java | 1 + DrawingAircraft.java | 5 + DrawingModernAircraft.java | 5 + EnginesFabric.java | 17 ++++ FormAircraft.form | 28 +++-- FormAircraft.java | 63 +++++++++--- FormMapWithSetAircrafts.form | 163 +++++++++++++++++++++++++++++ FormMapWithSetAircrafts.java | 175 ++++++++++++++++++++++++++++++++ Main.java | 2 +- MapWithSetAircraftsGeneric.java | 153 ++++++++++++++++++++++++++++ SetAircraftsGeneric.java | 50 +++++++++ formAircraftGenerator.form | 29 ++++++ formAircraftGenerator.java | 76 ++++++++++++++ 14 files changed, 794 insertions(+), 23 deletions(-) create mode 100644 AircraftMixer.java create mode 100644 EnginesFabric.java create mode 100644 FormMapWithSetAircrafts.form create mode 100644 FormMapWithSetAircrafts.java create mode 100644 MapWithSetAircraftsGeneric.java create mode 100644 SetAircraftsGeneric.java create mode 100644 formAircraftGenerator.form create mode 100644 formAircraftGenerator.java diff --git a/AircraftMixer.java b/AircraftMixer.java new file mode 100644 index 0000000..7583cae --- /dev/null +++ b/AircraftMixer.java @@ -0,0 +1,50 @@ +import java.util.Random; + +public class AircraftMixer { + private T[] aircrafts; + private U[] engines; + + public AircraftMixer(int countAircrafts, int countEngines) { + aircrafts = (T[])(new EntityAircraft[countAircrafts]); + engines = (U[])(new IDrawingEngines[countEngines]); + } + + public int add(T a) { + int index = 0; + while(index < aircrafts.length && aircrafts[index] != null) index++; + + if(index == aircrafts.length) return -1; + + aircrafts[index] = a; + return index; + } + + public int add(U e) { + int index = 0; + while(index < engines.length && engines[index] != null) index++; + + if(index == engines.length) return -1; + + engines[index] = e; + return index; + } + + public DrawingAircraft constructAircraft(int width, int height) { + Random rnd = new Random(); + + DrawingAircraft air; + + T selectedAircraft = aircrafts[rnd.nextInt(0, aircrafts.length)]; + U selectedEngines = engines[rnd.nextInt(0, engines.length)]; + + DrawingAircraft result; + + if(selectedAircraft instanceof EntityModernAircraft) { + result = new DrawingModernAircraft(selectedAircraft, selectedEngines); + } + else result = new DrawingAircraft(selectedAircraft, selectedEngines); + + result.SetPosition(10, 10, width, height); + return result; + } +} diff --git a/Canvas.java b/Canvas.java index 6421b3a..689b502 100644 --- a/Canvas.java +++ b/Canvas.java @@ -10,6 +10,7 @@ public class Canvas extends JComponent { @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; + form.Draw(g2); } } diff --git a/DrawingAircraft.java b/DrawingAircraft.java index b3625a3..a007097 100644 --- a/DrawingAircraft.java +++ b/DrawingAircraft.java @@ -38,6 +38,11 @@ class DrawingAircraft peekRandomEngines(bodyColor); } + public DrawingAircraft(EntityAircraft airFighter, IDrawingEngines engines) + { + AirFighter = airFighter; + this.drawingEngines = engines; + } public DrawingAircraft(int speed, float weight, Color bodyColor, int airFighterWidth, int airFighterHeight) { diff --git a/DrawingModernAircraft.java b/DrawingModernAircraft.java index a5de0e0..09d50c6 100644 --- a/DrawingModernAircraft.java +++ b/DrawingModernAircraft.java @@ -2,6 +2,11 @@ import java.awt.*; public class DrawingModernAircraft extends DrawingAircraft { + public DrawingModernAircraft(EntityAircraft airFighter, IDrawingEngines engines) + { + super(airFighter, engines); + } + public DrawingModernAircraft(int speed, float weight, Color bodyColor, Color dopColor, boolean dopWings, boolean rockets) { super(speed, weight, bodyColor, 195, 166); diff --git a/EnginesFabric.java b/EnginesFabric.java new file mode 100644 index 0000000..9fc1e4f --- /dev/null +++ b/EnginesFabric.java @@ -0,0 +1,17 @@ +import java.awt.*; +import java.util.Random; + +public class EnginesFabric { + public static IDrawingEngines createRandom(Color color) { + Random rnd = new Random(); + + int type = rnd.nextInt(0, 3); + + return switch(type) { + case 0 -> new DrawingEngines(rnd.nextInt(1, 7), color); + case 1 -> new DrawingWavyEngines(rnd.nextInt(1, 7), color); + case 2 -> new DrawingTruncatedEngines(rnd.nextInt(1, 7), color); + default -> null; + }; + } +} diff --git a/FormAircraft.form b/FormAircraft.form index ee165ee..78248a5 100644 --- a/FormAircraft.form +++ b/FormAircraft.form @@ -52,7 +52,7 @@ - + @@ -62,7 +62,7 @@ - + @@ -75,7 +75,7 @@ - + @@ -88,7 +88,7 @@ - + @@ -101,7 +101,7 @@ - + @@ -114,7 +114,7 @@ - + @@ -125,6 +125,22 @@ + + + + + + + + + + + + + + + + diff --git a/FormAircraft.java b/FormAircraft.java index 451ccc7..e8a527e 100644 --- a/FormAircraft.java +++ b/FormAircraft.java @@ -1,11 +1,10 @@ import javax.swing.*; -import javax.swing.border.LineBorder; import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.Random; -public class FormAircraft implements Form { +public class FormAircraft extends JDialog implements Form { private JButton createButton; private JButton upButton; private JButton rightButton; @@ -16,22 +15,19 @@ public class FormAircraft implements Form { private JLabel speedLabel; private JLabel weightLabel; private JLabel colorLabel; + private JButton createModifButton; + private JButton selectButton; DrawingAircraft _airFighter; + private DrawingAircraft selectedAircraft; - - private JFrame jframe = getFrame(); - - private JFrame getFrame() { - JFrame frame = new JFrame(); - frame.setVisible(true); - frame.setBounds(300, 100, 800, 600); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - return frame; + public DrawingAircraft getSelectedAircraft() { + return selectedAircraft; } + public void run() { - jframe.add(mainPanel); + add(mainPanel); Canvas canv = new Canvas(this); DrawPlace.add(canv); @@ -40,8 +36,11 @@ public class FormAircraft implements Form { Dimension canvSize = canv.getSize(); Random rnd = new Random(); - _airFighter = new DrawingAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); + Color color = JColorChooser.showDialog(this, "Цвет", Color.BLACK); + + if(color == null) color = Color.BLACK; + + _airFighter = new DrawingAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color); _airFighter.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height); @@ -55,12 +54,44 @@ public class FormAircraft implements Form { canv.repaint(); }); - jframe.addComponentListener(new ComponentAdapter() { + createModifButton.addActionListener(e -> { + + Dimension canvSize = canv.getSize(); + Random rnd = new Random(); + + Color color = JColorChooser.showDialog(this, "Цвет", Color.BLACK); + Color dopColor = JColorChooser.showDialog(this, "Цвет", Color.BLACK); + + if(color == null) color = Color.BLACK; + if(dopColor == null) dopColor = Color.BLACK; + + _airFighter = new DrawingModernAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), + color, dopColor, + rnd.nextInt(0, 2) == 1, rnd.nextInt(0, 2) == 1); + + _airFighter.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height); + + Color bodyColor = _airFighter.AirFighter.BodyColor; + String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")"; + + speedLabel.setText("Скорость: " + _airFighter.AirFighter.Speed + " "); + weightLabel.setText("Вес: " + _airFighter.AirFighter.Weight + " "); + colorLabel.setText("Цвет: " + colorString); + + canv.repaint(); + }); + + selectButton.addActionListener(e -> { + selectedAircraft = _airFighter; + dispose(); + }); + + addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { if(_airFighter == null) return; _airFighter.ChangeBorders(canv.getSize().width, canv.getSize().height); - jframe.revalidate(); + revalidate(); } }); diff --git a/FormMapWithSetAircrafts.form b/FormMapWithSetAircrafts.form new file mode 100644 index 0000000..bfb4848 --- /dev/null +++ b/FormMapWithSetAircrafts.form @@ -0,0 +1,163 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormMapWithSetAircrafts.java b/FormMapWithSetAircrafts.java new file mode 100644 index 0000000..29546ae --- /dev/null +++ b/FormMapWithSetAircrafts.java @@ -0,0 +1,175 @@ +import javax.swing.*; +import javax.swing.text.DefaultFormatterFactory; +import javax.swing.text.MaskFormatter; +import java.awt.*; +import java.text.ParseException; +import java.util.Objects; + +public class FormMapWithSetAircrafts implements Form { + private JPanel MainPane; + private JButton buttonAdd; + private JComboBox comboBoxSelectorMap; + private JTextField textBoxPosition; + private JButton buttonRemove; + private JButton buttonShowStorage; + private JButton buttonShowOnMap; + private JButton upButton; + private JButton leftButton; + private JButton rightButton; + private JButton downButton; + private JPanel drawPanel; + + + private MapWithSetAircraftsGeneric _mapCarsCollectionGeneric; + private Canvas canv = new Canvas(this); + + JFrame jFrame = getFrame(); + Image img; + + private JFrame getFrame() { + JFrame frame = new JFrame(); + frame.setVisible(true); + frame.setBounds(300, 100, 800, 600); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + return frame; + } + + + public void run() { + jFrame.add(MainPane); + drawPanel.add(canv); + + comboBoxSelectorMap.setSelectedIndex(-1); + + comboBoxSelectorMap.addActionListener(e -> { + AbstractMap map = null; + switch (comboBoxSelectorMap.getSelectedItem().toString()) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Моя карта": + map = new MyMap(); + break; + } + if (map != null) + { + Dimension canvSize = canv.getSize(); + _mapCarsCollectionGeneric = new MapWithSetAircraftsGeneric( + canvSize.width, canvSize.height, map); + } + else + { + _mapCarsCollectionGeneric = null; + } + }); + + buttonAdd.addActionListener(e -> { + if (_mapCarsCollectionGeneric == null) 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 (_mapCarsCollectionGeneric.addAircraft(aircraft) != -1) + { + JOptionPane.showMessageDialog(jFrame, "Объект добавлен"); + img = _mapCarsCollectionGeneric.ShowSet(); + canv.repaint(); + } + else + { + JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект"); + } + }); + + buttonRemove.addActionListener(e -> { + if(_mapCarsCollectionGeneric == null) return; + + String text = textBoxPosition.getText(); + if(text.isEmpty()) return; + + if(JOptionPane.showConfirmDialog( + jFrame, + "Вы действительно хотите удалить объект?", + "Удаление", + JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return; + + int pos; + try { + pos = Integer.parseInt(text); + } catch (Exception err) { + return; + } + pos = Integer.parseInt(text); + + if(_mapCarsCollectionGeneric.removeAircraft(pos) != null) { + JOptionPane.showMessageDialog(jFrame, "Объект удален"); + img = _mapCarsCollectionGeneric.ShowSet(); + canv.repaint(); + } else { + JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект"); + } + }); + + buttonShowStorage.addActionListener(e -> { + if (_mapCarsCollectionGeneric == null) + { + return; + } + + img = _mapCarsCollectionGeneric.ShowSet(); + canv.repaint(); + }); + + buttonShowOnMap.addActionListener(e -> { + if (_mapCarsCollectionGeneric == null) + { + return; + } + + img = _mapCarsCollectionGeneric.ShowOnMap(); + canv.repaint(); + }); + + leftButton.addActionListener(e -> { + if(_mapCarsCollectionGeneric == null) return; + img = _mapCarsCollectionGeneric.MoveObject(Direction.Left); + canv.repaint(); + }); + + rightButton.addActionListener(e -> { + if(_mapCarsCollectionGeneric == null) return; + img = _mapCarsCollectionGeneric.MoveObject(Direction.Right); + canv.repaint(); + }); + + upButton.addActionListener(e -> { + if(_mapCarsCollectionGeneric == null) return; + img = _mapCarsCollectionGeneric.MoveObject(Direction.Up); + canv.repaint(); + }); + + downButton.addActionListener(e -> { + + if(_mapCarsCollectionGeneric == null) return; + img = _mapCarsCollectionGeneric.MoveObject(Direction.Down); + canv.repaint(); + }); + + + } + + @Override + public void Draw(Graphics2D g) { + if(img == null) return; + g.drawImage(img, 0, 0, null); + } +} diff --git a/Main.java b/Main.java index 195c2fb..de98287 100644 --- a/Main.java +++ b/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - new formMap().run(); + new formAircraftGenerator().run(); } } diff --git a/MapWithSetAircraftsGeneric.java b/MapWithSetAircraftsGeneric.java new file mode 100644 index 0000000..64f4533 --- /dev/null +++ b/MapWithSetAircraftsGeneric.java @@ -0,0 +1,153 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetAircraftsGeneric +{ + private int _pictureWidth; + private int _pictureHeight; + private int _placeSizeWidth = 210; + private int _placeSizeHeight = 170; + private SetAircraftsGeneric _setCars; + private U _map; + + public MapWithSetAircraftsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setCars = new SetAircraftsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int addAircraft(T aircraft) + { + return _setCars.Insert(aircraft); + } + + public T removeAircraft(int position) + { + return _setCars.Remove(position); + } + + public Image ShowSet() + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D gr = (Graphics2D) img.getGraphics(); + + DrawBackground(gr); + DrawCars(gr); + + return img; + } + + public Image ShowOnMap() + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = (Graphics2D) img.getGraphics(); + + Shaking(); + for (int i = 0; i < _setCars.getCount(); i++) + { + var car = _setCars.Get(i); + if (car != null) + { + _map.CreateMap(_pictureWidth, _pictureHeight, car); + _map.DrawMapWithObject(g); + return img; + } + } + + return img; + } + + public Image MoveObject(Direction direction) + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = (Graphics2D) img.getGraphics(); + + if (_map != null) + { + _map.MoveObject(direction); + _map.DrawMapWithObject(g); + } + return img; + } + + private void Shaking() + { + int j = _setCars.getCount() - 1; + for (int i = 0; i < _setCars.getCount(); i++) + { + if (_setCars.Get(i) == null) + { + for (; j > i; j--) + { + var car = _setCars.Get(j); + if (car != null) + { + _setCars.Insert(car, i); + _setCars.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics2D g) + { + Polygon angar = new Polygon(); + + + angar.addPoint(0, _pictureHeight ); + angar.addPoint(0, _pictureHeight / 4 ); + angar.addPoint(_pictureWidth / 2 , 9); + angar.addPoint(_pictureWidth, _pictureHeight / 4 ); + angar.addPoint(_pictureWidth, _pictureHeight ); + + g.setPaint(new Color(211, 136, 84)); + g.fillPolygon(angar); + + g.setPaint(new Color(160, 160, 160)); + g.fillRect(_pictureWidth / 6, (_pictureHeight * 5) / 12, (_pictureWidth * 2) / 3, (_pictureHeight * 7) / 12); + + g.setPaint(Color.black); + g.setStroke(new BasicStroke(3)); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * + _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.drawLine( i * _placeSizeWidth, 0, i * _placeSizeWidth, + (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + + g.setStroke(new BasicStroke(1)); + } + + private void DrawCars(Graphics2D g) + { + int width = _pictureWidth / _placeSizeWidth; + + for (int i = 0; i < _setCars.getCount(); i++) + { + int x = i % width; + int y = i / width; + + T current =_setCars.Get(i); + + if(current == null) continue; + + current.SetObject(x * _placeSizeWidth, y * _placeSizeHeight, _pictureWidth, _pictureHeight); + current.DrawningObject(g); + } + } +} diff --git a/SetAircraftsGeneric.java b/SetAircraftsGeneric.java new file mode 100644 index 0000000..99e8af1 --- /dev/null +++ b/SetAircraftsGeneric.java @@ -0,0 +1,50 @@ +public class SetAircraftsGeneric +{ + private T[] _places; + + public int getCount() { + return _places.length; + } + + public SetAircraftsGeneric(int count) + { + _places = (T[])(new Object[count]); + } + public int Insert(T car) + { + for(int i = 0; i < _places.length; i++) + { + if (_places[i] == null) + { + _places[i] = car; + return i; + } + } + return -1; + } + public int Insert(T car, int position) + { + int index = position; + + while (_places[index] != null && index < _places.length) index++; + + if (index == _places.length) return -1; + for (int i = index; i > position; --i) _places[i] = _places[i - 1]; + + _places[position] = car; + return position; + } + + public T Remove(int position) + { + if(position >= _places.length) return null; + T res = _places[position]; + _places[position] = null; + return res; + } + + public T Get(int position) + { + return _places[position]; + } +} diff --git a/formAircraftGenerator.form b/formAircraftGenerator.form new file mode 100644 index 0000000..bd0df4a --- /dev/null +++ b/formAircraftGenerator.form @@ -0,0 +1,29 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/formAircraftGenerator.java b/formAircraftGenerator.java new file mode 100644 index 0000000..18d1544 --- /dev/null +++ b/formAircraftGenerator.java @@ -0,0 +1,76 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class formAircraftGenerator implements Form { + private JButton generateButton; + private JPanel drawPanel; + private JPanel mainPanel; + private Canvas canv = new Canvas(this); + + private DrawingAircraft aircraft; + private AircraftMixer mixer = new AircraftMixer<>(10, 20); + private JFrame jframe = getFrame(); + + private JFrame getFrame() { + JFrame frame = new JFrame(); + frame.setVisible(true); + frame.setBounds(300, 100, 400, 300); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + return frame; + } + + public void run() { + jframe.add(mainPanel); + drawPanel.add(canv); + + Random rnd = new Random(); + + for(int i = 0; i < 10; ++i) { + if(rnd.nextBoolean()) { + mixer.add(new EntityAircraft(rnd.nextInt(100, 250), rnd.nextInt(1000, 2000), + new Color( + rnd.nextInt(0, 255), + rnd.nextInt(0, 255), + rnd.nextInt(0, 255) + ))); + continue; + } + + mixer.add(new EntityModernAircraft(rnd.nextInt(100, 250), rnd.nextInt(1000, 2000), + new Color( + rnd.nextInt(0, 255), + rnd.nextInt(0, 255), + rnd.nextInt(0, 255) + ), + new Color( + rnd.nextInt(0, 255), + rnd.nextInt(0, 255), + rnd.nextInt(0, 255) + ), + rnd.nextBoolean(), rnd.nextBoolean() + )); + } + + for(int i = 0; i < 20; ++i) { + Color randomColor = new Color( + rnd.nextInt(0, 255), + rnd.nextInt(0, 255), + rnd.nextInt(0, 255)); + + mixer.add(EnginesFabric.createRandom(randomColor)); + } + + generateButton.addActionListener(e -> { + Dimension canvSize = canv.getSize(); + aircraft = mixer.constructAircraft(canvSize.width, canvSize.height); + canv.repaint(); + }); + } + + @Override + public void Draw(Graphics2D g) { + if(aircraft == null) return; + aircraft.DrawTransport(g); + } +} -- 2.25.1