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

Closed
TImourka wants to merge 7 commits from laba4 into laba3
8 changed files with 357 additions and 119 deletions

View File

@ -3,12 +3,10 @@ package laba1Loco;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
public class FormTrain{
private class Canvas extends JComponent{
public DrawingTrain _drawingTrain;
public Canvas(){
}
public void paintComponent (Graphics g){
@ -148,7 +146,6 @@ public class FormTrain{
pictureBoxWidth,
pictureBoxHeight);
_drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingTrain = _drawingTrain;
Draw();
}
}
@ -173,7 +170,6 @@ public class FormTrain{
pictureBoxWidth,
pictureBoxHeight);
_drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
canv._drawingTrain = _drawingTrain;
Draw();
}
}

View File

@ -4,48 +4,97 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
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 LinkedList<DrawingTrain> linkedListRemoved;
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("Объект добавлен");
@ -63,12 +112,23 @@ public class FormTrainCollecltion {
}
}
);
linkedListRemoved = new LinkedList<DrawingTrain>();
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,8 +148,10 @@ public class FormTrainCollecltion {
}
int pos = Integer.parseInt(TextBoxNumber.getText());
if (_trains.remove(pos) != null)
var removed = obj.remove(pos);
if (removed != null)
{
linkedListRemoved.add(removed);
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Объект удален");
Draw();
@ -103,7 +165,24 @@ public class FormTrainCollecltion {
}
);
JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection");
JButton buttonGetRemoved = new JButton("buttonGetRemoved");
buttonGetRemoved.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (linkedListRemoved.size()==0){
JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE);
System.out.println("Нет удалённых");
return;
}
FormTrain form = new FormTrain();
form._drawingTrain = linkedListRemoved.getLast();
linkedListRemoved.removeLast();
form.Draw();
}
}
);
JButton ButtonRefreshCollection = new JButton("Refresh Collection");
ButtonRefreshCollection.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
@ -121,21 +200,80 @@ 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);
buttonGetRemoved.setBounds(pictureBoxWidth, 330, 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.add(buttonGetRemoved);
w.setVisible(true);
}
}

View File

