diff --git a/src/main/java/Direction.java b/src/main/java/Direction.java new file mode 100644 index 0000000..a641b80 --- /dev/null +++ b/src/main/java/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} diff --git a/src/main/java/DrawingArmoredCar.java b/src/main/java/DrawingArmoredCar.java new file mode 100644 index 0000000..b4fa674 --- /dev/null +++ b/src/main/java/DrawingArmoredCar.java @@ -0,0 +1,119 @@ +import java.awt.*; +import java.util.Random; + +public class DrawingArmoredCar { + private EntityArmoredCar armoredCar; + + private float startPosX; + + private float startPosY; + + private int pictureWidth; + + private int pictureHeight; + + private static final int carWidth = 80; + + private static final int carHeight = 50; + + private DrawingCaterpillar drawingCaterpillar; + + public EntityArmoredCar getArmoredCar() { + return armoredCar; + } + + public void Init(int speed, float weight, Color bodyColor) { + this.armoredCar = new EntityArmoredCar(); + this.armoredCar.init(speed, weight, bodyColor); + Random r = new Random(); + this.drawingCaterpillar = new DrawingCaterpillar(); + this.drawingCaterpillar.Init(r.nextInt(4, 7), bodyColor); + } + + public void SetPosition(int x, int y, int width, int height) { + if (x >= 0 && y >= 0 && x + carWidth < width && y + carHeight < height) { + startPosX = x; + startPosY = y; + pictureWidth = width; + pictureHeight = height; + } + } + + public void MoveTransport(Direction direction) { + if (pictureWidth < 1|| pictureHeight < 1) + { + return; + } + switch (direction) + { + // вправо + case Right: + if (startPosX + carWidth + armoredCar.step < pictureWidth) + { + startPosX += armoredCar.step; + } + break; + //влево + case Left: + if (startPosX - armoredCar.step > 0) + { + startPosX -= armoredCar.step; + } + break; + //вверх + case Up: + if (startPosY - armoredCar.step > 0) + { + startPosY -= armoredCar.step; + } + break; + //вниз + case Down: + if (startPosY + carHeight + armoredCar.step < pictureHeight) + { + startPosY += armoredCar.step; + } + break; + } + } + + public void DrawTransport(Graphics2D g2d) + { + if (startPosX < 0 || startPosY < 0 + || pictureHeight < 1 || pictureWidth < 1) + { + return; + } + // отрисовка корпуса и гусеницы + g2d.setPaint(armoredCar.getBodyColor()); + g2d.fillRect((int ) startPosX + 20, (int) startPosY, 40, 20); + g2d.setPaint(Color.LIGHT_GRAY); + g2d.fillRect((int ) startPosX, (int ) startPosY + 20, 80, 20); + + g2d.fillOval((int ) startPosX, (int ) startPosY + 30, 20, 20); + g2d.fillOval((int ) startPosX + 80 - 20, (int ) startPosY + 30, 20, 20); + g2d.fillRect((int ) startPosX + 15, (int ) startPosY + 20, 60, 30); + // отрисовка катков в гусенице + drawingCaterpillar.DrawCaterpillar(g2d, (int)startPosX, (int)startPosY); + } + + public void ChangeBorders(int width, int height) + { + pictureWidth = width; + pictureHeight = height; + if (pictureWidth <= carWidth || pictureHeight <= carHeight) + { + pictureWidth = 0; + pictureHeight = 0; + return; + } + if (startPosX + carWidth > pictureWidth) + { + startPosX = pictureWidth - carWidth; + } + if (startPosY + carHeight > pictureHeight) + { + startPosY = pictureHeight - carHeight; + } + } +} diff --git a/src/main/java/DrawingCaterpillar.java b/src/main/java/DrawingCaterpillar.java new file mode 100644 index 0000000..ae2fb0c --- /dev/null +++ b/src/main/java/DrawingCaterpillar.java @@ -0,0 +1,37 @@ +import java.awt.*; + +public class DrawingCaterpillar { + private NumRinks numRinks = NumRinks.Four; + private Color color; + + public void Init(int n, Color color) { + setNumRinks(n); + this.color = color; + } + + public void setNumRinks(int n) { + switch (n) { + case 4 -> numRinks = NumRinks.Four; + + case 5 -> numRinks = NumRinks.Five; + + case 6 -> numRinks = NumRinks.Six; + + default -> { + break; + } + } + } + + public void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY) + { + color = color != null ? color : Color.YELLOW; + g2d.setPaint(color); + int size = numRinks == NumRinks.Four ? 15 : 10; + int dist = numRinks == NumRinks.Four ? 20 : 13; + startPosX = numRinks == NumRinks.Five ? startPosX + 5 : startPosX; + for (int i = 0; i < numRinks.val(); i++) { + g2d.fillOval(startPosX + dist * i, startPosY + 30, size, size); + } + } +} diff --git a/src/main/java/EntityArmoredCar.java b/src/main/java/EntityArmoredCar.java new file mode 100644 index 0000000..f1d54e5 --- /dev/null +++ b/src/main/java/EntityArmoredCar.java @@ -0,0 +1,28 @@ +import java.awt.*; + +public class EntityArmoredCar { + + private int speed; + private float weight; + private Color bodyColor; + public float step; + + public void init(int speed, float weight, Color bodyColor) { + this.speed = speed; + this.weight = weight; + this.bodyColor = bodyColor; + this.step = speed * 100 / weight; + } + + public int getSpeed() { + return speed; + } + + public float getWeight() { + return weight; + } + + public Color getBodyColor() { + return bodyColor; + } +} diff --git a/src/main/java/FormArmoredCar.form b/src/main/java/FormArmoredCar.form new file mode 100644 index 0000000..e82df6d --- /dev/null +++ b/src/main/java/FormArmoredCar.form @@ -0,0 +1,108 @@ + +
diff --git a/src/main/java/FormArmoredCar.java b/src/main/java/FormArmoredCar.java new file mode 100644 index 0000000..045cdaa --- /dev/null +++ b/src/main/java/FormArmoredCar.java @@ -0,0 +1,112 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; + +public class FormArmoredCar extends JFrame{ + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonRight; + private JButton buttonLeft; + private JPanel mainPanel; + private JPanel drawPanel; + private JButton buttonCreate; + private JLabel labelSpeed; + private JLabel labelWeight; + private JLabel labelColor; + + private DrawingArmoredCar armoredCar; + + public FormArmoredCar() { + super("Бронированная машина"); + setBounds(100, 100, 700, 700); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent componentEvent) { + if (armoredCar != null) { + armoredCar.ChangeBorders(drawPanel.getWidth(), drawPanel.getHeight()); + repaint(); + } + } + }); + + labelSpeed.setText("Скорость: "); + labelWeight.setText("Вес: "); + labelColor.setText("Цвет: "); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + Random rnd = new Random(); + armoredCar = new DrawingArmoredCar(); + armoredCar.Init(rnd.nextInt(100,300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + + armoredCar.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), + drawPanel.getWidth(), drawPanel.getHeight()); + + labelSpeed.setText("Скорость: " + armoredCar.getArmoredCar().getSpeed()); + labelWeight.setText("Вес: " + armoredCar.getArmoredCar().getWeight()); + labelColor.setText("Цвет: " + armoredCar.getArmoredCar().getBodyColor().getRGB()); + } + }); + + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (armoredCar != null) + armoredCar.MoveTransport(Direction.Up); + drawPanel.repaint(); + } + }); + + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (armoredCar != null) + armoredCar.MoveTransport(Direction.Down); + drawPanel.repaint(); + + } + }); + + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (armoredCar != null) + armoredCar.MoveTransport(Direction.Left); + drawPanel.repaint(); + } + }); + + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (armoredCar != null) + armoredCar.MoveTransport(Direction.Right); + drawPanel.repaint(); + } + }); + + setContentPane(mainPanel); + setVisible(true); + } + + private void createUIComponents() { + drawPanel = new JPanel() { + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + if (armoredCar != null) { + Graphics2D g2d = (Graphics2D) g; + armoredCar.DrawTransport(g2d); + } + super.repaint(); + } + }; + } +} \ No newline at end of file diff --git a/src/main/java/NumRinks.java b/src/main/java/NumRinks.java new file mode 100644 index 0000000..846cf67 --- /dev/null +++ b/src/main/java/NumRinks.java @@ -0,0 +1,6 @@ +public enum NumRinks { + Four{public int val() {return 4;}}, + Five{public int val() {return 5;}}, + Six{public int val() {return 6;}}; + public abstract int val(); +} diff --git a/src/main/java/Program.java b/src/main/java/Program.java index b57b1a9..2d1f91a 100644 --- a/src/main/java/Program.java +++ b/src/main/java/Program.java @@ -1,8 +1,5 @@ -import javax.swing.*; -import java.awt.*; - public class Program { public static void main(String[] args) { - + new FormArmoredCar(); } } diff --git a/src/main/resources/Down.png b/src/main/resources/Down.png new file mode 100644 index 0000000..b08133a Binary files /dev/null and b/src/main/resources/Down.png differ diff --git a/src/main/resources/Left.png b/src/main/resources/Left.png new file mode 100644 index 0000000..bc5a5cc Binary files /dev/null and b/src/main/resources/Left.png differ diff --git a/src/main/resources/Right.png b/src/main/resources/Right.png new file mode 100644 index 0000000..f981219 Binary files /dev/null and b/src/main/resources/Right.png differ diff --git a/src/main/resources/Up.png b/src/main/resources/Up.png new file mode 100644 index 0000000..9afd579 Binary files /dev/null and b/src/main/resources/Up.png differ