Лёвушкина Анна, ПИбд-21, лаб4 сложная #8

Closed
AnnaLioness wants to merge 4 commits from lab4_compl into lab3_compl
7 changed files with 318 additions and 142 deletions

View File

@ -73,6 +73,7 @@ public class DrawingShip {
_shipHeight = shipHeight; _shipHeight = shipHeight;
_shipWidth = shipWidth; _shipWidth = shipWidth;
EntityShip = new EntityShip(speed, weight, bodyColor); EntityShip = new EntityShip(speed, weight, bodyColor);
} }
public void SetPosition(int x, int y) public void SetPosition(int x, int y)

View File

@ -7,7 +7,6 @@ public class FormContainerShip{
private AbstractStrategy _abstractStrategy; private AbstractStrategy _abstractStrategy;
Canvas canv; Canvas canv;
class Canvas extends JComponent{ class Canvas extends JComponent{
public DrawingShip _drawingShip;
public Canvas(){ public Canvas(){
@ -80,7 +79,6 @@ public class FormContainerShip{
color,960, 560); color,960, 560);
_drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100)); _drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100));
canv._drawingShip = _drawingShip;
Draw(); Draw();
} }
} }
@ -101,13 +99,9 @@ public class FormContainerShip{
color2 = selectedColor2; color2 = selectedColor2;
} }
_drawingShip = new DrawingContainerShip(random.nextInt(100, 300), _drawingShip = new DrawingContainerShip(random.nextInt(100, 300),
random.nextInt(1000, 3000), random.nextInt(1000, 3000),color,color2,random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560);
color,
color2,
random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560);
_drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100)); _drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100));
canv._drawingShip = _drawingShip;
Draw(); Draw();
} }
} }
@ -187,7 +181,6 @@ public class FormContainerShip{
right.addActionListener(actioListener); right.addActionListener(actioListener);
w.setSize (1000, 600); w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null); w.setLayout(null);
canv = new Canvas(); canv = new Canvas();
canv.setBounds(0, 0, 1000, 600); canv.setBounds(0, 0, 1000, 600);
@ -213,18 +206,4 @@ public class FormContainerShip{
w.setVisible (true); w.setVisible (true);
} }
} }
class Canvas extends JComponent{
public DrawingShip _drawingShip;
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingShip == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingShip.DrawShip(g2d);
super.repaint();
}
}

View File

