Compare commits
33 Commits
Author | SHA1 | Date | |
---|---|---|---|
9262f67b28 | |||
dbfe33b361 | |||
2d621bbe70 | |||
8a94c4d44b | |||
ee384c3f2b | |||
0fb5dbace9 | |||
80e51d1ecf | |||
d66ca977a5 | |||
55e0a35928 | |||
89ab094b5b | |||
fdd140a179 | |||
4364071332 | |||
9acaae8ff2 | |||
7611f197fa | |||
b28bb84cf8 | |||
d91e28ae4a | |||
896285b4c5 | |||
95edf9e7d2 | |||
4509648494 | |||
2920306b70 | |||
400672b6dd | |||
18a72916b6 | |||
bf246e17d4 | |||
aba03789ef | |||
8e0a97dd2b | |||
febb4eaa6b | |||
b18173f05a | |||
4526388454 | |||
673ba65efe | |||
c68da6d45b | |||
e9053085ef | |||
885fa94fd3 | |||
8e2dd53ece |
163
AbstractMap.java
Normal file
163
AbstractMap.java
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
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 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, IDrawningObject 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.DrawningObject(gr);
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,3 +1,3 @@
|
|||||||
public enum Direction {
|
public enum Direction {
|
||||||
Up, Down, Left, Right
|
None,Up, Down, Left, Right
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,53 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
class DrawningLocomotive {
|
public class DrawningLocomotive {
|
||||||
public EntityLocomotive Locomotive;
|
public EntityLocomotive Locomotive;
|
||||||
public ExtraWheelsDraw extraWheelsDraw;
|
public IDrawningExtra drawningExtra;
|
||||||
/// Левая координата отрисовки локомотива
|
/// Левая координата отрисовки локомотива
|
||||||
private float _startPosX;
|
protected float _startPosX;
|
||||||
/// Верхняя координата отрисовки локомотива
|
/// Верхняя координата отрисовки локомотива
|
||||||
private float _startPosY;
|
protected float _startPosY;
|
||||||
/// Ширина окна отрисовки
|
/// Ширина окна отрисовки
|
||||||
private Integer _pictureWidth = null;
|
private Integer _pictureWidth = null;
|
||||||
/// Высота окна отрисовки
|
/// Высота окна отрисовки
|
||||||
private Integer _pictureHeight = null;
|
private Integer _pictureHeight = null;
|
||||||
/// Ширина отрисовки локомотива
|
/// Ширина отрисовки локомотива
|
||||||
private final int _locomotiveWidth = 120;
|
private int _locomotiveWidth = 110;
|
||||||
/// Высота отрисовки локомотива
|
/// Высота отрисовки локомотива
|
||||||
private final int _locomotiveHeight = 50;
|
private int _locomotiveHeight = 50;
|
||||||
/// Инициализация свойств
|
/// Инициализация свойств
|
||||||
public void Init(int speed, float weight, Color bodyColor, int wheelsNum, EntityLocomotive entity)
|
private final Random random = new Random();
|
||||||
|
public DrawningLocomotive(int speed, float weight, Color bodyColor)
|
||||||
{
|
{
|
||||||
Locomotive = entity;
|
int randExtra = random.nextInt(2);
|
||||||
extraWheelsDraw = new ExtraWheelsDraw();
|
switch (random.nextInt(3)){
|
||||||
extraWheelsDraw.Init(wheelsNum, bodyColor);
|
case 0:
|
||||||
Locomotive.Init(speed, weight, bodyColor);
|
drawningExtra = new ExtraStarWheelDraw(randExtra, bodyColor);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
drawningExtra = new ExtraRoundWheelDraw(randExtra, bodyColor);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Locomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DrawningLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) {
|
||||||
|
drawningExtra = extra;
|
||||||
|
Locomotive = locomotive;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Новый конструктор
|
||||||
|
protected DrawningLocomotive (int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight)
|
||||||
|
{
|
||||||
|
this(speed, weight, bodyColor);
|
||||||
|
_locomotiveWidth = locomotiveWidth;
|
||||||
|
_locomotiveHeight = locomotiveHeight;
|
||||||
|
}
|
||||||
|
|
||||||
/// Установка позиции локомотива
|
/// Установка позиции локомотива
|
||||||
public void SetPosition(int x, int y, int width, int height)
|
public void SetPosition(int x, int y, int width, int height)
|
||||||
{
|
{
|
||||||
@ -90,18 +115,18 @@ class DrawningLocomotive {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//тело
|
//тело
|
||||||
g.setColor(Color.BLACK);
|
|
||||||
g.drawRect((int)_startPosX , (int)_startPosY, _locomotiveWidth - 20, _locomotiveHeight - 10);
|
|
||||||
//окна
|
|
||||||
g.setColor(Locomotive.getBodyColor());
|
g.setColor(Locomotive.getBodyColor());
|
||||||
|
g.fillRect((int)_startPosX , (int)_startPosY, 110 - 10, 50 - 10);
|
||||||
|
//окна
|
||||||
|
g.setColor(Color.BLUE);
|
||||||
g.fillRect((int)_startPosX + 10, (int)_startPosY + 10, 10, 10);
|
g.fillRect((int)_startPosX + 10, (int)_startPosY + 10, 10, 10);
|
||||||
g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 10);
|
g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 10);
|
||||||
g.fillRect((int)_startPosX + 80, (int)_startPosY + 10, 10, 10);
|
g.fillRect((int)_startPosX + 80, (int)_startPosY + 10, 10, 10);
|
||||||
//дверь
|
//дверь
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
g.drawRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20);
|
g.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20);
|
||||||
//колеса
|
//extra
|
||||||
extraWheelsDraw.DrawWheels((int)_startPosX, (int)_startPosY, g);
|
drawningExtra.DrawExtra((int)_startPosX, (int)_startPosY, g);
|
||||||
//движок
|
//движок
|
||||||
g.setColor(Locomotive.getBodyColor());
|
g.setColor(Locomotive.getBodyColor());
|
||||||
g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30);
|
g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30);
|
||||||
@ -126,4 +151,10 @@ class DrawningLocomotive {
|
|||||||
_startPosY = _pictureHeight - _locomotiveHeight;
|
_startPosY = _pictureHeight - _locomotiveHeight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Получение текущей позиции объекта
|
||||||
|
public float[] GetCurrentPosition()
|
||||||
|
{
|
||||||
|
return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _locomotiveWidth, /*DOWN*/ _startPosY + _locomotiveHeight, /*LEFT*/ _startPosX};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
42
DrawningObjectLocomotive.java
Normal file
42
DrawningObjectLocomotive.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class DrawningObjectLocomotive implements IDrawningObject {
|
||||||
|
private DrawningLocomotive _locomotive = null;
|
||||||
|
public DrawningLocomotive GetDrawningLocomotive() {
|
||||||
|
return _locomotive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawningObjectLocomotive(DrawningLocomotive locomotive)
|
||||||
|
{
|
||||||
|
_locomotive = locomotive;
|
||||||
|
}
|
||||||
|
public float getStep() {
|
||||||
|
if (_locomotive.Locomotive != null) {
|
||||||
|
return _locomotive.Locomotive.Step();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawningObject(Graphics g)
|
||||||
|
{
|
||||||
|
if (_locomotive != null) _locomotive.DrawTransport((Graphics2D) g);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float[] GetCurrentPosition()
|
||||||
|
{
|
||||||
|
if (_locomotive != null) {
|
||||||
|
return _locomotive.GetCurrentPosition();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MoveObject(Direction direction)
|
||||||
|
{
|
||||||
|
if (_locomotive != null) _locomotive.MoveTransport(direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetObject(int x, int y, int width, int height)
|
||||||
|
{
|
||||||
|
if (_locomotive != null) _locomotive.SetPosition(x, y, width, height);
|
||||||
|
}
|
||||||
|
}
|
44
DrawningWarmlyLocomotive.java
Normal file
44
DrawningWarmlyLocomotive.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
public class DrawningWarmlyLocomotive extends DrawningLocomotive{
|
||||||
|
public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean storage)
|
||||||
|
{
|
||||||
|
super(speed, weight, bodyColor, 140, 70);
|
||||||
|
Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawningWarmlyLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) {
|
||||||
|
super(locomotive, extra);
|
||||||
|
Locomotive = locomotive;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void DrawTransport(Graphics2D g)
|
||||||
|
{
|
||||||
|
if (Locomotive instanceof EntityWarmlyLocomotive)
|
||||||
|
{
|
||||||
|
EntityWarmlyLocomotive warmlyLocomotive = (EntityWarmlyLocomotive) Locomotive;
|
||||||
|
|
||||||
|
g.setColor(warmlyLocomotive.ExtraColor);
|
||||||
|
|
||||||
|
if (warmlyLocomotive.Pipe)
|
||||||
|
{
|
||||||
|
g.fillRect((int)_startPosX + 10, (int)_startPosY, 30, 20);
|
||||||
|
g.fillRect((int)_startPosX + 60, (int)_startPosY, 20, 20);
|
||||||
|
g.fillRect((int)_startPosX + 60, (int)_startPosY + 10, 30, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (warmlyLocomotive.FuelStorage)
|
||||||
|
{
|
||||||
|
g.fillRect((int)_startPosX + 120, (int)_startPosY + 10, 10, 50);
|
||||||
|
g.fillRect((int)_startPosX + 110, (int)_startPosY + 40, 20, 20);
|
||||||
|
g.fillRect((int)_startPosX + 110, (int)_startPosY, 30, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
_startPosY += 20;
|
||||||
|
|
||||||
|
super.DrawTransport((Graphics2D)g);
|
||||||
|
|
||||||
|
_startPosY -= 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,7 +19,7 @@ public class EntityLocomotive {
|
|||||||
return Speed * 100 / Weight;
|
return Speed * 100 / Weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init(int speed, float weight, Color bodyColor)
|
public EntityLocomotive(int speed, float weight, Color bodyColor)
|
||||||
{
|
{
|
||||||
Random rnd = new Random();
|
Random rnd = new Random();
|
||||||
if (speed <= 0) {
|
if (speed <= 0) {
|
||||||
|
15
EntityWarmlyLocomotive.java
Normal file
15
EntityWarmlyLocomotive.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class EntityWarmlyLocomotive extends EntityLocomotive{
|
||||||
|
public final Color ExtraColor;
|
||||||
|
public final boolean Pipe;
|
||||||
|
public final boolean FuelStorage;
|
||||||
|
public EntityWarmlyLocomotive (int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean fuelStorage)
|
||||||
|
{
|
||||||
|
super(speed, weight, bodyColor);
|
||||||
|
ExtraColor = extraColor;
|
||||||
|
Pipe = pipe;
|
||||||
|
FuelStorage = fuelStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
44
EntityWithExtraCreator.java
Normal file
44
EntityWithExtraCreator.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class EntityWithExtraCreator <T extends EntityLocomotive, U extends IDrawningExtra> {
|
||||||
|
private final Object[] entityArr;
|
||||||
|
private final Object[] extraArr;
|
||||||
|
|
||||||
|
int entitiesCount = 0;
|
||||||
|
int extraCount = 0;
|
||||||
|
|
||||||
|
public EntityWithExtraCreator(int countEntities, int countExtra) {
|
||||||
|
entityArr = new Object[countEntities];
|
||||||
|
extraArr = new Object[countExtra];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Insert(T entityLocomotive) {
|
||||||
|
if(entitiesCount < entityArr.length) {
|
||||||
|
entityArr[entitiesCount] = entityLocomotive;
|
||||||
|
entitiesCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Insert (U extra) {
|
||||||
|
if(extraCount < extraArr.length) {
|
||||||
|
extraArr[extraCount] = extra;
|
||||||
|
extraCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawningLocomotive getEntityWithExtra() {
|
||||||
|
Random random = new Random();
|
||||||
|
int getEntityRandomIndex = random.nextInt(entityArr.length);
|
||||||
|
int getExtraRandomIndex = random.nextInt(extraArr.length);
|
||||||
|
|
||||||
|
EntityLocomotive locomotive = (T)entityArr[getEntityRandomIndex];
|
||||||
|
IDrawningExtra extra = (U)extraArr[getExtraRandomIndex];
|
||||||
|
|
||||||
|
if (locomotive instanceof EntityWarmlyLocomotive) {
|
||||||
|
return new DrawningWarmlyLocomotive(locomotive, extra);
|
||||||
|
}
|
||||||
|
return new DrawningLocomotive(locomotive, extra);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
43
ExtraRoundWheelDraw.java
Normal file
43
ExtraRoundWheelDraw.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class ExtraRoundWheelDraw implements IDrawningExtra{
|
||||||
|
private WheelsCount wheelsCount = WheelsCount.Two;
|
||||||
|
private ExtraWheelsDraw extraWheelsDraw;
|
||||||
|
public void setExtraNum(int num) {
|
||||||
|
switch (num) {
|
||||||
|
case 0: {
|
||||||
|
wheelsCount = WheelsCount.Three;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 1: {
|
||||||
|
wheelsCount = WheelsCount.Four;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExtraRoundWheelDraw (int num, Color bodyColor) {
|
||||||
|
setExtraNum(num);
|
||||||
|
extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawExtra(int startPosX, int startPosY, Graphics2D g) {
|
||||||
|
extraWheelsDraw.DrawExtra(startPosX, startPosY, g);
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.fillOval(startPosX + 5, startPosY + 35, 10, 10);
|
||||||
|
g.fillOval(startPosX + 95, startPosY + 35, 10, 10);
|
||||||
|
switch (wheelsCount) {
|
||||||
|
case Four: {
|
||||||
|
g.fillOval(startPosX + 75, startPosY + 35, 10, 10);
|
||||||
|
}
|
||||||
|
case Three: {
|
||||||
|
g.fillOval(startPosX + 25, startPosY + 35, 10, 10);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
50
ExtraStarWheelDraw.java
Normal file
50
ExtraStarWheelDraw.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class ExtraStarWheelDraw implements IDrawningExtra{
|
||||||
|
private WheelsCount wheelsCount = WheelsCount.Two;
|
||||||
|
private ExtraWheelsDraw extraWheelsDraw;
|
||||||
|
public void setExtraNum(int num) {
|
||||||
|
switch (num) {
|
||||||
|
case 0: {
|
||||||
|
wheelsCount = WheelsCount.Three;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 1: {
|
||||||
|
wheelsCount = WheelsCount.Four;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExtraStarWheelDraw (int num, Color bodyColor) {
|
||||||
|
setExtraNum(num);
|
||||||
|
extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawExtra(int startPosX, int startPosY, Graphics2D g) {
|
||||||
|
extraWheelsDraw.DrawExtra(startPosX, startPosY, g);
|
||||||
|
DrawStarOnWheel(startPosX, startPosY + 30, g);
|
||||||
|
DrawStarOnWheel(startPosX + 90, startPosY + 30, g);
|
||||||
|
switch (wheelsCount) {
|
||||||
|
case Four: {
|
||||||
|
DrawStarOnWheel(startPosX + 70, startPosY + 30, g);
|
||||||
|
}
|
||||||
|
case Three: {
|
||||||
|
DrawStarOnWheel(startPosX + 20, startPosY + 30, g);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawStarOnWheel(int startPosX, int startPosY, Graphics2D g) {
|
||||||
|
g.drawLine(startPosX + 10, startPosY, startPosX + 15, startPosY + 17);
|
||||||
|
g.drawLine(startPosX + 10, startPosY, startPosX + 5, startPosY + 17);
|
||||||
|
g.drawLine(startPosX + 15, startPosY + 17, startPosX + 2, startPosY + 8);
|
||||||
|
g.drawLine(startPosX + 5, startPosY + 17, startPosX + 18, startPosY + 8);
|
||||||
|
g.drawLine(startPosX + 2, startPosY + 8, startPosX + 18, startPosY + 8);
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
public class ExtraWheelsDraw {
|
public class ExtraWheelsDraw implements IDrawningExtra{
|
||||||
private WheelsCount wheelsCount = WheelsCount.Two;
|
private WheelsCount wheelsCount = WheelsCount.Two;
|
||||||
public void setWheelsNum(int num) {
|
public void setExtraNum(int num) {
|
||||||
switch (num) {
|
switch (num) {
|
||||||
case 0: {
|
case 0: {
|
||||||
wheelsCount = WheelsCount.Three;
|
wheelsCount = WheelsCount.Three;
|
||||||
@ -18,21 +18,29 @@ public class ExtraWheelsDraw {
|
|||||||
}
|
}
|
||||||
private Color color;
|
private Color color;
|
||||||
|
|
||||||
public void Init(int num, Color color) {
|
public ExtraWheelsDraw(int num, Color color) {
|
||||||
setWheelsNum(num);
|
setExtraNum(num);
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawWheels(int startPosX, int startPosY, Graphics2D g) {
|
public void DrawExtra(int startPosX, int startPosY, Graphics2D g) {
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.drawOval(startPosX, startPosY + 30, 20, 20);
|
||||||
|
g.drawOval(startPosX + 90, startPosY + 30, 20, 20);
|
||||||
g.setColor(color);
|
g.setColor(color);
|
||||||
g.drawOval(startPosX, startPosY + 40, 10, 10);
|
g.fillOval(startPosX, startPosY + 30, 20, 20);
|
||||||
g.drawOval(startPosX + 90, startPosY + 40, 10, 10);
|
g.fillOval(startPosX + 90, startPosY + 30, 20, 20);
|
||||||
switch (wheelsCount) {
|
switch (wheelsCount) {
|
||||||
case Four: {
|
case Four: {
|
||||||
g.drawOval(startPosX + 70, startPosY + 40, 10, 10);
|
g.setColor(Color.BLACK);
|
||||||
|
g.drawOval(startPosX + 70, startPosY + 30, 20, 20);
|
||||||
|
g.setColor(color);
|
||||||
|
g.fillOval(startPosX + 70, startPosY + 30, 20, 20);
|
||||||
}
|
}
|
||||||
case Three: {
|
case Three: {
|
||||||
g.drawOval(startPosX + 20, startPosY + 40, 10, 10);
|
g.fillOval(startPosX + 20, startPosY + 30, 20, 20);
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.drawOval(startPosX + 20, startPosY + 30, 20, 20);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
87
FormEntityWithExtraGallery.java
Normal file
87
FormEntityWithExtraGallery.java
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class FormEntityWithExtraGallery extends JComponent {
|
||||||
|
private DrawningLocomotive _locomotiveFirst;
|
||||||
|
private DrawningLocomotive _locomotiveSecond;
|
||||||
|
private DrawningLocomotive _locomotiveThird;
|
||||||
|
EntityWithExtraCreator<EntityLocomotive, IDrawningExtra> entityWithExtraCreator;
|
||||||
|
|
||||||
|
public FormEntityWithExtraGallery() {
|
||||||
|
JFrame formFrame = new JFrame("Gallery");
|
||||||
|
formFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||||
|
formFrame.setSize(900, 500);
|
||||||
|
formFrame.setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
Panel statusPanel = new Panel();
|
||||||
|
statusPanel.setBackground(Color.WHITE);
|
||||||
|
statusPanel.setLayout(new FlowLayout());
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
add(statusPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
JButton showRandomEntity = new JButton("Create entity from parts");
|
||||||
|
showRandomEntity.addActionListener(e -> {
|
||||||
|
|
||||||
|
Random random = new Random();
|
||||||
|
if (entityWithExtraCreator == null) {
|
||||||
|
entityWithExtraCreator = new EntityWithExtraCreator<EntityLocomotive, IDrawningExtra>(20, 20);
|
||||||
|
for (int i = 0; i < 20; i ++) {
|
||||||
|
if (random.nextBoolean()) {
|
||||||
|
entityWithExtraCreator.Insert(new EntityLocomotive(random.nextInt(100), random.nextInt(100),
|
||||||
|
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
entityWithExtraCreator.Insert(new EntityWarmlyLocomotive(random.nextInt(100), random.nextInt(100),
|
||||||
|
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)),
|
||||||
|
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)),
|
||||||
|
random.nextBoolean(), random.nextBoolean()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 20; i ++) {
|
||||||
|
int extraRand = random.nextInt(3);
|
||||||
|
switch (extraRand) {
|
||||||
|
case 0:
|
||||||
|
entityWithExtraCreator.Insert(new ExtraWheelsDraw(random.nextInt(3),
|
||||||
|
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
entityWithExtraCreator.Insert(new ExtraStarWheelDraw(random.nextInt(3),
|
||||||
|
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
entityWithExtraCreator.Insert(new ExtraRoundWheelDraw(random.nextInt(3),
|
||||||
|
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_locomotiveFirst = entityWithExtraCreator.getEntityWithExtra();
|
||||||
|
_locomotiveFirst.SetPosition(200, 200, formFrame.getWidth(), formFrame.getHeight() - 75);
|
||||||
|
|
||||||
|
_locomotiveSecond = entityWithExtraCreator.getEntityWithExtra();
|
||||||
|
_locomotiveSecond.SetPosition(400, 200, formFrame.getWidth(), formFrame.getHeight() - 75);
|
||||||
|
|
||||||
|
_locomotiveThird = entityWithExtraCreator.getEntityWithExtra();
|
||||||
|
_locomotiveThird.SetPosition(600, 200, formFrame.getWidth(), formFrame.getHeight() - 75);
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
statusPanel.add(showRandomEntity);
|
||||||
|
|
||||||
|
formFrame.getContentPane().add(this);
|
||||||
|
|
||||||
|
formFrame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2 = (Graphics2D)g;
|
||||||
|
if (_locomotiveFirst != null) _locomotiveFirst.DrawTransport(g2);
|
||||||
|
if (_locomotiveSecond != null) _locomotiveSecond.DrawTransport(g2);
|
||||||
|
if (_locomotiveThird != null) _locomotiveThird.DrawTransport(g2);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
}
|
@ -1,31 +1,18 @@
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class FormLocomotive extends JComponent{
|
public class FormLocomotive extends JComponent{
|
||||||
private DrawningLocomotive _locomotive;
|
private DrawningLocomotive _locomotive;
|
||||||
private EntityLocomotive _entity;
|
private DrawningLocomotive SelectedLocomotive;
|
||||||
public FormLocomotive() {
|
public DrawningLocomotive getSelectedLocomotive() {
|
||||||
JFrame formFrame = new JFrame("Locomotive");
|
return SelectedLocomotive;
|
||||||
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
}
|
||||||
formFrame.setSize(800, 500);
|
public void SetLocomotive(DrawningObjectLocomotive locomotive){
|
||||||
formFrame.setVisible(true);
|
_locomotive = locomotive.GetDrawningLocomotive();
|
||||||
formFrame.setLocationRelativeTo(null);
|
repaint();
|
||||||
|
}
|
||||||
formFrame.addComponentListener(new ComponentListener() {
|
public FormLocomotive(JDialog caller) {
|
||||||
@Override
|
|
||||||
public void componentResized(ComponentEvent e) {
|
|
||||||
if (_locomotive != null) _locomotive.ChangeBorders(formFrame.getWidth(), formFrame.getHeight());
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void componentMoved(ComponentEvent e) {}
|
|
||||||
@Override
|
|
||||||
public void componentShown(ComponentEvent e) {}
|
|
||||||
@Override
|
|
||||||
public void componentHidden(ComponentEvent e) {}
|
|
||||||
});
|
|
||||||
|
|
||||||
Panel statusPanel = new Panel();
|
Panel statusPanel = new Panel();
|
||||||
statusPanel.setBackground(Color.WHITE);
|
statusPanel.setBackground(Color.WHITE);
|
||||||
@ -40,40 +27,68 @@ public class FormLocomotive extends JComponent{
|
|||||||
JButton createButton = new JButton("Create");
|
JButton createButton = new JButton("Create");
|
||||||
createButton.addActionListener(e -> {
|
createButton.addActionListener(e -> {
|
||||||
Random rnd = new Random();
|
Random rnd = new Random();
|
||||||
_locomotive = new DrawningLocomotive();
|
|
||||||
_entity = new EntityLocomotive();
|
Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
|
||||||
_locomotive.Init(100 + rnd.nextInt(200), 1000 + rnd.nextInt(1000), Color.getHSBColor(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextInt(3), _entity);
|
|
||||||
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75);
|
_locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst);
|
||||||
|
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-75);
|
||||||
speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed());
|
speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed());
|
||||||
weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight());
|
weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight());
|
||||||
colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() );
|
colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() );
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
JButton modifiedButton = new JButton("Modified");
|
||||||
|
modifiedButton.addActionListener(e -> {
|
||||||
|
Random rnd = new Random();
|
||||||
|
|
||||||
|
Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
|
||||||
|
Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
|
||||||
|
|
||||||
|
_locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
|
||||||
|
colorFirst,
|
||||||
|
colorSecond,
|
||||||
|
rnd.nextBoolean(),
|
||||||
|
rnd.nextBoolean());
|
||||||
|
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75);
|
||||||
|
speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed());
|
||||||
|
weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight());
|
||||||
|
colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() );
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
JButton selectLocomotiveButton = new JButton("Select");
|
||||||
|
selectLocomotiveButton.addActionListener(e -> {
|
||||||
|
SelectedLocomotive = _locomotive;
|
||||||
|
caller.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
statusPanel.add(createButton);
|
statusPanel.add(createButton);
|
||||||
|
statusPanel.add(modifiedButton);
|
||||||
|
statusPanel.add(selectLocomotiveButton);
|
||||||
statusPanel.add(speedLabel);
|
statusPanel.add(speedLabel);
|
||||||
statusPanel.add(weightLabel);
|
statusPanel.add(weightLabel);
|
||||||
statusPanel.add(colorLabel);
|
statusPanel.add(colorLabel);
|
||||||
|
|
||||||
JButton moveDownButton = new JButton("Down");
|
JButton moveDownButton = new JButton("D");
|
||||||
moveDownButton.addActionListener(e -> {
|
moveDownButton.addActionListener(e -> {
|
||||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Down);
|
if (_locomotive != null) _locomotive.MoveTransport(Direction.Down);
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
|
|
||||||
JButton moveUpButton = new JButton("Up");
|
JButton moveUpButton = new JButton("U");
|
||||||
moveUpButton.addActionListener(e -> {
|
moveUpButton.addActionListener(e -> {
|
||||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Up);
|
if (_locomotive != null) _locomotive.MoveTransport(Direction.Up);
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
|
|
||||||
JButton moveLeftButton = new JButton("Left");
|
JButton moveLeftButton = new JButton("L");
|
||||||
moveLeftButton.addActionListener(e -> {
|
moveLeftButton.addActionListener(e -> {
|
||||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Left);
|
if (_locomotive != null) _locomotive.MoveTransport(Direction.Left);
|
||||||
repaint();
|
repaint();
|
||||||
});
|
});
|
||||||
|
|
||||||
JButton moveRightButton = new JButton("Right");
|
JButton moveRightButton = new JButton("R");
|
||||||
moveRightButton.addActionListener(e -> {
|
moveRightButton.addActionListener(e -> {
|
||||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Right);
|
if (_locomotive != null) _locomotive.MoveTransport(Direction.Right);
|
||||||
repaint();
|
repaint();
|
||||||
@ -83,10 +98,7 @@ public class FormLocomotive extends JComponent{
|
|||||||
statusPanel.add(moveDownButton);
|
statusPanel.add(moveDownButton);
|
||||||
statusPanel.add(moveLeftButton);
|
statusPanel.add(moveLeftButton);
|
||||||
statusPanel.add(moveRightButton);
|
statusPanel.add(moveRightButton);
|
||||||
|
|
||||||
formFrame.getContentPane().add(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
@ -94,8 +106,7 @@ public class FormLocomotive extends JComponent{
|
|||||||
if (_locomotive != null) _locomotive.DrawTransport(g2);
|
if (_locomotive != null) _locomotive.DrawTransport(g2);
|
||||||
super.repaint();
|
super.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
FormLocomotive formLocomotive = new FormLocomotive();
|
new FormMapWithSetLocomotives();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
129
FormMap.java
Normal file
129
FormMap.java
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
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",
|
||||||
|
"Spike Map",
|
||||||
|
"Rail 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 ("Spike Map"):
|
||||||
|
_abstractMap = new SpikeMap();
|
||||||
|
break;
|
||||||
|
case ("Rail Map"):
|
||||||
|
_abstractMap = new RailMap();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
JButton createButton = new JButton("Create");
|
||||||
|
createButton.addActionListener(e -> {
|
||||||
|
Random rnd = new Random();
|
||||||
|
var locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
||||||
|
speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed());
|
||||||
|
weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight());
|
||||||
|
colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue());
|
||||||
|
if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawningObjectLocomotive(locomotive));
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
JButton modifiedButton = new JButton("Modified");
|
||||||
|
modifiedButton.addActionListener(e -> {
|
||||||
|
Random rnd = new Random();
|
||||||
|
var locomotive = new DrawningWarmlyLocomotive(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: " + locomotive.Locomotive.getSpeed());
|
||||||
|
weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight());
|
||||||
|
colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue() );
|
||||||
|
if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawningObjectLocomotive(locomotive));
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
300
FormMapWithSetLocomotives.java
Normal file
300
FormMapWithSetLocomotives.java
Normal file
@ -0,0 +1,300 @@
|
|||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.text.MaskFormatter;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class FormMapWithSetLocomotives extends JComponent {
|
||||||
|
private BufferedImage bufferImg = null;
|
||||||
|
/// Словарь для выпадающего списка
|
||||||
|
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {
|
||||||
|
{
|
||||||
|
put("Simple Map", new SimpleMap());
|
||||||
|
put("Spike Map", new SpikeMap());
|
||||||
|
put("Rail Map", new RailMap());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/// Объект от коллекции карт
|
||||||
|
private final MapsCollection _mapsCollection;
|
||||||
|
|
||||||
|
// Элементы на форме
|
||||||
|
JFrame formFrame;
|
||||||
|
Panel statusPanel;
|
||||||
|
TextField textFieldNewMapName;
|
||||||
|
JComboBox mapSelectComboBox;
|
||||||
|
DefaultListModel<String> mapsListModel = new DefaultListModel<>();
|
||||||
|
JScrollPane listScroller = new JScrollPane();
|
||||||
|
JList listBoxMaps;
|
||||||
|
|
||||||
|
public FormMapWithSetLocomotives() {
|
||||||
|
formFrame = new JFrame("Form Map With SetLocomotives");
|
||||||
|
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
formFrame.setSize(750, 500);
|
||||||
|
formFrame.setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
statusPanel = new Panel();
|
||||||
|
statusPanel.setBackground(Color.WHITE);
|
||||||
|
statusPanel.setLayout(new GridLayout(0, 1, 20, 5));
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
add(statusPanel, BorderLayout.EAST);
|
||||||
|
|
||||||
|
// Текстовое поле для ввода названия карты
|
||||||
|
textFieldNewMapName = new TextField();
|
||||||
|
statusPanel.add(textFieldNewMapName);
|
||||||
|
|
||||||
|
// КомбоБокс с картами
|
||||||
|
String[] maps = {
|
||||||
|
"Simple Map",
|
||||||
|
"Spike Map",
|
||||||
|
"Rail Map"
|
||||||
|
};
|
||||||
|
mapSelectComboBox = new JComboBox(maps);
|
||||||
|
mapSelectComboBox.setEditable(true);
|
||||||
|
statusPanel.add(mapSelectComboBox);
|
||||||
|
|
||||||
|
// Initialization
|
||||||
|
_mapsCollection = new MapsCollection(600, 500);
|
||||||
|
mapSelectComboBox.removeAllItems();
|
||||||
|
for (var elem : _mapsDict.keySet())
|
||||||
|
{
|
||||||
|
mapSelectComboBox.addItem(elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Кнопка добавления карты
|
||||||
|
JButton addMapButton = new JButton("Add Map");
|
||||||
|
addMapButton.addActionListener(e -> {
|
||||||
|
// логика добавления
|
||||||
|
if (mapSelectComboBox.getSelectedIndex() == -1 || textFieldNewMapName.getText() == null)
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "Not all data is complete!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!_mapsDict.containsKey(mapSelectComboBox.getSelectedItem().toString()))
|
||||||
|
{
|
||||||
|
JOptionPane.showMessageDialog(null, "No such map");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
|
||||||
|
ReloadMaps();
|
||||||
|
});
|
||||||
|
statusPanel.add(addMapButton);
|
||||||
|
|
||||||
|
// ListBox для созданных карт
|
||||||
|
listBoxMaps = new JList(mapsListModel);
|
||||||
|
listBoxMaps.addListSelectionListener(e -> {
|
||||||
|
if(listBoxMaps.getSelectedValue() == null) return;
|
||||||
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
statusPanel.add(listBoxMaps);
|
||||||
|
|
||||||
|
listScroller.setViewportView(listBoxMaps);
|
||||||
|
listBoxMaps.setLayoutOrientation(JList.VERTICAL);
|
||||||
|
statusPanel.add(listScroller);
|
||||||
|
|
||||||
|
// Кнопка для удаления карты
|
||||||
|
JButton deleteMapButton = new JButton("Delete Map");
|
||||||
|
deleteMapButton.addActionListener(e -> {
|
||||||
|
// логика удаления
|
||||||
|
if (listBoxMaps.getSelectedIndex() == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
||||||
|
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
|
||||||
|
ReloadMaps();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
statusPanel.add(deleteMapButton);
|
||||||
|
|
||||||
|
// Кнопка добавления локомотива
|
||||||
|
JButton addLocomotiveButton = new JButton("Add Locomotive");
|
||||||
|
addLocomotiveButton.addActionListener(e -> {
|
||||||
|
// логика добавления
|
||||||
|
if (listBoxMaps.getSelectedIndex() == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
JDialog dialog = new JDialog(formFrame, "Dialog", true);
|
||||||
|
FormLocomotive formLocomotive = new FormLocomotive(dialog);
|
||||||
|
dialog.setSize(800, 500);
|
||||||
|
dialog.setContentPane(formLocomotive);
|
||||||
|
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||||
|
dialog.setVisible(true);
|
||||||
|
DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive());
|
||||||
|
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(locomotive)!= -1) {
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
|
||||||
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
statusPanel.add(addLocomotiveButton);
|
||||||
|
|
||||||
|
// Текстовое поле для ввода позиции с маской
|
||||||
|
JFormattedTextField maskedTextFieldPosition = null;
|
||||||
|
try {
|
||||||
|
MaskFormatter positionMask = new MaskFormatter("##");
|
||||||
|
maskedTextFieldPosition = new JFormattedTextField(positionMask);
|
||||||
|
statusPanel.add(maskedTextFieldPosition);
|
||||||
|
}
|
||||||
|
catch (ParseException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
//Кнопка удаления локомотива
|
||||||
|
JButton deleteLocomotiveButton = new JButton("Delete Locomotive");
|
||||||
|
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
|
||||||
|
deleteLocomotiveButton.addActionListener(e -> {
|
||||||
|
// логика удаления
|
||||||
|
if ((String)mapSelectComboBox.getSelectedItem() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (finalMaskedTextFieldPosition == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
|
||||||
|
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) {
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
|
||||||
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
statusPanel.add(deleteLocomotiveButton);
|
||||||
|
//Кнопка просмотра хранилища
|
||||||
|
JButton showStorageButton = new JButton("Show Storage");
|
||||||
|
showStorageButton.addActionListener(e -> {
|
||||||
|
// логика просмотра
|
||||||
|
if (listBoxMaps.getSelectedIndex() == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
statusPanel.add(showStorageButton);
|
||||||
|
//Кнопка просмотра карты
|
||||||
|
JButton showOnMapButton = new JButton("Show On Map");
|
||||||
|
showOnMapButton.addActionListener(e -> {
|
||||||
|
// логика просмотра
|
||||||
|
if (listBoxMaps.getSelectedIndex() == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
|
||||||
|
repaint();
|
||||||
|
});
|
||||||
|
statusPanel.add(showOnMapButton);
|
||||||
|
|
||||||
|
ActionListener moveButtonListener = new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (listBoxMaps.getSelectedIndex() == -1)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String name = e.getActionCommand();
|
||||||
|
Direction dir = Direction.None;
|
||||||
|
switch (name)
|
||||||
|
{
|
||||||
|
case "Up":
|
||||||
|
dir = Direction.Up;
|
||||||
|
break;
|
||||||
|
case "Down":
|
||||||
|
dir = Direction.Down;
|
||||||
|
break;
|
||||||
|
case "Left":
|
||||||
|
dir = Direction.Left;
|
||||||
|
break;
|
||||||
|
case "Right":
|
||||||
|
dir = Direction.Right;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).MoveObject(dir);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//Кнопки управления
|
||||||
|
JButton moveDownButton = new JButton("Down");
|
||||||
|
moveDownButton.addActionListener(moveButtonListener);
|
||||||
|
|
||||||
|
JButton moveUpButton = new JButton("Up");
|
||||||
|
moveUpButton.addActionListener(moveButtonListener);
|
||||||
|
|
||||||
|
JButton moveLeftButton = new JButton("Left");
|
||||||
|
moveLeftButton.addActionListener(moveButtonListener);
|
||||||
|
|
||||||
|
JButton moveRightButton = new JButton("Right");
|
||||||
|
moveRightButton.addActionListener(moveButtonListener);
|
||||||
|
|
||||||
|
statusPanel.add(moveUpButton);
|
||||||
|
statusPanel.add(moveDownButton);
|
||||||
|
statusPanel.add(moveLeftButton);
|
||||||
|
statusPanel.add(moveRightButton);
|
||||||
|
|
||||||
|
JButton showGalleryButton = new JButton("Show Gallery");
|
||||||
|
showGalleryButton.addActionListener(e -> {
|
||||||
|
new FormEntityWithExtraGallery();
|
||||||
|
});
|
||||||
|
statusPanel.add(showGalleryButton);
|
||||||
|
|
||||||
|
// Кнопка показа удаленных объектов
|
||||||
|
JButton showDeletedButton = new JButton("Show Deleted");
|
||||||
|
showDeletedButton.addActionListener(e -> {
|
||||||
|
// По отдельной кнопке вызывать форму работы с объектом (из
|
||||||
|
//первой лабораторной), передавая туда элемент из коллекции
|
||||||
|
//удаленных, если там есть
|
||||||
|
DrawningObjectLocomotive locomotive = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).getDeleted();
|
||||||
|
if (locomotive == null) {
|
||||||
|
JOptionPane.showMessageDialog(null, "No deleted objects");
|
||||||
|
}
|
||||||
|
if (locomotive != null) {
|
||||||
|
JDialog dialog = new JDialog(formFrame, "Deleted", true);
|
||||||
|
FormLocomotive formLocomotive = new FormLocomotive(dialog);
|
||||||
|
formLocomotive.SetLocomotive(locomotive);
|
||||||
|
dialog.setSize(800, 500);
|
||||||
|
dialog.setContentPane(formLocomotive);
|
||||||
|
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||||
|
dialog.setVisible(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
statusPanel.add(showDeletedButton);
|
||||||
|
|
||||||
|
formFrame.getContentPane().add(this);
|
||||||
|
formFrame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReloadMaps(){
|
||||||
|
int index = listBoxMaps.getSelectedIndex();
|
||||||
|
listBoxMaps.removeAll();
|
||||||
|
mapsListModel.removeAllElements();
|
||||||
|
for (int i = 0; i < _mapsCollection.keys().size(); i++){
|
||||||
|
mapsListModel.addElement(_mapsCollection.keys().get(i));
|
||||||
|
}
|
||||||
|
if (mapsListModel.size() > 0 && (index == -1 || index >= mapsListModel.size())){
|
||||||
|
listBoxMaps.setSelectedIndex(0);
|
||||||
|
}
|
||||||
|
else if (mapsListModel.size() > 0 && index > -1 && index < mapsListModel.size())
|
||||||
|
{
|
||||||
|
listBoxMaps.setSelectedIndex(index);
|
||||||
|
}
|
||||||
|
listBoxMaps.setModel(mapsListModel);
|
||||||
|
statusPanel.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
Graphics2D g2 = (Graphics2D)g;
|
||||||
|
if (bufferImg != null) g2.drawImage(bufferImg, 0,0,600,500,null);
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
6
IDrawningExtra.java
Normal file
6
IDrawningExtra.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public interface IDrawningExtra {
|
||||||
|
void setExtraNum(int num);
|
||||||
|
void DrawExtra(int startPosX, int startPosY, Graphics2D g);
|
||||||
|
}
|
17
IDrawningObject.java
Normal file
17
IDrawningObject.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
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();
|
||||||
|
//0 - up
|
||||||
|
//1 - right
|
||||||
|
//2 - down
|
||||||
|
//3 - left
|
||||||
|
}
|
177
MapWithSetLocomotivesGeneric.java
Normal file
177
MapWithSetLocomotivesGeneric.java
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class MapWithSetLocomotivesGeneric
|
||||||
|
<T extends IDrawningObject, U extends AbstractMap>
|
||||||
|
{
|
||||||
|
/// Ширина окна отрисовки
|
||||||
|
private final int _pictureWidth;
|
||||||
|
/// Высота окна отрисовки
|
||||||
|
private final int _pictureHeight;
|
||||||
|
/// Размер занимаемого объектом места (ширина)
|
||||||
|
private final int _placeSizeWidth = 210;
|
||||||
|
/// Размер занимаемого объектом места (высота)
|
||||||
|
private final int _placeSizeHeight = 90;
|
||||||
|
public final SetLocomotivesGeneric<T> _setLocomotives;
|
||||||
|
|
||||||
|
// Список удаленных объектов
|
||||||
|
LinkedList<DrawningObjectLocomotive> deletedLocomotives = new LinkedList<>();
|
||||||
|
|
||||||
|
public DrawningObjectLocomotive getDeleted() {
|
||||||
|
if (deletedLocomotives.isEmpty()) return null;
|
||||||
|
return deletedLocomotives.removeFirst();
|
||||||
|
}
|
||||||
|
/// Карта
|
||||||
|
private final U _map;
|
||||||
|
/// Конструктор
|
||||||
|
public MapWithSetLocomotivesGeneric(int picWidth, int picHeight, U map)
|
||||||
|
{
|
||||||
|
int width = picWidth / _placeSizeWidth;
|
||||||
|
int height = picHeight / _placeSizeHeight;
|
||||||
|
_setLocomotives = new SetLocomotivesGeneric<T>(width * height);
|
||||||
|
_pictureWidth = picWidth;
|
||||||
|
_pictureHeight = picHeight;
|
||||||
|
_map = map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Добавление
|
||||||
|
public int Plus(T locomotive)
|
||||||
|
{
|
||||||
|
return this._setLocomotives.Insert(locomotive);
|
||||||
|
}
|
||||||
|
/// Удаление
|
||||||
|
public T Minus(int position)
|
||||||
|
{
|
||||||
|
T temp = this._setLocomotives.Remove(position);
|
||||||
|
if (temp == null) return null;
|
||||||
|
deletedLocomotives.add((DrawningObjectLocomotive) temp);
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
/// Вывод всего набора объектов
|
||||||
|
public BufferedImage ShowSet()
|
||||||
|
{
|
||||||
|
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics gr = bmp.getGraphics();
|
||||||
|
DrawBackground((Graphics2D)gr);
|
||||||
|
DrawLocomotives((Graphics2D)gr);
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Просмотр объекта на карте
|
||||||
|
public BufferedImage ShowOnMap()
|
||||||
|
{
|
||||||
|
Shaking();
|
||||||
|
for (var locomotive : _setLocomotives.GetLocomotives())
|
||||||
|
{
|
||||||
|
if (locomotive != null)
|
||||||
|
{
|
||||||
|
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Перемещение объекта по крате
|
||||||
|
public BufferedImage MoveObject(Direction direction)
|
||||||
|
{
|
||||||
|
if (_map != null)
|
||||||
|
{
|
||||||
|
return _map.MoveObject(direction);
|
||||||
|
}
|
||||||
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
||||||
|
private void Shaking()
|
||||||
|
{
|
||||||
|
int j = _setLocomotives.Count() - 1;
|
||||||
|
for (int i = 0; i < _setLocomotives.Count(); i++)
|
||||||
|
{
|
||||||
|
if (_setLocomotives.Get(i) == null)
|
||||||
|
{
|
||||||
|
for (; j > i; j--)
|
||||||
|
{
|
||||||
|
var locomotive = _setLocomotives.Get(j);
|
||||||
|
if (locomotive != null)
|
||||||
|
{
|
||||||
|
_setLocomotives.Insert(locomotive, i);
|
||||||
|
_setLocomotives.Remove(j);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j <= i)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Метод отрисовки фона
|
||||||
|
private void DrawBackground(Graphics2D g)
|
||||||
|
{
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
g.fillRect(0,0,600, 500);
|
||||||
|
for (int j = _placeSizeHeight; j < _pictureHeight; j+= _placeSizeHeight)
|
||||||
|
{
|
||||||
|
//нижняя линия рельс
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.setStroke(new BasicStroke(5));
|
||||||
|
|
||||||
|
g.drawLine(0, j, _pictureWidth, j);
|
||||||
|
for (int i = 0; i < _pictureWidth; i+=20)
|
||||||
|
{
|
||||||
|
g.drawLine(i, j, i, j + 10);
|
||||||
|
}
|
||||||
|
g.drawLine(0, j + 10, _pictureWidth, j + 10);
|
||||||
|
|
||||||
|
//верхняя линия рельс
|
||||||
|
|
||||||
|
g.setColor(Color.GRAY);
|
||||||
|
|
||||||
|
g.drawLine(0, j - 20, _pictureWidth, j - 20);
|
||||||
|
for (int i = 0; i < _pictureWidth; i += 20)
|
||||||
|
{
|
||||||
|
g.drawLine(i, j - 20, i, j - 10);
|
||||||
|
}
|
||||||
|
g.drawLine(0, j - 10, _pictureWidth, j - 10);
|
||||||
|
|
||||||
|
//фонари
|
||||||
|
for (int i = _placeSizeWidth; i < _pictureWidth; i += _placeSizeWidth)
|
||||||
|
{
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.setStroke(new BasicStroke(10));
|
||||||
|
g.drawLine(i, j - _placeSizeHeight + 20, i, j);
|
||||||
|
g.setColor(Color.YELLOW);
|
||||||
|
g.setStroke(new BasicStroke(20));
|
||||||
|
g.drawLine(i, j - _placeSizeHeight + 18, i, j - _placeSizeHeight + 38);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g.setStroke(new BasicStroke(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Метод прорисовки объектов
|
||||||
|
private void DrawLocomotives(Graphics2D g)
|
||||||
|
{
|
||||||
|
int width = _pictureWidth / _placeSizeWidth;
|
||||||
|
int height = _pictureHeight / _placeSizeHeight;
|
||||||
|
|
||||||
|
int curWidth = 0;
|
||||||
|
int curHeight = 0;
|
||||||
|
|
||||||
|
for (var locomotive : _setLocomotives.GetLocomotives())
|
||||||
|
{
|
||||||
|
// установка позиции
|
||||||
|
if (locomotive != null) locomotive.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight);
|
||||||
|
if (locomotive != null) locomotive.DrawningObject(g);
|
||||||
|
if (curWidth < width) curWidth++;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
curWidth = 0;
|
||||||
|
curHeight++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
47
MapsCollection.java
Normal file
47
MapsCollection.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class MapsCollection {
|
||||||
|
/// Словарь (хранилище) с картами
|
||||||
|
final HashMap<String, MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>> _mapStorages;
|
||||||
|
|
||||||
|
/// Возвращение списка названий карт
|
||||||
|
public ArrayList<String> keys() {
|
||||||
|
return new ArrayList<String>(_mapStorages.keySet());
|
||||||
|
}
|
||||||
|
/// Ширина окна отрисовки
|
||||||
|
private final int _pictureWidth;
|
||||||
|
/// Высота окна отрисовки
|
||||||
|
private final int _pictureHeight;
|
||||||
|
/// Конструктор
|
||||||
|
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||||
|
{
|
||||||
|
_mapStorages = new HashMap<String, MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>>();
|
||||||
|
_pictureWidth = pictureWidth;
|
||||||
|
_pictureHeight = pictureHeight;
|
||||||
|
}
|
||||||
|
/// Добавление карты
|
||||||
|
public void AddMap(String name, AbstractMap map)
|
||||||
|
{
|
||||||
|
// Логика для добавления
|
||||||
|
if (!_mapStorages.containsKey(name)) _mapStorages.put(name, new MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||||
|
}
|
||||||
|
/// Удаление карты
|
||||||
|
public void DelMap(String name)
|
||||||
|
{
|
||||||
|
// Логика для удаления
|
||||||
|
if (_mapStorages.containsKey(name)) _mapStorages.remove(name);
|
||||||
|
}
|
||||||
|
/// Доступ к парковке
|
||||||
|
public MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> Get(String ind)
|
||||||
|
{
|
||||||
|
// Логика получения объекта
|
||||||
|
if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// Доп.индексатор из задания
|
||||||
|
public DrawningObjectLocomotive Get (String name, int position) {
|
||||||
|
if (_mapStorages.containsKey(name)) return _mapStorages.get(name)._setLocomotives.Get(position);
|
||||||
|
else return null;
|
||||||
|
}
|
||||||
|
}
|
58
RailMap.java
Normal file
58
RailMap.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class RailMap extends AbstractMap{
|
||||||
|
/// Цвет участка закрытого
|
||||||
|
private final Color barrierColor = Color.BLACK;
|
||||||
|
/// Цвет участка открытого
|
||||||
|
private final Color roadColor = Color.PINK;
|
||||||
|
|
||||||
|
@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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 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 < 1)
|
||||||
|
{
|
||||||
|
int y = _random.nextInt(85);
|
||||||
|
|
||||||
|
for(int x = 0; x < 99; x++)
|
||||||
|
{
|
||||||
|
_map[x][y] = _barrier;
|
||||||
|
_map[x][y + 5] = _barrier;
|
||||||
|
|
||||||
|
if (x % 5 == 0)
|
||||||
|
{
|
||||||
|
_map[x][y + 1] = _barrier;
|
||||||
|
_map[x][y + 2] = _barrier;
|
||||||
|
_map[x][y + 3] = _barrier;
|
||||||
|
_map[x][y + 4] = _barrier;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
counter += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
49
SetLocomotivesGeneric.java
Normal file
49
SetLocomotivesGeneric.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class SetLocomotivesGeneric <T>
|
||||||
|
{
|
||||||
|
/// Список хранимых объектов
|
||||||
|
private final ArrayList<T> _places;
|
||||||
|
|
||||||
|
public int Count() {
|
||||||
|
return _places.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ограничение на количество
|
||||||
|
private final int _maxCount;
|
||||||
|
|
||||||
|
public SetLocomotivesGeneric(int count) {
|
||||||
|
_maxCount = count;
|
||||||
|
_places = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Insert (T locomotive) {
|
||||||
|
return Insert(locomotive, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Insert (T locomotive, int position) {
|
||||||
|
if (position >= _maxCount|| position < 0) return -1;
|
||||||
|
_places.add(position, locomotive);
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Remove (int position) {
|
||||||
|
if (position >= _maxCount || position < 0) return null;
|
||||||
|
T result = _places.get(position);
|
||||||
|
_places.remove(position);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Get(int position)
|
||||||
|
{
|
||||||
|
if (position >= _maxCount || position < 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return _places.get(position);
|
||||||
|
}
|
||||||
|
public Iterable<T> GetLocomotives()
|
||||||
|
{
|
||||||
|
return _places;
|
||||||
|
}
|
||||||
|
}
|
49
SimpleMap.java
Normal file
49
SimpleMap.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class SimpleMap extends AbstractMap{
|
||||||
|
/// Цвет участка закрытого
|
||||||
|
private final Color barrierColor = Color.BLACK;
|
||||||
|
/// Цвет участка открытого
|
||||||
|
private final Color roadColor = Color.GRAY;
|
||||||
|
|
||||||
|
@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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
53
SpikeMap.java
Normal file
53
SpikeMap.java
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class SpikeMap extends AbstractMap{
|
||||||
|
/// Цвет участка закрытого
|
||||||
|
private final Color barrierColor = Color.BLACK;
|
||||||
|
/// Цвет участка открытого
|
||||||
|
private final Color roadColor = Color.GREEN;
|
||||||
|
|
||||||
|
@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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 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 < 15)
|
||||||
|
{
|
||||||
|
int x = _random.nextInt(95) + 1;
|
||||||
|
int y = _random.nextInt(85) + 1;
|
||||||
|
if (_map[x][y] == _freeRoad)
|
||||||
|
{
|
||||||
|
_map[x][y] = _barrier;
|
||||||
|
if (_map[x + 1][y] == _freeRoad) _map[x + 1][y] = _barrier;
|
||||||
|
if (_map[x - 1][y] == _freeRoad) _map[x - 1][y] = _barrier;
|
||||||
|
if (_map[x][y + 1] == _freeRoad) _map[x][y + 1] = _barrier;
|
||||||
|
if (_map[x][y - 1] == _freeRoad) _map[x][y - 1] = _barrier;
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user