This commit is contained in:
devil_1nc 2022-12-05 18:09:08 +04:00
parent 2a1c499096
commit 7660243c11
17 changed files with 541 additions and 25 deletions

162
AbstractMap.java Normal file
View File

@ -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;
}
}

View File

@ -1,4 +1,4 @@
enum Direction
{
Right, Left, Up, Down
None, Right, Left, Up, Down
}

View File

@ -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);
}
}
}
}
}

View File

@ -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};
}
}

View File

@ -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;
}

32
DrawingObjectAirbus.java Normal file
View File

@ -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;
}
}

2
DrawingSquareIlum.java Normal file
View File

@ -0,0 +1,2 @@
public class DrawingSquareIlum {
}

3
DrawingStarIlum.java Normal file
View File

@ -0,0 +1,3 @@
public class DrawingStarIlum {
}

15
EntityAdvancedAirbus.java Normal file
View File

@ -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;
}
}

View File

@ -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);

View File

@ -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();
}
}

123
FormMap.java Normal file
View File

@ -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();
}
}

6
IDrawingIlum.java Normal file
View File

@ -0,0 +1,6 @@
import java.awt.*;
public interface IDrawingIlum {
void setIl(int num);
void DrawIl(int startPosX, int startPosY, Graphics2D g);
}

17
IDrawingObject.java Normal file
View File

@ -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
}

5
Program.java Normal file
View File

@ -0,0 +1,5 @@
public class Program {
public static void main(String[] args) {
new FormMap();
}
}

46
SimpleMap.java Normal file
View File

@ -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)));
}
}

45
SkyMap.java Normal file
View File

@ -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);
}
}