PIbd-23_Nasyrov_A_G_Lab4_hard #4
@ -3,15 +3,20 @@ package src;
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.Generics.AirplaneGenericCollection;
|
||||
import src.MovementStrategy.DrawningObjectAirplane;
|
||||
import src.Generics.AirplaneGenericStorage;
|
||||
import src.Generics.AirplaneTrashCollection;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class FormAirplaneCollection {
|
||||
private final AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes;
|
||||
private final AirplaneGenericStorage _storage;
|
||||
private JList<String> listBoxStorages;
|
||||
private DefaultListModel<String> listBoxModel;
|
||||
private int pictureBoxWidth = 630;
|
||||
private int pictureBoxHeight = 426;
|
||||
CollectionCanvas canv;
|
||||
@ -20,10 +25,33 @@ public class FormAirplaneCollection {
|
||||
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 FormAirplaneCollection(){
|
||||
_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);
|
||||
@ -33,36 +61,74 @@ public class FormAirplaneCollection {
|
||||
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(callTrashButton);
|
||||
toolBox.add(refreshButton);
|
||||
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;
|
||||
FormAirplaneWithRadar form = new FormAirplaneWithRadar();
|
||||
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());
|
||||
FormAirplaneWithRadar form = new FormAirplaneWithRadar();
|
||||
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);
|
||||
Draw();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
canv._airplanes = _airplanes;
|
||||
form.AirplaneFrame.dispose();
|
||||
Draw();
|
||||
}
|
||||
@ -72,12 +138,15 @@ public class FormAirplaneCollection {
|
||||
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;
|
||||
}
|
||||
String tmp = airplaneNumb.getText();
|
||||
int numb;
|
||||
|
||||
try{
|
||||
numb = Integer.parseInt(tmp);
|
||||
}
|
||||
@ -85,6 +154,8 @@ public class FormAirplaneCollection {
|
||||
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);
|
||||
@ -95,6 +166,10 @@ public class FormAirplaneCollection {
|
||||
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;
|
||||
}
|
||||
@ -106,17 +181,18 @@ public class FormAirplaneCollection {
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,12 @@ public class FormAirplaneWithRadar {
|
||||
public DrawningAirplane SelectedAirplane(){
|
||||
return DrawningAirplane;
|
||||
}
|
||||
public void ChangeAirplane(DrawningAirplane newAirplane){
|
||||
newAirplane.SetPosition(0,0);
|
||||
DrawningAirplane = newAirplane;
|
||||
canv.DrawningAirplane = DrawningAirplane;
|
||||
|
||||
}
|
||||
public FormAirplaneWithRadar(){
|
||||
AirplaneFrame =new JFrame ();
|
||||
JButton buttonCreate = new JButton("Создать");
|
||||
|
@ -18,16 +18,24 @@ public class AirplaneGenericCollection<T extends DrawningAirplane, U extends IMo
|
||||
_pictureHeight = picHeight;
|
||||
_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);
|
||||
}
|
||||
public boolean Remove(int position){
|
||||
return _collection.Remove(position);
|
||||
}
|
||||
public T Get(int position){
|
||||
if(position < 0 || position >= _collection.Count)
|
||||
return null;
|
||||
return _collection.Get(position);
|
||||
}
|
||||
public U GetU(int pos){
|
||||
T ans = _collection.Get(pos);
|
||||
if(ans == null)
|
||||
|
48
src/Generics/AirplaneGenericStorage.java
Normal file
48
src/Generics/AirplaneGenericStorage.java
Normal file
@ -0,0 +1,48 @@
|
||||
package src.Generics;
|
||||
|
||||
import java.util.Dictionary;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import src.MovementStrategy.DrawningObjectAirplane;
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
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);
|
||||
}
|
||||
}
|
32
src/Generics/AirplaneTrashCollection.java
Normal file
32
src/Generics/AirplaneTrashCollection.java
Normal 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 DrawningAirplane Pop(){
|
||||
if(_queue.size() ==0)
|
||||
return null;
|
||||
|
||||
return _queue.remove();
|
||||
}
|
||||
|
||||
public T GetTop(){
|
||||
return _queue.peek();
|
||||
}
|
||||
}
|
@ -1,47 +1,48 @@
|
||||
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 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 Remove(int position){
|
||||
if(!(position >= 0 && position < Count))
|
||||
public boolean Insert(T airplane){
|
||||
if(_places.size() == _maxCount)
|
||||
return false;
|
||||
_places[position] = null;
|
||||
Insert(airplane, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean Insert(T airplane, int position){
|
||||
if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount))
|
||||
return false;
|
||||
_places.add(position, airplane);
|
||||
Count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean Remove(int position){
|
||||
if(!(position >= 0 && position < _places.size()))
|
||||
return false;
|
||||
_places.remove(position);
|
||||
return true;
|
||||
}
|
||||
public T Get(int position){
|
||||
if(!(position >= 0 && position < Count))
|
||||
if(!(position >= 0 && position < _places.size()))
|
||||
return null;
|
||||
return (T)_places[position];
|
||||
return (T)_places.get(position);
|
||||
}
|
||||
public ArrayList<T> GetAirplanes(int maxAirplanes){
|
||||
ArrayList<T> toRet = new ArrayList<>();
|
||||
for(int i = 0; i < _places.size(); i++){
|
||||
toRet.add(_places.get(i));
|
||||
if(i == maxAirplanes)
|
||||
return toRet;
|
||||
}
|
||||
return toRet;
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,6 @@ import javax.swing.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
FormForHard form = new FormForHard();
|
||||
FormAirplaneCollection form = new FormAirplaneCollection();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user