diff --git a/src/DrawingField.java b/src/DrawingField.java deleted file mode 100644 index 825f30d..0000000 --- a/src/DrawingField.java +++ /dev/null @@ -1,66 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.util.Random; - -public class DrawingField extends JPanel{ - private final FormGasolineTanker Field; - DrawingGasolineTanker _gasolineTanker=null; - public DrawingField(FormGasolineTanker field) { - Field = field; - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - Graphics2D g2 =(Graphics2D)g; - if (_gasolineTanker!=null) - _gasolineTanker.DrawTransport(g2); - else return; - } - public void DirectionButtonAction(Direction side){ - if(_gasolineTanker == null) - return; - _gasolineTanker.MoveTransport(side); - } - private void SetData() { - Random rand=new Random(); - _gasolineTanker.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); - Field.SpeedLabel.setText("Speed: "+_gasolineTanker.getGasolineTanker().getSpeed()); - Field.WeightLabel.setText("Weight: "+_gasolineTanker.getGasolineTanker().getWeight()); - Field.BodyColorLabel.setText("Color: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2)); - } - public void CreateButtonAction(){ - Random rand=new Random(); - Color color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - if(color1==null) - color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - _gasolineTanker=new DrawingGasolineTanker(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3)); - SetData(); - } - public void CreateModifButtonAction(){ - Random rand=new Random(); - Color color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - Color color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - if(color1==null) - color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - if (color2==null) - color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - _gasolineTanker = new DrawingImprovedGasolineTanker(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1, - color2, rand.nextBoolean(), rand.nextBoolean(), rand.nextInt(3)); - SetData(); - } - public void ResizeField(){ - if (_gasolineTanker!=null) - _gasolineTanker.ChangeBorders(getWidth(),getHeight()); - else return; - } - - public void Draw(Graphics2D graphics) { - if (_gasolineTanker!=null) - _gasolineTanker.DrawTransport(graphics); - else return; - } - public DrawingGasolineTanker GetDrawingGasolineTanker() { - return _gasolineTanker; - } -} diff --git a/src/DrawingObjectGasolineTanker.java b/src/DrawingObjectGasolineTanker.java index 7a1c0d7..c637751 100644 --- a/src/DrawingObjectGasolineTanker.java +++ b/src/DrawingObjectGasolineTanker.java @@ -2,7 +2,7 @@ import java.awt.*; public class DrawingObjectGasolineTanker implements IDrawingObject { - private DrawingGasolineTanker _gasolineTanker=null; + private DrawingGasolineTanker _gasolineTanker; public DrawingObjectGasolineTanker(DrawingGasolineTanker gasolineTanker) { @@ -36,4 +36,7 @@ public class DrawingObjectGasolineTanker implements IDrawingObject { return _gasolineTanker.GetCurrentPosition(); return null; } + public DrawingGasolineTanker GetGasolineTanker() { + return _gasolineTanker; + } } diff --git a/src/FormGasolineTanker.java b/src/FormGasolineTanker.java index adba354..eaed5d9 100644 --- a/src/FormGasolineTanker.java +++ b/src/FormGasolineTanker.java @@ -2,11 +2,11 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.util.Random; public class FormGasolineTanker extends JFrame{ private int Width; private int Height; - DrawingField field = new DrawingField(this); DrawingGasolineTanker SelectedGasolineTanker; private JButton ButtonDown; private JButton ButtonRight; @@ -19,18 +19,17 @@ public class FormGasolineTanker extends JFrame{ public JLabel BodyColorLabel; private JPanel PictureBox; private JButton ButtonSelect; + private DrawingGasolineTanker _gasolineTanker=null; public FormGasolineTanker(){ - super("Gasoline Tanker"); + setTitle("Gasoline Tanker"); setContentPane(PictureBox); - setSize(1000,700); - Width = getWidth(); - Height = getHeight(); + setSize(1000,500); + setResizable(false); ShowWindow(); - field.setBounds(0,0,Width,Height); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setVisible(true); } + ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")). getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); ImageIcon spriteDown =new ImageIcon((new ImageIcon("Material\\KeyDown.png")). @@ -39,38 +38,69 @@ public class FormGasolineTanker extends JFrame{ getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); ImageIcon spriteRight =new ImageIcon((new ImageIcon("Material\\KeyRight.png")). getImage().getScaledInstance(10,10,Image.SCALE_SMOOTH)); + + private void SetData() { + Random rand=new Random(); + _gasolineTanker.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); + SpeedLabel.setText("Скорость: "+_gasolineTanker.getGasolineTanker().getSpeed()); + WeightLabel.setText("Вес: "+_gasolineTanker.getGasolineTanker().getWeight()); + BodyColorLabel.setText("Цвет: "+Integer.toHexString(_gasolineTanker.getGasolineTanker().getBodyColor().getRGB()).substring(2)); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + + if (_gasolineTanker != null) { + PictureBox.paintComponents(PictureBox.getGraphics()); + _gasolineTanker.DrawTransport(g); + } + } private void ShowWindow(){ ButtonCreate.addActionListener(e -> { - field.CreateButtonAction(); - ReDraw(); + Random rand=new Random(); + _gasolineTanker=new DrawingGasolineTanker(rand.nextInt(50)+10,rand.nextInt(3000)+20000,new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),rand.nextInt(3)); + SetData(); + repaint(); }); ButtonCreateModif.addActionListener(e -> { - field.CreateModifButtonAction(); - ReDraw(); + Random rand=new Random(); + _gasolineTanker = new DrawingImprovedGasolineTanker(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), + new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3)); + SetData(); + repaint(); }); ButtonUp.setIcon(spriteUp); ButtonUp.addActionListener(e -> { - field.DirectionButtonAction(Direction.Up); - ReDraw(); + if(_gasolineTanker == null) + return; + _gasolineTanker.MoveTransport(Direction.Up); + repaint(); }); ButtonLeft.setIcon(spriteLeft); ButtonLeft.addActionListener(e -> { - field.DirectionButtonAction(Direction.Left); - ReDraw(); + if(_gasolineTanker == null) + return; + _gasolineTanker.MoveTransport(Direction.Left); + repaint();; }); ButtonRight.setIcon(spriteRight); ButtonRight.addActionListener(e -> { - field.DirectionButtonAction(Direction.Right); - ReDraw(); + if(_gasolineTanker == null) + return; + _gasolineTanker.MoveTransport(Direction.Right); + repaint(); }); ButtonDown.setIcon(spriteDown); ButtonDown.addActionListener(e -> { - field.DirectionButtonAction(Direction.Down); - ReDraw(); + if(_gasolineTanker == null) + return; + _gasolineTanker.MoveTransport(Direction.Down); + repaint(); }); ButtonSelect.addActionListener(e -> { - SelectedGasolineTanker=field.GetDrawingGasolineTanker(); + SelectedGasolineTanker=GetDrawingGasolineTanker(); JOptionPane.showMessageDialog(PictureBox, "Gasoline tanker added."); }); @@ -81,18 +111,21 @@ public class FormGasolineTanker extends JFrame{ Width=getWidth(); Height=getHeight(); - field.ResizeField(); - ReDraw(); - field.setBounds(0,0,Width,Height); + if (_gasolineTanker!=null) + _gasolineTanker.ChangeBorders(getWidth(),getHeight()); + else return; + repaint(); + setBounds(0,0,Width,Height); } }); } - private void ReDraw() - { - Graphics2D graphics = (Graphics2D) PictureBox.getGraphics(); - graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); - PictureBox.paintComponents(graphics); - field.Draw(graphics); + public DrawingGasolineTanker GetDrawingGasolineTanker() { + return _gasolineTanker; + } + public void SetDrawingGasolineTanker(DrawingGasolineTanker gasolineTanker) { + _gasolineTanker=gasolineTanker; + SetData(); + repaint(); } } diff --git a/src/FormMapWithSetGasolineTanker.form b/src/FormMapWithSetGasolineTanker.form index 30f7091..3ea9891 100644 --- a/src/FormMapWithSetGasolineTanker.form +++ b/src/FormMapWithSetGasolineTanker.form @@ -3,12 +3,12 @@ <grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> - <xy x="20" y="20" width="668" height="474"/> + <xy x="20" y="20" width="668" height="652"/> </constraints> <properties/> <border type="none"/> <children> - <grid id="42b0" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="9" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <grid id="42b0" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="10" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> <grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> @@ -19,7 +19,7 @@ <grid id="bc845" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> - <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> </constraints> <properties/> <border type="none"/> @@ -31,17 +31,6 @@ </vspacer> </children> </grid> - <component id="52e81" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap"> - <constraints> - <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/> - </constraints> - <properties> - <model> - <item value="Simple map"/> - <item value="Long map"/> - </model> - </properties> - </component> <vspacer id="f144d"> <constraints> <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> @@ -52,7 +41,7 @@ <grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> </constraints> <properties> - <text value="Add"/> + <text value="Add GL"/> </properties> </component> <component id="ceb03" class="javax.swing.JTextField" binding="TextBoxPosition"> @@ -68,7 +57,7 @@ <grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> </constraints> <properties> - <text value="Remove"/> + <text value="Remove GL"/> </properties> </component> <vspacer id="76e51"> @@ -92,6 +81,67 @@ <text value="Map"/> </properties> </component> + <grid id="13e89" binding="MapPanel" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <margin top="0" left="0" bottom="0" right="0"/> + <constraints> + <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> + </constraints> + <properties/> + <border type="bevel-lowered" title="Maps" title-justification="1" title-position="2"/> + <children> + <component id="52e81" class="javax.swing.JComboBox" binding="СomboBoxSelectorMap"> + <constraints> + <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <model> + <item value="Simple map"/> + <item value="Long map"/> + </model> + </properties> + </component> + <component id="db334" class="javax.swing.JTextField" binding="TextFieldMap"> + <constraints> + <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> + <preferred-size width="150" height="-1"/> + </grid> + </constraints> + <properties/> + </component> + <component id="e500c" class="javax.swing.JButton" binding="CreateMapButton"> + <constraints> + <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <text value="Create map"/> + </properties> + </component> + <component id="8b172" class="javax.swing.JButton" binding="DeleteMapButton"> + <constraints> + <grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <text value="Delete map"/> + </properties> + </component> + <component id="10e77" class="javax.swing.JList" binding="ListBoxMaps"> + <constraints> + <grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false"> + <preferred-size width="150" height="50"/> + </grid> + </constraints> + <properties/> + </component> + </children> + </grid> + <component id="fdb6d" class="javax.swing.JButton" binding="ButtonShowDeleted"> + <constraints> + <grid row="9" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <text value="Show deleted GL"/> + </properties> + </component> </children> </grid> <grid id="c9878" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> diff --git a/src/FormMapWithSetGasolineTanker.java b/src/FormMapWithSetGasolineTanker.java index e07db24..dc6d5f1 100644 --- a/src/FormMapWithSetGasolineTanker.java +++ b/src/FormMapWithSetGasolineTanker.java @@ -1,10 +1,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.Objects; +import java.util.HashMap; public class FormMapWithSetGasolineTanker extends JFrame{ private JPanel GroupBoxTools; @@ -20,8 +16,22 @@ public class FormMapWithSetGasolineTanker extends JFrame{ private JPanel MainPanel; private JTextField TextBoxPosition; private JPanel PictureBox; + private JPanel MapPanel; + private JTextField TextFieldMap; + private JButton CreateMapButton; + private JButton DeleteMapButton; + private JButton ButtonShowDeleted; + private JList ListBoxMaps; private Image bufferedImage; + private MapsCollection _mapsCollection; + private final HashMap<String,AbstractMap> _mapsDict=new HashMap<String,AbstractMap>(){ + { + put("Simple map",new SimpleMap()); + put("Long map",new LongMap()); + } + }; + private MapWithSetGasolineTankerGeneric<DrawingObjectGasolineTanker,AbstractMap> _mapGasolineTankerCollectionGeneric; ImageIcon spriteUp =new ImageIcon((new ImageIcon("Material\\KeyUp.png")). @@ -40,6 +50,11 @@ public class FormMapWithSetGasolineTanker extends JFrame{ ShowWindow(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); + _mapsCollection=new MapsCollection(getWidth(),getHeight()); + СomboBoxSelectorMap.removeAllItems(); + for(String elem:_mapsDict.keySet()){ + СomboBoxSelectorMap.addItem(elem); + } } @Override @@ -52,28 +67,79 @@ public class FormMapWithSetGasolineTanker extends JFrame{ } } + private void ReloadMaps() + { + int index = ListBoxMaps.getSelectedIndex(); + ListBoxMaps.setListData(_mapsCollection.Keys().toArray(new String[0])); + + 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); + } + repaint(); + } + private void ShowWindow(){ - ButtonShowOnMap.addActionListener(e -> { - if (_mapGasolineTankerCollectionGeneric == null) + ListBoxMaps.addListSelectionListener(e -> { + if(ListBoxMaps.getSelectedIndex()==-1) + return; + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); + repaint(); + }); + + DeleteMapButton.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() == -1) { return; } - bufferedImage = _mapGasolineTankerCollectionGeneric.ShowOnMap(); + if(JOptionPane.showConfirmDialog(this,"Delete map "+ListBoxMaps.getSelectedValue().toString()+"?", + "Удаление",JOptionPane.YES_NO_OPTION)==0) + { + _mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString()); + ReloadMaps(); + } + }); + + CreateMapButton.addActionListener(e -> { + if (СomboBoxSelectorMap.getSelectedIndex() == -1 || TextFieldMap.getText()==null || TextFieldMap.getText().equals("")) + { + JOptionPane.showMessageDialog(this,"\n" + "Not all data is complete","Error",JOptionPane.ERROR_MESSAGE); + return; + } + if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem())) + { + JOptionPane.showMessageDialog(this,"No such card","Error",JOptionPane.ERROR_MESSAGE); + return; + } + _mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(СomboBoxSelectorMap.getSelectedItem().toString())); + ReloadMaps(); + }); + + ButtonShowOnMap.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex() ==-1) + { + return; + } + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap(); repaint(); }); ButtonShowStorage.addActionListener(e -> { - if (_mapGasolineTankerCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() ==-1) { return; } - bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); repaint(); }); ButtonAddGasolineTanker.addActionListener(e -> { - if (_mapGasolineTankerCollectionGeneric == null) + if (ListBoxMaps.getSelectedIndex() ==-1) { return; } @@ -85,9 +151,9 @@ public class FormMapWithSetGasolineTanker extends JFrame{ if (dialog.getSelectedGasolineTanker()!=null) { DrawingObjectGasolineTanker gasolineTanker = new DrawingObjectGasolineTanker(dialog.getSelectedGasolineTanker()); - if (_mapGasolineTankerCollectionGeneric.plus(gasolineTanker) >= 0) { - JOptionPane.showMessageDialog(this, "\n" + "Object added", "Success", JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); + if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).plus(gasolineTanker)>=0) { + JOptionPane.showMessageDialog(this, "Object added", "Success", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); repaint(); } else { JOptionPane.showMessageDialog(this, "Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE); @@ -97,79 +163,68 @@ public class FormMapWithSetGasolineTanker extends JFrame{ ButtonRemoveGasolineTanker.addActionListener(e -> { String txt=TextBoxPosition.getText(); - if (txt==null||_mapGasolineTankerCollectionGeneric==null||txt.isEmpty()) + if (txt==null||ListBoxMaps.getSelectedIndex() ==-1) { return; } int result = JOptionPane.showConfirmDialog( this, "Delete object?", - "Delete", + "Removal", JOptionPane.YES_NO_CANCEL_OPTION); - if (result == JOptionPane.NO_OPTION) + if (result!=0) { return; } int pos = Integer.parseInt(txt); - if (_mapGasolineTankerCollectionGeneric.minus(pos) !=null) - { - JOptionPane.showMessageDialog(this,"Object added", "Success",JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapGasolineTankerCollectionGeneric.ShowSet(); + if(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).minus(pos)!=null){ + JOptionPane.showMessageDialog(this, "Object removed", "Success", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet(); repaint(); } else { - JOptionPane.showMessageDialog(this,"Failed to add object", "Error",JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this,"Failed to delete object","\n" + "Error",JOptionPane.INFORMATION_MESSAGE); } }); - СomboBoxSelectorMap.addActionListener(e -> { - AbstractMap map = null; - - switch (СomboBoxSelectorMap.getSelectedItem().toString()) - { - case "Simple map": - map = new SimpleMap(); - break; - case "Long map": - map = new LongMap(); - break; - } - if (map != null) - { - _mapGasolineTankerCollectionGeneric = new MapWithSetGasolineTankerGeneric<>( - PictureBox.getWidth(), PictureBox.getHeight(), map); - } - else - { - _mapGasolineTankerCollectionGeneric = null; + ButtonShowDeleted.addActionListener(e -> { + if (ListBoxMaps.getSelectedIndex()==-1) + return; + DrawingObjectGasolineTanker gasolineTanker=(DrawingObjectGasolineTanker)_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetGasolineTankerInDeleted(); + if(gasolineTanker!=null){ + FormGasolineTanker dialog=new FormGasolineTanker(); + dialog.SetDrawingGasolineTanker(gasolineTanker.GetGasolineTanker()); + dialog.setSize(1200,700); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); } }); ButtonUp.setIcon(spriteUp); ButtonUp.addActionListener(e -> { - if (_mapGasolineTankerCollectionGeneric != null) { - bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Up); + if (ListBoxMaps.getSelectedIndex() !=-1) { + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up); repaint(); } }); ButtonDown.setIcon(spriteDown); ButtonDown.addActionListener(e -> { - if (_mapGasolineTankerCollectionGeneric != null) { - bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Down); + if (ListBoxMaps.getSelectedIndex() !=-1) { + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down); repaint(); } }); ButtonRight.setIcon(spriteRight); ButtonRight.addActionListener(e -> { - if (_mapGasolineTankerCollectionGeneric != null) { - bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Right); + if (ListBoxMaps.getSelectedIndex() !=-1) { + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right); repaint(); } }); ButtonLeft.setIcon(spriteLeft); ButtonLeft.addActionListener(e -> { - if (_mapGasolineTankerCollectionGeneric != null) { - bufferedImage = _mapGasolineTankerCollectionGeneric.MoveObject(Direction.Left); + if (ListBoxMaps.getSelectedIndex() !=-1) { + bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left); repaint(); } }); diff --git a/src/MapWithSetGasolineTankerGeneric.java b/src/MapWithSetGasolineTankerGeneric.java index effb49a..cf714fd 100644 --- a/src/MapWithSetGasolineTankerGeneric.java +++ b/src/MapWithSetGasolineTankerGeneric.java @@ -1,5 +1,6 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.util.ArrayDeque; public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends AbstractMap>{ private final int _pictureWidth; @@ -7,6 +8,7 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends private final int _placeSizeWidth = 210; private final int _placeSizeHeight = 90; private final SetGasolineTankerGeneric<T> _setGasolineTanker; + private final ArrayDeque<T> DeletedGasolineTanker; private final U _map; public MapWithSetGasolineTankerGeneric(int picWidth,int picHeight, U map) @@ -17,6 +19,7 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; + DeletedGasolineTanker = new ArrayDeque<>(); } public int plus(T gasolineTanker) @@ -26,7 +29,9 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends public T minus(int position) { - return _setGasolineTanker.Remove(position); + T gasolineTanker=_setGasolineTanker.Remove(position); + DeletedGasolineTanker.push(gasolineTanker); + return gasolineTanker; } public Image ShowSet() @@ -41,13 +46,9 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends public Image ShowOnMap() { Shaking(); - for (int i = 0; i < _setGasolineTanker.Count(); i++) + for (IDrawingObject gasolineTanker : _setGasolineTanker) { - T gasolineTanker = _setGasolineTanker.Get(i); - if (gasolineTanker != null) - { - return _map.CreateMap(_pictureWidth, _pictureHeight, gasolineTanker); - } + return _map.CreateMap(_pictureWidth, _pictureHeight, gasolineTanker); } return new BufferedImage(_pictureWidth, _pictureHeight,1); } @@ -122,4 +123,13 @@ public class MapWithSetGasolineTankerGeneric <T extends IDrawingObject,U extends } } } + public T GetGasolineTankerArrayDeque(int i){ + return _setGasolineTanker.Get(i); + } + + public T GetGasolineTankerInDeleted() { + if(DeletedGasolineTanker.isEmpty()) + return null; + return DeletedGasolineTanker.pop(); + } } diff --git a/src/SetGasolineTankerGeneric.java b/src/SetGasolineTankerGeneric.java index 1e55731..0fb6725 100644 --- a/src/SetGasolineTankerGeneric.java +++ b/src/SetGasolineTankerGeneric.java @@ -1,68 +1,58 @@ -public class SetGasolineTankerGeneric <T extends Object>{ - private final Object[] _places; - +import java.util.ArrayList; +import java.util.Iterator; +public class SetGasolineTankerGeneric <T extends Object> implements Iterable<T>{ + private final ArrayList<T> _places; public int Count() { - return _places.length; + return _places.size(); } - + private final int _maxCount; public SetGasolineTankerGeneric(int count) { - _places = new Object[count]; + _maxCount=count; + _places = new ArrayList<>(); } public int Insert(T gasolineTanker) { - return Insert(gasolineTanker,0); + if (_places.size()+1>=_maxCount) + return -1; + _places.add(0,gasolineTanker); + return 0; } public int Insert(T gasolineTanker, int position) { - int EmptyEl=-1; - - if (position>=Count() || position < 0) + if (position>=_maxCount||position<0) return -1; - - if (_places[position] == null) - { - _places[position] = gasolineTanker; - return 1; - } - - else if (_places[position] != null) - { - for (int i = position + 1; i < Count(); i++) - if (_places[i] == null) - { - EmptyEl = i; - break; - } - - if (EmptyEl == -1) - return -1; - - for (int i = EmptyEl; i > position; i--) - _places[i] = _places[i - 1]; - } - - _places[position] = gasolineTanker; - return 1; + if (_places.size()+1>=_maxCount) + return -1; + _places.add(position,gasolineTanker); + return position; } public T Remove(int position) { - if (position >= Count() || position < 0 || _places[position]==null) + if (position>=_maxCount||position<0) return null; - - T deleted =(T)_places[position]; - _places[position] = null; + T deleted=_places.get(position); + _places.remove(position); return deleted; } public T Get(int position) { - if (position >= Count() || position < 0) + if (position>=_maxCount||position<0) return null; + return _places.get(position); + } - return (T)_places[position]; + public void Set(int position,T gasolineTanker){ + if (position >= _maxCount||position<0) + return; + Insert(gasolineTanker,position); + } + @Override + public Iterator<T> iterator(){ + return _places.iterator(); } }