This commit is contained in:
Timourka 2023-10-24 02:19:14 +04:00
parent 9ae0458bb2
commit accd1149e2
5 changed files with 230 additions and 52 deletions

View File

@ -3,7 +3,6 @@ package laba1Loco;
import java.awt.*; import java.awt.*;
import java.util.*; import java.util.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*; import java.awt.event.*;
public class FormTrain{ public class FormTrain{

View File

@ -5,47 +5,94 @@ import java.awt.Graphics2D;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.JTextField; import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FormTrainCollecltion { public class FormTrainCollecltion {
private class Canvas extends JComponent{ private class Canvas extends JComponent{
public TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains;
public Canvas(){ public Canvas(){
} }
public void paintComponent (Graphics g){ public void paintComponent (Graphics g){
super.paintComponent(g); super.paintComponent(g);
if (_trains.ShowTrains() != null) { if (jListStorage.getSelectedIndex() == -1)
g.drawImage(_trains.ShowTrains(), 0, 0, this); {
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if (obj.ShowTrains() != null) {
g.drawImage(obj.ShowTrains(), 0, 0, this);
} }
super.repaint(); super.repaint();
} }
} }
Canvas canv; Canvas canv;
static int pictureBoxWidth = 860; static int pictureBoxWidth = 820;
static int pictureBoxHeight = 580; static int pictureBoxHeight = 580;
private TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains; private TrainsGenericStorage _storage;
public void Draw(){ public void Draw(){
canv.repaint(); canv.repaint();
} }
private JList<String> jListStorage;
private DefaultListModel<String> listModel;
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = jListStorage.getSelectedIndex();
listModel.clear();
for (String key : _storage.Keys()) {
listModel.addElement(key);
}
if (listModel.size() > 0 && (index == -1 || index >= listModel.size()))
{
jListStorage.setSelectedIndex(0);
}
else if (listModel.size() > 0 && index > -1 && index < listModel.size())
{
jListStorage.setSelectedIndex(index);
}
}
FormTrainCollecltion(){ FormTrainCollecltion(){
listModel = new DefaultListModel<String>();
jListStorage = new JList<String>(listModel);
canv = new Canvas(); canv = new Canvas();
JFrame w = new JFrame ("TrainCollecltion"); JFrame w = new JFrame ("TrainCollecltion");
_trains = new TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>(pictureBoxWidth, pictureBoxHeight); _storage = new TrainsGenericStorage(pictureBoxWidth, pictureBoxHeight);
canv._trains = _trains;
JButton ButtonAddTrain = new JButton("ButtonAddTrain"); JButton ButtonAddTrain = new JButton("Add Train");
ButtonAddTrain.addActionListener( ButtonAddTrain.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
FormTrain form = new FormTrain(); FormTrain form = new FormTrain();
form.buttonSelectTrain.addActionListener( form.buttonSelectTrain.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
if (_trains.Add(form._drawingTrain) != -1) if (obj.Add(form._drawingTrain) != -1)
{ {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект добавлен"); System.out.println("Объект добавлен");
@ -65,10 +112,20 @@ public class FormTrainCollecltion {
); );
JTextField TextBoxNumber = new JTextField(); JTextField TextBoxNumber = new JTextField();
JButton ButtonRemoveTrain = new JButton("ButtonRemoveTrain"); JButton ButtonRemoveTrain = new JButton("Remove Train");
ButtonRemoveTrain.addActionListener( ButtonRemoveTrain.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{ {
return; return;
@ -88,7 +145,7 @@ public class FormTrainCollecltion {
} }
int pos = Integer.parseInt(TextBoxNumber.getText()); int pos = Integer.parseInt(TextBoxNumber.getText());
if (_trains.remove(pos) != null) if (obj.remove(pos) != null)
{ {
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект удален"); System.out.println("Объект удален");
@ -103,7 +160,7 @@ public class FormTrainCollecltion {
} }
); );
JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection"); JButton ButtonRefreshCollection = new JButton("Refresh Collection");
ButtonRefreshCollection.addActionListener( ButtonRefreshCollection.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e){ public void actionPerformed(ActionEvent e){
@ -121,21 +178,76 @@ public class FormTrainCollecltion {
} }
); );
JTextField textBoxSetName = new JTextField();
JButton buttonAddSet = new JButton("Add Set");
buttonAddSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (textBoxSetName.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Не все данные заполнены");
return;
}
_storage.AddSet(textBoxSetName.getText());
ReloadObjects();
}
}
);
jListStorage.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e){
Draw();
}
}
);
JButton buttonRemoveSet = new JButton("Remove Set");
buttonRemoveSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект " + jListStorage.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
_storage.DelSet(jListStorage.getSelectedValue());
ReloadObjects();
}
}
);
w.setSize (1000, 600); w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null); w.setLayout(null);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
ButtonAddTrain.setBounds(pictureBoxWidth, 0, 120, 20); ButtonAddTrain.setBounds(pictureBoxWidth, 0, 160, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 30, 120, 20); TextBoxNumber.setBounds(pictureBoxWidth, 30, 160, 20);
ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 120, 20); ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 160, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 120, 20); ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 160, 20);
toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 120, 20); toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 160, 20);
buttonAddSet.setBounds(pictureBoxWidth, 150, 160, 20);
textBoxSetName.setBounds(pictureBoxWidth, 180, 160, 20);
jListStorage.setBounds(pictureBoxWidth, 210, 160, 80);
buttonRemoveSet.setBounds(pictureBoxWidth, 300, 160, 20);
w.add(canv); w.add(canv);
w.add(ButtonAddTrain); w.add(ButtonAddTrain);
w.add(ButtonRemoveTrain); w.add(ButtonRemoveTrain);
w.add(ButtonRefreshCollection); w.add(ButtonRefreshCollection);
w.add(TextBoxNumber); w.add(TextBoxNumber);
w.add(toForm4GenericDopClass); w.add(toForm4GenericDopClass);
w.add(buttonAddSet);
w.add(textBoxSetName);
w.add(jListStorage);
w.add(buttonRemoveSet);
w.setVisible(true); w.setVisible(true);
} }
} }

