This commit is contained in:
marusya 2023-12-29 23:33:12 +04:00
parent 5ccd5bf644
commit 5eef861388
7 changed files with 270 additions and 109 deletions

View File

@ -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() {

View File

@ -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();
}
}
}

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormUstaCollections">
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="1175" height="504"/>
<xy x="232" y="129" width="913" height="469"/>
</constraints>
<properties/>
<border type="none"/>
@ -11,23 +11,23 @@
<grid id="c25f7" binding="pictureBoxCollections" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="700" height="500"/>
<preferred-size width="700" height="500"/>
<maximum-size width="700" height="500"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true">
<minimum-size width="700" height="430"/>
<preferred-size width="700" height="430"/>
<maximum-size width="700" height="430"/>
</grid>
</constraints>
<properties/>
<border type="line"/>
<children/>
</grid>
<grid id="40d5" binding="Instruments" layout-manager="GridLayoutManager" row-count="7" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="40d5" binding="Instruments" layout-manager="GridLayoutManager" row-count="10" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="200" height="400"/>
<preferred-size width="300" height="400"/>
<maximum-size width="300" height="400"/>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true">
<minimum-size width="160" height="400"/>
<preferred-size width="160" height="400"/>
<maximum-size width="160" height="400"/>
</grid>
</constraints>
<properties>
@ -39,20 +39,64 @@
<children>
<vspacer id="1eee0">
<constraints>
<grid row="1" column="1" row-span="6" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="3" column="1" row-span="7" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="5039" class="javax.swing.JTextField" binding="textBoxStorageName">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="50eff" class="javax.swing.JButton" binding="ButtonAddObject">
<constraints>
<grid row="1" 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>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="Добавить набор"/>
</properties>
</component>
<component id="5ef9c" class="javax.swing.JList" binding="listBoxStorage">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties>
<model/>
</properties>
</component>
<component id="1b1c" class="javax.swing.JButton" binding="ButtonCreateRandomUsta">
<constraints>
<grid row="9" 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>
<properties>
<text value="Создать рандомную арт. уст. "/>
</properties>
</component>
<component id="138d" class="javax.swing.JButton" binding="ButtonAddUsta">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" 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>
<properties>
<text value="Добавить арт. уст."/>
</properties>
</component>
<component id="b21cb" class="javax.swing.JButton" binding="ButtonRefreshCollection">
<constraints>
<grid row="8" 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>
<properties>
<text value="Обновить "/>
</properties>
</component>
<component id="1d01d" class="javax.swing.JButton" binding="ButtonRemoveUsta">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="7" 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>
<properties>
<text value="Удалить арт. уст"/>
@ -60,7 +104,7 @@
</component>
<component id="25553" class="javax.swing.JTextField" binding="textFieldNumber">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
@ -68,24 +112,24 @@
<text value="-"/>
</properties>
</component>
<component id="b21cb" class="javax.swing.JButton" binding="ButtonRefreshCollection">
<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>
<properties>
<text value="Обновить "/>
</properties>
</component>
<component id="1b1c" class="javax.swing.JButton" binding="ButtonCreateRandomUsta">
<constraints>
<grid row="5" 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>
<properties>
<text value="Создать рандомную арт. уст."/>
<text value="Удалить набор"/>
</properties>
</component>
</children>
</grid>
<component id="ab73" class="javax.swing.JButton" binding="Button_OpenFormRemovedUsta">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Открыть удаленную арт. уст."/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -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<DrawingUsta, DrawingObjectUsta> _usta;
final UstaGenericStorage _storage;
FrameUstaBat _frameRemovedUsta;
private Stack<DrawingUsta> _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);
}
}

View File

@ -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);

View File

@ -1,61 +1,67 @@
import java.util.ArrayList;
public class SetGeneric<T extends DrawingUsta>{
private T[] _places;
public int Count(){
return _places.length;
}
public SetGeneric(int count) {
_places = (T[]) new DrawingUsta[count];
}
private ArrayList<T> _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<T> GetEnumerator() {
return _places;
}
}

View File

@ -2,10 +2,8 @@ import java.awt.*;
public class UstaGenericCollection<T extends DrawingUsta,U extends IMoveableObject>
{
//ширина/высота окна
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<T extends DrawingUsta,U extends IMoveableObje
public UstaGenericCollection(int picWidth, int picHeight)
{
// немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло...
// высчитываем размер массива для setgeneric
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
@ -23,18 +19,21 @@ public class UstaGenericCollection<T extends DrawingUsta,U extends IMoveableObje
_collection = new SetGeneric<T>(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