From 22007c1e7e2183ee17f4ee02350449de52061103 Mon Sep 17 00:00:00 2001 From: devil_1nc <diamondoriginal367@gmail.com> Date: Tue, 6 Dec 2022 12:26:38 +0400 Subject: [PATCH] com 1 --- DrawingAdvancedAirbus.java | 4 + DrawingAirbus.java | 9 +- DrawingPlusIlum.java | 2 +- DrawingSquareIlum.java | 2 +- EntityWithIlumCreator.java | 43 +++++++ FormAirbus.java | 82 +++++++------ FormEntityWithExtraGallery.java | 87 ++++++++++++++ FormMap.java | 9 +- FormMapWithSetAirbus.java | 202 ++++++++++++++++++++++++++++++++ MapWithSetAirbusGeneric.java | 141 ++++++++++++++++++++++ Program.java | 2 +- SetAirbusGeneric.java | 60 ++++++++++ 12 files changed, 592 insertions(+), 51 deletions(-) create mode 100644 EntityWithIlumCreator.java create mode 100644 FormEntityWithExtraGallery.java create mode 100644 FormMapWithSetAirbus.java create mode 100644 MapWithSetAirbusGeneric.java create mode 100644 SetAirbusGeneric.java diff --git a/DrawingAdvancedAirbus.java b/DrawingAdvancedAirbus.java index 7861055..89445b8 100644 --- a/DrawingAdvancedAirbus.java +++ b/DrawingAdvancedAirbus.java @@ -6,6 +6,10 @@ public class DrawingAdvancedAirbus extends DrawingAirbus{ super(speed, weight, bodyColor, 145, 55); Airbus = new EntityAdvancedAirbus(speed, weight, bodyColor, extraColor, superTurbine, extraCell); } + public DrawingAdvancedAirbus(EntityAirbus entity, IDrawingIlum ilum){ + super(entity, ilum); + Airbus = entity; + } @Override public void DrawTransport(Graphics2D g) { diff --git a/DrawingAirbus.java b/DrawingAirbus.java index 7f39cd9..7c4191c 100644 --- a/DrawingAirbus.java +++ b/DrawingAirbus.java @@ -18,10 +18,10 @@ public class DrawingAirbus { int randExtra = random.nextInt(2); switch (random.nextInt(3)){ case 0: - drawingilum = new DrawingPlusIlum(randExtra); + drawingilum = new DrawingPlusIlum(randExtra, bodyColor); break; case 1: - drawingilum = new DrawingSquareIlum(randExtra); + drawingilum = new DrawingSquareIlum(randExtra, bodyColor); break; case 2: drawingilum = new DrawingIlum(randExtra, bodyColor); @@ -29,6 +29,10 @@ public class DrawingAirbus { } Airbus = new EntityAirbus(speed, weight, bodyColor); } + public DrawingAirbus(EntityAirbus entity, IDrawingIlum ilum){ + Airbus = entity; + drawingilum = ilum; + } // Новый конструктор protected DrawingAirbus (int speed, float weight, Color bodyColor, int airbusWidth, int airbusHeight) { @@ -36,6 +40,7 @@ public class DrawingAirbus { _airbusWidth = airbusWidth; _airbusHeight = airbusHeight; } + public void SetPosition(int x, int y, int width, int height) { if (x < 0 || x + _airbusWidth >= width) diff --git a/DrawingPlusIlum.java b/DrawingPlusIlum.java index 652644b..994d5cd 100644 --- a/DrawingPlusIlum.java +++ b/DrawingPlusIlum.java @@ -16,7 +16,7 @@ public class DrawingPlusIlum implements IDrawingIlum{ break; } } - public DrawingPlusIlum(int num){ + public DrawingPlusIlum(int num, Color color){ setIl(num); } @Override diff --git a/DrawingSquareIlum.java b/DrawingSquareIlum.java index 8e5f070..860220f 100644 --- a/DrawingSquareIlum.java +++ b/DrawingSquareIlum.java @@ -16,7 +16,7 @@ public class DrawingSquareIlum implements IDrawingIlum { break; } } - public DrawingSquareIlum(int num){ + public DrawingSquareIlum(int num, Color color){ setIl(num); } @Override diff --git a/EntityWithIlumCreator.java b/EntityWithIlumCreator.java new file mode 100644 index 0000000..0aac12d --- /dev/null +++ b/EntityWithIlumCreator.java @@ -0,0 +1,43 @@ +import java.util.Random; + +public class EntityWithIlumCreator <T extends EntityAirbus, U extends IDrawingIlum> +{ + private final Object[] entityArr; + private final Object[] IlumArr; + + int entitiesCount = 0; + int ilumCount = 0; + + public EntityWithIlumCreator(int countEntities, int countExtra) { + entityArr = new Object[countEntities]; + IlumArr = new Object[countExtra]; + } + + public void Insert(T entityAirbus) { + if(entitiesCount < entityArr.length) { + entityArr[entitiesCount] = entityAirbus; + entitiesCount++; + } + } + + public void Insert (U extra) { + if(ilumCount < IlumArr.length) { + IlumArr[ilumCount] = extra; + ilumCount++; + } + } + + public DrawingAirbus getEntityWithExtra() { + Random random = new Random(); + int getEntityRandomIndex = random.nextInt(entityArr.length); + int getExtraRandomIndex = random.nextInt(IlumArr.length); + + EntityAirbus airbus = (T)entityArr[getEntityRandomIndex]; + IDrawingIlum ilum = (U) IlumArr[getExtraRandomIndex]; + + if (airbus instanceof EntityAdvancedAirbus) { + return new DrawingAdvancedAirbus(airbus, ilum); + } + return new DrawingAirbus(airbus, ilum); + } +} diff --git a/FormAirbus.java b/FormAirbus.java index 0c9ae24..28ecdf8 100644 --- a/FormAirbus.java +++ b/FormAirbus.java @@ -1,40 +1,15 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.*; import java.util.Random; public class FormAirbus extends JComponent { - private DrawingAirbus _plane; - private EntityAirbus _entity; - - public FormAirbus() - { - JFrame formFrame = new JFrame("Plane"); - formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - formFrame.setSize(840, 560); - formFrame.setVisible(true); - formFrame.setLocationRelativeTo(null); - - formFrame.addComponentListener(new ComponentListener() - { - @Override - public void componentResized(ComponentEvent e) - { - if (_plane != null) _plane.ChangeBorders(formFrame.getWidth(), formFrame.getHeight()); - repaint(); - } - - @Override - public void componentMoved(ComponentEvent e) {} - - @Override - public void componentShown(ComponentEvent e) {} - - @Override - public void componentHidden(ComponentEvent e) {} - - }); + private DrawingAirbus _airbus; + private DrawingAirbus SelectedAirbus; + public DrawingAirbus getSelectedAirbus() { + return SelectedAirbus; + } + public FormAirbus(JDialog caller) { Panel statusPanel = new Panel(); statusPanel.setBackground(Color.WHITE); statusPanel.setLayout(new GridLayout()); @@ -49,37 +24,61 @@ public class FormAirbus extends JComponent createButton.addActionListener(e -> { Random rand = new Random(); - _plane = new DrawingAirbus(rand.nextInt( 400 + rand.nextInt(200)), 1000 + rand.nextInt(1000), Color.getHSBColor(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256))); - _plane.SetPosition(10 + rand.nextInt(90), 10 + rand.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75); - speedLabel.setText("Speed: " + _plane.Airbus.getSpeed()); - weightLabel.setText("Weight: " + (int)_plane.Airbus.getWeight()); - colorLabel.setText("Color: " + _plane.Airbus.getBodyColor().getRed() + " " + _plane.Airbus.getBodyColor().getGreen() + " " + _plane.Airbus.getBodyColor().getBlue() ); + Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rand.nextInt(256), rand.nextInt(256),rand.nextInt(256))); + _airbus = new DrawingAirbus(rand.nextInt( 400 + rand.nextInt(200)), 1000 + rand.nextInt(1000), colorFirst); + _airbus.SetPosition(10 + rand.nextInt(90), 10 + rand.nextInt(90), 800, 500 - 75); + speedLabel.setText("Speed: " + _airbus.Airbus.getSpeed()); + weightLabel.setText("Weight: " + (int) _airbus.Airbus.getWeight()); + colorLabel.setText("Color: " + _airbus.Airbus.getBodyColor().getRed() + " " + _airbus.Airbus.getBodyColor().getGreen() + " " + _airbus.Airbus.getBodyColor().getBlue() ); repaint(); }); + JButton modifiedButton = new JButton("Modified"); + modifiedButton.addActionListener(e -> { + Random rand = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256))); + Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256))); + _airbus = new DrawingAdvancedAirbus(rand.nextInt(200) + 100, rand.nextInt(1000) + 1000, + colorFirst, + colorSecond, + rand.nextBoolean(), + rand.nextBoolean()); + _airbus.SetPosition(10 + rand.nextInt(90), 10 + rand.nextInt(90), 500, 500 - 75); + speedLabel.setText("Speed: " + _airbus.Airbus.getSpeed()); + weightLabel.setText("Weight: " + (int) _airbus.Airbus.getWeight()); + colorLabel.setText("Color: " + _airbus.Airbus.getBodyColor().getRed() + " " + _airbus.Airbus.getBodyColor().getGreen() + " " + _airbus.Airbus.getBodyColor().getBlue() ); + repaint(); + }); + JButton selectAirbusButton = new JButton("Select"); + selectAirbusButton.addActionListener(e -> { + SelectedAirbus = _airbus; + caller.dispose(); + }); + statusPanel.add(modifiedButton); statusPanel.add(createButton); statusPanel.add(speedLabel); statusPanel.add(weightLabel); statusPanel.add(colorLabel); + statusPanel.add(selectAirbusButton); JButton moveRightButton = new JButton(">"); moveRightButton.addActionListener(e -> { - if (_plane != null) _plane.MoveTransport(Direction.Right); + if (_airbus != null) _airbus.MoveTransport(Direction.Right); repaint(); }); JButton moveLeftButton = new JButton("<"); moveLeftButton.addActionListener(e -> { - if (_plane != null) _plane.MoveTransport(Direction.Left); + if (_airbus != null) _airbus.MoveTransport(Direction.Left); repaint(); }); JButton moveUpButton = new JButton("^"); moveUpButton.addActionListener(e -> { - if (_plane != null) _plane.MoveTransport(Direction.Up); + if (_airbus != null) _airbus.MoveTransport(Direction.Up); repaint(); }); JButton moveDownButton = new JButton("v"); moveDownButton.addActionListener(e -> { - if (_plane != null) _plane.MoveTransport(Direction.Down); + if (_airbus != null) _airbus.MoveTransport(Direction.Down); repaint(); }); @@ -88,13 +87,12 @@ public class FormAirbus extends JComponent statusPanel.add(moveUpButton); statusPanel.add(moveDownButton); - formFrame.getContentPane().add(this); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; - if (_plane != null) _plane.DrawTransport(g2); + if (_airbus != null) _airbus.DrawTransport(g2); super.repaint(); } } diff --git a/FormEntityWithExtraGallery.java b/FormEntityWithExtraGallery.java new file mode 100644 index 0000000..f6ba1b0 --- /dev/null +++ b/FormEntityWithExtraGallery.java @@ -0,0 +1,87 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; +public class FormEntityWithExtraGallery extends JComponent +{ + private DrawingAirbus _airbusFirst; + private DrawingAirbus _airbusSecond; + private DrawingAirbus _airbusThird; + EntityWithIlumCreator<EntityAirbus, IDrawingIlum> entityWithExtraCreator; + + public FormEntityWithExtraGallery() { + JFrame formFrame = new JFrame("Gallery"); + formFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + formFrame.setSize(900, 500); + formFrame.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new FlowLayout()); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.SOUTH); + + JButton showRandomEntity = new JButton("Create entity from parts"); + showRandomEntity.addActionListener(e -> { + + Random random = new Random(); + if (entityWithExtraCreator == null) { + entityWithExtraCreator = new EntityWithIlumCreator<EntityAirbus, IDrawingIlum>(20, 20); + for (int i = 0; i < 20; i ++) { + if (random.nextBoolean()) { + entityWithExtraCreator.Insert(new EntityAirbus(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + } + else { + entityWithExtraCreator.Insert(new EntityAdvancedAirbus(random.nextInt(100), random.nextInt(100), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)), + random.nextBoolean(), random.nextBoolean())); + } + } + for (int i = 0; i < 20; i ++) { + int extraRand = random.nextInt(3); + switch (extraRand) { + case 0: + entityWithExtraCreator.Insert(new DrawingIlum(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 1: + entityWithExtraCreator.Insert(new DrawingPlusIlum(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + + case 2: + entityWithExtraCreator.Insert(new DrawingSquareIlum(random.nextInt(3), + new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)))); + break; + } + } + } + _airbusFirst = entityWithExtraCreator.getEntityWithExtra(); + _airbusFirst.SetPosition(200, 200, formFrame.getWidth(), formFrame.getHeight() - 75); + + _airbusSecond = entityWithExtraCreator.getEntityWithExtra(); + _airbusSecond.SetPosition(400, 200, formFrame.getWidth(), formFrame.getHeight() - 75); + + _airbusThird = entityWithExtraCreator.getEntityWithExtra(); + _airbusThird.SetPosition(600, 200, formFrame.getWidth(), formFrame.getHeight() - 75); + repaint(); + }); + statusPanel.add(showRandomEntity); + + formFrame.getContentPane().add(this); + + formFrame.setVisible(true); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (_airbusFirst != null) _airbusFirst.DrawTransport(g2); + if (_airbusSecond != null) _airbusSecond.DrawTransport(g2); + if (_airbusThird != null) _airbusThird.DrawTransport(g2); + super.repaint(); + } +} diff --git a/FormMap.java b/FormMap.java index 6b24bc2..b580b24 100644 --- a/FormMap.java +++ b/FormMap.java @@ -73,6 +73,7 @@ public class FormMap extends JComponent{ if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawingObjectAirbus(airbus)); repaint(); }); + statusPanel.add(mapSelectComboBox); statusPanel.add(createButton); statusPanel.add(modifiedButton); @@ -80,25 +81,25 @@ public class FormMap extends JComponent{ statusPanel.add(weightLabel); statusPanel.add(colorLabel); - JButton moveDownButton = new JButton("Down"); + JButton moveDownButton = new JButton("v"); moveDownButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Down); repaint(); }); - JButton moveUpButton = new JButton("Up"); + JButton moveUpButton = new JButton("^"); moveUpButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Up); repaint(); }); - JButton moveLeftButton = new JButton("Left"); + JButton moveLeftButton = new JButton("<"); moveLeftButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Left); repaint(); }); - JButton moveRightButton = new JButton("Right"); + JButton moveRightButton = new JButton(">"); moveRightButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Right); repaint(); diff --git a/FormMapWithSetAirbus.java b/FormMapWithSetAirbus.java new file mode 100644 index 0000000..4875abb --- /dev/null +++ b/FormMapWithSetAirbus.java @@ -0,0 +1,202 @@ +import javax.swing.*; +import javax.swing.text.MaskFormatter; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.text.ParseException; + +public class FormMapWithSetAirbus extends JComponent{ + private BufferedImage bufferImg = null; + /// Объект от класса карты с набором объектов + private MapWithSetAirbusGeneric<DrawingObjectAirbus, AbstractMap> _mapAirbusCollectionGeneric; + public FormMapWithSetAirbus() { + JFrame formFrame = new JFrame("Form Map With SetAirbus"); + formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + formFrame.setSize(750, 500); + formFrame.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new GridLayout(0, 1, 20, 20)); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.EAST); + + // КомбоБокс с картами + String[] maps = { + "Simple Map", + "Sky Map", + }; + JComboBox mapSelectComboBox = new JComboBox(maps); + mapSelectComboBox.setEditable(true); + mapSelectComboBox.addActionListener(e -> { + AbstractMap map = null; + String item = (String)mapSelectComboBox.getSelectedItem(); + switch (item) { + case "Simple Map": + map = new SimpleMap(); + break; + case "Sky Map": + map = new SkyMap(); + break; + } + if (map != null) + { + _mapAirbusCollectionGeneric = new MapWithSetAirbusGeneric<DrawingObjectAirbus, AbstractMap> + (600, 500, map); + } + else + { + _mapAirbusCollectionGeneric = null; + } + }); + statusPanel.add(mapSelectComboBox); + + // Кнопка добавления аэробуса + JButton addAirbusButton = new JButton("Add Airbus"); + addAirbusButton.addActionListener(e -> { + // логика добавления + if (_mapAirbusCollectionGeneric == null) + { + return; + } + JDialog dialog = new JDialog(formFrame, "Dialog", true); + FormAirbus formAirbus = new FormAirbus(dialog); + dialog.setSize(800, 500); + dialog.setContentPane(formAirbus); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + DrawingObjectAirbus airbus = new DrawingObjectAirbus(formAirbus.getSelectedAirbus()); + if (_mapAirbusCollectionGeneric.Plus(airbus) != -1) { + JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION); + bufferImg = _mapAirbusCollectionGeneric.ShowSet(); + repaint(); + } + else { + JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION); + } + }); + statusPanel.add(addAirbusButton); + + // Текстовое поле для ввода позиции с маской + JFormattedTextField maskedTextFieldPosition = null; + try { + MaskFormatter positionMask = new MaskFormatter("##"); + maskedTextFieldPosition = new JFormattedTextField(positionMask); + statusPanel.add(maskedTextFieldPosition); + } + catch (ParseException e) { + e.printStackTrace(); + } + //Кнопка удаления аэробуса + JButton deleteAirbusButton = new JButton("Delete Airbus"); + JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition; + deleteAirbusButton.addActionListener(e -> { + // логика удаления + if ((String)mapSelectComboBox.getSelectedItem() == null) { + return; + } + if (finalMaskedTextFieldPosition == null) { + return; + } + int position = Integer.parseInt(finalMaskedTextFieldPosition.getText()); + if (_mapAirbusCollectionGeneric.Minus(position) != null) { + JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION); + bufferImg = _mapAirbusCollectionGeneric.ShowSet(); + repaint(); + } + else{ + JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION); + } + }); + statusPanel.add(deleteAirbusButton); + //Кнопка просмотра хранилища + JButton showStorageButton = new JButton("Show Storage"); + showStorageButton.addActionListener(e -> { + // логика просмотра + if (_mapAirbusCollectionGeneric == null) + { + return; + } + bufferImg = _mapAirbusCollectionGeneric.ShowSet(); + repaint(); + }); + statusPanel.add(showStorageButton); + //Кнопка просмотра карты + JButton showOnMapButton = new JButton("Show On Map"); + showOnMapButton.addActionListener(e -> { + // логика просмотра + if (_mapAirbusCollectionGeneric == null) + { + return; + } + bufferImg = _mapAirbusCollectionGeneric.ShowOnMap(); + }); + statusPanel.add(showOnMapButton); + + ActionListener moveButtonListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_mapAirbusCollectionGeneric == null) + { + return; + } + String name = e.getActionCommand(); + Direction dir = Direction.None; + switch (name) + { + case "Up": + dir = Direction.Up; + break; + case "Down": + dir = Direction.Down; + break; + case "Left": + dir = Direction.Left; + break; + case "Right": + dir = Direction.Right; + break; + } + bufferImg = _mapAirbusCollectionGeneric.MoveObject(dir); + } + }; + + //Кнопки управления + JButton moveDownButton = new JButton("Down"); + moveDownButton.addActionListener(moveButtonListener); + + JButton moveUpButton = new JButton("Up"); + moveUpButton.addActionListener(moveButtonListener); + + JButton moveLeftButton = new JButton("Left"); + moveLeftButton.addActionListener(moveButtonListener); + + JButton moveRightButton = new JButton("Right"); + moveRightButton.addActionListener(moveButtonListener); + + statusPanel.add(moveUpButton); + statusPanel.add(moveDownButton); + statusPanel.add(moveLeftButton); + statusPanel.add(moveRightButton); + + JButton showGalleryButton = new JButton("Show Gallery"); + showGalleryButton.addActionListener(e -> { + new FormEntityWithExtraGallery(); + }); + statusPanel.add(showGalleryButton); + + formFrame.getContentPane().add(this); + formFrame.setVisible(true); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (bufferImg != null) g2.drawImage(bufferImg, 0,0,600,500,null); + super.repaint(); + } + +} + diff --git a/MapWithSetAirbusGeneric.java b/MapWithSetAirbusGeneric.java new file mode 100644 index 0000000..312fcbb --- /dev/null +++ b/MapWithSetAirbusGeneric.java @@ -0,0 +1,141 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetAirbusGeneric <T extends IDrawingObject, U extends AbstractMap> +{ + /// Ширина окна отрисовки + private final int _pictureWidth; + /// Высота окна отрисовки + private final int _pictureHeight; + /// Размер занимаемого объектом места (ширина) + private final int _placeSizeWidth = 210; + /// Размер занимаемого объектом места (высота) + private final int _placeSizeHeight = 90; + + /// Набор объектов + private final SetAirbusGeneric<T> _setAirbus; + /// Карта + private final U _map; + /// Конструктор + public MapWithSetAirbusGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setAirbus = new SetAirbusGeneric<T>(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + /// Добавление + public int Plus(T airbus) + { + return this._setAirbus.Insert(airbus); + } + /// Удаление + public T Minus(int position) + { + return this._setAirbus.Remove(position); + } + /// Вывод всего набора объектов + public BufferedImage ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + DrawBackground((Graphics2D)gr); + DrawAirbus((Graphics2D)gr); + return bmp; + } + + /// Просмотр объекта на карте + public BufferedImage ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setAirbus.Count(); i++) + { + var airbus = _setAirbus.Get(i); + if (airbus != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, airbus); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + /// Перемещение объекта по крате + public BufferedImage MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + } + + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + private void Shaking() + { + int j = _setAirbus.Count() - 1; + for (int i = 0; i < _setAirbus.Count(); i++) + { + if (_setAirbus.Get(i) == null) + { + for (; j > i; j--) + { + var airbus = _setAirbus.Get(j); + if (airbus != null) + { + _setAirbus.Insert(airbus, i); + _setAirbus.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + /// Метод отрисовки фона + private void DrawBackground(Graphics2D g) + { + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.setColor(Color.BLACK); + g.drawLine( i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight); + g.setColor(Color.GRAY); + g.fillRect(i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight); + g.setColor(Color.YELLOW); + g.drawLine( i * _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2); + } + g.setColor(Color.BLACK); + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + g.setStroke(new BasicStroke(2)); + } + + // Метод прорисовки объектов + private void DrawAirbus(Graphics2D g) + { + int curWidth = _pictureWidth / _placeSizeWidth - 1; + int curHeight = _pictureHeight / _placeSizeHeight - 1; + + for (int i = 0; i < _setAirbus.Count(); i++) + { + if((_setAirbus.Get(i) != null)) + { + _setAirbus.Get(i).SetObject(curWidth * _placeSizeWidth + 30, curHeight * _placeSizeHeight + 40, _pictureWidth, _pictureHeight); + _setAirbus.Get(i).DrawingObject(g); + } + if (curWidth <= 0) { + curWidth = _pictureWidth / _placeSizeWidth - 1; + curHeight--; + } + else curWidth--; + } + } +} diff --git a/Program.java b/Program.java index ff7ede6..e840607 100644 --- a/Program.java +++ b/Program.java @@ -1,5 +1,5 @@ public class Program { public static void main(String[] args) { - new FormMap(); + new FormMapWithSetAirbus(); } } diff --git a/SetAirbusGeneric.java b/SetAirbusGeneric.java new file mode 100644 index 0000000..7306692 --- /dev/null +++ b/SetAirbusGeneric.java @@ -0,0 +1,60 @@ +public class SetAirbusGeneric <T> +{ + private final T[] _places; + + public int Count() { + return _places.length; + } + + public SetAirbusGeneric(int count) { + _places = (T[]) new Object[count]; + } + + public int Insert (T airbus) { + return Insert(airbus, 0); + } + + public int Insert (T airbus, int position) { + if (position >= _places.length || position < 0) return -1; + if (_places[position] == null) { + _places[position] = airbus; + return position; + } + + int emptyEl = -1; + for (int i = position + 1; i < Count(); i++) + { + if (_places[i] == null) + { + emptyEl = i; + break; + } + } + if (emptyEl == -1) + { + return -1; + } + for (int i = emptyEl; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = airbus; + return position; + } + + public T Remove (int position) { + if (position >= _places.length || position < 0) return null; + T result = _places[position]; + _places[position] = null; + return result; + } + + public T Get(int position) + { + if (position >= _places.length || position < 0) + { + return null; + } + return _places[position]; + } +}