Compare commits
No commits in common. "LabWork02" and "master" have entirely different histories.
157
AbstractMap.java
157
AbstractMap.java
@ -1,157 +0,0 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedReader;
|
||||
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;
|
||||
public BufferedImage CreateMap(int width, int height, IDrawningObject drawningObject){
|
||||
_width = width;
|
||||
_height = height;
|
||||
_drawningObject = drawningObject;
|
||||
GenerateMap();
|
||||
while (!SetObjectOnMap())
|
||||
{
|
||||
GenerateMap();
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
public BufferedImage MoveObject(Direction direction)
|
||||
{
|
||||
int _startX = (int) (_drawningObject.GetCurrentPosition()[3] / _size_x);
|
||||
int _startY = (int)(_drawningObject.GetCurrentPosition()[0] / _size_y);
|
||||
int _objWidth = (int)(_drawningObject.GetCurrentPosition()[1] / _size_x);
|
||||
int _objHeight = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y);
|
||||
|
||||
boolean isMove = true;
|
||||
switch (direction)
|
||||
{
|
||||
case Right:
|
||||
for (int i = _objWidth; i <= _objWidth + (int)(_drawningObject.getStep() / _size_x); i++)
|
||||
{
|
||||
for (int j = _startY; j <= _objHeight; j++)
|
||||
{
|
||||
if (_map[i][j] == _barrier)
|
||||
{
|
||||
isMove = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Left:
|
||||
for (int i = _startX; i >= (int)(_drawningObject.getStep() / _size_x); i--)
|
||||
{
|
||||
for (int j = _startY; j <= _objHeight; j++)
|
||||
{
|
||||
if (_map[i][j] == _barrier)
|
||||
{
|
||||
isMove = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Up:
|
||||
for (int i = _startX; i <= _objWidth; i++)
|
||||
{
|
||||
for (int j = _startY; j >= (int)(_drawningObject.getStep() / _size_y); j--)
|
||||
{
|
||||
if (_map[i][j] == _barrier)
|
||||
{
|
||||
isMove = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Down:
|
||||
for (int i = _startX; i <= _objWidth; i++)
|
||||
{
|
||||
for (int j = _objHeight; j <= _objHeight + (int)(_drawningObject.getStep() / _size_y); j++)
|
||||
{
|
||||
if (_map[i][j] == _barrier)
|
||||
{
|
||||
isMove = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (isMove)
|
||||
{
|
||||
_drawningObject.MoveObject(direction);
|
||||
}
|
||||
return DrawMapWithObject();
|
||||
}
|
||||
private boolean SetObjectOnMap()
|
||||
{
|
||||
if (_drawningObject == null || _map == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
int x = _random.nextInt(0, 10);
|
||||
int y = _random.nextInt(0, 10);
|
||||
_drawningObject.SetObject(x, y, _width, _height);
|
||||
// TODO првоерка, что объект не "накладывается" на закрытые участки
|
||||
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;
|
||||
}
|
||||
protected abstract void GenerateMap();
|
||||
protected abstract void DrawRoadPart(Graphics g, int i, int j);
|
||||
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
|
||||
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
public enum DirectionBlocksOnDeck {
|
||||
Two,
|
||||
Four,
|
||||
Six
|
||||
}
|
@ -1,194 +0,0 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningBattleship {
|
||||
EntityBattleship Battleship;
|
||||
private DrawningBlocks drawingBlocks;
|
||||
private IDrawningBlocks iDrawingBlocks;
|
||||
public EntityBattleship Battleship()
|
||||
{return Battleship; }
|
||||
/// <summary>
|
||||
/// Левая координата отрисовки корабля
|
||||
/// </summary>
|
||||
protected float _startPosX;
|
||||
/// <summary>
|
||||
/// Верхняя кооридната отрисовки корабля
|
||||
/// </summary>
|
||||
protected float _startPosY;
|
||||
/// <summary>
|
||||
/// Ширина окна отрисовки
|
||||
/// </summary>
|
||||
private Integer _pictureWidth = null;
|
||||
/// <summary>
|
||||
/// Высота окна отрисовки
|
||||
/// </summary>
|
||||
private Integer _pictureHeight = null;
|
||||
/// <summary>
|
||||
/// Ширина отрисовки корабля
|
||||
/// </summary>
|
||||
private int _battleshipWidth = 120;
|
||||
/// <summary>
|
||||
/// Высота отрисовки корабля
|
||||
/// </summary>
|
||||
private int _battleshipHeight = 50;
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес корабля</param>
|
||||
/// <param name="bodyColor">Цвет корпуса</param>
|
||||
public DrawningBattleship(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
|
||||
//Battleship.Init(speed, weight, bodyColor);
|
||||
Random random = new Random();
|
||||
int[] ArrayBlocks = new int[]{2, 4, 6};
|
||||
|
||||
switch (random.nextInt(3)){
|
||||
case 0:
|
||||
iDrawingBlocks = new DrawningBlocks(ArrayBlocks[random.nextInt(0, 3)], Color.BLACK);
|
||||
break;
|
||||
case 1:
|
||||
iDrawingBlocks = new DrawningRoundBlocks(ArrayBlocks[random.nextInt(0, 3)]);
|
||||
break;
|
||||
case 2:
|
||||
iDrawingBlocks = new DrawningTriangleBlocks(ArrayBlocks[random.nextInt(0, 3)]);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
Battleship = new EntityBattleship(speed, weight, bodyColor);
|
||||
}
|
||||
protected DrawningBattleship(int speed, float weight, Color bodyColor, int battleshipWidth, int battleshipHeight)
|
||||
{
|
||||
this(speed, weight, bodyColor);
|
||||
_battleshipWidth = battleshipWidth;
|
||||
_battleshipHeight = battleshipHeight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Установка позиции корабля
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
public void SetPosition(int x, int y, int width, int height)
|
||||
{
|
||||
if ((x > 0 && y > 0) && (_battleshipHeight + y < height) && (_battleshipWidth + x < width))
|
||||
{
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (_pictureWidth == null || _pictureHeight == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
// вправо
|
||||
case Right:
|
||||
if (_startPosX + _battleshipWidth + Battleship.GetStep() < _pictureWidth)
|
||||
{
|
||||
_startPosX += Battleship.GetStep();
|
||||
}
|
||||
break;
|
||||
//влево
|
||||
case Left:
|
||||
if (_startPosX - Battleship.GetStep() >= 0)
|
||||
{
|
||||
_startPosX -= Battleship.GetStep();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
//вверх
|
||||
case Up:
|
||||
if (_startPosY - Battleship.GetStep() >= 0)
|
||||
{
|
||||
_startPosY -= Battleship.GetStep();
|
||||
}
|
||||
|
||||
break;
|
||||
//вниз
|
||||
case Down:
|
||||
if (_startPosY + _battleshipHeight + Battleship.GetStep() < _pictureHeight)
|
||||
{
|
||||
_startPosY += Battleship.GetStep();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Отрисовка корабля
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (_startPosX < 0 || _startPosY < 0
|
||||
|| _pictureHeight == null || _pictureWidth == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
g.setColor(Color.BLACK);
|
||||
|
||||
//Корпус корабля
|
||||
g.setColor(Battleship.bodyColor);
|
||||
g.fillPolygon(new int[]{(int) _startPosX, (int) _startPosX , (int) _startPosX+80, (int) _startPosX + 120, (int)_startPosX + 80, (int)_startPosX},
|
||||
new int[]{(int) _startPosY, (int) _startPosY+50 , (int) _startPosY+50, (int) _startPosY + 25, (int)_startPosY, (int)_startPosY}, 6);
|
||||
//Пушка
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect((int)_startPosX + 20, (int)_startPosY + 20, 30, 10);
|
||||
g.drawRect((int)_startPosX + 50,(int) _startPosY + 10, 20, 30);
|
||||
g.fillRect((int)_startPosX + 20, (int)_startPosY + 20, 30, 10);
|
||||
g.fillRect((int)_startPosX + 50, (int)_startPosY + 10, 20, 30);
|
||||
|
||||
//Отсек
|
||||
g.setColor(Color.BLUE);
|
||||
g.drawOval((int)_startPosX+80, (int)_startPosY+15, 20, 20);
|
||||
g.fillOval((int)_startPosX + 80, (int)_startPosY + 15, 20, 20);
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillRect((int)_startPosX-5, (int)_startPosY+10, 5, 5);
|
||||
g.fillRect((int)_startPosX - 5, (int)_startPosY + 35, 5, 5);
|
||||
iDrawingBlocks.DrawBlocks(g, (int) _startPosX, (int) _startPosY, Color.BLACK);
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Смена границ формы отрисовки
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина картинки</param>
|
||||
/// <param name="height">Высота картинки</param>
|
||||
public void ChangeBorders(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_pictureWidth <= _battleshipWidth || _pictureHeight <= _battleshipHeight)
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
return;
|
||||
}
|
||||
if (_startPosX + _battleshipWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _battleshipWidth;
|
||||
}
|
||||
if (_startPosY + _battleshipHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _battleshipHeight;
|
||||
}
|
||||
|
||||
}
|
||||
public float[] GetCurrentPosition()
|
||||
{
|
||||
return new float[] {_startPosY, _startPosX + _battleshipWidth, /*DOWN*/ _startPosY + _battleshipHeight, /*LEFT*/ _startPosX};
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningBlocks implements IDrawningBlocks {
|
||||
private DirectionBlocksOnDeck blocksCount = DirectionBlocksOnDeck.Two;
|
||||
private Color color;
|
||||
public DrawningBlocks(int count, Color color){
|
||||
SetBlocks(count);
|
||||
this.color = color;
|
||||
}
|
||||
@Override
|
||||
public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) {
|
||||
g.setColor(color != null ? color : Color.BLACK);
|
||||
switch (blocksCount) {
|
||||
case Four -> {
|
||||
g.fillRect(x + 58, y, 8, 5);
|
||||
g.fillRect(x + 70, y, 8, 5);
|
||||
g.fillRect(x + 70, y + 40, 8, 5);
|
||||
g.fillRect(x + 58, y + 40, 8, 5);
|
||||
}
|
||||
case Six -> {
|
||||
g.fillRect(x + 46, y, 8, 5);
|
||||
g.fillRect(x + 58, y , 8, 5);
|
||||
g.fillRect(x + 70, y, 8, 5);
|
||||
g.fillRect(x + 46, y + 45, 8, 5);
|
||||
g.fillRect(x + 58, y + 45, 8, 5);
|
||||
g.fillRect(x + 70, y + 45, 8, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SetBlocks(int count) {
|
||||
switch(count)
|
||||
{
|
||||
case 4:
|
||||
blocksCount = DirectionBlocksOnDeck.Four;
|
||||
break;
|
||||
case 6:
|
||||
blocksCount = DirectionBlocksOnDeck.Six;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningLinkor extends DrawningBattleship {
|
||||
|
||||
public DrawningLinkor(int speed, float weight, Color bodyColor, Color dopColor, boolean turret, boolean missileBay) {
|
||||
super(speed, weight, bodyColor, 120, 50);
|
||||
Battleship = new EntityLinkor(speed, weight, bodyColor, dopColor, turret, missileBay);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
|
||||
if(!(Battleship instanceof EntityLinkor linkor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
super.DrawTransport(g);
|
||||
EntityLinkor entityLinkor = (EntityLinkor) Battleship;
|
||||
|
||||
if (entityLinkor.turret)
|
||||
{
|
||||
//Brush dopBrushRed = new SolidBrush(Color.Red);
|
||||
g.setColor(entityLinkor.dopColor);
|
||||
g.fillOval((int)_startPosX + 15, (int)_startPosY, 20, 20);
|
||||
g.fillOval((int) _startPosX + 15, (int)_startPosY + 30, 20, 20);
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillRect((int)_startPosX + 20, (int)_startPosY+5, 23, 7);
|
||||
g.fillRect((int)_startPosX + 20,(int) _startPosY + 37, 23, 7);
|
||||
|
||||
}
|
||||
if (entityLinkor.missileBay)
|
||||
{
|
||||
|
||||
g.setColor(Color.BLACK);
|
||||
g.fillOval((int)_startPosX, (int)_startPosY + 15, 20, 20);
|
||||
g.setColor(entityLinkor.dopColor);
|
||||
g.fillOval((int)_startPosX+5,(int) _startPosY + 20, 10, 10);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningObjectBattleship implements IDrawningObject {
|
||||
|
||||
private DrawningBattleship _battleship = null;
|
||||
public DrawningObjectBattleship(DrawningBattleship battleship){
|
||||
_battleship = battleship;
|
||||
}
|
||||
@Override
|
||||
public float getStep() {
|
||||
if (_battleship.Battleship != null) {
|
||||
return _battleship.Battleship.GetStep();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SetObject(int x, int y, int width, int height) {
|
||||
if (_battleship != null) _battleship.SetPosition(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void MoveObject(Direction direction) {
|
||||
if (_battleship!= null) _battleship.MoveTransport(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawningObject(Graphics g) {
|
||||
if (_battleship != null) _battleship.DrawTransport((Graphics2D) g);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float[] GetCurrentPosition() {
|
||||
if (_battleship != null) {return _battleship.GetCurrentPosition();}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningRoundBlocks implements IDrawningBlocks{
|
||||
|
||||
private DirectionBlocksOnDeck blocksOnDeck = DirectionBlocksOnDeck.Two;
|
||||
@Override
|
||||
public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) {
|
||||
g.setColor(Color.black);
|
||||
switch(blocksOnDeck){
|
||||
case Four -> {
|
||||
g.fillOval(x + 56, y , 8, 8);
|
||||
g.fillOval(x + 68, y , 8, 8);
|
||||
g.fillOval(x + 68, y +40, 8, 8);
|
||||
g.fillOval(x + 56, y + 40, 8, 8);
|
||||
}
|
||||
case Six -> {
|
||||
g.fillOval(x + 54, y , 8, 8);
|
||||
g.fillOval(x + 66, y , 8, 8);
|
||||
g.fillOval(x + 78, y , 8, 8);
|
||||
g.fillOval(x + 78, y + 40, 8, 8);
|
||||
g.fillOval(x + 66, y + 40, 8, 8);
|
||||
g.fillOval(x + 54, y + 40, 8, 8);
|
||||
}}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SetBlocks(int count) {
|
||||
switch(count)
|
||||
{
|
||||
case 2:
|
||||
blocksOnDeck = DirectionBlocksOnDeck.Two;
|
||||
break;
|
||||
case 4:
|
||||
blocksOnDeck = DirectionBlocksOnDeck.Four;
|
||||
break;
|
||||
case 6:
|
||||
blocksOnDeck = DirectionBlocksOnDeck.Six;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public DrawningRoundBlocks (int num) {
|
||||
SetBlocks(num);
|
||||
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningTriangleBlocks implements IDrawningBlocks {
|
||||
|
||||
private DirectionBlocksOnDeck blocksOnDeck = DirectionBlocksOnDeck.Two;
|
||||
|
||||
@Override
|
||||
public void DrawBlocks(Graphics g, int x, int y, Color bodyColor) {
|
||||
g.setColor(Color.black);
|
||||
switch(blocksOnDeck){
|
||||
case Four -> {
|
||||
g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 7, y, y + 7}, 3);
|
||||
g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 49, y + 42, y + 49}, 3);
|
||||
|
||||
g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3);
|
||||
g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3);
|
||||
|
||||
|
||||
|
||||
}
|
||||
case Six -> {
|
||||
g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 7, y, y + 7}, 3);
|
||||
g.fillPolygon(new int[]{x + 66, x + 64, x+ 80 }, new int[]{y + 7, y, y + 7}, 3);
|
||||
g.fillPolygon(new int[]{x + 46, x + 54, x+ 60 }, new int[]{y + 49, y + 42, y + 49}, 3);
|
||||
g.fillPolygon(new int[]{x + 66, x + 64, x+ 80 }, new int[]{y + 49, y + 42, y + 49}, 3);
|
||||
g.fillPolygon(new int[]{x + 58, x + 66, x+ 72}, new int[]{y + 7, y, y + 7}, 3);
|
||||
g.fillPolygon(new int[]{x + 58, x + 66, x+72}, new int[]{y + 49, y + 42, y + 49}, 3);
|
||||
|
||||
}}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SetBlocks(int count) {
|
||||
switch(count)
|
||||
{
|
||||
case 2:
|
||||
blocksOnDeck = DirectionBlocksOnDeck.Two;
|
||||
break;
|
||||
case 4:
|
||||
blocksOnDeck = DirectionBlocksOnDeck.Four;
|
||||
break;
|
||||
case 6:
|
||||
blocksOnDeck = DirectionBlocksOnDeck.Six;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
public DrawningTriangleBlocks (int num) {
|
||||
SetBlocks(num);
|
||||
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityBattleship {
|
||||
private int speed = 0;
|
||||
private float weight = 0;
|
||||
Color bodyColor;
|
||||
public int GetSpeed() {
|
||||
return speed;
|
||||
}
|
||||
public float GetWeight() {
|
||||
return weight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Цвет корпуса
|
||||
/// </summary>
|
||||
public Color GetBodyColor() {
|
||||
return bodyColor;
|
||||
}
|
||||
/// <summary>
|
||||
/// Шаг перемещения корабля
|
||||
/// </summary>
|
||||
public float GetStep(){
|
||||
return speed * 100 / weight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса корабля
|
||||
/// </summary>
|
||||
/// <param name="speed"></param>
|
||||
/// <param name="weight"></param>
|
||||
/// <param name="bodyColor"></param>
|
||||
/// <returns></returns>
|
||||
public EntityBattleship(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
this.speed = speed <= 0 ? rnd.nextInt(950) + 1050 : speed;
|
||||
this.weight = weight <= 0 ? rnd.nextInt(40) + 70 : weight;
|
||||
this.bodyColor = bodyColor;
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityLinkor extends EntityBattleship {
|
||||
public final boolean turret;
|
||||
public final boolean missileBay;
|
||||
public final Color dopColor;
|
||||
|
||||
public EntityLinkor(int speed, float weight, Color bodyColor, Color DopColor, boolean Turret, boolean MissileBay) {
|
||||
super(speed, weight, bodyColor);
|
||||
dopColor = DopColor;
|
||||
missileBay = MissileBay;
|
||||
turret = Turret;
|
||||
}
|
||||
}
|
5
FormBattleship.java
Normal file
5
FormBattleship.java
Normal file
@ -0,0 +1,5 @@
|
||||
public class FormBattleship {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
137
FormMap.java
137
FormMap.java
@ -1,137 +0,0 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.awt.event.ComponentListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Random;
|
||||
public class FormMap extends JComponent {
|
||||
private AbstractMap _abstractMap;
|
||||
private DrawningBattleship _battleship;
|
||||
private BufferedImage bufferedImage;
|
||||
public static void main(String[] args) {
|
||||
FormMap formMap = new FormMap();
|
||||
}
|
||||
public FormMap() {
|
||||
JFrame form = new JFrame("Карта");
|
||||
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
form.setSize(1000, 500);
|
||||
form.setVisible(true);
|
||||
form.setLocationRelativeTo(null);
|
||||
Panel statusPanel = new Panel();
|
||||
statusPanel.setBackground(Color.WHITE);
|
||||
statusPanel.setLayout(new FlowLayout());
|
||||
setLayout(new BorderLayout());
|
||||
form.add(statusPanel, BorderLayout.SOUTH);
|
||||
Label speedLabel = new Label("Скорость: ");
|
||||
Label weightLabel = new Label("Вес: ");
|
||||
Label colorLabel = new Label("Цвет: ");
|
||||
|
||||
String[] maps = {
|
||||
"Простая карта",
|
||||
"Морская карта"
|
||||
|
||||
};
|
||||
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 ("Простая карта"):
|
||||
_abstractMap = new SimpleMap();
|
||||
break;
|
||||
case ("Морская карта"):
|
||||
_abstractMap = new SeaMap();
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
JButton createButton = new JButton("Создать");
|
||||
createButton.addActionListener(e -> {
|
||||
|
||||
Random rnd = new Random();
|
||||
|
||||
var battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||||
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
|
||||
rnd.nextInt(0, 256)));
|
||||
speedLabel.setText("Скорость: " + battleship.Battleship.GetSpeed());
|
||||
weightLabel.setText("Вес: " + (int)battleship.Battleship.GetWeight());
|
||||
colorLabel.setText("Цвет: " + battleship.Battleship.GetBodyColor().getRed() + " " +
|
||||
battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() );
|
||||
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(battleship));
|
||||
repaint();
|
||||
|
||||
});
|
||||
JButton modifiedButton = new JButton("Модификация");
|
||||
modifiedButton.addActionListener(e -> {
|
||||
Random rnd = new Random();
|
||||
|
||||
Color colorFirst = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
|
||||
Color colorSecond = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
|
||||
|
||||
var battleship = new DrawningLinkor(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
|
||||
colorFirst,
|
||||
colorSecond,
|
||||
rnd.nextBoolean(),
|
||||
rnd.nextBoolean());
|
||||
battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75);
|
||||
speedLabel.setText("Скорость: " + battleship.Battleship.GetSpeed());
|
||||
weightLabel.setText("Вес: " + battleship.Battleship.GetWeight());
|
||||
colorLabel.setText("Цвет: " + battleship.Battleship.GetBodyColor().getRed() + " " + battleship.Battleship.GetBodyColor().getGreen() + " " + battleship.Battleship.GetBodyColor().getBlue() );
|
||||
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(1000, 490, new DrawningObjectBattleship(battleship));
|
||||
repaint();
|
||||
});
|
||||
statusPanel.add(mapSelectComboBox);
|
||||
statusPanel.add(createButton);
|
||||
statusPanel.add(modifiedButton);
|
||||
statusPanel.add(speedLabel);
|
||||
statusPanel.add(weightLabel);
|
||||
statusPanel.add(colorLabel);
|
||||
JButton upButton = new JButton("↑");
|
||||
JButton rightButton = new JButton("→");
|
||||
JButton leftButton = new JButton("←");
|
||||
JButton downButton = new JButton("↓");
|
||||
upButton.addActionListener(e -> {
|
||||
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Up);
|
||||
repaint();
|
||||
});
|
||||
|
||||
|
||||
rightButton.addActionListener(e -> {
|
||||
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Right);
|
||||
repaint();
|
||||
});
|
||||
|
||||
|
||||
leftButton.addActionListener(e -> {
|
||||
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Left);
|
||||
repaint();
|
||||
});
|
||||
|
||||
|
||||
downButton.addActionListener(e -> {
|
||||
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.Down);
|
||||
repaint();
|
||||
});
|
||||
|
||||
statusPanel.add(leftButton);
|
||||
statusPanel.add(upButton);
|
||||
statusPanel.add(rightButton);
|
||||
statusPanel.add(downButton);
|
||||
|
||||
form.getContentPane().add(this);
|
||||
super.repaint();
|
||||
}
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,1000,490,null);
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawningBlocks {
|
||||
void DrawBlocks(Graphics g, int startX, int startY, Color bodyColor);
|
||||
void SetBlocks(int count);
|
||||
|
||||
}
|
@ -1,9 +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();
|
||||
}
|
44
SeaMap.java
44
SeaMap.java
@ -1,44 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class SeaMap extends AbstractMap {
|
||||
|
||||
@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 < 10)
|
||||
{
|
||||
int x = _random.nextInt(1, 99);
|
||||
int y = _random.nextInt(1, 100);
|
||||
if (_map[x][y] == _freeRoad)
|
||||
{
|
||||
_map[x][y] = _barrier;
|
||||
_map[x + 1][y] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawRoadPart(Graphics g, int i, int j) {
|
||||
g.setColor(Color.BLUE);
|
||||
g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawBarrierPart(Graphics g, int i, int j) {
|
||||
g.setColor(Color.WHITE);
|
||||
g.fillPolygon(new int[]{i * (int) _size_x, i * (int) _size_x + 18, i * (int) _size_x - 18},new int[]{ j * (int) _size_y, j * (int) _size_y + 20,
|
||||
j * (int) _size_y + 20}, 3);
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class SimpleMap extends AbstractMap {
|
||||
|
||||
Color roadColor = Color.GRAY;
|
||||
Color barrierColor = Color.BLACK;
|
||||
@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(0, 100);
|
||||
int y = _random.nextInt(0, 100);
|
||||
if (_map[x][y] == _freeRoad)
|
||||
{
|
||||
_map[x][y] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawRoadPart(Graphics g, int i, int j) {
|
||||
g.setColor(roadColor);
|
||||
g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void DrawBarrierPart(Graphics g, int i, int j) {
|
||||
g.setColor(barrierColor);
|
||||
g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user