From 6b0601cabb4df55b8974a8f7a1c96da3e4605518 Mon Sep 17 00:00:00 2001 From: Arkadiy Radaev Date: Mon, 11 Dec 2023 14:47:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=20=D1=81=D0=B4=D0=B0=D1=87=D0=B8?= =?UTF-8?q?=203=D0=BB=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractStrategy.java | 1 + CatamaranGenericCollection.java | 81 ++++++++++++++++++++++ DrawningCatamaran.java | 17 +++-- DrawningCatamaranPro.java | 4 +- DrawningOars.java | 1 + DrawningOarsHandle.java | 1 + DrawningObjectCatamaran.java | 2 + EntityCatamaran.java | 2 + FormCatamaran.java | 55 ++++++++++++--- FormCatamaranCollection.java | 119 ++++++++++++++++++++++++++++++++ HardForm.java | 65 +++++++++++++++++ HardGeneric.java | 55 +++++++++++++++ Main.java | 2 +- MoveToBorder.java | 1 + MoveToCenter.java | 1 + ObjectParameters.java | 2 + SetGeneric.java | 53 ++++++++++++++ Status.java | 1 + 18 files changed, 445 insertions(+), 18 deletions(-) create mode 100644 CatamaranGenericCollection.java create mode 100644 FormCatamaranCollection.java create mode 100644 HardForm.java create mode 100644 HardGeneric.java create mode 100644 SetGeneric.java diff --git a/AbstractStrategy.java b/AbstractStrategy.java index 5720dee..bb9f1e7 100644 --- a/AbstractStrategy.java +++ b/AbstractStrategy.java @@ -1,3 +1,4 @@ + public abstract class AbstractStrategy { private IMoveableObject _moveableObject; private Status _state = Status.NotInit; diff --git a/CatamaranGenericCollection.java b/CatamaranGenericCollection.java new file mode 100644 index 0000000..e92b92e --- /dev/null +++ b/CatamaranGenericCollection.java @@ -0,0 +1,81 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class CatamaranGenericCollection { + private final int _pictureWidth; + + private final int _pictureHeight; + + private final int _placeSizeWidth = 140; + + private final int _placeSizeHeight = 90; + + private final SetGeneric _collection; + + public CatamaranGenericCollection(int picWidth, int picHeight){ + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + + public int Insert(T obj){ + if (obj == null) + { + return -1; + } + return _collection.Insert(obj); + } + + public boolean Remove(int position){ + return _collection.Remove(position); + } + public U GetU(int pos){ + T ans = _collection.Get(pos); + if(ans == null) + return null; + return (U)ans.GetMoveableObject(); + } + + private void DrawBackground(Graphics g) + { + g.setColor(Color.BLACK); + ((Graphics2D) g).setStroke(new BasicStroke(2)); + 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); + } + g.drawLine(i * _placeSizeWidth, 0, i * + _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + ((Graphics2D) g).setStroke(new BasicStroke(1)); + } + + private void DrawObjects(Graphics g) + { + for (int i = 0; i < _collection.Count; i++) + { + DrawningCatamaran catamaran = _collection.Get(i); + if (catamaran != null) + { + int inRow = _pictureWidth / _placeSizeWidth; + catamaran.SetPosition(i % inRow * _placeSizeWidth, ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight) + 5 ); + catamaran.DrawnCatamaran((Graphics2D) g); + } + } + } + public BufferedImage ShowCatamaran() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR); + Graphics gr = bmp.createGraphics(); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } +} diff --git a/DrawningCatamaran.java b/DrawningCatamaran.java index 47771c6..5d742e3 100644 --- a/DrawningCatamaran.java +++ b/DrawningCatamaran.java @@ -1,10 +1,11 @@ + import java.awt.*; import java.util.Random; public class DrawningCatamaran { protected EntityCatamaran EntityCatamaran; - private int _pictureWidth; - private int _pictureHeight; + public int _pictureWidth; + public int _pictureHeight; protected int _startPosX = 0; protected int _startPosY = 0; protected int _catamaranWidth = 120; @@ -14,6 +15,9 @@ public class DrawningCatamaran { public EntityCatamaran EntityCatamaran() { return EntityCatamaran; } + public IMoveableObject GetMoveableObject(){ + return new DrawningObjectCatamaran(this); + } public DrawningCatamaran(int speed, double weight, Color bodyColor, Color oarColor, int width, int height, boolean boduKit, boolean seats) { @@ -23,8 +27,7 @@ public class DrawningCatamaran { _pictureWidth = width; _pictureHeight = height; EntityCatamaran = new EntityCatamaran(speed, weight, bodyColor, oarColor, boduKit, seats); - SetPosition(rand.nextInt(10,50),rand.nextInt(10,50)); - + DrawningOars = new DrawningOars(_startPosX, _startPosY, oarColor); int variant = rand.nextInt(0, 3); if (variant == 0) @@ -34,6 +37,7 @@ public class DrawningCatamaran { else DrawningOars = new DrawningOarsHandle(_startPosX, _startPosY, oarColor); DrawningOars.ChangeOarsNumb(rand.nextInt(1, 4)); + } public void SetPosition(int x, int y) { @@ -41,7 +45,8 @@ public class DrawningCatamaran { return; _startPosX = x; _startPosY = y; - // проверка говна - фикс + DrawningOars.ChangeX(_startPosX); + DrawningOars.ChangeY(_startPosY); if (x + _catamaranWidth >= _pictureWidth || y + _catamaranHeight >= _pictureHeight) { _startPosX = 0; _startPosY = 0; @@ -76,7 +81,7 @@ public class DrawningCatamaran { can = _startPosX + EntityCatamaran.Step() + _catamaranWidth < _pictureWidth; break; case Down: - can = _startPosY + EntityCatamaran.Step() + _catamaranHeight < _pictureHeight + 20; + can = _startPosY + EntityCatamaran.Step() + _catamaranHeight < _pictureHeight; break; case Up: can = _startPosY - EntityCatamaran.Step() >= 0; diff --git a/DrawningCatamaranPro.java b/DrawningCatamaranPro.java index 584c552..d383e53 100644 --- a/DrawningCatamaranPro.java +++ b/DrawningCatamaranPro.java @@ -1,3 +1,4 @@ + import java.awt.*; public class DrawningCatamaranPro extends DrawningCatamaran { @@ -9,12 +10,13 @@ public class DrawningCatamaranPro extends DrawningCatamaran { motor, sheet, seats); } } - + @Override public void DrawnCatamaran(Graphics2D g2d) { if (!(EntityCatamaran instanceof EntityCatamaranPro)) { return; } + EntityCatamaranPro _catamaran = (EntityCatamaranPro) EntityCatamaran; g2d.setColor(_catamaran.AdditionalColor()); if (_catamaran.Motor()) { diff --git a/DrawningOars.java b/DrawningOars.java index 332440f..59d947e 100644 --- a/DrawningOars.java +++ b/DrawningOars.java @@ -1,3 +1,4 @@ + import java.awt.*; public class DrawningOars implements IDraw { diff --git a/DrawningOarsHandle.java b/DrawningOarsHandle.java index cba1553..fdc1153 100644 --- a/DrawningOarsHandle.java +++ b/DrawningOarsHandle.java @@ -1,3 +1,4 @@ + import java.awt.*; public class DrawningOarsHandle implements IDraw { diff --git a/DrawningObjectCatamaran.java b/DrawningObjectCatamaran.java index 0fbeff0..2fc1b49 100644 --- a/DrawningObjectCatamaran.java +++ b/DrawningObjectCatamaran.java @@ -1,3 +1,5 @@ + + public class DrawningObjectCatamaran implements IMoveableObject{ private final DrawningCatamaran _DrawningCatamaran; public DrawningObjectCatamaran(DrawningCatamaran drawningCatamaran){ diff --git a/EntityCatamaran.java b/EntityCatamaran.java index 11c9698..818efc1 100644 --- a/EntityCatamaran.java +++ b/EntityCatamaran.java @@ -25,4 +25,6 @@ public class EntityCatamaran { BodyKit = bodyKit; Seats = seats; } + + } diff --git a/FormCatamaran.java b/FormCatamaran.java index f8b58d8..65abc63 100644 --- a/FormCatamaran.java +++ b/FormCatamaran.java @@ -6,6 +6,7 @@ import javax.swing.*; public class FormCatamaran { Canvas canv; + public JFrame CatamaranFrame; private DrawningCatamaran DrawningCatamaran; private AbstractStrategy _abstractStrategy; private JButton UpButton; @@ -14,24 +15,41 @@ public class FormCatamaran { private JButton DownButton; private JButton CreateButton; private JButton CreateButtonPro; + private JButton buttonStep; + public JButton buttonSelect; final int pictureBoxWidth = 885; final int pictureBoxHeight = 462; + public void Draw() { canv.repaint(); } + public Color ChooseColor(JFrame CatamaranFrame) { + JColorChooser dialog = new JColorChooser(); + Color res = JColorChooser.showDialog(CatamaranFrame, "Выберите цвет", Color.WHITE); + return res; + } + + public DrawningCatamaran SelectedCatamaran() { + return DrawningCatamaran; + } + public FormCatamaran() { - JFrame CatamaranFrame = new JFrame(); + CatamaranFrame = new JFrame(); UpButton = new JButton("↑"); LeftButton = new JButton("←"); RightButton = new JButton("→"); DownButton = new JButton("↓ "); CreateButton = new JButton("Создать"); CreateButtonPro = new JButton("Создать про"); - JButton buttonStep = new JButton("Шаг"); + buttonStep = new JButton("Шаг"); + buttonSelect = new JButton ("Выбрать"); JComboBox comboBoxStrategy = new JComboBox( - new String[] {"Довести до центра","Довести до края", - }); + new String[] { + "Довести до центра", + "Довести до края", + }); + buttonStep.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (DrawningCatamaran == null) { @@ -72,9 +90,13 @@ public class FormCatamaran { @Override public void actionPerformed(ActionEvent e) { Random random = new Random(); - DrawningCatamaran = new DrawningCatamaran(random.nextInt(200,1000), random.nextDouble(1000,3000), - Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), - Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), pictureBoxWidth, + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color choosen = ChooseColor(CatamaranFrame); + if (choosen != null) { + color = choosen; + } + DrawningCatamaran = new DrawningCatamaran(400, 700, + color, color, pictureBoxWidth, pictureBoxHeight, random.nextBoolean(), random.nextBoolean()); canv.DrawningCatamaran = DrawningCatamaran; comboBoxStrategy.enable(true); @@ -84,12 +106,23 @@ public class FormCatamaran { CreateButtonPro.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Random random = new Random(); - DrawningCatamaran = new DrawningCatamaranPro(random.nextInt(200,1000), random.nextDouble(1000,3000), - Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), + Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)); + Color choosen = ChooseColor(CatamaranFrame); + if (choosen != null) { + color = choosen; + } + choosen = ChooseColor(CatamaranFrame); + if (choosen != null) { + additionalColor = choosen; + } + DrawningCatamaran = new DrawningCatamaranPro(400, 700, + color, Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), pictureBoxWidth, pictureBoxHeight, random.nextBoolean(), random.nextBoolean(), - Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)), + additionalColor, random.nextBoolean(), random.nextBoolean()); canv.DrawningCatamaran = DrawningCatamaran; comboBoxStrategy.enable(true); @@ -145,6 +178,7 @@ public class FormCatamaran { DownButton.setBounds(774, 391, 50, 50); comboBoxStrategy.setBounds(719, 12, 151, 28); buttonStep.setBounds(768, 46, 94, 29); + buttonSelect.setBounds(383,401, 180, 40); canv = new Canvas(); canv.setSize(pictureBoxWidth, pictureBoxHeight); CatamaranFrame.add(canv); @@ -156,6 +190,7 @@ public class FormCatamaran { CatamaranFrame.add(CreateButton); CatamaranFrame.add(CreateButtonPro); CatamaranFrame.add(buttonStep); + CatamaranFrame.add(buttonSelect); CatamaranFrame.setVisible(true); } } diff --git a/FormCatamaranCollection.java b/FormCatamaranCollection.java new file mode 100644 index 0000000..3478490 --- /dev/null +++ b/FormCatamaranCollection.java @@ -0,0 +1,119 @@ +import java.awt.*; +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class FormCatamaranCollection { + private final CatamaranGenericCollection _catamaran; + private int pictureBoxWidth = 605; + private int pictureBoxHeight = 426; + CollectionCanvas canv; + + void Draw() { + if (canv == null) + return; + canv.repaint(); + } + + public FormCatamaranCollection() { + _catamaran = new CatamaranGenericCollection<>(pictureBoxWidth, pictureBoxHeight); + canv = new CollectionCanvas(); + JPanel toolBox = new JPanel(); + JFrame collectionFrame = new JFrame(); + collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + collectionFrame.setSize(880, 450); + toolBox.setBounds(623, 12, 227, 426); + canv.setBounds(12, 12, pictureBoxWidth, pictureBoxHeight); + JButton addButton = new JButton("Добавить"); + JButton removeButton = new JButton("Удалить"); + JButton refreshButton = new JButton("Обновить"); + JTextField catamaranNumb = new JTextField(); + GridLayout lay = new GridLayout(7, 1); + lay.setVgap(10); + toolBox.setLayout(lay); + toolBox.add(addButton); + toolBox.add(catamaranNumb); + toolBox.add(removeButton); + toolBox.add(refreshButton); + collectionFrame.add(toolBox); + collectionFrame.add(canv); + collectionFrame.setVisible(true); + + addButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_catamaran == null) { + return; + } + FormCatamaran form = new FormCatamaran(); + form.buttonSelect.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_catamaran.Insert(form.SelectedCatamaran()) != -1) { + JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", + JOptionPane.INFORMATION_MESSAGE); + form.SelectedCatamaran()._pictureWidth = pictureBoxWidth; + form.SelectedCatamaran()._pictureHeight = pictureBoxHeight; + Draw(); + } else { + JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", + JOptionPane.INFORMATION_MESSAGE); + } + canv._catamaran = _catamaran; + form.CatamaranFrame.dispose(); + Draw(); + } + }); + } + }); + removeButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_catamaran == null) { + return; + } + String tmp = catamaranNumb.getText(); + int numb; + + try { + numb = Integer.parseInt(tmp); + } catch (Exception ex) { + JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + _catamaran.Remove(numb); + _catamaran.ShowCatamaran(); + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + Draw(); + } + }); + refreshButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_catamaran == null) { + return; + } + _catamaran.ShowCatamaran(); + Draw(); + } + }); + } +} + +class CollectionCanvas extends JComponent { + public CatamaranGenericCollection _catamaran; + + public CollectionCanvas() { + } + + @Override + public void paintComponent(Graphics g) { + if (_catamaran == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + g2d.drawImage(_catamaran.ShowCatamaran(), 0, 0, this); + super.repaint(); + } +} \ No newline at end of file diff --git a/HardForm.java b/HardForm.java new file mode 100644 index 0000000..71320d0 --- /dev/null +++ b/HardForm.java @@ -0,0 +1,65 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Random; + +public class HardForm { + private Canvas canv; + + private int pictureBoxWidth; + private int pictureBoxHeight; + + private EntityCatamaran makeEntity() { + Random rand = new Random(); + return new EntityCatamaran(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), + Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)), + Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)), rand.nextBoolean(), + rand.nextBoolean()); + } + + void Draw() { + if (canv == null) + return; + canv.repaint(); + } + + private IDraw makeIDraw(EntityCatamaran entity) { + return new DrawningOars(0, 0, entity.OarColor()); + } + + public HardForm() { + Random rand = new Random(); + int sz = rand.nextInt(1, 10); + + JFrame HardFrame = new JFrame(); + HardFrame.setSize(700, 400); + HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + canv = new Canvas(); + canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); + JButton makeObject = new JButton("Создать"); + makeObject.setBounds(0, 0, 100, 40); + canv.add(makeObject); + HardFrame.setContentPane(canv); + HardFrame.setVisible(true); + pictureBoxHeight = canv.getHeight(); + pictureBoxWidth = canv.getWidth(); + HardGeneric toDraw = new HardGeneric<>(sz, + sz, pictureBoxWidth, pictureBoxHeight); + for (int i = 0; i < sz; i++) { + EntityCatamaran ent = makeEntity(); + toDraw.InsertFirst(ent); + toDraw.InsertSecond(makeIDraw(ent)); + } + makeObject.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + DrawningCatamaran DrawningCatamaran = toDraw.MakeObject(); + DrawningCatamaran.SetPosition(pictureBoxWidth / 2 - DrawningCatamaran.GetWidth() / 2, + pictureBoxHeight / 2 - DrawningCatamaran.GetHeight() / 2); + canv.DrawningCatamaran = DrawningCatamaran; + Draw(); + } + }); + } +} diff --git a/HardGeneric.java b/HardGeneric.java new file mode 100644 index 0000000..61a2a37 --- /dev/null +++ b/HardGeneric.java @@ -0,0 +1,55 @@ +import java.util.Random; + +public class HardGeneric { + T[] arrFirst; + U[] arrSecond; + + private int curSz; + private int CountFirst; + private int CountSecond; + + private int pictureBoxWidth; + + private int pictureBoxHeight; + + public HardGeneric(int countFirst, int countSecond, int width, int height) { + curSz = 0; + CountFirst = countFirst; + CountSecond = countSecond; + arrFirst = (T[]) new EntityCatamaran[CountFirst]; + arrSecond = (U[]) new IDraw[CountSecond]; + pictureBoxHeight = height; + pictureBoxWidth = width; + } + + public int InsertFirst(T entityMonorail) { + if (arrFirst[CountFirst - 1] != null) + return -1; + for (int i = curSz - 1; i >= 0; i--) { + arrFirst[i + 1] = arrFirst[i]; + arrSecond[i + 1] = arrSecond[i]; + } + curSz++; + arrFirst[0] = entityMonorail; + + return 0; + } + + public int InsertSecond(U inter) { + if (arrSecond[CountSecond - 1] != null) + return -1; + arrSecond[0] = inter; + return 0; + } + + public DrawningCatamaran MakeObject() { + Random rand = new Random(); + int indFirst = rand.nextInt(0, curSz); + int indSecond = rand.nextInt(0, curSz); + EntityCatamaran entity = arrFirst[indFirst]; + IDraw inter = arrSecond[indSecond]; + DrawningCatamaran catamaran = new DrawningCatamaran(entity.Speed(), entity.Weight(), entity.BodyColor(), + entity.OarColor(), pictureBoxWidth, pictureBoxHeight, entity.BodyKit(), entity.Seats()); + return catamaran; + } +} diff --git a/Main.java b/Main.java index b3e5de6..0fb0a94 100644 --- a/Main.java +++ b/Main.java @@ -2,6 +2,6 @@ import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { - new FormCatamaran(); + new FormCatamaranCollection(); } } diff --git a/MoveToBorder.java b/MoveToBorder.java index f82e0f9..1b31dbf 100644 --- a/MoveToBorder.java +++ b/MoveToBorder.java @@ -1,3 +1,4 @@ + public class MoveToBorder extends AbstractStrategy { @Override protected boolean IsTargetDestination() { diff --git a/MoveToCenter.java b/MoveToCenter.java index c5cdc6b..d4415d3 100644 --- a/MoveToCenter.java +++ b/MoveToCenter.java @@ -1,3 +1,4 @@ + public class MoveToCenter extends AbstractStrategy { @Override protected boolean IsTargetDestination() { diff --git a/ObjectParameters.java b/ObjectParameters.java index 7b517fe..de7e9ef 100644 --- a/ObjectParameters.java +++ b/ObjectParameters.java @@ -1,3 +1,5 @@ + + public class ObjectParameters { private final int _x; private final int _y; diff --git a/SetGeneric.java b/SetGeneric.java new file mode 100644 index 0000000..4ad3555 --- /dev/null +++ b/SetGeneric.java @@ -0,0 +1,53 @@ + + +public class SetGeneric { + private final Object[] _places; + + public int Count; + + public SetGeneric(int count){ + _places = new Object[count]; + Count = count; + } + + public int Insert(T catamaran){ + return Insert(catamaran, 0); + } + + public int Insert(T catamaran, int position){ + if(!(position >= 0 && position < Count)) + return -1; + if(_places[position] == null){ + _places[position] = catamaran; + } + else{ + int place = -1; + for(int i = position; i < Count; i++){ + if(_places[i] == null){ + place = i; + break; + } + } + if(place == -1) + return -1; + + for(int i = place - 1; i >= position; i--) + _places[i+1] = _places[i]; + _places[position] = catamaran; + } + return position; + } + + public boolean Remove(int position){ + if(!(position >= 0 && position < Count)) + return false; + _places[position] = null; + return true; + } + + public T Get(int position){ + if(!(position >= 0 && position < Count)) + return null; + return (T)_places[position]; + } +} diff --git a/Status.java b/Status.java index 6467ed7..14b68b4 100644 --- a/Status.java +++ b/Status.java @@ -1,3 +1,4 @@ + public enum Status { NotInit, InProgress,