From 0487aff03051777c1ebe8d191f54c22412f015a7 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Mon, 28 Nov 2022 23:14:15 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D1=8B=20=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=84=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningPlane.java | 15 ++++---------- EntityPlane.java | 2 +- FormPlane.form | 45 +++++++++++++++++++---------------------- FormPlane.java | 50 +++++++++++++++++++++++++++++++--------------- 4 files changed, 60 insertions(+), 52 deletions(-) diff --git a/DrawningPlane.java b/DrawningPlane.java index de288de..2eb0c65 100644 --- a/DrawningPlane.java +++ b/DrawningPlane.java @@ -26,10 +26,9 @@ public class DrawningPlane extends JPanel { IlluminatorDraw.SetIlluminatorCount(numIllum); } - public void Init(int speed, float weight, Color bodyColor) + public DrawningPlane(int speed, float weight, Color bodyColor) { - Plane = new EntityPlane(); - Plane.Init(speed, weight, bodyColor); + Plane = new EntityPlane(speed, weight, bodyColor); IlluminatorDraw = new DrawningIlluminator(); SetIlluminator(); } @@ -79,20 +78,13 @@ public class DrawningPlane extends JPanel { } } - public void DrawTransport() { + public void DrawTransport(Graphics g) { if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { return; } - repaint(); - } - @Override - public void paintComponent(Graphics g) { if (GetPlane() == null) { return; } - if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { - return; - } super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; @@ -122,6 +114,7 @@ public class DrawningPlane extends JPanel { g2d.fillOval(_startPosX - 5, _startPosY + 25, 30, 10); IlluminatorDraw.DrawIlluminator(g, _startPosX, _startPosY); + repaint(); } public void ChangeBorders(int width, int height) diff --git a/EntityPlane.java b/EntityPlane.java index dd00e40..7ddb3c2 100644 --- a/EntityPlane.java +++ b/EntityPlane.java @@ -18,7 +18,7 @@ public class EntityPlane { } public float Step; - public void Init(int speed, float weight, Color bodyColor) + public EntityPlane(int speed, float weight, Color bodyColor) { Random rnd = new Random(); Speed = speed <= 0 ? rnd.nextInt(50,150) : speed; diff --git a/FormPlane.form b/FormPlane.form index 4e8ede6..2a60811 100644 --- a/FormPlane.form +++ b/FormPlane.form @@ -3,7 +3,7 @@ - + @@ -48,24 +48,6 @@ - - - - - - - - - - - - - - - - - - @@ -80,11 +62,6 @@ - - - - - @@ -98,6 +75,26 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/FormPlane.java b/FormPlane.java index fe8f8fe..e87eb23 100644 --- a/FormPlane.java +++ b/FormPlane.java @@ -5,24 +5,36 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.awt.image.BufferedImage; import java.util.Random; -public class FormPlane { +public class FormPlane extends JFrame{ public JPanel Mainpanel; private JButton ButtonCreate; private JButton ButtonLeft; private JButton ButtonUp; private JButton ButtonDown; private JButton ButtonRight; - protected DrawningPlane PictureBoxPlane; + protected DrawningPlane _plane; + private JPanel PictureBox; private JToolBar StatusStrip; private final JLabel JLabelSpeed = new JLabel(); private final JLabel JLabelWeight = new JLabel(); private final JLabel JLabelColor = new JLabel(); public void Draw() { - if (PictureBoxPlane.GetPlane() == null) { - return; + PictureBox.removeAll(); + BufferedImage bmp = new BufferedImage(PictureBox.getWidth(), PictureBox.getHeight(),BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + gr.setColor(new Color(238, 238, 238)); + gr.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); + if (_plane != null) { + _plane.DrawTransport(gr); + JLabel imageOfPlane = new JLabel(); + imageOfPlane.setPreferredSize(PictureBox.getSize()); + imageOfPlane.setMinimumSize(new Dimension(1, 1)); + imageOfPlane.setIcon(new ImageIcon(bmp)); + PictureBox.add(imageOfPlane,BorderLayout.CENTER); } - PictureBoxPlane.DrawTransport(); + validate(); } public FormPlane() { Box LabelBox = Box.createHorizontalBox(); @@ -45,35 +57,41 @@ public class FormPlane { } ButtonCreate.addActionListener(e -> { Random random = new Random(); - PictureBoxPlane.Init(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); - PictureBoxPlane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); - JLabelSpeed.setText("Cкорость: " + PictureBoxPlane.GetPlane().GetSpeed() + " "); - JLabelWeight.setText("Вес: " + PictureBoxPlane.GetPlane().GetWeight() + " "); - JLabelColor.setText(("Цвет: " + PictureBoxPlane.GetPlane().GetBodyColor() + " ")); + _plane = new DrawningPlane(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + _plane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBox.getWidth(), PictureBox.getHeight()); + JLabelSpeed.setText("Cкорость: " + _plane.GetPlane().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " ")); Draw(); }); - PictureBoxPlane.addComponentListener(new ComponentAdapter() { + PictureBox.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { + if(_plane == null || _plane.GetPlane() == null) return; super.componentResized(e); - PictureBoxPlane.ChangeBorders(PictureBoxPlane.getWidth(), PictureBoxPlane.getHeight()); + _plane.ChangeBorders(PictureBox.getWidth(), PictureBox.getHeight()); + PictureBox.revalidate(); Draw(); } }); ButtonUp.addActionListener(e -> { - PictureBoxPlane.MoveTransport(Direction.Up); + _plane.MoveTransport(Direction.Up); + PictureBox.revalidate(); Draw(); }); ButtonDown.addActionListener(e -> { - PictureBoxPlane.MoveTransport(Direction.Down); + _plane.MoveTransport(Direction.Down); + PictureBox.revalidate(); Draw(); }); ButtonRight.addActionListener(e -> { - PictureBoxPlane.MoveTransport(Direction.Right); + _plane.MoveTransport(Direction.Right); + PictureBox.revalidate(); Draw(); }); ButtonLeft.addActionListener(e -> { - PictureBoxPlane.MoveTransport(Direction.Left); + _plane.MoveTransport(Direction.Left); + PictureBox.revalidate(); Draw(); }); }