Compare commits

..

2 Commits

18 changed files with 713 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/ElectricLocomotive.iml" filepath="$PROJECT_DIR$/ElectricLocomotive.iml" />
</modules>
</component>
</project>

124
.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

11
ElectricLocomotive.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,13 @@
public enum AdditionalDirection {
TwoWheel(2),
ThreeWheel(3),
FourWheel(4);
private final int count;
AdditionalDirection(int value){
count = value;
}
public int getCount() {
return count;
}
}

7
src/Direction.java Normal file
View File

@ -0,0 +1,7 @@
public enum Direction {
Up,
Down,
Left,
Right,
None
}

141
src/DrawingLocomotive.java Normal file
View File

@ -0,0 +1,141 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingLocomotive extends JComponent {
public EntityLocomotive Locomotive;
public float _startPosX;
public float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private int _locomotiveWidth = 150;
private int _locomotiveHeight = 100;
private IDrawningObjectWheel _wheel;
public DrawingLocomotive(int speed, float weight, Color bodyColor, int wheelCount)
{
Random random = new Random();
Locomotive = new EntityLocomotive(speed, weight, bodyColor);
int randomPattern=random.nextInt(3);
if (randomPattern==0)
_wheel = new DrawingWheel();
else if(randomPattern==1)
_wheel = new DrawingWheelCircle();
else
_wheel = new DrawningWheelTriangle();
_wheel.SetWheelAmount(wheelCount);
}
protected DrawingLocomotive(int speed, float weight, Color bodyColor, int whelcount, int locomotiveWidth, int locomotiveHeight)
{
this(speed, weight, bodyColor, whelcount);
_locomotiveWidth = locomotiveWidth;
_locomotiveHeight = locomotiveHeight;
}
public void SetPosition(int x, int y, int width, int height)
{
if (width <= _locomotiveWidth + x || height <= _locomotiveHeight + y || x<0 || y<0)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void MoveTransport(Direction direction)
{
if (_pictureWidth==null || _pictureHeight==null)
{
return;
}
switch (direction)
{
case Right:
{
if (_startPosX + _locomotiveWidth + Locomotive.Step() < _pictureWidth) {
_startPosX += Locomotive.Step();
}
break;
}
case Left:
if (_startPosX - Locomotive.Step() > 0)
{
_startPosX -= Locomotive.Step();
}
break;
case Up:
if (_startPosY - Locomotive.Step() > 0)
{
_startPosY -= Locomotive.Step();
}
break;
case Down:
if (_startPosY + _locomotiveHeight + Locomotive.Step() < _pictureHeight)
{
_startPosY += Locomotive.Step();
}
break;
}
}
public void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g=(Graphics2D)gr;
if (_startPosX < 0 || _startPosY < 0
|| _pictureHeight==null || _pictureWidth==null)
{
return;
}
Color pen = new Color(0,0,0);
//границы лодки
int [] pointsX = new int[]{(int)(_startPosX + 10),(int)(_startPosX + 10),(int)(_startPosX+ 110),(int)(_startPosX + 140),(int)(_startPosX+140)};
int [] pointsY = new int[]{(int)(_startPosY + 90),(int)(_startPosY + 40),(int)(_startPosY + 40),(int)(_startPosY + 60),(int)(_startPosY + 90) };
try { pen = Locomotive.BodyColor(); }
catch (Exception e) {}
g.setPaint(pen);
g.fillPolygon( pointsX,pointsY,5);
g.setColor(Color.BLACK);
g.fillRect((int) _startPosX + 5, (int)_startPosY + 45, 5, 40);
// Окна
g.setColor(Color.CYAN);
g.drawRect((int) _startPosX + 20, (int)_startPosY + 50, 20, 25);
g.drawRect((int)_startPosX + 50,(int) _startPosY + 50, 20, 25);
// Дверь
g.drawRect((int) _startPosX + 85,(int) _startPosY + 45, 20, 40);
// Локомотив
g.drawPolygon(pointsX,pointsY,5);
_wheel.DrawWheel(gr, (int)_startPosX, (int)_startPosY, pen);
super.repaint();
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _locomotiveWidth || _pictureHeight <= _locomotiveHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _locomotiveWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _locomotiveWidth;
}
if (_startPosY + _locomotiveHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _locomotiveHeight;
}
}
public float[] GetCurrentPosition(){
float[] tuple = new float[4];
tuple[0] = _startPosX;
tuple[1] =_startPosY;
tuple[2] = _startPosX + _locomotiveWidth;
tuple[3] = _startPosY + _locomotiveHeight;
return tuple;
}
}

