Markov D.P. LabWork04 #14

Closed
khehelk wants to merge 1 commits from LabWork04 into LabWork03
7 changed files with 312 additions and 116 deletions

View File

@ -13,6 +13,9 @@ public class DrawingObjectShip implements IDrawingObject {
}
return 0;
}
public DrawingShip GetDrawingObjectShip() {
return _ship;
}
@Override
public void SetObject(int x, int y, int width, int height) {
_ship.SetPosition(x,y,width,height);

View File

@ -1,16 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetShipsGeneric">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<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="649" height="400"/>
<xy x="20" y="20" width="798" height="526"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="9bb5a" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="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="0" column="0" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="600" height="400"/>
<preferred-size width="600" height="400"/>
<maximum-size width="600" height="400"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
@ -18,32 +22,20 @@
</grid>
<hspacer id="b305e">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="4fa91" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="8" column-count="3" 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="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="1" column="1" row-span="3" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="95ba3" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Карта острова"/>
<item value="Карта скалы"/>
</model>
</properties>
</component>
<component id="cdb80" class="javax.swing.JButton" binding="ButtonAddShip">
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить корабль"/>
@ -51,7 +43,7 @@
</component>
<component id="e5494" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
@ -59,7 +51,7 @@
</component>
<component id="8bb89" class="javax.swing.JButton" binding="ButtonDeleteShip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить корабль"/>
@ -67,7 +59,7 @@
</component>
<component id="aa482" class="javax.swing.JButton" binding="ButtonShowStorage">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Показать хранилище"/>
@ -75,7 +67,7 @@
</component>
<component id="9ebdc" class="javax.swing.JButton" binding="ButtonShowMap">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Показать карту"/>
@ -129,6 +121,78 @@
<text value=""/>
</properties>
</component>
<component id="6734a" class="javax.swing.JButton" binding="ButtonCheckDel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удаленные"/>
</properties>
</component>
</children>
</grid>
<grid id="d087c" binding="GroupBoxMaps" layout-manager="GridLayoutManager" row-count="7" 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="1" 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"/>
<children>
<component id="124b6" class="javax.swing.JTextField" binding="textBoxNewMapName">
<constraints>
<grid row="1" 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>
<vspacer id="3ed0f">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="95ba3" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="2" 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="Простая карта"/>
<item value="Карта острова"/>
<item value="Карта скалы"/>
</model>
</properties>
</component>
<component id="fb777" class="javax.swing.JButton" binding="ButtonAddMap">
<constraints>
<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="Добавить карту"/>
</properties>
</component>
<component id="a358a" class="javax.swing.JButton" binding="ButtonDeleteMap">
<constraints>
<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="Удалить карту"/>
</properties>
</component>
<vspacer id="64e67">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="e221" class="javax.swing.JList" binding="ListBoxMaps" custom-create="true">
<constraints>
<grid row="4" 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>
</children>

View File

@ -6,7 +6,9 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FormMapWithSetShipsGeneric extends JFrame{
public JPanel Mainpanel;
private JPanel pictureBoxShip;
@ -21,7 +23,22 @@ public class FormMapWithSetShipsGeneric extends JFrame{
private JButton ButtonUp;
private JButton ButtonRight;
private JTextField maskedTextBoxPosition;
private JTextField textBoxNewMapName;
private int picWidth=600;
private int picHeight=400;
private JButton ButtonAddMap;
private JList ListBoxMaps;
private JButton ButtonDeleteMap;
private JPanel GroupBoxMaps;
private JButton ButtonCheckDel;
private MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap> _mapShipsCollectionGeneric;
private final HashMap<String,AbstractMap> _mapsDict = new HashMap<>(){{
put("Простая карта",new SimpleMap());
put("Карта острова",new IslandsMap());
put("Карта скалы",new RocksMap());
}};
private final MapsCollection _mapsCollection;
public void UpdateWindow(BufferedImage bmp)
{
pictureBoxShip.removeAll();
@ -32,8 +49,30 @@ public class FormMapWithSetShipsGeneric extends JFrame{
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
}
private void ReloadMaps(){
int index = ListBoxMaps.getSelectedIndex();
DefaultListModel<String> model1 = (DefaultListModel<String>) ListBoxMaps.getModel();
model1.removeAllElements();
for(int i=0;i<_mapsCollection.Keys().size();i++)
{
model1.addElement(_mapsCollection.Keys().get(i));
}
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);
}
}
public FormMapWithSetShipsGeneric()
{
_mapsCollection = new MapsCollection(picWidth, picHeight);
ComboBoxSelectorMap.removeAllItems();
for (String elem : _mapsDict.keySet()) {
ComboBoxSelectorMap.addItem(elem);
}
try {
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
ButtonUp.setIcon(new ImageIcon(img));
@ -47,51 +86,22 @@ public class FormMapWithSetShipsGeneric extends JFrame{
System.out.println(ex);
}
ComboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AbstractMap map = null;
ComboBoxSelectorMap = (JComboBox)e.getSource();
String item = (String)ComboBoxSelectorMap.getSelectedItem();
switch (item) {
case "Простая карта": {
map = new SimpleMap();
break;
}
case "Карта острова": {
map = new IslandsMap();
break;
}
case "Карта скалы": {
map = new RocksMap();
break;
}
}
if(map!=null)
{
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap>(pictureBoxShip.getWidth(),pictureBoxShip.getHeight(),map);
}
else
{
_mapShipsCollectionGeneric=null;
}
}
});
ButtonAddShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapShipsCollectionGeneric==null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
FormShip form = new FormShip();
form.setSize(1000,700);
form.setVisible(true);
form.setModal(true);
DrawingObjectShip ship = new DrawingObjectShip(form.GetSelectedShip());
if(_mapShipsCollectionGeneric.Add(ship)!=-1)
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Add(ship)!=-1)
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
else
{
@ -99,7 +109,14 @@ public class FormMapWithSetShipsGeneric extends JFrame{
}
}
});
ListBoxMaps.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (ListBoxMaps.getSelectedIndex() == -1)
return;
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
});
ButtonDeleteShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@ -113,10 +130,10 @@ public class FormMapWithSetShipsGeneric extends JFrame{
return;
}
int pos=Integer.parseInt(maskedTextBoxPosition.getText());
if(_mapShipsCollectionGeneric.Delete(pos)!=null)
if(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).Delete(pos)!=null)
{
JOptionPane.showMessageDialog(null, "Объект удален");
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
else
{
@ -127,27 +144,27 @@ public class FormMapWithSetShipsGeneric extends JFrame{
ButtonShowStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
});
ButtonShowMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
UpdateWindow(_mapShipsCollectionGeneric.ShowOnMap());
UpdateWindow(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).ShowOnMap());
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@ -155,7 +172,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Up)));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
@ -164,7 +181,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@ -172,7 +189,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Down)));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
@ -181,7 +198,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@ -189,7 +206,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Right)));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
@ -198,7 +215,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
@ -206,7 +223,7 @@ public class FormMapWithSetShipsGeneric extends JFrame{
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Left)));
imageWithMapAndObject.setIcon(new ImageIcon(_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
@ -222,5 +239,58 @@ public class FormMapWithSetShipsGeneric extends JFrame{
}
}
});
ButtonAddMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (ComboBoxSelectorMap.getSelectedIndex() == -1 || textBoxNewMapName.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
if(!_mapsDict.containsKey(ComboBoxSelectorMap.getSelectedItem()))
{
JOptionPane.showMessageDialog(null,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE);
}
_mapsCollection.AddMap(textBoxNewMapName.getText(),_mapsDict.get(ComboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
}
});
ButtonDeleteMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
if(JOptionPane.showConfirmDialog(null,"Удалить карту"+ListBoxMaps.getSelectedValue().toString()+"?","Удаление",JOptionPane.YES_NO_OPTION)==0)
{
_mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
ReloadMaps();
}
}
});
ButtonCheckDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
DrawingObjectShip ship=_mapsCollection.Get(ListBoxMaps.getSelectedValue().toString()).GetShipsDeleted();
if(ship==null)
{
JOptionPane.showMessageDialog(null,"Коллекция пуста","Ошибка",JOptionPane.ERROR_MESSAGE);
return;
}
FormShip formShip=new FormShip(ship);
formShip.setSize(1000,800);
formShip.setVisible(true);
}
});
}
private void createUIComponents() {
DefaultListModel<String> dlm = new DefaultListModel<String>();
ListBoxMaps = new JList(dlm);
}
}

