Compare commits
44 Commits
Author | SHA1 | Date | |
---|---|---|---|
29ba58c11a | |||
f329788ce7 | |||
2fe33e7569 | |||
9f39107385 | |||
1d30c011aa | |||
a9f39a20a9 | |||
0ca1f9efcb | |||
1812efd16e | |||
64a31c1a76 | |||
7e8bac26f3 | |||
6e69f8b100 | |||
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 {
|
||||
Up, Down, Left, Right
|
||||
None,Up, Down, Left, Right
|
||||
}
|
||||
|
@ -1,28 +1,47 @@
|
||||
import java.awt.*;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
|
||||
class DrawningLocomotive {
|
||||
public class DrawningLocomotive {
|
||||
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 _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, int wheelsCount)
|
||||
{
|
||||
Locomotive = entity;
|
||||
extraWheelsDraw = new ExtraWheelsDraw();
|
||||
extraWheelsDraw.Init(wheelsNum, bodyColor);
|
||||
Locomotive.Init(speed, weight, bodyColor);
|
||||
drawningExtra = new ExtraWheelsDraw(wheelsCount, bodyColor);
|
||||
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 wheelsCount, int locomotiveWidth, int locomotiveHeight)
|
||||
{
|
||||
this(speed, weight, bodyColor, wheelsCount);
|
||||
_locomotiveWidth = locomotiveWidth;
|
||||
_locomotiveHeight = locomotiveHeight;
|
||||
}
|
||||
|
||||
public void SetColor(Color color) {
|
||||
Locomotive.SetColor(color);
|
||||
}
|
||||
|
||||
/// Установка позиции локомотива
|
||||
public void SetPosition(int x, int y, int width, int height)
|
||||
{
|
||||
@ -90,18 +109,18 @@ class DrawningLocomotive {
|
||||
return;
|
||||
}
|
||||
//тело
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect((int)_startPosX , (int)_startPosY, _locomotiveWidth - 20, _locomotiveHeight - 10);
|
||||
//окна
|
||||
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 + 30, (int)_startPosY + 10, 10, 10);
|
||||
g.fillRect((int)_startPosX + 80, (int)_startPosY + 10, 10, 10);
|
||||
//дверь
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20);
|
||||
//колеса
|
||||
extraWheelsDraw.DrawWheels((int)_startPosX, (int)_startPosY, g);
|
||||
g.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20);
|
||||
//extra
|
||||
drawningExtra.DrawExtra((int)_startPosX, (int)_startPosY, g);
|
||||
//движок
|
||||
g.setColor(Locomotive.getBodyColor());
|
||||
g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30);
|
||||
@ -126,4 +145,63 @@ class DrawningLocomotive {
|
||||
_startPosY = _pictureHeight - _locomotiveHeight;
|
||||
}
|
||||
}
|
||||
|
||||
// Получение текущей позиции объекта
|
||||
public float[] GetCurrentPosition()
|
||||
{
|
||||
return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _locomotiveWidth, /*DOWN*/ _startPosY + _locomotiveHeight, /*LEFT*/ _startPosX};
|
||||
}
|
||||
|
||||
private static final char _separatorForObject = ':';
|
||||
public String getDataForSave()
|
||||
{
|
||||
var str = "" + Locomotive.getSpeed() + _separatorForObject
|
||||
+ Locomotive.getWeight() + _separatorForObject
|
||||
+ Locomotive.getBodyColor().getRed() + _separatorForObject
|
||||
+ Locomotive.getBodyColor().getGreen() + _separatorForObject
|
||||
+ Locomotive.getBodyColor().getBlue() + _separatorForObject
|
||||
+ drawningExtra.TypeString() + _separatorForObject
|
||||
+ drawningExtra.getWheelsCount();
|
||||
if (!(Locomotive instanceof EntityWarmlyLocomotive))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return str + _separatorForObject
|
||||
+ ((EntityWarmlyLocomotive) Locomotive).ExtraColor.getRed() + _separatorForObject
|
||||
+ ((EntityWarmlyLocomotive) Locomotive).ExtraColor.getGreen() + _separatorForObject
|
||||
+ ((EntityWarmlyLocomotive) Locomotive).ExtraColor.getBlue() + _separatorForObject
|
||||
+ ((EntityWarmlyLocomotive) Locomotive).Pipe + _separatorForObject
|
||||
+ ((EntityWarmlyLocomotive) Locomotive).FuelStorage;
|
||||
}
|
||||
|
||||
public static DrawningLocomotive createDrawningLocomotive(String info) {
|
||||
IDrawningExtra drawningExtra = null;
|
||||
EntityLocomotive Locomotive = null;
|
||||
String[] strs = info.split(Character.toString(_separatorForObject));
|
||||
if (strs[5].equals("Simple")) drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])));
|
||||
if (strs[5].equals("Star")) drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])));
|
||||
if (Objects.equals(strs[5], "Round")) drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])));
|
||||
if (drawningExtra == null) return null;
|
||||
if (strs.length == 7)
|
||||
{
|
||||
Locomotive = new EntityLocomotive(
|
||||
Integer.parseInt(strs[0]),
|
||||
Float.parseFloat(strs[1]),
|
||||
new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4]))
|
||||
);
|
||||
return new DrawningLocomotive(Locomotive, drawningExtra);
|
||||
}
|
||||
if (strs.length == 12) {
|
||||
Locomotive = new EntityWarmlyLocomotive(
|
||||
Integer.parseInt(strs[0]),
|
||||
Float.parseFloat(strs[1]),
|
||||
new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])),
|
||||
new Color(Integer.parseInt(strs[7]), Integer.parseInt(strs[8]), Integer.parseInt(strs[9])),
|
||||
Boolean.parseBoolean(strs[10]),
|
||||
Boolean.parseBoolean(strs[11])
|
||||
);
|
||||
return new DrawningWarmlyLocomotive(Locomotive, drawningExtra);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
51
DrawningObjectLocomotive.java
Normal file
51
DrawningObjectLocomotive.java
Normal file
@ -0,0 +1,51 @@
|
||||
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);
|
||||
}
|
||||
|
||||
public String getInfo(){
|
||||
if (_locomotive == null) return null;
|
||||
return _locomotive.getDataForSave();
|
||||
}
|
||||
|
||||
public static IDrawningObject Create(String data) {
|
||||
return new DrawningObjectLocomotive(DrawningLocomotive.createDrawningLocomotive(data));
|
||||
}
|
||||
}
|
51
DrawningWarmlyLocomotive.java
Normal file
51
DrawningWarmlyLocomotive.java
Normal file
@ -0,0 +1,51 @@
|
||||
import java.awt.*;
|
||||
public class DrawningWarmlyLocomotive extends DrawningLocomotive{
|
||||
public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, int wheelsCount, Color extraColor, boolean pipe, boolean storage)
|
||||
{
|
||||
super(speed, weight, bodyColor,wheelsCount, 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 SetColor(Color color) {
|
||||
((EntityWarmlyLocomotive) Locomotive).SetColor(color);
|
||||
}
|
||||
public void SetExtraColor(Color color) {
|
||||
((EntityWarmlyLocomotive) Locomotive).SetExtraColor(color);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
@ -14,12 +14,15 @@ public class EntityLocomotive {
|
||||
public Color getBodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
public void SetColor(Color color) {
|
||||
BodyColor = color;
|
||||
}
|
||||
|
||||
public float Step () {
|
||||
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();
|
||||
if (speed <= 0) {
|
||||
|
19
EntityWarmlyLocomotive.java
Normal file
19
EntityWarmlyLocomotive.java
Normal file
@ -0,0 +1,19 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityWarmlyLocomotive extends EntityLocomotive{
|
||||
public 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;
|
||||
}
|
||||
|
||||
public void SetExtraColor(Color color) {
|
||||
ExtraColor = color;
|
||||
}
|
||||
|
||||
}
|
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);
|
||||
}
|
||||
|
||||
}
|
15
EventListener.java
Normal file
15
EventListener.java
Normal file
@ -0,0 +1,15 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class EventListener<T> {
|
||||
private ArrayList<Consumer<T>> listeners = new ArrayList<>();
|
||||
public void Add(Consumer<T> listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void Emit(T artillery) {
|
||||
for (var listener : listeners) {
|
||||
listener.accept(artillery);
|
||||
}
|
||||
}
|
||||
}
|
57
ExtraRoundWheelDraw.java
Normal file
57
ExtraRoundWheelDraw.java
Normal file
@ -0,0 +1,57 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class ExtraRoundWheelDraw implements IDrawningExtra{
|
||||
private WheelsCount wheelsCount = WheelsCount.Two;
|
||||
private ExtraWheelsDraw extraWheelsDraw;
|
||||
private Color color;
|
||||
public void setExtraNum(int num) {
|
||||
switch (num) {
|
||||
case 3: {
|
||||
wheelsCount = WheelsCount.Three;
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
wheelsCount = WheelsCount.Four;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public String TypeString() {
|
||||
return "Round";
|
||||
}
|
||||
public int getWheelsCount() {
|
||||
if (wheelsCount == WheelsCount.Two) return 2;
|
||||
if (wheelsCount == WheelsCount.Three) return 3;
|
||||
else return 4;
|
||||
}
|
||||
|
||||
public ExtraRoundWheelDraw (int num, Color bodyColor) {
|
||||
setExtraNum(num);
|
||||
extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor);
|
||||
}
|
||||
|
||||
public void SetColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public void DrawExtra(int startPosX, int startPosY, Graphics2D g) {
|
||||
extraWheelsDraw.DrawExtra(startPosX, startPosY, g);
|
||||
g.setColor(color);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
66
ExtraStarWheelDraw.java
Normal file
66
ExtraStarWheelDraw.java
Normal file
@ -0,0 +1,66 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class ExtraStarWheelDraw implements IDrawningExtra{
|
||||
private WheelsCount wheelsCount = WheelsCount.Two;
|
||||
private ExtraWheelsDraw extraWheelsDraw;
|
||||
private Color color;
|
||||
public void setExtraNum(int num) {
|
||||
switch (num) {
|
||||
case 3: {
|
||||
wheelsCount = WheelsCount.Three;
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
wheelsCount = WheelsCount.Four;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public String TypeString() {
|
||||
return "Star";
|
||||
}
|
||||
|
||||
public int getWheelsCount() {
|
||||
if (wheelsCount == WheelsCount.Two) return 2;
|
||||
if (wheelsCount == WheelsCount.Three) return 3;
|
||||
else return 4;
|
||||
}
|
||||
|
||||
public ExtraStarWheelDraw (int num, Color bodyColor) {
|
||||
setExtraNum(num);
|
||||
extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor);
|
||||
}
|
||||
|
||||
public void SetColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
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.setColor(color);
|
||||
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,14 +1,14 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class ExtraWheelsDraw {
|
||||
public class ExtraWheelsDraw implements IDrawningExtra{
|
||||
private WheelsCount wheelsCount = WheelsCount.Two;
|
||||
public void setWheelsNum(int num) {
|
||||
public void setExtraNum(int num) {
|
||||
switch (num) {
|
||||
case 0: {
|
||||
case 3: {
|
||||
wheelsCount = WheelsCount.Three;
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
case 4: {
|
||||
wheelsCount = WheelsCount.Four;
|
||||
break;
|
||||
}
|
||||
@ -17,22 +17,43 @@ public class ExtraWheelsDraw {
|
||||
}
|
||||
}
|
||||
private Color color;
|
||||
|
||||
public void Init(int num, Color color) {
|
||||
setWheelsNum(num);
|
||||
public void SetColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public void DrawWheels(int startPosX, int startPosY, Graphics2D g) {
|
||||
g.setColor(color);
|
||||
g.drawOval(startPosX, startPosY + 40, 10, 10);
|
||||
g.drawOval(startPosX + 90, startPosY + 40, 10, 10);
|
||||
public ExtraWheelsDraw(int num, Color color) {
|
||||
setExtraNum(num);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String TypeString() {
|
||||
return "Simple";
|
||||
}
|
||||
|
||||
public int getWheelsCount() {
|
||||
if (wheelsCount == WheelsCount.Two) return 2;
|
||||
if (wheelsCount == WheelsCount.Three) return 3;
|
||||
else return 4;
|
||||
}
|
||||
|
||||
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.BLACK);
|
||||
g.fillOval(startPosX, startPosY + 30, 20, 20);
|
||||
g.fillOval(startPosX + 90, startPosY + 30, 20, 20);
|
||||
switch (wheelsCount) {
|
||||
case Four: {
|
||||
g.drawOval(startPosX + 70, startPosY + 40, 10, 10);
|
||||
g.setColor(color);
|
||||
g.drawOval(startPosX + 70, startPosY + 30, 20, 20);
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval(startPosX + 70, startPosY + 30, 20, 20);
|
||||
}
|
||||
case Three: {
|
||||
g.drawOval(startPosX + 20, startPosY + 40, 10, 10);
|
||||
g.fillOval(startPosX + 20, startPosY + 30, 20, 20);
|
||||
g.setColor(color);
|
||||
g.drawOval(startPosX + 20, startPosY + 30, 20, 20);
|
||||
break;
|
||||
}
|
||||
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 java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormLocomotive extends JComponent{
|
||||
private DrawningLocomotive _locomotive;
|
||||
private EntityLocomotive _entity;
|
||||
public FormLocomotive() {
|
||||
JFrame formFrame = new JFrame("Locomotive");
|
||||
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
formFrame.setSize(800, 500);
|
||||
formFrame.setVisible(true);
|
||||
formFrame.setLocationRelativeTo(null);
|
||||
|
||||
formFrame.addComponentListener(new ComponentListener() {
|
||||
@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) {}
|
||||
});
|
||||
private DrawningLocomotive SelectedLocomotive;
|
||||
public DrawningLocomotive getSelectedLocomotive() {
|
||||
return SelectedLocomotive;
|
||||
}
|
||||
public void SetLocomotive(DrawningObjectLocomotive locomotive){
|
||||
_locomotive = locomotive.GetDrawningLocomotive();
|
||||
repaint();
|
||||
}
|
||||
public FormLocomotive(JDialog caller) {
|
||||
|
||||
Panel statusPanel = new Panel();
|
||||
statusPanel.setBackground(Color.WHITE);
|
||||
@ -40,40 +27,69 @@ public class FormLocomotive extends JComponent{
|
||||
JButton createButton = new JButton("Create");
|
||||
createButton.addActionListener(e -> {
|
||||
Random rnd = new Random();
|
||||
_locomotive = new DrawningLocomotive();
|
||||
_entity = new EntityLocomotive();
|
||||
_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);
|
||||
|
||||
Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
|
||||
|
||||
_locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst, rnd.nextInt(2) + 2);
|
||||
_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 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,
|
||||
rnd.nextInt(2) + 2,
|
||||
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(modifiedButton);
|
||||
statusPanel.add(selectLocomotiveButton);
|
||||
statusPanel.add(speedLabel);
|
||||
statusPanel.add(weightLabel);
|
||||
statusPanel.add(colorLabel);
|
||||
|
||||
JButton moveDownButton = new JButton("Down");
|
||||
JButton moveDownButton = new JButton("D");
|
||||
moveDownButton.addActionListener(e -> {
|
||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Down);
|
||||
repaint();
|
||||
});
|
||||
|
||||
JButton moveUpButton = new JButton("Up");
|
||||
JButton moveUpButton = new JButton("U");
|
||||
moveUpButton.addActionListener(e -> {
|
||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Up);
|
||||
repaint();
|
||||
});
|
||||
|
||||
JButton moveLeftButton = new JButton("Left");
|
||||
JButton moveLeftButton = new JButton("L");
|
||||
moveLeftButton.addActionListener(e -> {
|
||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Left);
|
||||
repaint();
|
||||
});
|
||||
|
||||
JButton moveRightButton = new JButton("Right");
|
||||
JButton moveRightButton = new JButton("R");
|
||||
moveRightButton.addActionListener(e -> {
|
||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Right);
|
||||
repaint();
|
||||
@ -83,10 +99,7 @@ public class FormLocomotive extends JComponent{
|
||||
statusPanel.add(moveDownButton);
|
||||
statusPanel.add(moveLeftButton);
|
||||
statusPanel.add(moveRightButton);
|
||||
|
||||
formFrame.getContentPane().add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
@ -94,8 +107,7 @@ public class FormLocomotive extends JComponent{
|
||||
if (_locomotive != null) _locomotive.DrawTransport(g2);
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
FormLocomotive formLocomotive = new FormLocomotive();
|
||||
new FormMapWithSetLocomotives();
|
||||
}
|
||||
}
|
||||
|
337
FormLocomotiveConfig.form
Normal file
337
FormLocomotiveConfig.form
Normal file
@ -0,0 +1,337 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormLocomotiveConfig">
|
||||
<grid id="27dc6" binding="FormPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="688" height="485"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="1add4" binding="ParametersPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none" title="Parameters"/>
|
||||
<children>
|
||||
<grid id="ac7f6" binding="ColorPanel" layout-manager="GridLayoutManager" row-count="3" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none" title="Color Pick"/>
|
||||
<children>
|
||||
<grid id="56efb" binding="RedColorPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-65536"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="49a9d" binding="GreenColorPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-16711936"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="94ada" binding="BlueColorPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-16776961"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="39e91" binding="YellowColorPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-256"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="20acf" binding="WhiteColorPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-1"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="5cd7b" binding="BlackColorPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-16777216"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="54ec4" binding="AquaColorPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-16711681"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="5bf58" binding="PurpleColorPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="50" height="50"/>
|
||||
<maximum-size width="50" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<background color="-65281"/>
|
||||
</properties>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<vspacer id="47a2">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="4" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</vspacer>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="97483" binding="StatsPanel" layout-manager="GridLayoutManager" row-count="10" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none" title="Stats"/>
|
||||
<children>
|
||||
<component id="a2b73" class="javax.swing.JLabel" binding="SpeedLabel">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Speed:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="63e1f" class="javax.swing.JSpinner" binding="SpeedSpinner">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="4677d" class="javax.swing.JLabel" binding="WeightLabel">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Weight:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="dd3cf" class="javax.swing.JSpinner" binding="WeightSpinner">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="d60f6" class="javax.swing.JCheckBox" binding="PipeCheckBox">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Add Pipe"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="91766" class="javax.swing.JCheckBox" binding="FuelCheckBox">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Add Fuel Storage"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="cb567" class="javax.swing.JLabel" binding="SimpleObjectLabel">
|
||||
<constraints>
|
||||
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="100" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<text value="Simple "/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="8c3ff" class="javax.swing.JLabel" binding="AdvancedObjectLabel">
|
||||
<constraints>
|
||||
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="100" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<text value="Advanced"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="d7166" class="javax.swing.JLabel" binding="SimpleWheelsLabel">
|
||||
<constraints>
|
||||
<grid row="6" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="100" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<text value="SimpleWheels"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="b76f1" class="javax.swing.JLabel" binding="StarWheelsLabel">
|
||||
<constraints>
|
||||
<grid row="7" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="100" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<text value="StarWheels"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="a09d0" class="javax.swing.JLabel" binding="RoundWheelsLabel">
|
||||
<constraints>
|
||||
<grid row="8" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="100" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<text value="RoundWheels"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="55da5" class="javax.swing.JSpinner" binding="WheelsCountSpinner">
|
||||
<constraints>
|
||||
<grid row="9" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="5c650" class="javax.swing.JLabel" binding="WheelsCountLabel">
|
||||
<constraints>
|
||||
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="WheelsCount"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
<grid id="ddc6e" binding="CreatePanel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none" title="Create"/>
|
||||
<children>
|
||||
<component id="81e87" class="javax.swing.JLabel" binding="MainColorLabel">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="100" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<text value="BaseColor"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="b3269" class="javax.swing.JLabel" binding="ExtraColorLabel">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
|
||||
<minimum-size width="100" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties>
|
||||
<horizontalAlignment value="0"/>
|
||||
<horizontalTextPosition value="0"/>
|
||||
<text value="ExtraColor"/>
|
||||
</properties>
|
||||
</component>
|
||||
<grid id="4dc4a" binding="ObjectViewPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<component id="c9686" class="javax.swing.JButton" binding="AddButton">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Add"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="ff438" class="javax.swing.JButton" binding="CancelButton">
|
||||
<constraints>
|
||||
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Cancel"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
</grid>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
158
FormLocomotiveConfig.java
Normal file
158
FormLocomotiveConfig.java
Normal file
@ -0,0 +1,158 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class FormLocomotiveConfig extends JFrame{
|
||||
// Рабочие поля
|
||||
private DrawningLocomotive locomotive;
|
||||
private final EventListener<DrawningLocomotive> eventListener = new EventListener<>();
|
||||
|
||||
//Элементы формы
|
||||
private JPanel FormPanel;
|
||||
private JPanel ParametersPanel;
|
||||
private JPanel CreatePanel;
|
||||
private JPanel ColorPanel;
|
||||
private JPanel StatsPanel;
|
||||
private JLabel SpeedLabel;
|
||||
private JSpinner SpeedSpinner;
|
||||
private JLabel WeightLabel;
|
||||
private JSpinner WeightSpinner;
|
||||
private JCheckBox PipeCheckBox;
|
||||
private JCheckBox FuelCheckBox;
|
||||
private JLabel SimpleObjectLabel;
|
||||
private JLabel AdvancedObjectLabel;
|
||||
private JLabel SimpleWheelsLabel;
|
||||
private JLabel StarWheelsLabel;
|
||||
private JLabel RoundWheelsLabel;
|
||||
private JSpinner WheelsCountSpinner;
|
||||
private JLabel WheelsCountLabel;
|
||||
private JPanel RedColorPanel;
|
||||
private JPanel GreenColorPanel;
|
||||
private JPanel BlueColorPanel;
|
||||
private JPanel YellowColorPanel;
|
||||
private JPanel WhiteColorPanel;
|
||||
private JPanel BlackColorPanel;
|
||||
private JPanel AquaColorPanel;
|
||||
private JPanel PurpleColorPanel;
|
||||
private JLabel MainColorLabel;
|
||||
private JLabel ExtraColorLabel;
|
||||
private JPanel ObjectViewPanel;
|
||||
private JButton AddButton;
|
||||
private JButton CancelButton;
|
||||
|
||||
public FormLocomotiveConfig() {
|
||||
|
||||
// Добавили цветные границы
|
||||
SimpleObjectLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
AdvancedObjectLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
SimpleWheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
RoundWheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
StarWheelsLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
MainColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
ExtraColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
||||
|
||||
// Модели для намериков
|
||||
SpeedSpinner.setModel(new SpinnerNumberModel(100, 50, 1000, 10));
|
||||
WeightSpinner.setModel(new SpinnerNumberModel(1000, 1000, 5000, 10));
|
||||
WheelsCountSpinner.setModel(new SpinnerNumberModel(2, 2, 4, 1));
|
||||
|
||||
//Обработчик d&d
|
||||
var DragDropAdapter = new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
super.mouseReleased(e);
|
||||
}
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
super.mouseReleased(e);
|
||||
Drop((JComponent) e.getSource());
|
||||
}
|
||||
};
|
||||
|
||||
SimpleObjectLabel.addMouseListener(DragDropAdapter);
|
||||
AdvancedObjectLabel.addMouseListener(DragDropAdapter);
|
||||
StarWheelsLabel.addMouseListener(DragDropAdapter);
|
||||
RoundWheelsLabel.addMouseListener(DragDropAdapter);
|
||||
StarWheelsLabel.addMouseListener(DragDropAdapter);
|
||||
RedColorPanel.addMouseListener(DragDropAdapter);
|
||||
GreenColorPanel.addMouseListener(DragDropAdapter);
|
||||
BlueColorPanel.addMouseListener(DragDropAdapter);
|
||||
YellowColorPanel.addMouseListener(DragDropAdapter);
|
||||
WhiteColorPanel.addMouseListener(DragDropAdapter);
|
||||
BlackColorPanel.addMouseListener(DragDropAdapter);
|
||||
AquaColorPanel.addMouseListener(DragDropAdapter);
|
||||
PurpleColorPanel.addMouseListener(DragDropAdapter);
|
||||
|
||||
AddButton.addActionListener(e -> {
|
||||
eventListener.Emit(locomotive);
|
||||
dispose();
|
||||
});
|
||||
|
||||
CancelButton.addActionListener(e -> dispose());
|
||||
|
||||
// Параметры фрейма
|
||||
setContentPane(FormPanel);
|
||||
setSize(800, 500);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
// Drop обработка
|
||||
public void Drop (JComponent component) {
|
||||
if (component == null) {
|
||||
return;
|
||||
}
|
||||
if (component instanceof JPanel panel) {
|
||||
if (MainColorLabel.getMousePosition() != null) {
|
||||
locomotive.SetColor(panel.getBackground());
|
||||
locomotive.drawningExtra.SetColor(panel.getBackground());
|
||||
}
|
||||
if (ExtraColorLabel.getMousePosition() != null && locomotive instanceof DrawningWarmlyLocomotive warmlyLocomotive) {
|
||||
warmlyLocomotive.SetExtraColor(panel.getBackground());
|
||||
}
|
||||
}
|
||||
if (component instanceof JLabel label && ObjectViewPanel.getMousePosition() != null) {
|
||||
int speed = (Integer) SpeedSpinner.getValue();
|
||||
int weight = (Integer) WeightSpinner.getValue();
|
||||
int wheelsCount = (Integer) WheelsCountSpinner.getValue();
|
||||
boolean pipe = PipeCheckBox.isSelected();
|
||||
boolean fuel = FuelCheckBox.isSelected();
|
||||
|
||||
if (label == SimpleObjectLabel) {
|
||||
locomotive = new DrawningLocomotive(speed, weight, Color.WHITE, wheelsCount);
|
||||
} else if (label == AdvancedObjectLabel) {
|
||||
locomotive = new DrawningWarmlyLocomotive(speed, weight, Color.WHITE, wheelsCount, Color.WHITE, pipe, fuel);
|
||||
} else if (locomotive != null && label == SimpleWheelsLabel) {
|
||||
locomotive.drawningExtra = new ExtraWheelsDraw(wheelsCount, locomotive.Locomotive.getBodyColor());
|
||||
locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor());
|
||||
} else if (locomotive != null && label == StarWheelsLabel) {
|
||||
locomotive.drawningExtra = new ExtraStarWheelDraw(wheelsCount, locomotive.Locomotive.getBodyColor());
|
||||
locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor());
|
||||
} else if (locomotive != null && label == RoundWheelsLabel) {
|
||||
locomotive.drawningExtra = new ExtraRoundWheelDraw(wheelsCount, locomotive.Locomotive.getBodyColor());
|
||||
locomotive.drawningExtra.SetColor( locomotive.Locomotive.getBodyColor());
|
||||
}
|
||||
}
|
||||
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void AddListener(Consumer<DrawningLocomotive> listener) {
|
||||
eventListener.Add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
if (locomotive != null) {
|
||||
g = ObjectViewPanel.getGraphics();
|
||||
locomotive.SetPosition(20, 20, ObjectViewPanel.getWidth(), ObjectViewPanel.getHeight());
|
||||
locomotive.DrawTransport((Graphics2D) g);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
364
FormMapWithSetLocomotives.java
Normal file
364
FormMapWithSetLocomotives.java
Normal file
@ -0,0 +1,364 @@
|
||||
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 -> {
|
||||
FormLocomotiveConfig formLocomotiveConfig = new FormLocomotiveConfig();
|
||||
formLocomotiveConfig.setVisible(true);
|
||||
formLocomotiveConfig.AddListener(locomotive -> {
|
||||
if (listBoxMaps.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
if (locomotive!=null) {
|
||||
DrawningObjectLocomotive objectLocomotive = new DrawningObjectLocomotive(locomotive);
|
||||
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -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);
|
||||
}
|
||||
});
|
||||
// Сохранения и загрузки
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
// Сохранение всех хранилищ
|
||||
JButton saveButton = new JButton("Save");
|
||||
saveButton.addActionListener(e -> {
|
||||
fileChooser.setDialogTitle("Saving");
|
||||
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
||||
if (result == JFileChooser.APPROVE_OPTION ) {
|
||||
if (_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Save success");
|
||||
}
|
||||
else JOptionPane.showMessageDialog(null, "Save failed");
|
||||
}
|
||||
|
||||
});
|
||||
statusPanel.add(saveButton);
|
||||
// Загрузка всех хранилищ
|
||||
JButton loadButton = new JButton("Load");
|
||||
loadButton.addActionListener(e -> {
|
||||
fileChooser.setDialogTitle("Loading");
|
||||
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
||||
if (result == JFileChooser.APPROVE_OPTION ) {
|
||||
if (_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Load success");
|
||||
}
|
||||
else JOptionPane.showMessageDialog(null, "Load failed");
|
||||
}
|
||||
ReloadMaps();
|
||||
repaint();
|
||||
});
|
||||
statusPanel.add(loadButton);
|
||||
|
||||
// Сохранение выбранной карты
|
||||
JButton saveMapButton = new JButton("Save Map");
|
||||
saveMapButton.addActionListener(e -> {
|
||||
fileChooser.setDialogTitle("Saving Map");
|
||||
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
||||
if (result == JFileChooser.APPROVE_OPTION ) {
|
||||
if (listBoxMaps.getSelectedIndex() == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
||||
if (_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Map saving success");
|
||||
}
|
||||
else JOptionPane.showMessageDialog(null, "Map saving fail");
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
statusPanel.add(saveMapButton);
|
||||
|
||||
// Загрузка одной карты
|
||||
JButton loadMapButton = new JButton("Load Map");
|
||||
loadMapButton.addActionListener(e -> {
|
||||
fileChooser.setDialogTitle("Loading");
|
||||
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
||||
if (result == JFileChooser.APPROVE_OPTION ) {
|
||||
if (_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Load Map success");
|
||||
}
|
||||
else JOptionPane.showMessageDialog(null, "Load Map failed");
|
||||
}
|
||||
ReloadMaps();
|
||||
repaint();
|
||||
});
|
||||
statusPanel.add(loadMapButton);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
9
IDrawningExtra.java
Normal file
9
IDrawningExtra.java
Normal file
@ -0,0 +1,9 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawningExtra {
|
||||
void setExtraNum(int num);
|
||||
void DrawExtra(int startPosX, int startPosY, Graphics2D g);
|
||||
void SetColor(Color color);
|
||||
String TypeString();
|
||||
int getWheelsCount();
|
||||
}
|
20
IDrawningObject.java
Normal file
20
IDrawningObject.java
Normal file
@ -0,0 +1,20 @@
|
||||
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);
|
||||
|
||||
String getInfo();
|
||||
|
||||
/// Получение текущей позиции объекта
|
||||
float[] GetCurrentPosition();
|
||||
//0 - up
|
||||
//1 - right
|
||||
//2 - down
|
||||
//3 - left
|
||||
}
|
201
MapWithSetLocomotivesGeneric.java
Normal file
201
MapWithSetLocomotivesGeneric.java
Normal file
@ -0,0 +1,201 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Получение данных в виде строки
|
||||
public String GetData(char separatorType, char separatorData)
|
||||
{
|
||||
String data = ""+ _map.getClass() + separatorType;
|
||||
for (var locomotive : _setLocomotives.GetLocomotives())
|
||||
{
|
||||
data += "" + locomotive.getInfo() + separatorData;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/// Загрузка списка из массива строк
|
||||
public void LoadData(String[] records)
|
||||
{
|
||||
Collections.reverse(Arrays.asList(records));
|
||||
for (var rec : records)
|
||||
{
|
||||
_setLocomotives.Insert((T)DrawningObjectLocomotive.Create(rec));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
186
MapsCollection.java
Normal file
186
MapsCollection.java
Normal file
@ -0,0 +1,186 @@
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class MapsCollection {
|
||||
/// Словарь (хранилище) с картами
|
||||
final HashMap<String, MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>> _mapStorages;
|
||||
|
||||
/// Возвращение списка названий карт
|
||||
public ArrayList<String> keys() {
|
||||
return new ArrayList<String>(_mapStorages.keySet());
|
||||
}
|
||||
/// Ширина окна отрисовки
|
||||
private final int _pictureWidth;
|
||||
/// Высота окна отрисовки
|
||||
private final int _pictureHeight;
|
||||
// Сепараторы
|
||||
private final String separatorDict = "~";
|
||||
private final char separatorData = ';';
|
||||
/// Конструктор
|
||||
public MapsCollection(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_mapStorages = new HashMap<String, MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
/// Добавление карты
|
||||
public void AddMap(String name, AbstractMap map)
|
||||
{
|
||||
// Логика для добавления
|
||||
if (!_mapStorages.containsKey(name)) _mapStorages.put(name, new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
}
|
||||
/// Удаление карты
|
||||
public void DelMap(String name)
|
||||
{
|
||||
// Логика для удаления
|
||||
if (_mapStorages.containsKey(name)) _mapStorages.remove(name);
|
||||
}
|
||||
/// Доступ к парковке
|
||||
public MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> Get(String ind)
|
||||
{
|
||||
// Логика получения объекта
|
||||
if (_mapStorages.containsKey(ind)) return _mapStorages.get(ind);
|
||||
return null;
|
||||
}
|
||||
// Доп.индексатор из задания
|
||||
public IDrawningObject Get (String name, int position) {
|
||||
if (_mapStorages.containsKey(name)) return _mapStorages.get(name)._setLocomotives.Get(position);
|
||||
else return null;
|
||||
}
|
||||
|
||||
/// Сохранение информации по локомотивам в хранилище в файл
|
||||
public boolean SaveData(String filename)
|
||||
{
|
||||
File file = new File(filename);
|
||||
if (file.exists())
|
||||
{
|
||||
file.delete();
|
||||
}
|
||||
try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
|
||||
bw.write("MapsCollection\n");
|
||||
for (var storage : _mapStorages.entrySet()) {
|
||||
bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict.charAt(0), separatorData) + "\n");
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Сохранение одной выбранной карты
|
||||
public boolean SaveMap(String map_name, String filename) {
|
||||
File file = new File(filename);
|
||||
MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> map = Get(map_name);
|
||||
if (file.exists())
|
||||
{
|
||||
file.delete();
|
||||
}
|
||||
try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
|
||||
bw.write("SingleMap\n");
|
||||
bw.write("" + map_name + "\n");
|
||||
bw.write("" + map.getClass() + "\n");
|
||||
for (var locomotive : map._setLocomotives.GetLocomotives()) {
|
||||
bw.write("" + locomotive.getInfo() + "\n");
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Загрузка одной карты
|
||||
public boolean LoadMap(String filename){
|
||||
File file = new File(filename);
|
||||
if (!file.exists())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(filename));
|
||||
String curLine = br.readLine();
|
||||
if (!curLine.contains("SingleMap")) {
|
||||
return false;
|
||||
}
|
||||
String mapName = br.readLine();
|
||||
String mapClass = br.readLine();
|
||||
if (_mapStorages.containsKey(mapName)) {
|
||||
_mapStorages.get(mapName)._setLocomotives.Clear();
|
||||
while((curLine = br.readLine()) != null) {
|
||||
_mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine));
|
||||
}
|
||||
_mapStorages.get(mapName)._setLocomotives.ReversePlaces();
|
||||
return true;
|
||||
}
|
||||
AbstractMap map = null;
|
||||
switch (mapClass)
|
||||
{
|
||||
case "class SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "class SpikeMap":
|
||||
map = new SpikeMap();
|
||||
break;
|
||||
case "class RailMap":
|
||||
map = new RailMap();
|
||||
break;
|
||||
}
|
||||
_mapStorages.put(mapName, new MapWithSetLocomotivesGeneric<>(_pictureWidth, _pictureHeight, map));
|
||||
while((curLine = br.readLine()) != null) {
|
||||
_mapStorages.get(mapName)._setLocomotives.Insert(DrawningObjectLocomotive.Create(curLine));
|
||||
}
|
||||
_mapStorages.get(mapName)._setLocomotives.ReversePlaces();
|
||||
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Загрузка информации по локомотивам в депо из файла
|
||||
public boolean LoadData(String filename)
|
||||
{
|
||||
File file = new File(filename);
|
||||
if (!file.exists())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(filename));
|
||||
String curLine = br.readLine();
|
||||
if (!curLine.contains("MapsCollection")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_mapStorages.clear();
|
||||
while ((curLine = br.readLine()) != null) {
|
||||
var elems = curLine.split(separatorDict);
|
||||
AbstractMap map = null;
|
||||
|
||||
switch (elems[1])
|
||||
{
|
||||
case "class SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "class SpikeMap":
|
||||
map = new SpikeMap();
|
||||
break;
|
||||
case "class RailMap":
|
||||
map = new RailMap();
|
||||
break;
|
||||
}
|
||||
|
||||
_mapStorages.put(elems[0], new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData)));
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
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;
|
||||
}
|
||||
}
|
||||
}
|
57
SetLocomotivesGeneric.java
Normal file
57
SetLocomotivesGeneric.java
Normal file
@ -0,0 +1,57 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
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 void Clear() {
|
||||
_places.clear();
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position >= _maxCount || position < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _places.get(position);
|
||||
}
|
||||
public Iterable<T> GetLocomotives()
|
||||
{
|
||||
return _places;
|
||||
}
|
||||
public void ReversePlaces() {
|
||||
Collections.reverse(_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