diff --git a/src/CreaterGeneric.java b/src/CreaterGeneric.java new file mode 100644 index 0000000..988034c --- /dev/null +++ b/src/CreaterGeneric.java @@ -0,0 +1,39 @@ +import java.util.ArrayList; +import java.util.Random; + +public class CreaterGeneric { + ArrayList Warships; + ArrayList Blocks; + int WarshipsCount=0; + int BlocksCount=0; + public CreaterGeneric(int warshipsCount,int blocksCount){ + Warships=new ArrayList<>(warshipsCount); + Blocks=new ArrayList<>(blocksCount); + } + public int Add(T warship){ + if(WarshipsCount<=Warships.size()){ + Warships.add(warship); + WarshipsCount++; + return WarshipsCount-1; + } + return -1; + } + public int Add(U block){ + if(BlocksCount<=Blocks.size()){ + Blocks.add(block); + BlocksCount++; + return BlocksCount-1; + } + return -1; + } + public DrawingWarship NewWarshipCreating() + { + Random rand=new Random(); + T warship = Warships.get(rand.nextInt(WarshipsCount)); + U block = Blocks.get(rand.nextInt(BlocksCount)); + if(warship instanceof EntityAdvancedWarship){ + return new DrawingAdvancedWarship(warship,block); + } + return new DrawingWarship(warship,block); + } +} diff --git a/src/DrawingAdvancedWarship.java b/src/DrawingAdvancedWarship.java index f92d3e6..2628b46 100644 --- a/src/DrawingAdvancedWarship.java +++ b/src/DrawingAdvancedWarship.java @@ -8,6 +8,15 @@ public class DrawingAdvancedWarship extends DrawingWarship { Blocks= GetFormOfBlock(blockForm); Blocks.SetBlockCount(2*(int)(Math.random()*3+1)); } + public DrawingAdvancedWarship(int speed, float weight, Color bodyColor, Color dopColor, boolean Helipad,boolean Antenna, boolean Missile,IDrawingObjectBlock blockForm) { + super(speed, weight, bodyColor, 120, 50); + Warship=new EntityAdvancedWarship(speed,weight,bodyColor,dopColor,Helipad,Antenna,Missile); + Blocks= blockForm; + } + public DrawingAdvancedWarship(EntityWarship warship,IDrawingObjectBlock block){ + super(warship,block); + Warship = warship; + } @Override public void DrawTransport(Graphics g) { diff --git a/src/DrawingField.java b/src/DrawingField.java index 4569581..35c9228 100644 --- a/src/DrawingField.java +++ b/src/DrawingField.java @@ -4,7 +4,9 @@ import java.util.Random; public class DrawingField extends JPanel{ private final FormWarship Field; - DrawingWarship _warship=null; + private DrawingWarship _warship=null; + + public DrawingWarship SelectedWarship; public DrawingField(FormWarship field) { Field = field; } @@ -51,4 +53,8 @@ public class DrawingField extends JPanel{ _warship.DrawTransport(graphics); else return; } + public DrawingWarship GetDrawingWarship() { + return _warship; + } + } diff --git a/src/DrawingMap.java b/src/DrawingMap.java deleted file mode 100644 index 150c1c9..0000000 --- a/src/DrawingMap.java +++ /dev/null @@ -1,65 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.util.Random; - -public class DrawingMap extends JPanel { - private final FormMap Map; - private AbstractMap _abstractMap; - BufferedImage bufferedImage; - - public DrawingMap(FormMap map){ - Map=map; - _abstractMap = new SimpleMap(); - } - - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - g.drawImage(bufferedImage,0,0,null); - } - - private void SetData(DrawingWarship warship) { - Random rand=new Random(); - warship.SetPosition(rand.nextInt(100)+10,rand.nextInt(100)+10,getWidth(),getHeight()); - Map.SpeedLabel.setText("Скорость: "+warship.GetWarship().GetSpeed()); - Map.WeightLabel.setText("Вес: "+warship.GetWarship().GetWeight()); - Map.BodyColorLabel.setText("Цвет: "+Integer.toHexString(warship.GetWarship().GetBodyColor().getRGB()).substring(2)); - bufferedImage = _abstractMap.CreateMap(700,550,new DrawingObjectWarship(warship)); - } - public void CreateButtonAction(){ - Random rand=new Random(); - Color color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - DrawingWarship warship=new DrawingWarship(rand.nextInt(50)+10,rand.nextInt(3000)+20000,color1,rand.nextInt(3)); - SetData(warship); - } - public void CreateModifButtonAction(){ - Random rand=new Random(); - Color color1=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - Color color2=new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); - DrawingWarship warship = new DrawingAdvancedWarship(rand.nextInt(50) + 10, rand.nextInt(3000) + 20000, color1, - color2, rand.nextBoolean(), rand.nextBoolean(), rand.nextBoolean(),rand.nextInt(3)); - SetData(warship); - } - public void DirectionButtonAction(Direction side){ - if(_abstractMap != null){ - bufferedImage = _abstractMap.MoveObject(side); - } - } - - public void ComboBoxSelectorMapAction(String name){ - switch (name){ - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Вторая карта": - _abstractMap = new SecondMap(); - break; - } - } - - public void DrawMap(Graphics g){ - g.drawImage(bufferedImage,0,0,null); - } - -} diff --git a/src/DrawingWarship.java b/src/DrawingWarship.java index 12244e8..7496ee8 100644 --- a/src/DrawingWarship.java +++ b/src/DrawingWarship.java @@ -20,6 +20,15 @@ public class DrawingWarship { Blocks= GetFormOfBlock(blockForm); Blocks.SetBlockCount(2*(int)(Math.random()*3+1)); } + public DrawingWarship(int speed, float weight, Color bodyColor, IDrawingObjectBlock blockForm) + { + Warship = new EntityWarship(speed, weight, bodyColor); + Blocks= blockForm; + } + public DrawingWarship(EntityWarship warship,IDrawingObjectBlock block){ + Warship = warship; + Blocks = block; + } public IDrawingObjectBlock GetFormOfBlock(int FormOfBlock){ BlockForm temp = null; diff --git a/src/FormCreater.form b/src/FormCreater.form new file mode 100644 index 0000000..03bf2c9 --- /dev/null +++ b/src/FormCreater.form @@ -0,0 +1,258 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormCreater.java b/src/FormCreater.java new file mode 100644 index 0000000..14158b7 --- /dev/null +++ b/src/FormCreater.java @@ -0,0 +1,132 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class FormCreater extends JDialog{ + private JLabel SetSpeedLabel; + private JPanel PropertiesPanel; + private JTextField SpeedTextField; + private JLabel SetWeightLabel; + private JTextField WeightTextField; + private JRadioButton BasicRadioButton; + private JRadioButton AdvancedRadioButton; + private JCheckBox MissileCheckBox; + private JCheckBox AntennaCheckBox; + private JCheckBox HelipadCheckBox; + private JPanel CargoPanel; + private JRadioButton TriangleFormRadioButton; + private JRadioButton RoundFormRadioButton; + private JRadioButton RectangleFormRadioButton; + private JPanel CountOfBlocksPanel; + private JRadioButton SixRadioButton; + private JRadioButton FourRadioButton; + private JRadioButton TwoRadioButton; + private JButton ShowWarshipButton; + private JButton ChooseButton; + private JPanel PictureBox; + BlockCount block=null; + IDrawingObjectBlock fblock=null; + private final CreaterGeneric createrGeneric=new CreaterGeneric<>(40,40); + private DrawingWarship _warship; + private DrawingWarship selectedWarship; + + public FormCreater(){ + setTitle("Военный корабль"); + setContentPane(PictureBox); + setResizable(false); + setSize(1000,500); + ShowWindow(); + setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + Graphics2D g2d = (Graphics2D) PictureBox.getGraphics(); + if (_warship != null) { + _warship.DrawTransport(g2d); + } + } + private void ShowWindow(){ + + TwoRadioButton.addActionListener(e -> { + block=BlockCount.TwoBlocks; + }); + FourRadioButton.addActionListener(e -> { + block=BlockCount.FourBlocks; + }); + SixRadioButton.addActionListener(e -> { + block=BlockCount.SixBlocks; + }); + + RectangleFormRadioButton.addActionListener(e -> { + if(block==null){ + return; + } + fblock=new DrawingBlock(block); + if(fblock==null) + return; + fblock.SetBlockCount(block.GetBlockCount()); + createrGeneric.Add(fblock); + }); + + RoundFormRadioButton.addActionListener(e -> { + if(block==null){ + return; + } + fblock=new DrawingRoundBlocks(block); + if(fblock==null) + return; + fblock.SetBlockCount(block.GetBlockCount()); + createrGeneric.Add(fblock); + }); + + TriangleFormRadioButton.addActionListener(e -> { + if(block==null){ + return; + } + fblock=new DrawingTriangleBlocks(block); + if(fblock==null) + return; + fblock.SetBlockCount(block.GetBlockCount()); + createrGeneric.Add(fblock); + }); + + BasicRadioButton.addActionListener(e -> { + Color color=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE); + if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color==null){ + return; + } + createrGeneric.Add(new EntityWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color)); + }); + + AdvancedRadioButton.addActionListener(e -> { + Color color1=JColorChooser.showDialog(this,"Выберите цвет корпуса корабля",Color.WHITE); + if(Integer.parseInt(SpeedTextField.getText())==0 || Integer.parseInt(WeightTextField.getText())==0 || color1==null){ + return; + } + Color color2=JColorChooser.showDialog(this,"Выберите цвет модификаций корабля",Color.WHITE); + if(color2==null){ + return; + } + createrGeneric.Add(new EntityAdvancedWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()), + color1,color2,HelipadCheckBox.isSelected(),AntennaCheckBox.isSelected(),MissileCheckBox.isSelected())); + }); + + ShowWarshipButton.addActionListener(e -> { + Random rand=new Random(); + _warship=createrGeneric.NewWarshipCreating(); + _warship.SetPosition(rand.nextInt(100),rand.nextInt(100),getWidth(),getHeight()); + repaint(); + }); + ChooseButton.addActionListener(e -> { + selectedWarship=_warship; + dispose(); + }); + } + public DrawingWarship getSelectedWarship() { + return selectedWarship; + } +} diff --git a/src/FormMap.form b/src/FormMap.form deleted file mode 100644 index ebc0e7f..0000000 --- a/src/FormMap.form +++ /dev/null @@ -1,175 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/src/FormMap.java b/src/FormMap.java deleted file mode 100644 index 603f457..0000000 --- a/src/FormMap.java +++ /dev/null @@ -1,76 +0,0 @@ -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.util.Objects; - -public class FormMap extends JFrame { - private int Width; - private int Height; - DrawingMap map = new DrawingMap(this); - private JButton ButtonDown; - private JButton ButtonRight; - private JButton ButtonLeft; - private JButton ButtonUp; - private JButton ButtonCreate; - private JButton ButtonCreateModif; - public JLabel SpeedLabel; - public JLabel WeightLabel; - public JLabel BodyColorLabel; - private JComboBox ComboBoxSelectorMap; - private JPanel PictureBox; - - public FormMap(){ - super("Военный корабль"); - setContentPane(PictureBox); - setSize(1000,700); - Width = getWidth(); - Height = getHeight(); - ShowWindow(); - map.setBounds(0,0,Width,Height); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setVisible(true); - } - - private void ShowWindow(){ - ButtonCreate.addActionListener(e -> { - map.CreateButtonAction(); - ReDraw(); - }); - ButtonCreateModif.addActionListener(e -> { - map.CreateModifButtonAction(); - ReDraw(); - }); - ButtonUp.addActionListener(e -> { - map.DirectionButtonAction(Direction.Up); - ReDraw(); - }); - ButtonLeft.addActionListener(e -> { - map.DirectionButtonAction(Direction.Left); - ReDraw(); - }); - ButtonRight.addActionListener(e -> { - map.DirectionButtonAction(Direction.Right); - ReDraw(); - }); - ButtonDown.addActionListener(e -> { - map.DirectionButtonAction(Direction.Down); - ReDraw(); - }); - - ComboBoxSelectorMap.addActionListener(e -> { - map.ComboBoxSelectorMapAction(Objects.requireNonNull(ComboBoxSelectorMap.getSelectedItem()).toString()); - ReDraw(); - }); - } - private void ReDraw() - { - Graphics2D graphics = (Graphics2D) PictureBox.getGraphics(); - graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); - PictureBox.paintComponents(graphics); - map.DrawMap(graphics); - } - -} diff --git a/src/FormMapWithSetWarships.form b/src/FormMapWithSetWarships.form new file mode 100644 index 0000000..4e1319a --- /dev/null +++ b/src/FormMapWithSetWarships.form @@ -0,0 +1,146 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/FormMapWithSetWarships.java b/src/FormMapWithSetWarships.java new file mode 100644 index 0000000..d8da024 --- /dev/null +++ b/src/FormMapWithSetWarships.java @@ -0,0 +1,165 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; + +public class FormMapWithSetWarships extends JFrame{ + + private JComboBox СomboBoxSelectorMap; + private JButton ButtonAddWarship; + private JTextField TextBoxPosition; + private JButton ButtonRemoveWarship; + private JButton ButtonShowStorage; + private JButton ButtonShowOnMap; + private JButton ButtonDown; + private JButton ButtonUp; + private JButton ButtonLeft; + private JButton ButtonRight; + private JPanel PictureBox; + private JPanel MainPanel; + private JPanel GroupBoxTools; + private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric; + private Image bufferedImage; + + public FormMapWithSetWarships(){ + setTitle("Военный корабль"); + setContentPane(MainPanel); + setResizable(false); + setSize(1000,685); + ShowWindow(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + + if (bufferedImage != null) { + PictureBox.paintComponents(bufferedImage.getGraphics()); + PictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null); + } + } + + private void ShowWindow(){ + + ButtonShowOnMap.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap(); + repaint(); + }); + + ButtonShowStorage.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + repaint(); + }); + + ButtonAddWarship.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + FormCreater dialog=new FormCreater(); + dialog.setSize(1200,700); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + if (dialog.getSelectedWarship()!=null) { + DrawingObjectWarship warship = new DrawingObjectWarship(dialog.getSelectedWarship()); + + if (_mapWarshipsCollectionGeneric.plus(warship) >= 0) { + JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + repaint(); + } else { + JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Ошибка",JOptionPane.INFORMATION_MESSAGE); + } + } + }); + + ButtonRemoveWarship.addActionListener(e -> { + String txt=TextBoxPosition.getText(); + if (txt==null||_mapWarshipsCollectionGeneric==null||txt.isEmpty()) + { + return; + } + int result = JOptionPane.showConfirmDialog( + this, + "Удалить объект?", + "Удаление", + JOptionPane.YES_NO_CANCEL_OPTION); + if (result == JOptionPane.NO_OPTION) + { + return; + } + int pos = Integer.parseInt(txt); + if (_mapWarshipsCollectionGeneric.minus(pos) !=null) + { + JOptionPane.showMessageDialog(this,"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + repaint(); + } + else + { + JOptionPane.showMessageDialog(this,"Не удалось удалить объект","Ошибка",JOptionPane.INFORMATION_MESSAGE); + } + }); + + СomboBoxSelectorMap.addActionListener(e -> { + AbstractMap map = null; + + switch (СomboBoxSelectorMap.getSelectedItem().toString()) + { + case "Первая карта": + map = new SimpleMap(); + break; + case "Вторая карта": + map = new SecondMap(); + break; + } + if (map != null) + { + _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric<>( + PictureBox.getWidth(), PictureBox.getHeight(), map); + } + else + { + _mapWarshipsCollectionGeneric = null; + } + }); + + ButtonUp.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric != null) { + bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Up); + repaint(); + } + }); + + ButtonDown.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric != null) { + bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Down); + repaint(); + } + }); + + ButtonRight.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric != null) { + bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Right); + repaint(); + } + }); + + ButtonLeft.addActionListener(e -> { + if (_mapWarshipsCollectionGeneric != null) { + bufferedImage = _mapWarshipsCollectionGeneric.MoveObject(Direction.Left); + repaint(); + } + }); + } +} diff --git a/src/FormWarship.form b/src/FormWarship.form index 741a544..f21f8c5 100644 --- a/src/FormWarship.form +++ b/src/FormWarship.form @@ -1,9 +1,9 @@
- + - + @@ -11,7 +11,7 @@ - + @@ -50,7 +50,7 @@ - + @@ -132,7 +132,7 @@ - + @@ -159,6 +159,24 @@ + + + + + + + + + + + + + + + + + + diff --git a/src/FormWarship.java b/src/FormWarship.java index 08b77e7..3cf0105 100644 --- a/src/FormWarship.java +++ b/src/FormWarship.java @@ -3,10 +3,11 @@ import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; -public class FormWarship extends JFrame{ +public class FormWarship extends JDialog{ private int Width; private int Height; DrawingField field = new DrawingField(this); + DrawingWarship SelectedWarship; private JButton ButtonDown; private JButton ButtonRight; private JButton ButtonLeft; @@ -17,16 +18,17 @@ public class FormWarship extends JFrame{ public JLabel WeightLabel; public JLabel BodyColorLabel; private JPanel PictureBox; + private JButton ButtonSelect; - public FormWarship(){ - super("Военный корабль"); + public FormWarship(JFrame frame){ + super(frame,"Военный корабль"); setContentPane(PictureBox); setSize(1000,700); Width = getWidth(); Height = getHeight(); ShowWindow(); field.setBounds(0,0,Width,Height); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); setVisible(true); } @@ -56,6 +58,10 @@ public class FormWarship extends JFrame{ field.DirectionButtonAction(Direction.Down); ReDraw(); }); + ButtonSelect.addActionListener(e -> { + SelectedWarship=field.GetDrawingWarship(); + JOptionPane.showMessageDialog(PictureBox, "Корабль добавлен."); + }); addComponentListener(new ComponentAdapter() { @Override @@ -70,8 +76,7 @@ public class FormWarship extends JFrame{ } }); } - private void ReDraw() - { + private void ReDraw() { Graphics2D graphics = (Graphics2D) PictureBox.getGraphics(); graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); PictureBox.paintComponents(graphics); diff --git a/src/Main.java b/src/Main.java index 7b927cf..6546bf6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - new FormMap(); + new FormMapWithSetWarships(); } } \ No newline at end of file diff --git a/src/MapWithSetWarshipsGeneric.java b/src/MapWithSetWarshipsGeneric.java new file mode 100644 index 0000000..dee5f53 --- /dev/null +++ b/src/MapWithSetWarshipsGeneric.java @@ -0,0 +1,126 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetWarshipsGeneric { + private final int _pictureWidth; + private final int _pictureHeight; + private final int _placeSizeWidth = 120; + private final int _placeSizeHeight = 50; + private final SetWarshipsGeneric _setWarship; + private final U _map; + + public MapWithSetWarshipsGeneric(int picWidth,int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight/_placeSizeHeight; + _setWarship = new SetWarshipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int plus(T warship) + { + return _setWarship.Insert(warship); + } + + public T minus(int position) + { + return _setWarship.Remove(position); + } + + public Image ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,BufferedImage.TYPE_INT_ARGB); + Graphics gr = bmp.getGraphics(); + DrawBackground(gr); + DrawWarship(gr); + return bmp; + } + + public Image ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setWarship.Count(); i++) + { + T warship = _setWarship.Get(i); + if (warship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight,1); + } + + public Image MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight,1); + } + + public void Shaking() + { + int j = _setWarship.Count() - 1; + for (int i = 0; i < _setWarship.Count(); i++) + { + if (_setWarship.Get(i) == null) + { + for (; j > i; j--) + { + T warship = _setWarship.Get(j); + if (warship != null) + { + _setWarship.Insert(warship, i); + _setWarship.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics gr) + { + Graphics2D g=(Graphics2D)gr; + + Color brush=Color.BLACK; + Stroke penWide = new BasicStroke(5); + Stroke penThin = new BasicStroke(1); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.setColor(brush); + g.setStroke(penWide); + g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2); + g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + _placeSizeHeight/2+2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight/2+2); + g.drawLine(i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2); + g.drawLine(i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth+ (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight); + g.setStroke(penThin); + } + } + } + + private void DrawWarship(Graphics gr) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _setWarship.Count(); i++) + { + if (_setWarship.Get(i) != null) + { + _setWarship.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setWarship.Get(i).DrawingObject(gr); + } + } + } +} diff --git a/src/SetWarshipsGeneric.java b/src/SetWarshipsGeneric.java new file mode 100644 index 0000000..ab5ca68 --- /dev/null +++ b/src/SetWarshipsGeneric.java @@ -0,0 +1,68 @@ +public class SetWarshipsGeneric { + private final Object[] _places; + + public int Count() { + return _places.length; + } + + public SetWarshipsGeneric(int count) + { + _places = new Object[count]; + } + + public int Insert(T warship) + { + return Insert(warship,0); + } + + public int Insert(T warship, int position) + { + int EmptyEl=-1; + + if (position>=Count() || position < 0) + return -1; + + if (_places[position] == null) + { + _places[position] = warship; + return 1; + } + + else if (_places[position] != null) + { + 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] = warship; + return 1; + } + + public T Remove(int position) + { + if (position >= Count() || position < 0 || _places[position]==null) + return null; + + T deleted =(T)_places[position]; + _places[position] = null; + return deleted; + } + + public T Get(int position) + { + if (position >= Count() || position < 0) + return null; + + return (T)_places[position]; + } +}