This commit is contained in:
[USERNAME] 2023-12-02 21:03:27 +04:00
parent 0b8131be6d
commit 0073a12bad
12 changed files with 347 additions and 1 deletions

BIN
Resources/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Resources/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Resources/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
Resources/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -0,0 +1,7 @@
public enum DirectionBulldozer {
Up,
Down,
Left,
Right
}

110
src/DrawningBulldozer.java Normal file
View File

@ -0,0 +1,110 @@
import java.awt.*;
public class DrawningBulldozer {
private EntityBulldozer entityBulldozer;
public EntityBulldozer getEntityBulldozer() {
return entityBulldozer;
}
private DrawningWheels drawningWheels;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private int _bulldozerWidth = 160;
private int _bulldozerHeight = 80;
public boolean Init(int speed, double weight, Color bulldozerColor, Color cabinColor, Color covshColor, boolean hasMoldboardfront, boolean hasRipper, int width, int height, int wheelNumber)
{
if (height < _bulldozerHeight || width < _bulldozerWidth) {
return false;
}
_pictureWidth = width;
_pictureHeight = height;
entityBulldozer = new EntityBulldozer();
entityBulldozer.Init(speed, weight, bulldozerColor, cabinColor, covshColor, hasMoldboardfront, hasRipper);
drawningWheels = new DrawningWheels();
drawningWheels.setWheelNumber(wheelNumber);
return true;
}
public void SetPosition(int x, int y)
{
if (x < 0 || x + _bulldozerWidth > _pictureWidth) { x = 0; }
if (y < 0 || y + _bulldozerHeight > _pictureHeight) { y = 0; }
_startPosX = x;
_startPosY = y;
}
public void MoveTransport(DirectionBulldozer direction) {
if (entityBulldozer == null) return;
switch (direction) {
//влево
case Left:
if (_startPosX - entityBulldozer.getStep() > 0)
{
_startPosX -= (int)entityBulldozer.getStep();
}
break;
//вверх
case Up:
if (_startPosY - entityBulldozer.getStep() > 0)
{
_startPosY -= (int)entityBulldozer.getStep();
}
break;
//вправо
case Right:
if (_startPosX + _bulldozerWidth + entityBulldozer.getStep() < _pictureWidth)
{
_startPosX += (int)entityBulldozer.getStep();
}
break;
//вниз
case Down:
if (_startPosY + _bulldozerHeight + entityBulldozer.getStep() < _pictureHeight)
{
_startPosY += (int)entityBulldozer.getStep();
}
break;
}
}
public void DrawTransport(Graphics2D g2D)
{
if (entityBulldozer == null) {
return;
}
g2D.setStroke(new BasicStroke(3));
g2D.setPaint(entityBulldozer.getBulldozerColor());
g2D.fillRect(_startPosX + 25, _startPosY + 20, 110, 30);
g2D.fillRect(_startPosX+60, _startPosY, 10, 30);
int x = _startPosX + 30; // начальная позиция X
int y = _startPosY; // начальная позиция Y
int width = 110; // ширина прямоугольника
int height = 30; // высота прямоугольника
int radius = 20; // радиус закругления углов
// Рисуем закругленный прямоугольник
g2D.setColor(Color.BLACK);
g2D.drawArc(x - 5, y + 50, radius * 2, radius * 2, 180, 90); // верхний левый угол
g2D.drawLine(x + radius - 5, y + 50, x + width - radius - 5, y + 50); // верхняя горизонталь
g2D.drawArc(x + width - radius * 2 - 5, y + 50, radius * 2, radius * 2, 270, 90); // верхний правый угол
g2D.drawArc(x + width - radius * 2 - 5, y + height - radius * 2 + 60, radius * 2, radius * 2, 0, 90); // нижний правый угол
g2D.drawLine(x + width - radius - 5, y + height + 60, x + radius - 5, y + height + 60); // нижняя горизонталь
g2D.drawArc(x - 5, y + height - radius * 2 + 60, radius * 2, radius * 2, 90, 90); // нижний левый угол
// Кабина
g2D.setPaint(entityBulldozer.getCabinColor());
g2D.fillRect(_startPosX + 105, _startPosY, 30, 20);
//Колёса
drawningWheels.drawWheels(g2D, Color.BLACK, _startPosX, _startPosY);
if (entityBulldozer.getHasMoldboardfront())
{
g2D.setPaint(entityBulldozer.getCabinColor());
int[] xPoints = {_startPosX + 25, _startPosX + 25, _startPosX};
int[] yPoints = {_startPosY + 30, _startPosY + 80, _startPosY + 80};
Polygon triangle = new Polygon(xPoints, yPoints, 3);
g2D.drawPolygon(triangle);
}
if (entityBulldozer.getHasRipper())
{
int[] xPoints2 = {_startPosX + 130, _startPosX + 160, _startPosX + 160};
int[] yPoints2 = {_startPosY + 50, _startPosY + 50, _startPosY + 80};
Polygon triangle2 = new Polygon(xPoints2, yPoints2, 3);
g2D.drawPolygon(triangle2);
}
}
}

