diff --git a/App.java b/App.java index f40d8f0..b007428 100644 --- a/App.java +++ b/App.java @@ -7,6 +7,6 @@ import javax.swing.*; public class App { public static void main(String[] args) { - new FormAirport(); + FormMapWithSetAircrafts form = new FormMapWithSetAircrafts(); } } diff --git a/Classes/DrawingAircraft.java b/Classes/DrawingAircraft.java index 9700abb..2e0d10f 100644 --- a/Classes/DrawingAircraft.java +++ b/Classes/DrawingAircraft.java @@ -8,12 +8,11 @@ public class DrawingAircraft public EntityAircraft Plane; private IDrawingEngines _engines; - protected int _startPosX; protected int _startPosY; - private Integer _pictureWidth = null; - private Integer _pictureHeight = null; + public Integer _pictureWidth = null; + public Integer _pictureHeight = null; protected final int _aircraftWidth = 100; protected final int _aircraftHeight = 120; diff --git a/Classes/DrawingObjectAircraft.java b/Classes/DrawingObjectAircraft.java index 975c669..e963fe6 100644 --- a/Classes/DrawingObjectAircraft.java +++ b/Classes/DrawingObjectAircraft.java @@ -3,12 +3,17 @@ package Classes; import java.awt.*; public class DrawingObjectAircraft implements IDrawingObject { - private DrawingAircraft _aircraft = null; + private DrawingAircraft _aircraft; public DrawingObjectAircraft(DrawingAircraft aircraft) { _aircraft = aircraft; } + + public DrawingAircraft getAircraft() + { + return _aircraft; + } @Override public float getStep() { diff --git a/Classes/MapWithSetAircraftsGeneric.java b/Classes/MapWithSetAircraftsGeneric.java index 46586ec..635fa0e 100644 --- a/Classes/MapWithSetAircraftsGeneric.java +++ b/Classes/MapWithSetAircraftsGeneric.java @@ -40,6 +40,11 @@ public class MapWithSetAircraftsGeneric map, int position) + { + return map._setAircrafts.get(position); + } + public Image showSet() { BufferedImage img = new BufferedImage(_pictureWidth,_pictureHeight,BufferedImage.TYPE_INT_ARGB); diff --git a/Classes/MapsCollection.java b/Classes/MapsCollection.java new file mode 100644 index 0000000..17e0fd4 --- /dev/null +++ b/Classes/MapsCollection.java @@ -0,0 +1,47 @@ +package Classes; + +import Classes.Maps.AbstractMap; + +import java.util.*; + +public class MapsCollection +{ + final HashMap> _mapsVault; + + public ArrayList Keys; + + private final int _pictureWidth; + private final int _pictureHeight; + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + _mapsVault = new HashMap>(); + Keys = new ArrayList<>(_mapsVault.keySet()); + } + + public void AddMap(String name, AbstractMap map) + { + + if (!Keys.contains(name)) + { + _mapsVault.put(name, new MapWithSetAircraftsGeneric(_pictureWidth, _pictureHeight, map)); + } + Keys = new ArrayList<>(_mapsVault.keySet()); + } + + public void DeleteMap(String name) + { + if (Keys.contains(name)) + { + _mapsVault.remove(name); + } + Keys = new ArrayList<>(_mapsVault.keySet()); + } + + public MapWithSetAircraftsGeneric getMap(String ind) + { + return _mapsVault.getOrDefault(ind, null); + } +} diff --git a/Classes/SetAircraftsGeneric.java b/Classes/SetAircraftsGeneric.java index e9161ae..bce683a 100644 --- a/Classes/SetAircraftsGeneric.java +++ b/Classes/SetAircraftsGeneric.java @@ -57,7 +57,7 @@ public class SetAircraftsGeneric public T get(int position) { - if (position < _maxCount && position >= 0) + if (position < _maxCount && position >= 0 && position < getCount()) { return _places.get(position); } diff --git a/Form/FormAirFighter.java b/Form/FormAirFighter.java index 4d67bf0..0190ff6 100644 --- a/Form/FormAirFighter.java +++ b/Form/FormAirFighter.java @@ -7,8 +7,6 @@ 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.util.Random; public class FormAirFighter extends JDialog @@ -47,7 +45,7 @@ public class FormAirFighter extends JDialog _aircraft.MoveTransport(Direction.Down); } } - Draw(); + repaint(); } private void SetData() @@ -57,6 +55,7 @@ public class FormAirFighter extends JDialog _aircraft.SetPosition( rnd.nextInt(0, 100), rnd.nextInt(0, 100), + Form.getWidth(), Form.getHeight() ); @@ -64,6 +63,20 @@ public class FormAirFighter extends JDialog weightLabel.setText("Weight: " + _aircraft.Plane.getWeight()); colorLabel.setText("Color: " + _aircraft.Plane.getBodyColor()); } + private void SetData(int width, int height) + { + Random rnd = new Random(); + + _aircraft.SetPosition( + rnd.nextInt(0, 100), + rnd.nextInt(0, 100), + width, + height + ); + speedLabel.setText("Speed: " + _aircraft.Plane.getSpeed()); + weightLabel.setText("Weight: " + _aircraft.Plane.getWeight()); + colorLabel.setText("Color: " + _aircraft.Plane.getBodyColor()); + } private void CreateEntity() { Random rnd = new Random(); @@ -74,7 +87,7 @@ public class FormAirFighter extends JDialog SetData(); - Draw(); + repaint(); } private void CreateModifiedEntity() @@ -87,7 +100,7 @@ public class FormAirFighter extends JDialog _aircraft = new DrawingMilitaryAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color, dopColor, rnd.nextBoolean(), rnd.nextBoolean()); SetData(); - Draw(); + repaint(); } private void selectAircraft() @@ -101,21 +114,13 @@ public class FormAirFighter extends JDialog private void resizeWindow() { _aircraft.ChangeBorders(pictureBox.getWidth(), pictureBox.getHeight()); - Draw(); - } - - private void Draw() { - Graphics2D graphics = (Graphics2D) pictureBox.getGraphics(); - graphics.clearRect(0, 0, pictureBox.getWidth(), pictureBox.getHeight()); - pictureBox.paintComponents(graphics); - _aircraft.DrawTransport(graphics); + repaint(); } public FormAirFighter() { setContentPane(Form); - // action move // ActionListener listener = this::moveButtonClick; moveRight.addActionListener(listener); @@ -123,16 +128,26 @@ public class FormAirFighter extends JDialog moveLeft.addActionListener(listener); moveUp.addActionListener(listener); - btnCreate.addActionListener(e -> CreateEntity()); btnModification.addActionListener(e -> CreateModifiedEntity()); SelectButton.addActionListener(e -> selectAircraft()); - /*Form.addComponentListener(new ComponentAdapter() { - @Override - public void componentResized(ComponentEvent e) { - super.componentResized(e); - resizeWindow(); - } - });*/ } + public FormAirFighter(DrawingAircraft aircraft) + { + this(); + _aircraft = aircraft; + SetData(_aircraft._pictureWidth, aircraft._pictureHeight); + repaint(); + } + @Override + public void paint(Graphics g) { + super.paint(g); + Graphics2D graphics = (Graphics2D) pictureBox.getGraphics(); + if(_aircraft != null) + { + _aircraft.DrawTransport(graphics); + } + + } + } diff --git a/Form/FormAirport.java b/Form/FormAirport.java index aac7fa4..238daf5 100644 --- a/Form/FormAirport.java +++ b/Form/FormAirport.java @@ -44,7 +44,6 @@ public class FormAirport extends JFrame{ public FormAirport() { setContentPane(Form); - Random rnd = new Random(); parts = new EntityWithEngines<>(); int mod = 0; @@ -89,7 +88,6 @@ public class FormAirport extends JFrame{ }); setSize(new Dimension(600, 600)); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } diff --git a/Form/FormMapWithSetAircrafts.form b/Form/FormMapWithSetAircrafts.form index 01a18bc..a3f5e48 100644 --- a/Form/FormMapWithSetAircrafts.form +++ b/Form/FormMapWithSetAircrafts.form @@ -2,7 +2,7 @@
- + @@ -20,7 +20,7 @@ - + @@ -28,7 +28,7 @@ - + @@ -39,7 +39,7 @@ - + @@ -47,7 +47,7 @@ - + @@ -55,7 +55,7 @@ - + @@ -63,7 +63,7 @@ - + @@ -71,7 +71,7 @@ - + @@ -79,7 +79,7 @@ - + @@ -93,7 +93,7 @@ - + @@ -107,7 +107,7 @@ - + @@ -121,7 +121,7 @@ - + @@ -135,7 +135,54 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Form/FormMapWithSetAircrafts.java b/Form/FormMapWithSetAircrafts.java index 6e931c4..820824e 100644 --- a/Form/FormMapWithSetAircrafts.java +++ b/Form/FormMapWithSetAircrafts.java @@ -2,10 +2,11 @@ package Form; import Classes.Direction; import Classes.DrawingObjectAircraft; -import Classes.MapWithSetAircraftsGeneric; +import Classes.IDrawingObject; import Classes.Maps.AbstractMap; import Classes.Maps.NightSkyMap; import Classes.Maps.SimpleMap; +import Classes.MapsCollection; import javax.swing.*; import javax.swing.text.DefaultFormatterFactory; @@ -13,9 +14,11 @@ import javax.swing.text.MaskFormatter; 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.text.ParseException; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Optional; +import java.util.Queue; public class FormMapWithSetAircrafts extends JFrame { @@ -32,45 +35,76 @@ public class FormMapWithSetAircrafts extends JFrame private JButton moveLeft; private JButton moveRight; private JFormattedTextField maskedTextBoxPosition; + private JTextField newMapTextBox; + private JButton AddMapButton; + private JList listBoxMaps; + + DefaultListModel listModel; + private JButton DeleteMapButton; + private JButton showDeletedAircrafts; public Image bufferedImage; - private MapWithSetAircraftsGeneric _mapAircraftsCollectionGeneric; + private final MapsCollection _mapsCollection; + public Queue deletedAircrafts; + + FormAirFighter form; + + private final HashMap _mapsDict = new HashMap<>(){{ + put("1. Simple map", new SimpleMap()); + put("2. NightSky map", new NightSkyMap()); + }}; + + private void RefreshMaps() + { + int index = listBoxMaps.getSelectedIndex(); + + listBoxMaps.removeAll(); + + listModel = new DefaultListModel(); + for (int i = 0; i < _mapsCollection.Keys.size(); i++) + { + listModel.addElement(_mapsCollection.Keys.get(i)); + + } + listBoxMaps.setModel(listModel); + if (listBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= listBoxMaps.getModel().getSize())) + { + listBoxMaps.setSelectedIndex(0); + } + else if (listBoxMaps.getModel().getSize() > 0 && index > -1 && index < listBoxMaps.getModel().getSize()) + { + listBoxMaps.setSelectedIndex(index); + } + } + private void showMap() { - if(_mapAircraftsCollectionGeneric == null) + if(listBoxMaps.getSelectedIndex() == -1) { return; } - bufferedImage = _mapAircraftsCollectionGeneric.ShowOnMap(); + bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).ShowOnMap(); repaint(); } private void indexChanged() { - AbstractMap map = null; - switch (comboBoxMapSelection.getSelectedItem().toString()) { - case "1. Simple map" -> map = new SimpleMap(); - case "2. NightSky map" -> map = new NightSkyMap(); - } - if(map != null) + if(listBoxMaps.getSelectedValue() != null) { - _mapAircraftsCollectionGeneric = new MapWithSetAircraftsGeneric<>(pictureBox.getWidth(),pictureBox.getHeight(),map); - } - else - { - _mapAircraftsCollectionGeneric = null; + bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); + repaint(); } } private void addAircraft() { - if(_mapAircraftsCollectionGeneric == null) + if(listBoxMaps.getSelectedIndex() == -1) { return; } - FormAirFighter form = new FormAirFighter(); + form = new FormAirFighter(); form.setSize(800,500); form.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); form.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); @@ -79,10 +113,10 @@ public class FormMapWithSetAircrafts extends JFrame if(form.getSelectedAircraft() != null) { DrawingObjectAircraft aircraft = new DrawingObjectAircraft(form.SelectedAircraft); - if (_mapAircraftsCollectionGeneric.addAircraft(_mapAircraftsCollectionGeneric,aircraft) != -1) + if (_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addAircraft(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")),aircraft) != -1) { JOptionPane.showMessageDialog(this, "Object was added", "Success", JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapAircraftsCollectionGeneric.showSet(); + bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); repaint(); } else @@ -94,7 +128,12 @@ public class FormMapWithSetAircrafts extends JFrame private void DeleteAircraft() { - if(maskedTextBoxPosition.getText().isEmpty()) + String pos = maskedTextBoxPosition.getText(); + if(listBoxMaps.getSelectedIndex() == -1) + { + return; + } + if(pos.isEmpty() || pos == null ) { return; } @@ -102,12 +141,17 @@ public class FormMapWithSetAircrafts extends JFrame { return; } - int position = Integer.parseInt(maskedTextBoxPosition.getText()); + int position = Integer.parseInt(pos); - if(_mapAircraftsCollectionGeneric.removeAircraft(_mapAircraftsCollectionGeneric,position) != null) + if(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).getAircraft(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")),position) != null) + { + deletedAircrafts.add(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).getAircraft(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")),position)); + } + + if(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).removeAircraft(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")),position) != null) { JOptionPane.showMessageDialog(this,"Object is removed."); - bufferedImage = _mapAircraftsCollectionGeneric.showSet(); + bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); repaint(); } else @@ -118,17 +162,17 @@ public class FormMapWithSetAircrafts extends JFrame private void ShowHangar() { - if(_mapAircraftsCollectionGeneric == null) + if(listBoxMaps.getSelectedIndex() == -1) { return; } - bufferedImage = _mapAircraftsCollectionGeneric.showSet(); + bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); repaint(); } private void moveObject(ActionEvent event) { - if (_mapAircraftsCollectionGeneric == null) + if(listBoxMaps.getSelectedIndex() == -1) { return; } @@ -142,19 +186,53 @@ public class FormMapWithSetAircrafts extends JFrame case "up" -> dir = Direction.Up; case "down" -> dir = Direction.Down; } - bufferedImage = _mapAircraftsCollectionGeneric.MoveObject(dir); + bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).MoveObject(dir); repaint(); } + private void addMap() + { + if (comboBoxMapSelection.getSelectedIndex() == -1 || newMapTextBox.getText() == null || newMapTextBox.getText().equals("")) + { + JOptionPane.showMessageDialog(this, "Not all data is filled", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + if (!_mapsDict.containsKey(comboBoxMapSelection.getSelectedItem())) + { + JOptionPane.showMessageDialog(this, "There is no such map", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + _mapsCollection.AddMap(newMapTextBox.getText(), _mapsDict.get(comboBoxMapSelection.getSelectedItem())); + RefreshMaps(); + } + + private void deleteMap() + { + if (listBoxMaps.getSelectedIndex() == -1) + { + return; + } + String str = String.format("Delete map %s?",listBoxMaps.getSelectedValue().toString()); + if (JOptionPane.showConfirmDialog(this,str,"Deletion",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) + { + _mapsCollection.DeleteMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")); + RefreshMaps(); + } + } public FormMapWithSetAircrafts() { + this.setContentPane(Form); + this.setSize(800,600); + this.setVisible(true); + _mapsCollection = new MapsCollection(pictureBox.getWidth(),pictureBox.getHeight()); try { maskedTextBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##"))); } catch (ParseException e) { e.printStackTrace(); } + deletedAircrafts = new LinkedList(); ActionListener listener = this::moveObject; moveDown.addActionListener(listener); moveUp.addActionListener(listener); @@ -162,10 +240,26 @@ public class FormMapWithSetAircrafts extends JFrame moveLeft.addActionListener(listener); ShowMapButton.addActionListener(e -> showMap()); - comboBoxMapSelection.addActionListener(e -> indexChanged()); + listBoxMaps.addListSelectionListener(e -> indexChanged()); AddAircraftButton.addActionListener(e -> addAircraft()); DeleteAircraftButton.addActionListener(e -> DeleteAircraft()); ShowHangarButton.addActionListener(e -> ShowHangar()); + AddMapButton.addActionListener(e -> addMap()); + DeleteMapButton.addActionListener(e -> deleteMap()); + showDeletedAircrafts.addActionListener(e -> { + if(deletedAircrafts.isEmpty()) + { + JOptionPane.showMessageDialog(this, "There are not any deleted aircrafts", "Error", JOptionPane.ERROR_MESSAGE); + return; + } + DrawingObjectAircraft deletedAircraft = (DrawingObjectAircraft)deletedAircrafts.remove(); + form = new FormAirFighter(deletedAircraft.getAircraft()); + form.setSize(800,500); + form.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + form.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + form.setVisible(true); + }); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g) {