lab2 last stage

This commit is contained in:
Артём Алейкин 2022-12-13 22:45:40 +04:00
parent a1971e880a
commit 5cd477b895
12 changed files with 346 additions and 17 deletions

115
AbstractMap.java Normal file
View File

@ -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 Random _random = new Random();
protected int _freeRoad = 0;
protected 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 CanMove(float[] position) //Проверка на возможность шага
{
for (int i = (int)(position[2] / _size_y); i <= (int)(position[3] / _size_y); ++i)
{
for (int j = (int)(position[0] / _size_x); j <= (int)(position[1] / _size_x); ++j)
{
if (i >= 0 && j >= 0 && i < _map.length && j < _map[0].length && _map[i][j] == _barrier) return false;
}
}
return true;
}
public BufferedImage MoveObject(Direction direction)
{
float[] position = _drawningObject.GetCurrentPosition();
if (direction == Direction.Left)
{
position[0] -= _drawningObject.getStep();
}
else if (direction == Direction.Right)
{
position[1] += _drawningObject.getStep();
}
else if (direction == Direction.Up)
{
position[2] -= _drawningObject.getStep();
}
else if (direction == Direction.Down)
{
position[3] += _drawningObject.getStep();
}
if (CanMove(position))
{
_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);
for (int i = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y); i <= (int)(_drawningObject.GetCurrentPosition()[3] / _size_y); ++i)
{
for (int j = (int)(_drawningObject.GetCurrentPosition()[0] / _size_x); j <= (int)(_drawningObject.GetCurrentPosition()[1] / _size_x); ++j)
{
if (_map[i][j] == _barrier) _map[i][j] = _freeRoad;
}
}
return true;
}
private BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
if (_drawningObject == null || _map == null)
{
return bmp;
}
Graphics g = 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(g, i, j);
}
else if (_map[i][j] == _barrier)
{
DrawBarrierPart(g, i, j);
}
}
}
_drawningObject.DrawningObject(g);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}

View File

@ -1,5 +1,6 @@
public enum Direction public enum Direction
{ {
None(0),
Up(1), Up(1),
Down(2), Down(2),
Left(3), Left(3),

View File

@ -1,11 +1,13 @@
import java.awt.*; import java.awt.*;
public class DrawEngines public class DrawEngines implements IDrawningEngines
{ {
private Engines numEngines; private Engines numEngines;
@Override
public void SetNumEngines(int i) { numEngines = Engines.GetNumEngines(i); } public void SetNumEngines(int i) { numEngines = Engines.GetNumEngines(i); }
@Override
public void DrawningEngines(float _startPosX, float _startPosY, Graphics2D g2d, int _airBomberWidth, int _airBomberHeight) public void DrawningEngines(float _startPosX, float _startPosY, Graphics2D g2d, int _airBomberWidth, int _airBomberHeight)
{ {
switch(numEngines) switch(numEngines)

25
DrawRocketEngine.java Normal file
View File

@ -0,0 +1,25 @@
import java.awt.*;
public class DrawRocketEngine implements IDrawningEngines
{
private Engines numRocketEngines;
@Override
public void SetNumEngines(int i) { numRocketEngines = Engines.GetNumEngines(i); }
@Override
public void DrawningEngines(float _startPosX, float _startPosY, Graphics2D g2d, int _airBomberWidth, int _airBomberHeight)
{
switch(numRocketEngines)
{
case TwoEngines:
break;
case FourEngines:
g2d.fillRect((int)_startPosX + _airBomberWidth, (int)_startPosY + _airBomberHeight / 2 - 10, 20, 20);
break;
case SixEngines:
g2d.fillRect((int)_startPosX + _airBomberWidth, (int)_startPosY + _airBomberHeight / 2 - 10, 20, 20);
break;
}
}
}

27
DrawWings.java Normal file
View File

@ -0,0 +1,27 @@
import java.awt.*;
public class DrawWings implements IDrawningEngines
{
private Engines numWings;
@Override
public void SetNumEngines(int i) { numWings = Engines.GetNumEngines(i); }
@Override
public void DrawningEngines(float _startPosX, float _startPosY, Graphics2D g2d, int _airBomberWidth, int _airBomberHeight)
{
switch(numWings)
{
case TwoEngines:
break;
case FourEngines:
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 + 25, (int)_startPosY + _airBomberHeight / 2 - 10, 20, 10);
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 + 25, (int)_startPosY + _airBomberHeight / 2, 20, 10);
break;
case SixEngines:
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 + 25, (int)_startPosY + _airBomberHeight / 2 - 10, 20, 10);
g2d.fillRect((int)_startPosX + _airBomberWidth / 2 + 25, (int)_startPosY + _airBomberHeight / 2, 20, 10);
break;
}
}
}

View File

@ -14,12 +14,25 @@ public class DrawningBomber extends JPanel
private int _airBomberWidth = 100; private int _airBomberWidth = 100;
private int _airBomberHeight = 100; private int _airBomberHeight = 100;
private DrawEngines drawEngines = new DrawEngines(); private DrawEngines drawEngines = new DrawEngines();
private IDrawningEngines ide;
public DrawningBomber(int speed, float weight, Color bodyColor) public DrawningBomber(int speed, float weight, Color bodyColor)
{ {
AirBomber = new EntityAirBomber(speed, weight, bodyColor); AirBomber = new EntityAirBomber(speed, weight, bodyColor);
Random random = new Random(); Random random = new Random();
drawEngines.SetNumEngines(random.nextInt(1, 4)); switch (random.nextInt(3))
{
case 0:
ide = new DrawEngines();
break;
case 1:
ide = new DrawRocketEngine();
break;
case 2:
ide = new DrawWings();
break;
}
ide.SetNumEngines(random.nextInt(1, 4));
} }
protected DrawningBomber(int speed, float weight, Color bodyColor, int airBomberWidth, int airBomberHeight) protected DrawningBomber(int speed, float weight, Color bodyColor, int airBomberWidth, int airBomberHeight)
@ -141,7 +154,7 @@ public class DrawningBomber extends JPanel
(int)(_startPosY + _airBomberHeight / 2 + 5)}; (int)(_startPosY + _airBomberHeight / 2 + 5)};
g2d.fillPolygon(xPos, yPos, xPos.length); g2d.fillPolygon(xPos, yPos, xPos.length);
drawEngines.DrawningEngines(_startPosX, _startPosY, g2d, _airBomberWidth, _airBomberHeight); ide.DrawningEngines(_startPosX, _startPosY, g2d, _airBomberWidth, _airBomberHeight);
} }
public void ChangeBorders(int width, int height) public void ChangeBorders(int width, int height)

