From 2895044e8b1c7e42164cc0d43b812114b14893b8 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sat, 21 Oct 2023 19:25:05 +0400 Subject: [PATCH 1/4] bad code, with comment and without modifications. --- .../DrawingLocomotive.java | 4 + .../FormElectricLocomotive.form | 24 ++++-- .../FormElectricLocomotive.java | 24 ++++-- .../FormLocomotiveCollections.form | 83 ++++++++++++++++++ .../FormLocomotiveCollections.java | 59 +++++++++++++ .../LocomotiveGenericCollection.java | 86 +++++++++++++++++++ .../MainFrameElectricLocomotive.java | 7 +- ProjectElectricLocomotive/SetGeneric.java | 63 ++++++++++++++ 8 files changed, 333 insertions(+), 17 deletions(-) create mode 100644 ProjectElectricLocomotive/FormLocomotiveCollections.form create mode 100644 ProjectElectricLocomotive/FormLocomotiveCollections.java create mode 100644 ProjectElectricLocomotive/LocomotiveGenericCollection.java create mode 100644 ProjectElectricLocomotive/SetGeneric.java diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java index 3e7b17b..c794f80 100644 --- a/ProjectElectricLocomotive/DrawingLocomotive.java +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -202,4 +202,8 @@ public class DrawingLocomotive { } return false; } + + public IMoveableObject GetMoveableObject() { + return new DrawingObjectLocomotive(this); + } } diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.form b/ProjectElectricLocomotive/FormElectricLocomotive.form index 9b1c126..2734e4b 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.form +++ b/ProjectElectricLocomotive/FormElectricLocomotive.form @@ -1,6 +1,6 @@
- + @@ -15,12 +15,12 @@ - + - + @@ -28,7 +28,7 @@ - + @@ -56,7 +56,7 @@ - + @@ -71,7 +71,7 @@ - + @@ -86,7 +86,7 @@ - + @@ -105,7 +105,7 @@ - + @@ -121,6 +121,14 @@ + + + + + + + + diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index dfe35f0..7250958 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -6,7 +6,7 @@ import java.awt.event.ActionListener; import java.util.Random; public class FormElectricLocomotive { - DrawingLocomotive _drawingLocomotive; + public DrawingLocomotive _drawingLocomotive; AbstractStrategy _abstractStrategy; private JButton buttonCreateElectricLocomotive; private JPanel pictureBox; @@ -17,6 +17,9 @@ public class FormElectricLocomotive { public JComboBox comboBoxStrategy; private JButton buttonStep; private JButton buttonCreateLocomotive; + private JButton ButtonSelectLocomotive; + public DrawingLocomotive SelectedLocomotive; + public boolean IsSelect = false; public JPanel getPictureBox() { return pictureBox; @@ -30,11 +33,9 @@ public class FormElectricLocomotive { buttonCreateLocomotive.addActionListener(e -> { Random rnd = new Random(); + Color color = JColorChooser.showDialog(null, "Цвет", null); _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), - rnd.nextInt(1000, 3000), - new Color(rnd.nextInt(0, 256), - rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), + rnd.nextInt(1000, 3000), color, pictureBox.getWidth(), pictureBox.getHeight()); @@ -45,11 +46,14 @@ public class FormElectricLocomotive { buttonCreateElectricLocomotive.addActionListener(e -> { Random random = new Random(); + Color color = JColorChooser.showDialog(null, "Цвет", null); + Color addColor = JColorChooser.showDialog(null, "Цвет2", null); + _drawingLocomotive = new DrawingElectricLocomotive( 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, + addColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), @@ -61,6 +65,12 @@ public class FormElectricLocomotive { Draw(); }); + ButtonSelectLocomotive.addActionListener(e->{ + SelectedLocomotive = _drawingLocomotive; + IsSelect = true; +// DialogResult = DialogResult.OK; + }); + buttonStep.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form new file mode 100644 index 0000000..abfa939 --- /dev/null +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -0,0 +1,83 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java new file mode 100644 index 0000000..4b55913 --- /dev/null +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -0,0 +1,59 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; +import java.awt.*; + +public class FormLocomotiveCollections extends JFrame { + private JPanel MainPanel; + private JPanel pictureBoxCollections; + private JPanel Instruments; + private JButton ButtonAddLocomotive; + private JTextField textField1; + private JButton ButtonRefreshCollection; + private JButton ButtonRemoveLocomotive; + public DrawingLocomotive loco; + FormElectricLocomotive _formElectricLocomotive; + private final LocomotiveGenericCollection _locomotives; + public JPanel getPictureBoxCollections() { + return MainPanel; + } + public FormLocomotiveCollections() + { + _locomotives = new LocomotiveGenericCollection<>(pictureBoxCollections.getWidth(), + pictureBoxCollections.getHeight()); + + ButtonAddLocomotive.addActionListener(e->{ + FormElectricLocomotive form = new FormElectricLocomotive(); + VisFormElectricLocomotive(); + + if (_locomotives == null) return; + + loco = form._drawingLocomotive; + if(loco != null) { + //проверяем, удалось ли нам загрузить объект + if (_locomotives.AddOverload(loco) != -1 && form.IsSelect == true) { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); + Refresh(); + //pictureBoxCollections = _locomotives.ShowLocomotives(); + } else { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект"); + } + } + }); + } + public void VisFormElectricLocomotive(){ + setTitle("ElectroLoco"); + _formElectricLocomotive = new FormElectricLocomotive(); + setContentPane(_formElectricLocomotive.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(200, 500); + pack(); + setVisible(true); + } + + public void Refresh(){ + Graphics g = pictureBoxCollections.getGraphics(); + pictureBoxCollections.paint(g); + _locomotives.ShowLocomotives(g); + } +} diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java new file mode 100644 index 0000000..535464c --- /dev/null +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -0,0 +1,86 @@ +package ProjectElectricLocomotive; + +import java.awt.*; + +public class LocomotiveGenericCollection +{ + //ширина/высота окна + private final int _pictureWidth; + private final int _pictureHeight; + //ширина/высота занимаемого места + private final int _placeSizeWidth = 85; + private final int _placeSizeHeight = 50; + + /// Набор объектов + private final SetGeneric _collection; + + public LocomotiveGenericCollection(int picWidth, int picHeight) + { + // немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло... + // высчитываем размер массива для setgeneric + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width*height); + } + + /// Перегрузка оператора сложения + //да емае, почему в яве все по-другому?... + + public int AddOverload(T loco){ + if(loco == null){ + return -1; + } + return _collection.Insert(loco); + } + + public T SubOverload(int pos){ + return _collection.Remove(pos); + } + + // получение объекта imoveableObj + public U GetU(int pos) +{ + return (U)_collection.Get(pos).GetMoveableObject(); +} + + /// Вывод всего набора объектов + public void ShowLocomotives(Graphics gr) + { + DrawBackground(gr); + DrawObjects(gr); + } + private void DrawBackground(Graphics g) + { + Color blackColor = Color.BLACK; + g.setColor(blackColor); + 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); + } + } + + private void DrawObjects(Graphics g) + { + int HeightObjCount = _pictureHeight / _placeSizeHeight; + int WidthObjCount = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _collection.Count(); i++) + { + T type = _collection.Get(i); + if (type != null) + { + type.SetPosition( + (int)(i / HeightObjCount * _placeSizeWidth), + (HeightObjCount - 1) * _placeSizeHeight - (int)(i % HeightObjCount * _placeSizeHeight) + ); + type.DrawTransport(g); + } + } + } +} diff --git a/ProjectElectricLocomotive/MainFrameElectricLocomotive.java b/ProjectElectricLocomotive/MainFrameElectricLocomotive.java index 9dcfab4..4d4d1b4 100644 --- a/ProjectElectricLocomotive/MainFrameElectricLocomotive.java +++ b/ProjectElectricLocomotive/MainFrameElectricLocomotive.java @@ -4,13 +4,16 @@ import javax.swing.*; public class MainFrameElectricLocomotive extends JFrame { private FormElectricLocomotive _formElectricLocomotive; + private FormLocomotiveCollections _formLocomotiveCollections; public MainFrameElectricLocomotive() { super(); setTitle("ElectroLoco"); setDefaultCloseOperation(EXIT_ON_CLOSE); - _formElectricLocomotive = new FormElectricLocomotive(); - setContentPane(_formElectricLocomotive.getPictureBox()); + //_formElectricLocomotive = new FormElectricLocomotive(); + _formLocomotiveCollections = new FormLocomotiveCollections(); + //setContentPane(_formElectricLocomotive.getPictureBox()); + setContentPane(_formLocomotiveCollections.getPictureBoxCollections()); setDefaultLookAndFeelDecorated(false); setLocation(500, 200); pack(); diff --git a/ProjectElectricLocomotive/SetGeneric.java b/ProjectElectricLocomotive/SetGeneric.java new file mode 100644 index 0000000..3d0e778 --- /dev/null +++ b/ProjectElectricLocomotive/SetGeneric.java @@ -0,0 +1,63 @@ +package ProjectElectricLocomotive; + +public class SetGeneric{ + private T[] _places; + public int Count(){ + return _places.length; + } + public SetGeneric(int count) { + _places = (T[]) new DrawingLocomotive[count]; + } + + public int Insert(T loco) + { + return Insert(loco, 0); + } + + public int Insert(T loco, int position) + { + int NoEmpty = 0, temp = 0; + for (int i = position; i < Count(); i++) + { + if (_places[i] != null) NoEmpty++; + } + if (NoEmpty == Count() - position - 1) return -1; + + if (position < Count() && position >= 0) + { + for (int j = position; j < Count(); j++) + { + if (_places[j] == null) + { + temp = j; + break; + } + } + // shift right + for (int i = temp; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = loco; + return position; + } + return -1; + } + + public T Remove(int position) + { + if (position >= Count() || position < 0) + return null; + + T tmp = _places[position]; + _places[position] = null; + return tmp; + } + + public T Get(int position) + { + // TODO проверка позиции + if (position < 0 || position >= Count()) return null; + return _places[position]; + } +} From f69fe262f844cf7f2925e2fa14ebce724c3dd653 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sat, 21 Oct 2023 22:32:22 +0400 Subject: [PATCH 2/4] Code almost ready --- .../FormElectricLocomotive.java | 2 +- .../FormLocomotiveCollections.form | 2 +- .../FormLocomotiveCollections.java | 77 +++++++++++-------- .../FrameElectricLocomotive.java | 18 +++++ .../FrameLocomotiveCollection.java | 18 +++++ .../LocomotiveGenericCollection.java | 2 +- ProjectElectricLocomotive/Main.java | 2 +- .../MainFrameElectricLocomotive.java | 22 ------ 8 files changed, 87 insertions(+), 56 deletions(-) create mode 100644 ProjectElectricLocomotive/FrameElectricLocomotive.java create mode 100644 ProjectElectricLocomotive/FrameLocomotiveCollection.java delete mode 100644 ProjectElectricLocomotive/MainFrameElectricLocomotive.java diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index 7250958..fa27453 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -17,7 +17,7 @@ public class FormElectricLocomotive { public JComboBox comboBoxStrategy; private JButton buttonStep; private JButton buttonCreateLocomotive; - private JButton ButtonSelectLocomotive; + public JButton ButtonSelectLocomotive; public DrawingLocomotive SelectedLocomotive; public boolean IsSelect = false; diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index abfa939..28e0da3 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -58,7 +58,7 @@ - + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 4b55913..9b3028c 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -3,55 +3,72 @@ package ProjectElectricLocomotive; import javax.swing.*; import java.awt.*; -public class FormLocomotiveCollections extends JFrame { +public class FormLocomotiveCollections { private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; private JButton ButtonAddLocomotive; - private JTextField textField1; + private JTextField textFieldNumber; private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; public DrawingLocomotive loco; FormElectricLocomotive _formElectricLocomotive; private final LocomotiveGenericCollection _locomotives; + public JPanel getPictureBoxCollections() { return MainPanel; } - public FormLocomotiveCollections() - { - _locomotives = new LocomotiveGenericCollection<>(pictureBoxCollections.getWidth(), - pictureBoxCollections.getHeight()); - ButtonAddLocomotive.addActionListener(e->{ - FormElectricLocomotive form = new FormElectricLocomotive(); - VisFormElectricLocomotive(); + public FormLocomotiveCollections() { + _locomotives = new LocomotiveGenericCollection<>(400, 300); - if (_locomotives == null) return; - loco = form._drawingLocomotive; - if(loco != null) { - //проверяем, удалось ли нам загрузить объект - if (_locomotives.AddOverload(loco) != -1 && form.IsSelect == true) { - JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); - Refresh(); - //pictureBoxCollections = _locomotives.ShowLocomotives(); - } else { - JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект"); + ButtonAddLocomotive.addActionListener(e -> { + FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive(); + frameElectricLocomotive.setVisible(true); + frameElectricLocomotive._formLocomotiveCollection.ButtonSelectLocomotive.addActionListener(e2 -> { + loco = frameElectricLocomotive._formLocomotiveCollection._drawingLocomotive; + frameElectricLocomotive.dispose(); + if (loco != null) { + //проверяем, удалось ли нам загрузить объект + if (_locomotives.AddOverload(loco) != -1) { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); + Refresh(); + } else { + JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект"); + } } + }); + }); + + ButtonRemoveLocomotive.addActionListener(e->{ + try { + int pos = Integer.parseInt(textFieldNumber.getText()); + if (_locomotives.SubOverload(pos) != null) { + Refresh(); + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Объект удален", + "Успех", + JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Не удалось удалить объект", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + } + } catch (Exception ex) { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Неверное значение", + "Ошибка", + JOptionPane.ERROR_MESSAGE); } }); - } - public void VisFormElectricLocomotive(){ - setTitle("ElectroLoco"); - _formElectricLocomotive = new FormElectricLocomotive(); - setContentPane(_formElectricLocomotive.getPictureBox()); - setDefaultLookAndFeelDecorated(false); - setLocation(200, 500); - pack(); - setVisible(true); - } - public void Refresh(){ + ButtonRefreshCollection.addActionListener(e->{ + Refresh(); + }); + } + public void Refresh() { Graphics g = pictureBoxCollections.getGraphics(); pictureBoxCollections.paint(g); _locomotives.ShowLocomotives(g); diff --git a/ProjectElectricLocomotive/FrameElectricLocomotive.java b/ProjectElectricLocomotive/FrameElectricLocomotive.java new file mode 100644 index 0000000..abf9d6b --- /dev/null +++ b/ProjectElectricLocomotive/FrameElectricLocomotive.java @@ -0,0 +1,18 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; + +public class FrameElectricLocomotive extends JFrame { + public FormElectricLocomotive _formLocomotiveCollection; + public FrameElectricLocomotive() { + super(); + setTitle("ElectroLoco"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + _formLocomotiveCollection = new FormElectricLocomotive(); + setContentPane(_formLocomotiveCollection.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(500, 200); + pack(); + setVisible(true); + } +} diff --git a/ProjectElectricLocomotive/FrameLocomotiveCollection.java b/ProjectElectricLocomotive/FrameLocomotiveCollection.java new file mode 100644 index 0000000..7883880 --- /dev/null +++ b/ProjectElectricLocomotive/FrameLocomotiveCollection.java @@ -0,0 +1,18 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; + +public class FrameLocomotiveCollection extends JFrame { + public FormLocomotiveCollections _formLocomotiveCollections; + public FrameLocomotiveCollection(){ + super(); + setTitle("LocoCollection"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + _formLocomotiveCollections = new FormLocomotiveCollections(); + setContentPane(_formLocomotiveCollections.getPictureBoxCollections()); + setDefaultLookAndFeelDecorated(false); + setLocation(400, 50); + pack(); + setVisible(true); + } +} diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index 535464c..9abe2b2 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -8,7 +8,7 @@ public class LocomotiveGenericCollection Date: Mon, 23 Oct 2023 12:05:54 +0400 Subject: [PATCH 3/4] Lab 4 base on C# in lab 4 hard on java --- .../ProjectElectricLocomotive/img/arrowDown.jpg | Bin 0 -> 702 bytes .../ProjectElectricLocomotive/img/arrowLeft.jpg | Bin 0 -> 706 bytes .../ProjectElectricLocomotive/img/arrowRight.jpg | Bin 0 -> 702 bytes .../ProjectElectricLocomotive/img/arrowUp.jpg | Bin 0 -> 706 bytes 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowDown.jpg create mode 100644 out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowLeft.jpg create mode 100644 out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowRight.jpg create mode 100644 out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowUp.jpg diff --git a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowDown.jpg b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowDown.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2d41e934bd5fb7cb8c8714d8dc7db3f6fbb203db GIT binary patch literal 702 zcmex=kSFKvnvHC#j1O51viho)>VwJvM^~|F+7hZR9eClQsBo=7c zWAL{6s$lA@$_Gtm>R(kRZR~X|o+u(wA@S_*sjY0B3%{q8-tfqHdSS|qsYllpoVMB? z+4iQ6;pvnw3Tp1!UG1-p>NBKex|QpqdWv>mbuxau`-D92j;G9Jd9}6MZSu(>GMy6LE_*g9V zUH_eYL@u;+PHDE+Te+h@uAErIVfNcI`&IwNlP0|{jeL#YY*@A9R=-upr{_tcUe~vO zUN+N5{6VN1hjZbp`rz$s2XB9kbE;{+dck^I9>avK=XP@3SgWz?zDr8rhFP~R-8g3Q i`_gS;d$-q*fB$E25x-z@)st)O0Sk792QvKsZvp@o0^z^_ literal 0 HcmV?d00001 diff --git a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowLeft.jpg b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowLeft.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5f03da1e254208cb67698990b3e6146f3b2c352b GIT binary patch literal 706 zcmex=X_-jEpc@CT5UB85uZ)ggFI8M8$xLL=dtt zRUlD8xE{FZEe0NDMxe#af(-Ty?;ktt+OoE5(}ZTbTj8NI8J52`TQ~pmM6bADQ`7Bb zbC|f^M=VLuSeG^@(D8{*kz@m(?URUO&MJ@2t<~0XVNHoJi25zWeq>@9|D7_w=5O~x zKK-0M@jH+0tFsU6HgP{m_|e@_{Mw3*udz5MDj>^g&8EEBzTbalosv87SaageKg%`$ zeB0vL6SU;tBnD&O2Z3t*jR^urk6&=oc`k9n;iv1i^E#_feXf0SaGfvjt=NjWGKcd+ zx)`QCTR0(`=f`YS{e@l&GVY}E)i3GU;=*&jw`Xa^w=xmlphJhN%I0smU6!A>@cw^> z2`U%A-ri=)HAVCfK{(3EuKTo_!! z)+Fhtc+}Eul^&ap@l4QOGIi&SGSQuGGlJDEo=5EZJoSn8 z4KBteC*_`y2@BPvrYvO+Ywe7Svs_zYclA`_*6o~+FL|x!4bACPO3hz?#cH12sh<7) mva3RmeQxOIh;wh`WN>WhZok^{YLn<=hYViE1t#+UZvp^{OyCXx literal 0 HcmV?d00001 diff --git a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowRight.jpg b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowRight.jpg new file mode 100644 index 0000000000000000000000000000000000000000..41134aa26e21aa8247198a4d35962be34118bae3 GIT binary patch literal 702 zcmex=WICV(Up}Vflj8 zU1}Uok*Ag2BGL<)w`tn_ONm_Uwrch5H+puA2P`~9{IV+J4(kre4==iRrYqFDn?bH zZBtDLkGE zQ1R6K=J#&u-uY$Bi??sM)g9|`XS(B$!;W2|6H}(LDPL18>S>$9GSlz0`kym_PVvbB z&e}^RxlL*jG00di{KxN(G<*KlGT|GxvCMlm7qGOhD?Q8qrL)qEh$r6{} gd*}H4b9;UL_rD|J4=k>FvaLN}!OrkNhX4Of0J5yy$p8QV literal 0 HcmV?d00001 diff --git a/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowUp.jpg b/out/production/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/ProjectElectricLocomotive/img/arrowUp.jpg new file mode 100644 index 0000000000000000000000000000000000000000..74ea15e1491c7584847014bff2d4997206cf343d GIT binary patch literal 706 zcmex=P*_A%OcAI`1R)JG z10*VlY9vtV|1Aa{W=5dJ%z_N|4DTO1?Ao%nYtw{gyIbL*GZ~h@He1hsd7@Wbu&L?x zN~-&SE<*2Ec?4RS9j(a z4Ncy?KGHs=(n_tZE4{>TdOPW7zx5JRHu=nz!>B&xSpUuQi5iEFx;8Isn&R;*`@~AA znUkEp?)W!xo7J_jD7UIxdx}^~CT%wsT9)2xsdsR>%xm$uqD@>`<;Q%41ZA95m4yj}wwi>q=v>`K#q{7Dr(VRJ^p z`M&J$7wYG(nk~%s(LGZA{r;vf*58{-VM8QluF1G-N1ieR7Z1-L+`_VsfYlZUN#g`Wyn>L}(Ic!UVWw^r$nW-N|nvDEE kGxcbOEK#%AnykoO&|_K^lQ2Kmj_Gg)FXI9ew&wpg0f?>D%m4rY literal 0 HcmV?d00001 From 34f72ac391a45d78c66b70f63bcccd49e77d2408 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sun, 29 Oct 2023 17:10:37 +0400 Subject: [PATCH 4/4] Lab 3 Ready hard --- .../DopClassParameters.java | 65 ++++++++++++++ .../DrawingElectricLocomotive.java | 13 ++- .../DrawingLocomotive.java | 21 ++++- .../FormDopClassParameters.form | 38 ++++++++ .../FormDopClassParameters.java | 86 +++++++++++++++++++ .../FormElectricLocomotive.java | 1 - .../FormLocomotiveCollections.form | 12 ++- .../FormLocomotiveCollections.java | 17 +++- .../FrameDopClassParameters.java | 19 ++++ .../FrameElectricLocomotive.java | 4 +- .../FrameLocomotiveCollection.java | 3 +- .../LocomotiveGenericCollection.java | 3 - 12 files changed, 262 insertions(+), 20 deletions(-) create mode 100644 ProjectElectricLocomotive/DopClassParameters.java create mode 100644 ProjectElectricLocomotive/FormDopClassParameters.form create mode 100644 ProjectElectricLocomotive/FormDopClassParameters.java create mode 100644 ProjectElectricLocomotive/FrameDopClassParameters.java diff --git a/ProjectElectricLocomotive/DopClassParameters.java b/ProjectElectricLocomotive/DopClassParameters.java new file mode 100644 index 0000000..23fb7e8 --- /dev/null +++ b/ProjectElectricLocomotive/DopClassParameters.java @@ -0,0 +1,65 @@ +package ProjectElectricLocomotive; + +import java.util.ArrayList; +import java.util.Random; + +public class DopClassParameters { + public T[] _entityLocomotive; + public U[] _idrawningWheels; + + int _locomotivesCount; + int _idrawningWheelsCount; + + private int _pictureWidth; + private int _pictureHeight; + + public DopClassParameters(int count, int width, int height) { + _entityLocomotive = (T[]) new EntityLocomotive[count]; + _idrawningWheels = (U[]) new IDrawingWheels[count]; + _pictureWidth = width; + _pictureHeight = height; + _locomotivesCount = 0; + _idrawningWheelsCount = 0; + } + + public void Add(T entityLocoObj) { + if (_locomotivesCount < _entityLocomotive.length) { + _entityLocomotive[_locomotivesCount] = entityLocoObj; + _locomotivesCount++; + } + } + + public void Add(U idrawingWheels) { + if (_idrawningWheelsCount < _idrawningWheels.length) { + _idrawningWheels[_idrawningWheelsCount] = idrawingWheels; + _idrawningWheelsCount++; + } + } + + public DrawingLocomotive RandomLocomotive(int pictureWidth, int pictureHeight) { + Random rnd = new Random(); + if (_entityLocomotive == null || _idrawningWheels == null) { + return null; + } + T entityLocomotive = _entityLocomotive[rnd.nextInt(_locomotivesCount)]; + U idrawingWheels = _idrawningWheels[rnd.nextInt(_idrawningWheelsCount)]; + + DrawingLocomotive drawLocomotive; + if (entityLocomotive instanceof EntityElectricLocomotive) { + drawLocomotive = new DrawingElectricLocomotive( + (EntityElectricLocomotive) entityLocomotive, + idrawingWheels, + pictureWidth, + pictureHeight + ); + } else { + drawLocomotive = new DrawingLocomotive( + entityLocomotive, + idrawingWheels, + pictureWidth, + pictureHeight + ); + } + return drawLocomotive; + } +} diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java index 004ca7e..19db7f5 100644 --- a/ProjectElectricLocomotive/DrawingElectricLocomotive.java +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -1,6 +1,7 @@ package ProjectElectricLocomotive; import java.awt.*; public class DrawingElectricLocomotive extends DrawingLocomotive { + public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries, int width, int height) { @@ -10,16 +11,22 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); } } + + public DrawingElectricLocomotive(EntityElectricLocomotive entityElectricLocomotive, IDrawingWheels iDrawingWheels, + int width, int height) + { + super(entityElectricLocomotive,iDrawingWheels, width, height, 150, 50); + } @Override public void DrawTransport(Graphics g) { if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF { - Color colorBlack = Color.BLACK; + Color addColor = electricLocomotive.AdditionalColor; if (electricLocomotive.Horns) { //horns - g.setColor(colorBlack); + g.setColor(addColor); g.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); g.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); @@ -28,7 +35,7 @@ public class DrawingElectricLocomotive extends DrawingLocomotive { } if (electricLocomotive.SeifBatteries) { - g.setColor(colorBlack); + g.setColor(addColor); g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10); } super.DrawTransport(g); diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java index c794f80..f301d5f 100644 --- a/ProjectElectricLocomotive/DrawingLocomotive.java +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -26,14 +26,14 @@ public class DrawingLocomotive { return _locoHeight; } - public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth) + public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int height) { - if (width < _locoWidth || heigth < _locoHeight) + if (width < _locoWidth || height < _locoHeight) { return; } _pictureWidth = width; - _pictureHeight = heigth; + _pictureHeight = height; EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); Random rnd = new Random(); @@ -58,6 +58,21 @@ public class DrawingLocomotive { _locoHeight = locoHeight; } + protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels, + int width, int height){ + EntityLocomotive = entityLocomotive; + _drawingWheels = iDrawingWheels; + _pictureWidth = width; + _pictureHeight = height; + } + + protected DrawingLocomotive(EntityLocomotive entityLocomotive, IDrawingWheels iDrawingWheels, + int width, int height, int locoWidth, int locoHeight){ + this(entityLocomotive, iDrawingWheels, width, height); + _locoWidth = locoWidth; + _locoHeight = locoHeight; + } + public void SetWheelsCount(int wheelsCount){ _drawingWheels.SetWheelsCount(wheelsCount); } diff --git a/ProjectElectricLocomotive/FormDopClassParameters.form b/ProjectElectricLocomotive/FormDopClassParameters.form new file mode 100644 index 0000000..e0de2f6 --- /dev/null +++ b/ProjectElectricLocomotive/FormDopClassParameters.form @@ -0,0 +1,38 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectElectricLocomotive/FormDopClassParameters.java b/ProjectElectricLocomotive/FormDopClassParameters.java new file mode 100644 index 0000000..1c842d5 --- /dev/null +++ b/ProjectElectricLocomotive/FormDopClassParameters.java @@ -0,0 +1,86 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormDopClassParameters { + private JButton ButtonGenerationRandomLocomotive; + private JPanel pictureBoxGenerated; + Random rnd; + DrawingLocomotive drawingLocomotive; + DopClassParameters _dopClassParameters; + + public JPanel getPictureBoxGenerated() { + return pictureBoxGenerated; + } + + public FormDopClassParameters() { + pictureBoxGenerated.setPreferredSize(new Dimension(400, 300)); + _dopClassParameters = new DopClassParameters<>( + 10, + pictureBoxGenerated.getWidth(), + pictureBoxGenerated.getHeight() + ); + + ButtonGenerationRandomLocomotive.addActionListener(e -> { + AddEntityLocomotive(); + AddRandomWheels(); + + drawingLocomotive = _dopClassParameters.RandomLocomotive( + pictureBoxGenerated.getWidth(), + pictureBoxGenerated.getHeight() + ); + drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + Draw(); + }); + } + + public void AddEntityLocomotive() { + rnd = new Random(); + EntityLocomotive entityLocomotive; + Color color = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); + if (rnd.nextBoolean()) { + entityLocomotive = new EntityLocomotive( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + color + ); + } else { + entityLocomotive = new EntityElectricLocomotive( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + color, + color, + rnd.nextBoolean(), + rnd.nextBoolean() + ); + } + _dopClassParameters.Add(entityLocomotive); + } + + public void AddRandomWheels() { + rnd = new Random(); + IDrawingWheels iDrawingWheels; + int wheelsChoice = rnd.nextInt(0, 3); + if (wheelsChoice == 0) { + iDrawingWheels = new DrawingWheel(); + } else if (wheelsChoice == 1) { + iDrawingWheels = new DrawingEmptyWheels(); + } else { + iDrawingWheels = new DrawingWheelsBlueCrom(); + } + iDrawingWheels.SetWheelsCount(rnd.nextInt(2, 5)); + _dopClassParameters.Add(iDrawingWheels); + } + + public void Draw() { + if (drawingLocomotive.EntityLocomotive == null) { + return; + } + Graphics g = pictureBoxGenerated.getGraphics(); + pictureBoxGenerated.paint(g); + drawingLocomotive.DrawTransport(g); + } + +} diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index fa27453..1b9ae8b 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -68,7 +68,6 @@ public class FormElectricLocomotive { ButtonSelectLocomotive.addActionListener(e->{ SelectedLocomotive = _drawingLocomotive; IsSelect = true; -// DialogResult = DialogResult.OK; }); buttonStep.addActionListener(new ActionListener() { diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index 28e0da3..b1bcaa2 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -21,7 +21,7 @@
- + @@ -39,7 +39,7 @@ - + @@ -76,6 +76,14 @@ + + + + + + + + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 9b3028c..3a48f7f 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -4,6 +4,7 @@ import javax.swing.*; import java.awt.*; public class FormLocomotiveCollections { + FrameDopClassParameters frameDopClassParameters; private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; @@ -11,9 +12,9 @@ public class FormLocomotiveCollections { private JTextField textFieldNumber; private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; + private JButton ButtonCreateRandomLoco; public DrawingLocomotive loco; - FormElectricLocomotive _formElectricLocomotive; - private final LocomotiveGenericCollection _locomotives; + LocomotiveGenericCollection _locomotives; public JPanel getPictureBoxCollections() { return MainPanel; @@ -41,7 +42,13 @@ public class FormLocomotiveCollections { }); }); - ButtonRemoveLocomotive.addActionListener(e->{ + ButtonCreateRandomLoco.addActionListener(e->{ + if(frameDopClassParameters!=null) frameDopClassParameters.dispose(); + frameDopClassParameters = new FrameDopClassParameters(); + frameDopClassParameters.setVisible(true); + }); + + ButtonRemoveLocomotive.addActionListener(e -> { try { int pos = Integer.parseInt(textFieldNumber.getText()); if (_locomotives.SubOverload(pos) != null) { @@ -64,10 +71,12 @@ public class FormLocomotiveCollections { } }); - ButtonRefreshCollection.addActionListener(e->{ + ButtonRefreshCollection.addActionListener(e -> { Refresh(); }); + } + public void Refresh() { Graphics g = pictureBoxCollections.getGraphics(); pictureBoxCollections.paint(g); diff --git a/ProjectElectricLocomotive/FrameDopClassParameters.java b/ProjectElectricLocomotive/FrameDopClassParameters.java new file mode 100644 index 0000000..e5a06ac --- /dev/null +++ b/ProjectElectricLocomotive/FrameDopClassParameters.java @@ -0,0 +1,19 @@ +package ProjectElectricLocomotive; + +import javax.swing.*; + +public class FrameDopClassParameters extends JFrame { + public FormDopClassParameters _formDopClassParameters; + + public FrameDopClassParameters(){ + super(); + setTitle("Рандомный локомотив"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + _formDopClassParameters = new FormDopClassParameters(); + setContentPane(_formDopClassParameters.getPictureBoxGenerated()); + setDefaultLookAndFeelDecorated(false); + setLocation(500, 200); + pack(); + setVisible(true); + } +} diff --git a/ProjectElectricLocomotive/FrameElectricLocomotive.java b/ProjectElectricLocomotive/FrameElectricLocomotive.java index abf9d6b..9344738 100644 --- a/ProjectElectricLocomotive/FrameElectricLocomotive.java +++ b/ProjectElectricLocomotive/FrameElectricLocomotive.java @@ -6,8 +6,8 @@ public class FrameElectricLocomotive extends JFrame { public FormElectricLocomotive _formLocomotiveCollection; public FrameElectricLocomotive() { super(); - setTitle("ElectroLoco"); - setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle("Электролокомотив"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); _formLocomotiveCollection = new FormElectricLocomotive(); setContentPane(_formLocomotiveCollection.getPictureBox()); setDefaultLookAndFeelDecorated(false); diff --git a/ProjectElectricLocomotive/FrameLocomotiveCollection.java b/ProjectElectricLocomotive/FrameLocomotiveCollection.java index 7883880..189aba5 100644 --- a/ProjectElectricLocomotive/FrameLocomotiveCollection.java +++ b/ProjectElectricLocomotive/FrameLocomotiveCollection.java @@ -6,8 +6,7 @@ public class FrameLocomotiveCollection extends JFrame { public FormLocomotiveCollections _formLocomotiveCollections; public FrameLocomotiveCollection(){ super(); - setTitle("LocoCollection"); - setDefaultCloseOperation(EXIT_ON_CLOSE); + setTitle("Коллекция");setDefaultCloseOperation(EXIT_ON_CLOSE); _formLocomotiveCollections = new FormLocomotiveCollections(); setContentPane(_formLocomotiveCollections.getPictureBoxCollections()); setDefaultLookAndFeelDecorated(false); diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index 9abe2b2..cf1207c 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -10,14 +10,11 @@ public class LocomotiveGenericCollection _collection; public LocomotiveGenericCollection(int picWidth, int picHeight) { - // немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло... - // высчитываем размер массива для setgeneric int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _pictureWidth = picWidth;