LabWork01

This commit is contained in:
BoiledMilk123 2024-05-06 22:29:21 +04:00
parent 2289b95d93
commit 3b693a35eb
14 changed files with 780 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_X" default="true" project-jdk-name="openjdk-22" 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$/.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 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>

View File

@ -0,0 +1,10 @@
package ProjectElectricLocomotive;
import ProjectElectricLocomotive.src.*;
public class Main {
public static void main(String[] args)
{
new FormElectricLocomotive();
}
}

View File

@ -0,0 +1,58 @@
package ProjectElectricLocomotive.src;
import java.awt.*;
public class AdditionalClass {
private AdditionalType _additionalType;
public AdditionalType getAdditionalType()
{
return _additionalType;
}
public void setAdditonalType(int value)
{
switch (value)
{
case 1:
_additionalType = AdditionalType.ONE;
break;
case 2:
_additionalType = AdditionalType.TWO;
break;
case 3:
_additionalType = AdditionalType.THREE;
break;
case 4:
_additionalType = AdditionalType.FOURTH;
break;
default:
_additionalType = AdditionalType.ONE;
break;
}
}
public void drawingWheels(Graphics g, Double startPosX, Double startPosY, Color wheelsColor)
{
Graphics2D g2d = (Graphics2D) g;
g.setColor(wheelsColor);
switch (_additionalType)
{
case FOURTH:
g.fillOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 10, 10);
g.drawOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 8, 8);
case THREE:
g.fillOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 10, 10);
g.drawOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 8, 8);
case TWO:
g.fillOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 10, 10);
g.drawOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 8, 8);
case ONE:
g.fillOval(startPosX.intValue() + 9, startPosY.intValue() + 37, 10, 10);
g.drawOval(startPosX.intValue() + 9, startPosY.intValue() + 37, 8, 8);
break;
}
}
}

View File

@ -0,0 +1,7 @@
package ProjectElectricLocomotive.src;
public enum AdditionalType {
ONE,
TWO,
THREE,
FOURTH;
}

View File

@ -0,0 +1,11 @@
package ProjectElectricLocomotive.src;
public enum DirectionType {
Up,
Down,
Left,
Right,
}

View File