@ -1,44 +1,89 @@
import java.awt.*; import java.awt.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*; import java.awt.event.*;
import java.util.Stack;
public class FormShipCollection { public class FormShipCollection {
class Canvas extends JComponent{ class Canvas extends JComponent{
public ShipGenericCollection<DrawingShip, DrawningObjectShip> _ships;
public Canvas(){} public Canvas(){}
public void paintComponent(Graphics g){ public void paintComponent(Graphics g){
super.paintComponent(g); super.paintComponent(g);
if(_ships.ShowShips() != null){ if (jListStorage.getSelectedIndex() == -1)
g.drawImage(_ships.ShowShips(), 0, 0, this); {
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if(obj.ShowShips() != null){
g.drawImage(obj.ShowShips(), 0, 0, this);
} }
super.repaint(); super.repaint();
} }
} }
Canvas canv; Canvas canv;
static int pictureBoxWidth = 860; static int pictureBoxWidth = 820;
static int pictureBoxHeight = 580; static int pictureBoxHeight = 580;
private ShipGenericCollection<DrawingShip, DrawningObjectShip> _ships; private Stack<DrawingShip> removedShips;
private ShipGenericStorage _storage;
private JList<String> jListStorage;
private DefaultListModel<String> listModel;
private void ReloadObjects()
{
int index = jListStorage.getSelectedIndex();
listModel.clear();
for (String key : _storage.Keys()) {
listModel.addElement(key);
}
if (listModel.size() > 0 && (index == -1 || index >= listModel.size()))
{
jListStorage.setSelectedIndex(0);
}
else if (listModel.size() > 0 && index > -1 && index < listModel.size())
{
jListStorage.setSelectedIndex(index);
}
}
public void Draw(){ public void Draw(){
canv.repaint(); canv.repaint();
} }
FormShipCollection(){ FormShipCollection(){
canv = new Canvas(); canv = new Canvas();
JFrame w = new JFrame("FormShipCollection"); JFrame w = new JFrame("FormShipCollection");
_ships = new ShipGenericCollection<DrawingShip, DrawningObjectShip>(pictureBoxWidth, pictureBoxHeight); _storage = new ShipGenericStorage(pictureBoxWidth, pictureBoxHeight);
canv._ships = _ships; listModel = new DefaultListModel<String>();
jListStorage = new JList<String>(listModel);
JButton ButtonAddShip = new JButton("Добавить кораблик"); JButton ButtonAddShip = new JButton("Добавить кораблик");
JButton ButtonDeleteShip = new JButton("Удалить кораблик"); JButton ButtonDeleteShip = new JButton("Удалить кораблик");
JButton ButtonRefreshCollection = new JButton("Обновить коллекцию"); JButton ButtonRefreshCollection = new JButton("Обновить коллекцию");
JButton forNewForm = new JButton("новая форма"); JButton forNewForm = new JButton("новая форма");
JButton buttonGetRemoved = new JButton("Получить удалённое");
JButton buttonAddSet = new JButton("Добавить коллекцию");
JButton buttonRemoveSet = new JButton("Удалить коллекцию");
JTextField textBoxSetName = new JTextField();
JTextField TextBoxNumber = new JTextField(); JTextField TextBoxNumber = new JTextField();
ButtonAddShip.addActionListener( ButtonAddShip.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
FormContainerShip form = new FormContainerShip(); FormContainerShip form = new FormContainerShip();
form.buttonSelectedShip.addActionListener( form.buttonSelectedShip.addActionListener(
new ActionListener(){ new ActionListener(){
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
if(_ships.Add(form._drawingShip) != -1){ if(obj.Add(form._drawingShip) != -1){
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw(); Draw();
} }
@ -61,17 +106,29 @@ public class FormShipCollection {
} }
} }
); );
removedShips = new Stack<DrawingShip>();
ButtonDeleteShip.addActionListener( ButtonDeleteShip.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{ {
return; return;
} }
int pos = Integer.parseInt(TextBoxNumber.getText()); int pos = Integer.parseInt(TextBoxNumber.getText());
if (_ships.remove(pos) != null) var rem = obj.remove(pos);
if (rem != null)
{ {
removedShips.push(rem);
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw(); Draw();
@ -84,6 +141,20 @@ public class FormShipCollection {
} }
} }
); );
buttonGetRemoved.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (removedShips.empty()){
JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
FormContainerShip form = new FormContainerShip();
form._drawingShip = removedShips.pop();
form._drawingShip.SetPosition(100, 100);
form.Draw();
}
}
);
ButtonRefreshCollection.addActionListener( ButtonRefreshCollection.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
@ -91,21 +162,68 @@ public class FormShipCollection {
} }
} }
); );
buttonAddSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (textBoxSetName.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_storage.AddSet(textBoxSetName.getText());
ReloadObjects();
}
}
);
jListStorage.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e){
Draw();
}
}
);
buttonRemoveSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект " + jListStorage.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
_storage.DelSet(jListStorage.getSelectedValue());
ReloadObjects();
}
}
);
w.setSize (1000, 600); w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null); w.setLayout(null);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
ButtonAddShip.setBounds(pictureBoxWidth, 0, 120, 20); ButtonAddShip.setBounds(pictureBoxWidth, 0, 130, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 50, 120, 20); TextBoxNumber.setBounds(pictureBoxWidth, 50, 130, 20);
ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 120, 20); ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 130, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 120, 120, 20); ButtonRefreshCollection.setBounds(pictureBoxWidth, 130, 120, 20);
forNewForm.setBounds(pictureBoxWidth, 150, 120, 20); forNewForm.setBounds(pictureBoxWidth, 150, 130, 20);
buttonAddSet.setBounds(pictureBoxWidth, 180, 130, 20);
textBoxSetName.setBounds(pictureBoxWidth, 210, 130, 20);
jListStorage.setBounds(pictureBoxWidth, 240, 130, 60);
buttonRemoveSet.setBounds(pictureBoxWidth, 310, 130, 20);
buttonGetRemoved.setBounds(pictureBoxWidth, 340, 130, 20);
w.add(canv); w.add(canv);
w.add(ButtonAddShip); w.add(ButtonAddShip);
w.add(ButtonDeleteShip); w.add(ButtonDeleteShip);
w.add(ButtonRefreshCollection); w.add(ButtonRefreshCollection);
w.add(forNewForm); w.add(forNewForm);
w.add(TextBoxNumber); w.add(TextBoxNumber);
w.add(buttonAddSet);
w.add(textBoxSetName);
w.add(jListStorage);
w.add(buttonRemoveSet);
w.add(buttonGetRemoved);
w.setVisible(true); w.setVisible(true);
} }
} }

View File

