From deba91e044413bce3b7e04fefcc8e12004ebe432 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Tue, 15 Nov 2022 17:46:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=85=20?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE=D0=BC=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FormMapWithSetWarships.java | 42 +++----- src/FormWarshipCreator.form | 155 +++++++++++++++-------------- src/FormWarshipCreator.java | 138 +++++++++++++------------ src/MapWithSetWarshipsGeneric.java | 24 ++--- src/SetWarshipsGeneric.java | 14 +-- src/WarshipCreatorGeneric.java | 23 +++-- 6 files changed, 202 insertions(+), 194 deletions(-) diff --git a/src/FormMapWithSetWarships.java b/src/FormMapWithSetWarships.java index f5ca3d8..3f18958 100644 --- a/src/FormMapWithSetWarships.java +++ b/src/FormMapWithSetWarships.java @@ -1,7 +1,5 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; public class FormMapWithSetWarships extends JFrame{ private JPanel mainPanel; @@ -36,7 +34,7 @@ public class FormMapWithSetWarships extends JFrame{ public void InitializeComponent(){ setContentPane(mainPanel); setTitle("Warship"); - setSize(1000, 685); + setSize(1000, 693); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); @@ -49,32 +47,24 @@ public class FormMapWithSetWarships extends JFrame{ Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg"); ButtonRight.setIcon(iconRight); - СomboBoxSelectorMap.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - AbstractMap map = null; - switch (СomboBoxSelectorMap.getSelectedItem().toString()){ - case "Простая карта": - map = new SimpleMap(); - break; - case "Преграды-линии": - map = new LineMap(); - break; - } - if( map != null){ - _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric( - PictureBox.getWidth(), PictureBox.getHeight(), map); - } - else - { - _mapWarshipsCollectionGeneric = null; - } + СomboBoxSelectorMap.addActionListener(e -> { + AbstractMap map = switch (СomboBoxSelectorMap.getSelectedItem().toString()) { + case "Простая карта" -> new SimpleMap(); + case "Преграды-линии" -> new LineMap(); + default -> null; + }; + if( map != null){ + _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric( + PictureBox.getWidth(), PictureBox.getHeight(), map); + } + else + { + _mapWarshipsCollectionGeneric = null; } }); ButtonAddWarship.addActionListener(e -> { if(_mapWarshipsCollectionGeneric == null){ - System.out.println("null"); return; } FormWarshipCreator warshipCreator = new FormWarshipCreator(); @@ -85,7 +75,7 @@ public class FormMapWithSetWarships extends JFrame{ if (warshipCreator.getSelectedWarship() != null) { DrawingObjectWarship warship = new DrawingObjectWarship(warshipCreator.getSelectedWarship()); - if (_mapWarshipsCollectionGeneric.add(warship) >= 0) { + if (_mapWarshipsCollectionGeneric.Plus(warship) >= 0) { JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); bufferedImage = _mapWarshipsCollectionGeneric.ShowSet(); @@ -113,7 +103,7 @@ public class FormMapWithSetWarships extends JFrame{ return; } int pos = Integer.parseInt(text); - if (_mapWarshipsCollectionGeneric.remove(pos) != null) + if (_mapWarshipsCollectionGeneric.Minus(pos) != null) { JOptionPane.showMessageDialog(this, "Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE); diff --git a/src/FormWarshipCreator.form b/src/FormWarshipCreator.form index aa52652..bcd4f74 100644 --- a/src/FormWarshipCreator.form +++ b/src/FormWarshipCreator.form @@ -8,7 +8,7 @@ - + @@ -16,53 +16,22 @@ - + - - - - - - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + @@ -70,8 +39,8 @@ - @@ -79,18 +48,51 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -118,72 +120,75 @@ - + + + + + + + + + + + + + + + + - + - + - + - + - - - - - - - - - - - - - - + - + - + - - + - - + - diff --git a/src/FormWarshipCreator.java b/src/FormWarshipCreator.java index f08b869..880517b 100644 --- a/src/FormWarshipCreator.java +++ b/src/FormWarshipCreator.java @@ -4,29 +4,30 @@ import java.util.Random; public class FormWarshipCreator extends JDialog{ private JPanel PictureBox; - private JPanel PropertiesPanel; - private JRadioButton BasicRadioButton; - private JRadioButton AdvancedRadioButton; - private JTextField WeightTextField; - private JTextField SpeedTextField; - private JPanel CargoPanel; - private JRadioButton TriangleFormRadioButton; + private JPanel SettingsPanel; + private JPanel BlocksTypePanel; + private JPanel CountOfBlocksPanel; + private JPanel ModificationPanel; + private JPanel TypePanel; + private JRadioButton RoundRectangleFormRadioButton; 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 JCheckBox MissileCheckBox; - private JCheckBox AntennaCheckBox; - private JCheckBox HelipadCheckBox; - BlockCount _block = null; - IDrawingObjectBlock objectBlock = null; - private final WarshipCreatorGeneric createrGeneric=new WarshipCreatorGeneric<>(40,40); + private JButton CreateButton; + private JButton SelectButton; + private JCheckBox EngineCheckBox; + private JCheckBox CabinCheckBox; + private JCheckBox AreaCheckBox; + private JComboBox comboBoxType; + private BlockCount _block = null; + private IDrawingObjectBlock objectBlock = null; + private final WarshipCreatorGeneric warshipCreator =new WarshipCreatorGeneric<>(40,40); private DrawingWarship _warship; private DrawingWarship selectedWarship; + private boolean isModified = false; + public FormWarshipCreator(){ InitializeComponent(); @@ -49,80 +50,91 @@ public class FormWarshipCreator extends JDialog{ setContentPane(PictureBox); setTitle("Warship"); setResizable(false); - setSize(900, 700); + setSize(1000, 600); + //Определение типа объекта + comboBoxType.addItemListener(e -> isModified = e.getItem().toString().equals("AircraftCarrier")); + + //количество блоков TwoRadioButton.addActionListener(e -> { - _block =BlockCount.TwoBlocks; + _block = BlockCount.TwoBlocks; }); FourRadioButton.addActionListener(e -> { - _block =BlockCount.FourBlocks; + _block = BlockCount.FourBlocks; }); SixRadioButton.addActionListener(e -> { - _block =BlockCount.SixBlocks; + _block = BlockCount.SixBlocks; }); + //Квадратная форма блоков RectangleFormRadioButton.addActionListener(e -> { - if(_block ==null){ - return; - } - objectBlock =new DrawingBlocks(_block); - if(objectBlock ==null) - return; + if(_block == null) return; + + objectBlock = new DrawingBlocks(_block); objectBlock.SetBlockCount(_block.GetBlockCount()); - createrGeneric.AddBlock(objectBlock); + warshipCreator.AddBlock(objectBlock); }); + //Круглая форма блоков RoundFormRadioButton.addActionListener(e -> { if(_block ==null){ return; } - objectBlock =new DrawingRoundBlocks(_block); - if(objectBlock ==null) - return; + objectBlock = new DrawingRoundBlocks(_block); objectBlock.SetBlockCount(_block.GetBlockCount()); - createrGeneric.AddBlock(objectBlock); + warshipCreator.AddBlock(objectBlock); }); - TriangleFormRadioButton.addActionListener(e -> { - if(_block ==null){ + //Закруглено-квадратная форма блоков + RoundRectangleFormRadioButton.addActionListener(e -> { + if(_block == null){ return; } - objectBlock =new DrawingRoundBlocks(_block); - if(objectBlock ==null) - return; + objectBlock = new DrawingRoundBlocks(_block); objectBlock.SetBlockCount(_block.GetBlockCount()); - createrGeneric.AddBlock(objectBlock); + warshipCreator.AddBlock(objectBlock); }); - 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.AddWarship(new EntityWarship(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()),color)); - }); + CreateButton.addActionListener(e -> { + Random rnd = new Random(); + if (_block != null && objectBlock != null) { + Color color = JColorChooser.showDialog(this, "Choose color", Color.WHITE); - 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.AddWarship(new EntityAircraftCarrier(Integer.parseInt(SpeedTextField.getText()),Integer.parseInt(WeightTextField.getText()), - color1,color2,HelipadCheckBox.isSelected(),AntennaCheckBox.isSelected(),MissileCheckBox.isSelected())); - }); + if (isModified) { + Color additionalColor = JColorChooser.showDialog(this, "Choose color", Color.GRAY); - ShowWarshipButton.addActionListener(e -> { - Random rand=new Random(); - _warship=createrGeneric.NewWarshipCreating(); - _warship.SetPosition(rand.nextInt(100),rand.nextInt(100),getWidth(),getHeight()); - repaint(); + warshipCreator.AddWarship( + new EntityAircraftCarrier( + rnd.nextInt(10, 300), + rnd.nextFloat(10, 3000), + color, + additionalColor, + AreaCheckBox.isSelected(), + CabinCheckBox.isSelected(), + EngineCheckBox.isSelected() + ) + ); + } + else { + warshipCreator.AddWarship( + new EntityWarship( + rnd.nextInt(10, 300), + rnd.nextFloat(10, 3000), + color) + ); + } + + _warship = warshipCreator.NewWarshipCreating(); + _warship.SetPosition( + rnd.nextInt(20), + rnd.nextInt(20), + PictureBox.getWidth(), + PictureBox.getHeight()); + repaint(); + } }); - ChooseButton.addActionListener(e -> { - selectedWarship=_warship; + SelectButton.addActionListener(e -> { + selectedWarship = _warship; dispose(); }); } diff --git a/src/MapWithSetWarshipsGeneric.java b/src/MapWithSetWarshipsGeneric.java index b4f55a0..15a95b9 100644 --- a/src/MapWithSetWarshipsGeneric.java +++ b/src/MapWithSetWarshipsGeneric.java @@ -19,19 +19,19 @@ public class MapWithSetWarshipsGeneric { - private T[] _places; +public class SetWarshipsGeneric{ + private final Object[] _places; public int getCount() { - return _places != null ? _places.length : 0; + return _places.length; } public SetWarshipsGeneric(int count) { - _places = (T[]) new Object[count]; + _places = new Object[count]; } public int Insert(T warship) @@ -17,7 +17,7 @@ public class SetWarshipsGeneric{ public int Insert(T warship, int position) { - if (position < 0 || position >= getCount() || _places[position] == null) + if (position < 0 || position >= getCount()) return -1; int empty = -1; @@ -43,7 +43,7 @@ public class SetWarshipsGeneric{ if (position >= getCount() || position < 0 || _places[position] == null) return null; - T deleted =_places[position]; + T deleted =(T) _places[position]; _places[position] = null; return deleted; } @@ -53,6 +53,6 @@ public class SetWarshipsGeneric{ if (position >= getCount() || position < 0) return null; - return _places[position]; + return (T) _places[position]; } } diff --git a/src/WarshipCreatorGeneric.java b/src/WarshipCreatorGeneric.java index a4495ea..2c67a1b 100644 --- a/src/WarshipCreatorGeneric.java +++ b/src/WarshipCreatorGeneric.java @@ -1,36 +1,39 @@ +import java.util.ArrayList; import java.util.Random; public class WarshipCreatorGeneric { - private final EntityWarship[] Warships; - private final IDrawingObjectBlock[] Blocks; + private final ArrayList Warships; + private final ArrayList Blocks; private int WarshipsCount = 0; private int BlocksCount = 0; public WarshipCreatorGeneric(int warshipsCount, int blocksCount){ - Warships = new EntityWarship[warshipsCount]; - Blocks = new IDrawingObjectBlock[blocksCount]; + Warships = new ArrayList<>(warshipsCount); + Blocks = new ArrayList<>(blocksCount); } public int AddWarship(T warship){ - if(WarshipsCount < Warships.length){ - Warships[WarshipsCount] = warship; + if(WarshipsCount <= Warships.size()){ + Warships.add(warship); WarshipsCount++; return WarshipsCount - 1; } return -1; } + public int AddBlock(U block){ - if(BlocksCount < Blocks.length){ - Blocks[BlocksCount] = block; + if(BlocksCount <= Blocks.size()){ + Blocks.add(block); BlocksCount++; return BlocksCount - 1; } return -1; } + public DrawingWarship NewWarshipCreating() { Random rand=new Random(); - T warship = (T)Warships[rand.nextInt(WarshipsCount)]; - U block = (U)Blocks[rand.nextInt(BlocksCount)]; + T warship = (T)Warships.get(rand.nextInt(WarshipsCount)); + U block = (U)Blocks.get(rand.nextInt(BlocksCount)); if(warship instanceof EntityAircraftCarrier){ return new DrawingAircraftCarrier(warship, block);