Compare commits

...

4 Commits

31 changed files with 1063 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,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 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_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

9
.idea/modules.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PIbd-11_Gorskov_E.M._Hard.iml" filepath="$PROJECT_DIR$/.idea/PIbd-11_Gorskov_E.M._Hard.iml" />
<module fileurl="file://$PROJECT_DIR$/src/src.iml" filepath="$PROJECT_DIR$/src/src.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="" vcs="Git" />
</component>
</project>

BIN
src/image/down.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

BIN
src/image/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

BIN
src/image/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

BIN
src/image/up.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

11
src/src.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,12 @@
package ChoiseAndDrawingPortholes;
public enum CountPortholes {
TenPortholes(10),
TwentyPortholes(20),
ThirtyPortholes(30);
private int numberofportholes;
CountPortholes(int numberofportholes) {
this.numberofportholes = numberofportholes;
}
public int getNumportholes() {return numberofportholes;}
}

View File

@ -0,0 +1,57 @@
package ChoiseAndDrawingPortholes;
import java.awt.*;
import Entities.*;
public class DrawingPortholesCircle implements IDifferentPortholes {
private CountPortholes _porthole;
public CountPortholes getCount() {
return _porthole;
}
public void SetCount(int count) {
switch (count) {
case 10:
_porthole = CountPortholes.TenPortholes;
break;
case 20:
_porthole = CountPortholes.TwentyPortholes;
break;
case 30:
_porthole = CountPortholes.ThirtyPortholes;
break;
default:
_porthole = CountPortholes.TenPortholes;
break;
}
}
protected void drawPortholes(Graphics2D g, int posX, int posY) {
g.setColor(Color.cyan);
g.fillOval(posX, posY, 3, 3);
g.setColor(Color.black);
g.drawOval(posX, posY, 3, 3);
}
public void Draw(Graphics2D g, int _startPosX, int _startPosY) {
if (_porthole == null) {
return;
}
for (int i = 0; i < 10; ++i) {
drawPortholes(g, _startPosX + 19 + i * 8, _startPosY + 21);
}
if (_porthole != CountPortholes.TenPortholes) {
for (int i = 0; i < 5; ++i) {
drawPortholes(g, _startPosX - 15 + i * 5, _startPosY + 28);
drawPortholes(g, _startPosX + 115 + i * 5, _startPosY + 28);
}
}
if (_porthole == CountPortholes.ThirtyPortholes) {
for (int i = 0; i < 10; ++i) {
drawPortholes(g, _startPosX + 19 + i * 8, _startPosY + 37);
}
}
}
}

View File

@ -0,0 +1,14 @@
package ChoiseAndDrawingPortholes;
import java.awt.*;
public class DrawingPortholesHeart extends DrawingPortholesCircle {
protected void drawPortholes(Graphics2D g, int posX, int posY) {
int[] HeartX = {posX + 2, posX, posX, posX + 1, posX + 2, posX + 3, posX + 5, posX + 5};
int[] HeartY = {posY + 4, posY + 2, posY, posY, posY + 1, posY, posY, posY + 2};
g.setColor(Color.cyan);
g.fillPolygon(HeartX, HeartY, HeartX.length);
g.setColor(Color.black);
g.drawPolygon(HeartX, HeartY, HeartX.length);
}
}

View File

@ -0,0 +1,12 @@
package ChoiseAndDrawingPortholes;
import java.awt.*;
public class DrawingPortholesSquare extends DrawingPortholesCircle {
protected void drawPortholes(Graphics2D g, int posX, int posY){
g.setColor(Color.cyan);
g.fillRect(posX, posY, 3, 3);
g.setColor(Color.black);
g.drawRect(posX, posY, 3, 3);
}
}

View File

@ -0,0 +1,9 @@
package ChoiseAndDrawingPortholes;
import java.awt.*;
public interface IDifferentPortholes {
public CountPortholes getCount();
public void SetCount (int count);
public void Draw (Graphics2D g, int _startPosX, int _startPoxY);
}

View File

