ПИбд-23 Кислица Егор Дмитриевич Лабораторная работа №4 (усложненная) #4

Closed
Egor wants to merge 2 commits from Laba4 into Laba3
6 changed files with 217 additions and 48 deletions
Showing only changes of commit ceee7cf028 - Show all commits

View File

@ -35,7 +35,12 @@ public class FormAirFighter {
return;
canv.repaint();
}
public void ChangeAirplane(DrawningAirplane newAirplane){
newAirplane.SetPosition(0,0);
DrawningAirplane = newAirplane;
canv.DrawningAirplane = DrawningAirplane;
}
public FormAirFighter(){
AirplaneFrame =new JFrame ();
JButton buttonCreate = new JButton("Создать");

View File

@ -3,16 +3,21 @@ package src;
import src.DrawningObjects.DrawningAirplane;
import src.Generics.AirplaneGenericCollection;
import src.FormAirFighter;
import src.Generics.AirplaneGenericStorage;
import src.Generics.AirplaneTrashCollection;
import src.MovementStrategy.DrawningObjectAirplane;
import src.MovementStrategy.IMoveableObject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class FormAirFighterCollection {
private final AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes;
private final AirplaneGenericStorage _storage;
private JList<String> listBoxStorages;
private DefaultListModel<String> listBoxModel;
private int pictureBoxWidth = 902;
private int pictureBoxHeight = 453;
@ -23,11 +28,32 @@ public class FormAirFighterCollection {
return;
canv.repaint();
}
private void ReloadObjects(){
int index = listBoxStorages.getSelectedIndex();
listBoxModel.clear();
List<String> keys = _storage.Keys();
for(int i = 0; i < keys.size(); i++){
listBoxModel.addElement(keys.get(i));
}
if(listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size()))
listBoxStorages.setSelectedIndex(0);
else if(listBoxModel.size() > 0)
listBoxStorages.setSelectedIndex(index);
}
public FormAirFighterCollection(){
_airplanes = new AirplaneGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
AirplaneTrashCollection<DrawningAirplane> _trashCollection = new AirplaneTrashCollection<>();
JButton callTrashButton = new JButton("мусор");
_storage = new AirplaneGenericStorage(pictureBoxWidth, pictureBoxHeight);
JScrollPane scrollPane = new JScrollPane();
canv = new CollectionCanvas();
JPanel toolBox = new JPanel();
JTextField storageName = new JTextField();
JButton addStorageButton = new JButton("Добавить набор");
listBoxModel = new DefaultListModel<>();
listBoxStorages= new JList<>(listBoxModel);
scrollPane.setViewportView(listBoxStorages);
JButton delStorageButton = new JButton("Удалить набор");
toolBox.setBounds(623,12, 227, 80);
JFrame collectionFrame = new JFrame();
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880,497);
@ -37,27 +63,67 @@ public class FormAirFighterCollection {
JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить");
JTextField airplaneNumb = new JTextField();
GridLayout lay = new GridLayout(4,1);
GridLayout lay = new GridLayout(9,1);
toolBox.add(storageName);
toolBox.add(addStorageButton);
toolBox.add(scrollPane);
toolBox.add(delStorageButton);
toolBox.setLayout(lay);
toolBox.add(addButton);
toolBox.add(airplaneNumb);
toolBox.add(removeButton);
toolBox.add(refreshButton);
toolBox.add(callTrashButton);
collectionFrame.add(toolBox);
collectionFrame.add(canv);
collectionFrame.setVisible(true);
canv._storage = _storage;
canv.listBoxStorages = listBoxStorages;
addStorageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(storageName.getText() == null)
return;
_storage.AddSet(storageName.getText());
ReloadObjects();
}
});
delStorageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStorages.getSelectedIndex() == -1) {
return;
}
_storage.DelSet(listBoxStorages.getSelectedValue());
ReloadObjects();
}
});
callTrashButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_trashCollection.GetSize() == 0)
return;
FormAirFighter form = new FormAirFighter();
form.ChangeAirplane(_trashCollection.GetTop());
_trashCollection.Pop();
}
});
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_airplanes == null) {
if(listBoxStorages.getSelectedIndex() == -1) {
return;
}
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue());
FormAirFighter form = new FormAirFighter();
form.buttonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_airplanes.Insert(form.SelectedAirplane()) != -1)
if (_airplanes.Insert(form.SelectedAirplane()))
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form.SelectedAirplane()._pictureWidth = pictureBoxWidth;
@ -68,7 +134,7 @@ public class FormAirFighterCollection {
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
canv._airplanes = _airplanes;
form.AirplaneFrame.dispose();
Draw();
}
@ -78,6 +144,10 @@ public class FormAirFighterCollection {
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStorages.getSelectedIndex() == -1) {
return;
}
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue());
if(_airplanes == null) {
return;
}
@ -91,6 +161,8 @@ public class FormAirFighterCollection {
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
DrawningAirplane curAirplane = _airplanes.Get(numb);
_trashCollection.Push(curAirplane);
_airplanes.Remove(numb);
_airplanes.ShowAirplanes();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
@ -101,6 +173,10 @@ public class FormAirFighterCollection {
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStorages.getSelectedIndex() == -1) {
return;
}
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue());
if(_airplanes == null) {
return;
}
@ -113,18 +189,19 @@ public class FormAirFighterCollection {
}
class CollectionCanvas extends JComponent {
public AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes;
public AirplaneGenericStorage _storage;
public JList<String> listBoxStorages;
public CollectionCanvas(){
}
@Override
public void paintComponent (Graphics g){
if (_airplanes == null){
if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(_airplanes.ShowAirplanes(), 0, 0, this);
g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowAirplanes(), 0, 0, this);
super.repaint();
}
}