@ -0,0 +1,233 @@
package ProjectElectricLocomotive.src;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class DrawningElectricLocomotive {
public EntityElectricLocomotive EntityElectricLocomotive;
private Double pictureWidth;
private Double pictureHeight;
private Double startPosX;
private Double startPosY;
public AdditionalClass _additionalClass;
private final int drawningElectricLocomotiveWidth = 83;
private final int drawningElectricLocomotiveHeight = 45;
public void init(int speed, double weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement, int wheelsCount) {
EntityElectricLocomotive = new EntityElectricLocomotive();
_additionalClass = new AdditionalClass();
_additionalClass.setAdditonalType(wheelsCount);
EntityElectricLocomotive.init(speed, weight, bodyColor, additionalColor, electricHorns, batteryPlacement);
pictureWidth = null;
pictureHeight = null;
startPosX = null;
startPosY = null;
}
public boolean setPictureSize(int width, int height) {
if (drawningElectricLocomotiveHeight > height || drawningElectricLocomotiveWidth > width) {
return false;
}
pictureWidth = (double)width;
pictureHeight = (double)height;
if (startPosY != null && startPosY + drawningElectricLocomotiveHeight < pictureHeight) {
startPosY = pictureHeight - drawningElectricLocomotiveHeight;
}
if (startPosX != null && startPosX + drawningElectricLocomotiveWidth < pictureWidth) {
startPosX = pictureWidth - drawningElectricLocomotiveWidth;
}
return true;
}
public void setPosition(int x, int y) {
if (pictureHeight == null || pictureWidth == null) {
return;
}
startPosX = (double)x;
startPosY = (double)y;
if (drawningElectricLocomotiveHeight + y > pictureHeight) {
startPosY = pictureHeight - drawningElectricLocomotiveHeight;
}
if (drawningElectricLocomotiveWidth + x > pictureWidth) {
startPosX = pictureWidth - drawningElectricLocomotiveWidth;
}
if (x < 0) {
startPosX = (double)0;
}
if (y < 0) {
startPosY = (double)0;
}
}
public boolean moveTransport(DirectionType direction) {
if (EntityElectricLocomotive == null || startPosX == null || startPosY == null) {
return false;
}
switch (direction) {
case Left:
if (startPosX - EntityElectricLocomotive.getStep() > 0) {
startPosX -= EntityElectricLocomotive.getStep();
}
return true;
case Up:
if (startPosY - EntityElectricLocomotive.getStep() > 0) {
startPosY -= EntityElectricLocomotive.getStep();
}
return true;
case Right:
if (startPosX + drawningElectricLocomotiveWidth + EntityElectricLocomotive.getStep() < pictureWidth) {
startPosX += EntityElectricLocomotive.getStep();
}
return true;
case Down:
if (startPosY + drawningElectricLocomotiveHeight + EntityElectricLocomotive.getStep() < pictureHeight) {
startPosY += EntityElectricLocomotive.getStep();
}
return true;
default:
return false;
}
}
public Image getImage() {
// Создаем новое изображение с заданными размерами
BufferedImage image = new BufferedImage(pictureWidth.intValue(), pictureHeight.intValue(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
// Вызываем метод прорисовки нашего объекта DrawningElectricLocomotive
drawTransport(g2d);
_additionalClass.drawingWheels(g2d, startPosX, startPosY, EntityElectricLocomotive.getAdditionalColor());
// Освобождаем ресурсы графического контекста
g2d.dispose();
return image;
}
public void drawTransport(Graphics g) {
if (EntityElectricLocomotive == null || startPosX == null || startPosY == null) {
return;
}
// Создание перьев и кистей для прорисовки электровоза
Color blackColor = Color.BLACK;
Color deepSkyBlueColor = new Color(0, 191, 255);
Color yellowColor = Color.YELLOW;
Graphics2D g2d = (Graphics2D) g;
// Инициализация опорных точек для прорисовки корпуса
int pointStartX = startPosX.intValue() + 75;
int pointStartY = startPosY.intValue() + 20;
Point pointStart = new Point(pointStartX, pointStartY);
Point point1 = new Point(pointStartX, pointStartY - 15);
Point point2 = new Point(startPosX.intValue() + 10, pointStartY - 15);
Point pointFinish = new Point(startPosX.intValue() + 5, pointStartY);
// Инициализация опорных точек для прорисовки первой "юбки"
Point point3 = new Point(startPosX.intValue() + 7, startPosY.intValue() + 37);
Point point4 = new Point(startPosX.intValue(), startPosY.intValue() + 43);
Point point5 = new Point(startPosX.intValue() + 7, startPosY.intValue() + 43);
// Инициализация опорных точек для прорисовки второй "юбки"
Point point6 = new Point(startPosX.intValue() + 69, startPosY.intValue() + 37);
Point point7 = new Point(startPosX.intValue() + 82, startPosY.intValue() + 43);
Point point8 = new Point(startPosX.intValue() + 69, startPosY.intValue() + 43);
// Совокупность точек полигона уголка первой "юбки"
Point[] firstTrianglePoints = { point3, point4, point5 };
// Совокупность точек полигона уголка второй "юбки"
Point[] secondTrianglePoints = { point6, point7, point8 };
// Прорисовка уголков "юбок"
g.setColor(blackColor);
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.intValue() + 5, startPosY.intValue() + 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.intValue() + 12, startPosY.intValue() + 9, 8, 8);
g.drawRect(startPosX.intValue() + 63, startPosY.intValue() + 9, 8, 8);
// Прорисовка двух передних колёс
//g.fillOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 10, 10);
//g.setColor(blackColor);
//g.drawOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 8, 8);
// Прорисовка двух задних колёс
//g.setColor(Color.YELLOW);
//g.fillOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 10, 10);
//g.fillOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 10, 10);
g.setColor(blackColor);
//g.drawOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 8, 8);
//g.drawOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 8, 8);
// Прорисовка заднего "шлюза"
g.fillRect(startPosX.intValue() + 75, startPosY.intValue() + 9, 6, 27);
if (EntityElectricLocomotive.isElectricHorns()) {
// Инициализация опорных точек для прорисовки "рогов"
Point pointHorns1 = new Point(startPosX.intValue() + 13, startPosY.intValue() + 5);
Point pointHorns2 = new Point(startPosX.intValue() + 16, startPosY.intValue() + 2);
Point pointHorns3 = new Point(startPosX.intValue() + 11, startPosY.intValue());
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.isBatteryPlacement()) {
// Инициализация опорных точек для прорисовки молнии на хранилище батарей
Point pointLightning1 = new Point(startPosX.intValue() + 39, startPosY.intValue() + 37);
Point pointLightning2 = new Point(startPosX.intValue() + 37, startPosY.intValue() + 39);
Point pointLightning3 = new Point(startPosX.intValue() + 39, startPosY.intValue() + 40);
Point pointLightning4 = new Point(startPosX.intValue() + 37, startPosY.intValue() + 41);
g.setColor(EntityElectricLocomotive.getAdditionalColor());
// Прорисовка "хранилища батарей" электровоза
g.fillRect(startPosX.intValue() + 36, startPosY.intValue() + 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);
g.setColor(blackColor);
}
}
}

View File

@ -0,0 +1,117 @@
package ProjectElectricLocomotive.src;
import java.awt.Color;
/**
* Класс-сущность "Тепловоз"
*/
public class EntityElectricLocomotive {
/**
* Скорость
*/
private int speed;
private int wheelsCount;
/**
* Вес
*/
private double weight;
/**
* Основной цвет
*/
private Color bodyColor;
/**
* Дополнительный цвет (для дополнительных элементов)
*/
private Color additionalColor;
/**
* Признак (опция) наличия "корпус"
*/
private boolean electricHorns;
/**
* Признак (опция) наличия места под батарею
*/
private boolean batteryPlacement;
/**
* Инициализация полей объекта-класса тепловоза
*
* @param speed Скорость
* @param weight Вес
* @param bodyColor Основной цвет
* @param additionalColor Дополнительный цвет
* @param electricHorns Наличие "корпуса"
* @param batteryPlacement Наличие места под батарею
*/
public void init(int speed, double weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement) {
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
this.additionalColor = additionalColor;
this.electricHorns = electricHorns;
this.batteryPlacement = batteryPlacement;
}
/**
* Получение шага перемещения объекта
*
* @return Шаг перемещения
*/
public double getStep() {
return speed * 100 / weight;
}
// Геттеры и сеттеры для приватных полей
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Color getBodyColor() {
return bodyColor;
}
public void setBodyColor(Color bodyColor) {
this.bodyColor = bodyColor;
}
public Color getAdditionalColor() {
return additionalColor;
}
public void setAdditionalColor(Color additionalColor) {
this.additionalColor = additionalColor;
}
public boolean isElectricHorns() {
return electricHorns;
}
public void setElectricHorns(boolean electricHorns) {
this.electricHorns = electricHorns;
}
public boolean isBatteryPlacement() {
return batteryPlacement;
}
public void setBatteryPlacement(boolean batteryPlacement) {
this.batteryPlacement = batteryPlacement;
}
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectElectricLocomotive.src.FormElectricLocomotive">
<grid id="27dc6" 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>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="a5a06">
<constraints>
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="a8703" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Up"/>
</properties>
</component>
<component id="4aff6" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Right"/>
</properties>
</component>
<component id="861ee" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Down"/>
</properties>
</component>
<component id="4d9c3" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-3561946"/>
<enabled value="true"/>
<hideActionText value="true"/>
<text value="Left"/>
<verticalAlignment value="0"/>
</properties>
</component>
<component id="e4bb7" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="2" column="0" row-span="2" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Create"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -0,0 +1,124 @@
package ProjectElectricLocomotive.src;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormElectricLocomotive extends JFrame {
private DrawningElectricLocomotive _drawningElectricLocomotive;
private JPanel panel;
private JButton buttonCreate;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
private JLabel pictureBoxElectricLocomotive;
public FormElectricLocomotive() {
initComponents();
}
private void initComponents() {
setTitle("Electric Locomotive");
setVisible(true);
setSize(520, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
buttonCreate = new JButton("Create");
buttonUp = new JButton("Up");
buttonDown = new JButton("Down");
buttonLeft = new JButton("Left");
buttonRight = new JButton("Right");
pictureBoxElectricLocomotive = new JLabel();
panel.setLayout(new FlowLayout());
panel.add(buttonCreate);
panel.add(buttonUp);
panel.add(buttonDown);
panel.add(buttonLeft);
panel.add(buttonRight);
add(panel, BorderLayout.NORTH);
add(pictureBoxElectricLocomotive, BorderLayout.CENTER);
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createElectricLocomotive();
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveTransport(DirectionType.Up);
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveTransport(DirectionType.Down);
}
});
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveTransport(DirectionType.Left);
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveTransport(DirectionType.Right);
}
});
}
private void createElectricLocomotive() {
Random random = new Random();
_drawningElectricLocomotive = new DrawningElectricLocomotive();
_drawningElectricLocomotive.init(
random.nextInt(200) + 100,
random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(),
random.nextBoolean(),
random.nextInt(0,5)
);
_drawningElectricLocomotive.setPictureSize(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight());
_drawningElectricLocomotive.setPosition(random.nextInt(pictureBoxElectricLocomotive.getWidth()), random.nextInt(pictureBoxElectricLocomotive.getHeight()));
draw();
}
private void moveTransport(DirectionType direction) {
if (_drawningElectricLocomotive != null) {
if (_drawningElectricLocomotive.moveTransport(direction)) {
draw();
}
}
}
private void draw() {
if (_drawningElectricLocomotive != null) {
Image image = new ImageIcon(_drawningElectricLocomotive.getImage()).getImage();
Image scaledImage = image.getScaledInstance(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight(), Image.SCALE_SMOOTH);
pictureBoxElectricLocomotive.setIcon(new ImageIcon(scaledImage));
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
FormElectricLocomotive form = new FormElectricLocomotive();
form.setVisible(true);
});
}
}