This commit is contained in:
VictoriaPresnyakova 2022-12-23 11:15:23 +04:00
parent defe5faee7
commit d0d0452b67
4 changed files with 105 additions and 25 deletions

View File

@ -1,6 +1,14 @@
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
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 FormMapWithSetBoats extends JFrame { public class FormMapWithSetBoats extends JFrame {

View File

@ -1,5 +1,6 @@
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.LinkedList;
public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends AbstractMap> public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends AbstractMap>
{ {
@ -9,9 +10,12 @@ public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends Abstra
private int _placeSizeHeight = 90; private int _placeSizeHeight = 90;
private SetBoatsGeneric<T> _setBoats; private SetBoatsGeneric<T> _setBoats;
private U _map; private U _map;
private LinkedList<T> _deletedBoats;
public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map) public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map)
{ {
_deletedBoats = new LinkedList<>();
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight; int height = picHeight / _placeSizeHeight;
_setBoats = new SetBoatsGeneric<T>(width * height); _setBoats = new SetBoatsGeneric<T>(width * height);
@ -27,7 +31,9 @@ public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends Abstra
public T removeBoat(int position) public T removeBoat(int position)
{ {
return _setBoats.Remove(position); T boat = _setBoats.Remove(position);
_deletedBoats.offerFirst(boat);
return boat;
} }
public BufferedImage ShowSet() public BufferedImage ShowSet()
@ -129,4 +135,16 @@ public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends Abstra
} }
} }
} }
public T GetSelectedBoat(int ind){
return _setBoats.Get(ind);
}
public T GetBoatsDeleted()
{
if(_deletedBoats.isEmpty())
{
return null;
}
return _deletedBoats.removeFirst();
}
} }

44
MapsCollection.java Normal file
View File

@ -0,0 +1,44 @@
import java.util.ArrayList;
import java.util.HashMap;
public class MapsCollection {
private final HashMap<String,MapWithSetBoatsGeneric<DrawningObjectBoat, 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 MapWithSetBoatsGeneric<>(_pictureWidth,_pictureHeight,Map));
}
}
public void DelMap(String name)
{
_mapStorages.remove(name);
}
public MapWithSetBoatsGeneric<DrawningObjectBoat,AbstractMap> Get(String ind)
{
if(_mapStorages.containsKey(ind))
{
return _mapStorages.get(ind);
}
return null;
}
public DrawningObjectBoat Get(String name, int ind) {
if (_mapStorages.containsKey(name))
{
return _mapStorages.get(name).GetSelectedBoat(ind);
}
return null;
}
}

View File

@ -1,44 +1,54 @@
import java.util.ArrayList;
import java.util.Iterator;
public class SetBoatsGeneric<T> { public class SetBoatsGeneric<T> {
private T[] _places; private ArrayList<T> _places;
private final int _maxCount;
public int Count() { public int Count() {
return _places.length; return _places.size();
} }
public SetBoatsGeneric(int count) { public SetBoatsGeneric(int count) {
_places = (T[]) (new Object[count]); _maxCount=count;
_places = new ArrayList<>();
} }
public int Insert(T boat) { public int Insert(T boat) {
for (int i = 0; i < _places.length; i++) { return Insert(boat, 0);
if (_places[i] == null) {
_places[i] = boat;
return i;
}
}
return -1;
} }
public int Insert(T boat, int position) { public int Insert(T boat, int position) {
int index = position; if (position < 0 || position > Count() || _maxCount == Count()) return -1;
_places.add(position,boat);
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] = boat;
return position; return position;
} }
public T Remove(int position) { public T Remove(int position) {
if (position >= _places.length) return null; if (position >= Count() || position < 0)
T res = _places[position]; {
_places[position] = null; return null;
return res; }
T ship = (T) _places.get(position);
_places.remove(ship);
return ship;
} }
public T Get(int position) { public T Get(int position) {
return _places[position]; if (position >= Count() || position<0)
{
return null;
}
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();
} }
} }