diff --git a/DrawingContainerShip.java b/DrawingContainerShip.java index dd20df4..682669e 100644 --- a/DrawingContainerShip.java +++ b/DrawingContainerShip.java @@ -10,7 +10,7 @@ public class DrawingContainerShip extends DrawingShip{ public DrawingContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, boolean crane, boolean containers,int deck, int deckType, int width, int height) { - super(speed, weight, bodyColor,width, height, 110, 65); + super(speed, weight, bodyColor, width, height, 110, 65); if (EntityShip != null) { EntityShip = new EntityContainerShip(speed, weight, bodyColor, @@ -29,6 +29,15 @@ additionalColor, boolean crane, boolean containers,int deck, int deckType, int w iDecksDrawing.setNumDecks(deck); } } + public DrawingContainerShip(EntityContainerShip ship, IDecksDrawing decks, int width, int height){ + super(ship,width, height); + if(width < _pictureWidth || height < _pictureHeight){ + return; + } + iDecksDrawing = decks; + + } + @Override public void DrawShip(Graphics2D g) { diff --git a/DrawingShip.java b/DrawingShip.java index fb396f6..79f9bc7 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -1,5 +1,10 @@ import java.awt.*; public class DrawingShip { + public IMoveableObject GetMoveableObject() + { + return new DrawningObjectShip(this); + } + public EntityShip EntityShip; public int _pictureWidth; public int _pictureHeight; @@ -46,6 +51,16 @@ public class DrawingShip { _pictureWidth = width; _pictureHeight = height; EntityShip = new EntityShip(speed, weight, bodyColor); + + } + public DrawingShip(EntityShip ship, int width, int height){ + if(width < _shipWidth || height < _shipHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityShip = ship; } protected DrawingShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth, int shipHeight) { @@ -57,8 +72,9 @@ public class DrawingShip { _pictureHeight = height; _shipHeight = shipHeight; _shipWidth = shipWidth; - EntityShip = new EntityShip(speed, weight, bodyColor); + } + public void SetPosition(int x, int y) { _startPosX = Math.min(x, _pictureWidth - _shipWidth); @@ -129,5 +145,6 @@ public class DrawingShip { g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80); g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75); + } } diff --git a/FormContainerShip.java b/FormContainerShip.java index 0ac9f3b..bac5db6 100644 --- a/FormContainerShip.java +++ b/FormContainerShip.java @@ -2,19 +2,40 @@ import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*; -public class FormContainerShip { - private DrawingShip _drawingShip; +public class FormContainerShip{ + public DrawingShip _drawingShip; private AbstractStrategy _abstractStrategy; Canvas canv; - + class Canvas extends JComponent{ + public DrawingShip _drawingShip; + + + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingShip == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _drawingShip.DrawShip(g2d); + super.repaint(); + } +} + public DrawingShip SelectedShip; + public boolean DialogResult = false; + public JButton buttonSelectedShip; + public JFrame w; public void Draw(){ canv.repaint(); } public FormContainerShip(){ - JFrame w=new JFrame ("ContainerShip"); + SelectedShip = null; + w = new JFrame ("ContainerShip"); JButton buttonCreateShip = new JButton("создать кораблик"); JButton buttonCreateContainerShip = new JButton("создать контейнеровоз"); JButton buttonStep = new JButton("шаг"); + buttonSelectedShip = new JButton("выбрать"); String[] items = { "MoveToCenter", "MoveToRight-Down" }; @@ -49,9 +70,14 @@ public class FormContainerShip { public void actionPerformed(ActionEvent e){ System.out.println(e.getActionCommand()); Random random = new Random(); + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color selectedColor = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE); + if(selectedColor != null){ + color = selectedColor; + } _drawingShip = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(0,256), random.nextInt(0,256), random.nextInt(0,256)),960, 560); + color,960, 560); _drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100)); canv._drawingShip = _drawingShip; @@ -64,10 +90,20 @@ public class FormContainerShip { public void actionPerformed(ActionEvent e){ System.out.println(e.getActionCommand()); Random random = new Random(); + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color selectedColor = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE); + if(selectedColor != null){ + color = selectedColor; + } + Color color2 = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color selectedColor2 = JColorChooser.showDialog(w, "Выберите цвет", Color.WHITE); + if(selectedColor2 != null){ + color2 = selectedColor2; + } _drawingShip = new DrawingContainerShip(random.nextInt(100, 300), random.nextInt(1000, 3000), - 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)), + color, + color2, random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560); _drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100)); @@ -157,6 +193,7 @@ public class FormContainerShip { canv.setBounds(0, 0, 1000, 600); buttonCreateShip.setBounds(2, 540, 200, 20); buttonCreateContainerShip.setBounds(215, 540, 200, 20); + buttonSelectedShip.setBounds(550, 540, 150, 20); comboBoxStrategy.setBounds(800, 10, 150, 30); buttonStep.setBounds(800, 45, 100, 20); up.setBounds(900, 480, 45, 45); @@ -166,6 +203,7 @@ public class FormContainerShip { w.add(canv); w.add(buttonCreateShip); w.add(buttonCreateContainerShip); + w.add(buttonSelectedShip); w.add(comboBoxStrategy); w.add(buttonStep); w.add(up); diff --git a/FormShipCollection.java b/FormShipCollection.java new file mode 100644 index 0000000..7d37b1d --- /dev/null +++ b/FormShipCollection.java @@ -0,0 +1,111 @@ +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; + +public class FormShipCollection { + class Canvas extends JComponent{ + public ShipGenericCollection _ships; + public Canvas(){} + public void paintComponent(Graphics g){ + super.paintComponent(g); + if(_ships.ShowShips() != null){ + g.drawImage(_ships.ShowShips(), 0, 0, this); + } + super.repaint(); + } + } + Canvas canv; + static int pictureBoxWidth = 860; + static int pictureBoxHeight = 580; + private ShipGenericCollection _ships; + public void Draw(){ + canv.repaint(); + } + FormShipCollection(){ + canv = new Canvas(); + JFrame w = new JFrame("FormShipCollection"); + _ships = new ShipGenericCollection(pictureBoxWidth, pictureBoxHeight); + canv._ships = _ships; + JButton ButtonAddShip = new JButton("Добавить кораблик"); + JButton ButtonDeleteShip = new JButton("Удалить кораблик"); + JButton ButtonRefreshCollection = new JButton("Обновить коллекцию"); + JButton forNewForm = new JButton("новая форма"); + JTextField TextBoxNumber = new JTextField(); + ButtonAddShip.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + FormContainerShip form = new FormContainerShip(); + form.buttonSelectedShip.addActionListener( + new ActionListener(){ + public void actionPerformed(ActionEvent e){ + if(_ships.Add(form._drawingShip) != -1){ + JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + Draw(); + } + else{ + JOptionPane.showMessageDialog(null, "Объект не добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + } + form.w.dispose(); + } + } + ); + } + } + ); + + + forNewForm.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + NewFormRand newFormRand = new NewFormRand(); + } + } + ); + + ButtonDeleteShip.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) + { + return; + } + int pos = Integer.parseInt(TextBoxNumber.getText()); + if (_ships.remove(pos) != null) + { + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + + Draw(); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + + } + } + } + ); + ButtonRefreshCollection.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + Draw(); + } + } + ); + w.setSize (1000, 600); + w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); + w.setLayout(null); + canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); + ButtonAddShip.setBounds(pictureBoxWidth, 0, 120, 20); + TextBoxNumber.setBounds(pictureBoxWidth, 50, 120, 20); + ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 120, 20); + ButtonRefreshCollection.setBounds(pictureBoxWidth, 120, 120, 20); + forNewForm.setBounds(pictureBoxWidth, 150, 120, 20); + w.add(canv); + w.add(ButtonAddShip); + w.add(ButtonDeleteShip); + w.add(ButtonRefreshCollection); + w.add(forNewForm); + w.add(TextBoxNumber); + w.setVisible(true); + } +} diff --git a/Main.java b/Main.java index 8414fdc..2508ddf 100644 --- a/Main.java +++ b/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args){ - FormContainerShip formContainerShip = new FormContainerShip(); + FormShipCollection formShipCollection = new FormShipCollection(); } } diff --git a/NewFormRand.java b/NewFormRand.java new file mode 100644 index 0000000..f34bc3c --- /dev/null +++ b/NewFormRand.java @@ -0,0 +1,53 @@ +import java.awt.*; +import javax.swing.*; +import java.awt.event.*; +public class NewFormRand extends JFrame { + static int pictureBoxWidth = 960; + static int pictureBoxHeight = 560; + public DrawingShip _drawingShip; + private class Canvas extends JComponent{ + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingShip == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _drawingShip.SetPosition(100, 100); + _drawingShip.DrawShip(g2d); + super.repaint(); + } + } + ShipGenericDop _ShipGenericDop; + public NewFormRand(){ + _drawingShip = null; + Canvas canv = new Canvas(); + setSize(1000, 600); + setLayout(null); + _ShipGenericDop = new ShipGenericDop<>(50,50,pictureBoxWidth,pictureBoxHeight); + canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); + _ShipGenericDop.AddShip(new EntityShip(200,200,Color.PINK)); + _ShipGenericDop.AddShip(new EntityShip(200,200,Color.GRAY)); + _ShipGenericDop.AddShip(new EntityContainerShip(200, 200, Color.GREEN, Color.BLUE, true, false, 2, 1)); + _ShipGenericDop.AddShip(new EntityContainerShip(200, 200, Color.LIGHT_GRAY, Color.MAGENTA, false, true, 3, 3)); + _ShipGenericDop.AddDeck(new DrawingDecks()); + _ShipGenericDop.AddDeck(new DrawingDecksRect()); + _ShipGenericDop.AddDeck(new DrawingDecksTrapez()); + JButton creatButton = new JButton("создать"); + creatButton.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + _drawingShip = _ShipGenericDop.getObjectRandomShip(); + canv.repaint(); + } + } + ); + creatButton.setBounds(820, 490, 120, 20); + + add(canv); + add(creatButton); + setVisible(true); + } +} + diff --git a/SetGeneric.java b/SetGeneric.java new file mode 100644 index 0000000..0a1345e --- /dev/null +++ b/SetGeneric.java @@ -0,0 +1,118 @@ +public class SetGeneric +{ + private Object[] _places; + + public int Count; + + public SetGeneric(int count) + { + _places = new Object[count]; + Count = _places.length; + } + + public int Insert(T ship) + { + // TODO вставка в начало набора + int temp = 0; + int k = 0; + for(int j = 0; j < _places.length; j++) { + if (_places[j] != null) + { + k++; + } + } + if (k == _places.length) + { + return -1; + } + else + { + for (int i = 0; i < _places.length; i++) + { + if (_places[i] == null) + { + temp = i; + break; + } + + } + for (int i = temp; i > 0; i--) + { + _places[i] = _places[i -1]; + + } + _places[0] = ship; + return 0; + } + } + + public boolean Insert(T ship, int position) + { + + int temp = 0; + int k = 0; + for (int j = position; j < _places.length; j++) + { + if (_places[j] != null) + { + k++; + } + } + if (position < _places.length && k < _places.length-position) + { + for (int i = position; i < _places.length; i++) + { + if (_places[i] == null) + { + temp = i; + break; + } + + } + for (int i = temp; i > position; i--) + { + _places[i] = _places[i - 1]; + + } + + _places[position] = ship; + return true; + } + else + { + return false; + } + + } + + public boolean Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присвоив элементу массива + //значение null + if(position < _places.length) + { + _places[position] = null; + return true; + } + else + { + return false; + } + + } + + public T Get(int position) + { + // TODO проверка позиции + if(position < _places.length) + { + return (T)_places[position]; + } + else + { + return null; + } + } + +} diff --git a/ShipGenericCollection.java b/ShipGenericCollection.java new file mode 100644 index 0000000..29b8ab9 --- /dev/null +++ b/ShipGenericCollection.java @@ -0,0 +1,106 @@ +import java.awt.image.BufferedImage; +import java.awt.Color; +import java.awt.Graphics2D; + +public class ShipGenericCollection +{ + private int _pictureWidth; + + private int _pictureHeight; + + private int _placeSizeWidth = 210; + + private int _placeSizeHeight = 90; + + private SetGeneric _collection; + + public ShipGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + + /*public static int operator +(ShipGenericCollection collect, T obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj) ?? -1; + }*/ + public int Add(T obj) + { + if (obj == null) + { + return -1; + } + return _collection.Insert(obj); + } + + /*public static bool operator -(ShipGenericCollection collect, int pos) + { + T obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + }*/ + public T remove(int pos) + { + T obj = _collection.Get(pos); + if (obj != null) + { + _collection.Remove(pos); + } + return obj; + } + + public U GetU(int pos) + { + return (U)_collection.Get(pos).GetMoveableObject(); + } + + public BufferedImage ShowShips() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D gr = bmp.createGraphics(); + DrawBackground(gr); + DrawObjects(gr); + gr.dispose(); + return bmp; + } + + private void DrawBackground(Graphics2D gr) + { + gr.setColor(Color.BLACK); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия рамзетки места + gr.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + gr.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + private void DrawObjects(Graphics2D gr) + { + for (int i = 0; i < _collection.Count; i++) + { + T t = _collection.Get(i); + if (t != null) + { + t.SetPosition(((_collection.Count -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection.Count - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); + t.DrawShip(gr); + } + // TODO получение объекта + // TODO установка позиции + // TODO прорисовка объекта + } + } + +} diff --git a/ShipGenericDop.java b/ShipGenericDop.java new file mode 100644 index 0000000..0e9fc69 --- /dev/null +++ b/ShipGenericDop.java @@ -0,0 +1,58 @@ +import java.util.Random; +public class ShipGenericDop { + private Object[] Ships; + private Object[] Decks; + private int countShips; + private int max_countShips; + private int countDecks; + private int max_countDecks; + private Random rand; + private int _pictureWidth; + private int _pictureHeight; + public ShipGenericDop(int _max_countShips, int _max_countDecks, int pictureWidth, int pictureHeight){ + max_countShips = _max_countShips; + max_countDecks = _max_countDecks; + Ships = new Object[max_countShips]; + Decks = new Object[max_countDecks]; + countShips = 0; + countDecks = 0; + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + rand = new Random(); + } + public boolean AddShip(T ship){ + if(ship == null){ + return false; + } + if (countShips > max_countShips){ + return false; + } + Ships[countShips++] = ship; + return true; + } + public boolean AddDeck(U deck){ + if(deck == null){ + return false; + } + if (countDecks > max_countDecks){ + return false; + } + Decks[countDecks++] = deck; + return true; + } + public DrawingShip getObjectRandomShip(){ + if(countShips == 0 || countDecks == 0){ + return null; + } + int i = rand.nextInt(countShips); + int j = rand.nextInt(countDecks); + DrawingShip drawingShip; + DrawingContainerShip drawingContainerShip; + if(Ships[i] instanceof EntityContainerShip){ + return new DrawingContainerShip((EntityContainerShip)Ships[i], (IDecksDrawing)Decks[j], _pictureWidth, _pictureHeight); + } + else{ + return new DrawingShip((EntityShip)Ships[i],_pictureWidth, _pictureHeight); + } + } +}