PIbd-21. Kryukov A.I. Lab work 01 hard #1

Closed
SooNooClose wants to merge 1 commits from lab_1 into main
17 changed files with 486 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$/src" 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>

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-21_Kryukov_A_I_Excavator_HARD.iml" filepath="$PROJECT_DIR$/.idea/PIbd-21_Kryukov_A_I_Excavator_HARD.iml" />
</modules>
</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>

9
src/Direction.java Normal file
View File

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

65
src/DrawningRollers.java Normal file
View File

@ -0,0 +1,65 @@
import java.awt.*;
import java.util.Random;
public class DrawningRollers {
private RollersCount rollersCount;
private Color colorRollers;
void setRollersCount(int count){
switch (count) {
case 4 -> rollersCount = RollersCount.Four;
case 5 -> rollersCount = RollersCount.Five;
case 6 -> rollersCount = RollersCount.Six;
default -> rollersCount = RollersCount.Four;
}
}
public void Init(int count, Color colorRollers){
setRollersCount(count);
this.colorRollers = colorRollers;
}
public void DrawRollers(Graphics g, float _startPosX, float _startPosY){
Color penColor = Color.BLACK;
Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers;
// Крупные катки - всегда
g.setColor(mainColor);
g.fillOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
g.setColor(penColor);
g.drawOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22);
g.setColor(mainColor);
g.fillOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
g.setColor(penColor);
g.drawOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22);
// Малые катки - всегда
g.setColor(mainColor);
g.fillOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
g.setColor(penColor);
g.drawOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6);
g.setColor(mainColor);
g.fillOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
g.setColor(penColor);
g.drawOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6);
// Средние катки - не всегда
switch (rollersCount){
case Six:
g.setColor(mainColor);
g.fillOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
g.setColor(penColor);
g.drawOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10);
case Five:
g.setColor(mainColor);
g.fillOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
g.setColor(penColor);
g.drawOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10);
}
// Центры крупных катков
g.setColor(Color.BLACK);
g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6);
g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6);
}
}

157
src/DrawningTracktor.java Normal file
View File

@ -0,0 +1,157 @@
import java.awt.*;
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
public class DrawningTracktor {
private EntityTracktor Tracktor; // Класс-сущность
private float _startPosX; // Левая координата отрисовки трактора
private float _startPosY; // Верхняя кооридната отрисовки трактора
private Integer _pictureWidth = null; // Ширина окна отрисовки
private Integer _pictureHeight = null; // Высота окна отрисовки
private final int _tracktorWidth = 110; // Ширина отрисовки трактора
private final int _tracktorHeight = 87; // Высота отрисовки трактора
private DrawningRollers drawningRollers;
// Инициализация свойств
public void Init(int speed, float weight, Color bodyColor, int countRollers)
{
Tracktor = new EntityTracktor();
Tracktor.Init(speed, weight, bodyColor);
drawningRollers = new DrawningRollers();
drawningRollers.Init(countRollers, bodyColor);
}
// Установка позиции Трактора
public void SetPosition(int x, int y, int width, int height)
{
if (x + _tracktorWidth > width ||
x < 0 ||
y + _tracktorHeight > height ||
y < 0)
{
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
// Изменение направления перемещения
public void MoveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null)
{
return;
}
switch (direction)
{
// вправо
case Right:
if (_startPosX + _tracktorWidth + Tracktor.getStep() < _pictureWidth)
{
_startPosX += Tracktor.getStep();
}
break;
//влево
case Left:
if (_startPosX - Tracktor.getStep() > 0)
{
_startPosX -= Tracktor.getStep();
}
break;
//вверх
case Up:
if (_startPosY - Tracktor.getStep() > 0)
{
_startPosY -= Tracktor.getStep();
}
break;
//вниз
case Down:
if (_startPosY + _tracktorHeight + Tracktor.getStep() < _pictureHeight)
{
_startPosY += Tracktor.getStep();
}
break;
}
}
// Отрисовка Трактора
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
{
return;
}
Color penColor = Color.BLACK;
// корпус
Color br = Tracktor!=null ? Tracktor.getBodyColor() : Color.GRAY ;
g.setColor(br);
g.fillRect((int)_startPosX + 10, (int)_startPosY + 30, 90, 25);
g.setColor(penColor);
g.drawRect((int)_startPosX + 10, (int)_startPosY + 30, 90, 25);
// окно
g.setColor(Color.CYAN);
g.fillRect((int)_startPosX + 65, (int)_startPosY + 1, 30, 29);
g.setColor(penColor);
g.drawRect((int)_startPosX + 65, (int)_startPosY + 1, 30, 29);
// труба
g.setColor(Color.RED);
g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 20);
g.setColor(penColor);
g.drawRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 20);
// гусеница
g.setColor(Color.DARK_GRAY);
g.fillOval((int)_startPosX + 1, (int)_startPosY + 57, 20, 20);
g.setColor(penColor);
g.drawOval((int)_startPosX + 1, (int)_startPosY + 57, 20, 20);
g.setColor(Color.DARK_GRAY);
g.fillOval((int)_startPosX + 1, (int)_startPosY + 65, 20, 20);
g.setColor(penColor);
g.drawOval((int)_startPosX + 1, (int)_startPosY + 65, 20, 20);
g.setColor(Color.DARK_GRAY);
g.fillOval((int)_startPosX + 90, (int)_startPosY + 57, 20, 20);
g.setColor(penColor);
g.drawOval((int)_startPosX + 90, (int)_startPosY + 57, 20, 20);
g.setColor(Color.DARK_GRAY);
g.fillOval((int)_startPosX + 90, (int)_startPosY + 65, 20, 20);
g.setColor(penColor);
g.drawOval((int)_startPosX + 90, (int)_startPosY + 65, 20, 20);
g.setColor(Color.DARK_GRAY);
g.fillRect((int)_startPosX + 10, (int)_startPosY + 57, 90, 30);
g.fillRect((int)_startPosX + 1, (int)_startPosY + 65, 110, 10);
g.setColor(penColor);
g.drawLine((int)_startPosX + 10, (int)_startPosY + 57, (int)_startPosX + 100, (int)_startPosY + 57);
g.drawLine((int)_startPosX + 10, (int)_startPosY + 86, (int)_startPosX + 100, (int)_startPosY + 86);
g.drawLine((int)_startPosX + 1, (int)_startPosY + 65, (int)_startPosX + 1, (int)_startPosY + 75);
g.drawLine((int)_startPosX + 110, (int)_startPosY + 65, (int)_startPosX + 110, (int)_startPosY + 75);
drawningRollers.DrawRollers(g,_startPosX, _startPosY);
}
// Смена границ формы отрисовки
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _tracktorWidth || _pictureHeight <= _tracktorHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _tracktorWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _tracktorWidth;
}
if (_startPosY + _tracktorHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _tracktorHeight;
}
}
}

