diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..a641b80 --- /dev/null +++ b/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} diff --git a/DrawningTracktor.java b/DrawningTracktor.java new file mode 100644 index 0000000..cb25994 --- /dev/null +++ b/DrawningTracktor.java @@ -0,0 +1,192 @@ +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; // Высота отрисовки трактора + + // Инициализация свойств + public void Init(int speed, float weight, Color bodyColor) + { + Tracktor = new EntityTracktor(); + Tracktor.Init(speed, weight, 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); + + // Крупные катки + g.setColor(Color.LIGHT_GRAY); + 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(Color.LIGHT_GRAY); + 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(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10); + g.setColor(penColor); + g.drawOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10); + g.setColor(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 50, (int)_startPosY + 73, 10, 10); + g.setColor(penColor); + g.drawOval((int)_startPosX + 50, (int)_startPosY + 73, 10, 10); + g.setColor(Color.LIGHT_GRAY); + 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.LIGHT_GRAY); + 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(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6); + g.setColor(penColor); + g.drawOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6); + + // Центры крупных катков + g.setColor(Color.BLACK); + g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); + g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); + } + + // Смена границ формы отрисовки + 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; + } + } +} diff --git a/EntityTracktor.java b/EntityTracktor.java new file mode 100644 index 0000000..f6c85e9 --- /dev/null +++ b/EntityTracktor.java @@ -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; + } +} diff --git a/FormTracktor.form b/FormTracktor.form new file mode 100644 index 0000000..d6e43fa --- /dev/null +++ b/FormTracktor.form @@ -0,0 +1,107 @@ + +
diff --git a/FormTracktor.java b/FormTracktor.java new file mode 100644 index 0000000..b0d7973 --- /dev/null +++ b/FormTracktor.java @@ -0,0 +1,82 @@ +import javax.swing.*; +import java.awt.*; +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)) + ); + _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(); + } + }); + } + + @Override + public void paint(Graphics g){ + super.paint(g); + g = pictureBox.getGraphics(); + if (_tracktor != null){ + _tracktor.DrawTransport(g); + } + } +} diff --git a/Program.java b/Program.java new file mode 100644 index 0000000..3f5ddf6 --- /dev/null +++ b/Program.java @@ -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); + } +} diff --git a/Resources/arrowDown.png b/Resources/arrowDown.png new file mode 100644 index 0000000..cbb6971 Binary files /dev/null and b/Resources/arrowDown.png differ diff --git a/Resources/arrowLeft.png b/Resources/arrowLeft.png new file mode 100644 index 0000000..62da809 Binary files /dev/null and b/Resources/arrowLeft.png differ diff --git a/Resources/arrowRight.png b/Resources/arrowRight.png new file mode 100644 index 0000000..1e902ae Binary files /dev/null and b/Resources/arrowRight.png differ diff --git a/Resources/arrowUp.png b/Resources/arrowUp.png new file mode 100644 index 0000000..b4c95a1 Binary files /dev/null and b/Resources/arrowUp.png differ