Compare commits

..

3 Commits

12 changed files with 481 additions and 0 deletions

6
Direction.java Normal file
View File

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

65
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;
public 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);
}
}

160
DrawningTracktor.java Normal file
View File

@ -0,0 +1,160 @@
import java.awt.*;
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
public class DrawningTracktor {
private EntityTracktor Tracktor; // Класс-сущность
public EntityTracktor getTracktor(){
return 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
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;
}
}

107
FormTracktor.form Normal file
View File

@ -0,0 +1,107 @@
<?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>
<component id="eb389" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" 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>
<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>
<component id="83511" class="javax.swing.JLabel" binding="speedLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость:"/>
</properties>
</component>
<component id="49134" class="javax.swing.JLabel" binding="weightLabel">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес:"/>
</properties>
</component>
<component id="15fa4" class="javax.swing.JLabel" binding="colorLabel">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Цвет:"/>
</properties>
</component>
<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/arrowLeft.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/arrowDown.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/arrowRight.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/arrowUp.png"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</form>

98
FormTracktor.java Normal file
View File

@ -0,0 +1,98 @@
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()
);
speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed());
weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight());
colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor()));
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);
}
}
}

9
Program.java Normal file
View File

@ -0,0 +1,9 @@
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
Resources/arrowDown.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
Resources/arrowLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
Resources/arrowRight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
Resources/arrowUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

5
RollersCount.java Normal file
View File

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