@ -1,47 +1,29 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SetGeneric<T extends Object> public class SetGeneric<T extends Object>
{ {
private Object[] _places; private ArrayList<T> _places;
public int Count; public int Count() {return _places.size();};
public int _maxCount;
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new Object[count]; _maxCount = count;
Count = _places.length; _places = new ArrayList<T>(count);
} }
public int Insert(T ship) public int Insert(T ship)
{ {
// TODO вставка в начало набора if(_places.size() >= _maxCount)
int temp = 0;
int k = 0;
for(int j = 0; j < _places.length; j++) {
if (_places[j] != null)
{
k++;
}
}
if (k == _places.length)
{ {
return -1; return -1;
} }
else else
{ {
for (int i = 0; i < _places.length; i++) _places.add(0, ship);
{
if (_places[i] == null)
{
temp = i;
break;
}
}
for (int i = temp; i > 0; i--)
{
_places[i] = _places[i -1];
}
_places[0] = ship;
return 0; return 0;
} }
} }
@ -49,50 +31,31 @@ public class SetGeneric<T extends Object>
public boolean Insert(T ship, int position) public boolean Insert(T ship, int position)
{ {
int temp = 0; if(_places.size() >= _maxCount)
int k = 0;
for (int j = position; j < _places.length; j++)
{
if (_places[j] != null)
{
k++;
}
}
if (position < _places.length && k < _places.length-position)
{
for (int i = position; i < _places.length; i++)
{
if (_places[i] == null)
{
temp = i;
break;
}
}
for (int i = temp; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = ship;
return true;
}
else
{ {
return false; return false;
} }
if(position < 0 || position > _places.size())
{
return false;
}
if(position == _places.size())
{
_places.add(ship);
}
else
{
_places.add(position, ship);
}
return true;
} }
public boolean Remove(int position) public boolean Remove(int position)
{ {
// TODO проверка позиции if(position < _places.size() && _places.size() < _maxCount)
// TODO удаление объекта из массива, присвоив элементу массива
//значение null
if(position < _places.length)
{ {
_places[position] = null; _places.remove(position);
return true; return true;
} }
else else
@ -100,19 +63,49 @@ public class SetGeneric<T extends Object>
return false; return false;
} }
} }
public T Get(int position) public T Get(int position)
{ {
// TODO проверка позиции if(position < _places.size() && position >= 0)
if(position < _places.length)
{ {
return (T)_places[position]; return _places.get(position);
} }
else else
{ {
return null; return null;
} }
} }
public Iterable<T> GetShips(final Integer maxShips) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int currentIndex = 0;
private int count = 0;
@Override
public boolean hasNext() {
return currentIndex < _places.size() && (maxShips == null || count < maxShips);
}
@Override
public T next() {
if (hasNext()) {
count++;
return _places.get(currentIndex++);
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
} }

View File

@ -72,17 +72,16 @@ public class ShipGenericCollection<T extends DrawingShip, U extends IMoveableObj
private void DrawObjects(Graphics2D gr) private void DrawObjects(Graphics2D gr)
{ {
for (int i = 0; i < _collection.Count; i++) int i = 0;
for (T ship: _collection.GetShips(100))
{ {
T t = _collection.Get(i);
if (t != null) if (ship != null)
{ {
t.SetPosition(((_collection.Count -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection.Count - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); ship.SetPosition(((_collection._maxCount -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection._maxCount - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
t.DrawShip(gr); ship.DrawShip(gr);
} }
// TODO получение объекта i++;
// TODO установка позиции
// TODO прорисовка объекта
} }
} }

86
ShipGenericStorage.java Normal file
View File

@ -0,0 +1,86 @@
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class ShipGenericStorage {
HashMap<String, ShipGenericCollection<DrawingShip,
DrawningObjectShip>> _shipStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<String> Keys() {return _shipStorages.keySet().stream().collect(Collectors.toList());}
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public ShipGenericStorage(int pictureWidth, int pictureHeight)
{
_shipStorages = new HashMap<String, ShipGenericCollection<DrawingShip,DrawningObjectShip>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(String name)
{
if (_shipStorages.containsKey(name))
{
return;
}
else
{
_shipStorages.put(name, new ShipGenericCollection<DrawingShip, DrawningObjectShip>(_pictureWidth, _pictureHeight));
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(String name)
{
if (!_shipStorages.containsKey(name))
{
return;
}
else
{
_shipStorages.remove(name);
}
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public ShipGenericCollection<DrawingShip, DrawningObjectShip> get(String ind)
{
if (_shipStorages.containsKey(ind))
{
return _shipStorages.get(ind);
}
else
{
return null;
}
}
public DrawningObjectShip get(String ind1, int ind2){
if (!_shipStorages.containsKey(ind1))
return null;
return _shipStorages.get(ind1).GetU(ind2);
}
}