bad code, with comment and without modifications.

This commit is contained in:
ekallin 2023-10-21 19:25:05 +04:00
parent 37a3d7d8d2
commit 2895044e8b
8 changed files with 333 additions and 17 deletions

View File

@ -202,4 +202,8 @@ public class DrawingLocomotive {
} }
return false; return false;
} }
public IMoveableObject GetMoveableObject() {
return new DrawingObjectLocomotive(this);
}
} }

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectElectricLocomotive.FormElectricLocomotive"> <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"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="24" y="37" width="663" height="500"/> <xy x="24" y="37" width="663" height="500"/>
@ -15,12 +15,12 @@
<children> <children>
<vspacer id="17764"> <vspacer id="17764">
<constraints> <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> </constraints>
</vspacer> </vspacer>
<component id="6cb47" class="javax.swing.JButton" binding="buttonCreateElectricLocomotive"> <component id="6cb47" class="javax.swing.JButton" binding="buttonCreateElectricLocomotive">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Создать электролокомотив"/> <text value="Создать электролокомотив"/>
@ -28,7 +28,7 @@
</component> </component>
<component id="ca377" class="javax.swing.JButton" binding="buttonCreateLocomotive"> <component id="ca377" class="javax.swing.JButton" binding="buttonCreateLocomotive">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Создать локомотив"/> <text value="Создать локомотив"/>
@ -56,7 +56,7 @@
</component> </component>
<component id="cc460" class="javax.swing.JButton" binding="buttonRight"> <component id="cc460" class="javax.swing.JButton" binding="buttonRight">
<constraints> <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"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -71,7 +71,7 @@
</component> </component>
<component id="2fa92" class="javax.swing.JButton" binding="buttonDown"> <component id="2fa92" class="javax.swing.JButton" binding="buttonDown">
<constraints> <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"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -86,7 +86,7 @@
</component> </component>
<component id="bbbb" class="javax.swing.JButton" binding="buttonUp"> <component id="bbbb" class="javax.swing.JButton" binding="buttonUp">
<constraints> <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"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -105,7 +105,7 @@
</component> </component>
<component id="326c5" class="javax.swing.JButton" binding="buttonLeft"> <component id="326c5" class="javax.swing.JButton" binding="buttonLeft">
<constraints> <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"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -121,6 +121,14 @@
<hideActionText class="java.lang.Boolean" value="false"/> <hideActionText class="java.lang.Boolean" value="false"/>
</clientProperties> </clientProperties>
</component> </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> </children>
</grid> </grid>
</form> </form>

View File

@ -6,7 +6,7 @@ import java.awt.event.ActionListener;
import java.util.Random; import java.util.Random;
public class FormElectricLocomotive { public class FormElectricLocomotive {
DrawingLocomotive _drawingLocomotive; public DrawingLocomotive _drawingLocomotive;
AbstractStrategy _abstractStrategy; AbstractStrategy _abstractStrategy;
private JButton buttonCreateElectricLocomotive; private JButton buttonCreateElectricLocomotive;
private JPanel pictureBox; private JPanel pictureBox;
@ -17,6 +17,9 @@ public class FormElectricLocomotive {
public JComboBox comboBoxStrategy; public JComboBox comboBoxStrategy;
private JButton buttonStep; private JButton buttonStep;
private JButton buttonCreateLocomotive; private JButton buttonCreateLocomotive;
private JButton ButtonSelectLocomotive;
public DrawingLocomotive SelectedLocomotive;
public boolean IsSelect = false;
public JPanel getPictureBox() { public JPanel getPictureBox() {
return pictureBox; return pictureBox;
@ -30,11 +33,9 @@ public class FormElectricLocomotive {
buttonCreateLocomotive.addActionListener(e -> { buttonCreateLocomotive.addActionListener(e -> {
Random rnd = new Random(); Random rnd = new Random();
Color color = JColorChooser.showDialog(null, "Цвет", null);
_drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000), rnd.nextInt(1000, 3000), color,
new Color(rnd.nextInt(0, 256),
rnd.nextInt(0, 256),
rnd.nextInt(0, 256)),
pictureBox.getWidth(), pictureBox.getWidth(),
pictureBox.getHeight()); pictureBox.getHeight());
@ -45,11 +46,14 @@ public class FormElectricLocomotive {
buttonCreateElectricLocomotive.addActionListener(e -> { buttonCreateElectricLocomotive.addActionListener(e -> {
Random random = new Random(); Random random = new Random();
Color color = JColorChooser.showDialog(null, "Цвет", null);
Color addColor = JColorChooser.showDialog(null, "Цвет2", null);
_drawingLocomotive = new DrawingElectricLocomotive( _drawingLocomotive = new DrawingElectricLocomotive(
random.nextInt(100, 300), random.nextInt(100, 300),
random.nextInt(1000, 3000), random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), color,
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), addColor,
random.nextBoolean(), random.nextBoolean(),
random.nextBoolean(), random.nextBoolean(),
pictureBox.getWidth(), pictureBox.getWidth(),
@ -61,6 +65,12 @@ public class FormElectricLocomotive {
Draw(); Draw();
}); });
ButtonSelectLocomotive.addActionListener(e->{
SelectedLocomotive = _drawingLocomotive;
IsSelect = true;
// DialogResult = DialogResult.OK;
});
buttonStep.addActionListener(new ActionListener() { buttonStep.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {

View File

@ -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>

View File

@ -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);
}
}

View File

@ -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);
}
}
}
}

View File

@ -4,13 +4,16 @@ import javax.swing.*;
public class MainFrameElectricLocomotive extends JFrame { public class MainFrameElectricLocomotive extends JFrame {
private FormElectricLocomotive _formElectricLocomotive; private FormElectricLocomotive _formElectricLocomotive;
private FormLocomotiveCollections _formLocomotiveCollections;
public MainFrameElectricLocomotive() { public MainFrameElectricLocomotive() {
super(); super();
setTitle("ElectroLoco"); setTitle("ElectroLoco");
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
_formElectricLocomotive = new FormElectricLocomotive(); //_formElectricLocomotive = new FormElectricLocomotive();
setContentPane(_formElectricLocomotive.getPictureBox()); _formLocomotiveCollections = new FormLocomotiveCollections();
//setContentPane(_formElectricLocomotive.getPictureBox());
setContentPane(_formLocomotiveCollections.getPictureBoxCollections());
setDefaultLookAndFeelDecorated(false); setDefaultLookAndFeelDecorated(false);
setLocation(500, 200); setLocation(500, 200);
pack(); pack();

View File

@ -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];
}
}