@ -1,90 +1,121 @@
package laba1Loco;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SetGeneric<T extends Object> {
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private Object[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new Object[count];
Count = _places.length;
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="train">Добавляемый поезд</param>
/// <returns></returns>
public int Insert(T train)
{
int i = 0;
for (;i < _places.length; i++)
{
if (_places[i] == null)
break;
/// Массив объектов, которые храним
/// </summary>
private ArrayList<T>_places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count () { return _places.size();};
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_maxCount = count;
_places = new ArrayList<T>(count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="train">Добавляемый поезд</param>
/// <returns></returns>
public int Insert(T train)
{
if (_places.size() >= _maxCount)
return -1;
_places.add(0, train);
return 0;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="train">Добавляемый поезд</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public boolean Insert(T train, int position)
{
if (_places.size() >= _maxCount)
return false;
if (position < 0 || position > _places.size())
return false;
if (position == _places.size())
_places.add(train);
else
_places.add(position, train);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public boolean Remove(int position)
{
if (position < 0 || position >= _places.size())
return false;
_places.remove(position);
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
{
if (position < 0 || position >= _places.size())
return null;
return _places.get(position);
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public Iterable<T> GetTrains(final Integer maxTrains) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int currentIndex = 0;
private int count = 0;
@Override
public boolean hasNext() {
return currentIndex < _places.size() && (maxTrains == null || count < maxTrains);
}
@Override
public T next() {
if (hasNext()) {
count++;
return _places.get(currentIndex++);
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
if (i == _places.length)
return -1;
for (; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[i] = train;
return i;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="train">Добавляемый поезд</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public boolean Insert(T train, int position)
{
if (position < 0 || position >= _places.length)
return false;
for (; position < _places.length; position++)
{
if (_places[position] == null)
break;
}
if (position == _places.length)
return false;
for (; position > 0; position--)
{
_places[position] = _places[position - 1];
}
_places[position] = train;
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public boolean Remove(int position)
{
if (position < 0 || position >= _places.length)
return false;
_places[position] = null;
return true;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T Get(int position)
{
if (position < 0 || position >= _places.length)
return null;
return (T)_places[position];
}
};
}
}

View File

@ -112,17 +112,18 @@ 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++)
int i = 0;
for (T train : _collection.GetTrains(100))
{
T t = _collection.Get(i);
if (t != null)
if (train != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t instanceof DrawingLoco)
((DrawingLoco) t).DrawTransport(g);
train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (train instanceof DrawingLoco)
((DrawingLoco)train).DrawTransport(g);
else
t.DrawTransport(g);
train.DrawTransport(g);
}
i++;
}
}
}

View File

@ -0,0 +1,72 @@
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>> _trainStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<String> Keys(){return _trainStorages.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)
{
_trainStorages = new HashMap<String, TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(String name)
{
if (_trainStorages.containsKey(name))
return;
_trainStorages.put(name, new TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>(_pictureWidth, _pictureHeight));
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(String name)
{
if (!_trainStorages.containsKey(name))
return;
_trainStorages.remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> get(String ind)
{
if (_trainStorages.containsKey(ind))
return _trainStorages.get(ind);
return null;
}
public DrawningObjectTrain get(String ind1, int ind2){
if (!_trainStorages.containsKey(ind1))
return null;
return _trainStorages.get(ind1).GetU(ind2);
}
}

View File

@ -48,7 +48,7 @@ public class WheelDrawingBalls implements IWheelDrawing{
DrawWheel(_startPosX + 46, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 72, _startPosY + 34, color, g2d);
if (numWheel == NumWheel.TwoWheel){
if (numWheel == NumWheel.ThreeWheel){
DrawWheel(_startPosX + 14, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 59, _startPosY + 34, color, g2d);
}
@ -65,7 +65,7 @@ public class WheelDrawingBalls implements IWheelDrawing{
DrawWheel(_startPosX + 46+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 72+85, _startPosY + 34, color, g2d);
if (numWheel == NumWheel.TwoWheel){
if (numWheel == NumWheel.ThreeWheel){
DrawWheel(_startPosX + 14+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 59+85, _startPosY + 34, color, g2d);
}

View File

@ -55,7 +55,7 @@ public class WheelDrawingDavidStar implements IWheelDrawing{
DrawWheel(_startPosX + 46, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 72, _startPosY + 34, color, g2d);
if (numWheel == NumWheel.TwoWheel){
if (numWheel == NumWheel.ThreeWheel){
DrawWheel(_startPosX + 14, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 59, _startPosY + 34, color, g2d);
}
@ -72,7 +72,7 @@ public class WheelDrawingDavidStar implements IWheelDrawing{
DrawWheel(_startPosX + 46+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 72+85, _startPosY + 34, color, g2d);
if (numWheel == NumWheel.TwoWheel){
if (numWheel == NumWheel.ThreeWheel){
DrawWheel(_startPosX + 14+85, _startPosY + 34, color, g2d);
DrawWheel(_startPosX + 59+85, _startPosY + 34, color, g2d);
}

View File

@ -39,7 +39,7 @@ public class WheelDrawingSimple implements IWheelDrawing{
g2d.fillOval(_startPosX + 26, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 46, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 72, _startPosY + 34, 8, 8);
if (numWheel == NumWheel.TwoWheel){
if (numWheel == NumWheel.ThreeWheel){
g2d.fillOval(_startPosX + 14, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 59, _startPosY + 34, 8, 8);
}
@ -55,7 +55,7 @@ public class WheelDrawingSimple implements IWheelDrawing{
g2d.fillOval(_startPosX + 26+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 46+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 72+85, _startPosY + 34, 8, 8);
if (numWheel == NumWheel.TwoWheel){
if (numWheel == NumWheel.ThreeWheel){
g2d.fillOval(_startPosX + 14+85, _startPosY + 34, 8, 8);
g2d.fillOval(_startPosX + 59+85, _startPosY + 34, 8, 8);
}