PIbd-21_MasenkinMS_LabWork01

This commit is contained in:
parent fc7a6a81e7
commit 5da451e1b5
13 changed files with 523 additions and 1 deletions

View File

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="AccordionBus.AccordionBusForm">
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="b336e" binding="createPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="7b96e" class="javax.swing.JButton" binding="buttonCreateAccordionBus">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать автобус-гармошку"/>
</properties>
</component>
</children>
</grid>
<vspacer id="b9759">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="5a846" binding="MovePanel" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="6" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="67e80" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="AccordionBus/img/Right.png"/>
<text value=""/>
</properties>
</component>
<component id="a51a7" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="AccordionBus/img/Down.png"/>
<text value=""/>
</properties>
</component>
<component id="32158" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="AccordionBus/img/Left.png"/>
<text value=""/>
</properties>
</component>
<component id="3b3f5" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="AccordionBus/img/Up.png"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,80 @@
package AccordionBus;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
public class AccordionBusForm {
private DrawingAccordionBus drawingAccordionBus;
private JPanel pictureBox;
public JPanel getPictureBox() {
return pictureBox;
}
private JPanel createPanel;
private JButton buttonCreateAccordionBus;
private JButton buttonCreate;
private JButton buttonLeft;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonUp;
private JPanel MovePanel;
public AccordionBusForm() {
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
buttonCreateAccordionBus.addActionListener(e -> {
Random random = new Random();
drawingAccordionBus = new DrawingAccordionBus();
drawingAccordionBus.Init(random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(),
random.nextBoolean(),
pictureBox.getWidth(),
pictureBox.getHeight());
drawingAccordionBus.SetNumDoors(random.nextInt(3, 6));
drawingAccordionBus.SetPosition(random.nextInt(10, 100),
random.nextInt(10, 100));
Draw();
});
ActionListener buttonMoveClickedListener = e -> {
String name = ((JButton)e.getSource()).getName();
switch (name) {
case "buttonUp" -> {
drawingAccordionBus.MoveTransport(DirectionType.Up);
}
case "buttonDown" -> {
drawingAccordionBus.MoveTransport(DirectionType.Down);
}
case "buttonLeft" -> {
drawingAccordionBus.MoveTransport(DirectionType.Left);
}
case "buttonRight" -> {
drawingAccordionBus.MoveTransport(DirectionType.Right);
}
}
Draw();
};
buttonUp.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonRight.addActionListener(buttonMoveClickedListener);
}
public void Draw() {
if (drawingAccordionBus.AccordionBus == null) {
return;
}
Graphics g = pictureBox.getGraphics();
pictureBox.paint(g);
drawingAccordionBus.DrawTransport(g);
}
}

View File

@ -0,0 +1,13 @@
package AccordionBus;
// Направление перемещения
public enum DirectionType {
// Вверх
Up,
// Вниз
Down,
// Влево
Left,
// Вправо
Right,
}

View File

