ПИбд-23 Салин Олег Алексеевич Лабораторная работа №4 (усложненная) #5
@ -1,4 +1,3 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" default="true">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
|
@ -41,6 +41,13 @@ public class FormMonorail{
|
||||
canv.repaint();
|
||||
}
|
||||
|
||||
public void ChangeMonorail(DrawningMonorail newMonorail){
|
||||
newMonorail.SetPosition(0,0);
|
||||
DrawningMonorail = newMonorail;
|
||||
canv.DrawningMonorail = DrawningMonorail;
|
||||
|
||||
}
|
||||
|
||||
public FormMonorail(){
|
||||
MonorailFrame =new JFrame ();
|
||||
JButton buttonCreate = new JButton("Создать");
|
||||
|
@ -2,6 +2,8 @@ package MonorailHard;
|
||||
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
import MonorailHard.Generics.MonorailGenericCollection;
|
||||
import MonorailHard.Generics.MonorailGenericStorage;
|
||||
import MonorailHard.Generics.MonorailTrashCollection;
|
||||
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||||
|
||||
import javax.swing.*;
|
||||
@ -9,10 +11,14 @@ import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.text.Normalizer;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class FormMonorailCollection {
|
||||
private final MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails;
|
||||
private final MonorailGenericStorage _storage;
|
||||
|
||||
private JList<String> listBoxStorages;
|
||||
private DefaultListModel<String> listBoxModel;
|
||||
private int pictureBoxWidth = 605;
|
||||
private int pictureBoxHeight = 426;
|
||||
|
||||
@ -24,10 +30,33 @@ public class FormMonorailCollection {
|
||||
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 FormMonorailCollection(){
|
||||
_monorails = new MonorailGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
|
||||
MonorailTrashCollection<DrawningMonorail> _trashCollection = new MonorailTrashCollection<>();
|
||||
JButton callTrashButton = new JButton("мусор");
|
||||
_storage = new MonorailGenericStorage(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 +66,67 @@ public class FormMonorailCollection {
|
||||
JButton removeButton = new JButton("Удалить");
|
||||
JButton refreshButton = new JButton("Обновить");
|
||||
JTextField monorailNumb = 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(monorailNumb);
|
||||
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;
|
||||
FormMonorail form = new FormMonorail();
|
||||
form.ChangeMonorail(_trashCollection.GetTop());
|
||||
_trashCollection.Pop();
|
||||
}
|
||||
});
|
||||
|
||||
addButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(_monorails == null) {
|
||||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||||
FormMonorail form = new FormMonorail();
|
||||
form.buttonSelect.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_monorails.Insert(form.SelectedMonorail()) != -1)
|
||||
if (_monorails.Insert(form.SelectedMonorail()))
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
form.SelectedMonorail()._pictureWidth = pictureBoxWidth;
|
||||
@ -68,7 +137,6 @@ public class FormMonorailCollection {
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
canv._monorails = _monorails;
|
||||
form.MonorailFrame.dispose();
|
||||
Draw();
|
||||
}
|
||||
@ -78,6 +146,10 @@ public class FormMonorailCollection {
|
||||
removeButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||||
if(_monorails == null) {
|
||||
return;
|
||||
}
|
||||
@ -91,6 +163,8 @@ public class FormMonorailCollection {
|
||||
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
DrawningMonorail curMonorail = _monorails.Get(numb);
|
||||
_trashCollection.Push(curMonorail);
|
||||
_monorails.Remove(numb);
|
||||
_monorails.ShowMonorails();
|
||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
@ -101,6 +175,10 @@ public class FormMonorailCollection {
|
||||
refreshButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails = _storage.Get(listBoxStorages.getSelectedValue());
|
||||
if(_monorails == null) {
|
||||
return;
|
||||
}
|
||||
@ -112,18 +190,20 @@ public class FormMonorailCollection {
|
||||
}
|
||||
|
||||
class CollectionCanvas extends JComponent {
|
||||
public MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> _monorails;
|
||||
public MonorailGenericStorage _storage;
|
||||
public JList<String> listBoxStorages;
|
||||
|
||||
|
||||
public CollectionCanvas(){
|
||||
}
|
||||
@Override
|
||||
public void paintComponent (Graphics g){
|
||||
if (_monorails == null){
|
||||
if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1){
|
||||
return;
|
||||
}
|
||||
super.paintComponents (g) ;
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
g2d.drawImage(_monorails.ShowMonorails(), 0, 0, this);
|
||||
g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowMonorails(), 0, 0, this);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
|
@ -25,10 +25,13 @@ public class MonorailGenericCollection<T extends DrawningMonorail, 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);
|
||||
}
|
||||
@ -43,6 +46,11 @@ public class MonorailGenericCollection<T extends DrawningMonorail, U extends IMo
|
||||
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 +76,7 @@ public class MonorailGenericCollection<T extends DrawningMonorail, U extends IMo
|
||||
if (monorail != null)
|
||||
{
|
||||
int inRow = _pictureWidth / _placeSizeWidth;
|
||||
monorail.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight );
|
||||
monorail.SetPosition(i % inRow * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight);
|
||||
monorail.DrawMonorail((Graphics2D) g);
|
||||
}
|
||||
}
|
||||
|
49
src/MonorailHard/Generics/MonorailGenericStorage.java
Normal file
49
src/MonorailHard/Generics/MonorailGenericStorage.java
Normal file
@ -0,0 +1,49 @@
|
||||
package MonorailHard.Generics;
|
||||
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
import MonorailHard.MovementStrategy.DrawningObjectMonorail;
|
||||
|
||||
import java.util.Dictionary;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MonorailGenericStorage {
|
||||
final HashMap<String, MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail>> _monorailStorages;
|
||||
public List<String> Keys(){
|
||||
if(_monorailStorages == null)
|
||||
return null;
|
||||
return _monorailStorages.keySet().stream().collect(Collectors.toList());
|
||||
}
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
|
||||
public MonorailGenericStorage(int pictureWidth, int pictureHeight){
|
||||
_monorailStorages = new HashMap<>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
|
||||
public void AddSet(String name){
|
||||
if(_monorailStorages.containsKey(name))
|
||||
return;
|
||||
_monorailStorages.put(name, new MonorailGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
|
||||
public void DelSet(String name){
|
||||
if(!_monorailStorages.containsKey(name))
|
||||
return;
|
||||
_monorailStorages.remove(name);
|
||||
}
|
||||
|
||||
public MonorailGenericCollection<DrawningMonorail, DrawningObjectMonorail> Get(String name){
|
||||
if(!_monorailStorages.containsKey(name))
|
||||
return null;
|
||||
return _monorailStorages.get(name);
|
||||
}
|
||||
|
||||
public DrawningMonorail Get(String collectionName, int position){
|
||||
return _monorailStorages.get(collectionName).Get(position);
|
||||
}
|
||||
}
|
32
src/MonorailHard/Generics/MonorailTrashCollection.java
Normal file
32
src/MonorailHard/Generics/MonorailTrashCollection.java
Normal file
@ -0,0 +1,32 @@
|
||||
package MonorailHard.Generics;
|
||||
|
||||
import MonorailHard.DrawningObjects.DrawningMonorail;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
|
||||
public class MonorailTrashCollection <T extends DrawningMonorail> {
|
||||
Queue <T> _queue;
|
||||
|
||||
public MonorailTrashCollection(){
|
||||
_queue = new ArrayDeque<>();
|
||||
}
|
||||
|
||||
public void Push(T monorail){
|
||||
_queue.add(monorail);
|
||||
}
|
||||
|
||||
public int GetSize(){
|
||||
return _queue.size();
|
||||
}
|
||||
|
||||
public void Pop(){
|
||||
if(_queue.size() ==0)
|
||||
return;
|
||||
_queue.remove();
|
||||
}
|
||||
|
||||
public T GetTop(){
|
||||
return _queue.peek();
|
||||
}
|
||||
}
|
@ -1,53 +1,56 @@
|
||||
package MonorailHard.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 monorail){
|
||||
return Insert(monorail, 0);
|
||||
public boolean Insert(T monorail){
|
||||
if(_places.size() == _maxCount)
|
||||
return false;
|
||||
Insert(monorail, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
public int Insert(T monorail, int position){
|
||||
if(!(position >= 0 && position < Count))
|
||||
return -1;
|
||||
if(_places[position] == null){
|
||||
_places[position] = monorail;
|
||||
}
|
||||
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] = monorail;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user