Compare commits
1 Commits
bf246e17d4
...
master
Author | SHA1 | Date | |
---|---|---|---|
f7cf9ea559 |
163
AbstractMap.java
163
AbstractMap.java
@ -1,163 +0,0 @@
|
|||||||
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 {
|
||||||
None,Up, Down, Left, Right
|
Up, Down, Left, Right
|
||||||
}
|
}
|
||||||
|
@ -1,37 +1,28 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
class DrawningLocomotive {
|
class DrawningLocomotive {
|
||||||
public EntityLocomotive Locomotive;
|
public EntityLocomotive Locomotive;
|
||||||
public ExtraWheelsDraw extraWheelsDraw;
|
public ExtraWheelsDraw extraWheelsDraw;
|
||||||
/// Левая координата отрисовки локомотива
|
/// Левая координата отрисовки локомотива
|
||||||
protected float _startPosX;
|
private float _startPosX;
|
||||||
/// Верхняя координата отрисовки локомотива
|
/// Верхняя координата отрисовки локомотива
|
||||||
protected float _startPosY;
|
private float _startPosY;
|
||||||
/// Ширина окна отрисовки
|
/// Ширина окна отрисовки
|
||||||
private Integer _pictureWidth = null;
|
private Integer _pictureWidth = null;
|
||||||
/// Высота окна отрисовки
|
/// Высота окна отрисовки
|
||||||
private Integer _pictureHeight = null;
|
private Integer _pictureHeight = null;
|
||||||
/// Ширина отрисовки локомотива
|
/// Ширина отрисовки локомотива
|
||||||
private int _locomotiveWidth = 110;
|
private final int _locomotiveWidth = 120;
|
||||||
/// Высота отрисовки локомотива
|
/// Высота отрисовки локомотива
|
||||||
private int _locomotiveHeight = 50;
|
private final int _locomotiveHeight = 50;
|
||||||
/// Инициализация свойств
|
/// Инициализация свойств
|
||||||
private final Random random = new Random();
|
public void Init(int speed, float weight, Color bodyColor, int wheelsNum, EntityLocomotive entity)
|
||||||
public DrawningLocomotive(int speed, float weight, Color bodyColor)
|
|
||||||
{
|
{
|
||||||
extraWheelsDraw = new ExtraWheelsDraw(random.nextInt(3), bodyColor);
|
Locomotive = entity;
|
||||||
Locomotive = new EntityLocomotive(speed, weight, bodyColor);
|
extraWheelsDraw = new ExtraWheelsDraw();
|
||||||
|
extraWheelsDraw.Init(wheelsNum, bodyColor);
|
||||||
|
Locomotive.Init(speed, weight, bodyColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Новый конструктор
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@ -100,7 +91,7 @@ class DrawningLocomotive {
|
|||||||
}
|
}
|
||||||
//тело
|
//тело
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
g.drawRect((int)_startPosX , (int)_startPosY, 110 - 10, 50 - 10);
|
g.drawRect((int)_startPosX , (int)_startPosY, _locomotiveWidth - 20, _locomotiveHeight - 10);
|
||||||
//окна
|
//окна
|
||||||
g.setColor(Locomotive.getBodyColor());
|
g.setColor(Locomotive.getBodyColor());
|
||||||
g.fillRect((int)_startPosX + 10, (int)_startPosY + 10, 10, 10);
|
g.fillRect((int)_startPosX + 10, (int)_startPosY + 10, 10, 10);
|
||||||
@ -135,10 +126,4 @@ class DrawningLocomotive {
|
|||||||
_startPosY = _pictureHeight - _locomotiveHeight;
|
_startPosY = _pictureHeight - _locomotiveHeight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Получение текущей позиции объекта
|
|
||||||
public float[] GetCurrentPosition()
|
|
||||||
{
|
|
||||||
return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _locomotiveWidth, /*DOWN*/ _startPosY + _locomotiveHeight, /*LEFT*/ _startPosX};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class DrawningObjectLocomotive implements IDrawningObject {
|
|
||||||
private DrawningLocomotive _locomotive = null;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
@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 + 110, (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 EntityLocomotive(int speed, float weight, Color bodyColor)
|
public void Init(int speed, float weight, Color bodyColor)
|
||||||
{
|
{
|
||||||
Random rnd = new Random();
|
Random rnd = new Random();
|
||||||
if (speed <= 0) {
|
if (speed <= 0) {
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -18,7 +18,7 @@ public class ExtraWheelsDraw {
|
|||||||
}
|
}
|
||||||
private Color color;
|
private Color color;
|
||||||
|
|
||||||
public ExtraWheelsDraw(int num, Color color) {
|
public void Init(int num, Color color) {
|
||||||
setWheelsNum(num);
|
setWheelsNum(num);
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import java.util.Random;
|
|||||||
|
|
||||||
public class FormLocomotive extends JComponent{
|
public class FormLocomotive extends JComponent{
|
||||||
private DrawningLocomotive _locomotive;
|
private DrawningLocomotive _locomotive;
|
||||||
|
private EntityLocomotive _entity;
|
||||||
public FormLocomotive() {
|
public FormLocomotive() {
|
||||||
JFrame formFrame = new JFrame("Locomotive");
|
JFrame formFrame = new JFrame("Locomotive");
|
||||||
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
@ -39,22 +40,9 @@ 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(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
_locomotive = new DrawningLocomotive();
|
||||||
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75);
|
_entity = new EntityLocomotive();
|
||||||
speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed());
|
_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);
|
||||||
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();
|
|
||||||
_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());
|
|
||||||
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75);
|
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 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());
|
||||||
@ -63,30 +51,29 @@ public class FormLocomotive extends JComponent{
|
|||||||
});
|
});
|
||||||
|
|
||||||
statusPanel.add(createButton);
|
statusPanel.add(createButton);
|
||||||
statusPanel.add(modifiedButton);
|
|
||||||
statusPanel.add(speedLabel);
|
statusPanel.add(speedLabel);
|
||||||
statusPanel.add(weightLabel);
|
statusPanel.add(weightLabel);
|
||||||
statusPanel.add(colorLabel);
|
statusPanel.add(colorLabel);
|
||||||
|
|
||||||
JButton moveDownButton = new JButton("D");
|
JButton moveDownButton = new JButton("Down");
|
||||||
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("U");
|
JButton moveUpButton = new JButton("Up");
|
||||||
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("L");
|
JButton moveLeftButton = new JButton("Left");
|
||||||
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("R");
|
JButton moveRightButton = new JButton("Right");
|
||||||
moveRightButton.addActionListener(e -> {
|
moveRightButton.addActionListener(e -> {
|
||||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Right);
|
if (_locomotive != null) _locomotive.MoveTransport(Direction.Right);
|
||||||
repaint();
|
repaint();
|
||||||
@ -99,6 +86,7 @@ public class FormLocomotive extends JComponent{
|
|||||||
|
|
||||||
formFrame.getContentPane().add(this);
|
formFrame.getContentPane().add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
@ -108,6 +96,6 @@ public class FormLocomotive extends JComponent{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new FormMap();
|
FormLocomotive formLocomotive = new FormLocomotive();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
129
FormMap.java
129
FormMap.java
@ -1,129 +0,0 @@
|
|||||||
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("D");
|
|
||||||
moveDownButton.addActionListener(e -> {
|
|
||||||
if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Down);
|
|
||||||
repaint();
|
|
||||||
});
|
|
||||||
|
|
||||||
JButton moveUpButton = new JButton("U");
|
|
||||||
moveUpButton.addActionListener(e -> {
|
|
||||||
if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Up);
|
|
||||||
repaint();
|
|
||||||
});
|
|
||||||
|
|
||||||
JButton moveLeftButton = new JButton("L");
|
|
||||||
moveLeftButton.addActionListener(e -> {
|
|
||||||
if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Left);
|
|
||||||
repaint();
|
|
||||||
});
|
|
||||||
|
|
||||||
JButton moveRightButton = new JButton("R");
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
58
RailMap.java
58
RailMap.java
@ -1,58 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,49 +0,0 @@
|
|||||||
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++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,53 +0,0 @@
|
|||||||
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