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 @@ <?xml version="1.0" encoding="UTF-8"?> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectElectricLocomotive.FormElectricLocomotive"> - <grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="5" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="6" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <margin top="0" left="0" bottom="0" right="0"/> <constraints> <xy x="24" y="37" width="663" height="500"/> @@ -15,12 +15,12 @@ <children> <vspacer id="17764"> <constraints> - <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> + <grid row="3" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> </constraints> </vspacer> <component id="6cb47" class="javax.swing.JButton" binding="buttonCreateElectricLocomotive"> <constraints> - <grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + <grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> </constraints> <properties> <text value="Создать электролокомотив"/> @@ -28,7 +28,7 @@ </component> <component id="ca377" class="javax.swing.JButton" binding="buttonCreateLocomotive"> <constraints> - <grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + <grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> </constraints> <properties> <text value="Создать локомотив"/> @@ -56,7 +56,7 @@ </component> <component id="cc460" class="javax.swing.JButton" binding="buttonRight"> <constraints> - <grid row="4" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"> + <grid row="5" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"> <minimum-size width="30" height="30"/> <preferred-size width="30" height="30"/> <maximum-size width="30" height="30"/> @@ -71,7 +71,7 @@ </component> <component id="2fa92" class="javax.swing.JButton" binding="buttonDown"> <constraints> - <grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"> + <grid row="5" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"> <minimum-size width="30" height="30"/> <preferred-size width="30" height="30"/> <maximum-size width="30" height="30"/> @@ -86,7 +86,7 @@ </component> <component id="bbbb" class="javax.swing.JButton" binding="buttonUp"> <constraints> - <grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"> + <grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"> <minimum-size width="30" height="30"/> <preferred-size width="30" height="30"/> <maximum-size width="30" height="30"/> @@ -105,7 +105,7 @@ </component> <component id="326c5" class="javax.swing.JButton" binding="buttonLeft"> <constraints> - <grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"> + <grid row="5" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"> <minimum-size width="30" height="30"/> <preferred-size width="30" height="30"/> <maximum-size width="30" height="30"/> @@ -121,6 +121,14 @@ <hideActionText class="java.lang.Boolean" value="false"/> </clientProperties> </component> + <component id="7aad1" class="javax.swing.JButton" binding="ButtonSelectLocomotive"> + <constraints> + <grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <text value="Выбрать локо"/> + </properties> + </component> </children> </grid> </form> 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectElectricLocomotive.FormLocomotiveCollections"> + <grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <margin top="0" left="0" bottom="0" right="0"/> + <constraints> + <xy x="20" y="20" width="708" height="423"/> + </constraints> + <properties/> + <border type="none"/> + <children> + <grid id="c25f7" binding="pictureBoxCollections" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <margin top="0" left="0" bottom="0" right="0"/> + <constraints> + <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true"> + <minimum-size width="450" height="400"/> + <preferred-size width="450" height="400"/> + <maximum-size width="450" height="400"/> + </grid> + </constraints> + <properties/> + <border type="line"/> + <children/> + </grid> + <grid id="40d5" binding="Instruments" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> + <margin top="0" left="0" bottom="0" right="0"/> + <constraints> + <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true"> + <minimum-size width="160" height="400"/> + <preferred-size width="160" height="400"/> + <maximum-size width="160" height="400"/> + </grid> + </constraints> + <properties> + <enabled value="true"/> + </properties> + <border type="none"> + <font/> + </border> + <children> + <vspacer id="1eee0"> + <constraints> + <grid row="1" column="1" row-span="5" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> + </constraints> + </vspacer> + <component id="138d" class="javax.swing.JButton" binding="ButtonAddLocomotive"> + <constraints> + <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <text value="Добавить локо"/> + </properties> + </component> + <component id="1d01d" class="javax.swing.JButton" binding="ButtonRemoveLocomotive"> + <constraints> + <grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <text value="Удалить локо"/> + </properties> + </component> + <component id="25553" class="javax.swing.JTextField" binding="textField1" default-binding="true"> + <constraints> + <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> + <preferred-size width="150" height="-1"/> + </grid> + </constraints> + <properties> + <text value="-"/> + </properties> + </component> + <component id="b21cb" class="javax.swing.JButton" binding="ButtonRefreshCollection"> + <constraints> + <grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> + </constraints> + <properties> + <text value="Обновить "/> + </properties> + </component> + </children> + </grid> + </children> + </grid> +</form> 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<DrawingLocomotive, DrawingObjectLocomotive> _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<T extends DrawingLocomotive,U extends IMoveableObject> +{ + //ширина/высота окна + private final int _pictureWidth; + private final int _pictureHeight; + //ширина/высота занимаемого места + private final int _placeSizeWidth = 85; + private final int _placeSizeHeight = 50; + + /// Набор объектов + private final SetGeneric<T> _collection; + + public LocomotiveGenericCollection(int picWidth, int picHeight) + { + // немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло... + // высчитываем размер массива для setgeneric + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric<T>(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<T extends DrawingLocomotive>{ + 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]; + } +}