@ -0,0 +1,20 @@
package Drawings;
import Drawings.DrawingAirbus;
import javax.swing.*;
import java.awt.*;
public class CanvasAirbus extends JComponent {
public DrawingAirplan _drawingAirplan;
public CanvasAirbus(){}
public void paintComponent(Graphics g) {
if (_drawingAirplan == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingAirplan.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,42 @@
package Drawings;
import Drawings.DrawingAirplan;
import Entities.EntityAirbus;
import java.awt.*;
public class DrawingAirbus extends DrawingAirplan {
public DrawingAirbus(int speed, int weight, Color bodyColor, int countPortholes, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine, int width, int height) {
super(speed, weight, bodyColor, countPortholes, width, height);
if (entityAirplan != null) {
entityAirplan = new EntityAirbus(speed, weight, bodyColor, additionalColor, isCompartment, isAdditionalEngine);
}
}
@Override
public void DrawTransport(Graphics2D g) {
if (entityAirplan == null) {
return;
}
Color additionalColor = ((EntityAirbus) entityAirplan).getAdditionalColor();
//Пассажирсакий доп. отсек
if (((EntityAirbus) entityAirplan).IsCompartment()) {
g.setColor(additionalColor);
g.drawArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180);
g.fillArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180);
}
super.DrawTransport(g);
// Доп. двигатели
if (((EntityAirbus) entityAirplan).IsAdditionalEngine()) {
g.drawLine(_startPosX + 95, _startPosY + 68, _startPosX + 95, _startPosY + 75);
g.setColor(additionalColor);
int[] xPolygon = {_startPosX + 83, _startPosX + 103, _startPosX + 103, _startPosX + 83, _startPosX + 83};
int[] yPolygon = {_startPosY + 78, _startPosY + 73, _startPosY + 93, _startPosY + 88, _startPosY + 78};
g.drawPolygon(xPolygon, yPolygon, xPolygon.length);
g.fillPolygon(xPolygon, yPolygon, xPolygon.length);
}
}
}

View File

@ -0,0 +1,167 @@
package Drawings;
import Entities.*;
import ChoiseAndDrawingPortholes.*;
import MovementStrategy.Direction;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingAirplan {
public EntityAirplan entityAirplan;
public IDifferentPortholes _portholes;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
private int _airbusWidth = 210;
private int _airbusHeight = 100;
public int GetPosX() {
return _startPosX;
}
public int GetPosY() {
return _startPosY;
}
public int GetWidth() {
return _airbusWidth;
}
public int GetHeight() {
return _airbusHeight;
}
public DrawingAirplan(int speed, float weight, Color bodyColor, int countPortholes, int width, int height) {
if (width < _airbusHeight || height < _airbusWidth)
return;
_pictureWidth = width;
_pictureHeight = height;
entityAirplan = new EntityAirplan(speed, weight, bodyColor);
Random random = new Random();
switch (random.nextInt(0, 3)) {
case 0:
_portholes = new DrawingPortholesCircle();
break;
case 1:
_portholes = new DrawingPortholesHeart();
break;
case 2:
_portholes = new DrawingPortholesSquare();
break;
default:
_portholes = new DrawingPortholesCircle();
break;
}
_portholes.SetCount(countPortholes);
}
public void SetPosition(int x, int y) {
if (x > _pictureWidth || y > _pictureHeight || x < 0 || y < 0) {
_startPosX = 0;
_startPosY = 0;
} else {
_startPosX = x;
_startPosY = y;
}
}
public boolean CanMove(Direction direction) {
if (entityAirplan == null) {
return false;
}
switch (direction) {
case Left:
return _startPosX - entityAirplan.Step > 5;
case Right:
return _startPosX + _airbusWidth + entityAirplan.Step < _pictureWidth;
case Up:
return _startPosY - entityAirplan.Step > 0;
case Down:
return _startPosY + _airbusHeight + entityAirplan.Step < _pictureHeight;
default:
return false;
}
}
public void MoveTransport(Direction direction) {
if (!CanMove(direction) || entityAirplan == null) {
return;
}
switch (direction) {
case Left:
_startPosX -= entityAirplan.Step;
break;
case Right:
_startPosX += entityAirplan.Step;
break;
case Up:
_startPosY -= entityAirplan.Step;
break;
case Down:
_startPosY += entityAirplan.Step;
break;
}
}
public void DrawTransport(Graphics2D g) {
if (entityAirplan == null) {
return;
}
g.setColor(Color.BLACK);
//Тело
g.setColor(entityAirplan.getBodyColor());
g.fillRect(_startPosX + 5, _startPosY + 50, 170, 30);
g.fillArc(_startPosX - 5, _startPosY + 50, 20, 30, 90, 180);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 5, _startPosY + 50, 170, 30);
g.drawArc(_startPosX - 5, _startPosY + 50, 20, 30, 90, 180);
_portholes.Draw(g, _startPosX + 30, _startPosY + 34);
//Заднее крыло
g.setColor(Color.BLACK);
g.drawPolygon(new int[]{_startPosX + 50, _startPosX, _startPosX}, new int[]{_startPosY + 50, _startPosY, _startPosY + 50}, 3);
g.setColor(entityAirplan.getBodyColor());
g.fillPolygon(new int[]{_startPosX + 50, _startPosX, _startPosX}, new int[]{_startPosY + 50, _startPosY, _startPosY + 50}, 3);
//Заднее боковые крылья
g.setColor(Color.BLACK);
g.drawOval(_startPosX - 7, _startPosY + 45, 30, 8);
g.setColor(entityAirplan.getBodyColor());
g.fillOval(_startPosX - 7, _startPosY + 45, 30, 8);
//Нос
g.setColor(Color.BLACK);
g.drawPolygon(new int[]{_startPosX + 175, _startPosX + 200, _startPosX + 175}, new int[]{_startPosY + 50, _startPosY + 65, _startPosY + 65}, 3);
g.drawPolygon(new int[]{_startPosX + 175, _startPosX + 200, _startPosX + 175}, new int[]{_startPosY + 80, _startPosY + 65, _startPosY + 65}, 3);
g.setColor(entityAirplan.getBodyColor());
g.fillPolygon(new int[]{_startPosX + 175, _startPosX + 200, _startPosX + 175}, new int[]{_startPosY + 50, _startPosY + 65, _startPosY + 65}, 3);
g.fillPolygon(new int[]{_startPosX + 175, _startPosX + 200, _startPosX + 175}, new int[]{_startPosY + 80, _startPosY + 65, _startPosY + 65}, 3);
//Крылья
g.setColor(Color.BLACK);
g.drawArc(_startPosX + 49, _startPosY + 62, 5, 5, 90, 180);
g.drawLine(_startPosX + 51, _startPosY + 62, _startPosX + 140, _startPosY + 62);
g.drawArc(_startPosX + 137, _startPosY + 62, 5, 5, 2790, 180);
g.drawLine(_startPosX + 51, _startPosY + 67, _startPosX + 140, _startPosY + 67);
//Задние шасси
g.drawLine(_startPosX + 55, _startPosY + 80, _startPosX + 55, _startPosY + 90);
g.drawOval(_startPosX + 47, _startPosY + 90, 5, 5);
g.drawOval(_startPosX + 57, _startPosY + 90, 5, 5);
g.setColor(entityAirplan.getBodyColor());
g.fillOval(_startPosX + 47, _startPosY + 90, 5, 5);
g.fillOval(_startPosX + 57, _startPosY + 90, 5, 5);
//Передние шасси
g.setColor(Color.BLACK);
g.drawLine(_startPosX + 165, _startPosY + 80, _startPosX + 165, _startPosY + 90);
g.drawOval(_startPosX + 163, _startPosY + 91, 5, 5);
g.setColor(entityAirplan.getBodyColor());
g.fillOval(_startPosX + 163, _startPosY + 91, 5, 5);
}
}

