PIbd-23. Yunusov N.N. Lab work 04 Hard #4

Closed
Yunusov_Niyaz wants to merge 3 commits from Lab4 into Lab3
6 changed files with 199 additions and 43 deletions

View File

@ -10,7 +10,7 @@ public class BusesGenericCollection<T extends DrawingBus, U extends IMoveableObj
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height, (Class<T>) DrawingBus.class);
_collection = new SetGeneric<T>(width * height);
}
public void showBuses(Graphics2D g){
drawBackground(g);
@ -24,16 +24,18 @@ public class BusesGenericCollection<T extends DrawingBus, U extends IMoveableObj
}
public boolean remove(int pos)
{
T obj = _collection.Get(pos);
if (obj != null)
return _collection.remove(pos);
return false;
return _collection.remove(pos);
}
public U GetU(int pos) {
if(_collection.Get(pos) == null)
return null;
return (U)_collection.Get(pos).GetMoveableObject();
}
public T Get(int position){
if(position < 0 || position >= _collection.getCount())
return null;
return _collection.Get(position);
}
private void drawBackground(Graphics2D g) {
BasicStroke pen = new BasicStroke(3);
g.setStroke(pen);

View File

@ -0,0 +1,41 @@
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class BusesGenericStorage {
final HashMap<String, BusesGenericCollection<DrawingBus, DrawingObjectBus>> _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 BusesGenericStorage(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 BusesGenericCollection<>(_pictureWidth, _pictureHeight));
}
public void delSet(String name){
if(!_busStorages.containsKey(name))
return;
_busStorages.remove(name);
}
public BusesGenericCollection<DrawingBus, DrawingObjectBus> getCollection(String name){
if(!_busStorages.containsKey(name))
return null;
return _busStorages.get(name);
}
public DrawingBus Get(String collectionName, int position){
return _busStorages.get(collectionName).Get(position);
}
}

View File

@ -1,17 +1,26 @@
import javax.swing.*;
import javax.swing.border.StrokeBorder;
import java.awt.*;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.ArrayDeque;
import java.util.Queue;
public class FrameBusCollection extends JFrame {
private BusesGenericCollection<DrawingBus, DrawingObjectBus> buses;
private BusesGenericStorage storage;
private JList<String> listBoxStorages;
private DefaultListModel<String> listBoxModel;
JComponent pictureBoxCollection;
TextField textFieldNumber;
TextField textFieldStorageName;
Queue <DrawingBus> queue = new ArrayDeque<>();
public FrameBusCollection(){
super("Набор автобусов");
setSize(new Dimension(900,500));
setSize(new Dimension(1000,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGui();
buses = new BusesGenericCollection<>(pictureBoxCollection.getWidth(),pictureBoxCollection.getHeight());
storage = new BusesGenericStorage(pictureBoxCollection.getWidth(),pictureBoxCollection.getHeight());
setVisible(true);
}
private void createGui(){
@ -19,43 +28,83 @@ public class FrameBusCollection extends JFrame {
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (buses != null) buses.showBuses(graphics2D);
if (listBoxStorages == null || storage == null)
return;
var collection = storage.getCollection(listBoxStorages.getSelectedValue());
if (collection == null)
return;
collection.showBuses(graphics2D);
super.repaint();
}
};
pictureBoxCollection.setSize(new Dimension(700,450));
JButton buttonAddBus = new JButton("Добавить автобус");
buttonAddBus.setPreferredSize(new Dimension(160,30));
textFieldNumber = new TextField();
textFieldStorageName = new TextField();
JScrollPane scrollPane = new JScrollPane();
JButton buttonRemoveBus = new JButton("Удалить автобус");
buttonRemoveBus.setPreferredSize(new Dimension(160,30));
JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
buttonAddBus.setPreferredSize(new Dimension(160,30));
JButton buttonAddCollection = new JButton("Добавить набор");
listBoxModel = new DefaultListModel<>();
listBoxStorages= new JList<>(listBoxModel);
scrollPane.setViewportView(listBoxStorages);
JButton buttonDeleteCollection = new JButton("Удалить набор");
JButton buttonTrash = new JButton("Корзина");
buttonAddBus.addActionListener(e -> buttonAddBusClick());
buttonRemoveBus.addActionListener(e -> buttonRemoveBusClick());
buttonRefreshCollection.addActionListener(e -> buttonRefreshBusClick());
buttonAddCollection.addActionListener(e -> buttonAddCollectionClick());
buttonDeleteCollection.addActionListener(e -> buttonDeleteCollectionClick());
buttonTrash.addActionListener(e -> buttonTrashClick());
JPanel panelTools = new JPanel(new GridBagLayout());
panelTools.setBorder(new StrokeBorder(new BasicStroke(3)));
JPanel panelCollection = new JPanel(new GridBagLayout());
panelCollection.setBorder(new StrokeBorder(new BasicStroke(3)));
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.right = 2;
constraints.insets.top = constraints.insets.bottom = 30;
constraints.insets.left = constraints.insets.right = 5;
constraints.insets.top = constraints.insets.bottom = 5;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
panelCollection.add(buttonAddBus, constraints);
panelCollection.add(textFieldStorageName, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
panelCollection.add(textFieldNumber, constraints);
panelCollection.add(buttonAddCollection, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
panelCollection.add(buttonRemoveBus, constraints);
panelCollection.add(scrollPane, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
panelCollection.add(buttonRefreshCollection, constraints);
panelCollection.add(buttonDeleteCollection, constraints);
constraints.gridx = 0;
constraints.gridy = 0;
panelTools.add(panelCollection, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
panelTools.add(buttonAddBus, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
panelTools.add(textFieldNumber, constraints);
constraints.gridx = 0;
constraints.gridy = 3;
panelTools.add(buttonRemoveBus, constraints);
constraints.gridx = 0;
constraints.gridy = 4;
panelTools.add(buttonRefreshCollection, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
panelTools.add(buttonTrash, constraints);
setLayout(new BorderLayout());
add(panelCollection, BorderLayout.EAST);
add(panelTools, BorderLayout.EAST);
add(pictureBoxCollection,BorderLayout.CENTER);
}
private void buttonAddBusClick(){
if (listBoxStorages.getSelectedIndex() == -1)
return;
var obj = storage.getCollection(listBoxStorages.getSelectedValue());
if (obj == null)
return;
FrameTrolleybus form;
try{
form = new FrameTrolleybus();
@ -65,7 +114,7 @@ public class FrameBusCollection extends JFrame {
}
form.selectBusButton.addActionListener(e -> {
form.selectBus();
if (buses.insert(form.getSelectedBus())) {
if (obj.insert(form.getSelectedBus())) {
JOptionPane.showMessageDialog(this, "Объект добавлен");
pictureBoxCollection.repaint();
} else {
@ -75,10 +124,17 @@ public class FrameBusCollection extends JFrame {
});
}
private void buttonRemoveBusClick() {
if (listBoxStorages.getSelectedIndex() == -1)
return;
var obj = storage.getCollection(listBoxStorages.getSelectedValue());
if (obj == null)
return;
int pos = Integer.parseInt(textFieldNumber.getText());
if (buses.remove(pos))
queue.add(obj.Get(pos));
if (obj.remove(pos))
{
JOptionPane.showMessageDialog(this,"Объект удалён");
pictureBoxCollection.repaint();
}
else
{
@ -88,4 +144,49 @@ public class FrameBusCollection extends JFrame {
private void buttonRefreshBusClick(){
pictureBoxCollection.repaint();
}
private void reloadObjects(){
int index = listBoxStorages.getSelectedIndex();
listBoxModel.clear();
List<String> keys = storage.Keys();
for (String key : keys) {
listBoxModel.addElement(key);
}
if(listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size()))
listBoxStorages.setSelectedIndex(0);
else if(listBoxModel.size() > 0)
listBoxStorages.setSelectedIndex(index);
}
private void buttonAddCollectionClick() {
if(textFieldStorageName.getText() == null ) {
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
return;
}
String name = textFieldStorageName.getText();
if (Objects.equals(name, "")) {
JOptionPane.showMessageDialog(this, "Не все данные заполнены");
return;
}
storage.addSet(name);
reloadObjects();
}
private void buttonDeleteCollectionClick() {
if (listBoxStorages.getSelectedIndex() == -1)
return;
storage.delSet(listBoxStorages.getSelectedValue());
reloadObjects();
}
private void buttonTrashClick(){
if(queue.size() == 0)
return;
FrameTrolleybus form;
try{
form = new FrameTrolleybus();
form.ChangeTrolleybus(queue.peek());
queue.remove();
}
catch (IOException e){
throw new RuntimeException();
}
}
}

View File

@ -38,6 +38,7 @@ public class FrameTrolleybus extends JFrame {
pictureBoxTrolleybus.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
createBusButton.addActionListener(e -> buttonCreateBusClick());
createTrolleybusButton.addActionListener(e -> buttonCreateTrolleybusClick());
selectBusButton.addActionListener(e -> dispose());
stepButton.addActionListener(e -> buttonStepClick());
rightButton.setActionCommand("right");
rightButton.addActionListener(this::buttonMoveClick);
@ -187,5 +188,12 @@ public class FrameTrolleybus extends JFrame {
if (drawingBus == null) {
return;
}
selectedBus = drawingBus;}
}
selectedBus = drawingBus;
}
public void ChangeTrolleybus(DrawingBus bus){
bus.setPosition(0,0);
drawingBus = bus;
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
pictureBoxTrolleybus.repaint();
}
}

View File

@ -1,4 +1,4 @@
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException { new FrameHard(); }
public static void main(String[] args) throws IOException { new FrameBusCollection(); }
}

View File

@ -1,39 +1,43 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class SetGeneric<T extends Object>{
private final T[] places;
public int getCount() {return places.length;}
public SetGeneric(int count, Class<T> type){
places = (T[]) Array.newInstance(type, count);
private final List<T> places;
private final int maxCount;
public int getCount() {return places.size();}
public SetGeneric(int count){
maxCount = count;
places = new ArrayList<>();
}
public boolean insert(T bus){
if(places.size() == maxCount)
return false;
return insert(bus, 0);
}
public boolean insert(T bus, int position){
if (!(position >= 0 && position < places.length))
if (!(position >= 0 && position <= places.size() && places.size() < maxCount))
return false;
if (places[position] != null)
{
int ind = position;
while (ind < places.length && places[ind] != null)
ind++;
if (ind == places.length)
return false;
for (int i = ind - 1; i >= position; i--)
places[i + 1] = places[i];
}
places[position] = bus;
places.add(position, bus);
return true;
}
public boolean remove(int position){
if(!(position >= 0 && position < getCount()))
return false;
places[position] = null;
places.remove(position);
return true;
}
public T Get(int position){
if(!(position >= 0 && position < getCount()))
return null;
return places[position];
return (T)places.get(position);
}
public ArrayList<T> GetBuses(int maxBuses){
ArrayList<T> toRet = new ArrayList<>();
for(int i = 0; i < places.size(); i++){
toRet.add(places.get(i));
if(i == maxBuses)
return toRet;
}
return toRet;
}
}