This commit is contained in:
marusya 2023-12-29 23:29:22 +04:00
parent 845860c3dc
commit 5ccd5bf644
19 changed files with 611 additions and 25 deletions

2
.idea/.name generated
View File

@ -1 +1 @@
Main.java
DrawingEmptyWheels.java

View File

@ -0,0 +1,62 @@
import java.util.Random;
public class DopClassParameters<T extends EntityUsta, U extends IDrawingWheels> {
public T[] _entityUsta;
public U[] _idrawningWheels;
int _ustaCount;
int _idrawningWheelsCount;
private int _pictureWidth;
private int _pictureHeight;
public DopClassParameters(int count, int width, int height) {
_entityUsta = (T[]) new EntityUsta[count];
_idrawningWheels = (U[]) new IDrawingWheels[count];
_pictureWidth = width;
_pictureHeight = height;
_ustaCount = 0;
_idrawningWheelsCount = 0;
}
public void Add(T entityUstaObj) {
if (_ustaCount < _entityUsta.length) {
_entityUsta[_ustaCount] = entityUstaObj;
_ustaCount++;
}
}
public void Add(U idrawingWheels) {
if (_idrawningWheelsCount < _idrawningWheels.length) {
_idrawningWheels[_idrawningWheelsCount] = idrawingWheels;
_idrawningWheelsCount++;
}
}
public DrawingUsta RandomUsta(int pictureWidth, int pictureHeight) {
Random rnd = new Random();
if (_entityUsta == null || _idrawningWheels == null) {
return null;
}
T entityUsta = _entityUsta[rnd.nextInt(_ustaCount)];
U idrawingWheels = _idrawningWheels[rnd.nextInt(_idrawningWheelsCount)];
DrawingUsta drawUsta;
if (entityUsta instanceof EntityUstaBat) {
drawUsta = new DrawingUstaBat(
(EntityUstaBat) entityUsta,
idrawingWheels,
pictureWidth,
pictureHeight
);
} else {
drawUsta = new DrawingUsta(
entityUsta,
idrawingWheels,
pictureWidth,
pictureHeight
);
}
return drawUsta;
}
}

View File

@ -10,7 +10,7 @@ public class DrawingEmptyWheels implements IDrawingWheels{
return;
}
}
this._wheelsCount = WheelsCount.Five;
this._wheelsCount = WheelsCount.Four;
}
@Override

View File