View File

@ -0,0 +1,19 @@
package Entities;
import java.awt.*;
public class EntityAirbus extends EntityAirplan {
private Color AdditionalColor;
private boolean IsCompartment;
private boolean IsAdditionalEngine;
public EntityAirbus(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
IsCompartment = isCompartment;
IsAdditionalEngine = isAdditionalEngine;
}
public Color getAdditionalColor() { return AdditionalColor; }
public boolean IsCompartment() { return IsCompartment; }
public boolean IsAdditionalEngine() { return IsAdditionalEngine; }
}

View File

@ -0,0 +1,29 @@
package Entities;
import java.awt.*;
public class EntityAirplan {
private int Speed;
private float Weight;
private Color BodyColor;
public float Step;
public int getSpeed() {
return Speed;
}
public float getWeight() {
return Weight;
}
public Color getBodyColor() {
return BodyColor;
}
public EntityAirplan(int speed, float weight, Color bodyColor)
{
Weight = weight;
Speed = speed;
BodyColor = bodyColor;
Step = Speed * 200 / (int) Weight;
}
}

250
src/src/FormAirbus.java Normal file
View File

@ -0,0 +1,250 @@
import Drawings.DrawingAirbus;
import Drawings.DrawingAirplan;
import MovementStrategy.AbstractStrategy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import MovementStrategy.*;
public class FormAirbus extends JFrame {
private int width;
private int height;
private DrawingAirplan _drawingAirbus;
private AbstractStrategy _abstractStrategy;
private Canvas canvas;
// выбор кол-ва иллюминаторов
JLabel labelCount;
private JTextField fieldCount;
// выбор стратегии
JLabel labelStrategy;
JComboBox comboBoxStrategy;
JButton buttonStrategy;
private JButton buttonCreateAirbus;
private JButton buttonCreatePlane;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonLeft;
public FormAirbus() {
super("Создание самолёта");
InitializeComponent();
setVisible(true);
}
private void InitializeComponent() {
canvas = new Canvas();
labelCount = new JLabel("Введите число иллюминаторов:");
fieldCount = new JTextField();
labelStrategy = new JLabel("Шаг стратегии:");
comboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
buttonStrategy = new JButton("Выбрать стратегию");
buttonStrategy.setMargin(new Insets(0, 0, 0, 0));
buttonCreateAirbus = new JButton("Создать аэробус");
buttonCreateAirbus.setMargin(new Insets(0, 0, 0, 0));
buttonCreatePlane = new JButton("Создать самолёт");
buttonCreatePlane.setMargin(new Insets(0, 0, 0, 0));
buttonUp = new JButton();
buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon("images\\KeyUp.png"));
buttonRight = new JButton();
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon("images\\KeyRight.png"));
buttonLeft = new JButton();
buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png"));
buttonDown = new JButton();
buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon("images\\KeyDown.png"));
setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
buttonCreateAirbus.setBounds(12, 355, 146, 33);
buttonCreatePlane.setBounds(182, 355, 146, 33);
labelCount.setBounds(42, 405, 240, 20);
fieldCount.setBounds(240, 407, 48, 20);
labelStrategy.setBounds(630, 20, 146, 33);
comboBoxStrategy.setBounds(630, 50, 146, 20);
buttonStrategy.setBounds(630, 80, 146, 33);
buttonUp.setBounds(679, 313, 48, 44);
buttonRight.setBounds(728, 358, 48, 44);
buttonLeft.setBounds(630, 358, 48, 44);
buttonDown.setBounds(679, 358, 48, 44);
labelCount.setBounds(12, 405, 240, 20);
fieldCount.setBounds(210, 407, 48, 20);
canvas.setBounds(0, 0, 790, 460);
add(buttonCreateAirbus);
add(buttonCreatePlane);
add(labelCount);
add(fieldCount);
add(labelStrategy);
add(comboBoxStrategy);
add(buttonStrategy);
add(buttonUp);
add(buttonRight);
add(buttonDown);
add(buttonLeft);
add(labelCount);
add(fieldCount);
add(canvas);
// логика формы
buttonCreateAirbus.addActionListener(buttonCreateAirbusListener);
buttonCreatePlane.addActionListener(buttonCreatePlaneListener);
buttonStrategy.addActionListener(buttonStrategyListener);
buttonUp.addActionListener(buttonsMoveListener);
buttonRight.addActionListener(buttonsMoveListener);
buttonDown.addActionListener(buttonsMoveListener);
buttonLeft.addActionListener(buttonsMoveListener);
}
ActionListener buttonCreateAirbusListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int countPortholes;
try {
countPortholes = Integer.parseInt(fieldCount.getText());
} catch (Exception ex) {
countPortholes = 0;
}
if (countPortholes != 10 && countPortholes != 20 && countPortholes != 30) {
JOptionPane.showMessageDialog(null, "Число должно быть равно 10, 20 или 30.\nКол-во иллюминаторов приравнено к 10");
countPortholes = 10;
}
Random rand = new Random();
_drawingAirbus = new DrawingAirplan(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
countPortholes,
canvas.getWidth(), canvas.getHeight());
_drawingAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
comboBoxStrategy.setEnabled(true);
canvas.repaint();
}
};
ActionListener buttonCreatePlaneListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int countPortholes;
try {
countPortholes = Integer.parseInt(fieldCount.getText());
} catch (Exception ex) {
countPortholes = 0;
}
if (countPortholes != 10 && countPortholes != 20 && countPortholes != 30) {
JOptionPane.showMessageDialog(null, "Число должно быть равно 10, 20 или 30.\nКол-во иллюминаторов приравнено к 10");
countPortholes = 10;
}
Random rand = new Random();
_drawingAirbus = new DrawingAirbus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
countPortholes,
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
rand.nextBoolean(), rand.nextBoolean(),
canvas.getWidth(), canvas.getHeight());
_drawingAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
comboBoxStrategy.setEnabled(true);
canvas.repaint();
}
};
ActionListener buttonStrategyListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawingAirbus == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) {
case 0:
_abstractStrategy = new MoveToCenter();
break;
case 1:
_abstractStrategy = new MoveToBorder();
break;
default:
_abstractStrategy = null;
break;
}
;
if (_abstractStrategy == null) {
return;
}
_abstractStrategy.SetData(new MoveableAirplan(_drawingAirbus), canvas.getWidth(), canvas.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null) {
return;
}
_abstractStrategy.MakeStep();
if (_abstractStrategy.GetStatus() == StrategyStatus.Finish) {
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
canvas.repaint();
}
};
ActionListener buttonsMoveListener = new ActionListener() {
// реакция на нажатие
public void actionPerformed(ActionEvent e) {
if (_drawingAirbus == null) {
return;
}
String command = ((JButton) (e.getSource())).getName();
switch (command) {
case "up":
_drawingAirbus.MoveTransport(Direction.Up);
break;
case "down":
_drawingAirbus.MoveTransport(Direction.Down);
break;
case "right":
_drawingAirbus.MoveTransport(Direction.Right);
break;
case "left":
_drawingAirbus.MoveTransport(Direction.Left);
break;
}
canvas.repaint();
}
};
class Canvas extends JComponent {
public Canvas() {
}
public void paintComponent(Graphics g) {
if (_drawingAirbus == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingAirbus.DrawTransport(g2d);
super.repaint();
}
}
}

