From 01959c985035d34e23a31f2afbcd45bd9a98de0c Mon Sep 17 00:00:00 2001 From: prodigygirl Date: Fri, 7 Oct 2022 19:33:25 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A4=D0=BE=D1=80=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/FormCar.form | 105 +++++++++++++++++++++++++++++++ src/main/java/FormCar.java | 123 +++++++++++++++++++++++++++++++++++++ src/main/java/Program.java | 2 +- 3 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 src/main/java/FormCar.form create mode 100644 src/main/java/FormCar.java diff --git a/src/main/java/FormCar.form b/src/main/java/FormCar.form new file mode 100644 index 0000000..ad5b35a --- /dev/null +++ b/src/main/java/FormCar.form @@ -0,0 +1,105 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/FormCar.java b/src/main/java/FormCar.java new file mode 100644 index 0000000..3569b7c --- /dev/null +++ b/src/main/java/FormCar.java @@ -0,0 +1,123 @@ +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 FormCar 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 FormCar() { + 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("Цвет: "); + + Canvas canvas = new Canvas(); + drawPanel.add(canvas); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + Random rnd = new Random(); + armoredCar = new DrawingArmoredCar(); + armoredCar.Init(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + + armoredCar.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90) + 10, + drawPanel.getWidth(), drawPanel.getHeight()); + + canvas.setArmoredCar(armoredCar); + labelSpeed.setText("Скорость: " + armoredCar.armoredCar.getSpeed()); + labelWeight.setText("Вес: " + armoredCar.armoredCar.getWeight()); + labelColor.setText("Цвет: " + armoredCar.armoredCar.getWeight()); + } + }); + + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (armoredCar != null) + armoredCar.MoveTransport(Direction.Up); + canvas.repaint(); + } + }); + + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (armoredCar != null) + armoredCar.MoveTransport(Direction.Down); + canvas.repaint(); + } + }); + + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (armoredCar != null) + armoredCar.MoveTransport(Direction.Left); + canvas.repaint(); + } + }); + + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (armoredCar != null) + armoredCar.MoveTransport(Direction.Right); + canvas.repaint(); + } + }); + + setContentPane(mainPanel); + setVisible(true); + } +} + +class Canvas extends JPanel { + private DrawingArmoredCar armoredCar; + public Canvas () { + super(); + } + + public void setArmoredCar(DrawingArmoredCar armoredCar) { + this.armoredCar = armoredCar; + } + + public void paintComponent(Graphics g) { + super.paintComponent(g); + if (armoredCar != null) { + Graphics2D g2d = (Graphics2D) g; + armoredCar.DrawTransport(g2d); + } + super.repaint(); + } +} diff --git a/src/main/java/Program.java b/src/main/java/Program.java index b57b1a9..5182f41 100644 --- a/src/main/java/Program.java +++ b/src/main/java/Program.java @@ -3,6 +3,6 @@ import java.awt.*; public class Program { public static void main(String[] args) { - + new FormCar(); } }