@ -1,6 +1,6 @@
import java.awt.*;
import java.awt.geom.Path2D;
import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;
import java.util.Random;
public class DrawingUsta {
@ -62,6 +62,21 @@ public class DrawingUsta {
_ustaHeight = ustaHeight;
}
protected DrawingUsta(EntityUsta entityUsta, IDrawingWheels iDrawingWheels,
int width, int height){
EntityUsta = entityUsta;
_drawingWheels = iDrawingWheels;
_pictureWidth = width;
_pictureHeight = height;
}
protected DrawingUsta(EntityUsta entityUsta, IDrawingWheels iDrawingWheels,
int width, int height, int ustaWidth, int ustaHeight){
this(entityUsta, iDrawingWheels, width, height);
_ustaWidth = ustaWidth;
_ustaHeight = ustaHeight;
}
public void SetPosition(int x, int y)
{
@ -103,6 +118,9 @@ public class DrawingUsta {
if (EntityUsta == null) {
return;
}
Color bodyColor = EntityUsta.BodyColor;
Color blackBrush = Color.BLACK;
@ -128,7 +146,6 @@ public class DrawingUsta {
g.draw(path);
_drawingWheels.DrawWheels(g, blackBrush, _startPosX, _startPosY, 9,9);
g.setColor(bodyColor);
g.fillRect(_startPosX + 45, _startPosY + 20, 40, 20);
g.setColor(Color.BLACK);
@ -164,4 +181,8 @@ public class DrawingUsta {
}
return false;
}
public IMoveableObject GetMoveableObject() {
return new DrawingObjectUsta(this);
}
}

View File

@ -11,6 +11,12 @@ public class DrawingUstaBat extends DrawingUsta {
EntityUsta = new EntityUstaBat(speed, width, bodyColor, additionalColor, pushka, bodyKit);
}
}
public DrawingUstaBat(EntityUstaBat entityUstaBat, IDrawingWheels iDrawingWheels,
int width, int height)
{
super(entityUstaBat,iDrawingWheels, width, height, 150, 50);
}
@Override
public void DrawTransport(Graphics2D g)
{

View File

@ -1,6 +1,4 @@
import java.awt.*;
import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;
public class DrawingWheel implements IDrawingWheels {
private WheelsCount _wheelsCount;
@ -11,7 +9,7 @@ public class DrawingWheel implements IDrawingWheels {
return;
}
}
this._wheelsCount = WheelsCount.Five;
this._wheelsCount = WheelsCount.Six;
}
@Override

View File

@ -10,7 +10,7 @@ public class DrawingWheelsBlueCrom implements IDrawingWheels{
return;
}
}
this._wheelsCount = WheelsCount.Five;
this._wheelsCount = WheelsCount.Six;
}
@Override

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="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="ButtonGenerationRandomUsta">
<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,85 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FormDopClassParameters {
private JButton ButtonGenerationRandomUsta;
private JPanel pictureBoxGenerated;
Random rnd;
DrawingUsta drawingUsta;
DopClassParameters<EntityUsta, IDrawingWheels> _dopClassParameters;
public JPanel getPictureBoxGenerated() {
return pictureBoxGenerated;
}
public FormDopClassParameters() {
pictureBoxGenerated.setPreferredSize(new Dimension(400, 300));
_dopClassParameters = new DopClassParameters<>(
10,
pictureBoxGenerated.getWidth(),
pictureBoxGenerated.getHeight()
);
ButtonGenerationRandomUsta.addActionListener(e -> {
AddEntityUsta();
AddRandomWheels();
drawingUsta = _dopClassParameters.RandomUsta(
pictureBoxGenerated.getWidth(),
pictureBoxGenerated.getHeight()
);
drawingUsta.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
Draw();
});
}
public void AddEntityUsta() {
rnd = new Random();
EntityUsta entityLocomotive;
Color color = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
Color color2 = new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
if (rnd.nextBoolean()) {
entityLocomotive = new EntityUsta(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
color
);
} else {
entityLocomotive = new EntityUstaBat(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000),
color,
color2,
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(3, 7));
_dopClassParameters.Add(iDrawingWheels);
}
public void Draw() {
if (drawingUsta.EntityUsta == null) {
return;
}
Graphics g = pictureBoxGenerated.getGraphics();
pictureBoxGenerated.paint(g);
drawingUsta.DrawTransport((Graphics2D) g);
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormUstaBat">
<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="736" height="499"/>
@ -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="buttonCreateUstaBat">
<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="buttonCreateUsta">
<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"/>
@ -70,7 +70,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"/>
@ -84,7 +84,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"/>
@ -102,7 +102,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"/>
@ -117,6 +117,14 @@
<hideActionText class="java.lang.Boolean" value="false"/>
</clientProperties>
</component>
<component id="7aad1" class="javax.swing.JButton" binding="ButtonSelectUsta">
<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>

View File

@ -16,6 +16,10 @@ public class FormUstaBat {
public JComboBox comboBoxStrategy;
private JButton buttonStep;
private JButton buttonCreateUsta;
public JButton ButtonSelectUsta;
public DrawingUsta SelectedUsta;
public boolean IsSelect = false;
public JPanel getPictureBox() {
return pictureBox;
}
@ -28,25 +32,27 @@ public class FormUstaBat {
buttonCreateUsta.addActionListener(e -> {
Random rnd = new Random();
Color color = JColorChooser.showDialog(null, "Цвет", null);
_drawingUsta = new DrawingUsta(rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000), new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
rnd.nextInt(1000, 3000), color,
pictureBox.getWidth(),
pictureBox.getHeight());
_drawingUsta.SetWheelsCount(rnd.nextInt(4, 7));
_drawingUsta.SetWheelsCount(rnd.nextInt(3, 7));
_drawingUsta.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
Draw();
});
buttonCreateUstaBat.addActionListener(e -> {
Random random = new Random();
Color color = JColorChooser.showDialog(null, "Цвет", null);
Color addColor = JColorChooser.showDialog(null, "Цвет2", null);
_drawingUsta = new DrawingUstaBat(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
color,
addColor,
random.nextBoolean(),
random.nextBoolean(),
pictureBox.getWidth(),
@ -58,6 +64,12 @@ public class FormUstaBat {
Draw();
});
ButtonSelectUsta.addActionListener(e->{
SelectedUsta = _drawingUsta;
IsSelect = true;
// DialogResult = DialogResult.OK;
});
buttonStep.addActionListener(new ActionListener() {
@Override
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="FormUstaCollections">
<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="1175" height="504"/>
</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="false">
<minimum-size width="700" height="500"/>
<preferred-size width="700" height="500"/>
<maximum-size width="700" height="500"/>
</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="false">
<minimum-size width="200" height="400"/>
<preferred-size width="300" height="400"/>
<maximum-size width="300" 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="ButtonAddUsta">
<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="ButtonRemoveUsta">
<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="ButtonCreateRandomUsta">
<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,84 @@
import javax.swing.*;
import java.awt.*;
//готовая лаба 3
public class FormUstaCollections {
FrameDopClassParameters frameDopClassParameters;
private JPanel MainPanel;
private JPanel pictureBoxCollections;
private JPanel Instruments;
private JButton ButtonAddUsta;
private JTextField textFieldNumber;
private JButton ButtonRefreshCollection;
private JButton ButtonRemoveUsta;
private JButton ButtonCreateRandomUsta;
public DrawingUsta usta;
UstaGenericCollection<DrawingUsta, DrawingObjectUsta> _usta;
public JPanel getPictureBoxCollections() {
return MainPanel;
}
public FormUstaCollections() {
_usta = new UstaGenericCollection<>(700, 430);
ButtonAddUsta.addActionListener(e -> {
FrameUstaBat frameElectricUsta = new FrameUstaBat();
frameElectricUsta.setVisible(true);
frameElectricUsta._formUstaCollection.ButtonSelectUsta.addActionListener(e2 -> {
usta = frameElectricUsta._formUstaCollection._drawingUsta;
frameElectricUsta.dispose();
if (usta != null) {
//проверяем, удалось ли нам загрузить объект
if (_usta.AddOverload(usta) != -1) {
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен");
Refresh();
} else {
JOptionPane.showMessageDialog(getPictureBoxCollections(), "Не удалось добавить объект");
}
}
});
});
ButtonCreateRandomUsta.addActionListener(e->{
if(frameDopClassParameters!=null) frameDopClassParameters.dispose();
frameDopClassParameters = new FrameDopClassParameters();
frameDopClassParameters.setVisible(true);
});
ButtonRemoveUsta.addActionListener(e -> {
try {
int pos = Integer.parseInt(textFieldNumber.getText());
if (_usta.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);
_usta.ShowLocomotives((Graphics2D) g);
}
}

View File

@ -0,0 +1,17 @@
import javax.swing.*;
public class FrameDopClassParameters extends JFrame {
public FormDopClassParameters _formDopClassParameters;
public FrameDopClassParameters(){
super();
setTitle("Random usta");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
_formDopClassParameters = new FormDopClassParameters();
setContentPane(_formDopClassParameters.getPictureBoxGenerated());
setDefaultLookAndFeelDecorated(false);
setLocation(500, 200);
pack();
setVisible(true);
}
}

View File

@ -0,0 +1,16 @@
import javax.swing.*;
public class FrameUstaCollection extends JFrame {
public FormUstaCollections _formUstaCollections;
public FrameUstaCollection(){
super();
setTitle("UstaCollection");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formUstaCollections = new FormUstaCollections();
setContentPane(_formUstaCollections.getPictureBoxCollections());
setDefaultLookAndFeelDecorated(false);
setLocation(400, 50);
pack();
setVisible(true);
}
}

View File

@ -1,6 +1,4 @@
import java.awt.*;
import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;
public interface IDrawingWheels {
void SetWheelsCount(int wheelsCount);
@ -56,7 +54,7 @@ public interface IDrawingWheels {
g.fillOval(startPosX + 120, startPosY + 50, 25, 24);
}
if (wheelsCount.count == wheelsCount.Six.count) {
if (wheelsCount.count >= wheelsCount.Six.count) {
/* наверху гусеницы*/

View File

@ -1,6 +1,6 @@
public class Main {
public static void main(String[] args)
{
FrameUstaBat mainFrame = new FrameUstaBat();
FrameUstaCollection mainFrame = new FrameUstaCollection();
}
}

61
src/SetGeneric.java Normal file
View File

@ -0,0 +1,61 @@
public class SetGeneric<T extends DrawingUsta>{
private T[] _places;
public int Count(){
return _places.length;
}
public SetGeneric(int count) {
_places = (T[]) new DrawingUsta[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];
}
}

View File

@ -0,0 +1,89 @@
import java.awt.*;
public class UstaGenericCollection<T extends DrawingUsta,U extends IMoveableObject>
{
//ширина/высота окна
private final int _pictureWidth;
private final int _pictureHeight;
//ширина/высота занимаемого места
private final int _placeSizeWidth = 210;
private final int _placeSizeHeight = 90;
/// Набор объектов
private final SetGeneric<T> _collection;
public UstaGenericCollection(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(Graphics2D 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(Graphics2D g)
{
int c = 11;
for (int i = 0; i < _collection.Count(); i++)
{
if (i % 3 == 0 && i != 0)
{
c = c - 3;
}
T type = _collection.Get(i);
if (type != null)
{
type.SetPosition(
(int)(i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth + 3),
(c / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight + 10)
);
type.DrawTransport(g);
}
}
}
}