73
src/DrawningWheels.java Normal file
View File

@ -0,0 +1,73 @@
import java.awt.*;
public class DrawningWheels {
private WheelNumber wheelNumber;
public void setWheelNumber(int number) {
switch (number) {
case 1:
wheelNumber = WheelNumber.Four;
break;
case 2:
wheelNumber = WheelNumber.Five;
break;
case 3:
wheelNumber = WheelNumber.Six;
break;
default: wheelNumber = WheelNumber.Four;
}
}
public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (wheelNumber) {
case Four:
drawFourWheels(g2D, color, _startPosX, _startPosY);
break;
case Five:
drawFiveWheels(g2D, color, _startPosX, _startPosY);
break;
case Six:
drawSixWheels(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
int wheelRadius = 10;
g2D.drawOval(_startPosX + 30, _startPosY + 60, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 30, _startPosY + 60, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 56, _startPosY + 60, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 56, _startPosY + 60, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 84, _startPosY + 60, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 84, _startPosY + 60, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 110, _startPosY + 60, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 110, _startPosY + 60, wheelRadius * 2, wheelRadius * 2);
}
private void drawFiveWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
int wheelRadius = 10;
g2D.drawOval(_startPosX + 34, _startPosY + 55, wheelRadius, wheelRadius);
g2D.fillOval(_startPosX + 34, _startPosY + 55, wheelRadius, wheelRadius);
g2D.drawOval(_startPosX + 45, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 45, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 69, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 69, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 94, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 94, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 115, _startPosY + 55, wheelRadius, wheelRadius);
g2D.fillOval(_startPosX + 115, _startPosY + 55, wheelRadius, wheelRadius);
}
private void drawSixWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(color);
int wheelRadius = 10;
g2D.drawOval(_startPosX + 34, _startPosY + 54, wheelRadius, wheelRadius);
g2D.fillOval(_startPosX + 34, _startPosY + 54, wheelRadius, wheelRadius);
g2D.drawOval(_startPosX + 35, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 35, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 58, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 58, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 82, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 82, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 105, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.fillOval(_startPosX + 105, _startPosY + 65, wheelRadius * 2, wheelRadius * 2);
g2D.drawOval(_startPosX + 115, _startPosY + 54, wheelRadius, wheelRadius);
g2D.fillOval(_startPosX + 115, _startPosY + 54, wheelRadius, wheelRadius);
}
}

41
src/EntityBulldozer.java Normal file
View File

@ -0,0 +1,41 @@
import java.awt.*;
public class EntityBulldozer {
private int Speed;
public int getSpeed() {
return Speed;
}
private double Weight;
public double getWeight() {
return Weight;
}
private Color BulldozerColor;
public Color getBulldozerColor() {
return BulldozerColor;
}
private Color CabinColor;
public Color getCabinColor() {
return CabinColor;
}
private Color CovshColor;
public Color getCovshColor() {return CovshColor;}
private boolean HasMoldboardfront;
public boolean getHasMoldboardfront() {
return HasMoldboardfront;
}
private boolean HasRipper;
public boolean getHasRipper() {
return HasRipper;
}
public double getStep() {
return (double)Speed * 800 / Weight;
}
public void Init(int speed, double weight, Color bulldozerColor, Color cabinColor, Color covshColor, boolean hasMoldboardfront, boolean hasRipper) {
Speed = speed;
Weight = weight;
BulldozerColor = bulldozerColor;
CabinColor = cabinColor;
CovshColor = covshColor;
HasMoldboardfront = hasMoldboardfront;
HasRipper = hasRipper;
}
}

