Merge pull request 'PIbd - 21 Bakalskaya E.D. LabWork3 HARD' (#3) from LabWork3 into LabWork2

Reviewed-on: http://student.git.athene.tech/ekallin/PIbd-21_Bakalskaya_E.D._ElectricLocomotive._HARD/pulls/3
This commit is contained in:
ekallin 2023-10-29 17:12:40 +04:00
commit dc5fb84108
20 changed files with 630 additions and 41 deletions

View File

@ -0,0 +1,65 @@
package ProjectElectricLocomotive;
import java.util.ArrayList;
import java.util.Random;
public class DopClassParameters<T extends EntityLocomotive, U extends IDrawingWheels> {
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;
}
}

View File

@ -1,6 +1,7 @@
package ProjectElectricLocomotive; package ProjectElectricLocomotive;
import java.awt.*; import java.awt.*;
public class DrawingElectricLocomotive extends DrawingLocomotive { public class DrawingElectricLocomotive extends DrawingLocomotive {
public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor,
boolean horns, boolean seifBatteries, int width, int height) 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); 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 @Override
public void DrawTransport(Graphics g) public void DrawTransport(Graphics g)
{ {
if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF
{ {
Color colorBlack = Color.BLACK; Color addColor = electricLocomotive.AdditionalColor;
if (electricLocomotive.Horns) { if (electricLocomotive.Horns) {
//horns //horns
g.setColor(colorBlack); g.setColor(addColor);
g.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); g.fillRect(_startPosX + 30, _startPosY + 15, 20, 5);
g.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); g.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10);
g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY);
@ -28,7 +35,7 @@ public class DrawingElectricLocomotive extends DrawingLocomotive {
} }
if (electricLocomotive.SeifBatteries) { if (electricLocomotive.SeifBatteries) {
g.setColor(colorBlack); g.setColor(addColor);
g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10); g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10);
} }
super.DrawTransport(g); super.DrawTransport(g);

View File

@ -26,14 +26,14 @@ public class DrawingLocomotive {
return _locoHeight; 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; return;
} }
_pictureWidth = width; _pictureWidth = width;
_pictureHeight = heigth; _pictureHeight = height;
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
Random rnd = new Random(); Random rnd = new Random();
@ -58,6 +58,21 @@ public class DrawingLocomotive {
_locoHeight = locoHeight; _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){ public void SetWheelsCount(int wheelsCount){
_drawingWheels.SetWheelsCount(wheelsCount); _drawingWheels.SetWheelsCount(wheelsCount);
} }
@ -202,4 +217,8 @@ public class DrawingLocomotive {
} }
return false; return false;
} }
public IMoveableObject GetMoveableObject() {
return new DrawingObjectLocomotive(this);
}
} }

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectElectricLocomotive.FormDopClassParameters">
<grid id="27dc6" binding="pictureBoxGenerated" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="0" vgap="0">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="758" height="552"/>
</constraints>
<properties>
<enabled value="true"/>
<maximumSize width="600" height="400"/>
<minimumSize width="600" height="400"/>
<opaque value="true"/>
<preferredSize width="600" height="400"/>
<requestFocusEnabled value="false"/>
</properties>
<border type="none"/>
<children>
<component id="1ee3" class="javax.swing.JButton" binding="ButtonGenerationRandomLocomotive">
<constraints>
<grid row="0" 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="Генерировать локомотив"/>
</properties>
</component>
<hspacer id="b7e9d">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="d0ce8">
<constraints>
<grid row="1" 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>
</children>
</grid>
</form>

View File

@ -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<EntityLocomotive, IDrawingWheels> _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);
}
}

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;
public 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,11 @@ public class FormElectricLocomotive {
Draw(); Draw();
}); });
ButtonSelectLocomotive.addActionListener(e->{
SelectedLocomotive = _drawingLocomotive;
IsSelect = true;
});
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,91 @@
<?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="7" 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="6" 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="textFieldNumber">
<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>
<component id="1b1c" class="javax.swing.JButton" binding="ButtonCreateRandomLoco">
<constraints>
<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="Создать рандомный локо "/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,85 @@
package ProjectElectricLocomotive;
import javax.swing.*;
import java.awt.*;
public class FormLocomotiveCollections {
FrameDopClassParameters frameDopClassParameters;
private JPanel MainPanel;
private JPanel pictureBoxCollections;
private JPanel Instruments;
private JButton ButtonAddLocomotive;
private JTextField textFieldNumber;
private JButton ButtonRefreshCollection;
private JButton ButtonRemoveLocomotive;
private JButton ButtonCreateRandomLoco;
public DrawingLocomotive loco;
LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> _locomotives;
public JPanel getPictureBoxCollections() {
return MainPanel;
}
public FormLocomotiveCollections() {
_locomotives = new LocomotiveGenericCollection<>(400, 300);
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(), "Не удалось добавить объект");
}
}
});
});
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) {
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);
}
});
ButtonRefreshCollection.addActionListener(e -> {
Refresh();
});
}
public void Refresh() {
Graphics g = pictureBoxCollections.getGraphics();
pictureBoxCollections.paint(g);
_locomotives.ShowLocomotives(g);
}
}

View File

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

View File

@ -0,0 +1,18 @@
package ProjectElectricLocomotive;
import javax.swing.*;
public class FrameElectricLocomotive extends JFrame {
public FormElectricLocomotive _formLocomotiveCollection;
public FrameElectricLocomotive() {
super();
setTitle("Электролокомотив");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
_formLocomotiveCollection = new FormElectricLocomotive();
setContentPane(_formLocomotiveCollection.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(500, 200);
pack();
setVisible(true);
}
}

View File

@ -0,0 +1,17 @@
package ProjectElectricLocomotive;
import javax.swing.*;
public class FrameLocomotiveCollection extends JFrame {
public FormLocomotiveCollections _formLocomotiveCollections;
public FrameLocomotiveCollection(){
super();
setTitle("Коллекция");setDefaultCloseOperation(EXIT_ON_CLOSE);
_formLocomotiveCollections = new FormLocomotiveCollections();
setContentPane(_formLocomotiveCollections.getPictureBoxCollections());
setDefaultLookAndFeelDecorated(false);
setLocation(400, 50);
pack();
setVisible(true);
}
}

View File

@ -0,0 +1,83 @@
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 = 150;
private final int _placeSizeHeight = 50;
/// Набор объектов
private final SetGeneric<T> _collection;
public LocomotiveGenericCollection(int picWidth, int picHeight)
{
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

@ -3,6 +3,6 @@ package ProjectElectricLocomotive;
public class Main { public class Main {
public static void main(String[] args) public static void main(String[] args)
{ {
MainFrameElectricLocomotive mainFrameElectricLocomotive = new MainFrameElectricLocomotive(); FrameLocomotiveCollection mainFrame = new FrameLocomotiveCollection();
} }
} }

View File

@ -1,19 +0,0 @@
package ProjectElectricLocomotive;
import javax.swing.*;
public class MainFrameElectricLocomotive extends JFrame {
private FormElectricLocomotive _formElectricLocomotive;
public MainFrameElectricLocomotive() {
super();
setTitle("ElectroLoco");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formElectricLocomotive = new FormElectricLocomotive();
setContentPane(_formElectricLocomotive.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(500, 200);
pack();
setVisible(true);
}
}

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 702 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 706 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 702 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 706 B