This commit is contained in:
Вячеслав Иванов 2023-11-14 15:00:54 +04:00
parent 4b1b74674c
commit b98cc07a16
7 changed files with 240 additions and 55 deletions

View File

@ -1,23 +1,22 @@
package DoubleDeckerBus; package DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningBus; import DoubleDeckerBus.Generics.BusGenericStorage;
import DoubleDeckerBus.Generics.TheBusesGenericCollection;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
public class CollectionCanvas extends JComponent { public class CollectionCanvas extends JComponent {
public TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus; public BusGenericStorage _storage;
public JList<String> listBoxStorages;
@Override @Override
public void paintComponent (Graphics g){ public void paintComponent (Graphics g){
if (_bus == null) { if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1) {
return; return;
} }
super.paintComponents(g); super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g; Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(_bus.ShowTheBuses(), 0, 0, this); g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowTheBuses(), 0, 0, this);
super.repaint(); super.repaint();
} }
} }

View File

@ -1,15 +1,21 @@
package DoubleDeckerBus; package DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningBus; import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.Generics.BusGenericStorage;
import DoubleDeckerBus.Generics.BusTrashCollection;
import DoubleDeckerBus.Generics.TheBusesGenericCollection; import DoubleDeckerBus.Generics.TheBusesGenericCollection;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import java.util.List;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
public class FormBusCollection { public class FormBusCollection {
private final TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus; private final BusGenericStorage _storage;
private JList<String> listBoxStorages;
private DefaultListModel<String> listBoxModel;
private int pictureBoxWidth = 605; private int pictureBoxWidth = 605;
private int pictureBoxHeight = 426; private int pictureBoxHeight = 426;
@ -22,10 +28,34 @@ public class FormBusCollection {
canvas.repaint(); canvas.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 FormBusCollection() { public FormBusCollection() {
_bus = new TheBusesGenericCollection<>(pictureBoxWidth, pictureBoxHeight); BusTrashCollection<DrawningBus> _trashCollection = new BusTrashCollection<>();
JButton callTrashButton = new JButton("мусор");
_storage = new BusGenericStorage(pictureBoxWidth, pictureBoxHeight);
JScrollPane scrollPane = new JScrollPane();
canvas = new CollectionCanvas(); canvas = new CollectionCanvas();
JPanel toolBox = new JPanel(); 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(); JFrame collectionFrame = new JFrame();
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880,497); collectionFrame.setSize(880,497);
@ -35,27 +65,70 @@ public class FormBusCollection {
JButton removeButton = new JButton("Удалить"); JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить"); JButton refreshButton = new JButton("Обновить");
JTextField busNumb = new JTextField(); JTextField busNumb = 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.setLayout(lay);
toolBox.add(addButton); toolBox.add(addButton);
toolBox.add(busNumb); toolBox.add(busNumb);
toolBox.add(removeButton); toolBox.add(removeButton);
toolBox.add(refreshButton); toolBox.add(refreshButton);
toolBox.add(callTrashButton);
collectionFrame.add(toolBox); collectionFrame.add(toolBox);
collectionFrame.add(canvas); collectionFrame.add(canvas);
collectionFrame.setVisible(true); collectionFrame.setVisible(true);
canvas._storage = _storage;
canvas.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;
}
FormDoubleDeckerBus form = new FormDoubleDeckerBus();
form.ChangeBus(_trashCollection.GetTop());
_trashCollection.Pop();
}
});
addButton.addActionListener(new ActionListener() { addButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(_bus == null) { if (listBoxStorages.getSelectedIndex() == -1) {
return; return;
} }
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus = _storage.Get(listBoxStorages.getSelectedValue());
FormDoubleDeckerBus form = new FormDoubleDeckerBus(); FormDoubleDeckerBus form = new FormDoubleDeckerBus();
form.buttonSelect.addActionListener(new ActionListener() { form.buttonSelect.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_bus.Insert(form.SelectedBus()) != -1) if (_bus.Insert(form.SelectedBus()))
{ {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form.SelectedBus()._pictureWidth = pictureBoxWidth; form.SelectedBus()._pictureWidth = pictureBoxWidth;
@ -66,17 +139,21 @@ public class FormBusCollection {
{ {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
} }
canvas._bus = _bus;
form.BusFrame.dispose(); form.BusFrame.dispose();
Draw(); Draw();
} }
}); });
} }
}); });
removeButton.addActionListener(new ActionListener() { removeButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(_bus == null) { if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus = _storage.Get(listBoxStorages.getSelectedValue());
if (_bus == null) {
return; return;
} }
String tmp = busNumb.getText(); String tmp = busNumb.getText();
@ -88,6 +165,8 @@ public class FormBusCollection {
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return; return;
} }
DrawningBus curBus = _bus.Get(numb);
_trashCollection.Push(curBus);
_bus.Remove(numb); _bus.Remove(numb);
_bus.ShowTheBuses(); _bus.ShowTheBuses();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
@ -98,7 +177,11 @@ public class FormBusCollection {
refreshButton.addActionListener(new ActionListener() { refreshButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(_bus == null) { if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus = _storage.Get(listBoxStorages.getSelectedValue());
if (_bus == null) {
return; return;
} }
_bus.ShowTheBuses(); _bus.ShowTheBuses();

View File

@ -43,6 +43,12 @@ public class FormDoubleDeckerBus {
canvas.repaint(); canvas.repaint();
} }
public void ChangeBus(DrawningBus newBus){
newBus.SetPosition(0,0);
DrawningBus = newBus;
canvas.DrawningBus = DrawningBus;
}
public FormDoubleDeckerBus() { public FormDoubleDeckerBus() {
BusFrame = new JFrame(); BusFrame = new JFrame();
JButton buttonCreate = new JButton("Создать"); JButton buttonCreate = new JButton("Создать");

View File

@ -0,0 +1,51 @@
package DoubleDeckerBus.Generics;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class BusGenericStorage {
final HashMap<String, TheBusesGenericCollection<DrawningBus, DrawningObjectBus>> _busStorages;
public List<String> Keys() {
if (_busStorages == null) {
return null;
}
return _busStorages.keySet().stream().collect(Collectors.toList());
}
private final int _pictureWidth;
private final int _pictureHeight;
public BusGenericStorage(int pictureWidth, int pictureHeight){
_busStorages = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(String name){
if (_busStorages.containsKey(name)) {
return;
}
_busStorages.put(name, new TheBusesGenericCollection<>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name) {
if (!_busStorages.containsKey(name)) {
return;
}
_busStorages.remove(name);
}
public TheBusesGenericCollection<DrawningBus, DrawningObjectBus> Get(String name){
if (!_busStorages.containsKey(name)) {
return null;
}
return _busStorages.get(name);
}
public DrawningBus Get(String collectionName, int position) {
return _busStorages.get(collectionName).Get(position);
}
}

View File

@ -0,0 +1,35 @@
package DoubleDeckerBus.Generics;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import java.util.Stack;
public class BusTrashCollection<T extends DrawningBus> {
Stack<T> _stack;
public BusTrashCollection(){
_stack = new Stack<>();
}
public void Push(T bus){
_stack.push(bus);
}
public int GetSize(){
return _stack.size();
}
public void Pop(){
if (_stack.size() == 0) {
return;
}
_stack.pop();
}
public T GetTop() {
if (_stack.isEmpty()) {
return null;
}
return _stack.peek();
}
}

View File

@ -1,53 +1,42 @@
package DoubleDeckerBus.Generics; package DoubleDeckerBus.Generics;
import java.util.ArrayList;
import java.util.List;
public class SetGeneric<T extends Object>{ public class SetGeneric<T extends Object>{
private final Object[] _places; private final List<T> _places;
public int Count; public int Count;
private final int _maxCount;
public SetGeneric(int count){ public SetGeneric(int count){
_places = new Object[count]; _maxCount = count;
Count = count; _places = new ArrayList<>();
} }
public int Insert(T bus){ public boolean Insert(T bus){
return Insert(bus, 0); if (_places.size() == _maxCount) {
return false;
}
Insert(bus, 0);
return true;
} }
public int Insert(T bus, int position){ public boolean Insert(T bus, int position){
if (!(position >= 0 && position < Count)) { if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount)) {
return -1; return false;
} }
_places.add(position, bus);
if (_places[position] == null){ Count++;
_places[position] = bus; return true;
} 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] = bus;
}
return position;
} }
public boolean Remove(int position){ public boolean Remove(int position){
if (!(position >= 0 && position < Count)) { if (!(position >= 0 && position < _places.size())) {
return false; return false;
} }
_places[position] = null; _places.remove(position);
Count--;
return true; return true;
} }
@ -55,6 +44,17 @@ public class SetGeneric<T extends Object>{
if (!(position >= 0 && position < Count)) { if (!(position >= 0 && position < Count)) {
return null; return null;
} }
return (T)_places[position]; return (T)_places.get(position);
} }
}
public ArrayList<T> GetBus(int maxBus){
ArrayList<T> toRet = new ArrayList<>();
for (int i = 0; i < _places.size(); i++) {
toRet.add(_places.get(i));
if (i == maxBus) {
return toRet;
}
}
return toRet;
}
}

View File

@ -25,9 +25,13 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
_collection = new SetGeneric<T>(width * height); _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) { if (obj == null) {
return -1; return false;
} }
return _collection.Insert(obj); return _collection.Insert(obj);
} }
@ -36,7 +40,7 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
return _collection.Remove(position); return _collection.Remove(position);
} }
public U GetU(int pos){ public U GetU(int pos) {
T ans = _collection.Get(pos); T ans = _collection.Get(pos);
if (ans == null) { if (ans == null) {
return null; return null;
@ -44,6 +48,13 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
return (U)ans.GetMoveableObject(); return (U)ans.GetMoveableObject();
} }
public T Get(int position) {
if (position < 0 || position >= _collection.Count) {
return null;
}
return _collection.Get(position);
}
public BufferedImage ShowTheBuses() { public BufferedImage ShowTheBuses() {
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR); BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics gr = bmp.createGraphics(); Graphics gr = bmp.createGraphics();
@ -57,10 +68,10 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight,
i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
} }
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
_pictureHeight / _placeSizeHeight * _placeSizeHeight); _pictureHeight / _placeSizeHeight * _placeSizeHeight);
} }
} }