43
src/DrawingWheel.java Normal file
View File

@ -0,0 +1,43 @@
import java.awt.*;
public class DrawingWheel implements IDrawningObjectWheel {
private AdditionalDirection _wheel;
@Override
public void SetWheelAmount(int wheelAmount) {
for (AdditionalDirection item: _wheel.values()) {
if (item.getCount() == wheelAmount) {
_wheel = item;
return;
}
}
}
@Override
public void DrawWheel(Graphics gr, int _startPosWheelX, int _startPosWheelY, Color pen) {
// super.paintComponent(gr);
Graphics2D g=(Graphics2D)gr;
if (_wheel.getCount() >= 2) {
paintWheel(g, _startPosWheelX, _startPosWheelY,pen);
paintWheel(g, _startPosWheelX +90, _startPosWheelY,pen);
}
if (_wheel.getCount() >= 3) {
paintWheel(g, _startPosWheelX +65, _startPosWheelY,pen);
}
if (_wheel.getCount() >= 4) {
paintWheel(g, _startPosWheelX +25, _startPosWheelY, pen);
}
// super.repaint();
}
protected void paintWheel(Graphics2D g, int _startPosX1, int _startPosY, Color pen){
try { g.setPaint(pen); }
catch (Exception e) {
g.setPaint(Color.black);
}
g.fillOval((int)_startPosX1+ 17, (int)_startPosY + 90, 20, 20);
g.setColor(Color.BLACK);
g.drawOval((int) _startPosX1 + 17, (int) _startPosY + 90, 20, 20);
}
}

24
src/EntityLocomotive.java Normal file
View File

@ -0,0 +1,24 @@
import java.awt.*;
import java.util.Random;
public class EntityLocomotive {
private int speed;
public int Speed() { return speed; }
private float weight;
public float Weight() {
return weight;
}
private Color bodyColor;
public Color BodyColor() {
return bodyColor;
}
public float Step()
{ return Speed() * 20 / Weight(); }
public EntityLocomotive(int speed, float weight, Color bodyColor)
{
Random random = new Random();
this.speed = speed <= 0 ? random.nextInt(50, 150) : speed;
this.weight = weight <= 0 ? random.nextInt(40, 70) : weight;
this.bodyColor = bodyColor;
}
}

190
src/FormLocomotive.form Normal file
View File

@ -0,0 +1,190 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormLocomotive">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="5" column-count="7" 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="766" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="bbded" binding="pictureBoxLocomotive" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="2" col-span="6" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-1"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="59ac9" binding="radioButtonsBox" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="95721" class="javax.swing.JRadioButton" binding="radioButtonWheel2">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Два колеса"/>
</properties>
</component>
<component id="58261" class="javax.swing.JRadioButton" binding="radioButtonWheel3">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Три колеса"/>
</properties>
</component>
<component id="fe956" class="javax.swing.JRadioButton" binding="radioButtonWheel4">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Четыре колеса"/>
</properties>
</component>
<grid id="ba39f" binding="statusStrip" layout-manager="GridLayoutManager" row-count="4" column-count="4" 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="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="7fb0a" class="javax.swing.JLabel" binding="toolStripStatusLabelBodyColor">
<constraints>
<grid row="0" column="2" row-span="4" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Цвет: "/>
</properties>
</component>
<component id="4180a" class="javax.swing.JLabel" binding="toolStripStatusLabelSpeed">
<constraints>
<grid row="0" column="0" row-span="4" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость: "/>
</properties>
</component>
<hspacer id="42ce">
<constraints>
<grid row="1" column="3" row-span="3" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="2ba60" class="javax.swing.JLabel" binding="toolStripStatusLabelWeight">
<constraints>
<grid row="0" column="1" row-span="4" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес: "/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<vspacer id="93048">
<constraints>
<grid row="1" column="6" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<hspacer id="8d179">
<constraints>
<grid row="4" column="1" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="1de1f">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="8ef30" binding="buttonsBox" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="4" row-span="2" col-span="2" vsize-policy="3" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="150"/>
<preferred-size width="150" height="150"/>
<maximum-size width="150" height="150"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="68b27" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/right.jpg"/>
<text value=""/>
</properties>
</component>
<component id="80f7e" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/left.jpg"/>
<text value=""/>
</properties>
</component>
<component id="f431c" class="javax.swing.JButton" binding="buttonUp">
<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">
<minimum-size width="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/up.jpg"/>
<text value=""/>
</properties>
</component>
<component id="2c574" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="1" 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="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/down.jpg"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
<component id="d05dd" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="120" height="40"/>
<preferred-size width="120" height="40"/>
<maximum-size width="120" height="40"/>
</grid>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
</children>
</grid>
</form>

