This commit is contained in:
Леонид Малафеев 2023-12-04 22:59:06 +04:00
parent 984fbdc7da
commit 59e45656f2
11 changed files with 460 additions and 15 deletions

View File

@ -0,0 +1,71 @@
package CruiserHard.Complication;
import java.util.ArrayList;
import java.util.Random;
import CruiserHard.Entity.*;
import CruiserHard.Drawing.*;
public class GenericDopClass<T extends EntityCruiser, U extends IDrawBlock> {
private ArrayList<T> Cruisers;
private ArrayList<U> Blocks;
private int maxCountCruisers;
private int countCruisers;
private int maxCountBlocks;
private int countBlocks;
private Random random;
private int _pictureWidth;
private int _pictureHeight;
public GenericDopClass(int _maxCountCruiser, int _maxCountBlock, int pictureWidth, int pictureHeight){
maxCountCruisers = _maxCountCruiser;
maxCountBlocks = _maxCountBlock;
Cruisers = new ArrayList<T>(maxCountCruisers);
Blocks = new ArrayList<U>(maxCountBlocks);
countCruisers = 0;
countBlocks = 0;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
random = new Random();
}
public boolean add(T cruiser){
if (cruiser == null)
return false;
if (countCruisers > maxCountCruisers)
return false;
Cruisers.add(countCruisers++, cruiser);
return true;
}
public boolean add(U block){
if (block == null)
return false;
if (countBlocks > maxCountBlocks)
return false;
Blocks.add(countBlocks++, block);
return true;
}
public DrawingCruiser getDrawingObject(){
if (countCruisers == 0 || countBlocks == 0)
return null;
if(Cruisers.isEmpty()) {
return null;
}
T cruiser = Cruisers.get(random.nextInt(Cruisers.size()));
U block = null;
if(!Cruisers.isEmpty()){
block = Blocks.get(random.nextInt(Blocks.size()));
}
DrawingCruiser drawingCruiser;
if (cruiser instanceof EntityHelipadCruiser){
drawingCruiser = new DrawingHelipadCruiser((EntityHelipadCruiser)cruiser, block, _pictureWidth, _pictureHeight);
}
else{
drawingCruiser = new DrawingCruiser(cruiser, block, _pictureWidth, _pictureHeight);
}
return drawingCruiser;
}
}

View File

