Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
de0ba340c3 | |||
635427fed8 | |||
9cce5599bf | |||
8573ebc9cd | |||
c56ee336ef | |||
79c31bbb8b | |||
7384aa9514 | |||
9a93dd2af5 | |||
1d7e6e80a0 | |||
405904860a | |||
e71eaf0f2d | |||
df4d079c07 | |||
92be17d6f6 | |||
33ef22cccc | |||
de8c7eb3ae | |||
0db5c98d2d |
157
AbstractMap.java
Normal file
157
AbstractMap.java
Normal file
@ -0,0 +1,157 @@
|
||||
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);
|
||||
|
||||
}
|
7
Direction.java
Normal file
7
Direction.java
Normal file
@ -0,0 +1,7 @@
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
None
|
||||
}
|
5
DirectionBlocksOnDeck.java
Normal file
5
DirectionBlocksOnDeck.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum DirectionBlocksOnDeck {
|
||||
Two,
|
||||
Four,
|
||||
Six
|
||||
}
|
198
DrawningBattleship.java
Normal file
198
DrawningBattleship.java
Normal file
@ -0,0 +1,198 @@
|
||||
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);
|
||||
}
|
||||
public DrawningBattleship(EntityBattleship entity, IDrawningBlocks blocks){
|
||||
Battleship = entity;
|
||||
iDrawingBlocks = blocks;
|
||||
}
|
||||
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};
|
||||
}
|
||||
}
|
45
DrawningBlocks.java
Normal file
45
DrawningBlocks.java
Normal file
@ -0,0 +1,45 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
44
DrawningLinkor.java
Normal file
44
DrawningLinkor.java
Normal file
@ -0,0 +1,44 @@
|
||||
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);
|
||||
}
|
||||
public DrawningLinkor(EntityBattleship battleship, IDrawningBlocks blocks){
|
||||
super(battleship, blocks);
|
||||
Battleship = battleship;
|
||||
}
|
||||
@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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
38
DrawningObjectBattleship.java
Normal file
38
DrawningObjectBattleship.java
Normal file
@ -0,0 +1,38 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
48
DrawningRoundBlocks.java
Normal file
48
DrawningRoundBlocks.java
Normal file
@ -0,0 +1,48 @@
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
52
DrawningTriangleBlocks.java
Normal file
52
DrawningTriangleBlocks.java
Normal file
@ -0,0 +1,52 @@
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
40
EntityBattleship.java
Normal file
40
EntityBattleship.java
Normal file
@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
}
|
14
EntityLinkor.java
Normal file
14
EntityLinkor.java
Normal file
@ -0,0 +1,14 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,5 +1,110 @@
|
||||
public class FormBattleship {
|
||||
public static void main(String[] args) {
|
||||
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 FormBattleship extends JComponent {
|
||||
private AbstractMap _abstractMap;
|
||||
private DrawningBattleship _battleship;
|
||||
private DrawningBattleship SelectedBattleship;
|
||||
private EntityBattleship _entityBattleship;
|
||||
private BufferedImage bufferedImage = null;
|
||||
|
||||
public DrawningBattleship GetSelectedBattleship() {
|
||||
return SelectedBattleship;
|
||||
}
|
||||
public FormBattleship(JDialog caller) {
|
||||
Panel statusPanel = new Panel();
|
||||
statusPanel.setBackground(Color.WHITE);
|
||||
statusPanel.setLayout(new FlowLayout());
|
||||
setLayout(new BorderLayout());
|
||||
add(statusPanel, BorderLayout.SOUTH);
|
||||
Label speedLabel = new Label("Скорость: ");
|
||||
Label weightLabel = new Label("Вес: ");
|
||||
Label colorLabel = new Label("Цвет: ");
|
||||
|
||||
|
||||
JButton createButton = new JButton("Создать");
|
||||
createButton.addActionListener(e -> {
|
||||
|
||||
Random rnd = new Random();
|
||||
Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
|
||||
_battleship = new DrawningBattleship(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||||
colorFirst);
|
||||
_battleship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), getWidth(), getHeight() - 75);
|
||||
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() );
|
||||
repaint();
|
||||
|
||||
});
|
||||
JButton modifiedButton = new JButton("Модификация");
|
||||
modifiedButton.addActionListener(e -> {
|
||||
Random rnd = new Random();
|
||||
Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
||||
Color colorSecond = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
|
||||
_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), getWidth(), getHeight() - 75);
|
||||
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 selectButton = new JButton("Выбрать");
|
||||
selectButton.addActionListener(e -> {
|
||||
SelectedBattleship = _battleship;
|
||||
caller.dispose();
|
||||
});
|
||||
statusPanel.add(createButton);
|
||||
statusPanel.add(modifiedButton);
|
||||
statusPanel.add(selectButton);
|
||||
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 (_battleship != null) _battleship.MoveTransport(Direction.Up);
|
||||
repaint();
|
||||
});
|
||||
|
||||
|
||||
rightButton.addActionListener(e -> {
|
||||
if (_battleship != null) _battleship.MoveTransport(Direction.Right);
|
||||
repaint();
|
||||
});
|
||||
|
||||
|
||||
leftButton.addActionListener(e -> {
|
||||
if (_battleship != null) _battleship.MoveTransport(Direction.Left);
|
||||
repaint();
|
||||
});
|
||||
|
||||
|
||||
downButton.addActionListener(e -> {
|
||||
if (_battleship != null) _battleship.MoveTransport(Direction.Down);
|
||||
repaint();
|
||||
});
|
||||
|
||||
statusPanel.add(leftButton);
|
||||
statusPanel.add(upButton);
|
||||
statusPanel.add(rightButton);
|
||||
statusPanel.add(downButton);
|
||||
|
||||
}
|
||||
}
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
if (_battleship != null) _battleship.DrawTransport(g2);
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
}
|
134
FormMap.java
Normal file
134
FormMap.java
Normal file
@ -0,0 +1,134 @@
|
||||
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 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();
|
||||
}
|
||||
|
||||
}
|
197
FormMapWithSetBattleship.java
Normal file
197
FormMapWithSetBattleship.java
Normal file
@ -0,0 +1,197 @@
|
||||
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;
|
||||
|
||||
public class FormMapWithSetBattleship extends JComponent {
|
||||
private BufferedImage bufferedImage = null;
|
||||
|
||||
private MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap> _mapBattleshipsCollectionGeneric;
|
||||
public static void main(String[] args) {
|
||||
FormMapWithSetBattleship formMap = new FormMapWithSetBattleship();
|
||||
}
|
||||
public FormMapWithSetBattleship(){
|
||||
JFrame form = new JFrame("FormMapWithSetBattleship");
|
||||
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
form.setSize(750, 500);
|
||||
form.setLocationRelativeTo(null);
|
||||
|
||||
Panel statusPanel = new Panel();
|
||||
statusPanel.setBackground(Color.WHITE);
|
||||
statusPanel.setLayout(new GridLayout(0, 1, 20, 20));
|
||||
setLayout(new BorderLayout());
|
||||
add(statusPanel, BorderLayout.EAST);
|
||||
String[] maps = {
|
||||
"Простая карта",
|
||||
"Морская карта",
|
||||
};
|
||||
JComboBox mapSelectComboBox = new JComboBox(maps);
|
||||
mapSelectComboBox.setEditable(true);
|
||||
mapSelectComboBox.addActionListener(e -> {
|
||||
AbstractMap map = null;
|
||||
String item = (String)mapSelectComboBox.getSelectedItem();
|
||||
switch (item) {
|
||||
case "Простая карта":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "Морская карта":
|
||||
map = new SeaMap();
|
||||
break;
|
||||
}
|
||||
if (map != null)
|
||||
{
|
||||
_mapBattleshipsCollectionGeneric = new MapWithSetBattleshipsGeneric<DrawningObjectBattleship, AbstractMap>
|
||||
(1000, 700, map);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mapBattleshipsCollectionGeneric = null;
|
||||
}
|
||||
});
|
||||
statusPanel.add(mapSelectComboBox);
|
||||
|
||||
|
||||
JButton addBattleshipButton = new JButton("Добавить корабль");
|
||||
addBattleshipButton.addActionListener(e -> {
|
||||
|
||||
if (_mapBattleshipsCollectionGeneric == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
JDialog dialog = new JDialog(form, "Диалого", true);
|
||||
FormBattleship formShip = new FormBattleship(dialog);
|
||||
dialog.setSize(800, 500);
|
||||
dialog.setContentPane(formShip);
|
||||
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||
dialog.setVisible(true);
|
||||
DrawningObjectBattleship battleship = new DrawningObjectBattleship(formShip.GetSelectedBattleship());
|
||||
if (_mapBattleshipsCollectionGeneric.Add(battleship) != -1) {
|
||||
JOptionPane.showMessageDialog(form, "Объект добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
|
||||
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
|
||||
repaint();
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(form, "Объект не добавлен", "Добавление", JOptionPane.OK_CANCEL_OPTION);
|
||||
}
|
||||
});
|
||||
statusPanel.add(addBattleshipButton);
|
||||
|
||||
JFormattedTextField maskedTextFieldPosition = null;
|
||||
try {
|
||||
MaskFormatter positionMask = new MaskFormatter("##");
|
||||
maskedTextFieldPosition = new JFormattedTextField(positionMask);
|
||||
statusPanel.add(maskedTextFieldPosition);
|
||||
}
|
||||
catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
JButton deleteBattleshipButton = new JButton("Удалить корабль");
|
||||
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
|
||||
deleteBattleshipButton.addActionListener(e -> {
|
||||
if ((String)mapSelectComboBox.getSelectedItem() == null) {
|
||||
return;
|
||||
}
|
||||
if (finalMaskedTextFieldPosition == null) {
|
||||
return;
|
||||
}
|
||||
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
|
||||
if (_mapBattleshipsCollectionGeneric.Subtraction(position) != null) {
|
||||
JOptionPane.showMessageDialog(form, "Объект удален", "Удаление", JOptionPane.OK_CANCEL_OPTION);
|
||||
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
|
||||
repaint();
|
||||
}
|
||||
else{
|
||||
JOptionPane.showMessageDialog(form, "Объект не удален", "Удаление", JOptionPane.OK_CANCEL_OPTION);
|
||||
}
|
||||
});
|
||||
statusPanel.add(deleteBattleshipButton);
|
||||
JButton showStorageButton = new JButton("Хранилище");
|
||||
showStorageButton.addActionListener(e -> {
|
||||
|
||||
if (_mapBattleshipsCollectionGeneric == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bufferedImage = _mapBattleshipsCollectionGeneric.ShowSet();
|
||||
repaint();
|
||||
});
|
||||
statusPanel.add(showStorageButton);
|
||||
|
||||
JButton showOnMapButton = new JButton("Карта");
|
||||
showOnMapButton.addActionListener(e -> {
|
||||
|
||||
if (_mapBattleshipsCollectionGeneric == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bufferedImage = _mapBattleshipsCollectionGeneric.ShowOnMap();
|
||||
});
|
||||
statusPanel.add(showOnMapButton);
|
||||
|
||||
ActionListener moveButtonListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_mapBattleshipsCollectionGeneric == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
String name = e.getActionCommand();
|
||||
Direction dir = Direction.None;
|
||||
switch (name)
|
||||
{
|
||||
case "↑":
|
||||
dir = Direction.Up;
|
||||
break;
|
||||
case "↓":
|
||||
dir = Direction.Down;
|
||||
break;
|
||||
case "←":
|
||||
dir = Direction.Left;
|
||||
break;
|
||||
case "→":
|
||||
dir = Direction.Right;
|
||||
break;
|
||||
}
|
||||
bufferedImage = _mapBattleshipsCollectionGeneric.MoveObject(dir);
|
||||
}
|
||||
};
|
||||
|
||||
//Кнопки управления
|
||||
JButton moveDownButton = new JButton("↓");
|
||||
moveDownButton.addActionListener(moveButtonListener);
|
||||
|
||||
JButton moveUpButton = new JButton("↑");
|
||||
moveUpButton.addActionListener(moveButtonListener);
|
||||
|
||||
JButton moveLeftButton = new JButton("←");
|
||||
moveLeftButton.addActionListener(moveButtonListener);
|
||||
|
||||
JButton moveRightButton = new JButton("→");
|
||||
moveRightButton.addActionListener(moveButtonListener);
|
||||
|
||||
JButton showGalleryButton = new JButton("Галерея");
|
||||
showGalleryButton.addActionListener(e -> {
|
||||
new FormParameterClass();
|
||||
});
|
||||
statusPanel.add(showGalleryButton);
|
||||
statusPanel.add(moveUpButton);
|
||||
statusPanel.add(moveDownButton);
|
||||
statusPanel.add(moveLeftButton);
|
||||
statusPanel.add(moveRightButton);
|
||||
form.getContentPane().add(this);
|
||||
form.setVisible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,600,500,null);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
|
95
FormParameterClass.java
Normal file
95
FormParameterClass.java
Normal file
@ -0,0 +1,95 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormParameterClass extends JComponent {
|
||||
private DrawningBattleship battleship1;
|
||||
private DrawningBattleship battleship2;
|
||||
private DrawningBattleship battleship3;
|
||||
private DrawningBattleship battleship4;
|
||||
private DrawningBattleship battleship5;
|
||||
MyParametrClass<EntityBattleship, IDrawningBlocks> parameterClass;
|
||||
public FormParameterClass(){
|
||||
JFrame form = new JFrame("Экземпляры от параметризованного класса");
|
||||
form.setSize(900, 500);
|
||||
form.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
form.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("Создать");
|
||||
showRandomEntity.addActionListener(e -> {
|
||||
Random random = new Random();
|
||||
int[] arrayBlocks = {2, 4, 6};
|
||||
if (parameterClass == null) {
|
||||
parameterClass = new MyParametrClass<EntityBattleship, IDrawningBlocks>(20, 20);
|
||||
for (int i = 0; i < 20; i ++) {
|
||||
if (random.nextBoolean()) {
|
||||
parameterClass.Insert(new EntityBattleship(random.nextInt(100), random.nextInt(100),
|
||||
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))));
|
||||
}
|
||||
else {
|
||||
parameterClass.Insert(new EntityLinkor(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 rnd = random.nextInt(3);
|
||||
|
||||
switch (rnd) {
|
||||
case 0:
|
||||
parameterClass.Insert(new DrawningBlocks(arrayBlocks[random.nextInt(3)],
|
||||
new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255))));
|
||||
break;
|
||||
|
||||
case 1:
|
||||
parameterClass.Insert(new DrawningRoundBlocks(arrayBlocks[random.nextInt(3)]));
|
||||
break;
|
||||
|
||||
case 2:
|
||||
parameterClass.Insert(new DrawningTriangleBlocks(arrayBlocks[random.nextInt(3)]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
battleship1 = parameterClass.GetDrawningBattleship();
|
||||
battleship1.SetPosition(200, 200, form.getWidth(), form.getHeight() - 75);
|
||||
|
||||
battleship2 = parameterClass.GetDrawningBattleship();
|
||||
battleship2.SetPosition(400, 200, form.getWidth(), form.getHeight() - 75);
|
||||
|
||||
battleship3 = parameterClass.GetDrawningBattleship();
|
||||
battleship3.SetPosition(600, 200, form.getWidth(), form.getHeight() - 75);
|
||||
|
||||
battleship4 = parameterClass.GetDrawningBattleship();
|
||||
battleship4.SetPosition(300, 300, form.getWidth(), form.getHeight() - 75);
|
||||
|
||||
battleship5 = parameterClass.GetDrawningBattleship();
|
||||
battleship5.SetPosition(500, 300, form.getWidth(), form.getHeight() - 75);
|
||||
repaint();
|
||||
});
|
||||
statusPanel.add(showRandomEntity);
|
||||
|
||||
form.getContentPane().add(this);
|
||||
|
||||
form.setVisible(true);
|
||||
}
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
if (battleship1 != null) battleship1.DrawTransport(g2);
|
||||
if (battleship2 != null) battleship2.DrawTransport(g2);
|
||||
if (battleship3 != null) battleship3.DrawTransport(g2);
|
||||
if (battleship4 != null) battleship4.DrawTransport(g2);
|
||||
if (battleship5 != null) battleship5.DrawTransport(g2);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
7
IDrawningBlocks.java
Normal file
7
IDrawningBlocks.java
Normal file
@ -0,0 +1,7 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawningBlocks {
|
||||
void DrawBlocks(Graphics g, int startX, int startY, Color bodyColor);
|
||||
void SetBlocks(int count);
|
||||
|
||||
}
|
9
IDrawningObject.java
Normal file
9
IDrawningObject.java
Normal file
@ -0,0 +1,9 @@
|
||||
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();
|
||||
}
|
138
MapWithSetBattleshipsGeneric.java
Normal file
138
MapWithSetBattleshipsGeneric.java
Normal file
@ -0,0 +1,138 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class MapWithSetBattleshipsGeneric <T extends IDrawningObject, U extends AbstractMap>{
|
||||
|
||||
/// Ширина окна отрисовки
|
||||
private final int _pictureWidth;
|
||||
/// Высота окна отрисовки
|
||||
private final int _pictureHeight;
|
||||
/// Размер занимаемого объектом места (ширина)
|
||||
private final int _placeSizeWidth = 250;
|
||||
/// Размер занимаемого объектом места (высота)
|
||||
private final int _placeSizeHeight = 100;
|
||||
private final SetBattleshipGeneric<T> _setBattleship;
|
||||
/// Карта
|
||||
private final U _map;
|
||||
/// Конструктор
|
||||
public MapWithSetBattleshipsGeneric(int picWidth, int picHeight, U map)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_setBattleship = new SetBattleshipGeneric<T>(width * height);
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_map = map;
|
||||
}
|
||||
public int Add(T battleship)
|
||||
{
|
||||
return this._setBattleship.Insert(battleship);
|
||||
}
|
||||
public T Subtraction(int position)
|
||||
{
|
||||
return this._setBattleship.Remove(position);
|
||||
}
|
||||
public BufferedImage ShowSet()
|
||||
{
|
||||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics gr = bmp.getGraphics();
|
||||
DrawBackground((Graphics2D)gr);
|
||||
DrawBattleship((Graphics2D)gr);
|
||||
return bmp;
|
||||
}
|
||||
public BufferedImage ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
for (int i = 0; i < _setBattleship.Count(); i++)
|
||||
{
|
||||
var battleship = _setBattleship.Get(i);
|
||||
if (battleship != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, battleship);
|
||||
}
|
||||
}
|
||||
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 = _setBattleship.Count() - 1;
|
||||
for (int i = 0; i < _setBattleship.Count(); i++)
|
||||
{
|
||||
if (_setBattleship.Get(i) == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var battleship = _setBattleship.Get(j);
|
||||
if (battleship != null)
|
||||
{
|
||||
_setBattleship.Insert(battleship, i);
|
||||
_setBattleship.Remove(j);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j <= i)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void DrawBackground(Graphics2D g)
|
||||
{
|
||||
g.setColor(Color.blue);
|
||||
g.fillRect(0, 0, _pictureWidth, _pictureHeight);
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
|
||||
for (int j = 0; j <= _pictureHeight / _placeSizeHeight; ++j)
|
||||
{
|
||||
g.setStroke(new BasicStroke(7));
|
||||
g.setColor(Color.black);
|
||||
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i *
|
||||
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||
g.setColor(Color.RED);
|
||||
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight-5, i * _placeSizeWidth + 20, j * _placeSizeHeight - 23);
|
||||
g.drawLine( i * _placeSizeWidth + 70, j * _placeSizeHeight - 5, i * _placeSizeWidth + 70, j * _placeSizeHeight - 23);
|
||||
}
|
||||
|
||||
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
|
||||
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
private void DrawBattleship(Graphics2D g)
|
||||
{
|
||||
int countColumns = _pictureWidth / _placeSizeWidth - 1;
|
||||
int countRows = _pictureHeight / _placeSizeHeight - 1;
|
||||
int currentColumn = 0;
|
||||
for (int i = 0; i < _setBattleship.Count(); i++)
|
||||
{
|
||||
if((_setBattleship.Get(i) != null))
|
||||
{
|
||||
_setBattleship.Get(i).SetObject((currentColumn) * _placeSizeWidth + 10, (countRows) * _placeSizeHeight + 15, _pictureWidth, _pictureHeight);
|
||||
_setBattleship.Get(i).DrawningObject(g);
|
||||
}
|
||||
if (currentColumn >= countColumns)
|
||||
{
|
||||
currentColumn = 0;
|
||||
countRows--;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentColumn++;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
40
MyParametrClass.java
Normal file
40
MyParametrClass.java
Normal file
@ -0,0 +1,40 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class MyParametrClass<T extends EntityBattleship, U extends IDrawningBlocks> {
|
||||
private final ArrayList<T> EntityArray;
|
||||
private final ArrayList<U> BlocksArray;
|
||||
int entitiesCount = 0;
|
||||
int blocksCount = 0;
|
||||
public MyParametrClass(int entitiesCount, int blocksCount) {
|
||||
EntityArray = new ArrayList<T>();
|
||||
BlocksArray = new ArrayList<U>();
|
||||
}
|
||||
public void Insert(T battleship){
|
||||
|
||||
EntityArray.add(battleship);
|
||||
entitiesCount++;
|
||||
|
||||
}
|
||||
public void Insert(U drawningBlocks){
|
||||
|
||||
BlocksArray.add(drawningBlocks);
|
||||
blocksCount++;
|
||||
|
||||
}
|
||||
public DrawningBattleship GetDrawningBattleship() {
|
||||
|
||||
Random random = new Random();
|
||||
int entityIndex = random.nextInt(EntityArray.size());
|
||||
int blockIndex = random.nextInt( BlocksArray.size());
|
||||
|
||||
|
||||
EntityBattleship battleship = EntityArray.get(entityIndex);
|
||||
IDrawningBlocks blocks = BlocksArray.get(blockIndex);
|
||||
|
||||
if (battleship instanceof EntityLinkor) {
|
||||
return new DrawningLinkor(battleship, blocks);
|
||||
}
|
||||
return new DrawningLinkor(battleship, blocks);
|
||||
}
|
||||
}
|
44
SeaMap.java
Normal file
44
SeaMap.java
Normal file
@ -0,0 +1,44 @@
|
||||
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);
|
||||
}
|
||||
}
|
59
SetBattleshipGeneric.java
Normal file
59
SetBattleshipGeneric.java
Normal file
@ -0,0 +1,59 @@
|
||||
public class SetBattleshipGeneric <T>
|
||||
{
|
||||
private final T[] _places;
|
||||
|
||||
public SetBattleshipGeneric(int count) {
|
||||
_places = (T[]) new Object[count];
|
||||
}
|
||||
|
||||
public int Count() {
|
||||
return _places.length;
|
||||
}
|
||||
public int Insert(T battleship)
|
||||
{
|
||||
return Insert(battleship, 0);
|
||||
}
|
||||
public int Insert(T battleship, int position)
|
||||
{
|
||||
if (position >= _places.length || position < 0) return -1;
|
||||
if (_places[position] == null) {
|
||||
_places[position] = battleship;
|
||||
return position;
|
||||
}
|
||||
|
||||
int emptyEl = -1;
|
||||
for (int i = position + 1; i < Count(); i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
emptyEl = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (emptyEl == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
for (int i = emptyEl; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[position] = battleship;
|
||||
return position;
|
||||
}
|
||||
public T Remove (int position) {
|
||||
if (position >= _places.length || position < 0) return null;
|
||||
T result = _places[position];
|
||||
_places[position] = null;
|
||||
return result;
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position >= Count() || position < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _places[position];
|
||||
}
|
||||
}
|
43
SimpleMap.java
Normal file
43
SimpleMap.java
Normal file
@ -0,0 +1,43 @@
|
||||
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