5 Commits
lab2 ... lab4

Author SHA1 Message Date
791d5205b3 методы добавления из 3ей лабы исправлены на полиморфные 2023-12-03 10:15:33 +04:00
bcc4a0d32f готово!!! 2023-11-28 12:02:52 +04:00
facad042be работа с формой.... 2023-11-27 23:23:06 +04:00
aad104ad8e it's all 2023-11-27 10:20:47 +04:00
492fee19fe in progress 2023-11-26 19:10:56 +04:00
11 changed files with 739 additions and 18 deletions

View File

@@ -16,6 +16,11 @@ public class DrawningAirbus {
private int _airbusWidth = 124;
private int _airbusHeight = 44;
// Получение объекта IMoveableObject из объекта DrawningCar;
public IMoveableObject GetMoveableObject() {
return new DrawningObjectAirbus(this);
}
public int GetPosX() { return _startPosX; }
public int GetPosY() { return _startPosY; }
public int GetWidth() { return _airbusWidth; }

View File

@@ -5,13 +5,13 @@ import Entities.*;
public class DrawningPortholesCircle implements IDrawningPortholes {
private CountPortholes _porthole;
public CountPortholes getCount()
{
return _porthole;
public int Count;
public int getCount() {
return Count;
}
public void SetCount (int count) {
switch (count) {
Count = count;
switch (Count) {
case 10:
_porthole = CountPortholes.Ten;
break;

View File

@@ -4,7 +4,7 @@ import java.awt.*;
import Entities.CountPortholes;
public interface IDrawningPortholes {
public CountPortholes getCount();
public int getCount();
public void SetCount (int count);
public void Draw (Graphics2D g, int _startPosX, int _startPoxY);
}

View File

@@ -9,7 +9,7 @@ public class FormAirbus extends JFrame {
private int width;
private int height;
private DrawningAirbus _drawningAirbus;
public DrawningAirbus _drawningAirbus;
private AbstractStrategy _abstractStrategy;
private Canvas canvas;
@@ -24,6 +24,7 @@ public class FormAirbus extends JFrame {
private JButton buttonCreateAirbus;
private JButton buttonCreatePlane;
public JButton buttonSelectAirbus;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonRight;
@@ -53,6 +54,9 @@ public class FormAirbus extends JFrame {
buttonCreatePlane = new JButton("Создать самолёт");
buttonCreatePlane.setMargin(new Insets(0, 0, 0, 0));
buttonSelectAirbus = new JButton("Выбрать");
buttonSelectAirbus.setMargin(new Insets(0, 0, 0, 0));
buttonUp = new JButton();
buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon("images\\KeyUp.png"));
@@ -70,11 +74,11 @@ public class FormAirbus extends JFrame {
buttonDown.setIcon(new ImageIcon("images\\KeyDown.png"));
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
buttonCreateAirbus.setBounds(12, 355, 146, 33);
buttonCreatePlane.setBounds(182, 355, 146, 33);
buttonCreateAirbus.setBounds(12, 355, 130, 25);
buttonCreatePlane.setBounds(buttonCreateAirbus.getX()+140, 355, 130, 25);
buttonSelectAirbus. setBounds(buttonCreatePlane.getX()+140, 355, 130, 25);
labelCount.setBounds(42, 405, 240, 20);
fieldCount.setBounds(240, 407, 48, 20);
@@ -93,6 +97,7 @@ public class FormAirbus extends JFrame {
add(buttonCreateAirbus);
add(buttonCreatePlane);
add(buttonSelectAirbus);
add(labelCount);
add(fieldCount);
add(labelStrategy);
@@ -119,6 +124,15 @@ public class FormAirbus extends JFrame {
ActionListener buttonCreateAirbusListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
// базовый цвет
Color color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", Color.BLACK);
if (selectedColor != null)
{
color = selectedColor;
}
int countPortholes;
try
{
@@ -134,9 +148,9 @@ public class FormAirbus extends JFrame {
countPortholes = 10;
}
Random rand = new Random();
_drawningAirbus = new DrawningAirbus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
_drawningAirbus = new DrawningAirbus(
rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
color,
countPortholes,
canvas.getWidth(), canvas.getHeight());
@@ -149,6 +163,23 @@ public class FormAirbus extends JFrame {
ActionListener buttonCreatePlaneListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
// базовый цвет
Color color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", Color.BLACK);
if (selectedColor != null)
{
color = selectedColor;
}
// доп цвет
Color additionalColor = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
selectedColor = JColorChooser.showDialog(null, "Выберите доп. цвет", Color.GRAY);
if (selectedColor != null)
{
additionalColor = selectedColor;
}
int countPortholes;
try
{
@@ -164,11 +195,11 @@ public class FormAirbus extends JFrame {
countPortholes = 10;
}
Random rand = new Random();
_drawningAirbus = new DrawningPlane(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
_drawningAirbus = new DrawningPlane(
rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
color,
countPortholes,
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
additionalColor,
rand.nextBoolean(), rand.nextBoolean(),
canvas.getWidth(), canvas.getHeight());

View File

@@ -0,0 +1,299 @@
import Drawnings.DrawningAirbus;
import Generics.AirbusGenericCollection;
import Generics.AirbusGenericStorage;
import MovementStrategy.DrawningObjectAirbus;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FormAirbusCollection extends JFrame {
private Canvas canvas;
private AirbusGenericStorage _storage;
static int pictureBoxWidth = 650;
static int pictureBoxHeight = 460;
private JButton buttonGenerateAirbus;
private JButton buttonUpdate;
private JButton buttonAddAirbus;
private JButton buttonDeleteAirbus;
// работа с наборами
private JButton buttonAddStorage;
private JButton buttonDeleteStorage;
private JButton buttonGetDeleted;
private JTextField textFieldStorage;
private JList<String> listStorage;
private DefaultListModel<String> listModel;
LinkedList<DrawningAirbus> linkedListDeleted;
private JTextField textFieldNumber;
private AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus> _airbus;
FormAirbusCollection() {
listModel = new DefaultListModel<String>();
listStorage = new JList<String>(listModel);
_airbus = new AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus> (pictureBoxWidth, pictureBoxHeight);
_storage = new AirbusGenericStorage(pictureBoxWidth, pictureBoxHeight);
linkedListDeleted = new LinkedList<DrawningAirbus>();
canvas = new Canvas();
JFrame frame = new JFrame("Коллекция аэробусов");
buttonAddStorage = new JButton("Добавить набор");
buttonAddStorage.setMargin(new Insets(0, 0, 0, 0));
buttonDeleteStorage = new JButton("Удалить набор");
buttonDeleteStorage.setMargin(new Insets(0, 0, 0, 0));
buttonAddAirbus = new JButton("Добавить аэробус");
buttonAddAirbus.setMargin(new Insets(0, 0, 0, 0));
buttonDeleteAirbus = new JButton("Удалить аэробус");
buttonDeleteAirbus.setMargin(new Insets(0, 0, 0, 0));
buttonUpdate = new JButton("Обновить");
buttonUpdate.setMargin(new Insets(0, 0, 0, 0));
buttonGetDeleted = new JButton("Загрузить удалёнки");
buttonGetDeleted.setMargin(new Insets(0, 0, 0, 0));
buttonGenerateAirbus = new JButton("Форма генерации");
buttonGenerateAirbus.setMargin(new Insets(0,0,0,0));
textFieldStorage = new JTextField();
textFieldNumber = new JTextField();
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
textFieldStorage.setBounds(pictureBoxWidth, 20, 120, 25);
buttonAddStorage.setBounds(pictureBoxWidth, textFieldStorage.getY()+30, 120, 25);
listStorage.setBounds(pictureBoxWidth, buttonAddStorage.getY()+30, 120, 120);
buttonDeleteStorage.setBounds(pictureBoxWidth, listStorage.getY()+125, 120, 25);
buttonAddAirbus.setBounds(pictureBoxWidth, buttonDeleteStorage.getY()+30, 120, 25);
textFieldNumber.setBounds(pictureBoxWidth, buttonAddAirbus.getY()+30, 120, 25);
buttonDeleteAirbus.setBounds(pictureBoxWidth, textFieldNumber.getY()+30, 120, 25);
buttonUpdate.setBounds(pictureBoxWidth, buttonDeleteAirbus.getY()+30, 120, 25);
buttonGetDeleted.setBounds(pictureBoxWidth, buttonUpdate.getY()+30, 120, 25);
buttonGenerateAirbus.setBounds(pictureBoxWidth, buttonGetDeleted.getY()+30, 120, 25);
canvas.setBounds(0,0,pictureBoxWidth, pictureBoxHeight);
add(canvas);
add(buttonAddStorage);
add(listStorage);
add(textFieldStorage);
add(buttonDeleteStorage);
add(buttonAddAirbus);
add(buttonDeleteAirbus);
add(buttonUpdate);
add(buttonGenerateAirbus);
add(buttonGetDeleted);
add(textFieldNumber);
setVisible(true);
// логика формы
buttonAddStorage.addActionListener(AddStorageListener);
buttonDeleteStorage.addActionListener(DeleteStorageListener);
buttonGetDeleted.addActionListener(GetDeletedListener);
listStorage.addListSelectionListener(listSelectionListener);
buttonAddAirbus.addActionListener(AddAirbusListener);
buttonDeleteAirbus.addActionListener(DeleteAirbusListener);
buttonUpdate.addActionListener(UpdateAirbusListener);
buttonGenerateAirbus.addActionListener(OpenGenerationForm);
}
// секция работы с наборами
private void ReloadObject()
{
int index = listStorage.getSelectedIndex();
listModel.clear();
for (int i = 0; i < _storage.Keys().size(); i++)
{
listModel.addElement(_storage.Keys().get(i));
}
if (listModel.size() > 0 && (index == -1 || index >= listModel.size()))
{
listStorage.setSelectedIndex(0);
}
else if (listModel.size() > 0 && index > -1 && index < listModel.size())
{
listStorage.setSelectedIndex(index);
}
}
// добавить набор
ActionListener AddStorageListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (textFieldStorage.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_storage.AddSet(textFieldStorage.getText());
ReloadObject();
}
};
// выбрать
ListSelectionListener listSelectionListener = new ListSelectionListener() {
// public т.к. в интерфейсе метод тоже public
public void valueChanged(ListSelectionEvent e) {
canvas.repaint();
}
};
// удалить набор
ActionListener DeleteStorageListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listStorage.getSelectedIndex() == -1)
return;
if (JOptionPane.showOptionDialog(null, "Удалить " + listStorage.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Да", "Нет"}, "Да")
== JOptionPane.NO_OPTION)
return;
_storage.DelSet(listStorage.getSelectedValue());
ReloadObject();
}
};
// загрузить удалёнки
ActionListener GetDeletedListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (linkedListDeleted.size() == 0)
{
JOptionPane.showMessageDialog(null, "Нет удалённых аэробусов", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
FormAirbus form = new FormAirbus();
form._drawningAirbus = linkedListDeleted.getLast();
linkedListDeleted.removeLast();
canvas.repaint();
}
};
// конец секции работы с наборами
ActionListener OpenGenerationForm = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
FormGenerationAirbus form = new FormGenerationAirbus();
dispose();
}
};
// добавить аэробус в набор
ActionListener AddAirbusListener = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
if (listStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listStorage.getSelectedValue());
if (obj == null)
return;
// выбор аэробуса на второй форме
FormAirbus form = new FormAirbus();
form.buttonSelectAirbus.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningAirbus selectedAirbus = form._drawningAirbus;
form.dispose();
if (selectedAirbus != null) {
if (obj.Add(selectedAirbus) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
canvas.repaint();
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Ошибка", JOptionPane.ERROR_MESSAGE);
canvas.repaint();
}
}
}
}
);
}
};
// удалить аэробус
ActionListener DeleteAirbusListener = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
if (listStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listStorage.getSelectedValue());
if (obj == null)
return;
Object[] options= {"да", "нет"};
if (JOptionPane.showOptionDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Да", "Нет"}, "Да")
== JOptionPane.NO_OPTION)
return;
try {
int pos = Integer.parseInt(textFieldNumber.getText());
// запоминаем удалёнку
DrawningAirbus deleted = obj.GetT(pos);
if (obj.Remove(pos)) {
// добавить в список удалёнок
linkedListDeleted.add(deleted);
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
canvas.repaint();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
}
};
// обновить
ActionListener UpdateAirbusListener = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
canvas.repaint();
}
};
class Canvas extends JComponent {
public Canvas() {}
public void paintComponent(Graphics g) {
super.paintComponents(g);
if (listStorage.getSelectedIndex() == -1)
return;
var obj = _storage.get(listStorage.getSelectedValue());
if (obj == null)
return;
if (obj.ShowAirbus() != null)
g.drawImage(obj.ShowAirbus(), 0, 0, this);
super.repaint();
}
}
}

View File

@@ -0,0 +1,91 @@
import Drawnings.*;
import Entities.EntityAirbus;
import Entities.EntityPlane;
import Generics.AirbusGenericCollection;
import Generics.GenericParametrized;
import MovementStrategy.DrawningObjectAirbus;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class FormGenerationAirbus extends JFrame {
Canvas canvas;
GenericParametrized<EntityAirbus, IDrawningPortholes> generic;
DrawningAirbus _airbus;
private int _width = 800;
private int _height = 500;
JButton buttonGenerate;
FormGenerationAirbus() {
JFrame frame = new JFrame("Генерация аэробусов");
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
generic = new GenericParametrized<>(50, 50, _width, _height);
canvas = new FormGenerationAirbus.Canvas();
canvas.setBounds(0 , 0, _width, _height);
buttonGenerate = new JButton("Сгенерировать аэробус");
buttonGenerate.setMargin(new Insets(0, 0, 0, 0));
buttonGenerate.setBounds(600, 20, 170, 25);
add(canvas);
add(buttonGenerate);
setVisible(true);
buttonGenerate.addActionListener(GenerateAirbus);
}
ActionListener GenerateAirbus = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
// генерация сущности
EntityAirbus entity;
if (rand.nextBoolean()) {
entity = new EntityAirbus(
rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)));
} else {
entity = new EntityPlane(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)),
new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)),
rand.nextBoolean(), rand.nextBoolean());
}
generic.add(entity);
// генерация иллюминаторов
IDrawningPortholes potholes;
int n = rand.nextInt(3);
if (n == 0)
potholes = new DrawningPortholesCircle();
else if (n == 1)
potholes = new DrawningPortholesHeart();
else
potholes = new DrawningPortholesSquare();
potholes.SetCount(rand.nextInt(1,4)*10);
generic.add(potholes);
// отрисовка
_airbus = generic.randomDrawingObject();
_airbus.SetPosition(100, 100);
canvas.repaint();
}
};
class Canvas extends JComponent {
public Canvas() {}
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
if (_airbus != null)
_airbus.DrawTransport(g2d);
super.repaint();
}
}
}

