in progress
This commit is contained in:
parent
bcf703d795
commit
492fee19fe
@ -16,6 +16,11 @@ public class DrawningAirbus {
|
|||||||
private int _airbusWidth = 124;
|
private int _airbusWidth = 124;
|
||||||
private int _airbusHeight = 44;
|
private int _airbusHeight = 44;
|
||||||
|
|
||||||
|
// Получение объекта IMoveableObject из объекта DrawningCar;
|
||||||
|
public IMoveableObject GetMoveableObject() {
|
||||||
|
return new DrawningObjectAirbus(this);
|
||||||
|
}
|
||||||
|
|
||||||
public int GetPosX() { return _startPosX; }
|
public int GetPosX() { return _startPosX; }
|
||||||
public int GetPosY() { return _startPosY; }
|
public int GetPosY() { return _startPosY; }
|
||||||
public int GetWidth() { return _airbusWidth; }
|
public int GetWidth() { return _airbusWidth; }
|
||||||
|
@ -5,13 +5,13 @@ import Entities.*;
|
|||||||
|
|
||||||
public class DrawningPortholesCircle implements IDrawningPortholes {
|
public class DrawningPortholesCircle implements IDrawningPortholes {
|
||||||
private CountPortholes _porthole;
|
private CountPortholes _porthole;
|
||||||
|
public int Count;
|
||||||
public CountPortholes getCount()
|
public int getCount() {
|
||||||
{
|
return Count;
|
||||||
return _porthole;
|
|
||||||
}
|
}
|
||||||
public void SetCount (int count) {
|
public void SetCount (int count) {
|
||||||
switch (count) {
|
Count = count;
|
||||||
|
switch (Count) {
|
||||||
case 10:
|
case 10:
|
||||||
_porthole = CountPortholes.Ten;
|
_porthole = CountPortholes.Ten;
|
||||||
break;
|
break;
|
||||||
|
@ -4,7 +4,7 @@ import java.awt.*;
|
|||||||
import Entities.CountPortholes;
|
import Entities.CountPortholes;
|
||||||
|
|
||||||
public interface IDrawningPortholes {
|
public interface IDrawningPortholes {
|
||||||
public CountPortholes getCount();
|
public int getCount();
|
||||||
public void SetCount (int count);
|
public void SetCount (int count);
|
||||||
public void Draw (Graphics2D g, int _startPosX, int _startPoxY);
|
public void Draw (Graphics2D g, int _startPosX, int _startPoxY);
|
||||||
}
|
}
|
@ -9,7 +9,7 @@ public class FormAirbus extends JFrame {
|
|||||||
|
|
||||||
private int width;
|
private int width;
|
||||||
private int height;
|
private int height;
|
||||||
private DrawningAirbus _drawningAirbus;
|
public DrawningAirbus _drawningAirbus;
|
||||||
private AbstractStrategy _abstractStrategy;
|
private AbstractStrategy _abstractStrategy;
|
||||||
private Canvas canvas;
|
private Canvas canvas;
|
||||||
|
|
||||||
@ -24,6 +24,7 @@ public class FormAirbus extends JFrame {
|
|||||||
|
|
||||||
private JButton buttonCreateAirbus;
|
private JButton buttonCreateAirbus;
|
||||||
private JButton buttonCreatePlane;
|
private JButton buttonCreatePlane;
|
||||||
|
public JButton buttonSelectAirbus;
|
||||||
private JButton buttonUp;
|
private JButton buttonUp;
|
||||||
private JButton buttonDown;
|
private JButton buttonDown;
|
||||||
private JButton buttonRight;
|
private JButton buttonRight;
|
||||||
@ -53,6 +54,9 @@ public class FormAirbus extends JFrame {
|
|||||||
buttonCreatePlane = new JButton("Создать самолёт");
|
buttonCreatePlane = new JButton("Создать самолёт");
|
||||||
buttonCreatePlane.setMargin(new Insets(0, 0, 0, 0));
|
buttonCreatePlane.setMargin(new Insets(0, 0, 0, 0));
|
||||||
|
|
||||||
|
buttonSelectAirbus = new JButton("Выбрать");
|
||||||
|
buttonSelectAirbus.setMargin(new Insets(0, 0, 0, 0));
|
||||||
|
|
||||||
buttonUp = new JButton();
|
buttonUp = new JButton();
|
||||||
buttonUp.setName("up");
|
buttonUp.setName("up");
|
||||||
buttonUp.setIcon(new ImageIcon("images\\KeyUp.png"));
|
buttonUp.setIcon(new ImageIcon("images\\KeyUp.png"));
|
||||||
@ -73,8 +77,9 @@ public class FormAirbus extends JFrame {
|
|||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setLayout(null);
|
setLayout(null);
|
||||||
|
|
||||||
buttonCreateAirbus.setBounds(12, 355, 146, 33);
|
buttonCreateAirbus.setBounds(12, 355, 130, 25);
|
||||||
buttonCreatePlane.setBounds(182, 355, 146, 33);
|
buttonCreatePlane.setBounds(buttonCreateAirbus.getX()+140, 355, 130, 25);
|
||||||
|
buttonSelectAirbus. setBounds(buttonCreatePlane.getX()+140, 355, 130, 25);
|
||||||
|
|
||||||
labelCount.setBounds(42, 405, 240, 20);
|
labelCount.setBounds(42, 405, 240, 20);
|
||||||
fieldCount.setBounds(240, 407, 48, 20);
|
fieldCount.setBounds(240, 407, 48, 20);
|
||||||
@ -93,6 +98,7 @@ public class FormAirbus extends JFrame {
|
|||||||
|
|
||||||
add(buttonCreateAirbus);
|
add(buttonCreateAirbus);
|
||||||
add(buttonCreatePlane);
|
add(buttonCreatePlane);
|
||||||
|
add(buttonSelectAirbus);
|
||||||
add(labelCount);
|
add(labelCount);
|
||||||
add(fieldCount);
|
add(fieldCount);
|
||||||
add(labelStrategy);
|
add(labelStrategy);
|
||||||
@ -119,6 +125,15 @@ public class FormAirbus extends JFrame {
|
|||||||
ActionListener buttonCreateAirbusListener = new ActionListener() {
|
ActionListener buttonCreateAirbusListener = new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
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;
|
int countPortholes;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -134,9 +149,9 @@ public class FormAirbus extends JFrame {
|
|||||||
countPortholes = 10;
|
countPortholes = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
Random rand = new Random();
|
_drawningAirbus = new DrawningAirbus(
|
||||||
_drawningAirbus = new DrawningAirbus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
|
rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
|
||||||
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
|
color,
|
||||||
countPortholes,
|
countPortholes,
|
||||||
canvas.getWidth(), canvas.getHeight());
|
canvas.getWidth(), canvas.getHeight());
|
||||||
|
|
||||||
@ -149,6 +164,23 @@ public class FormAirbus extends JFrame {
|
|||||||
ActionListener buttonCreatePlaneListener = new ActionListener() {
|
ActionListener buttonCreatePlaneListener = new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
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;
|
int countPortholes;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -164,11 +196,11 @@ public class FormAirbus extends JFrame {
|
|||||||
countPortholes = 10;
|
countPortholes = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
Random rand = new Random();
|
_drawningAirbus = new DrawningPlane(
|
||||||
_drawningAirbus = new DrawningPlane(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
|
rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
|
||||||
new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)),
|
color,
|
||||||
countPortholes,
|
countPortholes,
|
||||||
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
|
additionalColor,
|
||||||
rand.nextBoolean(), rand.nextBoolean(),
|
rand.nextBoolean(), rand.nextBoolean(),
|
||||||
canvas.getWidth(), canvas.getHeight());
|
canvas.getWidth(), canvas.getHeight());
|
||||||
|
|
||||||
|
155
src/FormAirbusCollection.java
Normal file
155
src/FormAirbusCollection.java
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
import Drawnings.DrawningAirbus;
|
||||||
|
import Generics.AirbusGenericCollection;
|
||||||
|
import MovementStrategy.DrawningObjectAirbus;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class FormAirbusCollection extends JFrame {
|
||||||
|
|
||||||
|
Canvas canvas;
|
||||||
|
|
||||||
|
static int pictureBoxWidth = 650;
|
||||||
|
static int pictureBoxHeight = 460;
|
||||||
|
JButton buttonAddAirbus;
|
||||||
|
JButton buttonDeleteAirbus;
|
||||||
|
JButton buttonUpdate;
|
||||||
|
// открыть форму генерации
|
||||||
|
private JButton buttonGenerateAirbus;
|
||||||
|
|
||||||
|
JTextField textFieldNumber = new JTextField();
|
||||||
|
private AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus> _airbus;
|
||||||
|
|
||||||
|
FormAirbusCollection() {
|
||||||
|
canvas = new Canvas();
|
||||||
|
|
||||||
|
JFrame frame = new JFrame("Коллекция аэробусов");
|
||||||
|
_airbus = new AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus> (pictureBoxWidth, pictureBoxHeight);
|
||||||
|
canvas._airbus = _airbus;
|
||||||
|
|
||||||
|
buttonAddAirbus = new JButton("Добавить аэробус");
|
||||||
|
buttonAddAirbus.setMargin(new Insets(0, 0, 0, 0));
|
||||||
|
textFieldNumber = new JTextField();
|
||||||
|
buttonDeleteAirbus = new JButton("Удалить аэробус");
|
||||||
|
buttonDeleteAirbus.setMargin(new Insets(0, 0, 0, 0));
|
||||||
|
buttonUpdate = new JButton("Обновить");
|
||||||
|
buttonUpdate.setMargin(new Insets(0, 0, 0, 0));
|
||||||
|
buttonGenerateAirbus = new JButton("Форма генерации");
|
||||||
|
buttonGenerateAirbus.setMargin(new Insets(0,0,0,0));
|
||||||
|
|
||||||
|
setSize(800,500);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLayout(null);
|
||||||
|
|
||||||
|
buttonAddAirbus.setBounds(pictureBoxWidth, 20, 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);
|
||||||
|
buttonGenerateAirbus.setBounds(pictureBoxWidth, buttonUpdate.getY()+30, 120, 25);
|
||||||
|
canvas.setBounds(0,0,pictureBoxWidth, pictureBoxHeight);
|
||||||
|
|
||||||
|
add(canvas);
|
||||||
|
add(buttonAddAirbus);
|
||||||
|
add(buttonDeleteAirbus);
|
||||||
|
add(buttonUpdate);
|
||||||
|
add(buttonGenerateAirbus);
|
||||||
|
add(textFieldNumber);
|
||||||
|
setVisible(true);
|
||||||
|
|
||||||
|
// логика формы
|
||||||
|
buttonAddAirbus.addActionListener(AddAirbusListener);
|
||||||
|
buttonDeleteAirbus.addActionListener(DeleteAirbusListener);
|
||||||
|
buttonUpdate.addActionListener(UpdateAirbusListener);
|
||||||
|
buttonGenerateAirbus.addActionListener(OpenGenerationForm);
|
||||||
|
}
|
||||||
|
|
||||||
|
ActionListener OpenGenerationForm = new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
FormGenerationAirbus form = new FormGenerationAirbus();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ActionListener AddAirbusListener = new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
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 (_airbus.Add(selectedAirbus) != -1) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
||||||
|
_airbus.ShowAirbus();
|
||||||
|
canvas.repaint();
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||||||
|
canvas.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ActionListener DeleteAirbusListener = new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
int pos;
|
||||||
|
try {
|
||||||
|
pos = Integer.parseInt(textFieldNumber.getText());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Ошибка ввода", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Object[] options= {"да", "нет"};
|
||||||
|
if (JOptionPane.showOptionDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Да", "Нет"}, "Да")
|
||||||
|
== JOptionPane.NO_OPTION)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_airbus.Remove(pos) != false)
|
||||||
|
{
|
||||||
|
_airbus.ShowAirbus();
|
||||||
|
canvas.repaint();
|
||||||
|
JOptionPane.showMessageDialog(null, "Объект удален");
|
||||||
|
} else {
|
||||||
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ActionListener UpdateAirbusListener = new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
_airbus.ShowAirbus();
|
||||||
|
canvas.repaint();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Canvas extends JComponent {
|
||||||
|
public AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus> _airbus;
|
||||||
|
|
||||||
|
public Canvas() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
super.paintComponents(g);
|
||||||
|
if (_airbus.ShowAirbus() != null) {
|
||||||
|
g.drawImage(_airbus.ShowAirbus(), 0, 0, this);
|
||||||
|
}
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
src/FormGenerationAirbus.java
Normal file
44
src/FormGenerationAirbus.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import Drawnings.DrawningAirbus;
|
||||||
|
import Generics.AirbusGenericCollection;
|
||||||
|
import MovementStrategy.DrawningObjectAirbus;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class FormGenerationAirbus extends JFrame {
|
||||||
|
Canvas canvas;
|
||||||
|
DrawningAirbus _airbus;
|
||||||
|
|
||||||
|
JButton buttonGenerate;
|
||||||
|
|
||||||
|
FormGenerationAirbus() {
|
||||||
|
JFrame frame = new JFrame("Генерация аэробусов");
|
||||||
|
setSize(800,500);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setLayout(null);
|
||||||
|
|
||||||
|
canvas = new FormGenerationAirbus.Canvas();
|
||||||
|
canvas.setBounds(0 , 0, 800, 500);
|
||||||
|
|
||||||
|
buttonGenerate = new JButton("Сгенерировать аэробус");
|
||||||
|
buttonGenerate.setMargin(new Insets(0, 0, 0, 0));
|
||||||
|
buttonGenerate.setBounds(600, 20, 170, 25);
|
||||||
|
|
||||||
|
add(canvas);
|
||||||
|
add(buttonGenerate);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Canvas extends JComponent {
|
||||||
|
public AirbusGenericCollection<DrawningAirbus, DrawningObjectAirbus> _airbus;
|
||||||
|
|
||||||
|
public Canvas() {}
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
super.paintComponents(g);
|
||||||
|
if (_airbus.ShowAirbus() != null) {
|
||||||
|
g.drawImage(_airbus.ShowAirbus(), 0, 0, this);
|
||||||
|
}
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
104
src/Generics/AirbusGenericCollection.java
Normal file
104
src/Generics/AirbusGenericCollection.java
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// вывод всего набора
|
||||||
|
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 (int i = 0; i < _collection.Count; ++i)
|
||||||
|
{
|
||||||
|
DrawningAirbus _airbus = _collection.Get(i);
|
||||||
|
if (_airbus == null)
|
||||||
|
{
|
||||||
|
--x;
|
||||||
|
if (x < 0)
|
||||||
|
{
|
||||||
|
x = _pictureWidth / _placeSizeWidth - 1;
|
||||||
|
--y;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
_airbus.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
|
||||||
|
_airbus.DrawTransport(gr);
|
||||||
|
--x;
|
||||||
|
// новая строка
|
||||||
|
if (x < 0)
|
||||||
|
{
|
||||||
|
x = _pictureWidth / _placeSizeWidth - 1;
|
||||||
|
--y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
74
src/Generics/GenericParametrized.java
Normal file
74
src/Generics/GenericParametrized.java
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean addAirbus(T airbus)
|
||||||
|
{
|
||||||
|
if (airbus == null || CountAirbus >= MaxCountAirbus)
|
||||||
|
return false;
|
||||||
|
_airbus.add(airbus);
|
||||||
|
CountAirbus++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean addPortholes(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 EntityAirbus)
|
||||||
|
drawningAirbus = new DrawningAirbus(
|
||||||
|
_airbus.get(ar).getSpeed(),
|
||||||
|
_airbus.get(ar).getWeight(),
|
||||||
|
_airbus.get(ar).getBodyColor(),
|
||||||
|
_portholes.get(port).getCount(),
|
||||||
|
_pictureWidth, _pictureHeight);
|
||||||
|
else
|
||||||
|
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);
|
||||||
|
return drawningAirbus;
|
||||||
|
}
|
||||||
|
}
|
67
src/Generics/SetGeneric.java
Normal file
67
src/Generics/SetGeneric.java
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package Generics;
|
||||||
|
|
||||||
|
public class SetGeneric<T extends Object> {
|
||||||
|
|
||||||
|
// список объектов
|
||||||
|
private Object[] _places;
|
||||||
|
// кол-во объектов
|
||||||
|
public int Count;
|
||||||
|
|
||||||
|
public SetGeneric(int count)
|
||||||
|
{
|
||||||
|
_places = new Object[count];
|
||||||
|
Count = _places.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Добавление объекта в набор
|
||||||
|
public int Insert(T airbus)
|
||||||
|
{
|
||||||
|
return Insert(airbus, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Добавление объекта в набор на конкретную позицию
|
||||||
|
public int Insert(T airbus, int position)
|
||||||
|
{
|
||||||
|
if (position < 0 || position >= Count)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int pos = position;
|
||||||
|
if (_places[position] != null) {
|
||||||
|
for (int i = position + 1; i < Count; ++i) {
|
||||||
|
if (_places[i] == null) {
|
||||||
|
pos = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pos == Count-1)
|
||||||
|
return -1;
|
||||||
|
for (int i = pos; i > position; --i) {
|
||||||
|
_places[i] = _places[i - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_places[position] = airbus;
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Удаление объекта из набора с конкретной позиции
|
||||||
|
public boolean Remove(int position)
|
||||||
|
{
|
||||||
|
if (position < 0 || position >= Count)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_places[position] = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Получение объекта из набора по позиции
|
||||||
|
public T Get(int position)
|
||||||
|
{
|
||||||
|
if (position < 0 || position >= Count)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (T)_places[position];
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new FormAirbus();
|
new FormAirbusCollection();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user