From a1971e880aa5fdef1f4380dad36c7da6d779d01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Tue, 13 Dec 2022 21:37:45 +0400 Subject: [PATCH] lab2 second stage --- DrawningBomber.java | 35 +++++++++++++------------ DrawningObjectBomber.java | 38 +++++++++++++++++++++++++++ DrawningWarplane.java | 48 ++++++++++++++++++++++++++++++++++ EntityWarplane.java | 22 ++++++++++++++++ FormAirBomber.java | 54 ++++++++++++++++++++++++++++++++++----- IDrawningObject.java | 9 +++++++ 6 files changed, 183 insertions(+), 23 deletions(-) create mode 100644 DrawningObjectBomber.java create mode 100644 DrawningWarplane.java create mode 100644 EntityWarplane.java create mode 100644 IDrawningObject.java diff --git a/DrawningBomber.java b/DrawningBomber.java index cb6fcfe..b2310aa 100644 --- a/DrawningBomber.java +++ b/DrawningBomber.java @@ -4,17 +4,15 @@ import java.util.*; public class DrawningBomber extends JPanel { - private EntityAirBomber AirBomber; + public EntityAirBomber AirBomber; public EntityAirBomber getAirBomber() { return AirBomber; } - private float _startPosX; - private float _startPosY; + public float _startPosX; + public float _startPosY; private int _pictureWidth = 0; private int _pictureHeight = 0; - private final int _airBomberWidth = 100; - private final int _airBomberHeight = 100; - - private int NumEngines = 1; + private int _airBomberWidth = 100; + private int _airBomberHeight = 100; private DrawEngines drawEngines = new DrawEngines(); public DrawningBomber(int speed, float weight, Color bodyColor) @@ -24,6 +22,13 @@ public class DrawningBomber extends JPanel drawEngines.SetNumEngines(random.nextInt(1, 4)); } + protected DrawningBomber(int speed, float weight, Color bodyColor, int airBomberWidth, int airBomberHeight) + { + this(speed, weight, bodyColor); + _airBomberWidth = airBomberWidth; + _airBomberHeight = airBomberHeight; + } + public void SetPosition(int x, int y, int width, int height) { if (x + _airBomberWidth > width || x < 0) return; @@ -69,17 +74,8 @@ public class DrawningBomber extends JPanel } } - public void DrawTransport() - { - if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0) - { - return; - } - repaint(); - } - @Override - public void paintComponent(Graphics g) + public void DrawTransport(Graphics g) { if (getAirBomber() == null || _startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0) { @@ -167,4 +163,9 @@ public class DrawningBomber extends JPanel _startPosY = _pictureHeight - _airBomberHeight; } } + + public float[] GetCurrentPosition() + { + return new float[]{_startPosX, _startPosX + _airBomberWidth, _startPosY, _startPosY + _airBomberHeight}; + } } diff --git a/DrawningObjectBomber.java b/DrawningObjectBomber.java new file mode 100644 index 0000000..faaf6a7 --- /dev/null +++ b/DrawningObjectBomber.java @@ -0,0 +1,38 @@ +import java.awt.*; + +public class DrawningObjectBomber implements IDrawningObject +{ + private DrawningBomber _airBomber = null; + + public DrawningObjectBomber(DrawningBomber airBomber) + { + _airBomber = airBomber; + } + + @Override + public float getStep() { + if (_airBomber == null || _airBomber.AirBomber == null) return 0; + return _airBomber.AirBomber.GetStep(); + } + + @Override + public void SetObject(int x, int y, int width, int height) { + _airBomber.SetPosition(x, y, width, height); + } + + @Override + public void MoveObject(Direction direction) { + _airBomber.MoveTransport(direction); + } + + @Override + public void DrawningObject(Graphics g) { + _airBomber.DrawTransport(g); + } + + @Override + public float[] GetCurrentPosition() { + if (_airBomber == null || _airBomber.AirBomber == null) return null; + return _airBomber.GetCurrentPosition(); + } +} \ No newline at end of file diff --git a/DrawningWarplane.java b/DrawningWarplane.java new file mode 100644 index 0000000..0985e1f --- /dev/null +++ b/DrawningWarplane.java @@ -0,0 +1,48 @@ +import java.awt.*; + +public class DrawningWarplane extends DrawningBomber +{ + public DrawningWarplane(int speed, float weight, Color bodyColor, Color dopColor, boolean engines, boolean weapons) + { + super(speed, weight, bodyColor, 100, 100); + AirBomber = new EntityWarplane(speed, weight, bodyColor, dopColor, engines, weapons); + } + + @Override + public void DrawTransport(Graphics g) + { + if (!(AirBomber instanceof EntityWarplane warplane)) + { + return; + } + + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.BLACK); + g2d.setColor(warplane.GetDopColor()); + if (warplane.GetEngines()) + { + g2d.fillOval((int)_startPosX + 50, (int)_startPosY + 15, 30, 15); + g2d.fillOval( (int)_startPosX + 45, (int)_startPosY + 35, 35, 15); + g2d.fillOval( (int)_startPosX + 45, (int)_startPosY + 70, 35, 15); + g2d.fillOval( (int)_startPosX + 50, (int)_startPosY + 90, 30, 15); + + g2d.drawOval((int)_startPosX + 50, (int)_startPosY + 15, 30, 15); + g2d.drawOval((int)_startPosX + 45, (int)_startPosY + 35, 35, 15); + g2d.drawOval((int)_startPosX + 45, (int)_startPosY + 70, 35, 15); + g2d.drawOval((int)_startPosX + 50, (int)_startPosY + 90, 30, 15); + } + + _startPosY += 25; + super.DrawTransport(g); + _startPosY -= 25; + + if (warplane.GetWeapons()) + { + g2d.fillRect((int)_startPosX + 40,(int) _startPosY, 30, 5); + g2d.fillRect( (int)_startPosX + 40, (int)_startPosY + 115, 30, 5); + + g2d.drawRect((int)_startPosX + 40, (int)_startPosY, 30, 5); + g2d.drawRect((int)_startPosX + 40, (int)_startPosY + 115, 30, 5); + } + } +} diff --git a/EntityWarplane.java b/EntityWarplane.java new file mode 100644 index 0000000..dbd65da --- /dev/null +++ b/EntityWarplane.java @@ -0,0 +1,22 @@ +import java.awt.*; + +public class EntityWarplane extends EntityAirBomber +{ + private Color DopColor; + private boolean Engines; + private boolean Weapons; + + public EntityWarplane(int speed, float weight, Color bodyColor, Color dopColor, boolean engines, boolean weapons) + { + super(speed, weight, bodyColor); + DopColor = dopColor; + Engines = engines; + Weapons = weapons; + } + + public Color GetDopColor(){return DopColor;} + + public boolean GetEngines(){return Engines;} + + public boolean GetWeapons(){return Weapons;} +} diff --git a/FormAirBomber.java b/FormAirBomber.java index 6fe0b6b..c890ad4 100644 --- a/FormAirBomber.java +++ b/FormAirBomber.java @@ -2,6 +2,7 @@ import javax.imageio.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; +import java.awt.image.*; import java.util.*; public class FormAirBomber { @@ -15,6 +16,8 @@ public class FormAirBomber { private JButton buttonLeft; private JButton buttonUp; private JButton buttonDown; + private JPanel GraphicsOutput; + private JButton buttonCreateModif; private JLabel JLabelSpeed = new JLabel(); private JLabel JLabelWeight = new JLabel(); private JLabel JLabelColor = new JLabel(); @@ -22,7 +25,27 @@ public class FormAirBomber { private void Draw() { if (pictureBomber.getAirBomber() == null) return; - pictureBomber.DrawTransport(); + GraphicsOutput.removeAll(); + BufferedImage bmp = new BufferedImage(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(),BufferedImage.TYPE_INT_RGB); + Graphics g = bmp.getGraphics(); + g.setColor(new Color(238, 238, 238)); + g.fillRect(0, 0, GraphicsOutput.getWidth(), GraphicsOutput.getHeight()); + pictureBomber.DrawTransport(g); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(GraphicsOutput.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(bmp)); + GraphicsOutput.add(imageOfShip,BorderLayout.CENTER); + //validate(); + } + + private void SetData() + { + Random random = new Random(); + pictureBomber.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight()); + JLabelSpeed.setText("Cкорость: " + pictureBomber.getAirBomber().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + pictureBomber.getAirBomber().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + pictureBomber.getAirBomber().GetColor() + " ")); } private void ButtonMove_Click(String name) @@ -72,10 +95,7 @@ public class FormAirBomber { public void actionPerformed(ActionEvent e) { Random random = new Random(); pictureBomber = new DrawningBomber(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); - pictureBomber.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBomber.getWidth(), pictureBomber.getHeight()); - JLabelSpeed.setText("Cкорость: " + pictureBomber.getAirBomber().GetSpeed() + " "); - JLabelWeight.setText("Вес: " + pictureBomber.getAirBomber().GetWeight() + " "); - JLabelColor.setText(("Цвет: " + pictureBomber.getAirBomber().GetColor() + " ")); + SetData(); Draw(); } }); @@ -103,13 +123,35 @@ public class FormAirBomber { ButtonMove_Click("buttonRight"); } }); - pictureBomber.addComponentListener(new ComponentAdapter() { + /*pictureBomber.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { super.componentResized(e); pictureBomber.ChangeBorders(pictureBomber.getWidth(), pictureBomber.getHeight()); Draw(); } + });*/ + GraphicsOutput.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + if (pictureBomber == null || pictureBomber.getAirBomber() == null) return; + pictureBomber.ChangeBorders(GraphicsOutput.getWidth(), GraphicsOutput.getHeight()); + GraphicsOutput.revalidate(); + Draw(); + } + }); + buttonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + pictureBomber = new DrawningWarplane(random.nextInt(100, 300), random.nextInt(1000, 3000), + 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()); + SetData(); + Draw(); + } }); } } diff --git a/IDrawningObject.java b/IDrawningObject.java new file mode 100644 index 0000000..670fcb8 --- /dev/null +++ b/IDrawningObject.java @@ -0,0 +1,9 @@ +import java.awt.*; + +public interface IDrawningObject { + float getStep(); + void SetObject(int x, int y, int width, int height); + void MoveObject(Direction direction); + void DrawningObject(Graphics g); + float[] GetCurrentPosition(); +} \ No newline at end of file