View File

@@ -0,0 +1,102 @@
package Generics;
import java.awt.*;
import java.awt.image.BufferedImage;
import Drawnings.DrawningAirbus;
import MovementStrategy.IMoveableObject;
public class AirbusGenericCollection<T extends DrawningAirbus, U extends IMoveableObject> {
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 124;
private int _placeSizeHeight = 44;
private SetGeneric<T> _collection;
public AirbusGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
// сложение
public int Add(T obj)
{
if (obj == null)
return -1;
return _collection.Insert(obj);
}
// вычитание
public boolean Remove(int pos)
{
if (_collection.Get(pos) == null)
return false;
return _collection.Remove(pos);
}
// получение объекта IMoveableObjecr
public U GetU(int pos)
{
if (_collection.Get(pos) != null)
return (U)_collection.Get(pos).GetMoveableObject();
return null;
}
// получение объекта DrawningAirbus
public T GetT(int pos)
{
if (_collection.Get(pos) != null)
return (T)_collection.GetAirbus().get(pos);
return null;
}
// вывод всего набора
public BufferedImage ShowAirbus()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
// прорисовка фона
private void DrawBackground(Graphics gr)
{
gr.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
// линия разметки
gr.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
gr.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
}
// прорисовка объекта
private void DrawObjects(Graphics2D gr)
{
// координаты
int x = _pictureWidth / _placeSizeWidth - 1;
int y = _pictureHeight / _placeSizeHeight - 1;
for (var _airbus : _collection.GetAirbus())
{
if (_airbus != null) {
if (x < 0)
{
x = _pictureWidth / _placeSizeWidth - 1;
--y;
}
_airbus.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
_airbus.DrawTransport(gr);
--x;
}
}
}
}