7
src/src/Main.java Normal file
View File

@ -0,0 +1,7 @@
import java.awt.*;
public class Main {
public static void main(String[] args) {
new FormAirbus();
}
}

View File

@ -0,0 +1,76 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
protected int FieldWidth;
protected int FieldHeight;
public StrategyStatus GetStatus() { return _state; }
// Изменить статус, установить поля
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_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(Direction.Up); }
protected boolean MoveRight() { return MoveTo(Direction.Right); }
protected boolean MoveUp() { return MoveTo(Direction.Up); }
protected boolean MoveDown() { return MoveTo(Direction.Down); }
// параметры
protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); }
// шаг
protected int GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return 0;
}
return _moveableObject.GetStep();
}
// перемещение
protected abstract void MoveToTarget();
// достигнута ли цель
protected abstract boolean IsTargetDestination();
// попытка перемещения по направлению
private boolean MoveTo(Direction directionType)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
if (_moveableObject.CheckCanMove(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public enum Direction {
Up,
Down,
Left,
Right;
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public interface IMoveableObject {
ObjectParameters GetObjectPosition();
int GetStep();
boolean CheckCanMove(Direction direction);
void MoveObject(Direction direction);
}

View File

@ -0,0 +1,45 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.RightBorder() + GetStep() >= FieldWidth && objParams.DownBorder() + GetStep() >= FieldHeight;
}
// движение к цели
@Override
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.RightBorder() - FieldWidth;
var diffY = objParams.DownBorder() - FieldHeight;
if (diffX >= 0)
{
MoveDown();
}
else if (diffY >= 0)
{
MoveRight();
}
else if (Math.abs(diffX) > Math.abs(diffY))
{
MoveRight();
}
else
{
MoveDown();
}
}
}

