Compare commits

...

2 Commits

Author SHA1 Message Date
de8c7eb3ae Второй коммит. 2022-10-24 21:01:54 +03:00
0db5c98d2d Второй коммит. 2022-10-24 20:59:27 +03:00
4 changed files with 229 additions and 2 deletions

7
Direction.java Normal file
View File

@ -0,0 +1,7 @@
public enum Direction {
Up,
Down,
Left,
Right
}

169
DrawningBattleship.java Normal file
View File

@ -0,0 +1,169 @@
import java.awt.*;
public class DrawningBattleship {
EntityBattleship Battleship;
public EntityBattleship Battleship()
{return Battleship; }
/// <summary>
/// Левая координата отрисовки корабля
/// </summary>
private float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки корабля
/// </summary>
private float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private Integer _pictureWidth = 100;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private Integer _pictureHeight = 100;
/// <summary>
/// Ширина отрисовки корабля
/// </summary>
private final int _battleshipWidth = 120;
/// <summary>
/// Высота отрисовки корабля
/// </summary>
private final int _battleshipHeight = 50;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес корабля</param>
/// <param name="bodyColor">Цвет корпуса</param>
public void Init(int speed, float weight, Color bodyColor, EntityBattleship entityBattleship)
{
Battleship = new EntityBattleship();
Battleship.Init(speed, weight, bodyColor);
}
/// <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(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| _pictureHeight == null || _pictureWidth == null)
{
return;
}
g.setColor(Color.BLACK);
//Корпус корабля
//Brush br = new SolidBrush(Battleship?.BodyColor ?? Color.Black);
// Polygon[] shipHullArrayPoints = {((int)_startPosX, (int)_startPosY)), (int)_startPosX, (int)_startPosY + 50), new Point((int)_startPosX + 80, (int)_startPosY + 50),
//(int)_startPosX + 120, (int)_startPosY + 25 ), new Point((int)_startPosX + 80, (int)_startPosY), new Point((int)_startPosX, (int)_startPosY)};
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) _startPosX + 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);
}
/// <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;
}
}
}

41
EntityBattleship.java Normal file
View File

@ -0,0 +1,41 @@
import java.awt.*;
import java.util.Random;
public class EntityBattleship {
private int speed;
private float weight;
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 void Init(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();
if(speed <= 0) speed = rnd.nextInt(50, 150);
if(weight <= 0) weight = rnd.nextInt(40, 70);
BodyColor = bodyColor;
}
}

View File

@ -1,5 +1,15 @@
public class FormBattleship {
public static void main(String[] args) {
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.Random;
public class FormBattleship extends JComponent {
private DrawningBattleship _battleship;
private EntityBattleship _entityBattleship;
public static void main(String[] args) {
FormBattleship formBattleship = new FormBattleship();
}
public FormBattleship() {
}
}