diff --git a/src/CreaterGeneric.java b/src/CreaterGeneric.java new file mode 100644 index 0000000..96a3212 --- /dev/null +++ b/src/CreaterGeneric.java @@ -0,0 +1,39 @@ +import java.util.Random; + +public class CreaterGeneric { + Object[] Warships; + Object[] Blocks; + int WarshipsCount=0; + int BlocksCount=0; + public CreaterGeneric(int warshipsCount,int blocksCount){ + Warships=new Object[warshipsCount]; + Blocks=new Object[blocksCount]; + } + public int AddWarship(T warship){ + if(WarshipsCount createrGeneric; + private DrawingObjectWarship _warship; + + public CreaterGenericField(FormCreater form){ + formCreater=form; + createrGeneric=new CreaterGeneric<>(40,40); + } + @Override + protected void paintComponent(Graphics g){ + super.paintComponent(g); + if(_warship!=null){ + _warship.DrawingObject(g); + } + } + public void Draw(Graphics g){ + if(_warship!=null){ + _warship.DrawingObject(g); + } + else return; + } + + public void BasicRadioButtonAction(int speed,int weight){ + Color color=JColorChooser.showDialog(formCreater,"Выберите цвет корпуса корабля",Color.WHITE); + if(speed==0 || weight==0 || color==null){ + return; + } + createrGeneric.AddWarship(new EntityWarship(speed,weight,color)); + Draw(formCreater.getGraphics()); + } + public void AdvancedRadioButtonAction(int speed,int weight, Boolean antenna, Boolean missile, Boolean helipad){ + Color color1=JColorChooser.showDialog(formCreater,"Выберите цвет корпуса корабля",Color.WHITE); + if(speed==0 || weight==0 || color1==null){ + return; + } + Color color2=JColorChooser.showDialog(formCreater,"Выберите цвет модификаций корабля",Color.WHITE); + if(color2==null){ + return; + } + createrGeneric.AddWarship(new EntityAdvancedWarship(speed,weight,color1,color2,helipad,antenna,missile)); + Draw(formCreater.getGraphics()); + } + public BlockCount TwoRadioButtonAction(){ + return BlockCount.TwoBlocks; + } + public BlockCount FourRadioButtonAction(){ + return BlockCount.FourBlocks; + } + public BlockCount SixRadioButtonAction(){ + return BlockCount.SixBlocks; + } + public void RectangleFormRadioButtonAction(BlockCount count){ + if(count==null){ + return; + } + IDrawingObjectBlock block=new DrawingBlock(count); + if(block==null) + return; + block.SetBlockCount(count.GetBlockCount()); + createrGeneric.AddBlock(block); + Draw(formCreater.getGraphics()); + } + public void RoundFormRadioButtonAction(BlockCount count){ + if(count==null){ + return; + } + IDrawingObjectBlock block=new DrawingRoundBlocks(count); + if(block==null) + return; + block.SetBlockCount(count.GetBlockCount()); + createrGeneric.AddBlock(block); + Draw(formCreater.getGraphics()); + } + public void TriangleFormRadioButtonAction(BlockCount count){ + if(count==null){ + return; + } + IDrawingObjectBlock block=new DrawingTriangleBlocks(count); + if(block==null) + return; + block.SetBlockCount(count.GetBlockCount()); + createrGeneric.AddBlock(block); + Draw(formCreater.getGraphics()); + } + public void ShowWarshipButtonAction(){ + Random rand=new Random(); + _warship=new DrawingObjectWarship(createrGeneric.NewWarshipCreate()); + _warship.SetObject(rand.nextInt(100),rand.nextInt(100),getWidth(),getHeight()); + Draw(formCreater.getGraphics()); + } + public void RemoveSelect(){ + _warship=null; + } + public DrawingObjectWarship GetWarship(){ + return _warship; + } +} diff --git a/src/DrawingAdvancedWarship.java b/src/DrawingAdvancedWarship.java index f92d3e6..cdaddb7 100644 --- a/src/DrawingAdvancedWarship.java +++ b/src/DrawingAdvancedWarship.java @@ -9,6 +9,11 @@ public class DrawingAdvancedWarship extends DrawingWarship { Blocks.SetBlockCount(2*(int)(Math.random()*3+1)); } + public DrawingAdvancedWarship(EntityWarship selectedWarship, IDrawingObjectBlock selectedBlock) { + super(selectedWarship,selectedBlock); + Warship=selectedWarship; + } + @Override public void DrawTransport(Graphics g) { diff --git a/src/DrawingDocks.java b/src/DrawingDocks.java new file mode 100644 index 0000000..40d0619 --- /dev/null +++ b/src/DrawingDocks.java @@ -0,0 +1,119 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class DrawingDocks extends JPanel { + private final FormMapWithSetWarships Dock; + private FormCreater formCreater; + private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric; + BufferedImage bufferedImage; + public DrawingDocks(FormMapWithSetWarships dock) { + Dock = dock; + formCreater=new FormCreater(Dock); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2=(Graphics2D)g; + g2.drawImage(bufferedImage,0,0,null); + } + public void DrawDock(Graphics g){ + Graphics2D g2=(Graphics2D)g; + g2.drawImage(bufferedImage,0,0,null); + } + + public void AddWarshipButtonAction(){ + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + formCreater.Open(); + DrawingObjectWarship warship = formCreater.GetField().GetWarship(); + if (_mapWarshipsCollectionGeneric.plus(warship)>=0) + { + JOptionPane.showMessageDialog(Dock,"Объект добавлен"); + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + DrawDock(Dock.getGraphics()); + } + else + { + JOptionPane.showMessageDialog(Dock,"Не удалось добавить объект"); + } + } + public void DeleteWarshipButtonAction(String count, JPanel panel){ + if (count==""||_mapWarshipsCollectionGeneric==null) + { + return; + } + int result = JOptionPane.showConfirmDialog( + panel, + "Удалить объект?", + "Удаление", + JOptionPane.YES_NO_CANCEL_OPTION); + if (result == JOptionPane.NO_OPTION) + { + return; + } + int pos = Integer.parseInt(count); + if (_mapWarshipsCollectionGeneric.minus(pos) !=null) + { + JOptionPane.showMessageDialog(Dock,"Объект удален"); + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + } + else + { + JOptionPane.showMessageDialog(Dock,"Не удалось удалить объект"); + } + } + + public void ShowStorageButtonAction() + { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); + DrawDock(Dock.getGraphics()); + } + + public void ShowOnMapButtonAction() + { + if (_mapWarshipsCollectionGeneric == null) + { + return; + } + bufferedImage = _mapWarshipsCollectionGeneric.ShowOnMap(); + DrawDock(Dock.getGraphics()); + } + + public void ComboBoxAction(String name,JPanel panel){ + AbstractMap map = null; + + switch (name) + { + case "Первая карта": + map = new SimpleMap(); + break; + case "Вторая карта": + map = new SecondMap(); + break; + } + if (map != null) + { + _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric( + panel.getWidth(), panel.getHeight(), map); + } + else + { + _mapWarshipsCollectionGeneric = null; + } + } + public void DirectionButtonAction(Direction side){ + if(_mapWarshipsCollectionGeneric != null){ + bufferedImage=_mapWarshipsCollectionGeneric.MoveObject(side); + } + DrawDock(Dock.getGraphics()); + } +} diff --git a/src/DrawingField.java b/src/DrawingField.java index 4569581..bd83c48 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,11 @@ public class DrawingField extends JPanel{ _warship.DrawTransport(graphics); else return; } + public DrawingWarship GetDrawingWarship() { + return _warship; + } + + public void ResetSelected() { + _warship=null; + } } 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..922c8b0 100644 --- a/src/DrawingWarship.java +++ b/src/DrawingWarship.java @@ -20,6 +20,10 @@ public class DrawingWarship { Blocks= GetFormOfBlock(blockForm); Blocks.SetBlockCount(2*(int)(Math.random()*3+1)); } + public DrawingWarship(EntityWarship selectedWarship, IDrawingObjectBlock selectedBlock) { + Warship=selectedWarship; + Blocks=selectedBlock; + } public IDrawingObjectBlock GetFormOfBlock(int FormOfBlock){ BlockForm temp = null; diff --git a/src/FormCreater.form b/src/FormCreater.form new file mode 100644 index 0000000..9380f81 --- /dev/null +++ b/src/FormCreater.form @@ -0,0 +1,253 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormCreater.java b/src/FormCreater.java new file mode 100644 index 0000000..8c6ad03 --- /dev/null +++ b/src/FormCreater.java @@ -0,0 +1,94 @@ +import javax.swing.*; +import java.awt.*; + +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; + CreaterGenericField field=new CreaterGenericField(this); + BlockCount block=null; + + public FormCreater(JFrame frame){ + super(frame,"Создание корабля"); + setContentPane(PictureBox); + setModal(true); + setSize(1200,700); + setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + ShowWindow(); + } + private void ShowWindow(){ + BasicRadioButton.addActionListener(e -> { + field.BasicRadioButtonAction(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText())); + ReDraw(); + }); + AdvancedRadioButton.addActionListener(e -> { + field.AdvancedRadioButtonAction(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),AntennaCheckBox.isSelected(),MissileCheckBox.isSelected(),HelipadCheckBox.isSelected()); + ReDraw(); + }); + TwoRadioButton.addActionListener(e -> { + block=field.TwoRadioButtonAction(); + ReDraw(); + }); + FourRadioButton.addActionListener(e -> { + block=field.FourRadioButtonAction(); + ReDraw(); + }); + SixRadioButton.addActionListener(e -> { + block=field.SixRadioButtonAction(); + ReDraw(); + }); + RectangleFormRadioButton.addActionListener(e -> { + field.RectangleFormRadioButtonAction(block); + ReDraw(); + }); + RoundFormRadioButton.addActionListener(e -> { + field.RoundFormRadioButtonAction(block); + ReDraw(); + }); + TriangleFormRadioButton.addActionListener(e -> { + field.TriangleFormRadioButtonAction(block); + ReDraw(); + }); + ShowWarshipButton.addActionListener(e -> { + field.ShowWarshipButtonAction(); + ReDraw(); + }); + ChooseButton.addActionListener(e -> { + Close(); + }); + } + public void Close(){ + setVisible(false); + } + public void Open(){ + field.RemoveSelect(); + setVisible(true); + } + public CreaterGenericField GetField(){ + return field; + } + public void ReDraw(){ + Graphics graphics = PictureBox.getGraphics(); + graphics.clearRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); + PictureBox.paintComponents(graphics); + field.Draw(graphics); + } +} 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..1a9fec9 --- /dev/null +++ b/src/FormMapWithSetWarships.form @@ -0,0 +1,136 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/FormMapWithSetWarships.java b/src/FormMapWithSetWarships.java new file mode 100644 index 0000000..235ba5a --- /dev/null +++ b/src/FormMapWithSetWarships.java @@ -0,0 +1,84 @@ + +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; + +public class FormMapWithSetWarships extends JFrame{ + DrawingDocks dock = new DrawingDocks(this); + 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 GroupBoxTools; + private JPanel PictureBox; + private JPanel MainPanel; + + public FormMapWithSetWarships(){ + super("Военный корабль"); + setContentPane(MainPanel); + setResizable(false); + setSize(1000,700); + ShowWindow(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + private void ShowWindow(){ + + ButtonShowOnMap.addActionListener(e -> { + dock.ShowOnMapButtonAction(); + ReDraw(); + }); + + ButtonShowStorage.addActionListener(e -> { + dock.ShowStorageButtonAction(); + ReDraw(); + }); + + ButtonAddWarship.addActionListener(e -> { + dock.AddWarshipButtonAction(); + ReDraw(); + }); + + ButtonRemoveWarship.addActionListener(e -> { + dock.DeleteWarshipButtonAction(TextBoxPosition.getText(),PictureBox); + ReDraw(); + }); + + СomboBoxSelectorMap.addActionListener(e -> { + dock.ComboBoxAction(СomboBoxSelectorMap.getSelectedItem().toString(),PictureBox); + ReDraw(); + }); + ButtonUp.addActionListener(e -> { + dock.DirectionButtonAction(Direction.Up); + ReDraw(); + }); + ButtonDown.addActionListener(e -> { + dock.DirectionButtonAction(Direction.Down); + ReDraw(); + }); + ButtonRight.addActionListener(e -> { + dock.DirectionButtonAction(Direction.Right); + ReDraw(); + }); + ButtonLeft.addActionListener(e -> { + dock.DirectionButtonAction(Direction.Left); + ReDraw(); + }); + } + public void ReDraw(){ + Graphics graphics = MainPanel.getGraphics(); + graphics.clearRect(0, 0, MainPanel.getWidth(), MainPanel.getHeight()); + MainPanel.paintComponents(graphics); + dock.DrawDock(graphics); + } +} 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..d9069ed --- /dev/null +++ b/src/MapWithSetWarshipsGeneric.java @@ -0,0 +1,121 @@ +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 BufferedImage ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,1); + Graphics gr = bmp.getGraphics(); + DrawBackground(gr); + DrawWarship(gr); + return bmp; + } + + public BufferedImage 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 BufferedImage 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; + g.setColor(Color.BLACK); + BasicStroke pen = new BasicStroke(5); + g.setStroke(pen); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + 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); + } + } + } + + 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]; + } +}