diff --git a/DrawingAdvancedArtillery.java b/DrawingAdvancedArtillery.java index d7d8c79..33e26ee 100644 --- a/DrawingAdvancedArtillery.java +++ b/DrawingAdvancedArtillery.java @@ -6,6 +6,10 @@ public class DrawingAdvancedArtillery extends DrawingArtillery { artillery = new EntityAdvancedArtillery(speed, weight, bodyColor, dopColor, weapon, salvoBattery); } + public DrawingAdvancedArtillery(EntityAdvancedArtillery entity, IDrawingRollers rollers) { + super(entity, rollers); + } + @Override public void drawTransport(Graphics2D g) { if (!(artillery instanceof EntityAdvancedArtillery advancedArtillery)) { diff --git a/DrawingArtillery.java b/DrawingArtillery.java index 2d8c095..e62e52c 100644 --- a/DrawingArtillery.java +++ b/DrawingArtillery.java @@ -25,6 +25,11 @@ public class DrawingArtillery { _artilleryHeight = artilleryHeight; } + public DrawingArtillery(EntityArtillery entity, IDrawingRollers rollers) { + artillery = entity; + drawingRollers = rollers; + } + public void setPosition(int x, int y, int width, int height) { if (x < 0 || x + _artilleryWidth >= width) { diff --git a/EntityWithRollers.java b/EntityWithRollers.java new file mode 100644 index 0000000..5d992dd --- /dev/null +++ b/EntityWithRollers.java @@ -0,0 +1,43 @@ +import java.util.Random; + +public class EntityWithRollers { + static Random rnd = new Random(); + private final Object[] entities; + public int entitiesCount = 0; + private final Object[] rollers; + public int rollersCount = 0; + + public EntityWithRollers(int count) { + entities = new Object[count]; + rollers = new Object[count]; + } + + public boolean add(EntityArtillery entity) { + if (entitiesCount >= entities.length) { + return false; + } + entities[entitiesCount++] = entity; + return true; + } + + public boolean add(IDrawingRollers roller) { + if (rollersCount >= rollers.length) { + return false; + } + rollers[rollersCount++] = roller; + return true; + } + + public IDrawingObject constructArtillery() { + if (entitiesCount == 0 || rollersCount == 0) { + return null; + } + EntityArtillery entity = (EntityArtillery) entities[rnd.nextInt(0, entitiesCount)]; + IDrawingRollers roller = (IDrawingRollers) rollers[rnd.nextInt(0, rollersCount)]; + + if (entity instanceof EntityAdvancedArtillery advancedEntity) { + return new DrawingObjectArtillery(new DrawingAdvancedArtillery(advancedEntity, roller)); + } + return new DrawingObjectArtillery(new DrawingArtillery(entity, roller)); + } +} diff --git a/FormArtillery.form b/FormArtillery.form index d1e17c1..72a7427 100644 --- a/FormArtillery.form +++ b/FormArtillery.form @@ -53,7 +53,7 @@ - + @@ -61,11 +61,6 @@ - - - - - @@ -78,7 +73,7 @@ - + @@ -93,7 +88,7 @@ - + @@ -107,7 +102,7 @@ - + @@ -122,7 +117,7 @@ - + @@ -153,6 +148,19 @@ + + + + + + + + + + + + + diff --git a/FormArtillery.java b/FormArtillery.java index 69c29cd..4bf9cb8 100644 --- a/FormArtillery.java +++ b/FormArtillery.java @@ -1,12 +1,9 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; +import java.awt.event.*; import java.util.Random; -public class FormArtillery extends JFrame { +public class FormArtillery extends JDialog { private JPanel artilleryPane; private JLabel speedLabel; private JLabel weightLabel; @@ -18,23 +15,21 @@ public class FormArtillery extends JFrame { private JButton buttonLeft; private JButton buttonRight; private JButton createAdvancedButton; + public JButton buttonSelect; private DrawingArtillery _artillery; + private DrawingArtillery selectedArtillery; public FormArtillery() { this.setTitle("Artillery"); this.setContentPane(artilleryPane); createButton.addActionListener(e -> { Random rnd = new Random(); - _artillery = new DrawingArtillery( - rnd.nextInt(100, 300), - rnd.nextInt(1000, 2000), - new Color( - rnd.nextInt(0, 256), - rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), - rnd.nextInt(4, 7) - ); + Color color = JColorChooser.showDialog(this, "Цвет", new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256))); + if (color == null) { + color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + } + _artillery = new DrawingArtillery(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color, rnd.nextInt(4, 7)); _artillery.setPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); speedLabel.setText(String.format("Скорость: %s", _artillery.getArtillery().getSpeed())); weightLabel.setText(String.format("Вес: %s", _artillery.getArtillery().getWeight())); @@ -66,18 +61,20 @@ public class FormArtillery extends JFrame { }); createAdvancedButton.addActionListener(e -> { Random rnd = new Random(); + Color color = JColorChooser.showDialog(this, "Основной цвет", Color.white); + if (color == null) { + color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + } + Color dopColor = JColorChooser.showDialog(this, "Дополнительный цвет", Color.white); + if (dopColor == null) { + dopColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + } _artillery = new DrawingAdvancedArtillery( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - new Color( - rnd.nextInt(0, 256), - rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), + color, rnd.nextInt(4, 7), - new Color( - rnd.nextInt(0, 256), - rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), + dopColor, rnd.nextBoolean(), rnd.nextBoolean() ); @@ -87,6 +84,14 @@ public class FormArtillery extends JFrame { colorLabel.setText(String.format("Цвет: %x", _artillery.getArtillery().getBodyColor().getRGB())); repaint(); }); + buttonSelect.addActionListener(e -> { + selectedArtillery = _artillery; + dispose(); + }); + } + + public DrawingArtillery getSelectedArtillery() { + return selectedArtillery; } @Override diff --git a/FormGallery.form b/FormGallery.form new file mode 100644 index 0000000..60fed61 --- /dev/null +++ b/FormGallery.form @@ -0,0 +1,30 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormGallery.java b/FormGallery.java new file mode 100644 index 0000000..8344044 --- /dev/null +++ b/FormGallery.java @@ -0,0 +1,66 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormGallery extends JFrame { + private static final Random rnd = new Random(); + private JPanel contentPane; + private JButton buttonRefresh; + private JPanel pictureBox; + private IDrawingObject first; + private IDrawingObject second; + private IDrawingObject third; + + private final EntityWithRollers storage; + + public FormGallery() { + setTitle("Галлерея"); + setContentPane(contentPane); + + storage = new EntityWithRollers<>(20); + + for(int i = 0; i < 20; i++) { + if (rnd.nextBoolean()) { + storage.add(new EntityAdvancedArtillery( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextBoolean(), + rnd.nextBoolean() + )); + } else { + storage.add(new EntityArtillery( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)) + )); + } + storage.add(RollersType.random(rnd.nextInt(4, 7), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)))); + } + + buttonRefresh.addActionListener(e -> { + first = storage.constructArtillery(); + second = storage.constructArtillery(); + third = storage.constructArtillery(); + + first.setObject(0, 10, pictureBox.getWidth(), pictureBox.getHeight()); + second.setObject(90, 10, pictureBox.getWidth(), pictureBox.getHeight()); + third.setObject(190, 10, pictureBox.getWidth(), pictureBox.getHeight()); + + repaint(); + }); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + Graphics2D g2g = (Graphics2D) pictureBox.getGraphics(); + + if (first != null && second != null && third != null) { + first.drawingObject(g2g); + second.drawingObject(g2g); + third.drawingObject(g2g); + } + } +} diff --git a/FormMap.java b/FormMap.java deleted file mode 100644 index 465f70b..0000000 --- a/FormMap.java +++ /dev/null @@ -1,110 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.event.ItemEvent; -import java.awt.event.ItemListener; -import java.util.Random; - -public class FormMap extends JFrame { - private JPanel artilleryPane; - private JLabel speedLabel; - private JLabel weightLabel; - private JLabel colorLabel; - private JPanel pictureBox; - private JButton buttonUp; - private JButton buttonDown; - private JButton buttonLeft; - private JButton buttonRight; - private JButton createButton; - private JButton createAdvancedButton; - private JComboBox comboBoxSelectorMap; - - private AbstractMap _abstractMap; - private Image bufferedImage; - - public FormMap() { - this.setTitle("Artillery"); - this.setContentPane(artilleryPane); - - _abstractMap = new SimpleMap(); - - createButton.addActionListener(e -> { - Random rnd = new Random(); - var artillery = new DrawingArtillery( - rnd.nextInt(100, 300), - rnd.nextInt(1000, 2000), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - rnd.nextInt(4, 6 + 1) - ); - setData(artillery); - }); - buttonLeft.setForeground(new Color(0, 0, 0, 0)); - buttonRight.setForeground(new Color(0, 0, 0, 0)); - buttonUp.setForeground(new Color(0, 0, 0, 0)); - buttonDown.setForeground(new Color(0, 0, 0, 0)); - buttonLeft.addActionListener(e -> { - if (_abstractMap != null) { - bufferedImage = _abstractMap.moveObject(Direction.Left); - repaint(); - } - }); - buttonRight.addActionListener(e -> { - if (_abstractMap != null) { - bufferedImage = _abstractMap.moveObject(Direction.Right); - repaint(); - } - }); - buttonUp.addActionListener(e -> { - if (_abstractMap != null) { - bufferedImage = _abstractMap.moveObject(Direction.Up); - repaint(); - } - }); - buttonDown.addActionListener(e -> { - if (_abstractMap != null) { - bufferedImage = _abstractMap.moveObject(Direction.Down); - repaint(); - } - }); - createAdvancedButton.addActionListener(e -> { - Random rnd = new Random(); - var artillery = new DrawingAdvancedArtillery( - rnd.nextInt(100, 300), - rnd.nextInt(1000, 2000), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - rnd.nextInt(4, 6 + 1), - new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), - rnd.nextBoolean(), - rnd.nextBoolean() - ); - setData(artillery); - }); - comboBoxSelectorMap.addItemListener(e -> { - if (e.getStateChange() == ItemEvent.SELECTED) { - switch (e.getItem().toString()) { - case "Простая карта" -> _abstractMap = new SimpleMap(); - case "Лесная карта" -> _abstractMap = new ForestMap(); - } - } - }); - } - - private void setData(DrawingArtillery artillery) { - Random rnd = new Random(); - artillery.setPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight()); - speedLabel.setText(String.format("Скорость: %d", artillery.artillery.getSpeed())); - weightLabel.setText(String.format("Вес: %f", artillery.artillery.getWeight())); - colorLabel.setText(String.format("Цвет: %x", artillery.getArtillery().getBodyColor().getRGB())); - bufferedImage = _abstractMap.createMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawingObjectArtillery(artillery)); - repaint(); - } - - @Override - public void paint(Graphics g) { - super.paint(g); - - if (bufferedImage != null) { - pictureBox.paintComponents(bufferedImage.getGraphics()); - pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null); - } - } -} diff --git a/FormMap.form b/FormMapWithSetArtilleries.form similarity index 61% rename from FormMap.form rename to FormMapWithSetArtilleries.form index 62483a3..7cc983b 100644 --- a/FormMap.form +++ b/FormMapWithSetArtilleries.form @@ -1,61 +1,103 @@ -
- + + - + - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -68,19 +110,9 @@ - + - - - - - - - - - - - + @@ -92,9 +124,9 @@ - + - + @@ -107,9 +139,9 @@ - + - + @@ -122,35 +154,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/FormMapWithSetArtilleries.java b/FormMapWithSetArtilleries.java new file mode 100644 index 0000000..f29ca56 --- /dev/null +++ b/FormMapWithSetArtilleries.java @@ -0,0 +1,150 @@ +import javax.swing.*; +import javax.swing.text.DefaultFormatterFactory; +import javax.swing.text.MaskFormatter; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.text.ParseException; + +public class FormMapWithSetArtilleries extends JFrame { + private JPanel pictureBox; + private JPanel toolsGroup; + private JLabel toolsLabel; + private JComboBox comboBoxMapSelector; + private JButton buttonAddArtillery; + private JPanel paneArtilleries; + private JFormattedTextField textBoxPosition; + private JButton buttonRemoveArtillery; + private JButton buttonShowStorage; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + private JButton buttonShowOnMap; + + private Image bufferedImage; + private MapWithSetArtilleriesGeneric _mapArtilleriesCollectionGeneric; + + public FormMapWithSetArtilleries() { + this.setTitle("Artillery"); + this.setContentPane(paneArtilleries); + + try { + textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##"))); + } catch (ParseException e) { + e.printStackTrace(); + } + + comboBoxMapSelector.addActionListener(e -> { + AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) { + case "Простая карта" -> new SimpleMap(); + case "Лесная карта" -> new ForestMap(); + default -> null; + }; + + if (map != null) { + _mapArtilleriesCollectionGeneric = new MapWithSetArtilleriesGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map); + } else { + _mapArtilleriesCollectionGeneric = null; + } + }); + + buttonAddArtillery.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric == null) { + return; + } + + FormArtillery dialog = new FormArtillery(); + dialog.setSize(800, 500); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + + dialog.setVisible(true); + + if (dialog.getSelectedArtillery() != null) { + DrawingObjectArtillery car = new DrawingObjectArtillery(dialog.getSelectedArtillery()); + if (_mapArtilleriesCollectionGeneric.addArtillery(car) != -1) + { + JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); + repaint(); + } + else + { + JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE); + } + } + }); + + buttonRemoveArtillery.addActionListener(e -> { + String text = textBoxPosition.getText(); + if (text == null || text.isEmpty()) { + return; + } + + if (JOptionPane.showConfirmDialog(this, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { + return; + } + + int position = Integer.parseInt(text); + + if (_mapArtilleriesCollectionGeneric.removeArtilleryAt(position) != null) { + JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); + repaint(); + } else { + JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE); + } + }); + + buttonShowStorage.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric == null) { + return; + } + bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); + repaint(); + }); + + buttonShowOnMap.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric == null) { + return; + } + bufferedImage = _mapArtilleriesCollectionGeneric.showOnMap(); + repaint(); + }); + + buttonLeft.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric != null) { + bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Left); + repaint(); + } + }); + buttonRight.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric != null) { + bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Right); + repaint(); + } + }); + buttonUp.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric != null) { + bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Up); + repaint(); + } + }); + buttonDown.addActionListener(e -> { + if (_mapArtilleriesCollectionGeneric != null) { + bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Down); + repaint(); + } + }); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + + if (bufferedImage != null) { + pictureBox.paintComponents(bufferedImage.getGraphics()); + pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null); + } + } +} diff --git a/MapWithSetArtilleriesGeneric.java b/MapWithSetArtilleriesGeneric.java new file mode 100644 index 0000000..79fd53b --- /dev/null +++ b/MapWithSetArtilleriesGeneric.java @@ -0,0 +1,133 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetArtilleriesGeneric { + public final int _pictureWidth; + public final int _pictureHeight; + public final int _placeSizeWidth = 210; + public final int _placeSizeHeight = 90; + private final SetArtilleriesGeneric _setArtilleries; + private final U _map; + + public MapWithSetArtilleriesGeneric(int picWidth, int picHeight, U map) { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setArtilleries = new SetArtilleriesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int addArtillery(T artillery) { + return _setArtilleries.insert(artillery); + } + + public T removeArtilleryAt(int position) { + return _setArtilleries.remove(position); + } + + public Image showSet() { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = (Graphics2D) img.getGraphics(); + drawBackground(g2d); + drawArtilleries(g2d); + return img; + } + + public Image showOnMap() { + shaking(); + for (int i = 0; i < _setArtilleries.getCount(); i++) + { + var car = _setArtilleries.get(i); + if (car != null) + { + return _map.createMap(_pictureWidth, _pictureHeight, car); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + } + + public Image moveObject(Direction direction) { + if (_map != null) { + return _map.moveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + } + + private void shaking() { + int j = _setArtilleries.getCount() - 1; + for (int i = 0; i < _setArtilleries.getCount(); i++) + { + if (_setArtilleries.get(i) == null) + { + for (; j > i; j--) + { + var artillery = _setArtilleries.get(j); + if (artillery != null) + { + _setArtilleries.insert(artillery, i); + _setArtilleries.remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void drawBackground(Graphics2D g) { + Color pen = Color.black; + Color box = new Color(0, 100, 0, 255); + Color flag = Color.red; + Stroke normalStroke = new BasicStroke(1); + Stroke penStroke = new BasicStroke(3); + Stroke thinPenStroke = new BasicStroke(2); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.setColor(pen); + g.setStroke(penStroke); + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + + g.setColor(box); + g.fillRect(i * _placeSizeWidth + 5, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 - 5, _placeSizeWidth / 3 - 5, _placeSizeHeight / 3 - 5); + + g.setColor(pen); + g.setStroke(thinPenStroke); + g.drawLine(i * _placeSizeWidth + 5, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 - 5, i * _placeSizeWidth + _placeSizeWidth / 3, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 + _placeSizeHeight / 3 - 10); + g.drawLine(i * _placeSizeWidth + 5, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 + _placeSizeHeight / 3 - 10, i * _placeSizeWidth + _placeSizeWidth / 3, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 - 5); + + g.setColor(flag); + g.fillRect(i * _placeSizeWidth + _placeSizeWidth * 5 / 12, j * _placeSizeHeight + _placeSizeHeight * 5 / 8 - 5, _placeSizeWidth / 5, _placeSizeHeight / 5); + + g.setColor(pen); + g.setStroke(thinPenStroke); + g.drawLine(i * _placeSizeWidth + _placeSizeWidth * 5 / 12, j * _placeSizeHeight + _placeSizeHeight - 5, i * _placeSizeWidth + _placeSizeWidth * 5 / 12, j * _placeSizeHeight + _placeSizeHeight * 5 / 8 - 5); + } + g.setColor(pen); + g.setStroke(penStroke); + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + g.setStroke(normalStroke); + } + + private void drawArtilleries(Graphics2D g) { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _setArtilleries.getCount(); i++) + { + var artillery = _setArtilleries.get(i); + if (artillery != null) + { + artillery.setObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + artillery.drawingObject(g); + } + } + } +} diff --git a/Program.java b/Program.java index 8d8002d..5a3c167 100644 --- a/Program.java +++ b/Program.java @@ -2,8 +2,8 @@ import javax.swing.*; public class Program { public static void main(String[] args) { - FormMap form = new FormMap(); - form.setSize(640, 480); + FormGallery form = new FormGallery(); + form.setSize(320, 240); form.setVisible(true); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } diff --git a/SetArtilleriesGeneric.java b/SetArtilleriesGeneric.java new file mode 100644 index 0000000..77d1f52 --- /dev/null +++ b/SetArtilleriesGeneric.java @@ -0,0 +1,70 @@ +public class SetArtilleriesGeneric { + private final Object[] _places; + + public int getCount() { + return _places.length; + } + + public SetArtilleriesGeneric(int count) { + _places = new Object[count]; + } + + public int insert(T artillery) { + return insert(artillery, 0); + } + + public int insert(T artillery, int position) { + if (position < 0 || position >= getCount()) { + return -1; + } + + if (_places[position] == null) { + _places[position] = artillery; + return position; + } + + int firstNull = -1; + + for (int i = position + 1; i < getCount(); i++) + { + if (_places[i] == null) + { + firstNull = i; + break; + } + } + + if (firstNull == -1) + { + return -1; + } + + System.arraycopy(_places, position, _places, position + 1, firstNull - position); + + _places[position] = artillery; + return position; + } + + @SuppressWarnings("unchecked") + public T remove(int position) { + if (position < 0 || position >= getCount()) + { + return null; + } + + var result = _places[position]; + + _places[position] = null; + + return (T) result; + } + + @SuppressWarnings("unchecked") + public T get(int position) { + if (position < 0 || position >= getCount()) { + return null; + } + + return (T) _places[position]; + } +}