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/DrawingArtillery.java b/DrawingArtillery.java new file mode 100644 index 0000000..f765894 --- /dev/null +++ b/DrawingArtillery.java @@ -0,0 +1,113 @@ +import java.awt.*; + +public class DrawingArtillery { + private EntityArtillery artillery; + private DrawingRollers drawingRollers; + private float _startPosX; + private float _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _artilleryWidth = 80; + private final int _artilleryHeight = 50; + + public EntityArtillery getArtillery() { + return artillery; + } + + public void Init(int speed, float weight, Color bodyColor, int rollersCount) { + artillery = new EntityArtillery(); + artillery.Init(speed, weight, bodyColor); + drawingRollers = new DrawingRollers(); + drawingRollers.Init(rollersCount, bodyColor); + } + + public void SetPosition(int x, int y, int width, int height) { + if (x < 0 || x + _artilleryWidth >= width) + { + return; + } + + if (y < 0 || y + _artilleryHeight >= height) + { + 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 + _artilleryWidth + artillery.getStep() < _pictureWidth) + { + _startPosX += artillery.getStep(); + } + break; + + case Left: + if (_startPosX - artillery.getStep() >= 0) + { + _startPosX -= artillery.getStep(); + } + break; + + case Up: + if (_startPosY - artillery.getStep() >= 0) + { + _startPosY -= artillery.getStep(); + } + break; + + case Down: + if (_startPosY + _artilleryHeight + artillery.getStep() < _pictureHeight) + { + _startPosY += artillery.getStep(); + } + break; + } + } + + public void drawTransport(Graphics2D g) { + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) + { + return; + } + g.setColor(artillery.getBodyColor() != null ? artillery.getBodyColor() : Color.BLACK); + g.fillRect((int) (_startPosX + _artilleryWidth / 8 * 2), (int) _startPosY, _artilleryWidth / 8 * 4, _artilleryWidth / 5); + g.fillRect((int) _startPosX, (int) (_startPosY + _artilleryHeight / 5), _artilleryWidth, _artilleryHeight / 3); + + + g.setColor(Color.GRAY); + g.fillOval((int) _startPosX, (int) (_startPosY + _artilleryHeight * 2 / 5), _artilleryWidth, _artilleryHeight * 2 / 5); + drawingRollers.draw(g, (int) _startPosX, (int) _startPosY, _artilleryWidth, _artilleryHeight); + } + + public void changeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _artilleryWidth || _pictureHeight <= _artilleryHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _artilleryWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _artilleryWidth; + } + if (_startPosY + _artilleryHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _artilleryHeight; + } + } +} diff --git a/DrawingRollers.java b/DrawingRollers.java new file mode 100644 index 0000000..0b2b1fc --- /dev/null +++ b/DrawingRollers.java @@ -0,0 +1,39 @@ +import java.awt.*; + +public class DrawingRollers { + private RollersCount rollersCount; + private Color color; + + public void Init(int rollersCount, Color bodyColor) { + setRollersCount(rollersCount); + color = bodyColor; + } + + public void setRollersCount(int num) { + if (num <= 4) { + rollersCount = RollersCount.Four; + } else if (num >= 6) { + rollersCount = RollersCount.Six; + } else { + rollersCount = RollersCount.Five; + } + } + + public void draw(Graphics2D g, int x, int y, int artilleryWidth, int artilleryHeight) { + g.setColor(color != null ? color : Color.BLACK); + g.fillOval(x + artilleryWidth / 20, y + artilleryHeight * 7 / 15, artilleryWidth * 4 / 20, artilleryHeight * 10 / 32); + g.fillOval(x + artilleryWidth * 15 / 20, y + artilleryHeight * 7 / 15, artilleryWidth * 4 / 20, artilleryHeight * 10 / 32); + switch (rollersCount) { + case Six: { + g.fillOval(x + artilleryWidth * 8 / 20, y + artilleryHeight * 10 / 16, artilleryWidth * 2 / 20, artilleryHeight * 6 / 32); + } + case Five: { + g.fillOval(x + artilleryWidth * 10 / 20, y + artilleryHeight * 10 / 16, artilleryWidth * 2 / 20, artilleryHeight * 6 / 32); + } + case Four: { + g.fillOval(x + artilleryWidth * 5 / 20, y + artilleryHeight * 9 / 16, artilleryWidth * 3 / 20, artilleryHeight * 8 / 32); + g.fillOval(x + artilleryWidth * 12 / 20, y + artilleryHeight * 9 / 16, artilleryWidth * 3 / 20, artilleryHeight * 8 / 32); + } + } + } +} diff --git a/EntityArtillery.java b/EntityArtillery.java new file mode 100644 index 0000000..4367dae --- /dev/null +++ b/EntityArtillery.java @@ -0,0 +1,31 @@ +import java.awt.*; +import java.util.Random; + +public class EntityArtillery { + private int speed; + private float weight; + private Color bodyColor; + + public void Init(int speed, float weight, Color bodyColor) { + Random rnd = new Random(); + this.speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; + this.weight = weight <= 0 ? rnd.nextInt(40, 70) : weight; + this.bodyColor = bodyColor; + } + + public int getSpeed() { + return speed; + } + + public float getWeight() { + return weight; + } + + public Color getBodyColor() { + return bodyColor; + } + + public float getStep() { + return speed * 100 / weight; + } +} diff --git a/FormArtillery.form b/FormArtillery.form new file mode 100644 index 0000000..243bd18 --- /dev/null +++ b/FormArtillery.form @@ -0,0 +1,152 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FormArtillery.java b/FormArtillery.java new file mode 100644 index 0000000..8e75a9a --- /dev/null +++ b/FormArtillery.java @@ -0,0 +1,75 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; + +public class FormArtillery extends JFrame { + private JPanel artilleryPane; + private JLabel speedLabel; + private JLabel weightLabel; + private JLabel colorLabel; + private JPanel pictureBox; + private JButton createButton; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + + private DrawingArtillery _artillery; + + public FormArtillery() { + this.setTitle("Artillery"); + this.setContentPane(artilleryPane); + createButton.addActionListener(e -> { + Random rnd = new Random(); + _artillery = new DrawingArtillery(); + _artillery.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(4, 7) + ); + _artillery.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); + speedLabel.setText(String.format("Скорость: %s", _artillery.getArtillery().getSpeed())); + weightLabel.setText(String.format("Вес: %s", _artillery.getArtillery().getWeight())); + colorLabel.setText(String.format("Цвет: %x", _artillery.getArtillery().getBodyColor().getRGB())); + repaint(); + }); + pictureBox.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + if (_artillery != null) _artillery.changeBorders(pictureBox.getWidth(), pictureBox.getHeight()); + repaint(); + } + }); + buttonLeft.addActionListener(e -> { + if (_artillery != null) _artillery.moveTransport(Direction.Left); + repaint(); + }); + buttonRight.addActionListener(e -> { + if (_artillery != null) _artillery.moveTransport(Direction.Right); + repaint(); + }); + buttonUp.addActionListener(e -> { + if (_artillery != null) _artillery.moveTransport(Direction.Up); + repaint(); + }); + buttonDown.addActionListener(e -> { + if (_artillery != null) _artillery.moveTransport(Direction.Down); + repaint(); + }); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + Graphics2D g2d = (Graphics2D) artilleryPane.getGraphics(); + if (_artillery != null) { + _artillery.drawTransport(g2d); + } + } +} diff --git a/Program.java b/Program.java new file mode 100644 index 0000000..36372f9 --- /dev/null +++ b/Program.java @@ -0,0 +1,10 @@ +import javax.swing.*; + +public class Program { + public static void main(String[] args) { + FormArtillery form = new FormArtillery(); + form.setSize(640, 480); + form.setVisible(true); + form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/Resources/ArrowDown.png b/Resources/ArrowDown.png new file mode 100644 index 0000000..35064b4 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..dc1eb1b 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..fc1670f 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..7f1bb55 Binary files /dev/null and b/Resources/ArrowUp.png differ diff --git a/RollersCount.java b/RollersCount.java new file mode 100644 index 0000000..7b9cdbe --- /dev/null +++ b/RollersCount.java @@ -0,0 +1,5 @@ +public enum RollersCount { + Four, + Five, + Six +}