Сейв перед ошибками

This commit is contained in:
Кашин Максим 2022-10-19 10:08:53 +04:00
parent 523b5466b2
commit b2c1f7e979
5 changed files with 255 additions and 0 deletions

148
src/AbstractMap.java Normal file
View File

@ -0,0 +1,148 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class AbstractMap {
private IDrawingObject _drawingObject = 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 _freeWaterArea = 0;
protected final int _land = 1;
public BufferedImage CreateMap(int width, int height, IDrawingObject drawingObject)
{
_width = width;
_height = height;
_drawingObject = drawingObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public BufferedImage MoveObject(Direction direction)
{
float[] dim = _drawingObject.GetCurrentPosition();
if (CheckLand(dim[0], dim[1], dim[2], dim[3]) != 0)
{
_drawingObject.MoveObject(SetOppositDirection(direction));
}
if (true)
{
_drawingObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private boolean SetObjectOnMap()
{
if (_drawingObject==null || _map == null)
{
return false;
}
int x = _random.nextInt(10);
int y = _random.nextInt(10);
_drawingObject.SetObject(x, y, _width, _height);
float[] dim = _drawingObject.GetCurrentPosition();
while (CheckLand(dim[0], dim[1], dim[2], dim[3]) != 2)
{
int res;
do
{
res = CheckLand(dim[0], dim[1], dim[2], dim[3]);
if (res == 0)
{
_drawingObject.SetObject((int)dim[0], (int)dim[1], _width, _height);
return true;
}
else
{
dim[0] += _size_x;
}
} while (res != 2);
dim[0] = x;
dim[1] += _size_y;
}
return false;
}
private BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width, _height,BufferedImage.TYPE_INT_RGB);
if (_drawingObject == null || _map == null)
{
return bmp;
}
Graphics gr = bmp.getGraphics();
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[i].length; ++j)
{
if (_map[i][j] == _freeWaterArea)
{
DrawWaterPart(gr, i, j);
}
else if (_map[i][j] == _land)
{
DrawLandPart(gr, i, j);
}
}
}
_drawingObject.DrawingObject(gr);
return bmp;
}
private int CheckLand(float Left, float Right, float Top, float Bottom)
{
int RUi = (int)(Left / _size_x);
int RUj = (int)(Right / _size_y);
int LDi = (int)(Top / _size_x);
int LDj = (int)(Bottom / _size_y);
if (RUi < 0 || RUj < 0 || LDi >= _map[0].length || LDj >= _map.length)
{
return -1;
}
for (int x = RUi; x <= LDi; x++)
{
for (int y = RUj; y <= LDj; y++)
{
if (_map[x][y] == _land)
{
return 1;
}
}
}
return 0;
}
private Direction SetOppositDirection(Direction dir)
{
switch (dir)
{
case Up:
return Direction.Down;
case Down:
return Direction.Up;
case Left:
return Direction.Right;
case Right:
return Direction.Left;
case None:
return Direction.None;
}
return Direction.None;
}
protected abstract void GenerateMap();
protected abstract void DrawWaterPart(Graphics gr, int i, int j);
protected abstract void DrawLandPart(Graphics gr, int i, int j);
}

View File

@ -0,0 +1,26 @@
import java.awt.*;
public class EntityImprovedGasolineTanker extends EntityGasolineTanker{
public Color DopColor;
public Color GetDopColor() {
return DopColor;
}
public boolean BodyKit ;
public boolean GetBodyKit () {
return BodyKit ;
}
public boolean OrnamentWheels ;
public boolean GetOrnamentWheels () {
return OrnamentWheels ;
}
public EntityImprovedGasolineTanker(int speed, float weight, Color bodyColor, Color dopColor, boolean bodyKit, boolean ornamentWheels){
super(speed,weight,bodyColor);
DopColor = dopColor;
BodyKit = bodyKit;
OrnamentWheels = ornamentWheels;
}
}

8
src/IDrawingObject.java Normal file
View File

@ -0,0 +1,8 @@
import java.awt.*;
public interface IDrawingObject {
public float Step=0;
void SetObject(int x, int y, int width, int height);
void MoveObject(Direction direction);
void DrawingObject(Graphics g);
float[] GetCurrentPosition();
}

37
src/LongMap.java Normal file
View File

@ -0,0 +1,37 @@
import java.awt.*;
public class LongMap extends AbstractMap{
@Override
protected void GenerateMap() {
_map = new int[60][60];
_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[i].length; ++j) {
_map[i][j] = _freeWaterArea;
}
}
while (counter < 20) {
int x = _random.nextInt(59);
int y = _random.nextInt(59);
if (_map[x][y] == _freeWaterArea) {
_map[x][y] = _land;
counter++;
}
}
}
@Override
protected void DrawWaterPart(Graphics gr, int i, int j) {
gr.setColor(new Color(0xFFFFFF));
gr.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void DrawLandPart(Graphics gr, int i, int j) {
gr.setColor(new Color(0x050303));
gr.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
}

36
src/SimpleMap.java Normal file
View File

@ -0,0 +1,36 @@
import java.awt.*;
public class SimpleMap extends AbstractMap {
@Override
protected void DrawWaterPart(Graphics gr, int i, int j) {
gr.setColor(new Color(0x5285B6));
gr.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void DrawLandPart(Graphics gr, int i, int j) {
gr.setColor(new Color(0x422A1D));
gr.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@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[i].length; ++j) {
_map[i][j] = _freeWaterArea;
}
}
while (counter < 50) {
int x = _random.nextInt(99);
int y = _random.nextInt(99);
if (_map[x][y] == _freeWaterArea) {
_map[x][y] = _land;
counter++;
}
}
}
}