done
This commit is contained in:
parent
4b1b74674c
commit
b98cc07a16
@ -1,23 +1,22 @@
|
||||
package DoubleDeckerBus;
|
||||
|
||||
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
||||
import DoubleDeckerBus.Generics.TheBusesGenericCollection;
|
||||
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
|
||||
import DoubleDeckerBus.Generics.BusGenericStorage;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class CollectionCanvas extends JComponent {
|
||||
public TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus;
|
||||
public BusGenericStorage _storage;
|
||||
public JList<String> listBoxStorages;
|
||||
|
||||
@Override
|
||||
public void paintComponent (Graphics g){
|
||||
if (_bus == null) {
|
||||
if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
g2d.drawImage(_bus.ShowTheBuses(), 0, 0, this);
|
||||
g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowTheBuses(), 0, 0, this);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,21 @@
|
||||
package DoubleDeckerBus;
|
||||
|
||||
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
||||
import DoubleDeckerBus.Generics.BusGenericStorage;
|
||||
import DoubleDeckerBus.Generics.BusTrashCollection;
|
||||
import DoubleDeckerBus.Generics.TheBusesGenericCollection;
|
||||
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
|
||||
|
||||
import java.util.List;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
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 pictureBoxHeight = 426;
|
||||
|
||||
@ -22,10 +28,34 @@ public class FormBusCollection {
|
||||
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() {
|
||||
_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();
|
||||
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);
|
||||
@ -35,27 +65,70 @@ public class FormBusCollection {
|
||||
JButton removeButton = new JButton("Удалить");
|
||||
JButton refreshButton = new JButton("Обновить");
|
||||
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.add(addButton);
|
||||
toolBox.add(busNumb);
|
||||
toolBox.add(removeButton);
|
||||
toolBox.add(refreshButton);
|
||||
toolBox.add(callTrashButton);
|
||||
collectionFrame.add(toolBox);
|
||||
collectionFrame.add(canvas);
|
||||
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() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(_bus == null) {
|
||||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> _bus = _storage.Get(listBoxStorages.getSelectedValue());
|
||||
FormDoubleDeckerBus form = new FormDoubleDeckerBus();
|
||||
form.buttonSelect.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_bus.Insert(form.SelectedBus()) != -1)
|
||||
if (_bus.Insert(form.SelectedBus()))
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
form.SelectedBus()._pictureWidth = pictureBoxWidth;
|
||||
@ -66,17 +139,21 @@ public class FormBusCollection {
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
canvas._bus = _bus;
|
||||
form.BusFrame.dispose();
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
removeButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
String tmp = busNumb.getText();
|
||||
@ -88,6 +165,8 @@ public class FormBusCollection {
|
||||
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
DrawningBus curBus = _bus.Get(numb);
|
||||
_trashCollection.Push(curBus);
|
||||
_bus.Remove(numb);
|
||||
_bus.ShowTheBuses();
|
||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
@ -98,7 +177,11 @@ public class FormBusCollection {
|
||||
refreshButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
_bus.ShowTheBuses();
|
||||
|
@ -43,6 +43,12 @@ public class FormDoubleDeckerBus {
|
||||
canvas.repaint();
|
||||
}
|
||||
|
||||
public void ChangeBus(DrawningBus newBus){
|
||||
newBus.SetPosition(0,0);
|
||||
DrawningBus = newBus;
|
||||
canvas.DrawningBus = DrawningBus;
|
||||
}
|
||||
|
||||
public FormDoubleDeckerBus() {
|
||||
BusFrame = new JFrame();
|
||||
JButton buttonCreate = new JButton("Создать");
|
||||
|
51
src/DoubleDeckerBus/Generics/BusGenericStorage.java
Normal file
51
src/DoubleDeckerBus/Generics/BusGenericStorage.java
Normal 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);
|
||||
}
|
||||
}
|
35
src/DoubleDeckerBus/Generics/BusTrashCollection.java
Normal file
35
src/DoubleDeckerBus/Generics/BusTrashCollection.java
Normal 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();
|
||||
}
|
||||
}
|
@ -1,53 +1,42 @@
|
||||
package DoubleDeckerBus.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 bus){
|
||||
return Insert(bus, 0);
|
||||
public boolean Insert(T bus){
|
||||
if (_places.size() == _maxCount) {
|
||||
return false;
|
||||
}
|
||||
Insert(bus, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
public int Insert(T bus, int position){
|
||||
if (!(position >= 0 && position < Count)) {
|
||||
return -1;
|
||||
public boolean Insert(T bus, int position){
|
||||
if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_places[position] == null){
|
||||
_places[position] = bus;
|
||||
} 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;
|
||||
_places.add(position, bus);
|
||||
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;
|
||||
}
|
||||
|
||||
@ -55,6 +44,17 @@ public class SetGeneric<T extends Object>{
|
||||
if (!(position >= 0 && position < Count)) {
|
||||
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;
|
||||
}
|
||||
}
|
@ -25,9 +25,13 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
|
||||
_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);
|
||||
}
|
||||
@ -36,7 +40,7 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
|
||||
return _collection.Remove(position);
|
||||
}
|
||||
|
||||
public U GetU(int pos){
|
||||
public U GetU(int pos) {
|
||||
T ans = _collection.Get(pos);
|
||||
if (ans == null) {
|
||||
return null;
|
||||
@ -44,6 +48,13 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
|
||||
return (U)ans.GetMoveableObject();
|
||||
}
|
||||
|
||||
public T Get(int position) {
|
||||
if (position < 0 || position >= _collection.Count) {
|
||||
return null;
|
||||
}
|
||||
return _collection.Get(position);
|
||||
}
|
||||
|
||||
public BufferedImage ShowTheBuses() {
|
||||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
|
||||
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 j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
|
||||
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,
|
||||
_pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||
_pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user