diff --git a/DrawningBoat.java b/DrawningBoat.java index 4eb539e..5ef5d6e 100644 --- a/DrawningBoat.java +++ b/DrawningBoat.java @@ -41,6 +41,11 @@ public class DrawningBoat extends JPanel { SetOars(); } + protected DrawningBoat(EntityBoat boat, IDrawningOars oar){ + Boat = boat; + OarsDraw = oar; + } + public void SetPosition(int x, int y, int width, int height) { if (x >= 0 && x + _BoatWidth <= width && y >= 0 && y + _BoatHeight <= height) { diff --git a/DrawningCatamaran.java b/DrawningCatamaran.java index bf11929..ed87c55 100644 --- a/DrawningCatamaran.java +++ b/DrawningCatamaran.java @@ -7,6 +7,12 @@ public class DrawningCatamaran extends DrawningBoat{ super(speed, weight, bodyColor, 140, 70); Boat = new EntityCatamaran(speed, weight, bodyColor, dopColor, sail, floats); } + + protected DrawningCatamaran(EntityBoat boat, IDrawningOars oars){ + super(boat,oars); + Boat = boat; + } + @Override public void DrawTransport(Graphics g) { diff --git a/DrawningEntities.java b/DrawningEntities.java new file mode 100644 index 0000000..77075fd --- /dev/null +++ b/DrawningEntities.java @@ -0,0 +1,57 @@ +import java.util.Random; + +public class DrawningEntities +{ + public T[] _entities; + public U[] _oars; + int entitiesCount = 0; + int oarsCount = 0; + String indx; + String indy; + + public DrawningEntities(int countE,int countI){ + _entities = (T[]) new EntityBoat[countE]; + _oars = (U[]) new IDrawningOars[countI]; + } + + public int Insert(T boat){ + if(entitiesCount < _entities.length){ + _entities[entitiesCount] = boat; + entitiesCount++; + return entitiesCount - 1; + } + return -1; + } + + public int Insert(U oars){ + if(oarsCount < _oars.length){ + _oars[oarsCount] = oars; + oarsCount++; + return oarsCount - 1; + } + return -1; + } + + public void SetIndexs(int ind1, int ind2) + { + indx=Integer.toString(ind1); + indy=Integer.toString(ind2); + } + + public DrawningBoat CreateBoat(){ + Random random = new Random(); + int indEnt = 0; + int indOar = 0; + if(entitiesCount - 1 != 0 & oarsCount - 1 != 0){ + indEnt = random.nextInt(0,entitiesCount - 1); + indOar = random.nextInt(0, oarsCount - 1); + } + T boat = (T)_entities[indEnt]; + U oar = (U)_oars[indOar]; + SetIndexs(indEnt,indOar); + if(boat instanceof EntityCatamaran){ + return new DrawningCatamaran(boat,oar); + } + return new DrawningBoat(boat,oar); + } +} \ No newline at end of file diff --git a/FormBoat.form b/FormBoat.form index c46e5ab..6a871a6 100644 --- a/FormBoat.form +++ b/FormBoat.form @@ -98,11 +98,14 @@ - + - + - + + + + diff --git a/FormBoat.java b/FormBoat.java index 77fe7d1..b0778a1 100644 --- a/FormBoat.java +++ b/FormBoat.java @@ -5,8 +5,9 @@ import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.image.BufferedImage; import java.util.Random; +import static java.lang.Boolean.parseBoolean; -public class FormBoat extends JFrame{ +public class FormBoat extends JDialog{ public JPanel Mainpanel; private JButton ButtonCreate; private JButton ButtonLeft; @@ -17,9 +18,15 @@ public class FormBoat extends JFrame{ private JPanel PictureBox; private JToolBar StatusStrip; private JButton ButtonModif; + private JButton ButtonSelectBoat; private final JLabel JLabelSpeed = new JLabel(); private final JLabel JLabelWeight = new JLabel(); private final JLabel JLabelColor = new JLabel(); + private DrawningBoat SelectedBoat; + + public DrawningBoat GetSelectedBoat() { + return SelectedBoat; + } public void Draw() { PictureBox.removeAll(); BufferedImage bmp = new BufferedImage(PictureBox.getWidth(), PictureBox.getHeight(),BufferedImage.TYPE_INT_RGB); @@ -28,11 +35,11 @@ public class FormBoat extends JFrame{ gr.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); if (_boat != null) { _boat.DrawTransport(gr); - JLabel imageOfPlane = new JLabel(); - imageOfPlane.setPreferredSize(PictureBox.getSize()); - imageOfPlane.setMinimumSize(new Dimension(1, 1)); - imageOfPlane.setIcon(new ImageIcon(bmp)); - PictureBox.add(imageOfPlane,BorderLayout.CENTER); + JLabel imageOfBoat = new JLabel(); + imageOfBoat.setPreferredSize(PictureBox.getSize()); + imageOfBoat.setMinimumSize(new Dimension(1, 1)); + imageOfBoat.setIcon(new ImageIcon(bmp)); + PictureBox.add(imageOfBoat,BorderLayout.CENTER); } validate(); } @@ -44,6 +51,7 @@ public class FormBoat extends JFrame{ JLabelColor.setText(("Цвет: " + _boat.GetBoat().GetBodyColor() + " ")); } public FormBoat() { + add(Mainpanel); Box LabelBox = Box.createHorizontalBox(); LabelBox.setMinimumSize(new Dimension(1, 20)); LabelBox.add(JLabelSpeed); @@ -64,13 +72,16 @@ public class FormBoat extends JFrame{ } ButtonCreate.addActionListener(e -> { Random random = new Random(); - _boat = new DrawningBoat(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + Color color = JColorChooser.showDialog(null, "Цвет", null); + _boat = new DrawningBoat(random.nextInt(100, 300),random.nextInt(1000, 2000),color); SetData(); Draw(); }); ButtonModif.addActionListener(e -> { Random random = new Random(); - _boat = new DrawningCatamaran(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean()); + Color first = JColorChooser.showDialog(null, "Цвет", null); + Color second = JColorChooser.showDialog(null, "Цвет", null); + _boat = new DrawningCatamaran(random.nextInt(100, 300), random.nextInt(1000, 2000), first, second, random.nextBoolean(), random.nextBoolean()); SetData(); Draw(); }); @@ -104,5 +115,10 @@ public class FormBoat extends JFrame{ PictureBox.revalidate(); Draw(); }); + ButtonSelectBoat.addActionListener(e -> { + SelectedBoat = _boat; + setVisible(false); + dispose(); + }); } } diff --git a/FormMap.form b/FormMap.form deleted file mode 100644 index ff5bafc..0000000 --- a/FormMap.form +++ /dev/null @@ -1,121 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/FormMap.java b/FormMap.java deleted file mode 100644 index 99f8a9a..0000000 --- a/FormMap.java +++ /dev/null @@ -1,128 +0,0 @@ -import javax.imageio.ImageIO; -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.image.BufferedImage; -import java.util.Random; - -public class FormMap extends JFrame{ - public JPanel Mainpanel; - private JButton ButtonCreate; - private JButton ButtonLeft; - private JButton ButtonUp; - private JButton ButtonDown; - private JButton ButtonRight; - private JPanel PictureBox; - private JToolBar StatusStrip; - private JButton ButtonModif; - private JComboBox comboBoxSelector; - private final JLabel JLabelSpeed = new JLabel(); - private final JLabel JLabelWeight = new JLabel(); - private final JLabel JLabelColor = new JLabel(); - private AbstractMap _abstractMap; - - private void ButtonMove_Click(String name) - { - Direction dir = Direction.None; - switch (name) - { - case "buttonLeft": - dir = Direction.Left; - break; - case "buttonUp": - dir = Direction.Up; - break; - case "buttonRight": - dir = Direction.Right; - break; - case "buttonDown": - dir = Direction.Down; - break; - } - PictureBox.removeAll(); - JLabel imageWithMapAndObject = new JLabel(); - imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); - imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(dir))); - PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); - PictureBox.revalidate(); - PictureBox.repaint(); - } - - private void SetData(DrawningBoat _boat){ - PictureBox.removeAll(); - JLabelSpeed.setText("Cкорость: " + _boat.GetBoat().GetSpeed() + " "); - JLabelWeight.setText("Вес: " + _boat.GetBoat().GetWeight() + " "); - JLabelColor.setText(("Цвет: " + _boat.GetBoat().GetBodyColor() + " ")); - JLabel imageWithMapAndObject = new JLabel(); - imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); - imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); - imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.CreateMap(PictureBox.getWidth(),PictureBox.getHeight(), new DrawningObjectBoat(_boat)))); - PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); - PictureBox.revalidate(); - } - public FormMap() { - comboBoxSelector.addItem("Простая карта"); - comboBoxSelector.addItem("Вторая карта"); - _abstractMap = new SimpleMap(); - Box LabelBox = Box.createHorizontalBox(); - LabelBox.setMinimumSize(new Dimension(1, 20)); - LabelBox.add(JLabelSpeed); - LabelBox.add(JLabelWeight); - LabelBox.add(JLabelColor); - StatusStrip.add(LabelBox); - try { - Image img = ImageIO.read(FormMap.class.getResource("/Resource/arrowUp.jpg")); - ButtonUp.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMap.class.getResource("/Resource/arrowDown.jpg")); - ButtonDown.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMap.class.getResource("/Resource/arrowLeft.jpg")); - ButtonLeft.setIcon(new ImageIcon(img)); - img = ImageIO.read(FormMap.class.getResource("/Resource/arrowRight.jpg")); - ButtonRight.setIcon(new ImageIcon(img)); - } catch (Exception ex) { - System.out.println(ex); - } - ButtonCreate.addActionListener(e -> { - Random random = new Random(); - var _boat = new DrawningBoat(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); - SetData(_boat); - }); - ButtonModif.addActionListener(e -> { - Random random = new Random(); - var _boat = new DrawningCatamaran(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean()); - SetData(_boat); - }); - ButtonUp.addActionListener(e -> { - ButtonMove_Click("buttonUp"); - }); - ButtonDown.addActionListener(e -> { - ButtonMove_Click("buttonDown"); - }); - ButtonLeft.addActionListener(e -> { - ButtonMove_Click("buttonLeft"); - }); - ButtonRight.addActionListener(e -> { - ButtonMove_Click("buttonRight"); - }); - comboBoxSelector.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - comboBoxSelector = (JComboBox)e.getSource(); - String item = (String)comboBoxSelector.getSelectedItem(); - switch (item) { - case "Простая карта" -> { - _abstractMap = new SimpleMap(); - break; - } - case "Вторая карта" -> { - _abstractMap = new MyMap(); - break; - } - } - } - }); - - } -} diff --git a/FormMapWithSetBoats.form b/FormMapWithSetBoats.form new file mode 100644 index 0000000..ed0d7dc --- /dev/null +++ b/FormMapWithSetBoats.form @@ -0,0 +1,166 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormMapWithSetBoats.java b/FormMapWithSetBoats.java new file mode 100644 index 0000000..a08b182 --- /dev/null +++ b/FormMapWithSetBoats.java @@ -0,0 +1,192 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; + +public class FormMapWithSetBoats extends JFrame { + + private MapWithSetBoatsGeneric _mapBoatsCollectionGeneric; + + public JPanel Mainpanel; + private JButton buttonAdd; + private JComboBox comboBoxSelectorMap; + private JTextField textBoxPosition; + private JButton buttonRemove; + private JButton buttonShowStorage; + private JButton buttonShowOnMap; + private JButton buttonUp; + private JButton buttonLeft; + private JButton buttonRight; + private JButton buttonDown; + private JPanel PictureBox; + private JPanel groupBox; + AbstractMap _abstractMap; + + private JFrame getFrame() { + JFrame frame = new JFrame(); + frame.setVisible(false); + frame.setBounds(300, 100, 800, 600); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + return frame; + } + + JFrame jFrame = getFrame(); + + private void ButtonMove_Click(String name) + { + if (_mapBoatsCollectionGeneric == null) return; + Direction direction = Direction.None; + switch (name) + { + case "buttonLeft": + direction = Direction.Left; + break; + case "buttonUp": + direction = Direction.Up; + break; + case "buttonRight": + direction = Direction.Right; + break; + case "buttonDown": + direction = Direction.Down; + break; + } + PictureBox.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.MoveObject(direction))); + PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + } + + public FormMapWithSetBoats() { + comboBoxSelectorMap.addItem("Простая карта"); + comboBoxSelectorMap.addItem("Вторая карта"); + try { + Image img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/up.jpg")); + buttonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/down.jpg")); + buttonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/left.jpg")); + buttonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormMapWithSetBoats.class.getResource("/Resource/right.jpg")); + buttonRight.setIcon(new ImageIcon(img)); + } catch (Exception ex) { + System.out.println(ex); + } + + comboBoxSelectorMap.addActionListener(e -> { + AbstractMap map = null; + switch (comboBoxSelectorMap.getSelectedItem().toString()) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Вторая карта": + map = new MyMap(); + break; + } + if (map != null) + { + _mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric(PictureBox.getWidth(), PictureBox.getHeight(), map); + } + else + { + _mapBoatsCollectionGeneric = null; + } + }); + buttonAdd.addActionListener(e -> { + if (_mapBoatsCollectionGeneric == null) + { + return; + } + FormBoat dialog = new FormBoat(); + dialog.setSize(800, 600); + dialog.setLocation(500, 200); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + + if (dialog.GetSelectedBoat() == null) return; + + DrawningObjectBoat boat = new DrawningObjectBoat(dialog.GetSelectedBoat()); + + if (_mapBoatsCollectionGeneric.addBoat(boat) != -1) + { + JOptionPane.showMessageDialog(jFrame, "Объект добавлен"); + PictureBox.removeAll(); + JLabel imageOfBoat = new JLabel(); + imageOfBoat.setPreferredSize(PictureBox.getSize()); + imageOfBoat.setMinimumSize(new Dimension(1, 1)); + imageOfBoat.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet())); + PictureBox.add(imageOfBoat,BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + } + else + { + JOptionPane.showMessageDialog(jFrame, "Не удалось добавить объект"); + } + }); + buttonRemove.addActionListener(e -> { + if(_mapBoatsCollectionGeneric == null) return; + + String text = textBoxPosition.getText(); + if(text.isEmpty()) return; + + if(JOptionPane.showConfirmDialog(jFrame, "Вы действительно хотите удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return; + + try { + Integer.parseInt(text); + } + catch (Exception ex){ + return; + } + + int pos = Integer.parseInt(text); + if (_mapBoatsCollectionGeneric.removeBoat(pos) != null) + { + JOptionPane.showMessageDialog(jFrame, "Объект удален"); + PictureBox.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(PictureBox.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet())); + PictureBox.add(imageOfShip,BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + } + else + { + JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект"); + } + }); + buttonShowStorage.addActionListener(e -> { + if (_mapBoatsCollectionGeneric == null) return; + PictureBox.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(PictureBox.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowSet())); + PictureBox.add(imageOfShip,BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + }); + buttonShowOnMap.addActionListener(e -> { + if (_mapBoatsCollectionGeneric == null) return; + PictureBox.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(PictureBox.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_mapBoatsCollectionGeneric.ShowOnMap())); + PictureBox.add(imageOfShip,BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + }); + buttonUp.addActionListener(e -> ButtonMove_Click("buttonUp")); + buttonLeft.addActionListener(e -> ButtonMove_Click("buttonLeft")); + buttonDown.addActionListener(e -> ButtonMove_Click("buttonDown")); + buttonRight.addActionListener(e -> ButtonMove_Click("buttonRight")); + } +} diff --git a/FormParam.form b/FormParam.form new file mode 100644 index 0000000..d0def78 --- /dev/null +++ b/FormParam.form @@ -0,0 +1,62 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormParam.java b/FormParam.java new file mode 100644 index 0000000..d26c764 --- /dev/null +++ b/FormParam.java @@ -0,0 +1,103 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.util.Random; +public class FormParam extends JFrame{ + public JPanel MainPanel; + private JPanel pictureBoxBoat; + private JButton ButtonCreate; + private JButton ButtonCreateModif; + private JToolBar StatusStrip; + private JLabel LabelInfo; + private JLabel JLabelSpeed = new JLabel(); + private JLabel JLabelWeight = new JLabel(); + private JLabel JLabelColor = new JLabel(); + private DrawningEntities _drawningEntities; + private IDrawningOars SetData() + { + Random random=new Random(); + int r = random.nextInt(3); + if(r==0) + { + return new DrawningOars(); + } + if(r==1) + { + return new DrawningSqareOars(); + } + else + { + return new DrawningOvalOars(); + } + } + private void Draw(DrawningBoat _boat) { + pictureBoxBoat.removeAll(); + Random random = new Random(); + BufferedImage bmp = new BufferedImage(pictureBoxBoat.getWidth(), pictureBoxBoat.getHeight(),BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + gr.setColor(new Color(238, 238, 238)); + gr.fillRect(0, 0, pictureBoxBoat.getWidth(), pictureBoxBoat.getHeight()); + if (_boat != null) { + _boat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxBoat.getWidth(), pictureBoxBoat.getHeight()); + _boat.DrawTransport(gr); + JLabelSpeed.setText("Cкорость: " + _boat.GetBoat().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + _boat.GetBoat().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + _boat.GetBoat().GetBodyColor() + " ")); + JLabel imageOfBoat = new JLabel(); + imageOfBoat.setPreferredSize(pictureBoxBoat.getSize()); + imageOfBoat.setMinimumSize(new Dimension(1, 1)); + imageOfBoat.setIcon(new ImageIcon(bmp)); + pictureBoxBoat.add(imageOfBoat,BorderLayout.CENTER); + } + validate(); + } + public FormParam() + { + Box LabelBox = Box.createHorizontalBox(); + LabelBox.setMinimumSize(new Dimension(1, 20)); + LabelBox.add(JLabelSpeed); + LabelBox.add(JLabelWeight); + LabelBox.add(JLabelColor); + StatusStrip.add(LabelBox); + _drawningEntities = new DrawningEntities<>(10,10); + ButtonCreate.addActionListener(e -> { + Random random = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); + EntityBoat _boat = new EntityBoat(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst); + IDrawningOars oar = SetData(); + int OarCount = random.nextInt(1,4); + oar.SetOarsCount(OarCount); + if((_drawningEntities.Insert(_boat)!=-1) & (_drawningEntities.Insert(oar)!=-1)) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + Draw(_drawningEntities.CreateBoat()); + LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + }); + ButtonCreateModif.addActionListener(e -> { + Random random = new Random(); + Color colorFirst = JColorChooser.showDialog(null, "Цвет", null); + Color colorSecond = JColorChooser.showDialog(null, "Цвет", null); + EntityCatamaran _boat = new EntityCatamaran(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst, colorSecond, random.nextBoolean(), random.nextBoolean()); + IDrawningOars oar = SetData(); + int OarCount = random.nextInt(1,4); + oar.SetOarsCount(OarCount); + if((_drawningEntities.Insert(_boat)!=-1) & (_drawningEntities.Insert(oar)!=-1)) + { + JOptionPane.showMessageDialog(null,"Объект добавлен"); + Draw(_drawningEntities.CreateBoat()); + LabelInfo.setText(_drawningEntities.indx+ " " + _drawningEntities.indy); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект"); + } + }); + } +} diff --git a/Main.java b/Main.java index a5eca93..3c73a2f 100644 --- a/Main.java +++ b/Main.java @@ -4,7 +4,7 @@ public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Лодка"); - frame.setContentPane(new FormMap().Mainpanel); + frame.setContentPane(new FormParam().MainPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(500, 200); frame.pack(); diff --git a/MapWithSetBoatsGeneric.java b/MapWithSetBoatsGeneric.java new file mode 100644 index 0000000..771d160 --- /dev/null +++ b/MapWithSetBoatsGeneric.java @@ -0,0 +1,132 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetBoatsGeneric +{ + private int _pictureWidth; + private int _pictureHeight; + private int _placeSizeWidth = 210; + private int _placeSizeHeight = 90; + private SetBoatsGeneric _setBoats; + private U _map; + + public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setBoats = new SetBoatsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int addBoat(T boat) + { + return _setBoats.Insert(boat); + } + + public T removeBoat(int position) + { + return _setBoats.Remove(position); + } + + public BufferedImage ShowSet() + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D gr = (Graphics2D) img.getGraphics(); + DrawBackground(gr); + DrawBoats(gr); + return img; + } + + public BufferedImage ShowOnMap() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); + + Shaking(); + for (int i = 0; i < _setBoats.Count(); i++) + { + var ship = _setBoats.Get(i); + if (ship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); + } + } + return bmp; + } + + public BufferedImage MoveObject(Direction direction) + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + if (_map != null) + { + _map.MoveObject(direction); + } + return img; + } + + private void Shaking() + { + int j = _setBoats.Count() - 1; + for (int i = 0; i < _setBoats.Count(); i++) + { + if (_setBoats.Get(i) == null) + { + for (; j > i; j--) + { + var boat = _setBoats.Get(j); + if (boat != null) + { + _setBoats.Insert(boat, i); + _setBoats.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics g) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.BLACK); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия рамзетки места + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * + _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + for (int k = 0; k < 7; k++) + { + g.drawOval( i * _placeSizeWidth + 15 * k, j * _placeSizeHeight, 15, 15); + g.drawOval( i * _placeSizeWidth + 15 * k, j * _placeSizeHeight - 15, 15, 15); + } + } + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + for (int k = 0; k < 6 * 4; k++) + { + g.drawOval( i * _placeSizeWidth, 0 + 15 * k, 15, 15); + } + } + } + private void DrawBoats(Graphics g) + { + int numberOfSeatsInWidth = _pictureWidth / _placeSizeWidth; + int numberOfSeatsInHeight = _pictureHeight / _placeSizeHeight; + int bottomLine = (numberOfSeatsInHeight - 1) * _placeSizeHeight; + + for (int i = 0; i < _setBoats.Count(); i++) + { + if(_setBoats.Get(i) != null) + { + _setBoats.Get(i).SetObject(i % numberOfSeatsInWidth * _placeSizeWidth, bottomLine - i / numberOfSeatsInWidth * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setBoats.Get(i).DrawningObject(g); + } + } + } +} diff --git a/SetBoatsGeneric.java b/SetBoatsGeneric.java new file mode 100644 index 0000000..255c57e --- /dev/null +++ b/SetBoatsGeneric.java @@ -0,0 +1,44 @@ +public class SetBoatsGeneric { + private T[] _places; + + public int Count() { + return _places.length; + } + + public SetBoatsGeneric(int count) { + _places = (T[]) (new Object[count]); + } + + public int Insert(T boat) { + for (int i = 0; i < _places.length; i++) { + if (_places[i] == null) { + _places[i] = boat; + return i; + } + } + return -1; + } + + public int Insert(T boat, int position) { + int index = position; + + while (_places[index] != null && index < _places.length) index++; + + if (index == _places.length) return -1; + for (int i = index; i > position; --i) _places[i] = _places[i - 1]; + + _places[position] = boat; + return position; + } + + public T Remove(int position) { + if (position >= _places.length) return null; + T res = _places[position]; + _places[position] = null; + return res; + } + + public T Get(int position) { + return _places[position]; + } +}