From 2895044e8b1c7e42164cc0d43b812114b14893b8 Mon Sep 17 00:00:00 2001 From: ekallin Date: Sat, 21 Oct 2023 19:25:05 +0400 Subject: [PATCH] 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]; + } +}