Repair Lab3

This commit is contained in:
ekallin 2023-11-06 20:57:19 +04:00
parent e36d02dd75
commit 25a6ddb3bf
13 changed files with 265 additions and 22 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);
} }

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

@ -68,7 +68,6 @@ public class FormElectricLocomotive {
ButtonSelectLocomotive.addActionListener(e->{ ButtonSelectLocomotive.addActionListener(e->{
SelectedLocomotive = _drawingLocomotive; SelectedLocomotive = _drawingLocomotive;
IsSelect = true; IsSelect = true;
// DialogResult = DialogResult.OK;
}); });
buttonStep.addActionListener(new ActionListener() { buttonStep.addActionListener(new ActionListener() {

View File

@ -21,7 +21,7 @@
<border type="line"/> <border type="line"/>
<children/> <children/>
</grid> </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"> <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"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <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"> <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">
@ -39,7 +39,7 @@
<children> <children>
<vspacer id="1eee0"> <vspacer id="1eee0">
<constraints> <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"/> <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> </constraints>
</vspacer> </vspacer>
<component id="138d" class="javax.swing.JButton" binding="ButtonAddLocomotive"> <component id="138d" class="javax.swing.JButton" binding="ButtonAddLocomotive">
@ -76,6 +76,14 @@
<text value="Обновить "/> <text value="Обновить "/>
</properties> </properties>
</component> </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> </children>
</grid> </grid>
</children> </children>

View File

@ -3,7 +3,10 @@ package ProjectElectricLocomotive;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
//готовая лаба 3
public class FormLocomotiveCollections { public class FormLocomotiveCollections {
FrameDopClassParameters frameDopClassParameters;
private JPanel MainPanel; private JPanel MainPanel;
private JPanel pictureBoxCollections; private JPanel pictureBoxCollections;
private JPanel Instruments; private JPanel Instruments;
@ -11,9 +14,9 @@ public class FormLocomotiveCollections {
private JTextField textFieldNumber; private JTextField textFieldNumber;
private JButton ButtonRefreshCollection; private JButton ButtonRefreshCollection;
private JButton ButtonRemoveLocomotive; private JButton ButtonRemoveLocomotive;
private JButton ButtonCreateRandomLoco;
public DrawingLocomotive loco; public DrawingLocomotive loco;
FormElectricLocomotive _formElectricLocomotive; LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> _locomotives;
private final LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> _locomotives;
public JPanel getPictureBoxCollections() { public JPanel getPictureBoxCollections() {
return MainPanel; return MainPanel;
@ -41,6 +44,12 @@ public class FormLocomotiveCollections {
}); });
}); });
ButtonCreateRandomLoco.addActionListener(e->{
if(frameDopClassParameters!=null) frameDopClassParameters.dispose();
frameDopClassParameters = new FrameDopClassParameters();
frameDopClassParameters.setVisible(true);
});
ButtonRemoveLocomotive.addActionListener(e -> { ButtonRemoveLocomotive.addActionListener(e -> {
try { try {
int pos = Integer.parseInt(textFieldNumber.getText()); int pos = Integer.parseInt(textFieldNumber.getText());
@ -67,6 +76,7 @@ public class FormLocomotiveCollections {
ButtonRefreshCollection.addActionListener(e -> { ButtonRefreshCollection.addActionListener(e -> {
Refresh(); Refresh();
}); });
} }
public void Refresh() { public void Refresh() {
Graphics g = pictureBoxCollections.getGraphics(); Graphics g = pictureBoxCollections.getGraphics();

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

@ -6,8 +6,8 @@ public class FrameElectricLocomotive extends JFrame {
public FormElectricLocomotive _formLocomotiveCollection; public FormElectricLocomotive _formLocomotiveCollection;
public FrameElectricLocomotive() { public FrameElectricLocomotive() {
super(); super();
setTitle("ElectroLoco"); setTitle("Электролокомотив");
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(DISPOSE_ON_CLOSE);
_formLocomotiveCollection = new FormElectricLocomotive(); _formLocomotiveCollection = new FormElectricLocomotive();
setContentPane(_formLocomotiveCollection.getPictureBox()); setContentPane(_formLocomotiveCollection.getPictureBox());
setDefaultLookAndFeelDecorated(false); setDefaultLookAndFeelDecorated(false);

View File

@ -6,8 +6,7 @@ public class FrameLocomotiveCollection extends JFrame {
public FormLocomotiveCollections _formLocomotiveCollections; public FormLocomotiveCollections _formLocomotiveCollections;
public FrameLocomotiveCollection(){ public FrameLocomotiveCollection(){
super(); super();
setTitle("LocoCollection"); setTitle("Коллекция");setDefaultCloseOperation(EXIT_ON_CLOSE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formLocomotiveCollections = new FormLocomotiveCollections(); _formLocomotiveCollections = new FormLocomotiveCollections();
setContentPane(_formLocomotiveCollections.getPictureBoxCollections()); setContentPane(_formLocomotiveCollections.getPictureBoxCollections());
setDefaultLookAndFeelDecorated(false); setDefaultLookAndFeelDecorated(false);

View File

@ -10,14 +10,11 @@ public class LocomotiveGenericCollection<T extends DrawingLocomotive,U extends I
//ширина/высота занимаемого места //ширина/высота занимаемого места
private final int _placeSizeWidth = 150; private final int _placeSizeWidth = 150;
private final int _placeSizeHeight = 50; private final int _placeSizeHeight = 50;
/// Набор объектов /// Набор объектов
private final SetGeneric<T> _collection; private final SetGeneric<T> _collection;
public LocomotiveGenericCollection(int picWidth, int picHeight) public LocomotiveGenericCollection(int picWidth, int picHeight)
{ {
// немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло...
// высчитываем размер массива для setgeneric
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight; int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth; _pictureWidth = picWidth;