View File

@ -44,8 +44,21 @@ public class FormShip extends JDialog{
}
validate();
}
public FormShip(DrawingObjectShip ship)
{
super(new Frame("Корабль"));
CreateWindow();
setModal(true);
_ship=ship.GetDrawingObjectShip();
getContentPane().add(Mainpanel);
}
public FormShip() {
super(new Frame("Корабль"));
CreateWindow();
setModal(true);
getContentPane().add(Mainpanel);
}
public void CreateWindow(){
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
@ -143,7 +156,5 @@ public class FormShip extends JDialog{
dispose();
}
});
setModal(true);
getContentPane().add(Mainpanel);
}
}

View File

@ -1,5 +1,6 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Stack;
public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends AbstractMap> {
private final int _pictureWidth;
@ -8,8 +9,10 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
private final int _placeSizeHeight = 90;
private final SetShipGeneric<T> _setShips;
private final U _map;
private Stack<T> _deletedShips;
public MapWithSetShipsGeneric(int picWidth, int picHeight, U map)
{
_deletedShips=new Stack<>();
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setShips = new SetShipGeneric<T>(width * height);
@ -23,7 +26,9 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
}
public T Delete(int position)
{
return _setShips.Remove(position);
T ship=_setShips.Remove(position);
_deletedShips.push(ship);
return ship;
}
public BufferedImage ShowSet()
{
@ -36,16 +41,23 @@ public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends Abstrac
public BufferedImage ShowOnMap()
{
Shaking();
for (int i = 0; i < _setShips.Count(); i++)
for (var ship : _setShips)
{
var ship = _setShips.Get(i);
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
return _map.CreateMap(_pictureWidth,_pictureHeight,ship);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
}
public T GetSelectedShip(int ind){
return _setShips.Get(ind);
}
public T GetShipsDeleted()
{
if(_deletedShips.isEmpty())
{
return null;
}
return _deletedShips.pop();
}
public BufferedImage MoveObject(Direction direction)
{
if (_map != null)

45
MapsCollection.java Normal file
View File

@ -0,0 +1,45 @@
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.HashMap;
public class MapsCollection {
private final HashMap<String,MapWithSetShipsGeneric<DrawingObjectShip, AbstractMap>> _mapStorages;
public ArrayList<String> Keys()
{
return new ArrayList<>(_mapStorages.keySet());
}
private final int _pictureWidth;
private final int _pictureHeight;
public MapsCollection(int pictureWidth,int pictureHeight)
{
_mapStorages=new HashMap<>();
_pictureWidth=pictureWidth;
_pictureHeight=pictureHeight;
}
public void AddMap(String Name, AbstractMap Map)
{
if(!_mapStorages.containsKey(Name))
{
_mapStorages.put(Name,new MapWithSetShipsGeneric<>(_pictureWidth,_pictureHeight,Map));
}
}
public void DelMap(String name)
{
_mapStorages.remove(name);
}
public MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap> Get(String ind)
{
if(_mapStorages.containsKey(ind))
{
return _mapStorages.get(ind);
}
return null;
}
public DrawingObjectShip Get(String name, int ind) {
if (_mapStorages.containsKey(name))
{
return _mapStorages.get(name).GetSelectedShip(ind);
}
return null;
}
}

View File

@ -1,12 +1,17 @@
public class SetShipGeneric<T extends Object> {
private final Object[] _places;
import java.util.ArrayList;
import java.util.Iterator;
public class SetShipGeneric<T extends Object> implements Iterable<T>{
private ArrayList<T> _places;
private final int _maxCount;
//private final Object[] _places;
public int Count()
{
return _places.length;
return _places.size();
}
public SetShipGeneric(int count)
{
_places = new Object[count];
_maxCount=count;
_places = new ArrayList<>();
}
public int Insert(T ship)
{
@ -14,52 +19,38 @@ public class SetShipGeneric<T extends Object> {
}
public int Insert(T ship, int position)
{
if (position >= _places.length || position < 0)
{
return -1;
}
if (_places[position] == null)
{
_places[position] = ship;
return position;
}
int emptyIndex = -1;
for (int i = position + 1; i < Count(); i++)
{
if (_places[i] == null)
{
emptyIndex = i;
break;
}
}
if (emptyIndex < 0)
{
return -1;
}
for (int i = emptyIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = ship;
if (position < 0 || position > Count() || _maxCount == Count()) return -1;
_places.add(position,ship);
return position;
}
public T Remove(int position)
{
if (position >= _places.length || position < 0)
if (position >= Count() || position < 0)
{
return null;
}
T ship = (T) _places[position];
_places[position] = null;
T ship = (T) _places.get(position);
_places.remove(ship);
return ship;
}
public T Get(int position)
{
if (position >= _places.length || position < 0)
if (position >= Count() || position<0)
{
return null;
}
return (T)_places[position];
return _places.get(position);
}
public void Set(int position,T value)
{
if (position < _maxCount || position >= 0)
{
Insert(value,position);
}
}
@Override
public Iterator<T> iterator() {
return _places.iterator();
}
}