From 7660243c1162f3e2d1ea26d5fa4b9d5f08e5250c Mon Sep 17 00:00:00 2001 From: devil_1nc Date: Mon, 5 Dec 2022 18:09:08 +0400 Subject: [PATCH] 1 com --- AbstractMap.java | 162 +++++++++++++++++++++++++++++++++++++ Direction.java | 2 +- DrawingAdvancedAirbus.java | 44 ++++++++++ DrawingAirbus.java | 45 ++++++++--- DrawingIlum.java | 2 +- DrawingObjectAirbus.java | 32 ++++++++ DrawingSquareIlum.java | 2 + DrawingStarIlum.java | 3 + EntityAdvancedAirbus.java | 15 ++++ EntityAirbus.java | 8 +- FormAirbus.java | 9 +-- FormMap.java | 123 ++++++++++++++++++++++++++++ IDrawingIlum.java | 6 ++ IDrawingObject.java | 17 ++++ Program.java | 5 ++ SimpleMap.java | 46 +++++++++++ SkyMap.java | 45 +++++++++++ 17 files changed, 541 insertions(+), 25 deletions(-) create mode 100644 AbstractMap.java create mode 100644 DrawingAdvancedAirbus.java create mode 100644 DrawingObjectAirbus.java create mode 100644 DrawingSquareIlum.java create mode 100644 DrawingStarIlum.java create mode 100644 EntityAdvancedAirbus.java create mode 100644 FormMap.java create mode 100644 IDrawingIlum.java create mode 100644 IDrawingObject.java create mode 100644 Program.java create mode 100644 SimpleMap.java create mode 100644 SkyMap.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..4771e13 --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,162 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawingObject _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; + + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics g, int i, int j); + protected abstract void DrawBarrierPart(Graphics g, int i, int j); + + public BufferedImage CreateMap(int width, int height, IDrawingObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); // abstract void + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + public BufferedImage MoveObject(Direction direction) + { + boolean isFree = true; + + int startPosX = (int)(_drawningObject.GetCurrentPosition()[3] / _size_x); + int startPosY = (int)(_drawningObject.GetCurrentPosition()[0] / _size_y); + + int objectWidth = (int)(_drawningObject.GetCurrentPosition()[1] / _size_x); + int objectHeight = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y); + + switch (direction) + { + case Right: + for (int i = objectWidth; i <= objectWidth + (int)(_drawningObject.getStep() / _size_x); i++) + { + for (int j = startPosY; j <= objectHeight; j++) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Left: + for (int i = startPosX; i >= (int)(_drawningObject.getStep() / _size_x); i--) + { + for (int j = startPosY; j <= objectHeight; j++) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Up: + for (int i = startPosX; i <= objectWidth; i++) + { + for (int j = startPosY; j >= (int)(_drawningObject.getStep() / _size_y); j--) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Down: + for (int i = startPosX; i <= objectWidth; i++) + { + for (int j = objectHeight; j <= objectHeight + (int)(_drawningObject.getStep() / _size_y); j++) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + } + if (isFree) + { + _drawningObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + + private boolean SetObjectOnMap() + { + if (_drawningObject == null || _map == null) + { + return false; + } + int x = _random.nextInt( 10); + int y = _random.nextInt( 10); + _drawningObject.SetObject(x, y, _width, _height); + + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (i * _size_x >= x && j * _size_y >= y && + i * _size_x <= x + _drawningObject.GetCurrentPosition()[1] && + j * _size_y <= j + _drawningObject.GetCurrentPosition()[2]) + { + if (_map[i][j] == _barrier) + { + 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(gr, i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawningObject.DrawingObject(gr); + return bmp; + } +} diff --git a/Direction.java b/Direction.java index baa66d8..a84454c 100644 --- a/Direction.java +++ b/Direction.java @@ -1,4 +1,4 @@ enum Direction { - Right, Left, Up, Down + None, Right, Left, Up, Down } diff --git a/DrawingAdvancedAirbus.java b/DrawingAdvancedAirbus.java new file mode 100644 index 0000000..92b1023 --- /dev/null +++ b/DrawingAdvancedAirbus.java @@ -0,0 +1,44 @@ +import java.awt.*; + +public class DrawingAdvancedAirbus extends DrawingAirbus{ + public DrawingAdvancedAirbus(int speed, float weight, Color bodyColor, Color extraColor, boolean superTurbine, boolean extraCell) + { + super(speed, weight, bodyColor, 145, 55); + Airbus = new EntityAdvancedAirbus(speed, weight, bodyColor, extraColor, superTurbine, extraCell); + } + @Override + public void DrawTransport(Graphics2D g) + { + if (Airbus instanceof EntityAdvancedAirbus) + { + EntityAdvancedAirbus advancedAirbus = (EntityAdvancedAirbus) Airbus; + + g.setColor(advancedAirbus.ExtraColor); + + if (advancedAirbus.SuperTurbine) + { + g.fillRect((int)_startPosX, (int)_startPosY + 20, 30, 22); + g.drawLine((int)_startPosX, (int)_startPosY + 42, (int)_startPosX + 30, (int)_startPosY + 42); + + } + _startPosX += 10; + _startPosY += 5; + + super.DrawTransport((Graphics2D)g); + + _startPosX -= 10; + _startPosY -= 5; + if (advancedAirbus.ExtraCell) + { + g.fillRect((int)_startPosX + 50, (int)_startPosY, 20, 15); + for (int i = (int)_startPosX + 50; i < (int)_startPosX + 70; i += 3) + { + g.setColor(Color.BLUE); + g.fillRect(i, (int)_startPosY + 7, 2, 2); + } + } + + + } + } +} diff --git a/DrawingAirbus.java b/DrawingAirbus.java index c162d1a..7bf4c38 100644 --- a/DrawingAirbus.java +++ b/DrawingAirbus.java @@ -1,22 +1,41 @@ import java.awt.*; +import java.util.Random; + public class DrawingAirbus { public EntityAirbus Airbus; - private float _startPosX; - private float _startPosY; + protected float _startPosX; + protected float _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; - private final int _airbusWidth = 125; - private final int _airbusHeight = 45; + private int _airbusWidth = 125; + private int _airbusHeight = 45; - public DrawingIlum drawingilum; - public void Init(int speed, float weight, Color bodyColor, int ilNum, EntityAirbus entity) + public IDrawingIlum drawingilum; + + private final Random random = new Random(); + public DrawingAirbus(int speed, float weight, Color bodyColor) { - Airbus = entity; - drawingilum = new DrawingIlum(); - drawingilum.Init(ilNum, bodyColor); - Airbus.Init(speed, weight, bodyColor); + int randExtra = random.nextInt(2); + switch (random.nextInt(3)){ + /*case 0: + drawingilum = new DrawingStarIlum(randExtra); + break; + case 1: + drawingilum = new DrawingSquareIlum(randExtra); + break; + case 2: + drawingilum = new DrawingIlum(randExtra, bodyColor); + break;*/ + } + Airbus = new EntityAirbus(speed, weight, bodyColor); + } + // Новый конструктор + protected DrawingAirbus (int speed, float weight, Color bodyColor, int airbusWidth, int airbusHeight) + { + this(speed, weight, bodyColor); + _airbusWidth = airbusWidth; + _airbusHeight = airbusHeight; } - public void SetPosition(int x, int y, int width, int height) { if (x < 0 || x + _airbusWidth >= width) @@ -169,4 +188,8 @@ public class DrawingAirbus { _startPosY = _pictureHeight - _airbusHeight; } } + public float[] GetCurrentPosition() + { + return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _airbusWidth, /*DOWN*/ _startPosY + _airbusHeight, /*LEFT*/ _startPosX}; + } } diff --git a/DrawingIlum.java b/DrawingIlum.java index b96bcac..9faa4cc 100644 --- a/DrawingIlum.java +++ b/DrawingIlum.java @@ -18,7 +18,7 @@ public class DrawingIlum { } private Color color; - public void Init(int num, Color color) { + public DrawingIlum(int num, Color color) { setIl(num); this.color = color; } diff --git a/DrawingObjectAirbus.java b/DrawingObjectAirbus.java new file mode 100644 index 0000000..417cffa --- /dev/null +++ b/DrawingObjectAirbus.java @@ -0,0 +1,32 @@ +import java.awt.*; + +public class DrawingObjectAirbus implements IDrawingObject { + + private DrawingAirbus _airbus = null; + + public DrawingObjectAirbus(DrawingAirbus airbus) + { + _airbus = airbus; + } + public float getStep() { + if (_airbus.Airbus != null) { + return _airbus.Airbus.Step(); + } + return 0; + } + public void SetObject(int x, int y, int width, int height) { + if (_airbus != null) _airbus.SetPosition(x, y, width, height); + } + public void MoveObject(Direction direction) { + if (_airbus != null) _airbus.MoveTransport(direction); + } + public void DrawingObject(Graphics g) { + if (_airbus != null) _airbus.DrawTransport((Graphics2D) g); + } + public float[] GetCurrentPosition() { + if (_airbus != null) { + return _airbus.GetCurrentPosition(); + } + return null; + } +} \ No newline at end of file diff --git a/DrawingSquareIlum.java b/DrawingSquareIlum.java new file mode 100644 index 0000000..bffbb0d --- /dev/null +++ b/DrawingSquareIlum.java @@ -0,0 +1,2 @@ +public class DrawingSquareIlum { +} diff --git a/DrawingStarIlum.java b/DrawingStarIlum.java new file mode 100644 index 0000000..6c5d2c7 --- /dev/null +++ b/DrawingStarIlum.java @@ -0,0 +1,3 @@ +public class DrawingStarIlum { + +} diff --git a/EntityAdvancedAirbus.java b/EntityAdvancedAirbus.java new file mode 100644 index 0000000..a8b0e4e --- /dev/null +++ b/EntityAdvancedAirbus.java @@ -0,0 +1,15 @@ +import java.awt.*; + +public class EntityAdvancedAirbus extends EntityAirbus{ + public final Color ExtraColor; + public final boolean SuperTurbine; + public final boolean ExtraCell; + + public EntityAdvancedAirbus(int speed, float weight, Color bodyColor, Color extraColor, boolean superTurbine, boolean extraCell) + { + super(speed, weight, bodyColor); + ExtraColor = extraColor; + SuperTurbine = superTurbine; + ExtraCell = extraCell; + } +} diff --git a/EntityAirbus.java b/EntityAirbus.java index 3218cba..3ea53cc 100644 --- a/EntityAirbus.java +++ b/EntityAirbus.java @@ -1,9 +1,9 @@ -import java.awt.Color; +import java.awt.*; import java.util.Random; public class EntityAirbus { - private int Speed = 0; - private float Weight = 0; + private int Speed; + private float Weight; private Color BodyColor; public int getSpeed(){return Speed;} @@ -12,7 +12,7 @@ public class EntityAirbus { public float Step(){return Speed * 100 / Weight;} - public void Init(int speed, float weight, Color bodyColor) + public EntityAirbus(int speed, float weight, Color bodyColor) { Random rand = new Random(); if (speed <= 0) speed = rand.nextInt(350, 550); diff --git a/FormAirbus.java b/FormAirbus.java index 69795d6..0c9ae24 100644 --- a/FormAirbus.java +++ b/FormAirbus.java @@ -49,9 +49,7 @@ public class FormAirbus extends JComponent createButton.addActionListener(e -> { Random rand = new Random(); - _plane = new DrawingAirbus(); - _entity = new EntityAirbus(); - _plane.Init(rand.nextInt( 400 + rand.nextInt(200)), 1000 + rand.nextInt(1000), Color.getHSBColor(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), rand.nextInt(3), _entity); + _plane = new DrawingAirbus(rand.nextInt( 400 + rand.nextInt(200)), 1000 + rand.nextInt(1000), Color.getHSBColor(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256))); _plane.SetPosition(10 + rand.nextInt(90), 10 + rand.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75); speedLabel.setText("Speed: " + _plane.Airbus.getSpeed()); weightLabel.setText("Weight: " + (int)_plane.Airbus.getWeight()); @@ -99,9 +97,4 @@ public class FormAirbus extends JComponent if (_plane != null) _plane.DrawTransport(g2); super.repaint(); } - - public static void main(String[] args) { - new FormAirbus(); - } - } diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..6b24bc2 --- /dev/null +++ b/FormMap.java @@ -0,0 +1,123 @@ +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 JComponent{ + private AbstractMap _abstractMap; + private BufferedImage bufferImg = null; + + public FormMap() { + JFrame formFrame = new JFrame("Form Map"); + formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + formFrame.setSize(1000, 500); + formFrame.setVisible(true); + formFrame.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new FlowLayout()); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.SOUTH); + + Label speedLabel = new Label("Speed: "); + Label weightLabel = new Label("Weight: "); + Label colorLabel = new Label("Color: "); + + String[] maps = { + "Simple Map", + "Sky Map", + }; + JComboBox mapSelectComboBox = new JComboBox(maps); + mapSelectComboBox.setEditable(true); + mapSelectComboBox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String item = (String)mapSelectComboBox.getSelectedItem(); + if (item == null) return; + switch (item) { + case ("Simple Map"): + _abstractMap = new SimpleMap(); + break; + case ("Sky Map"): + _abstractMap = new SkyMap(); + break; + } + } + }); + + JButton createButton = new JButton("Create"); + createButton.addActionListener(e -> { + Random rnd = new Random(); + var airbus = new DrawingAirbus(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + speedLabel.setText("Speed: " + airbus.Airbus.getSpeed()); + weightLabel.setText("Weight: " + (int)airbus.Airbus.getWeight()); + colorLabel.setText("Color: " + airbus.Airbus.getBodyColor().getRed() + " " + airbus.Airbus.getBodyColor().getGreen() + " " + airbus.Airbus.getBodyColor().getBlue()); + if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawingObjectAirbus(airbus)); + repaint(); + }); + + JButton modifiedButton = new JButton("Modified"); + modifiedButton.addActionListener(e -> { + Random rnd = new Random(); + var airbus = new DrawingAdvancedAirbus(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), + rnd.nextBoolean()); + speedLabel.setText("Speed: " + airbus.Airbus.getSpeed()); + weightLabel.setText("Weight: " + (int)airbus.Airbus.getWeight()); + colorLabel.setText("Color: " + airbus.Airbus.getBodyColor().getRed() + " " + airbus.Airbus.getBodyColor().getGreen() + " " + airbus.Airbus.getBodyColor().getBlue() ); + if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawingObjectAirbus(airbus)); + repaint(); + }); + statusPanel.add(mapSelectComboBox); + statusPanel.add(createButton); + statusPanel.add(modifiedButton); + statusPanel.add(speedLabel); + statusPanel.add(weightLabel); + statusPanel.add(colorLabel); + + JButton moveDownButton = new JButton("Down"); + moveDownButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Down); + repaint(); + }); + + JButton moveUpButton = new JButton("Up"); + moveUpButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Up); + repaint(); + }); + + JButton moveLeftButton = new JButton("Left"); + moveLeftButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Left); + repaint(); + }); + + JButton moveRightButton = new JButton("Right"); + moveRightButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Right); + repaint(); + }); + + statusPanel.add(moveUpButton); + statusPanel.add(moveDownButton); + statusPanel.add(moveLeftButton); + statusPanel.add(moveRightButton); + + formFrame.getContentPane().add(this); + super.repaint(); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (bufferImg != null) g2.drawImage(bufferImg, 0,0,1000,490,null); + super.repaint(); + } +} diff --git a/IDrawingIlum.java b/IDrawingIlum.java new file mode 100644 index 0000000..9e6db46 --- /dev/null +++ b/IDrawingIlum.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawingIlum { + void setIl(int num); + void DrawIl(int startPosX, int startPosY, Graphics2D g); +} diff --git a/IDrawingObject.java b/IDrawingObject.java new file mode 100644 index 0000000..14919ee --- /dev/null +++ b/IDrawingObject.java @@ -0,0 +1,17 @@ +import java.awt.*; +public interface IDrawingObject { + /// Шаг перемещения объекта + float getStep(); + /// Установка позиции объекта + void SetObject(int x, int y, int width, int height); + /// Изменение направления перемещения объекта + void MoveObject(Direction direction); + /// Отрисовка объекта + void DrawingObject(Graphics g); + /// Получение текущей позиции объекта + float[] GetCurrentPosition(); + //0 - up + //1 - right + //2 - down + //3 - left +} diff --git a/Program.java b/Program.java new file mode 100644 index 0000000..ff7ede6 --- /dev/null +++ b/Program.java @@ -0,0 +1,5 @@ +public class Program { + public static void main(String[] args) { + new FormMap(); + } +} diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..45f71fe --- /dev/null +++ b/SimpleMap.java @@ -0,0 +1,46 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap { + /// Цвет участка закрытого + private final Color barrierColor = Color.BLACK; + /// Цвет участка открытого + private final 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(100); + int y = _random.nextInt(85); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) { + g.setColor(roadColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + + 1)),(int)( j * (_size_y + 1))); + } + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) { + g.setColor(barrierColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + + 1)), (int)(j * (_size_y + 1))); + } +} diff --git a/SkyMap.java b/SkyMap.java new file mode 100644 index 0000000..de96933 --- /dev/null +++ b/SkyMap.java @@ -0,0 +1,45 @@ +import java.awt.*; + +public class SkyMap extends AbstractMap { + /// Цвет участка закрытого + private final Color barrierColor = Color.BLACK; + /// Цвет участка открытого + private final Color roadColor = Color.cyan; + @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 < 30) + { + int x = _random.nextInt(100); + int y = _random.nextInt(85); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) { + g.setColor(roadColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + + 1)),(int)( j * (_size_y + 1))); + } + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) { + g.setColor(barrierColor); + g.fillPolygon(new int[] {i * (int)_size_x, (int)_size_x + 8, (int)_size_x + 8}, new int[] {j * (int)_size_y, j * (int)_size_y - 5, j * (int)_size_y + 5}, 3); + } +}