12
src/FrameBulldozer.java Normal file
View File

@ -0,0 +1,12 @@
import javax.swing.*;
public class FrameBulldozer extends JFrame {
private PictureBoxBulldozer pictureBoxBulldozer;
public FrameBulldozer() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxBulldozer = new PictureBoxBulldozer();
add(pictureBoxBulldozer);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

View File

@ -1,5 +1,5 @@
public class Main {
public static void main(String[] args) {
FrameBulldozer frameBulldozer = new FrameBulldozer();
}
}

View File

@ -0,0 +1,98 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
public class PictureBoxBulldozer extends JPanel {
private DrawningBulldozer drawningBulldozer;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonCreateBulldozer;
public PictureBoxBulldozer() {
setLayout(null);
setBounds(0, 0, 800, 450);
buttonCreateBulldozer = new JButton("Создать");
buttonCreateBulldozer.setFocusable(false);
buttonCreateBulldozer.setBounds(12, 415, 100, 30);
add(buttonCreateBulldozer);
buttonCreateBulldozer.addActionListener(e -> {
Random random = new Random();
drawningBulldozer = new DrawningBulldozer();
drawningBulldozer.Init(random.nextInt(100)+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)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(),
this.getWidth(), this.getHeight(), random.nextInt(3) + 1);
drawningBulldozer.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
repaint();
});
ActionListener buttonMoveListener = e -> {
if (drawningBulldozer == null)
{
return;
}
String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) {
case ("buttonUp"):
drawningBulldozer.MoveTransport(DirectionBulldozer.Up);
break;
case ("buttonDown"):
drawningBulldozer.MoveTransport(DirectionBulldozer.Down);
break;
case ("buttonLeft"):
drawningBulldozer.MoveTransport(DirectionBulldozer.Left);
break;
case ("buttonRight"):
drawningBulldozer.MoveTransport(DirectionBulldozer.Right);
break;
}
repaint();
};
buttonLeft = new JButton();
buttonLeft.setName("buttonLeft");
buttonLeft.setFocusable(false);
buttonLeft.setPreferredSize(new Dimension(30, 30));
buttonLeft.setIcon(new ImageIcon("Resources/left.png"));
buttonLeft.addActionListener(buttonMoveListener);
buttonLeft.setBounds(686, 408, 30, 30);
add(buttonLeft);
buttonRight = new JButton();
buttonRight.setName("buttonRight");
buttonRight.setFocusable(false);
buttonRight.setPreferredSize(new Dimension(30, 30));
buttonRight.setIcon(new ImageIcon("Resources/right.png"));
buttonRight.addActionListener(buttonMoveListener);
buttonRight.setBounds(758, 408, 30, 30);
add(buttonRight);
buttonDown = new JButton();
buttonDown.setName("buttonDown");
buttonDown.setFocusable(false);
buttonDown.setPreferredSize(new Dimension(30, 30));
buttonDown.setIcon(new ImageIcon("Resources/down.png"));
buttonDown.addActionListener(buttonMoveListener);
buttonDown.setBounds(722, 408, 30, 30);
add(buttonDown);
buttonUp = new JButton();
buttonUp.setName("buttonUp");
buttonUp.setFocusable(false);
buttonUp.setPreferredSize(new Dimension(30, 30));
buttonUp.setIcon(new ImageIcon("Resources/up.png"));
buttonUp.addActionListener(buttonMoveListener);
buttonUp.setBounds(722, 372, 30, 30);
add(buttonUp);
setPreferredSize(new Dimension(800, 450));
}
@Override
protected void paintComponent(Graphics g) {
if (drawningBulldozer == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawningBulldozer.DrawTransport(g2d);
}
}

5
src/WheelNumber.java Normal file
View File

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