@ -0,0 +1,199 @@
package AccordionBus;
import java.awt.*;
import java.util.Random;
// Класс, отвечающий за прорисовку объекта
public class DrawingAccordionBus {
// Класс-сущность
public EntityAccordionBus AccordionBus;
// Прорисовка дверей
public DrawingDoors drawingDoors;
// Ширина окна
private int _pictureWidth;
// Высота окна
private int _pictureHeight;
// Левая координата прорисовки автобуса
private int _startPosX;
// Верхняя координата прорисовки автобуса
private int _startPosY;
// Ширина прорисовки автобуса
private int _busWidth = 100;
// Высота прорисовки автобуса
private final int _busHeight = 50;
// Инициализация свойста
public boolean Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean additionalBody, boolean additionalDoor, int width, int height) {
// Проверка на вместимость объекта в размеры картинки
if ((_busWidth >= width) || (_busHeight >= height)) {
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (additionalBody) {
_busWidth = 215;
}
AccordionBus = new EntityAccordionBus();
AccordionBus.Init(speed, weight,
bodyColor, additionalColor,
additionalBody, additionalDoor);
drawingDoors = new DrawingDoors();
return true;
}
// Количество дополнительных дверей
public void SetNumDoors(int numDoors) {
drawingDoors.setNumDoors(numDoors);
}
// Установка позиции
public void SetPosition(int x, int y) {
if (x < 0 || y < 0) {
_startPosX = 0;
_startPosY = 0;
}
else if ((x + _busWidth > _pictureWidth) || (y + _busHeight > _pictureHeight)) {
_startPosX = _pictureWidth - _busWidth;
_startPosY = _pictureHeight - _busHeight;
}
else {
_startPosX = x;
_startPosY = y;
}
}
// Изменение направления движения
public void MoveTransport(DirectionType direction) {
if (AccordionBus == null) {
return;
}
switch (direction) {
case Left:
if (_startPosX - AccordionBus.Step > 0) {
_startPosX -= (int)AccordionBus.Step;
}
break;
case Up:
if (_startPosY - AccordionBus.Step > 0) {
_startPosY -= (int)AccordionBus.Step;
}
break;
case Right:
if (_startPosX + AccordionBus.Step < _pictureWidth - _busWidth) {
_startPosX += (int)AccordionBus.Step;
}
break;
case Down:
if (_startPosY + AccordionBus.Step < _pictureHeight - _busHeight) {
_startPosY += (int)AccordionBus.Step;
}
break;
}
}
// Прорисовка объекта
public void DrawTransport(Graphics g) {
if (AccordionBus == null) {
return;
}
Graphics2D g2d = (Graphics2D)g;
Color bodyColor = AccordionBus.getBodyColor();
Color additionalColor = AccordionBus.getAdditionalColor();
Color blackColor = Color.BLACK;
Color whiteColor = Color.WHITE;
// Граница главного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(3));
g2d.drawRect(_startPosX, _startPosY, 100, 40);
// Кузов главного отсека
g2d.setColor(bodyColor);
g2d.fillRect(_startPosX + 1, _startPosY + 1, 99, 39);
// Окна главного отсека
g2d.setColor(Color.CYAN);
g2d.fillRect(_startPosX, _startPosY + 5, 100, 15);
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(_startPosX, _startPosY + 5, _startPosX + 100, _startPosY + 5);
g2d.drawLine(_startPosX, _startPosY + 20, _startPosX + 100, _startPosY + 20);
// Дверь главного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 10, _startPosY + 10, 15, 30);
g2d.setColor(whiteColor);
g2d.fillRect(_startPosX + 11, _startPosY + 11, 14, 29);
// Дополнительный отсек
if (AccordionBus.isAdditionalBody()) {
// Граница дополнительного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(3));
g2d.drawRect(_startPosX + 115, _startPosY, 100, 40);
// Кузов дополнительного отсека
g2d.setColor(bodyColor);
g2d.fillRect(_startPosX + 116, _startPosY + 1, 99, 39);
// Окна главного отсека
g2d.setColor(Color.CYAN);
g2d.fillRect(_startPosX + 115, _startPosY + 5, 100, 15);
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(_startPosX + 115, _startPosY + 5, _startPosX + 215, _startPosY + 5);
g2d.drawLine(_startPosX + 115, _startPosY + 20, _startPosX + 215, _startPosY + 20);
// Дверь дополнительного отсека
if (AccordionBus.isAdditionalDoor()) {
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 175, _startPosY + 10, 15, 30);
g2d.setColor(whiteColor);
g2d.fillRect(_startPosX + 176, _startPosY + 11, 14, 29);
}
// Гармошка
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(2));
for (int i = 0; i < 5; i++) {
g2d.drawLine(_startPosX + 100 + i * 3, _startPosY, _startPosX + 100 + i * 3, _startPosY + 40);
}
}
// Дополнительные двери
drawingDoors.DrawDoors(g2d, AccordionBus, additionalColor, _startPosX, _startPosY);
// Колеса главного отсека
g2d.setColor(blackColor);
g2d.setStroke(new BasicStroke(3));
g2d.drawOval(_startPosX + 15, _startPosY + 33, 15, 15);
g2d.drawOval(_startPosX + 70, _startPosY + 33, 15, 15);
g2d.setColor(whiteColor);
g2d.fillOval(_startPosX + 15, _startPosY + 33, 15, 15);
g2d.fillOval(_startPosX + 70, _startPosY + 33, 15, 15);
// Колеса дополнительного отсека
if (AccordionBus.isAdditionalBody()) {
g2d.setColor(blackColor);
g2d.drawOval(_startPosX + 130, _startPosY + 33, 15, 15);
g2d.drawOval(_startPosX + 185, _startPosY + 33, 15, 15);
g2d.setColor(whiteColor);
g2d.fillOval(_startPosX + 131, _startPosY + 33, 15, 15);
g2d.fillOval(_startPosX + 186, _startPosY + 33, 15, 15);
}
}
}