31
src/EntityTracktor.java Normal file
View File

@ -0,0 +1,31 @@
import java.awt.*;
import java.util.Random;
// Класс-сущность "Трактор"
public class EntityTracktor {
private int Speed;
private float Weight;
private Color BodyColor;
public int getSpeed() {
return Speed;
}
public float getWeight(){
return Weight;
}
public Color getBodyColor(){
return BodyColor;
}
public float getStep(){
return Speed * 100 / Weight;
}
// Инициализация полей объекта-класса Трактора
public void Init(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed;
Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight;
BodyColor = bodyColor;
}
}

83
src/FormTracktor.form Normal file
View File

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormTracktor">
<grid id="27dc6" binding="ContentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="8" 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="661" height="428"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="7a016">
<constraints>
<grid row="1" column="3" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="2fbf8" binding="pictureBox" 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="0" column="0" row-span="1" col-span="8" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="5a9f4" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/left.png"/>
<text value=""/>
</properties>
</component>
<component id="3c09f" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/down'.png"/>
<text value=""/>
</properties>
</component>
<component id="6d42a" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/right.png"/>
<text value=""/>
</properties>
</component>
<component id="82038" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="1" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/up.png"/>
<text value=""/>
</properties>
</component>
<component id="eb389" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
</children>
</grid>
</form>

94
src/FormTracktor.java Normal file
View File

@ -0,0 +1,94 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormTracktor extends JFrame {
private JPanel ContentPanel;
private JButton buttonCreate;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
private JButton buttonLeft;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonUp;
private JPanel pictureBox;
private DrawningTracktor _tracktor;
public FormTracktor(){
setTitle("Трактор");
setContentPane(ContentPanel);
setSize(800, 500);
// Обработка нажатия кнопки "Создать"
buttonCreate.addActionListener(e->{
Random rnd = new Random();
_tracktor = new DrawningTracktor();
_tracktor.Init(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
rnd.nextInt(3,8)
);
_tracktor.SetPosition(
rnd.nextInt(10, 100),
rnd.nextInt(10, 100),
pictureBox.getWidth(), pictureBox.getHeight()
);
repaint();
});
buttonUp.addActionListener(e->{
if (_tracktor != null){
_tracktor.MoveTransport(Direction.Up);
repaint();
}
});
buttonLeft.addActionListener(e->{
if (_tracktor != null){
_tracktor.MoveTransport(Direction.Left);
repaint();
}
});
buttonDown.addActionListener(e->{
if (_tracktor != null){
_tracktor.MoveTransport(Direction.Down);
repaint();
}
});
buttonRight.addActionListener(e->{
if (_tracktor != null){
_tracktor.MoveTransport(Direction.Right);
repaint();
}
});
pictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
if (_tracktor != null){
_tracktor.ChangeBorders(e.getComponent().getWidth(), e.getComponent().getHeight());
repaint();
}
}
});
}
@Override
public void paint(Graphics g){
super.paint(g);
g = pictureBox.getGraphics();
if (_tracktor != null){
_tracktor.DrawTransport(g);
}
}
}

8
src/Program.java Normal file
View File

@ -0,0 +1,8 @@
import javax.swing.*;
public class Program {
public static void main(String[] args){
FormTracktor formTracktor = new FormTracktor();
formTracktor.setVisible(true);
formTracktor.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

BIN
src/Resources/down'.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
src/Resources/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
src/Resources/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
src/Resources/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

5
src/RollersCount.java Normal file
View File

@ -0,0 +1,5 @@
public enum RollersCount {
Four,
Five,
Six
}