laba4 Кувшинов Тимур ПИбд-21 Сложная #6

Closed
TImourka wants to merge 7 commits from laba4 into laba3
5 changed files with 230 additions and 52 deletions
Showing only changes of commit accd1149e2 - Show all commits

View File

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

View File

@ -5,47 +5,94 @@ import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FormTrainCollecltion {
private class Canvas extends JComponent{
public TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains;
public Canvas(){
}
public void paintComponent (Graphics g){
super.paintComponent(g);
if (_trains.ShowTrains() != null) {
g.drawImage(_trains.ShowTrains(), 0, 0, this);
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if (obj.ShowTrains() != null) {
g.drawImage(obj.ShowTrains(), 0, 0, this);
}
super.repaint();
}
}
Canvas canv;
static int pictureBoxWidth = 860;
static int pictureBoxWidth = 820;
static int pictureBoxHeight = 580;
private TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains;
private TrainsGenericStorage _storage;
public void Draw(){
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(){
listModel = new DefaultListModel<String>();
jListStorage = new JList<String>(listModel);
canv = new Canvas();
JFrame w = new JFrame ("TrainCollecltion");
_trains = new TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>(pictureBoxWidth, pictureBoxHeight);
canv._trains = _trains;
_storage = new TrainsGenericStorage(pictureBoxWidth, pictureBoxHeight);
JButton ButtonAddTrain = new JButton("ButtonAddTrain");
JButton ButtonAddTrain = new JButton("Add Train");
ButtonAddTrain.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
FormTrain form = new FormTrain();
form.buttonSelectTrain.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (_trains.Add(form._drawingTrain) != -1)
if (obj.Add(form._drawingTrain) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект добавлен");
@ -65,10 +112,20 @@ public class FormTrainCollecltion {
);
JTextField TextBoxNumber = new JTextField();
JButton ButtonRemoveTrain = new JButton("ButtonRemoveTrain");
JButton ButtonRemoveTrain = new JButton("Remove Train");
ButtonRemoveTrain.addActionListener(
new ActionListener() {
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)
{
return;
@ -88,7 +145,7 @@ public class FormTrainCollecltion {
}
int pos = Integer.parseInt(TextBoxNumber.getText());
if (_trains.remove(pos) != null)
if (obj.remove(pos) != null)
{
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект удален");
@ -103,7 +160,7 @@ public class FormTrainCollecltion {
}
);
JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection");
JButton ButtonRefreshCollection = new JButton("Refresh Collection");
ButtonRefreshCollection.addActionListener(
new ActionListener() {
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.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
ButtonAddTrain.setBounds(pictureBoxWidth, 0, 120, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 30, 120, 20);
ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 120, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 120, 20);
toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 120, 20);
ButtonAddTrain.setBounds(pictureBoxWidth, 0, 160, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 30, 160, 20);
ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 160, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 160, 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(ButtonAddTrain);
w.add(ButtonRemoveTrain);
w.add(ButtonRefreshCollection);
w.add(TextBoxNumber);
w.add(toForm4GenericDopClass);
w.add(buttonAddSet);
w.add(textBoxSetName);
w.add(jListStorage);
w.add(buttonRemoveSet);
w.setVisible(true);
}
}

View File

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