Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
cc59420fbf | |||
352e557d37 | |||
3b693a35eb |
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
11
.idea/PIBD-12_Bugrov_D.A._Hard.iml
generated
Normal file
11
.idea/PIBD-12_Bugrov_D.A._Hard.iml
generated
Normal 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$" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/PIBD-12_Bugrov_D.A._Hard.iml" filepath="$PROJECT_DIR$/.idea/PIBD-12_Bugrov_D.A._Hard.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
124
.idea/uiDesigner.xml
generated
Normal file
124
.idea/uiDesigner.xml
generated
Normal 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
generated
Normal file
6
.idea/vcs.xml
generated
Normal 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>
|
10
ProjectElectricLocomotive/Main.java
Normal file
10
ProjectElectricLocomotive/Main.java
Normal file
@ -0,0 +1,10 @@
|
||||
package ProjectElectricLocomotive;
|
||||
|
||||
import ProjectElectricLocomotive.src.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
new FormElectricLocomotive();
|
||||
}
|
||||
}
|
19
ProjectElectricLocomotive/src/CountWheels.java
Normal file
19
ProjectElectricLocomotive/src/CountWheels.java
Normal file
@ -0,0 +1,19 @@
|
||||
package ProjectElectricLocomotive.src;
|
||||
|
||||
public enum CountWheels
|
||||
{
|
||||
One(1),
|
||||
Two(2),
|
||||
Three(3),
|
||||
Fourth(4);
|
||||
private final int Value;
|
||||
|
||||
CountWheels(int value) {
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public int GetCountWheels() {
|
||||
return Value;
|
||||
}
|
||||
}
|
||||
|
19
ProjectElectricLocomotive/src/DirectionType.java
Normal file
19
ProjectElectricLocomotive/src/DirectionType.java
Normal file
@ -0,0 +1,19 @@
|
||||
package ProjectElectricLocomotive.src;
|
||||
|
||||
public enum DirectionType
|
||||
{
|
||||
Unknown(-1),
|
||||
Up(1),
|
||||
Down(2),
|
||||
Left(3),
|
||||
Right(4);
|
||||
private final int Value;
|
||||
|
||||
DirectionType(int value) {
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return Value;
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package ProjectElectricLocomotive.src.Drawnings;
|
||||
|
||||
import ProjectElectricLocomotive.src.Entities.EntityElectricLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingElectricLocomotive extends DrawingLocomotive
|
||||
{
|
||||
|
||||
public IDrawingOrnament _ornament;
|
||||
|
||||
public DrawingElectricLocomotive(int speed, float weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement)
|
||||
{
|
||||
super(83,45);
|
||||
_entityLocomotive = new EntityElectricLocomotive(speed, weight, bodyColor, additionalColor, electricHorns, batteryPlacement);
|
||||
Random rnd = new Random();
|
||||
int varOrnament = rnd.nextInt(3);
|
||||
|
||||
switch (varOrnament)
|
||||
{
|
||||
case 0:
|
||||
_ornament = new DrawingOrnamentWheels();
|
||||
break;
|
||||
case 1:
|
||||
_ornament = new DrawningSecondOrnamentWheels();
|
||||
break;
|
||||
default:
|
||||
_ornament = new DrawingWheels();
|
||||
break;
|
||||
}
|
||||
_ornament.SetCountWheels(rnd.nextInt(1,5));
|
||||
}
|
||||
@Override
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (!(_entityLocomotive instanceof EntityElectricLocomotive _entityElectricLocomotive) || _startPosX == null) return;
|
||||
|
||||
super.DrawTransport(g);
|
||||
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(_startPosX + 48, _startPosY + 38, 20, 10);
|
||||
g.fillRect(_startPosX + 9, _startPosY + 38, 20, 10);
|
||||
|
||||
// Создание перьев и кистей для прорисовки электровоза
|
||||
Color blackColor = Color.BLACK;
|
||||
Color deepSkyBlueColor = new Color(0, 191, 255);
|
||||
Color yellowColor = Color.YELLOW;
|
||||
|
||||
if (_entityElectricLocomotive.GetElectricHorns()) {
|
||||
// Инициализация опорных точек для прорисовки "рогов"
|
||||
Point pointHorns1 = new Point(_startPosX + 13, _startPosY + 5);
|
||||
Point pointHorns2 = new Point(_startPosX + 16, _startPosY + 2);
|
||||
Point pointHorns3 = new Point(_startPosX + 11, _startPosY);
|
||||
|
||||
g.setColor(_entityElectricLocomotive.GetAdditionalColor());
|
||||
|
||||
// Прорисовка "рогов" электровоза
|
||||
g.drawLine(pointHorns1.x, pointHorns1.y, pointHorns2.x, pointHorns2.y);
|
||||
g.drawLine(pointHorns2.x, pointHorns2.y, pointHorns3.x, pointHorns3.y);
|
||||
|
||||
g.setColor(blackColor);
|
||||
}
|
||||
|
||||
if (_entityElectricLocomotive.GetBatteryPlacement()) {
|
||||
// Инициализация опорных точек для прорисовки молнии на хранилище батарей
|
||||
Point pointLightning1 = new Point(_startPosX + 39, _startPosY + 37);
|
||||
Point pointLightning2 = new Point(_startPosX + 37, _startPosY + 39);
|
||||
Point pointLightning3 = new Point(_startPosX + 39, _startPosY + 40);
|
||||
Point pointLightning4 = new Point(_startPosX + 37, _startPosY + 41);
|
||||
|
||||
g.setColor(_entityElectricLocomotive.GetAdditionalColor());
|
||||
|
||||
// Прорисовка "хранилища батарей" электровоза
|
||||
|
||||
|
||||
g.fillRect(_startPosX.intValue() + 36, _startPosY + 37, 8, 4);
|
||||
g.setColor(yellowColor);
|
||||
g.drawLine(pointLightning1.x, pointLightning1.y, pointLightning2.x, pointLightning2.y);
|
||||
g.drawLine(pointLightning2.x, pointLightning2.y, pointLightning3.x, pointLightning3.y);
|
||||
g.drawLine(pointLightning3.x, pointLightning3.y, pointLightning4.x, pointLightning4.y);
|
||||
}
|
||||
|
||||
_ornament.DrawWheels(g, _startPosX, _startPosY);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
111
ProjectElectricLocomotive/src/Drawnings/DrawingField.java
Normal file
111
ProjectElectricLocomotive/src/Drawnings/DrawingField.java
Normal file
@ -0,0 +1,111 @@
|
||||
package ProjectElectricLocomotive.src.Drawnings;
|
||||
|
||||
|
||||
import ProjectElectricLocomotive.src.DirectionType;
|
||||
import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive;
|
||||
import ProjectElectricLocomotive.src.FormElectricLocomotive;
|
||||
import ProjectElectricLocomotive.src.MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingField extends JPanel {
|
||||
private final FormElectricLocomotive field;
|
||||
DrawingLocomotive _drawingLocomotive;
|
||||
private AbstractStrategy _strategy = null;
|
||||
public DrawingField(FormElectricLocomotive field) {
|
||||
this.field = field;
|
||||
}
|
||||
@Override
|
||||
protected void paintComponent(Graphics g)
|
||||
{
|
||||
if (_drawingLocomotive == null) return;
|
||||
_drawingLocomotive.DrawTransport(g);
|
||||
}
|
||||
public void UpButtonAction()
|
||||
{
|
||||
if (_drawingLocomotive == null) return;
|
||||
_drawingLocomotive.MoveTransport(DirectionType.Up);
|
||||
}
|
||||
public void DownButtonAction()
|
||||
{
|
||||
if (_drawingLocomotive == null) return;
|
||||
_drawingLocomotive.MoveTransport(DirectionType.Down);
|
||||
}
|
||||
public void RightButtonAction()
|
||||
{
|
||||
if (_drawingLocomotive == null) return;
|
||||
_drawingLocomotive.MoveTransport(DirectionType.Right);
|
||||
}
|
||||
public void LeftButtonAction()
|
||||
{
|
||||
if (_drawingLocomotive == null) return;
|
||||
_drawingLocomotive.MoveTransport(DirectionType.Left);
|
||||
}
|
||||
public void CreateObject(String type){
|
||||
Random rnd = new Random();
|
||||
switch (type)
|
||||
{
|
||||
case "DrawningLocomotive":
|
||||
_drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 3000),
|
||||
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
||||
break;
|
||||
case "DrawningElectricLocomotive":
|
||||
_drawingLocomotive = new DrawingElectricLocomotive(rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 3000),
|
||||
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||
new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)),
|
||||
rnd.nextBoolean(),
|
||||
rnd.nextBoolean());
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
_drawingLocomotive.SetPictureSize(getWidth(),getHeight());
|
||||
_drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
|
||||
}
|
||||
|
||||
public void CreateButtonElectricLocomotiveAction(){
|
||||
CreateObject("DrawningElectricLocomotive");
|
||||
}
|
||||
public void CreateButtonLocomotiveAction(){
|
||||
CreateObject("DrawningLocomotive");
|
||||
}
|
||||
|
||||
public void ButtonStrategyStepAction()
|
||||
{
|
||||
if(_drawingLocomotive == null) return;
|
||||
if (field.comboBoxStrategy.isEnabled())
|
||||
{
|
||||
int selectedIndex = field.comboBoxStrategy.getSelectedIndex();
|
||||
switch (selectedIndex) {
|
||||
case 0:
|
||||
_strategy = new MoveToCenter();
|
||||
break;
|
||||
case 1:
|
||||
_strategy = new MoveToBorder();
|
||||
break;
|
||||
default:
|
||||
_strategy = null;
|
||||
}
|
||||
if(_strategy == null) return;
|
||||
_strategy.SetData(new MoveableLocomotive(_drawingLocomotive), getWidth(), getHeight());
|
||||
}
|
||||
if (_strategy == null) return;
|
||||
field.comboBoxStrategy.setEnabled(false);
|
||||
_strategy.MakeStep();
|
||||
|
||||
if(_strategy.getStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
field.comboBoxStrategy.setEnabled(true);
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void ResizeField(){
|
||||
if (_drawingLocomotive == null) return;
|
||||
_drawingLocomotive.SetPictureSize(getWidth(),getHeight());
|
||||
}
|
||||
}
|
177
ProjectElectricLocomotive/src/Drawnings/DrawingLocomotive.java
Normal file
177
ProjectElectricLocomotive/src/Drawnings/DrawingLocomotive.java
Normal file
@ -0,0 +1,177 @@
|
||||
package ProjectElectricLocomotive.src.Drawnings;
|
||||
|
||||
|
||||
import ProjectElectricLocomotive.src.DirectionType;
|
||||
|
||||
import ProjectElectricLocomotive.src.Entities.EntityLocomotive;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingLocomotive {
|
||||
public EntityLocomotive _entityLocomotive;
|
||||
public EntityLocomotive GetEntityLocomotive()
|
||||
{
|
||||
return _entityLocomotive;
|
||||
}
|
||||
protected Integer _startPosX = null;
|
||||
public Integer getPosX() {
|
||||
return _startPosX;
|
||||
}
|
||||
protected Integer _startPosY = null;
|
||||
public Integer getPosY() {
|
||||
return _startPosY;
|
||||
}
|
||||
private Integer _pictureWidth = null;
|
||||
public int getWidth() {
|
||||
return _drawingLocomotiveWidth;
|
||||
}
|
||||
private Integer _pictureHeight = null;
|
||||
public int getHeight() {
|
||||
return _drawingLocomotiveHeight;
|
||||
}
|
||||
private int _drawingLocomotiveWidth = 83;
|
||||
private int _drawingLocomotiveHeight = 45;
|
||||
|
||||
public DrawingLocomotive(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
this._entityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||||
}
|
||||
|
||||
private DrawingLocomotive()
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
protected DrawingLocomotive(int drawningLocomotiveWidth, int drawningLocomotiveHeight)
|
||||
{
|
||||
this._drawingLocomotiveWidth = drawningLocomotiveWidth;
|
||||
this._drawingLocomotiveHeight = drawningLocomotiveHeight;
|
||||
}
|
||||
|
||||
public boolean SetPictureSize(int width, int height)
|
||||
{
|
||||
if (_drawingLocomotiveWidth > width || _drawingLocomotiveHeight > height)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_startPosX != null && _drawingLocomotiveWidth + _startPosX > width) _startPosX = _pictureWidth - _drawingLocomotiveWidth;
|
||||
if (_startPosY != null && _startPosY + _drawingLocomotiveHeight > _pictureHeight) _startPosY = _pictureHeight - _drawingLocomotiveHeight;
|
||||
return true;
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (_pictureHeight == null || _pictureWidth == null) return;
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
if (x + _drawingLocomotiveWidth > _pictureWidth || y + _drawingLocomotiveHeight > _pictureHeight || x < 0 || y < 0)
|
||||
{
|
||||
if (x < 0) _startPosX = 0;
|
||||
if (y < 0 ) _startPosY = 0;
|
||||
if (x + _drawingLocomotiveWidth > _pictureWidth) _startPosX = _pictureWidth - _drawingLocomotiveWidth;
|
||||
if (y + _drawingLocomotiveHeight > _pictureHeight) _startPosY = _pictureHeight - _drawingLocomotiveHeight;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean MoveTransport(DirectionType direction)
|
||||
{
|
||||
if( _startPosX == null || _startPosY == null) return false;
|
||||
switch (direction)
|
||||
{
|
||||
// Влево
|
||||
case Left:
|
||||
if(_startPosX - _entityLocomotive.Step >= 0)
|
||||
{
|
||||
_startPosX -= (int)_entityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
// Вправо
|
||||
case Right:
|
||||
if(_startPosX + _entityLocomotive.Step + _drawingLocomotiveWidth < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)_entityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
// Вверх
|
||||
case Up:
|
||||
if(_startPosY - _entityLocomotive.Step >= 0)
|
||||
{
|
||||
_startPosY -= (int)_entityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
// Вниз
|
||||
case Down:
|
||||
if(_startPosY + _entityLocomotive.Step + _drawingLocomotiveHeight < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)_entityLocomotive.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics g) {
|
||||
if (_entityLocomotive == null || _startPosX == null || _startPosY == null) return;
|
||||
|
||||
// Создание перьев и кистей для прорисовки электровоза
|
||||
Color blackColor = Color.BLACK;
|
||||
Color deepSkyBlueColor = new Color(0, 191, 255);
|
||||
Color yellowColor = Color.YELLOW;
|
||||
|
||||
// Инициализация опорных точек для прорисовки корпуса
|
||||
int pointStartX = _startPosX + 75;
|
||||
int pointStartY = _startPosY + 20;
|
||||
|
||||
Point pointStart = new Point(pointStartX, pointStartY);
|
||||
Point point1 = new Point(pointStartX, pointStartY - 15);
|
||||
Point point2 = new Point(_startPosX + 10, pointStartY - 15);
|
||||
Point pointFinish = new Point(_startPosX + 5, pointStartY);
|
||||
|
||||
// Инициализация опорных точек для прорисовки первой "юбки"
|
||||
Point point3 = new Point(_startPosX + 7, _startPosY + 37);
|
||||
Point point4 = new Point(_startPosX, _startPosY + 43);
|
||||
Point point5 = new Point(_startPosX + 7, _startPosY + 43);
|
||||
|
||||
// Инициализация опорных точек для прорисовки второй "юбки"
|
||||
Point point6 = new Point(_startPosX + 69, _startPosY + 37);
|
||||
Point point7 = new Point(_startPosX + 82, _startPosY + 43);
|
||||
Point point8 = new Point(_startPosX + 69, _startPosY + 43);
|
||||
|
||||
// Совокупность точек полигона уголка первой "юбки"
|
||||
Point[] firstTrianglePoints = { point3, point4, point5 };
|
||||
|
||||
// Совокупность точек полигона уголка второй "юбки"
|
||||
Point[] secondTrianglePoints = { point6, point7, point8 };
|
||||
|
||||
g.setColor(blackColor);
|
||||
g.fillOval(_startPosX + 58, _startPosY + 37, 10, 10);
|
||||
g.drawOval(_startPosX + 58, _startPosY + 37, 8, 8);
|
||||
g.fillOval(_startPosX + 48, _startPosY + 37, 10, 10);
|
||||
g.drawOval(_startPosX + 48, _startPosY + 37, 8, 8);
|
||||
g.fillOval(_startPosX + 19, _startPosY + 37, 10, 10);
|
||||
g.drawOval(_startPosX + 19, _startPosY + 37, 8, 8);
|
||||
g.fillOval(_startPosX + 9, _startPosY + 37, 10, 10);
|
||||
g.drawOval(_startPosX + 9, _startPosY + 37, 8, 8);
|
||||
|
||||
// Прорисовка уголков "юбок"
|
||||
g.setColor(_entityLocomotive.GetBodyColor());
|
||||
g.fillPolygon(new Polygon(new int[]{ point3.x, point4.x, point5.x }, new int[]{ point3.y, point4.y, point5.y }, 3));
|
||||
g.fillPolygon(new Polygon(new int[]{ point6.x, point7.x, point8.x }, new int[]{ point6.y, point7.y, point8.y }, 3));
|
||||
|
||||
// Прорисовка корпуса
|
||||
g.drawRect(_startPosX + 5, _startPosY + 20, 70, 17);
|
||||
g.drawLine(point3.x, point3.y, point4.x, point4.y);
|
||||
g.drawLine(pointStart.x, pointStart.y, point1.x, point1.y);
|
||||
g.drawLine(point1.x, point1.y, point2.x, point2.y);
|
||||
g.drawLine(point2.x, point2.y, pointFinish.x, pointFinish.y);
|
||||
|
||||
// Прорисовка окон
|
||||
g.setColor(deepSkyBlueColor);
|
||||
g.drawRect(_startPosX + 12, _startPosY + 9, 8, 8);
|
||||
g.drawRect(_startPosX+ 63, _startPosY + 9, 8, 8);
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package ProjectElectricLocomotive.src.Drawnings;
|
||||
|
||||
import ProjectElectricLocomotive.src.CountWheels;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingOrnamentWheels implements IDrawingOrnament
|
||||
{
|
||||
private CountWheels _wheels;
|
||||
@Override
|
||||
public void SetCountWheels(int count)
|
||||
{
|
||||
for(CountWheels temp : CountWheels.values())
|
||||
{
|
||||
if(temp.GetCountWheels() >= count)
|
||||
{
|
||||
_wheels = temp;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawWheels(Graphics g,int _startPosX, int _startPosY)
|
||||
{
|
||||
g.setColor(Color.BLACK);
|
||||
switch (_wheels.GetCountWheels())
|
||||
{
|
||||
case 4:
|
||||
g.drawOval(_startPosX + 58, _startPosY + 37, 8, 8);
|
||||
g.drawLine(_startPosX + 62,_startPosY + 38, _startPosX + 62, _startPosY + 38 + 6 );
|
||||
|
||||
case 3:
|
||||
g.drawOval(_startPosX + 48, _startPosY + 37, 8, 8);
|
||||
g.drawLine(_startPosX + 52,_startPosY + 38, _startPosX + 52, _startPosY + 38 + 6 );
|
||||
|
||||
case 2:
|
||||
g.drawOval(_startPosX + 19, _startPosY + 37, 8, 8);
|
||||
g.drawLine(_startPosX + 23,_startPosY + 38, _startPosX + 23, _startPosY + 38 + 6 );
|
||||
|
||||
case 1:
|
||||
g.drawOval(_startPosX + 9, _startPosY + 37, 8, 8);
|
||||
g.drawLine(_startPosX + 13,_startPosY + 38, _startPosX + 13, _startPosY + 38 + 6 );
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
43
ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java
Normal file
43
ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java
Normal file
@ -0,0 +1,43 @@
|
||||
package ProjectElectricLocomotive.src.Drawnings;
|
||||
|
||||
import ProjectElectricLocomotive.src.CountWheels;
|
||||
|
||||
import java.awt.*;
|
||||
public class DrawingWheels implements IDrawingOrnament
|
||||
{
|
||||
private CountWheels _wheels;
|
||||
@Override
|
||||
public void SetCountWheels(int count)
|
||||
{
|
||||
for(CountWheels temp : CountWheels.values())
|
||||
{
|
||||
if(temp.GetCountWheels() >= count)
|
||||
{
|
||||
_wheels = temp;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawWheels(Graphics g,int _startPosX, int _startPosY)
|
||||
{
|
||||
g.setColor(Color.BLACK);
|
||||
switch (_wheels.GetCountWheels())
|
||||
{
|
||||
case 4:
|
||||
g.fillOval(_startPosX + 58, _startPosY + 37, 10, 10);
|
||||
g.drawOval(_startPosX + 58, _startPosY + 37, 8, 8);
|
||||
case 3:
|
||||
g.fillOval(_startPosX + 48, _startPosY + 37, 10, 10);
|
||||
g.drawOval(_startPosX + 48, _startPosY + 37, 8, 8);
|
||||
case 2:
|
||||
g.fillOval(_startPosX + 19, _startPosY + 37, 10, 10);
|
||||
g.drawOval(_startPosX + 19, _startPosY + 37, 8, 8);
|
||||
case 1:
|
||||
g.fillOval(_startPosX + 9, _startPosY + 37, 10, 10);
|
||||
g.drawOval(_startPosX + 9, _startPosY + 37, 8, 8);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package ProjectElectricLocomotive.src.Drawnings;
|
||||
|
||||
import ProjectElectricLocomotive.src.CountWheels;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningSecondOrnamentWheels implements IDrawingOrnament
|
||||
{
|
||||
private CountWheels _wheels;
|
||||
@Override
|
||||
public void SetCountWheels(int count)
|
||||
{
|
||||
for(CountWheels temp : CountWheels.values())
|
||||
{
|
||||
if(temp.GetCountWheels() >= count)
|
||||
{
|
||||
_wheels = temp;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawWheels(Graphics g,int _startPosX, int _startPosY)
|
||||
{
|
||||
g.setColor(Color.BLACK);
|
||||
switch (_wheels.GetCountWheels())
|
||||
{
|
||||
case 4:
|
||||
g.drawOval(_startPosX + 58, _startPosY + 37, 8, 8);
|
||||
g.drawLine(_startPosX + 58,_startPosY + 38, _startPosX + 58 + 6, _startPosY + 37 + 6 );
|
||||
g.drawLine(_startPosX + 64,_startPosY + 38, _startPosX + 58, _startPosY + 37 + 6 );
|
||||
case 3:
|
||||
g.drawOval(_startPosX + 48, _startPosY + 37, 8, 8);
|
||||
g.drawLine(_startPosX + 48,_startPosY + 38, _startPosX + 48 + 6, _startPosY + 37 + 6 );
|
||||
g.drawLine(_startPosX + 54,_startPosY + 38, _startPosX + 48, _startPosY + 37 + 6 );
|
||||
case 2:
|
||||
g.drawOval(_startPosX + 19, _startPosY + 37, 8, 8);
|
||||
g.drawLine(_startPosX + 19,_startPosY + 38, _startPosX + 19 + 6, _startPosY + 37 + 6 );
|
||||
g.drawLine(_startPosX + 25,_startPosY + 38, _startPosX + 19, _startPosY + 37 + 6 );
|
||||
case 1:
|
||||
g.drawOval(_startPosX + 9, _startPosY + 37, 8, 8);
|
||||
g.drawLine(_startPosX + 9,_startPosY + 38, _startPosX + 9 + 6, _startPosY + 37 + 6 );
|
||||
g.drawLine(_startPosX + 15,_startPosY + 38, _startPosX + 9, _startPosY + 37 + 6 );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package ProjectElectricLocomotive.src.Drawnings;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingOrnament
|
||||
{
|
||||
void SetCountWheels(int count);
|
||||
void DrawWheels(Graphics g, int _startPosX, int _startPosY);
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package ProjectElectricLocomotive.src.Entities;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityElectricLocomotive extends EntityLocomotive {
|
||||
|
||||
private Color AdditionalColor;
|
||||
public Color GetAdditionalColor()
|
||||
{
|
||||
return AdditionalColor;
|
||||
}
|
||||
private boolean ElectricHorns;
|
||||
public boolean GetElectricHorns()
|
||||
{
|
||||
return ElectricHorns;
|
||||
}
|
||||
private boolean BatteryPlacement;
|
||||
public boolean GetBatteryPlacement()
|
||||
{
|
||||
return BatteryPlacement;
|
||||
}
|
||||
public EntityElectricLocomotive(int speed, float weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement)
|
||||
{
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor = additionalColor;
|
||||
ElectricHorns = electricHorns;
|
||||
BatteryPlacement = batteryPlacement;
|
||||
}
|
||||
|
||||
}
|
36
ProjectElectricLocomotive/src/Entities/EntityLocomotive.java
Normal file
36
ProjectElectricLocomotive/src/Entities/EntityLocomotive.java
Normal file
@ -0,0 +1,36 @@
|
||||
package ProjectElectricLocomotive.src.Entities;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityLocomotive
|
||||
{
|
||||
private int Speed;
|
||||
public int GetSpeed()
|
||||
{
|
||||
return Speed;
|
||||
}
|
||||
private float Weight;
|
||||
public float GetWeight()
|
||||
{
|
||||
return Weight;
|
||||
}
|
||||
private Color BodyColor;
|
||||
public Color GetBodyColor()
|
||||
{
|
||||
return BodyColor;
|
||||
}
|
||||
public float Step;
|
||||
public float GetStep()
|
||||
{
|
||||
return Step;
|
||||
}
|
||||
public EntityLocomotive(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
Speed = speed <= 0 ? rnd.nextInt(100, 300) : speed;
|
||||
Weight = weight <= 0 ? rnd.nextInt(1000, 3000) : weight;
|
||||
BodyColor = bodyColor;
|
||||
Step = Speed * 100 / Weight;
|
||||
}
|
||||
}
|
162
ProjectElectricLocomotive/src/FormElectricLocomotive.java
Normal file
162
ProjectElectricLocomotive/src/FormElectricLocomotive.java
Normal file
@ -0,0 +1,162 @@
|
||||
package ProjectElectricLocomotive.src;
|
||||
|
||||
import ProjectElectricLocomotive.src.Drawnings.DrawingField;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
|
||||
public class FormElectricLocomotive extends JFrame{
|
||||
private int Width;
|
||||
private int Height;
|
||||
|
||||
JPanel BottomPanel = new JPanel();
|
||||
JPanel CreatePanel = new JPanel();
|
||||
JPanel BottomAndCreatePanel = new JPanel();
|
||||
JPanel DimPanel = new JPanel();
|
||||
JPanel UPanel = new JPanel();
|
||||
JPanel DPanel = new JPanel();
|
||||
JPanel LRPanel = new JPanel();
|
||||
|
||||
JPanel CBPanel = new JPanel();
|
||||
JPanel CPBandButtonPanel = new JPanel();
|
||||
|
||||
DrawingField field = new DrawingField(this);
|
||||
|
||||
JButton ButtonStep = new JButton("Шаг");
|
||||
|
||||
JButton ButtonCreateElectricLocomotive=new JButton("CreateElectricLocomotive");
|
||||
JButton ButtonCreateLocomotive=new JButton("CreateLocomotive");
|
||||
|
||||
public JComboBox<String> comboBoxStrategy = new JComboBox<>();
|
||||
|
||||
JButton ButtonUp=new JButton("Up");
|
||||
|
||||
|
||||
JButton ButtonDown=new JButton("Down");
|
||||
|
||||
|
||||
JButton ButtonRight=new JButton("Right");
|
||||
|
||||
JButton ButtonLeft=new JButton("Left");
|
||||
public FormElectricLocomotive(){
|
||||
super("Electric Locomotive");
|
||||
setSize(900,500);
|
||||
Width = getWidth();
|
||||
Height = getHeight();
|
||||
setBackground(Color.WHITE);
|
||||
ShowWindow();
|
||||
RefreshWindow();
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public void ShowWindow(){
|
||||
|
||||
Dimension dimen = new Dimension(30,30);
|
||||
|
||||
ButtonUp.setPreferredSize(dimen);
|
||||
ButtonUp.addActionListener(e->{
|
||||
field.UpButtonAction();
|
||||
repaint();
|
||||
});
|
||||
|
||||
ButtonDown.setPreferredSize(dimen);
|
||||
ButtonDown.addActionListener(e->{
|
||||
field.DownButtonAction();
|
||||
repaint();
|
||||
});
|
||||
|
||||
ButtonRight.setPreferredSize(dimen);
|
||||
ButtonRight.addActionListener(e->{
|
||||
field.RightButtonAction();
|
||||
repaint();
|
||||
});
|
||||
|
||||
ButtonLeft.setPreferredSize(dimen);
|
||||
ButtonLeft.addActionListener(e->{
|
||||
field.LeftButtonAction();
|
||||
repaint();
|
||||
});
|
||||
|
||||
LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0));
|
||||
LRPanel.setBackground(new Color(0,0,0,0));
|
||||
LRPanel.add(ButtonLeft);
|
||||
LRPanel.add(ButtonRight);
|
||||
|
||||
UPanel.setLayout(new FlowLayout());
|
||||
UPanel.setBackground(new Color(0,0,0,0));
|
||||
UPanel.add(ButtonUp);
|
||||
|
||||
DPanel.setLayout(new FlowLayout());
|
||||
DPanel.setBackground(new Color(0,0,0,0));
|
||||
DPanel.add(ButtonDown);
|
||||
|
||||
DimPanel.setLayout(new BoxLayout(DimPanel,BoxLayout.Y_AXIS));
|
||||
DimPanel.setBackground(new Color(0,0,0,0));
|
||||
DimPanel.add(UPanel);
|
||||
DimPanel.add(LRPanel);
|
||||
DimPanel.add(DPanel);
|
||||
add(DimPanel);
|
||||
|
||||
CreatePanel.setLayout(new FlowLayout());
|
||||
CreatePanel.setBackground(new Color(0,0,0,0));
|
||||
|
||||
CreatePanel.add(ButtonCreateLocomotive);
|
||||
ButtonCreateLocomotive.addActionListener(e->{
|
||||
field.CreateButtonLocomotiveAction();
|
||||
repaint();
|
||||
});
|
||||
|
||||
CreatePanel.add(ButtonCreateElectricLocomotive);
|
||||
ButtonCreateElectricLocomotive.addActionListener(e->{
|
||||
field.CreateButtonElectricLocomotiveAction();
|
||||
repaint();
|
||||
});
|
||||
comboBoxStrategy.setPreferredSize(new Dimension(80, 40));
|
||||
comboBoxStrategy.addItem("К центру");
|
||||
comboBoxStrategy.addItem("К краю");
|
||||
ButtonStep.setPreferredSize(new Dimension(80, 40));
|
||||
ButtonStep.addActionListener(e->{
|
||||
field.ButtonStrategyStepAction();
|
||||
repaint();
|
||||
});
|
||||
|
||||
CBPanel.setLayout(new FlowLayout());
|
||||
CBPanel.setBackground(new Color(0, 0, 0, 0));
|
||||
CBPanel.add(comboBoxStrategy);
|
||||
CBPanel.add(ButtonStep);
|
||||
CPBandButtonPanel.setLayout(new BoxLayout(CPBandButtonPanel, BoxLayout.X_AXIS));
|
||||
CPBandButtonPanel.setBackground(new Color(0, 0, 0, 0));
|
||||
CPBandButtonPanel.add(CBPanel);
|
||||
add(CPBandButtonPanel);
|
||||
|
||||
BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel, BoxLayout.Y_AXIS));
|
||||
BottomAndCreatePanel.setBackground(new Color(0,0,0,0));
|
||||
BottomAndCreatePanel.add(CreatePanel);
|
||||
BottomAndCreatePanel.add(BottomPanel);
|
||||
|
||||
add(BottomAndCreatePanel);
|
||||
add(field);
|
||||
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
super.componentResized(e);
|
||||
Width = getWidth();
|
||||
Height = getHeight();
|
||||
field.ResizeField();
|
||||
repaint();
|
||||
RefreshWindow();
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
public void RefreshWindow(){
|
||||
field.setLayout(new FlowLayout());
|
||||
CPBandButtonPanel.setBounds(Width - 200, 0, 190, 140);
|
||||
BottomAndCreatePanel.setBounds(-330, Height - 110, Width, 180);
|
||||
DimPanel.setBounds(Width - 170, Height - 170, 190, 140);
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
private IMoveableObject _moveableObject = null;
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
protected int fieldWidth;
|
||||
protected int fieldHeight;
|
||||
protected StrategyStatus state;
|
||||
|
||||
protected ObjectParameters getObjectParameters() {
|
||||
return (_moveableObject != null) ? _moveableObject.getObjectPosition() : null;
|
||||
}
|
||||
|
||||
protected Integer GetStep() {
|
||||
if (state != StrategyStatus.InProgress) {
|
||||
return null;
|
||||
}
|
||||
return (_moveableObject != null) ? _moveableObject.getStep() : null;
|
||||
}
|
||||
|
||||
public StrategyStatus getStatus() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height) {
|
||||
if (moveableObject == null) {
|
||||
state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
state = StrategyStatus.InProgress;
|
||||
this._moveableObject = moveableObject;
|
||||
fieldWidth = width;
|
||||
fieldHeight = height;
|
||||
}
|
||||
|
||||
public void MakeStep() {
|
||||
if (state != StrategyStatus.InProgress) {
|
||||
return;
|
||||
}
|
||||
if (isTargetDestination()) {
|
||||
state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
moveToTarget();
|
||||
}
|
||||
|
||||
protected boolean MoveLeft() {
|
||||
return moveTo(MovementDirection.Left);
|
||||
}
|
||||
|
||||
protected boolean MoveRight() {
|
||||
return moveTo(MovementDirection.Right);
|
||||
}
|
||||
|
||||
protected boolean MoveUp() {
|
||||
return moveTo(MovementDirection.Up);
|
||||
}
|
||||
|
||||
protected boolean MoveDown() {
|
||||
return moveTo(MovementDirection.Down);
|
||||
}
|
||||
|
||||
private boolean moveTo(MovementDirection movementDirection) {
|
||||
if (state != StrategyStatus.InProgress) {
|
||||
return false;
|
||||
}
|
||||
return (_moveableObject != null) && _moveableObject.tryMoveObject(movementDirection);
|
||||
}
|
||||
|
||||
protected abstract void moveToTarget();
|
||||
|
||||
protected abstract boolean isTargetDestination();
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||
|
||||
public interface IMoveableObject
|
||||
{
|
||||
ObjectParameters getObjectPosition();
|
||||
int getStep();
|
||||
boolean tryMoveObject(MovementDirection direction);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy
|
||||
{
|
||||
@Override
|
||||
protected void moveToTarget()
|
||||
{
|
||||
ObjectParameters objParams = getObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.getRightBorder() - fieldWidth;
|
||||
if (Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0) MoveLeft();
|
||||
else MoveRight();
|
||||
}
|
||||
int diffY = objParams.getDownBorder() - fieldHeight;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0) MoveUp();
|
||||
else MoveDown();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isTargetDestination() {
|
||||
ObjectParameters objParams = getObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.getRightBorder() + GetStep() >= fieldWidth && objParams.getDownBorder() + GetStep() >= fieldHeight;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy
|
||||
{
|
||||
|
||||
@Override
|
||||
protected void moveToTarget() {
|
||||
ObjectParameters objParams = getObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.getObjectMiddleHorizontal() - fieldWidth / 2;
|
||||
if(Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0) MoveLeft();
|
||||
else MoveRight();
|
||||
}
|
||||
int diffY = objParams.getObjectMiddleVertical() - fieldHeight / 2;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0) MoveUp();
|
||||
else MoveDown();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isTargetDestination() {
|
||||
ObjectParameters objParams = getObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.getObjectMiddleHorizontal() - GetStep() <= fieldWidth / 2 && objParams.getObjectMiddleHorizontal() + GetStep() >= fieldWidth / 2 &&
|
||||
objParams.getObjectMiddleVertical() - GetStep() <= fieldHeight / 2 && objParams.getObjectMiddleVertical() + GetStep() >= fieldHeight / 2;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||
|
||||
import ProjectElectricLocomotive.src.DirectionType;
|
||||
import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive;
|
||||
|
||||
public class MoveableLocomotive implements IMoveableObject {
|
||||
private DrawingLocomotive _locomotive = null;
|
||||
public MoveableLocomotive(DrawingLocomotive drawingLocomotive)
|
||||
{
|
||||
_locomotive = drawingLocomotive;
|
||||
}
|
||||
public ObjectParameters getObjectPosition() {
|
||||
if (_locomotive == null || _locomotive.GetEntityLocomotive() == null || _locomotive.getPosX() == null || _locomotive.getPosY() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ObjectParameters(_locomotive.getPosX(), _locomotive.getPosY(), _locomotive.getWidth(), _locomotive.getHeight());
|
||||
}
|
||||
public int getStep() {
|
||||
if(_locomotive == null || _locomotive.GetEntityLocomotive() == null) return 0;
|
||||
return (int)(_locomotive.GetEntityLocomotive().Step);
|
||||
}
|
||||
|
||||
public boolean tryMoveObject(MovementDirection direction) {
|
||||
if (_locomotive == null || _locomotive.GetEntityLocomotive() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DirectionType directionType = getDirectionType(direction);
|
||||
return _locomotive.MoveTransport(directionType);
|
||||
}
|
||||
|
||||
private DirectionType getDirectionType(MovementDirection direction) {
|
||||
switch (direction) {
|
||||
case Left:
|
||||
return DirectionType.Left;
|
||||
case Right:
|
||||
return DirectionType.Right;
|
||||
case Up:
|
||||
return DirectionType.Up;
|
||||
case Down:
|
||||
return DirectionType.Down;
|
||||
default:
|
||||
return DirectionType.Unknown;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||
|
||||
public enum MovementDirection
|
||||
{
|
||||
Up(1),
|
||||
Down(2),
|
||||
Left(3),
|
||||
Right(4);
|
||||
private final int Value;
|
||||
|
||||
MovementDirection(int value) {
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return Value;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||
|
||||
public class ObjectParameters
|
||||
{
|
||||
private final int x;
|
||||
public int getLeftBorder() {
|
||||
return x;
|
||||
}
|
||||
private final int y;
|
||||
public int getTopBorder() {
|
||||
return y;
|
||||
}
|
||||
private final int width;
|
||||
|
||||
private final int height;
|
||||
|
||||
public ObjectParameters(int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
|
||||
public int getRightBorder() {
|
||||
return x + width;
|
||||
}
|
||||
public int getDownBorder() {
|
||||
return y + height;
|
||||
}
|
||||
|
||||
public int getObjectMiddleHorizontal() {
|
||||
return x + width / 2;
|
||||
}
|
||||
|
||||
public int getObjectMiddleVertical() {
|
||||
return y + height / 2;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package ProjectElectricLocomotive.src.MovementStrategy;
|
||||
|
||||
public enum StrategyStatus
|
||||
{
|
||||
NotInit,
|
||||
|
||||
InProgress,
|
||||
|
||||
Finish
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user