View File

@ -4,7 +4,7 @@ public class DrawningWarplane extends DrawningBomber
{ {
public DrawningWarplane(int speed, float weight, Color bodyColor, Color dopColor, boolean engines, boolean weapons) public DrawningWarplane(int speed, float weight, Color bodyColor, Color dopColor, boolean engines, boolean weapons)
{ {
super(speed, weight, bodyColor, 100, 100); super(speed, weight, bodyColor, 120, 120);
AirBomber = new EntityWarplane(speed, weight, bodyColor, dopColor, engines, weapons); AirBomber = new EntityWarplane(speed, weight, bodyColor, dopColor, engines, weapons);
} }
@ -21,15 +21,11 @@ public class DrawningWarplane extends DrawningBomber
g2d.setColor(warplane.GetDopColor()); g2d.setColor(warplane.GetDopColor());
if (warplane.GetEngines()) if (warplane.GetEngines())
{ {
g2d.fillOval((int)_startPosX + 50, (int)_startPosY + 15, 30, 15); g2d.fillOval((int)_startPosX + 50, (int)_startPosY + 40, 30, 15);
g2d.fillOval( (int)_startPosX + 45, (int)_startPosY + 35, 35, 15); g2d.fillOval( (int)_startPosX + 45, (int)_startPosY + 60, 35, 15);
g2d.fillOval( (int)_startPosX + 45, (int)_startPosY + 70, 35, 15); g2d.fillOval( (int)_startPosX + 45, (int)_startPosY + 95, 35, 15);
g2d.fillOval( (int)_startPosX + 50, (int)_startPosY + 90, 30, 15); g2d.fillOval( (int)_startPosX + 50, (int)_startPosY + 115, 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; _startPosY += 25;
@ -38,11 +34,9 @@ public class DrawningWarplane extends DrawningBomber
if (warplane.GetWeapons()) if (warplane.GetWeapons())
{ {
g2d.fillRect((int)_startPosX + 40,(int) _startPosY, 30, 5); g2d.fillRect((int)_startPosX + 40,(int) _startPosY + 20, 30, 5);
g2d.fillRect( (int)_startPosX + 40, (int)_startPosY + 115, 30, 5); g2d.fillRect( (int)_startPosX + 40, (int)_startPosY + 140, 30, 5);
g2d.drawRect((int)_startPosX + 40, (int)_startPosY, 30, 5);
g2d.drawRect((int)_startPosX + 40, (int)_startPosY + 115, 30, 5);
} }
} }
} }

6
IDrawningEngines.java Normal file
View File

@ -0,0 +1,6 @@
import java.awt.*;
public interface IDrawningEngines {
void SetNumEngines(int count);
void DrawningEngines(float _startPosX, float _startPosY, Graphics2D g2d, int _airBomberWidth, int _airBomberHeight);
}

View File

@ -3,7 +3,7 @@ import javax.swing.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
JFrame frame = new JFrame("Hard №1"); JFrame frame = new JFrame("Hard №1");
frame.setContentPane(new FormAirBomber().Mainpanel); frame.setContentPane(new FormMap().Mainpanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200); frame.setLocation(500, 200);
frame.pack(); frame.pack();

49
SecondMap.java Normal file
View File

@ -0,0 +1,49 @@
import java.awt.*;
public class SecondMap extends AbstractMap
{
private Color barrierColor = Color.YELLOW;
private Color roadColor = Color.BLUE;
@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 < 35)
{
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 DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(barrierColor);
g2d.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)_size_x, (int)_size_y);
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(roadColor);
g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)_size_x, (int)_size_y);
}
}

49
SimpleMap.java Normal file
View File

@ -0,0 +1,49 @@
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 DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(barrierColor);
g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(roadColor);
g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
}

48
ThirdMap.java Normal file
View File

@ -0,0 +1,48 @@
import java.awt.*;
public class ThirdMap extends AbstractMap
{
private Color barrierColor = Color.RED;
private Color roadColor = Color.GREEN;
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(barrierColor);
g2d.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)_size_x, (int)_size_y);
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(roadColor);
g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)_size_x, (int)_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 < 40)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
}