From 0487aff03051777c1ebe8d191f54c22412f015a7 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Mon, 28 Nov 2022 23:14:15 +0400 Subject: [PATCH 1/6] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4?= =?UTF-8?q?=20=D0=BD=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D1=8B=20=D0=B8=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=84?= =?UTF-8?q?=D0=BE=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(); }); } -- 2.25.1 From 41e6816ee51a4bb2f65f072e91192dd7923af910 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Tue, 29 Nov 2022 00:13:32 +0400 Subject: [PATCH 2/6] Modified object --- DrawningAirbus.java | 53 +++++++++++++++++++++++++++++++++++++++++++++ DrawningPlane.java | 17 ++++++++++----- EntityAirbus.java | 27 +++++++++++++++++++++++ FormPlane.form | 32 +++++++++++++++++---------- FormPlane.java | 24 +++++++++++++++----- 5 files changed, 130 insertions(+), 23 deletions(-) create mode 100644 DrawningAirbus.java create mode 100644 EntityAirbus.java diff --git a/DrawningAirbus.java b/DrawningAirbus.java new file mode 100644 index 0000000..98c9a28 --- /dev/null +++ b/DrawningAirbus.java @@ -0,0 +1,53 @@ +import java.awt.*; + +public class DrawningAirbus extends DrawningPlane{ + + public DrawningAirbus(int speed, float weight, Color bodyColor, Color dopColor, boolean bodyKit, boolean wing, boolean sportLine) + { + super(speed, weight, bodyColor, 140, 70); + Plane = new EntityAirbus(speed, weight, bodyColor, dopColor, bodyKit, wing, sportLine); + } + @Override + public void DrawTransport(Graphics g) + { + if (! (Plane instanceof EntityAirbus Airbus)) + { + return; + } + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.BLACK); + _startPosX += 10; + _startPosY += 5; + super.DrawTransport(g); + _startPosX -= 10; + _startPosY -= 5; + if (Airbus.BodyKit) + { + g.drawRect(_startPosX + 70, _startPosY + 50, 22, 16); + g.drawRect(_startPosX + 8, _startPosY + 18, 22, 16); + g.drawOval(_startPosX, _startPosY + 18, 16, 16); + g.drawOval(_startPosX + 62, _startPosY + 50, 16, 16); + + g2d.setPaint(Airbus.DopColor); + g.fillRect(_startPosX + 70, _startPosY + 50, 22, 16); + g.fillOval(_startPosX + 84, _startPosY + 50, 16, 16); + g.fillOval(_startPosX + 24, _startPosY + 18, 16, 16); + g.fillRect(_startPosX + 8, _startPosY + 18, 22, 16); + + g2d.setPaint(Color.BLACK); + g.fillOval(_startPosX, _startPosY + 18, 16, 16); + g.fillOval(_startPosX + 62, _startPosY + 50, 16, 16); + } + if (Airbus.Wing) + { + g.drawLine(_startPosX + 70, _startPosY + 20, _startPosX + 70, _startPosY + 35); + g.drawLine(_startPosX + 70, _startPosY + 20, _startPosX + 90, _startPosY + 35); + } + if (Airbus.SportLine) + { + g.drawOval(_startPosX + 110, _startPosY + 40, 9, 9); + g.fillOval(_startPosX + 110, _startPosY + 40, 9, 9); + } + } +} diff --git a/DrawningPlane.java b/DrawningPlane.java index 2eb0c65..561078e 100644 --- a/DrawningPlane.java +++ b/DrawningPlane.java @@ -5,19 +5,19 @@ import java.util.random.RandomGenerator; public class DrawningPlane extends JPanel { - private EntityPlane Plane; + EntityPlane Plane; public EntityPlane GetPlane(){ return Plane; } - private int _startPosX; - private int _startPosY; + protected int _startPosX; + protected int _startPosY; public DrawningIlluminator IlluminatorDraw; public Integer _pictureWidth = null; public Integer _pictureHeight = null; - private final int _PlaneWidth = 130; - private final int _PlaneHeight = 70; + private int _PlaneWidth = 130; + private int _PlaneHeight = 70; public void SetIlluminator() { Random r = new Random(); @@ -78,6 +78,13 @@ public class DrawningPlane extends JPanel { } } + protected DrawningPlane(int speed, float weight, Color bodyColor, int planeWidth, int planeHeight) + { + this(speed, weight, bodyColor); + _PlaneWidth = planeWidth; + _PlaneHeight = planeHeight; + } + public void DrawTransport(Graphics g) { if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { return; diff --git a/EntityAirbus.java b/EntityAirbus.java new file mode 100644 index 0000000..396d409 --- /dev/null +++ b/EntityAirbus.java @@ -0,0 +1,27 @@ +import java.awt.*; + +public class EntityAirbus extends EntityPlane{ + + public Color DopColor; + public Color GetDopColor() { + return DopColor; + } + + public boolean BodyKit; + public boolean GetBodyKit(){ return BodyKit; } + + public boolean Wing; + public boolean GetWing(){ return Wing; } + + public boolean SportLine; + public boolean GetSportLine(){ return SportLine; } + + + public EntityAirbus(int speed, float weight, Color bodyColor, Color dopColor, boolean bodyKit, boolean wing, boolean sportLine) { + super(speed, weight, bodyColor); + DopColor = dopColor; + BodyKit = bodyKit; + Wing = wing; + SportLine = sportLine; + } +} diff --git a/FormPlane.form b/FormPlane.form index 2a60811..2426924 100644 --- a/FormPlane.form +++ b/FormPlane.form @@ -1,6 +1,6 @@
- + @@ -13,7 +13,7 @@ - + @@ -25,7 +25,7 @@ - + @@ -37,7 +37,7 @@ - + @@ -50,7 +50,7 @@ - + @@ -62,11 +62,6 @@ - - - - - @@ -77,7 +72,7 @@ - + @@ -85,7 +80,7 @@ - + @@ -95,6 +90,19 @@ + + + + + + + + + + + + + diff --git a/FormPlane.java b/FormPlane.java index e87eb23..ac34cbb 100644 --- a/FormPlane.java +++ b/FormPlane.java @@ -1,12 +1,13 @@ import javax.imageio.ImageIO; 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.awt.image.BufferedImage; import java.util.Random; + +import static java.lang.Boolean.parseBoolean; + public class FormPlane extends JFrame{ public JPanel Mainpanel; private JButton ButtonCreate; @@ -17,6 +18,7 @@ public class FormPlane extends JFrame{ protected DrawningPlane _plane; private JPanel PictureBox; private JToolBar StatusStrip; + private JButton ButtonModif; private final JLabel JLabelSpeed = new JLabel(); private final JLabel JLabelWeight = new JLabel(); private final JLabel JLabelColor = new JLabel(); @@ -36,6 +38,13 @@ public class FormPlane extends JFrame{ } validate(); } + private void SetData(){ + Random random = new Random(); + _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() + " ")); + } public FormPlane() { Box LabelBox = Box.createHorizontalBox(); LabelBox.setMinimumSize(new Dimension(1, 20)); @@ -58,10 +67,13 @@ public class FormPlane extends JFrame{ ButtonCreate.addActionListener(e -> { Random random = new Random(); _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() + " ")); + SetData(); + Draw(); + }); + ButtonModif.addActionListener(e -> { + Random random = new Random(); + _plane = new DrawningAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); + SetData(); Draw(); }); PictureBox.addComponentListener(new ComponentAdapter() { -- 2.25.1 From 56c85de90927889b1801b3fb8aaebad54d9a5de2 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Tue, 29 Nov 2022 00:25:21 +0400 Subject: [PATCH 3/6] Add interface --- DrawningObjectPlane.java | 38 ++++++++++++++++++++++++++++++++++++++ DrawningPlane.java | 9 +++++++++ IDrawningObject.java | 13 +++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 DrawningObjectPlane.java create mode 100644 IDrawningObject.java diff --git a/DrawningObjectPlane.java b/DrawningObjectPlane.java new file mode 100644 index 0000000..49f8fbc --- /dev/null +++ b/DrawningObjectPlane.java @@ -0,0 +1,38 @@ +import java.awt.*; + +public class DrawningObjectPlane implements IDrawningObject { + private DrawningPlane _plane = null; + + public DrawningObjectPlane(DrawningPlane plane) + { + _plane = plane; + } + + public float Step() { + if(_plane != null && _plane.Plane != null) + return _plane.Plane.Step; + return 0; + } + + @Override + public void SetObject(int x, int y, int width, int height) { + _plane.SetPosition(x,y,width,height); + } + + @Override + public void MoveObject(Direction direction) { + _plane.MoveTransport(direction); + } + + @Override + public void DrawingObject(Graphics g) { + _plane.DrawTransport(g); + } + + @Override + public float[] GetCurrentPosition() { + if(_plane!=null) + return _plane.GetCurrentPosition(); + return null; + } +} diff --git a/DrawningPlane.java b/DrawningPlane.java index 561078e..964996f 100644 --- a/DrawningPlane.java +++ b/DrawningPlane.java @@ -143,4 +143,13 @@ public class DrawningPlane extends JPanel { _startPosY = _pictureHeight - _PlaneHeight; } } + + public float[] GetCurrentPosition() { + float[] dim = new float[4]; + dim[0] = _startPosX; + dim[1] =_startPosY; + dim[2] = _startPosX + _PlaneWidth; + dim[3] = _startPosY + _PlaneHeight; + return dim; + } } diff --git a/IDrawningObject.java b/IDrawningObject.java new file mode 100644 index 0000000..9b32730 --- /dev/null +++ b/IDrawningObject.java @@ -0,0 +1,13 @@ +import java.awt.*; + +public interface IDrawningObject { + public float Step = 0; + + void SetObject(int x, int y, int width, int height); + + void MoveObject(Direction direction); + + void DrawingObject(Graphics g); + + float[] GetCurrentPosition(); +} -- 2.25.1 From 0c7b7b773c0ff8d424c41d0acb968f9f9df77b19 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Tue, 29 Nov 2022 20:49:03 +0400 Subject: [PATCH 4/6] =?UTF-8?q?=D0=A1=D0=BE=D0=B1=D1=80=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BF=D1=80=D0=BE=D1=81=D1=82=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=87=D0=B0=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractMap.java | 115 +++++++++++++++++++++++++++++++++++ Direction.java | 3 +- DrawningObjectPlane.java | 7 ++- EntityAirbus.java | 7 --- EntityPlane.java | 2 + FormMap.form | 121 ++++++++++++++++++++++++++++++++++++ FormMap.java | 128 +++++++++++++++++++++++++++++++++++++++ IDrawningObject.java | 5 +- Main.java | 2 +- MyMap.java | 44 ++++++++++++++ SimpleMap.java | 45 ++++++++++++++ 11 files changed, 465 insertions(+), 14 deletions(-) create mode 100644 AbstractMap.java create mode 100644 FormMap.form create mode 100644 FormMap.java create mode 100644 MyMap.java create mode 100644 SimpleMap.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..cf2e1b3 --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,115 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawningObject _drawningObject = null; + protected int[][] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected final Random _random = new Random(); + protected final int _freeRoad = 0; + protected final int _barrier = 1; + + public BufferedImage CreateMap(int width, int height, IDrawningObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + private boolean CheckBarriers(float topOffset, float rightOffset, float leftOffset, float bottomOffset) + { + float[] arrayPossition = _drawningObject.GetCurrentPosition(); + int top = (int)((arrayPossition[1] + topOffset) / _size_y); + int right = (int)((arrayPossition[2] + rightOffset) / _size_x); + int left = (int)((arrayPossition[0] + leftOffset) / _size_x); + int bottom = (int)((arrayPossition[3] + bottomOffset) / _size_y); + if (top < 0 || left < 0 || right >= _map[0].length || bottom >= _map.length) return false; + for (int i = top; i <= bottom; i++) + { + for (int j = left; j <= right; j++) + { + if (_map[j][i] == _barrier) return false; + } + } + return true; + } + + public BufferedImage MoveObject(Direction direction) + { + if (_drawningObject == null) return DrawMapWithObject(); + boolean isTrue = true; + switch (direction) + { + case Left: + if (!CheckBarriers(0, -1 * _drawningObject.Step(), -1 * _drawningObject.Step(), 0)) isTrue = false; + break; + case Right: + if (!CheckBarriers(0, _drawningObject.Step(), _drawningObject.Step(), 0)) isTrue = false; + break; + case Up: + if (!CheckBarriers(-1 * _drawningObject.Step(), 0, 0, -1 * _drawningObject.Step())) isTrue = false; + break; + case Down: + if (!CheckBarriers(_drawningObject.Step(), 0, 0, _drawningObject.Step())) isTrue = false; + break; + } + if (isTrue) + { + _drawningObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + + private boolean SetObjectOnMap() + { + if (_drawningObject == null || _map == null) + { + return false; + } + int x = _random.nextInt(0, 10); + int y = _random.nextInt(0, 10); + _drawningObject.SetObject(x, y, _width, _height); + if (!CheckBarriers(0, 0, 0, 0)) return false; + return true; + } + + private BufferedImage DrawMapWithObject() + { + BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); + if (_drawningObject == null || _map == null) + { + return bmp; + } + Graphics gr = bmp.getGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart((Graphics2D) gr, i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart((Graphics2D) gr, i, j); + } + } + } + _drawningObject.DrawningObject(gr); + return bmp; + } + + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics2D g, int i, int j); + protected abstract void DrawBarrierPart(Graphics2D g, int i, int j); +} diff --git a/Direction.java b/Direction.java index d82266b..d1def94 100644 --- a/Direction.java +++ b/Direction.java @@ -2,6 +2,7 @@ public enum Direction { Up(1), Down(2), Left(3), - Right(4); + Right(4), + None(0); Direction(int value){} } \ No newline at end of file diff --git a/DrawningObjectPlane.java b/DrawningObjectPlane.java index 49f8fbc..5c257f0 100644 --- a/DrawningObjectPlane.java +++ b/DrawningObjectPlane.java @@ -1,4 +1,5 @@ import java.awt.*; +import java.util.Enumeration; public class DrawningObjectPlane implements IDrawningObject { private DrawningPlane _plane = null; @@ -7,10 +8,10 @@ public class DrawningObjectPlane implements IDrawningObject { { _plane = plane; } - + @Override public float Step() { if(_plane != null && _plane.Plane != null) - return _plane.Plane.Step; + return _plane.Plane.GetStep(); return 0; } @@ -25,7 +26,7 @@ public class DrawningObjectPlane implements IDrawningObject { } @Override - public void DrawingObject(Graphics g) { + public void DrawningObject(Graphics g) { _plane.DrawTransport(g); } diff --git a/EntityAirbus.java b/EntityAirbus.java index 396d409..af93301 100644 --- a/EntityAirbus.java +++ b/EntityAirbus.java @@ -3,19 +3,12 @@ import java.awt.*; public class EntityAirbus extends EntityPlane{ public Color DopColor; - public Color GetDopColor() { - return DopColor; - } public boolean BodyKit; - public boolean GetBodyKit(){ return BodyKit; } public boolean Wing; - public boolean GetWing(){ return Wing; } public boolean SportLine; - public boolean GetSportLine(){ return SportLine; } - public EntityAirbus(int speed, float weight, Color bodyColor, Color dopColor, boolean bodyKit, boolean wing, boolean sportLine) { super(speed, weight, bodyColor); diff --git a/EntityPlane.java b/EntityPlane.java index 7ddb3c2..b232f25 100644 --- a/EntityPlane.java +++ b/EntityPlane.java @@ -16,7 +16,9 @@ public class EntityPlane { public Color GetBodyColor() { return BodyColor; } + public float Step; + public float GetStep(){return Step;} public EntityPlane(int speed, float weight, Color bodyColor) { diff --git a/FormMap.form b/FormMap.form new file mode 100644 index 0000000..ff5bafc --- /dev/null +++ b/FormMap.form @@ -0,0 +1,121 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..f25dbdf --- /dev/null +++ b/FormMap.java @@ -0,0 +1,128 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class FormMap extends JFrame{ + public JPanel Mainpanel; + private JButton ButtonCreate; + private JButton ButtonLeft; + private JButton ButtonUp; + private JButton ButtonDown; + private JButton ButtonRight; + private JPanel PictureBox; + private JToolBar StatusStrip; + private JButton ButtonModif; + private JComboBox comboBoxSelector; + private final JLabel JLabelSpeed = new JLabel(); + private final JLabel JLabelWeight = new JLabel(); + private final JLabel JLabelColor = new JLabel(); + private AbstractMap _abstractMap; + + private void ButtonMove_Click(String name) + { + Direction dir = Direction.None; + switch (name) + { + case "buttonLeft": + dir = Direction.Left; + break; + case "buttonUp": + dir = Direction.Up; + break; + case "buttonRight": + dir = Direction.Right; + break; + case "buttonDown": + dir = Direction.Down; + break; + } + PictureBox.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(dir))); + PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); + PictureBox.revalidate(); + PictureBox.repaint(); + } + + private void SetData(DrawningPlane _plane){ + PictureBox.removeAll(); + JLabelSpeed.setText("Cкорость: " + _plane.GetPlane().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + _plane.GetPlane().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + _plane.GetPlane().GetBodyColor() + " ")); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(PictureBox.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.CreateMap(PictureBox.getWidth(),PictureBox.getHeight(), new DrawningObjectPlane(_plane)))); + PictureBox.add(imageWithMapAndObject, BorderLayout.CENTER); + PictureBox.revalidate(); + } + public FormMap() { + comboBoxSelector.addItem("Простая карта"); + comboBoxSelector.addItem("Вторая карта"); + _abstractMap = new SimpleMap(); + Box LabelBox = Box.createHorizontalBox(); + LabelBox.setMinimumSize(new Dimension(1, 20)); + LabelBox.add(JLabelSpeed); + LabelBox.add(JLabelWeight); + LabelBox.add(JLabelColor); + StatusStrip.add(LabelBox); + try { + Image img = ImageIO.read(FormMap.class.getResource("/Resource/arrowUp.jpg")); + ButtonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormMap.class.getResource("/Resource/arrowDown.jpg")); + ButtonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormMap.class.getResource("/Resource/arrowLeft.jpg")); + ButtonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormMap.class.getResource("/Resource/arrowRight.jpg")); + ButtonRight.setIcon(new ImageIcon(img)); + } catch (Exception ex) { + System.out.println(ex); + } + ButtonCreate.addActionListener(e -> { + Random random = new Random(); + var _plane = new DrawningPlane(random.nextInt(100, 300),random.nextInt(1000, 2000),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + SetData(_plane); + }); + ButtonModif.addActionListener(e -> { + Random random = new Random(); + var _plane = new DrawningAirbus(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean(), random.nextBoolean()); + SetData(_plane); + }); + ButtonUp.addActionListener(e -> { + ButtonMove_Click("buttonUp"); + }); + ButtonDown.addActionListener(e -> { + ButtonMove_Click("buttonDown"); + }); + ButtonLeft.addActionListener(e -> { + ButtonMove_Click("buttonLeft"); + }); + ButtonRight.addActionListener(e -> { + ButtonMove_Click("buttonRight"); + }); + comboBoxSelector.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + comboBoxSelector = (JComboBox)e.getSource(); + String item = (String)comboBoxSelector.getSelectedItem(); + switch (item) { + case "Простая карта" -> { + _abstractMap = new SimpleMap(); + break; + } + case "Вторая карта" -> { + _abstractMap = new MyMap(); + break; + } + } + } + }); + + } +} diff --git a/IDrawningObject.java b/IDrawningObject.java index 9b32730..01157fa 100644 --- a/IDrawningObject.java +++ b/IDrawningObject.java @@ -1,13 +1,14 @@ import java.awt.*; +import java.util.Enumeration; public interface IDrawningObject { - public float Step = 0; + float Step(); void SetObject(int x, int y, int width, int height); void MoveObject(Direction direction); - void DrawingObject(Graphics g); + void DrawningObject(Graphics g); float[] GetCurrentPosition(); } diff --git a/Main.java b/Main.java index 0a4e1b1..f7c77d5 100644 --- a/Main.java +++ b/Main.java @@ -4,7 +4,7 @@ public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Самолёт"); - frame.setContentPane(new FormPlane().Mainpanel); + frame.setContentPane(new FormMap().Mainpanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(500, 200); frame.pack(); diff --git a/MyMap.java b/MyMap.java new file mode 100644 index 0000000..08bac74 --- /dev/null +++ b/MyMap.java @@ -0,0 +1,44 @@ +import java.awt.*; + +public class MyMap extends AbstractMap{ + private Color barrierColor = Color.WHITE; + private Color roadColor = Color.CYAN; + + @Override + protected void DrawBarrierPart(Graphics2D g, int i, int j) + { + g.setPaint(barrierColor); + g.fillRect((int)Math.floor(i * _size_x), (int)Math.floor(j * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + @Override + protected void DrawRoadPart(Graphics2D g, int i, int j) + { + g.setPaint(roadColor); + g.fillRect((int)Math.floor(i * _size_x), (int)Math.floor(j * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 25) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } +} diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..0b5f48c --- /dev/null +++ b/SimpleMap.java @@ -0,0 +1,45 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap{ + + private Color barrierColor = Color.BLACK; + private Color roadColor = Color.GRAY; + + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 50) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } + @Override + protected void DrawRoadPart(Graphics2D g, int i, int j) + { + g.setPaint(roadColor); + g.fillRect((int)Math.floor(i * _size_x), (int)Math.floor(j * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + @Override + protected void DrawBarrierPart(Graphics2D g, int i, int j) + { + g.setPaint(barrierColor); + g.fillRect((int)Math.floor(i * _size_x), (int)Math.floor(j * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } +} -- 2.25.1 From 0f832390b4a8db2f490b3b7ef20482a8a71c2d19 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Tue, 29 Nov 2022 21:33:34 +0400 Subject: [PATCH 5/6] Complete lab --- DrawningIlluminator.java | 6 +++--- DrawningPlane.java | 15 +++++++++++-- DrawningSqareIlluminator.java | 33 +++++++++++++++++++++++++++++ DrawningTriangleIlluminator.java | 36 ++++++++++++++++++++++++++++++++ IDrawningIlluminator.java | 7 +++++++ IDrawningObject.java | 1 - 6 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 DrawningSqareIlluminator.java create mode 100644 DrawningTriangleIlluminator.java create mode 100644 IDrawningIlluminator.java diff --git a/DrawningIlluminator.java b/DrawningIlluminator.java index 86de975..4134150 100644 --- a/DrawningIlluminator.java +++ b/DrawningIlluminator.java @@ -1,15 +1,15 @@ -import javax.swing.*; import java.awt.*; -public class DrawningIlluminator extends JComponent { +public class DrawningIlluminator implements IDrawningIlluminator{ private IlluminatorCount _Illuminator; + @Override public void SetIlluminatorCount(int numOfIllum) { _Illuminator = IlluminatorCount.GetIlluminatorCount(numOfIllum); } + @Override public void DrawIlluminator(Graphics g, int _startPosX, int _startPosY) { - super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.BLACK); int numOfIlluminator = 0; diff --git a/DrawningPlane.java b/DrawningPlane.java index 964996f..13e2e74 100644 --- a/DrawningPlane.java +++ b/DrawningPlane.java @@ -13,7 +13,7 @@ public class DrawningPlane extends JPanel { protected int _startPosX; protected int _startPosY; - public DrawningIlluminator IlluminatorDraw; + public IDrawningIlluminator IlluminatorDraw; public Integer _pictureWidth = null; public Integer _pictureHeight = null; private int _PlaneWidth = 130; @@ -29,7 +29,18 @@ public class DrawningPlane extends JPanel { public DrawningPlane(int speed, float weight, Color bodyColor) { Plane = new EntityPlane(speed, weight, bodyColor); - IlluminatorDraw = new DrawningIlluminator(); + Random random = new Random(); + switch (random.nextInt(3)){ + case 0: + IlluminatorDraw = new DrawningIlluminator(); + break; + case 1: + IlluminatorDraw = new DrawningSqareIlluminator(); + break; + case 2: + IlluminatorDraw = new DrawningTriangleIlluminator(); + break; + } SetIlluminator(); } diff --git a/DrawningSqareIlluminator.java b/DrawningSqareIlluminator.java new file mode 100644 index 0000000..735b59b --- /dev/null +++ b/DrawningSqareIlluminator.java @@ -0,0 +1,33 @@ +import java.awt.*; + +public class DrawningSqareIlluminator implements IDrawningIlluminator{ + private IlluminatorCount _Illuminator; + @Override + public void SetIlluminatorCount(int numOfIllum) {_Illuminator = IlluminatorCount.GetIlluminatorCount(numOfIllum); } + + @Override + public void DrawIlluminator(Graphics g, int _startPosX, int _startPosY) { + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.BLACK); + int numOfIlluminator = 0; + switch (_Illuminator) + { + case Ten: + numOfIlluminator = 10; + break; + case Twenty: + numOfIlluminator = 20; + break; + case Thirty: + numOfIlluminator = 30; + break; + } + + for(int i = numOfIlluminator; i >= 1; --i){ + g2d.setColor(Color.CYAN); + g2d.fillRect(_startPosX + 105 - 3 * i, _startPosY + 35, 3, 3); + g2d.setColor(Color.BLACK); + g2d.drawRect(_startPosX + 105 - 3 * i, _startPosY + 35, 3, 3); + } + } +} diff --git a/DrawningTriangleIlluminator.java b/DrawningTriangleIlluminator.java new file mode 100644 index 0000000..bfe5035 --- /dev/null +++ b/DrawningTriangleIlluminator.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class DrawningTriangleIlluminator implements IDrawningIlluminator{ + private IlluminatorCount _Illuminator; + + @Override + public void SetIlluminatorCount(int numOfIllum) { + _Illuminator = IlluminatorCount.GetIlluminatorCount(numOfIllum); + } + + @Override + public void DrawIlluminator(Graphics g, int _startPosX, int _startPosY) { + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.BLACK); + int numOfIlluminator = 0; + switch (_Illuminator) + { + case Ten: + numOfIlluminator = 10; + break; + case Twenty: + numOfIlluminator = 20; + break; + case Thirty: + numOfIlluminator = 30; + break; + } + + for(int i = numOfIlluminator; i >= 1; --i){ + g2d.setColor(Color.CYAN); + g2d.drawPolygon(new int[] {_startPosX + 105 - 3 * i, _startPosX + 102 - 3 * i, _startPosX + 108 - 3 * i}, new int[] {_startPosY + 35, _startPosY + 38, _startPosY + 38}, 3); + g2d.setColor(Color.BLACK); + g2d.fillPolygon(new int[] {_startPosX + 105 - 3 * i, _startPosX + 102 - 3 * i, _startPosX + 108 - 3 * i}, new int[] {_startPosY + 35, _startPosY + 38, _startPosY + 38}, 3); + } + } +} diff --git a/IDrawningIlluminator.java b/IDrawningIlluminator.java new file mode 100644 index 0000000..6d66d39 --- /dev/null +++ b/IDrawningIlluminator.java @@ -0,0 +1,7 @@ +import java.awt.*; + +public interface IDrawningIlluminator { + + void SetIlluminatorCount(int numOfIllum); + void DrawIlluminator(Graphics g, int _startPosX, int _startPosY); +} diff --git a/IDrawningObject.java b/IDrawningObject.java index 01157fa..7b6465a 100644 --- a/IDrawningObject.java +++ b/IDrawningObject.java @@ -1,5 +1,4 @@ import java.awt.*; -import java.util.Enumeration; public interface IDrawningObject { float Step(); -- 2.25.1 From 2a2abe6c33a2875b17c6cd9d867a68c546aee285 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Wed, 30 Nov 2022 11:22:41 +0400 Subject: [PATCH 6/6] Complete lab --- DrawningObjectPlane.java | 1 - DrawningPlane.java | 1 - 2 files changed, 2 deletions(-) diff --git a/DrawningObjectPlane.java b/DrawningObjectPlane.java index 5c257f0..1353dd5 100644 --- a/DrawningObjectPlane.java +++ b/DrawningObjectPlane.java @@ -1,5 +1,4 @@ import java.awt.*; -import java.util.Enumeration; public class DrawningObjectPlane implements IDrawningObject { private DrawningPlane _plane = null; diff --git a/DrawningPlane.java b/DrawningPlane.java index 13e2e74..fe462e7 100644 --- a/DrawningPlane.java +++ b/DrawningPlane.java @@ -1,7 +1,6 @@ import javax.swing.*; import java.awt.*; import java.util.Random; -import java.util.random.RandomGenerator; public class DrawningPlane extends JPanel { -- 2.25.1