View File

@ -25,10 +25,13 @@ public class AirplaneGenericCollection<T extends DrawningAirplane, U extends IMo
_collection = new SetGeneric<T>(width * height);
}
public int Insert(T obj){
public int Size(){
return _collection.Count;
}
public boolean Insert(T obj){
if (obj == null)
{
return -1;
return false;
}
return _collection.Insert(obj);
}
@ -42,7 +45,11 @@ public class AirplaneGenericCollection<T extends DrawningAirplane, U extends IMo
return null;
return (U)ans.GetMoveableObject();
}
public T Get(int position){
if(position < 0 || position >= _collection.Count)
return null;
return _collection.Get(position);
}
private void DrawBackground(Graphics g)
{
g.setColor(Color.BLACK);
@ -68,7 +75,7 @@ public class AirplaneGenericCollection<T extends DrawningAirplane, U extends IMo
if (airplane != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
airplane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeWidth /10 * 2 , (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + (_placeSizeHeight - _placeSizeHeight * 170 / 1000) / 2);
airplane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeWidth /10 * 2 , _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight + (_placeSizeHeight - _placeSizeHeight * 170 / 1000) / 2);
airplane.DrawAirplane((Graphics2D) g);
}
}

View File

@ -0,0 +1,49 @@
package src.Generics;
import src.DrawningObjects.DrawningAirplane;
import src.MovementStrategy.DrawningObjectAirplane;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;
public class AirplaneGenericStorage {
final HashMap<String, AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane>> _airplaneStorages;
public List<String> Keys(){
if(_airplaneStorages == null)
return null;
return _airplaneStorages.keySet().stream().collect(Collectors.toList());
}
private final int _pictureWidth;
private final int _pictureHeight;
public AirplaneGenericStorage(int pictureWidth, int pictureHeight){
_airplaneStorages = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(String name){
if(_airplaneStorages.containsKey(name))
return;
_airplaneStorages.put(name, new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name){
if(!_airplaneStorages.containsKey(name))
return;
_airplaneStorages.remove(name);
}
public AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> Get(String name){
if(!_airplaneStorages.containsKey(name))
return null;
return _airplaneStorages.get(name);
}
public DrawningAirplane Get(String collectionName, int position){
return _airplaneStorages.get(collectionName).Get(position);
}
}

View File

@ -0,0 +1,32 @@
package src.Generics;
import src.DrawningObjects.DrawningAirplane;
import java.util.ArrayDeque;
import java.util.Queue;
public class AirplaneTrashCollection <T extends DrawningAirplane> {
Queue <T> _queue;
public AirplaneTrashCollection(){
_queue = new ArrayDeque<>();
}
public void Push(T airplane){
_queue.add(airplane);
}
public int GetSize(){
return _queue.size();
}
public void Pop(){
if(_queue.size() ==0)
return;
_queue.remove();
}
public T GetTop(){
return _queue.peek();
}
}

View File

@ -1,53 +1,52 @@
package src.Generics;
import java.util.ArrayList;
import java.util.List;
public class SetGeneric <T extends Object>{
private final Object[] _places;
private final List<T> _places;
public int Count;
private final int _maxCount;
public SetGeneric(int count){
_places = new Object[count];
Count = count;
_maxCount = count;
_places = new ArrayList<>();
}
public int Insert(T airplane){
return Insert(airplane, 0);
public boolean Insert(T monorail){
if(_places.size() == _maxCount)
return false;
Insert(monorail, 0);
return true;
}
public int Insert(T airplane, int position){
if(!(position >= 0 && position < Count))
return -1;
if(_places[position] == null){
_places[position] = airplane;
}
else{
int place = -1;
for(int i = position; i < Count; i++){
if(_places[i] == null){
place = i;
break;
}
}
if(place == -1)
return -1;
for(int i = place - 1; i >= position; i--)
_places[i+1] = _places[i];
_places[position] = airplane;
}
return position;
public boolean Insert(T monorail, int position){
if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount))
return false;
_places.add(position, monorail);
Count++;
return true;
}
public boolean Remove(int position){
if(!(position >= 0 && position < Count))
if(!(position >= 0 && position < _places.size()))
return false;
_places[position] = null;
_places.remove(position);
Count--;
return true;
}
public T Get(int position){
if(!(position >= 0 && position < Count))
return null;
return (T)_places[position];
return (T)_places.get(position);
}
public ArrayList<T> GetMonorails(int maxMonorails){
ArrayList<T> toRet = new ArrayList<>();
for(int i = 0; i < _places.size(); i++){
toRet.add(_places.get(i));
if(i == maxMonorails)
return toRet;
}
return toRet;
}
}