View File

@ -1,22 +1,29 @@
package laba1Loco; package laba1Loco;
import java.util.ArrayList;
import java.util.Iterator;
public class SetGeneric<T extends Object> { public class SetGeneric<T extends Object> {
/// <summary> /// <summary>
/// Массив объектов, которые храним /// Массив объектов, которые храним
/// </summary> /// </summary>
private Object[] _places; private ArrayList<T>_places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в массиве
/// </summary> /// </summary>
public int Count; public int Count () { return _places.size();};
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new Object[count]; _maxCount = count;
Count = _places.length; _places = new ArrayList<T>(count);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
@ -25,20 +32,10 @@ public class SetGeneric<T extends Object> {
/// <returns></returns> /// <returns></returns>
public int Insert(T train) public int Insert(T train)
{ {
int i = 0; if (_places.size() >= _maxCount)
for (;i < _places.length; i++)
{
if (_places[i] == null)
break;
}
if (i == _places.length)
return -1; return -1;
for (; i > 0; i--) _places.add(0, train);
{ return 0;
_places[i] = _places[i - 1];
}
_places[i] = train;
return i;
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -48,20 +45,17 @@ public class SetGeneric<T extends Object> {
/// <returns></returns> /// <returns></returns>
public boolean Insert(T train, int position) public boolean Insert(T train, int position)
{ {
if (position < 0 || position >= _places.length) if (_places.size() >= _maxCount)
return false; return false;
for (; position < _places.length; position++)
{ if (position < 0 || position > _places.size())
if (_places[position] == null)
break;
}
if (position == _places.length)
return false; return false;
for (; position > 0; position--)
{ if (position == _places.size())
_places[position] = _places[position - 1]; _places.add(train);
} else
_places[position] = train; _places.add(position, train);
return true; return true;
} }
/// <summary> /// <summary>
@ -71,9 +65,9 @@ public class SetGeneric<T extends Object> {
/// <returns></returns> /// <returns></returns>
public boolean Remove(int position) public boolean Remove(int position)
{ {
if (position < 0 || position >= _places.length) if (position < 0 || position >= _places.size())
return false; return false;
_places[position] = null; _places.remove(position);
return true; return true;
} }
/// <summary> /// <summary>
@ -83,8 +77,15 @@ public class SetGeneric<T extends Object> {
/// <returns></returns> /// <returns></returns>
public T Get(int position) public T Get(int position)
{ {
if (position < 0 || position >= _places.length) if (position < 0 || position >= _places.size())
return null; return null;
return (T)_places[position]; return _places.get(position);
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public Iterator<T> iterator() {
return _places.iterator();
} }
} }

View File

@ -112,7 +112,7 @@ public class TrainsGenericCollection<T extends DrawingTrain, U extends IMoveable
/// <param name="g"></param> /// <param name="g"></param>
private void DrawObjects(Graphics2D g) private void DrawObjects(Graphics2D g)
{ {
for (int i = 0; i < _collection.Count; i++) for (int i = 0; i < _collection.Count(); i++)
{ {
T t = _collection.Get(i); T t = _collection.Get(i);
if (t != null) if (t != null)

View File

@ -0,0 +1,66 @@
package laba1Loco;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class TrainsGenericStorage {
/// <summary>
/// Словарь (хранилище)
/// </summary>
HashMap<String, TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>> _carStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<String> Keys(){return _carStorages.keySet().stream().collect(Collectors.toList());}
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public TrainsGenericStorage(int pictureWidth, int pictureHeight)
{
_carStorages = new HashMap<String, TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(String name)
{
if (_carStorages.containsKey(name))
return;
_carStorages.put(name, new TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>(_pictureWidth, _pictureHeight));
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(String name)
{
if (!_carStorages.containsKey(name))
return;
_carStorages.remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> get(String ind)
{
if (_carStorages.containsKey(ind))
return _carStorages.get(ind);
return null;
}
}