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

Closed
AnnaLioness wants to merge 4 commits from lab4_compl into lab3_compl
7 changed files with 313 additions and 131 deletions
Showing only changes of commit 947ef24205 - Show all commits

View File

@ -72,6 +72,7 @@ public class DrawingShip {
_pictureHeight = height;
_shipHeight = shipHeight;
_shipWidth = shipWidth;
EntityShip = new EntityShip(speed, weight, bodyColor);
}

View File

@ -21,7 +21,7 @@ public class FormContainerShip{
_drawingShip.DrawShip(g2d);
super.repaint();
}
}
}
public DrawingShip SelectedShip;
public boolean DialogResult = false;
public JButton buttonSelectedShip;
@ -101,10 +101,7 @@ public class FormContainerShip{
color2 = selectedColor2;
}
_drawingShip = new DrawingContainerShip(random.nextInt(100, 300),
random.nextInt(1000, 3000),
color,
color2,
random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560);
random.nextInt(1000, 3000),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));
canv._drawingShip = _drawingShip;
@ -213,18 +210,4 @@ public class FormContainerShip{
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 javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*;
import java.util.Stack;
public class FormShipCollection {
class Canvas extends JComponent{
public ShipGenericCollection<DrawingShip, DrawningObjectShip> _ships;
public Canvas(){}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(_ships.ShowShips() != null){
g.drawImage(_ships.ShowShips(), 0, 0, this);
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if(obj.ShowShips() != null){
g.drawImage(obj.ShowShips(), 0, 0, this);
}
super.repaint();
}
}
Canvas canv;
static int pictureBoxWidth = 860;
static int pictureBoxWidth = 820;
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(){
canv.repaint();
}
FormShipCollection(){
canv = new Canvas();
JFrame w = new JFrame("FormShipCollection");
_ships = new ShipGenericCollection<DrawingShip, DrawningObjectShip>(pictureBoxWidth, pictureBoxHeight);
canv._ships = _ships;
_storage = new ShipGenericStorage(pictureBoxWidth, pictureBoxHeight);
listModel = new DefaultListModel<String>();
jListStorage = new JList<String>(listModel);
JButton ButtonAddShip = new JButton("Добавить кораблик");
JButton ButtonDeleteShip = new JButton("Удалить кораблик");
JButton ButtonRefreshCollection = 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();
ButtonAddShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
FormContainerShip form = new FormContainerShip();
form.buttonSelectedShip.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if(_ships.Add(form._drawingShip) != -1){
if(obj.Add(form._drawingShip) != -1){
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
@ -61,17 +106,29 @@ public class FormShipCollection {
}
}
);
removedShips = new Stack<DrawingShip>();
ButtonDeleteShip.addActionListener(
new ActionListener() {
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)
{
return;
}
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);
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.peek();
removedShips.pop();
form.Draw();
}
}
);
ButtonRefreshCollection.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
@ -91,6 +162,42 @@ 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.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
@ -100,12 +207,23 @@ public class FormShipCollection {
ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 120, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 120, 120, 20);
forNewForm.setBounds(pictureBoxWidth, 150, 120, 20);
buttonAddSet.setBounds(pictureBoxWidth, 180, 120, 20);
textBoxSetName.setBounds(pictureBoxWidth, 210, 120, 20);
jListStorage.setBounds(pictureBoxWidth, 240, 120, 60);
buttonRemoveSet.setBounds(pictureBoxWidth, 310, 120, 20);
buttonGetRemoved.setBounds(pictureBoxWidth, 340, 120, 20);
w.add(canv);
w.add(ButtonAddShip);
w.add(ButtonDeleteShip);
w.add(ButtonRefreshCollection);
w.add(forNewForm);
w.add(TextBoxNumber);
w.add(buttonAddSet);
w.add(textBoxSetName);
w.add(jListStorage);
w.add(buttonRemoveSet);
w.add(buttonGetRemoved);
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>
{
private Object[] _places;
private ArrayList<T> _places;
public int Count;
public int Count() {return _places.size();};
public int _maxCount;
public SetGeneric(int count)
{
_places = new Object[count];
Count = _places.length;
_maxCount = count;
_places = new ArrayList<T>(count);
}
public int Insert(T ship)
{
// TODO вставка в начало набора
int temp = 0;
int k = 0;
for(int j = 0; j < _places.length; j++) {
if (_places[j] != null)
{
k++;
}
}
if (k == _places.length)
if(_places.size() >= _maxCount)
{
return -1;
}
else
{
for (int i = 0; i < _places.length; i++)
{
if (_places[i] == null)
{
temp = i;
break;
}
}
for (int i = temp; i > 0; i--)
{
_places[i] = _places[i -1];
}
_places[0] = ship;
_places.add(0, ship);
return 0;
}
}
@ -49,50 +31,31 @@ public class SetGeneric<T extends Object>
public boolean Insert(T ship, int position)
{
int temp = 0;
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
if(_places.size() >= _maxCount)
{
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)
{
// TODO проверка позиции
// TODO удаление объекта из массива, присвоив элементу массива
//значение null
if(position < _places.length)
if(position < _places.size() && _places.size() < _maxCount)
{
_places[position] = null;
_places.remove(position);
return true;
}
else
@ -100,19 +63,49 @@ public class SetGeneric<T extends Object>
return false;
}
}
public T Get(int position)
{
// TODO проверка позиции
if(position < _places.length)
if(position < _places.size() && position >= 0)
{
return (T)_places[position];
return _places.get(position);
}
else
{
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,14 +72,15 @@ public class ShipGenericCollection<T extends DrawingShip, U extends IMoveableObj
private void DrawObjects(Graphics2D gr)
{
for (int i = 0; i < _collection.Count; i++)
for (T ship: _collection.GetShips(100))
{
T t = _collection.Get(i);
if (t != null)
int i = 0;
if (ship != null)
{
t.SetPosition(((_collection.Count -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection.Count - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
t.DrawShip(gr);
ship.SetPosition(((_collection._maxCount -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection._maxCount - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
ship.DrawShip(gr);
}
i++;
// TODO получение объекта
// 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);
}
}