View File

@ -0,0 +1,53 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy {
@Override
protected boolean IsTargetDestination()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2;
}
// движение к цели
@Override
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,33 @@
package MovementStrategy;
import Drawings.DrawingAirplan;
public class MoveableAirplan implements IMoveableObject {
private DrawingAirplan _drawingAirplan = null;
public MoveableAirplan(DrawingAirplan drawingAirplan)
{
_drawingAirplan = drawingAirplan;
}
public ObjectParameters GetObjectPosition()
{
if (_drawingAirplan == null || _drawingAirplan.entityAirplan == null)
{
return null;
}
return new ObjectParameters(_drawingAirplan.GetPosX(), _drawingAirplan.GetPosY(), _drawingAirplan.GetWidth(), _drawingAirplan.GetHeight());
}
public int GetStep() { return (int)_drawingAirplan.entityAirplan.Step; }
@Override
public boolean CheckCanMove(Direction direction) {
return _drawingAirplan.CanMove(direction);
}
@Override
public void MoveObject(Direction direction) {
_drawingAirplan.MoveTransport(direction);
}
}

View File

@ -0,0 +1,25 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
public int LeftBorder() { return _x; }
public int TopBorder() { return _y; }
public int RightBorder() { return _x + _width; }
public int DownBorder() { return _y + _height; }
public int ObjectMiddleHorizontal () { return _x + _width / 2; }
public int ObjectMiddleVertical () { return _y + _height / 2; }
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
}
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public enum StrategyStatus {
NotInit,
InProgress,
Finish
}