View File

@@ -0,0 +1,52 @@
package Generics;
import Drawnings.DrawningAirbus;
import MovementStrategy.DrawningObjectAirbus;
import java.util.HashMap;
import java.util.List;
public class AirbusGenericStorage {
HashMap <String, AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus>> _airbusStorages;
public List<String> Keys() {
return _airbusStorages.keySet().stream().toList();
}
private int _pictureWidth;
private int _pictureHeight;
public AirbusGenericStorage(int pictureWidth, int pictureHeight)
{
_airbusStorages = new HashMap<String, AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus>> ();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(String name)
{
if (_airbusStorages.containsKey(name))
// уже занят
return;
_airbusStorages.put(name, new AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus>(_pictureWidth, _pictureHeight));
}
public void DelSet(String name)
{
if (!_airbusStorages.containsKey(name))
return;
_airbusStorages.remove(name);
}
// Доступ к набору
public AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus> get(String ind)
{
if (_airbusStorages.containsKey(ind))
return _airbusStorages.get(ind);
return null;
}
// вызов удалёнок
public DrawningObjectAirbus get(String iStorage, int iAirbus){
if (!_airbusStorages.containsKey(iStorage))
return null;
return _airbusStorages.get(iStorage).GetU(iAirbus);
}
}

View File

@@ -0,0 +1,76 @@
package Generics;
import java.util.ArrayList;
import java.util.Random;
import Entities.*;
import Drawnings.*;
public class GenericParametrized<T extends EntityAirbus, U extends IDrawningPortholes> {
private ArrayList<T> _airbus;
private ArrayList<U> _portholes;
private int CountAirbus;
private int CountPortholes;
private int MaxCountAirbus;
private int MaxCountPortoles;
private int _pictureWidth;
private int _pictureHeight;
public GenericParametrized(int maxCountAirbus, int maxCountPortoles, int pictureWidth, int pictureHeight)
{
MaxCountAirbus = maxCountAirbus;
CountAirbus = 0;
MaxCountPortoles = maxCountPortoles;
CountPortholes = 0;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_airbus = new ArrayList<T>();
_portholes = new ArrayList<U>();
}
public boolean add(T airbus)
{
if (airbus == null || CountAirbus >= MaxCountAirbus)
return false;
_airbus.add(airbus);
CountAirbus++;
return true;
}
public boolean add(U porthole)
{
if (porthole == null || CountPortholes >= MaxCountPortoles)
return false;
_portholes.add(porthole);
CountPortholes++;
return true;
}
public DrawningAirbus randomDrawingObject()
{
Random rand = new Random();
if (CountAirbus == 0 || CountPortholes == 0)
return null;
int ar = rand.nextInt(CountAirbus);
int port = rand.nextInt(CountPortholes);
DrawningAirbus drawningAirbus;
if (_airbus.get(ar) instanceof EntityPlane)
drawningAirbus = new DrawningPlane(
_airbus.get(ar).getSpeed(),
_airbus.get(ar).getWeight(),
_airbus.get(ar).getBodyColor(),
_portholes.get(port).getCount(),
((EntityPlane)_airbus.get(ar)).getAdditionalColor(),
((EntityPlane)_airbus.get(ar)).IsCompartment(),
((EntityPlane)_airbus.get(ar)).IsAdditionalEngine(),
_pictureWidth, _pictureHeight);
else
drawningAirbus = new DrawningAirbus(
_airbus.get(ar).getSpeed(),
_airbus.get(ar).getWeight(),
_airbus.get(ar).getBodyColor(),
_portholes.get(port).getCount(),
_pictureWidth, _pictureHeight);
return drawningAirbus;
}
}

View File

@@ -0,0 +1,65 @@
package Generics;
import java.util.ArrayList;
import java.util.Iterator;
public class SetGeneric<T extends Object> {
// список объектов
private ArrayList<T> _places;
// кол-во объектов
private int maxCount;
public int Count()
{
return _places.size();
};
public SetGeneric(int count)
{
_places = new ArrayList<>(count);
maxCount = count;
}
// Добавление объекта в набор
public int Insert(T airbus)
{
return Insert(airbus, 0);
}
// Добавление объекта в набор на конкретную позицию
public int Insert(T airbus, int position)
{
if (position < 0 || position >= maxCount || Count() >= maxCount)
{
return -1;
}
_places.add(position, airbus);
return position;
}
// Удаление объекта из набора с конкретной позиции
public boolean Remove(int position)
{
if (position < 0 || position >= Count())
{
return false;
}
_places.remove(position);
return true;
}
// Получение объекта из набора по позиции
public T Get(int position)
{
if (position < 0 || position >= Count())
{
return null;
}
return _places.get(position);
}
public ArrayList<T> GetAirbus()
{
return _places;
}
}

View File

@@ -1,6 +1,6 @@
public class Main {
public static void main(String[] args) {
new FormAirbus();
new FormAirbusCollection();
}
}