@ -1,7 +1,10 @@
package CruiserHard.Drawing; package CruiserHard.Drawing;
import CruiserHard.Complication.IDrawBlock;
import CruiserHard.Direction.Direction; import CruiserHard.Direction.Direction;
import CruiserHard.Entity.EntityCruiser; import CruiserHard.Entity.EntityCruiser;
import CruiserHard.MovementStrategy.DrawingObjectCruiser;
import CruiserHard.MovementStrategy.IMoveableObject;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -19,8 +22,8 @@ public class DrawingCruiser extends JPanel {
public int GetStartY() { public int GetStartY() {
return _startPosY; return _startPosY;
} }
private Integer _pictureWidth = null; //Ширина окна protected Integer _pictureWidth = null; //Ширина окна
private Integer _pictureHeight = null; //Высота окна protected Integer _pictureHeight = null; //Высота окна
private final int _cruiserWidth = 150; //Ширина отрисовки крейсера private final int _cruiserWidth = 150; //Ширина отрисовки крейсера
public int GetWidth() { public int GetWidth() {
return _cruiserWidth; return _cruiserWidth;
@ -29,13 +32,20 @@ public class DrawingCruiser extends JPanel {
public int GetHeight() { public int GetHeight() {
return _cruiserHeight; return _cruiserHeight;
} }
public IMoveableObject GetMoveableObject() { return new DrawingObjectCruiser(this);}
//Инициализация //Инициализация
public DrawingCruiser(int speed, float weight, Color bodyColor, int width, int height, int blockCount) { public DrawingCruiser(int speed, float weight, Color bodyColor, int width, int height, int blockCount) {
cruiser = new EntityCruiser(speed,weight,bodyColor); cruiser = new EntityCruiser(speed,weight,bodyColor);
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = height; _pictureHeight = height;
}
public DrawingCruiser(EntityCruiser _cruiser, IDrawBlock _blockDrawing, int width, int height){
if (height < _cruiserHeight || width < _cruiserWidth)
return;
_pictureWidth = width;
_pictureHeight = height;
cruiser = _cruiser;
} }
//Начальные коордитанты //Начальные коордитанты

View File

@ -18,7 +18,7 @@ public class DrawingHelipadCruiser extends DrawingCruiser {
if (cruiser != null) if (cruiser != null)
{ {
cruiser = new EntityHelipadCruiser(speed, weight, bodyColor, cruiser = new EntityHelipadCruiser(speed, weight, bodyColor,
elementsColor, rocketMines, helipad); elementsColor, rocketMines, helipad, blockCount);
} }
Random rand = new Random(); Random rand = new Random();
drawingBlocks = switch (rand.nextInt(3)) { drawingBlocks = switch (rand.nextInt(3)) {
@ -27,9 +27,16 @@ public class DrawingHelipadCruiser extends DrawingCruiser {
case 2 -> new DrawBlockTriangle(); case 2 -> new DrawBlockTriangle();
default -> new DrawBlock(); default -> new DrawBlock();
}; };
drawingBlocks.SetBlockCount(blockCount); drawingBlocks.SetBlockCount(blockCount);
} }
public DrawingHelipadCruiser(EntityHelipadCruiser _cruiser, IDrawBlock _blockDrawing, int width, int height ){
super(_cruiser, _blockDrawing, width, height);
_pictureWidth = width;
_pictureHeight = height;
cruiser = _cruiser;
drawingBlocks = _blockDrawing;
drawingBlocks.SetBlockCount(_cruiser.BlockCount);
}
@Override @Override
public void DrawTransport(Graphics g) { public void DrawTransport(Graphics g) {
if (!(cruiser instanceof EntityHelipadCruiser helipadCrusier)) { if (!(cruiser instanceof EntityHelipadCruiser helipadCrusier)) {
@ -38,14 +45,14 @@ public class DrawingHelipadCruiser extends DrawingCruiser {
super.DrawTransport(g); super.DrawTransport(g);
drawingBlocks.DrawBlocks(_startPosX, _startPosY,g); drawingBlocks.DrawBlocks(_startPosX, _startPosY,g);
if (helipadCrusier.GetRocketMines()){ if (helipadCrusier.GetRocketMines()){
g.setColor(Color.green); g.setColor(((EntityHelipadCruiser) cruiser).GetElementsColor()); // не использовался приходящий цвет с генерации, добавил
g.fillRect(_startPosX + 15, _startPosY + 10, 10, 15); g.fillRect(_startPosX + 15, _startPosY + 10, 10, 15);
g.fillRect(_startPosX + 30, _startPosY + 10, 10, 15); g.fillRect(_startPosX + 30, _startPosY + 10, 10, 15);
} }
if (helipadCrusier.GetHelipad()) if (helipadCrusier.GetHelipad())
{ {
// верт площадка // верт площадка
g.setColor(Color.ORANGE); g.setColor(((EntityHelipadCruiser) cruiser).GetElementsColor()); // не использовался приходящий цвет с генерации, добавил
g.fillOval(_startPosX + 15, _startPosY + 25, 25, 25); g.fillOval(_startPosX + 15, _startPosY + 25, 25, 25);
} }
} }

View File

@ -11,7 +11,7 @@ public class EntityCruiser {
//Инициализация //Инициализация
public EntityCruiser(int speed, float weight, Color bodyColor) { public EntityCruiser(int speed, float weight, Color bodyColor) {
Random random = new Random(); //че за рандом? не понял сам, стёр
Speed = speed; Speed = speed;
Weight = weight; Weight = weight;
BodyColor = bodyColor; BodyColor = bodyColor;

View File

@ -1,4 +1,6 @@
package CruiserHard.Entity; package CruiserHard.Entity;
import CruiserHard.Complication.Block;
import java.awt.*; import java.awt.*;
public class EntityHelipadCruiser extends EntityCruiser { public class EntityHelipadCruiser extends EntityCruiser {
@ -12,12 +14,14 @@ public class EntityHelipadCruiser extends EntityCruiser {
public boolean GetHelipad(){ public boolean GetHelipad(){
return Helipad; return Helipad;
} }
public int BlockCount;
public EntityHelipadCruiser(int speed, float weight, Color bodyColor, Color public EntityHelipadCruiser(int speed, float weight, Color bodyColor, Color
elementsColor, boolean rocketMines, boolean helipad) { elementsColor, boolean rocketMines, boolean helipad, int blockCount) {
super(speed, weight, bodyColor); super(speed, weight, bodyColor);
ElementsColor = elementsColor; ElementsColor = elementsColor;
RocketMines = rocketMines; RocketMines = rocketMines;
Helipad = helipad; Helipad = helipad;
BlockCount = blockCount;
} }
} }

View File

@ -10,17 +10,22 @@ import CruiserHard.Direction.*;
import CruiserHard.MovementStrategy.*; import CruiserHard.MovementStrategy.*;
public class FormCruiser{ public class FormCruiser{
private DrawingCruiser _drawingCruiser; public DrawingCruiser _drawingCruiser;
private AbstractStrategy _abstractStrategy; private AbstractStrategy _abstractStrategy;
Canvas canv; Canvas canv;
public JButton buttonSelectCruiser;
public JFrame frame;
public void Draw(){ public void Draw(){
canv.repaint(); canv.repaint();
} }
public FormCruiser(){ public FormCruiser(){
JFrame frame = new JFrame("RoadTrain"); frame = new JFrame("RoadTrain");
JButton buttonCreateCruiser = new JButton("Крейсер"); JButton buttonCreateCruiser = new JButton("Крейсер");
buttonCreateCruiser.setFocusPainted(false); buttonCreateCruiser.setFocusPainted(false);
buttonCreateCruiser.setContentAreaFilled(false); buttonCreateCruiser.setContentAreaFilled(false);
buttonSelectCruiser = new JButton("Добавить крейсер");
buttonSelectCruiser.setFocusPainted(false);
buttonSelectCruiser.setContentAreaFilled(false);
JButton buttonCreateHelipadCruiser = new JButton("СуперКрейсер"); JButton buttonCreateHelipadCruiser = new JButton("СуперКрейсер");
buttonCreateHelipadCruiser.setFocusPainted(false); buttonCreateHelipadCruiser.setFocusPainted(false);
buttonCreateHelipadCruiser.setContentAreaFilled(false); buttonCreateHelipadCruiser.setContentAreaFilled(false);
@ -55,9 +60,15 @@ public class FormCruiser{
buttonCreateCruiser.addActionListener( buttonCreateCruiser.addActionListener(
e -> { e -> {
Random random = new Random(); Random random = new Random();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(frame, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
color = selectedColor;
}
_drawingCruiser = new DrawingCruiser(random.nextInt(200) + 100, _drawingCruiser = new DrawingCruiser(random.nextInt(200) + 100,
random.nextInt(2000) + 1000, random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), color,
this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1); this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1);
_drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома _drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома
random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома
@ -69,10 +80,22 @@ public class FormCruiser{
buttonCreateHelipadCruiser.addActionListener( buttonCreateHelipadCruiser.addActionListener(
e -> { e -> {
Random random = new Random(); Random random = new Random();
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(frame, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
color = selectedColor;
}
Color colorelem = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
selectedColor = JColorChooser.showDialog(frame, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
colorelem = selectedColor;
}
_drawingCruiser = new DrawingHelipadCruiser(random.nextInt(100) + 50, _drawingCruiser = new DrawingHelipadCruiser(random.nextInt(100) + 50,
random.nextInt(1000) + 500, random.nextInt(1000) + 500,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), color,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), colorelem,
random.nextBoolean(), random.nextBoolean(), random.nextBoolean(), random.nextBoolean(),
this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1); this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1);
_drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома _drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома
@ -148,6 +171,7 @@ public class FormCruiser{
buttonRight.setBounds(840, 420, 40, 40); buttonRight.setBounds(840, 420, 40, 40);
comboBoxStrategy.setBounds(800,10,100,50); comboBoxStrategy.setBounds(800,10,100,50);
buttonStep.setBounds(800,80,100,40); buttonStep.setBounds(800,80,100,40);
buttonSelectCruiser.setBounds(740, 140, 150, 20);
frame.add(canv); frame.add(canv);
frame.add(buttonCreateCruiser); frame.add(buttonCreateCruiser);
frame.add(buttonCreateHelipadCruiser); frame.add(buttonCreateHelipadCruiser);
@ -157,6 +181,7 @@ public class FormCruiser{
frame.add(buttonRight); frame.add(buttonRight);
frame.add(comboBoxStrategy); frame.add(comboBoxStrategy);
frame.add(buttonStep); frame.add(buttonStep);
frame.add(buttonSelectCruiser);
frame.setVisible(true); frame.setVisible(true);
} }
class Canvas extends JComponent{ class Canvas extends JComponent{

View File

@ -0,0 +1,125 @@
package CruiserHard;
import CruiserHard.MovementStrategy.*;
import CruiserHard.Generic.*;
import CruiserHard.Drawing.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormCruiserCollection {
private class Canvas extends JComponent {
public CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruiser;
public Canvas() {
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
if (_cruiser.ShowCruisers() != null) {
g.drawImage(_cruiser.ShowCruisers(), 0, 10, this);
}
super.repaint();
}
}
Canvas canv;
static int pictureBoxWidth = 700;
static int pictureBoxHeight = 480;
private CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruiser;
public void Draw(){
canv.repaint();
}
FormCruiserCollection() {
canv = new Canvas();
JFrame Frame = new JFrame ("CruiserCollecltion");
_cruiser = new CruiserGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
canv._cruiser = _cruiser;
JButton ButtonAddTruck = new JButton("Добавить крейсер");
ButtonAddTruck.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
FormCruiser form = new FormCruiser();
form.buttonSelectCruiser.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (_cruiser.Add(form._drawingCruiser) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.frame.dispose();
}
}
);
}
}
);
JTextField TextBoxNumber = new JTextField();
JButton ButtonRemoveTruck = new JButton("Удалить технику");
ButtonRemoveTruck.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
return;
}
for (char it : TextBoxNumber.getText().toCharArray())
if (it < '0' || it > '9') {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (TextBoxNumber.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
int pos = Integer.parseInt(TextBoxNumber.getText());
if (_cruiser.remove(pos) != null) {
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
}
}
);
JButton ButtonRefreshCollection = new JButton("Обновить коллекцию");
ButtonRefreshCollection.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Draw();
}
}
);
JButton FormCruiserGenerate = new JButton("Генерировать");
FormCruiserGenerate.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
FormGenericDopClass formGenericDopClass = new FormGenericDopClass();
}
}
);
Frame.setSize (880, 520);
Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Frame.setLayout(null);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
ButtonAddTruck.setBounds(pictureBoxWidth - 50, 10, 170, 30);
TextBoxNumber.setBounds(pictureBoxWidth - 50, 50, 170, 20);
ButtonRemoveTruck.setBounds(pictureBoxWidth - 50, 80, 170, 30);
ButtonRefreshCollection.setBounds(pictureBoxWidth - 50, 120, 170, 30);
FormCruiserGenerate.setBounds(pictureBoxWidth - 50, 160, 170, 30);
Frame.add(canv);
Frame.add(ButtonAddTruck);
Frame.add(ButtonRemoveTruck);
Frame.add(ButtonRefreshCollection);
Frame.add(TextBoxNumber);
Frame.add(FormCruiserGenerate);
Frame.setVisible(true);
}
}

View File

@ -0,0 +1,63 @@
package CruiserHard;
import CruiserHard.Drawing.*;
import CruiserHard.Entity.*;
import CruiserHard.Complication.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class FormGenericDopClass extends JFrame {
static int pictureBoxWidth = 980;
static int pictureBoxHeight = 560;
public DrawingCruiser _drawingCruiser;
private class Canvas extends JComponent{
public Canvas(){
}
public void paintComponent (Graphics g){
if (_drawingCruiser == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawingCruiser.SetPosition(50, 50, pictureBoxWidth, pictureBoxHeight);
_drawingCruiser.DrawTransport(g2d);
super.repaint();
}
}
GenericDopClass<EntityCruiser, IDrawBlock> genericDopClass;
public FormGenericDopClass(){
_drawingCruiser = null;
Canvas canv = new Canvas();
setSize (1000, 600);
setLayout(null);
canv.setBounds(0,0,pictureBoxWidth, pictureBoxHeight);
genericDopClass = new GenericDopClass<>(100, 100, pictureBoxWidth, pictureBoxHeight);
genericDopClass.add(new EntityCruiser(100, 100, Color.BLUE));
genericDopClass.add(new EntityCruiser(100, 100, Color.RED));
genericDopClass.add(new EntityHelipadCruiser(100, 100, Color.MAGENTA, Color.ORANGE,false,true, 3));
genericDopClass.add(new EntityHelipadCruiser(100, 100, Color.cyan, Color.yellow,true,true, 3));
genericDopClass.add(new DrawBlockOval());
genericDopClass.add(new DrawBlockTriangle());
JButton createButton = new JButton("Создать");
createButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
_drawingCruiser = genericDopClass.getDrawingObject();
canv.repaint();
}
}
);
createButton.setBounds(pictureBoxWidth/2, pictureBoxHeight-20, 120, 20);
add(canv);
add(createButton);
setVisible(true);
}
}

View File

@ -0,0 +1,80 @@
package CruiserHard.Generic;
import CruiserHard.Drawing.DrawingCruiser;
import CruiserHard.Drawing.DrawingHelipadCruiser;
import CruiserHard.MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class CruiserGenericCollection<T extends DrawingCruiser, U extends IMoveableObject> {
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 160;
private int _placeSizeHeight = 60;
private SetGeneric<T> _collection;
public CruiserGenericCollection(int pictureWidth, int pictureHeight) {
int width = pictureWidth / _placeSizeWidth;
int height = pictureHeight / _placeSizeHeight;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Add(T obj) {
if (obj == null) {
return -1;
}
return _collection.Insert(obj);
}
public T remove(int pos) {
T obj = _collection.Get(pos);
if (obj != null) {
_collection.Remove(pos);
}
return obj;
}
public U GetU(int pos) {
return (U) _collection.Get(pos).GetMoveableObject();
}
public BufferedImage ShowCruisers() {
BufferedImage bitmap = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bitmap.createGraphics();
DrawBackground(g);
DrawObjects(g);
g.dispose();
return bitmap;
}
private void DrawBackground(Graphics g) {
/*g.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}*/
g.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
{
g.drawRect( i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
}
}
}
private void DrawObjects(Graphics g) {
for (int i = 0; i < _collection.Count; i++) {
T t = _collection.Get(i);
if (t != null) {
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth,
(i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight,
_pictureWidth, _pictureHeight);
if (t instanceof DrawingHelipadCruiser)
((DrawingHelipadCruiser) t).DrawTransport((Graphics2D) g);
else
t.DrawTransport((Graphics2D) g);
}
}
}
}

View File

@ -0,0 +1,60 @@
package CruiserHard.Generic;
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 cruiser) {
int i = 0;
for (; i < _places.length; i++) {
if (_places[i] == null)
break;
}
if (i == _places.length)
return -1;
for (; i > 0; i--) {
_places[i] = _places[i - 1];
}
_places[i] = cruiser;
return i;
}
// Добавление объекта в набор на конкретную позицию
public boolean Insert(T cruiser, 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] = cruiser;
return true;
}
// Удаление объекта из набора с конкретной позиции
public boolean Remove(int position) {
if (position < 0 || position >= _places.length)
return false;
_places[position] = null;
return true;
}
// Получение объекта из набора по позиции
public T Get(int position) {
if (position < 0 || position >= _places.length)
return null;
return (T) _places[position];
}
}

View File

@ -1,6 +1,6 @@
package CruiserHard; package CruiserHard;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
FormCruiser fm = new FormCruiser(); FormCruiserCollection fm = new FormCruiserCollection();;
} }
} }