View File

@ -0,0 +1,42 @@
package AccordionBus;
import java.awt.*;
public class DrawingDoors {
private NumDoors numDoors;
public void setNumDoors(int num) {
for (NumDoors temp : numDoors.values()) {
if (temp.getNumDoors() == num) {
numDoors = temp;
return;
}
}
}
public void DrawDoors(Graphics2D g2d, EntityAccordionBus AccordionBus, Color additionalColor, int _startPosX, int _startPosY) {
g2d.setColor(additionalColor);
int value = numDoors.getNumDoors();
if (value >= 3) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 40, _startPosY + 10, 15, 30);
g2d.setColor(Color.WHITE);
g2d.fillRect(_startPosX + 41, _startPosY + 11, 14, 29);
}
if (value >= 4) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 75, _startPosY + 10, 15, 30);
g2d.setColor(Color.WHITE);
g2d.fillRect(_startPosX + 76, _startPosY + 11, 14, 29);
}
if (value >= 5 && AccordionBus.isAdditionalBody()) {
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
g2d.drawRect(_startPosX + 145, _startPosY + 10, 15, 30);
g2d.setColor(Color.WHITE);
g2d.fillRect(_startPosX + 146, _startPosY + 11, 14, 29);
}
}
}

View File

@ -0,0 +1,55 @@
package AccordionBus;
import java.awt.*;
public class EntityAccordionBus {
// Скорость
private int Speed;
public int getSpeed() { return Speed; }
// Вес
private float Weight;
public float getWeight() {
return Weight;
}
// Основной цвет
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
// Дополнительный цвет
private Color AdditionalColor;;
public Color getAdditionalColor() {
return AdditionalColor;
}
// Дополнительный отсек
private boolean AdditionalBody;
public boolean isAdditionalBody() {
return AdditionalBody;
}
// Дополнительная дверь
private boolean AdditionalDoor;
public boolean isAdditionalDoor() {
return AdditionalDoor;
}
// Шаг перемещения автобуса
public double Step;
// Инициализация полей
public void Init(int speed, float weight,
Color bodyColor, Color additionalColor,
boolean additionalBody, boolean additionalDoor) {
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
AdditionalBody = additionalBody;
AdditionalDoor = additionalDoor;
Step = (double)Speed * 100 / Weight;
}
}

View File

@ -0,0 +1,21 @@
package AccordionBus;
import javax.swing.*;
import java.awt.*;
public class FrameAccordionBus extends JFrame {
private AccordionBusForm accordionBusForm;
public FrameAccordionBus() {
super();
setTitle("AccordionBus");
setDefaultCloseOperation(EXIT_ON_CLOSE);
accordionBusForm = new AccordionBusForm();
setContentPane(accordionBusForm.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setPreferredSize(new Dimension(900, 500));
setLocation(500, 500);
pack();
setVisible(true);
}
}

View File

@ -2,6 +2,6 @@ package AccordionBus;
public class Main {
public static void main(String[] args) {
new FrameAccordionBus();
}
}

View File

@ -0,0 +1,22 @@
package AccordionBus;
// Количество дополнительных дверей
public enum NumDoors {
// 3 двери
Three(3),
// 4 двери
Four(4),
// 5 дверей
Five(5);
// Количество дверей
private final int Value;
public int getNumDoors() { return Value; }
// Конструктор
NumDoors(int value) {
Value = value;
}
}

BIN
AccordionBus/img/Down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
AccordionBus/img/Left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

BIN
AccordionBus/img/Right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
AccordionBus/img/Up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB