diff --git a/src/FormDopClassParameters.java b/src/FormDopClassParameters.java
index c8f94fa..3da457f 100644
--- a/src/FormDopClassParameters.java
+++ b/src/FormDopClassParameters.java
@@ -36,17 +36,17 @@ public class FormDopClassParameters {
public void AddEntityUsta() {
rnd = new Random();
- EntityUsta entityLocomotive;
+ EntityUsta entityUsta;
Color color = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
Color color2 = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
if (rnd.nextBoolean()) {
- entityLocomotive = new EntityUsta(
+ entityUsta = new EntityUsta(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
color
);
} else {
- entityLocomotive = new EntityUstaBat(
+ entityUsta = new EntityUstaBat(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
color,
@@ -55,7 +55,7 @@ public class FormDopClassParameters {
rnd.nextBoolean()
);
}
- _dopClassParameters.Add(entityLocomotive);
+ _dopClassParameters.Add(entityUsta);
}
public void AddRandomWheels() {
diff --git a/src/FormUstaBat.java b/src/FormUstaBat.java
index fea29b5..efe1cc2 100644
--- a/src/FormUstaBat.java
+++ b/src/FormUstaBat.java
@@ -142,4 +142,14 @@ public class FormUstaBat {
pictureBox.paint(g);
_drawingUsta.DrawTransport((Graphics2D) g);
}
-}
+
+ public void ChangeUsta(DrawingUsta newUsta){
+ try {
+ _drawingUsta = newUsta;
+ _drawingUsta.SetPosition(20, 20);
+ Draw();
+ } catch (Exception e){
+ throw new RuntimeException();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/FormUstaCollections.form b/src/FormUstaCollections.form
index 8626716..eaedccb 100644
--- a/src/FormUstaCollections.form
+++ b/src/FormUstaCollections.form
@@ -1,9 +1,9 @@
diff --git a/src/FormUstaCollections.java b/src/FormUstaCollections.java
index 5bdcfb4..b7503a7 100644
--- a/src/FormUstaCollections.java
+++ b/src/FormUstaCollections.java
@@ -1,10 +1,12 @@
import javax.swing.*;
+import javax.swing.event.ListSelectionEvent;
import java.awt.*;
+import java.util.Stack;
//готовая лаба 3
public class FormUstaCollections {
- FrameDopClassParameters frameDopClassParameters;
+ FormUstaBat formustabat;
private JPanel MainPanel;
private JPanel pictureBoxCollections;
private JPanel Instruments;
@@ -13,26 +15,66 @@ public class FormUstaCollections {
private JButton ButtonRefreshCollection;
private JButton ButtonRemoveUsta;
private JButton ButtonCreateRandomUsta;
+ private JTextField textBoxStorageName;
+ private JButton ButtonAddObject;
+ private JList listBoxStorage;
+ private JButton ButtonRemoveObject;
+ private JButton Button_OpenFormRemovedUsta;
public DrawingUsta usta;
- UstaGenericCollection _usta;
-
+ final UstaGenericStorage _storage;
+ FrameUstaBat _frameRemovedUsta;
+ private Stack _stackRemoveObjects;
+ FrameDopClassParameters frameDopClassParameters;
public JPanel getPictureBoxCollections() {
return MainPanel;
}
public FormUstaCollections() {
- _usta = new UstaGenericCollection<>(700, 430);
+ formustabat = new FormUstaBat();
+ _storage = new UstaGenericStorage(700,430);
+ _stackRemoveObjects = new Stack<>();
+ listBoxStorage.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged);
+ ButtonAddObject.addActionListener(e ->
+ {
+ String NameStorage = textBoxStorageName.getText();
+ if (NameStorage.equals("")) {
+ JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
+ "Где данные? Напиши хоть что-нибудь",
+ "Ошибка",
+ JOptionPane.INFORMATION_MESSAGE);
+ return;
+ }
+ _storage.AddSet(NameStorage);
+ ReloadObjects();
+ Refresh();
+ });
+
+ ButtonRemoveObject.addActionListener(e ->
+ {
+ if (listBoxStorage.getSelectedIndex() == -1) {
+ return;
+ }
+ JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Коллекция удалена", "Удаление", JOptionPane.INFORMATION_MESSAGE);
+ _storage.DelSet((String) listBoxStorage.getSelectedValue()); //ТУТ СТРИНГ ОБРАТИ ВНИМАНИЕ КАК-НИБУДЬ
+ ReloadObjects();
+ });
ButtonAddUsta.addActionListener(e -> {
- FrameUstaBat frameElectricUsta = new FrameUstaBat();
- frameElectricUsta.setVisible(true);
- frameElectricUsta._formUstaCollection.ButtonSelectUsta.addActionListener(e2 -> {
- usta = frameElectricUsta._formUstaCollection._drawingUsta;
- frameElectricUsta.dispose();
+ if (listBoxStorage.getSelectedIndex() == -1)
+ return;
+ var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); // ТУТ ЕЩЕ РАЗ - ОБРАТИ ВНИМАНИЕ НА СТРИНГ
+ if (obj == null) {
+ return;
+ }
+ FrameUstaBat frameUstaBat = new FrameUstaBat();
+ frameUstaBat.setVisible(true);
+ frameUstaBat._formUstaCollection.ButtonSelectUsta.addActionListener(e2 -> {
+ usta = frameUstaBat._formUstaCollection._drawingUsta;
+ frameUstaBat.dispose();
if (usta != null) {
//проверяем, удалось ли нам загрузить объект
- if (_usta.AddOverload(usta) != -1) {
+ if (obj.AddOverload(usta)!= -1) {
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен");
Refresh();
} else {
@@ -42,32 +84,57 @@ public class FormUstaCollections {
});
});
- ButtonCreateRandomUsta.addActionListener(e->{
- if(frameDopClassParameters!=null) frameDopClassParameters.dispose();
+ ButtonCreateRandomUsta.addActionListener(e -> {
+ if (frameDopClassParameters != null) frameDopClassParameters.dispose();
frameDopClassParameters = new FrameDopClassParameters();
frameDopClassParameters.setVisible(true);
});
ButtonRemoveUsta.addActionListener(e -> {
+ if (listBoxStorage.getSelectedIndex() == -1) {
+ return;
+ }
+ var obj = _storage.get(listBoxStorage.getSelectedValue().toString());
+ if (obj == null) {
+ return;
+ }
+ int pos;
try {
- int pos = Integer.parseInt(textFieldNumber.getText());
- if (_usta.SubOverload(pos) != null) {
- Refresh();
- JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
- "Объект удален",
- "Успех",
- JOptionPane.INFORMATION_MESSAGE);
- } else {
- JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
- "Не удалось удалить объект",
- "Ошибка",
- JOptionPane.ERROR_MESSAGE);
- }
- } catch (Exception ex) {
+ pos = Integer.parseInt(textFieldNumber.getText());
+ } catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
"Неверное значение",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ Object[] options = {"Да", "Нет"};
+ int n = JOptionPane.showOptionDialog(this.getPictureBoxCollections(),
+ "Удалить объект?",
+ "Все серьезно",
+ JOptionPane.YES_NO_OPTION,
+ JOptionPane.QUESTION_MESSAGE,
+ null,
+ options,
+ options[0]
+ );
+ if (n == 1) {
+ return;
+ }
+ DrawingUsta removedUsta = obj.SubOverload(pos);
+ if (removedUsta != null) {
+ _stackRemoveObjects.push(removedUsta);
+ Refresh();
+
+ JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
+ "Объект удален",
+ "Успех",
+ JOptionPane.INFORMATION_MESSAGE);
+ } else {
+ JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
+ "Не удалось удалить объект",
+ "Ошибка",
+ JOptionPane.ERROR_MESSAGE);
}
});
@@ -75,10 +142,45 @@ public class FormUstaCollections {
Refresh();
});
+ Button_OpenFormRemovedUsta.addActionListener(e -> {
+ if(_stackRemoveObjects.empty()) {
+ JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
+ "Вы ничего не удалили, кажется...",
+ "ой",
+ JOptionPane.INFORMATION_MESSAGE);
+ return;
+ }
+ FrameUstaBat RemUsta = new FrameUstaBat();
+ RemUsta._formUstaCollection.ChangeUsta(_stackRemoveObjects.pop());
+ });
+
}
+
+ private void ReloadObjects() {
+ int index = listBoxStorage.getSelectedIndex();
+ listBoxStorage.setListData(_storage.Keys().toArray());
+ if (listBoxStorage.getModel().getSize() > 0 && (index == -1 || index >= listBoxStorage.getModel().getSize())) {
+ listBoxStorage.setSelectedIndex(0);
+ } else if (listBoxStorage.getModel().getSize() > 0 && index > -1 && index < listBoxStorage.getModel().getSize()) {
+ listBoxStorage.setSelectedIndex(index);
+ }
+ listBoxStorage.invalidate();
+ }
+
+ private void listBoxObjectsSelectedIndexChanged(ListSelectionEvent e) {
+ Refresh();
+ }
+
public void Refresh() {
+ if (listBoxStorage.getSelectedIndex() == -1) {
+ return;
+ }
+ var obj = _storage.get(listBoxStorage.getSelectedValue().toString());
+ if (obj == null) {
+ return;
+ }
Graphics g = pictureBoxCollections.getGraphics();
pictureBoxCollections.paint(g);
- _usta.ShowLocomotives((Graphics2D) g);
+ obj.ShowLocomotives((Graphics2D) g);
}
}
diff --git a/src/FrameUstaBat.java b/src/FrameUstaBat.java
index 7072744..8460485 100644
--- a/src/FrameUstaBat.java
+++ b/src/FrameUstaBat.java
@@ -5,7 +5,7 @@ public class FrameUstaBat extends JFrame {
public FrameUstaBat() {
super();
setTitle("UstaBat");
- setDefaultCloseOperation(EXIT_ON_CLOSE);
+ setDefaultCloseOperation(DISPOSE_ON_CLOSE);
_formUstaCollection = new FormUstaBat();
setContentPane(_formUstaCollection.getPictureBox());
setDefaultLookAndFeelDecorated(false);
diff --git a/src/SetGeneric.java b/src/SetGeneric.java
index dc4ee8c..1aca902 100644
--- a/src/SetGeneric.java
+++ b/src/SetGeneric.java
@@ -1,61 +1,67 @@
+import java.util.ArrayList;
+
public class SetGeneric{
- private T[] _places;
- public int Count(){
- return _places.length;
- }
- public SetGeneric(int count) {
- _places = (T[]) new DrawingUsta[count];
- }
+ private ArrayList _places;
- public int Insert(T loco)
+ public int Count()
{
- return Insert(loco, 0);
+ return _places.size();
}
- public int Insert(T loco, int position)
- {
- int NoEmpty = 0, temp = 0;
- for (int i = position; i < Count(); i++)
- {
- if (_places[i] != null) NoEmpty++;
- }
- if (NoEmpty == Count() - position - 1) return -1;
+ public int maxCount;
- if (position < Count() && position >= 0)
+ public SetGeneric(int count)
+ {
+ maxCount = count;
+ _places = new ArrayList<>(count);
+ }
+
+ public int Insert(T usta)
+ {
+ return Insert(usta, 0);
+ }
+
+ public int Insert(T usta, int position)
+ {
+ if(position < 0 || position > maxCount)
+ return -1;
+ else
{
- for (int j = position; j < Count(); j++)
- {
- if (_places[j] == null)
- {
- temp = j;
- break;
- }
- }
- // shift right
- for (int i = temp; i > position; i--)
- {
- _places[i] = _places[i - 1];
- }
- _places[position] = loco;
+ _places.add(position, usta);
return position;
}
- return -1;
}
public T Remove(int position)
{
if (position >= Count() || position < 0)
return null;
-
- T tmp = _places[position];
- _places[position] = null;
- return tmp;
+ else
+ {
+ T plane = _places.get(position);
+ _places.set(position, null);
+ return plane;
+ }
}
public T Get(int position)
{
- // TODO проверка позиции
if (position < 0 || position >= Count()) return null;
- return _places[position];
+ return _places.get(position);
}
-}
+
+ public void Set(int position, T usta) {
+ // Проверка позиции
+ // Проверка свободных мест в списке
+ if (position < 0 || position >= maxCount || Count() == maxCount) {
+ return;
+ }
+ // Вставка в список по позиции
+ _places.set(position, usta);
+ }
+
+ public ArrayList GetEnumerator() {
+ return _places;
+ }
+
+}
\ No newline at end of file
diff --git a/src/UstaGenericCollection.java b/src/UstaGenericCollection.java
index dee3020..6215882 100644
--- a/src/UstaGenericCollection.java
+++ b/src/UstaGenericCollection.java
@@ -2,10 +2,8 @@ import java.awt.*;
public class UstaGenericCollection
{
- //ширина/высота окна
private final int _pictureWidth;
private final int _pictureHeight;
- //ширина/высота занимаемого места
private final int _placeSizeWidth = 210;
private final int _placeSizeHeight = 90;
@@ -14,8 +12,6 @@ public class UstaGenericCollection(width*height);
}
- /// Перегрузка оператора сложения
- //да емае, почему в яве все по-другому?...
-
- public int AddOverload(T loco){
- if(loco == null){
+ public int AddOverload(T usta){
+ if(usta == null){
return -1;
}
- return _collection.Insert(loco);
+ return _collection.Insert(usta);
}
public T SubOverload(int pos){
- return _collection.Remove(pos);
+
+ T usta = _collection.Get(pos);
+ if (usta != null)
+ {
+ _collection.Remove(pos);
+ }
+ return usta;
}
// получение объекта imoveableObj