some modifications , but locos doesn't show

This commit is contained in:
ekallin 2023-11-07 20:36:52 +04:00
parent 3989c62269
commit 93d391fad3
5 changed files with 174 additions and 22 deletions

View File

@ -66,7 +66,9 @@
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
<properties>
<model/>
</properties>
</component>
<component id="1b1c" class="javax.swing.JButton" binding="ButtonCreateRandomLoco">
<constraints>
@ -110,7 +112,7 @@
<text value="-"/>
</properties>
</component>
<component id="d1477" class="javax.swing.JButton" binding="ButtomRemoveObject">
<component id="d1477" class="javax.swing.JButton" binding="ButtonRemoveObject">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>

View File

@ -1,6 +1,7 @@
package ProjectElectricLocomotive;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import java.awt.*;
//готовая лаба 3
@ -18,19 +19,53 @@ public class FormLocomotiveCollections {
private JTextField textBoxStorageName;
private JButton ButtonAddObject;
private JList listBoxStorage;
private JButton ButtomRemoveObject;
private JButton ButtonRemoveObject;
public DrawingLocomotive loco;
private DefaultListModel<String> listModel;
final LocomotivesGenericStorage _storage;
LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> _locomotives;
public JPanel getPictureBoxCollections() {
return MainPanel;
}
public FormLocomotiveCollections() {
_locomotives = new LocomotiveGenericCollection<>(400, 300);
_storage = new LocomotivesGenericStorage(pictureBoxCollections.getWidth(), pictureBoxCollections.getHeight());
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();
});
ButtonAddLocomotive.addActionListener(e -> {
if (listBoxStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); // ТУТ ЕЩЕ РАЗ - ОБРАТИ ВНИМАНИЕ НА СТРИНГ
if (obj == null) {
return;
}
FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive();
frameElectricLocomotive.setVisible(true);
frameElectricLocomotive._formLocomotiveCollection.ButtonSelectLocomotive.addActionListener(e2 -> {
@ -38,7 +73,7 @@ public class FormLocomotiveCollections {
frameElectricLocomotive.dispose();
if (loco != null) {
//проверяем, удалось ли нам загрузить объект
if (_locomotives.AddOverload(loco) != -1) {
if (obj.AddOverload(loco)!= -1) {
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен");
Refresh();
} else {
@ -48,16 +83,26 @@ public class FormLocomotiveCollections {
});
});
ButtonCreateRandomLoco.addActionListener(e->{
if(frameDopClassParameters!=null) frameDopClassParameters.dispose();
ButtonCreateRandomLoco.addActionListener(e -> {
if (frameDopClassParameters != null) frameDopClassParameters.dispose();
frameDopClassParameters = new FrameDopClassParameters();
frameDopClassParameters.setVisible(true);
});
ButtonRemoveLocomotive.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());
pos = Integer.parseInt(textFieldNumber.getText());
DrawingLocomotive deletedLoco = obj.SubOverload(pos);
if (_locomotives.SubOverload(pos) != null) {
// logic for push deleted loco in stack
Refresh();
JOptionPane.showMessageDialog(this.getPictureBoxCollections(),
"Объект удален",
@ -82,7 +127,26 @@ public class FormLocomotiveCollections {
});
}
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();
}
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);
_locomotives.ShowLocomotives(g);

View File

@ -26,11 +26,11 @@ public class LocomotiveGenericCollection<T extends DrawingLocomotive,U extends I
/// Перегрузка оператора сложения
//да емае, почему в яве все по-другому?...
public int AddOverload(T loco){
if(loco == null){
public int AddOverload(T obj){
if(obj == null){
return -1;
}
return _collection.Insert(loco);
return _collection.Insert(obj);
}
public T SubOverload(int pos)
@ -73,17 +73,18 @@ public class LocomotiveGenericCollection<T extends DrawingLocomotive,U extends I
private void DrawObjects(Graphics g)
{
int HeightObjCount = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count(); i++)
int i = 0;
for (var obj : _collection.GetEnumerator())
{
T type = _collection.Get(i);
if (type != null)
if (obj != null)
{
type.SetPosition(
obj.SetPosition(
(int)(i / HeightObjCount * _placeSizeWidth),
(HeightObjCount - 1) * _placeSizeHeight - (int)(i % HeightObjCount * _placeSizeHeight)
);
type.DrawTransport(g);
obj.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,71 @@
package ProjectElectricLocomotive;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class LocomotivesGenericStorage {
final HashMap<String, LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>> _locomotiveStorage;
public List<String> Keys()
{
return _locomotiveStorage.keySet().stream().toList();
}
private final int _pictureWidth;
private final int _pictureHeight;
public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) {
_locomotiveStorage = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(String name)
{
if (!(_locomotiveStorage.containsKey(name)))
{
_locomotiveStorage.put(name, new LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive>(_pictureWidth, _pictureHeight));
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(String name)
{
if (_locomotiveStorage.keySet().contains(name))
{
_locomotiveStorage.remove(name);
}
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> get(String ind)
{
// TODO Продумать логику получения набора
if (_locomotiveStorage.keySet().contains(ind))
{
return _locomotiveStorage.get(ind);
}
return null;
}
public DrawingObjectLocomotive get(String name, int ind)
{
if (_locomotiveStorage.keySet().contains(ind))
{
return _locomotiveStorage.get(name).GetU(ind);
}
return null;
}
}

View File

@ -6,12 +6,14 @@ import java.util.NoSuchElementException;
public class SetGeneric<T extends DrawingLocomotive>{
private ArrayList<T> _places;
public int Count()
{
return _places.size();
}
public int maxCount;
public SetGeneric(int count)
{
maxCount = count;
@ -25,11 +27,8 @@ public class SetGeneric<T extends DrawingLocomotive>{
public int Insert(T loco, int position)
{
int NoEmpty = 0, temp = 0;
if(position < 0 || position > Count())
return -1;
if(Count() > maxCount)
return -1;
else
{
_places.add(position, loco);
@ -43,9 +42,11 @@ public class SetGeneric<T extends DrawingLocomotive>{
return null;
else
{
T plane = _places.get(position);
_places.remove(position);
_places.set(position, null);
return plane;
}
return _places.get(position);
}
public T Get(int position)
@ -54,7 +55,21 @@ public class SetGeneric<T extends DrawingLocomotive>{
return _places.get(position);
}
public Iterable<T> GetLocomotives(final Integer maxLocomotives) {
public void Set(int position, T loco) {
// Проверка позиции
// Проверка свободных мест в списке
if (position < 0 || position >= maxCount || Count() == maxCount) {
return;
}
// Вставка в список по позиции
_places.set(position, loco);
}
public ArrayList<T> GetEnumerator() {
return _places;
}
/* public Iterable<T> GetLocomotives(final Integer maxLocomotives) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
@ -84,6 +99,5 @@ public class SetGeneric<T extends DrawingLocomotive>{
}
};
}
};
}
};*/
}