128
src/FormLocomotive.java Normal file
View File

@ -0,0 +1,128 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FormLocomotive extends JFrame {
public static void main(String[] args) {
FormLocomotive window = new FormLocomotive();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
private JPanel mainPanel;
private JPanel statusStrip;
private JLabel toolStripStatusLabelSpeed;
private JLabel toolStripStatusLabelBodyColor;
private JLabel toolStripStatusLabelWeight;
private JPanel buttonsBox;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonCreate;
private JRadioButton radioButtonWheel3;
private JRadioButton radioButtonWheel4;
private JPanel radioButtonsBox;
private JPanel pictureBoxLocomotive;
private JRadioButton radioButtonWheel2;
private DrawingLocomotive _locomotive;
private int pictureBoxLocomotiveWidth;
private int pictureBoxLocomotiveHeight;
ButtonGroup buttonGroupWheelRadBut;
public FormLocomotive() {
super("Локомотив");
buttonGroupWheelRadBut = new ButtonGroup();
buttonGroupWheelRadBut.add(radioButtonWheel2);
buttonGroupWheelRadBut.add(radioButtonWheel3);
buttonGroupWheelRadBut.add(radioButtonWheel4);
setPreferredSize(new Dimension(1000, 700));
getContentPane().add(mainPanel);
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
pictureBoxLocomotive.remove(_locomotive);
} catch (Exception c) {
}
Random random = new Random();
_locomotive = new DrawingLocomotive(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256),
random.nextInt(0, 256), random.nextInt(0, 256)), GetWheelAmount());
SetData();
}
});
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
ChangePictureBoxLocomotiveBorders();
if (_locomotive == null) return;
_locomotive.ChangeBorders(pictureBoxLocomotiveWidth, pictureBoxLocomotiveHeight);
try {
pictureBoxLocomotive.remove(_locomotive);
} catch (Exception c) {
}
pictureBoxLocomotive.add(_locomotive, BorderLayout.CENTER);
}
});
//джижение
ButtonsMove buttonsMove = new ButtonsMove();
buttonUp.setName("Up");
buttonLeft.setName("Left");
buttonRight.setName("Right");
buttonDown.setName("Down");
buttonUp.addActionListener(buttonsMove);
buttonLeft.addActionListener(buttonsMove);
buttonRight.addActionListener(buttonsMove);
buttonDown.addActionListener(buttonsMove);
}
class ButtonsMove implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (_locomotive == null) return;
JButton temp = (JButton) e.getSource();
String name = temp.getName();
switch (name) {
case "Up" -> _locomotive.MoveTransport(Direction.Up);
case "Right" -> _locomotive.MoveTransport(Direction.Right);
case "Left" -> _locomotive.MoveTransport(Direction.Left);
case "Down" -> _locomotive.MoveTransport(Direction.Down);
}
}
}
private void ChangePictureBoxLocomotiveBorders() {
char[] temp = pictureBoxLocomotive.getSize().toString().toCharArray();
for (int i = 0; i < temp.length; i++) {
if (!Character.isDigit(temp[i])) {
temp[i] = ' ';
}
}
String width = new String(temp);
String[] parameters = width.split("\\s*(\\s|,|!|\\.)\\s*", 4);
pictureBoxLocomotiveWidth = Integer.parseInt(parameters[1]);
pictureBoxLocomotiveHeight = Integer.parseInt(parameters[2]);
}
private int GetWheelAmount() {
if (radioButtonWheel2.isSelected()) {
return 2;
}
else if (radioButtonWheel3.isSelected()) {
return 3;
} else {
return 4;
}
}
private void SetData() {
Random random = new Random();
ChangePictureBoxLocomotiveBorders();
_locomotive.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxLocomotiveWidth, pictureBoxLocomotiveHeight);
toolStripStatusLabelSpeed.setText("Скорость: " + _locomotive.Locomotive.Speed());
toolStripStatusLabelWeight.setText("Вес: " + _locomotive.Locomotive.Weight());
toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_locomotive.Locomotive.BodyColor().getRGB()));
pictureBoxLocomotive.add(_locomotive, BorderLayout.CENTER);
}
}

BIN
src/images/down.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
src/images/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
src/images/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
src/images/up.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB