Лабораторная работа №2

This commit is contained in:
victinass 2024-05-11 22:59:48 +04:00
parent 5bfa75169f
commit 3609408b02
23 changed files with 872 additions and 257 deletions

View File

@ -1,25 +1,32 @@
package DifferentBlocks;
import java.awt.*; import java.awt.*;
public class DrawingBlocks { public class DrawingBlocks implements IDrawingBlocks {
private NumberBlock numBlocks; private NumBlocks numBlocks;
//определяет количество блоков //определяет количество блоков
public void setNumBlocks(int number) { public void setNumBlocks(int number) {
switch (number) { switch (number) {
case 1: case 1:
numBlocks = NumberBlock.Two; numBlocks = NumBlocks.Two;
break; break;
case 2: case 2:
numBlocks = NumberBlock.Four; numBlocks = NumBlocks.Four;
break; break;
case 3: case 3:
numBlocks = NumberBlock.Six; numBlocks = NumBlocks.Six;
break; break;
default: default:
numBlocks = NumberBlock.Four; numBlocks = NumBlocks.Four;
} }
} }
@Override
public NumBlocks getNumBlocks() {
return numBlocks;
}
public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numBlocks) { switch (numBlocks) {
case Two: case Two:

View File

@ -0,0 +1,104 @@
package DifferentBlocks;
import java.awt.*;
public class DrawingBlocksType1 implements IDrawingBlocks {
private NumBlocks numBlocks;
@Override
public NumBlocks getNumBlocks() {
return null;
}
@Override
public void setNumBlocks(int number) {
switch (number) {
case 1:
numBlocks = NumBlocks.Two;
break;
case 2:
numBlocks = NumBlocks.Four;
break;
case 3:
numBlocks = NumBlocks.Six;
break;
default:
numBlocks = NumBlocks.Four;
}
}
@Override
public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numBlocks) {
case Two:
drawTwoBlocks(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourBlocks(g2D, color, _startPosX, _startPosY);
break;
case Six:
drawSixBlocks(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawRect(_startPosX + 32, _startPosY + 13, 21, 12);
g2D.fillRect(_startPosX + 32, _startPosY + 13, 21, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 53, _startPosY + 7, 16, 24);
g2D.fillRect(_startPosX + 53, _startPosY + 7, 16, 24);
}
private void drawFourBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.magenta);
g2D.drawOval(_startPosX + 32, _startPosY + 13, 8, 12);
g2D.fillOval(_startPosX + 32, _startPosY + 13, 8, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GREEN);
g2D.drawOval(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.fillOval(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.setColor(Color.GREEN);
g2D.drawOval(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.fillOval(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.setColor(Color.DARK_GRAY);
g2D.drawOval(_startPosX + 19, _startPosY + 13, 8, 12);
g2D.fillOval(_startPosX + 19, _startPosY + 13, 8, 12);
}
private void drawSixBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.fillOval(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawOval(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.fillOval(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.setColor(Color.PINK);
g2D.drawOval(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.fillOval(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 28, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 28, 7, 8);
}
}

View File

@ -0,0 +1,103 @@
package DifferentBlocks;
import java.awt.*;
public class DrawingBlocksType2 implements IDrawingBlocks {
private NumBlocks numBlocks;
@Override
public void setNumBlocks(int number) {
switch (number) {
case 1:
numBlocks = NumBlocks.Two;
break;
case 2:
numBlocks = NumBlocks.Four;
break;
case 3:
numBlocks = NumBlocks.Six;
break;
default:
numBlocks = NumBlocks.Four;
}
}
@Override
public NumBlocks getNumBlocks() {
return null;
}
@Override
public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
switch (numBlocks) {
case Two:
drawTwoBlocks(g2D, color, _startPosX, _startPosY);
break;
case Four:
drawFourBlocks(g2D, color, _startPosX, _startPosY);
break;
case Six:
drawSixBlocks(g2D, color, _startPosX, _startPosY);
break;
}
}
private void drawTwoBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawRect(_startPosX + 32, _startPosY + 13, 21, 12);
g2D.fillRect(_startPosX + 32, _startPosY + 13, 21, 12);
//прямоугольник правый на палубе (вертикальный)
g2D.setColor(Color.GRAY);
g2D.drawRect(_startPosX + 53, _startPosY + 7, 16, 24);
g2D.fillRect(_startPosX + 53, _startPosY + 7, 16, 24);
}
private void drawFourBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
//прямоугольник левый на палубе (горизонтальный)
g2D.setColor(Color.magenta);
g2D.drawOval(_startPosX + 32, _startPosY + 13, 8, 12);
g2D.fillOval(_startPosX + 32, _startPosY + 13, 8, 12);
g2D.setColor(Color.GREEN);
g2D.drawOval(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.fillOval(_startPosX + 53, _startPosY + 8, 16, 8);
g2D.setColor(Color.GREEN);
g2D.drawOval(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.fillOval(_startPosX + 53, _startPosY + 23, 16, 8);
g2D.setColor(Color.DARK_GRAY);
g2D.drawOval(_startPosX + 19, _startPosY + 13, 8, 12);
g2D.fillOval(_startPosX + 19, _startPosY + 13, 8, 12);
}
private void drawSixBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) {
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.fillOval(_startPosX + 19, _startPosY + 13, 10, 13);
g2D.setColor(Color.LIGHT_GRAY);
g2D.drawOval(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.fillOval(_startPosX + 31, _startPosY + 15, 12, 9);
g2D.setColor(Color.PINK);
g2D.drawOval(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.fillOval(_startPosX + 46, _startPosY + 13, 10, 13);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 4, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 15, 7, 8);
g2D.setColor(Color.GRAY);
g2D.drawOval(_startPosX + 64, _startPosY + 28, 7, 8);
g2D.fillOval(_startPosX + 64, _startPosY + 28, 7, 8);
}
}

View File

@ -0,0 +1,8 @@
package DifferentBlocks;
import java.awt.*;
public interface IDrawingBlocks {
void setNumBlocks(int number);
NumBlocks getNumBlocks();
void drawBlocks(Graphics2D g2d, Color color, int _startPosX, int _startPosY);
}

View File

@ -0,0 +1,7 @@
package DifferentBlocks;
public enum NumBlocks {
Two,
Four,
Six;
}

View File

@ -1,177 +0,0 @@
import java.awt.*;
public class DrawingBattleship
{
public EntityBattleship EntityBattleship;
private DrawingBlocks drawingBlocks = null;
private Integer _pictureWidth;
private Integer _pictureHeight;
private Integer _startPosX;
private Integer _startPosY;
private int _drawingBattleshipWidth = 120;
private int _drawingBattleshipHeight = 80;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower, int numBlocks)
{
EntityBattleship = new EntityBattleship();
EntityBattleship.Init(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
drawingBlocks = new DrawingBlocks();
drawingBlocks.setNumBlocks(numBlocks);
}
public boolean SetPictureSize(int width, int height)
{
if (width < _drawingBattleshipWidth || height < _drawingBattleshipHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_startPosX + _drawingBattleshipWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawingBattleshipWidth;
}
else if (_startPosX < 0) _startPosX = 0;
if (_startPosY + _drawingBattleshipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawingBattleshipHeight;
}
else if (_startPosY < 0) _startPosY = 0;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(_pictureWidth != null && _pictureHeight != null)) return;
if (x + _drawingBattleshipWidth > _pictureWidth)
{
_startPosX = x - (x + _drawingBattleshipWidth - _pictureWidth);
}
else if (x < 0) _startPosX = 0;
else _startPosX = x;
if (y + _drawingBattleshipHeight > _pictureHeight)
{
_startPosY = y - (y + _drawingBattleshipHeight - _pictureHeight);
}
else if (y < 0) _startPosY = 0;
else _startPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityBattleship == null || _startPosX == null || _startPosY == null) return false;
switch (direction) {
case Left:
if (_startPosX - EntityBattleship.Step > 0) {
_startPosX -= (int)EntityBattleship.Step;
}
return true;
case Up:
if (_startPosY - EntityBattleship.Step > 0)
{
_startPosY -= (int)EntityBattleship.Step;
}
return true;
case Right:
if (_startPosX + _drawingBattleshipWidth + (int)EntityBattleship.Step < _pictureWidth - EntityBattleship.Step)
{
_startPosX += (int)EntityBattleship.Step;
}
return true;
case Down:
if (_startPosY + _drawingBattleshipHeight + (int)EntityBattleship.Step < _pictureHeight - EntityBattleship.Step)
{
_startPosY += (int)EntityBattleship.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g)
{
if (EntityBattleship == null || _startPosX == null || _startPosY == null)
{
return;
}
int y = _startPosY;
Color bodyColor = EntityBattleship.getBodyColor();
Color additionalColor = EntityBattleship.getAdditionalColor();
//границы линкора
g.drawRect(_startPosX + 10, _startPosY, 80, 40); //ширина высота (сама палуба)
//заливка линкора
g.setColor(EntityBattleship.getBodyColor());
g.fillRect(_startPosX + 10, _startPosY, 80, 40);
//границы двигателей
g.drawRect(_startPosX + 5, _startPosY + 7, 5, 10); //двигатель верхний
g.drawRect(_startPosX + 5, _startPosY + 22, 5, 10); //двигатель нижний
//заливка двигателей (2 фигни сзади)
g.setColor(Color.BLACK);
g.fillRect(_startPosX + 5, _startPosY + 7, 5, 10); //верхний
g.fillRect(_startPosX + 5, _startPosY + 22, 5, 10); //нижний
// блоки
drawingBlocks.drawBlocks(g, Color.BLACK, _startPosX, _startPosY);
//носик палубы
int[] xPoints = {_startPosX + 90,_startPosX + 120, _startPosX + 90};
int[] yPoints = {_startPosY,_startPosY + 20,_startPosY + 40};
Polygon Points = new Polygon(xPoints, yPoints, 3);
g.drawPolygon(Points);
g.setColor(EntityBattleship.BodyColor);
g.fillPolygon(Points);
//круг на палубе
g.drawOval(_startPosX + 78, _startPosY + 12, 15, 15);
//заливка круга
g.setColor(Color.BLUE);
g.fillOval(_startPosX + 78, _startPosY + 12, 15, 15);
//ПРОВЕРКА
if (EntityBattleship.Compartment)
{
//отсеки для ракет
//заливка отсеков
g.setColor(EntityBattleship.getAdditionalColor());
g.fillRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек для ракет
g.fillRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек для ракет
//границы отсеков
g.drawRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек
g.drawRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек
}
if (EntityBattleship.Tower)
{
//границы башни
g.drawRect(_startPosX + 107, _startPosY + 17, 30, 5);
g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10);
//заливка башни
g.setColor(EntityBattleship.getAdditionalColor());
g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10);
g.fillRect(_startPosX + 107, _startPosY + 17, 30, 5);
}
}
}

View File

@ -1,16 +1,20 @@
package Drawings;
import Drawings.DrawingWarship;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
public class CanvasBattleship extends JComponent { public class CanvasBattleship extends JComponent {
public DrawingBattleship _drawingBattleship; public DrawingWarship _drawingWarship;
public CanvasBattleship(){} public CanvasBattleship(){}
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
if (_drawingBattleship == null) { if (_drawingWarship == null) {
return; return;
} }
super.paintComponents(g); super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
_drawingBattleship.DrawTransport(g2d); _drawingWarship.DrawTransport(g2d);
super.repaint(); super.repaint();
} }
} }

View File

@ -0,0 +1,15 @@
package Drawings;
public enum DirectionType
{
Unknow,
Up,
Down,
Left,
Right,
}

View File

@ -0,0 +1,51 @@
package Drawings;
import Entities.EntityBattleship;
import DifferentBlocks.IDrawingBlocks;
import java.awt.*;
public class DrawingBattleship extends DrawingWarship
{
private IDrawingBlocks drawingBlocks;
public DrawingBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower)
{
EntityWarship = new EntityBattleship(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower);
DrawBlocks();
}
@Override
public void DrawTransport(Graphics2D g)
{
if (EntityWarship == null || !(EntityWarship instanceof EntityBattleship battleship) || _startPosX == null || _startPosY == null) {
return;
}
//ПРОВЕРКА
super.DrawTransport(g); // Обращение к методу DrawTransport базового класса
if (battleship.Compartment)
{
//отсеки для ракет
//заливка отсеков
g.setColor(battleship.getAdditionalColor());
g.fillRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек для ракет
g.fillRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек для ракет
//границы отсеков
g.drawRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек
g.drawRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек
}
if (battleship.Tower)
{
//границы башни
g.drawRect(_startPosX + 107, _startPosY + 17, 30, 5);
g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10);
//заливка башни
g.setColor(battleship.getAdditionalColor());
g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10);
g.fillRect(_startPosX + 107, _startPosY + 17, 30, 5);
}
}
}

View File

@ -0,0 +1,172 @@
package Drawings;
import DifferentBlocks.DrawingBlocks;
import DifferentBlocks.DrawingBlocksType1;
import DifferentBlocks.DrawingBlocksType2;
import DifferentBlocks.IDrawingBlocks;
import Entities.EntityWarship;
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Type;
public class DrawingWarship extends JPanel {
private IDrawingBlocks drawingBlocks;
public Entities.EntityWarship EntityWarship;
public Integer _pictureWidth;
public Integer _pictureHeight;
public Integer _startPosX;
public Integer _startPosY;
public int _drawingWarshipWidth = 120;
public int _drawingWarshipHeight = 80;
public Integer GetPosX() {return _startPosX;}
public Integer GetPosY() {return _startPosY;}
public Integer GetWidth() {return _drawingWarshipWidth;}
public Integer GetHeight() {return _drawingWarshipHeight;}
protected DrawingWarship() {
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public DrawingWarship(int speed, double weight, Color bodyColor)
{
super();
EntityWarship = new EntityWarship(speed, weight, bodyColor);
DrawBlocks();
}
//////////////////////
protected void DrawBlocks() {
int number = (int)(Math.random() * 4 + 0);
switch ((int)(Math.random() * 3 + 1)) {
case 1:
drawingBlocks = new DrawingBlocksType1();
break;
case 2:
drawingBlocks = new DrawingBlocksType2();
break;
case 3:
drawingBlocks = new DrawingBlocks();
break;
default:
number = 0;
break;
}
drawingBlocks.setNumBlocks(number);
}
public boolean SetPictureSize(int width, int height)
{
if (width < _drawingWarshipWidth || height < _drawingWarshipHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_startPosX + _drawingWarshipWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawingWarshipWidth;
}
else if (_startPosX < 0) _startPosX = 0;
if (_startPosY + _drawingWarshipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawingWarshipHeight;
}
else if (_startPosY < 0) _startPosY = 0;
}
return true;
}
public void SetPosition(int x, int y) {
if (!(_pictureWidth != null && _pictureHeight != null)) return;
if (x + _drawingWarshipWidth > _pictureWidth)
{
_startPosX = x - (x + _drawingWarshipWidth - _pictureWidth);
}
else if (x < 0) _startPosX = 0;
else _startPosX = x;
if (y + _drawingWarshipHeight > _pictureHeight)
{
_startPosY = y - (y + _drawingWarshipHeight - _pictureHeight);
}
else if (y < 0) _startPosY = 0;
else _startPosY = y;
}
public boolean MoveTransport(DirectionType direction) {
if (EntityWarship == null || _startPosX == null || _startPosY == null) return false;
switch (direction) {
case Left:
if (_startPosX - EntityWarship.Step > 0) {
_startPosX -= (int)EntityWarship.Step;
}
return true;
case Up:
if (_startPosY - EntityWarship.Step > 0)
{
_startPosY -= (int)EntityWarship.Step;
}
return true;
case Right:
if (_startPosX + _drawingWarshipWidth + (int)EntityWarship.Step < _pictureWidth - EntityWarship.Step)
{
_startPosX += (int)EntityWarship.Step;
}
return true;
case Down:
if (_startPosY + _drawingWarshipHeight + (int)EntityWarship.Step < _pictureHeight - EntityWarship.Step)
{
_startPosY += (int)EntityWarship.Step;
} return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g)
{
if (EntityWarship == null || _startPosX == null || _startPosY == null)
{
return;
}
int y = _startPosY;
Color bodyColor = EntityWarship.getBodyColor();
//границы линкора
g.drawRect(_startPosX + 10, _startPosY, 80, 40); //ширина высота (сама палуба)
//заливка линкора
g.setColor(EntityWarship.getBodyColor());
g.fillRect(_startPosX + 10, _startPosY, 80, 40);
//границы двигателей
g.drawRect(_startPosX + 5, _startPosY + 7, 5, 10); //двигатель верхний
g.drawRect(_startPosX + 5, _startPosY + 22, 5, 10); //двигатель нижний
//заливка двигателей (2 фигни сзади)
g.setColor(Color.BLACK);
g.fillRect(_startPosX + 5, _startPosY + 7, 5, 10); //верхний
g.fillRect(_startPosX + 5, _startPosY + 22, 5, 10); //нижний
// блоки
drawingBlocks.drawBlocks(g, Color.BLACK, _startPosX, _startPosY);
//носик палубы
int[] xPoints = {_startPosX + 90,_startPosX + 120, _startPosX + 90};
int[] yPoints = {_startPosY,_startPosY + 20,_startPosY + 40};
Polygon Points = new Polygon(xPoints, yPoints, 3);
g.drawPolygon(Points);
g.setColor(EntityWarship.getBodyColor());
g.fillPolygon(Points);
//круг на палубе
g.drawOval(_startPosX + 78, _startPosY + 12, 15, 15);
//заливка круга
g.setColor(Color.BLUE);
g.fillOval(_startPosX + 78, _startPosY + 12, 15, 15);
}
}

View File

@ -0,0 +1,23 @@
package Entities;
import java.awt.*;
public class EntityBattleship extends EntityWarship{
public Color AdditionalColor;
public Color getAdditionalColor(){
return AdditionalColor;
}
public boolean BodyDeck;
public boolean Compartment;
public boolean Tower;
public EntityBattleship(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower){
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
BodyDeck = bodyDeck;
Compartment = compartment;
Tower = tower;
}
}
//свойства в джаве нет

View File

@ -0,0 +1,20 @@
package Entities;
import java.awt.*;
public class EntityWarship {
private int Speed;
private double Weight;
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
public double Step;
public EntityWarship(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
Step = Speed * 100 / Weight;
}
}

View File

@ -1,28 +0,0 @@
import java.awt.*;
import java.util.Random;
public class EntityBattleship
{
public int Speed;
public double Weight;
public Color BodyColor;
public Color AdditionalColor;
public boolean BodyDeck;
public boolean Compartment;
public boolean Tower;
public double Step;
public Color getBodyColor() { return BodyColor; }
public Color getAdditionalColor() { return AdditionalColor; }
public double getStep() { return Speed * 100 / Weight; }
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower) {
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed;
Weight = weight <= 0 ? rnd.nextInt(40,70) : weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
BodyDeck = bodyDeck;
Compartment = compartment;
Tower = tower;
Step = Speed * 100 / Weight;
}
}

View File

@ -1,3 +1,9 @@
import Drawings.CanvasBattleship;
import Drawings.DirectionType;
import Drawings.DrawingWarship;
import Drawings.DrawingBattleship;
import MovementStrategy.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@ -10,75 +16,142 @@ public class FormBattleship extends JFrame {
private String title; private String title;
private Dimension dimension; private Dimension dimension;
private int Width, Height; private int Width, Height;
private CanvasBattleship canvasBattleship = new CanvasBattleship(); public CanvasBattleship canvasBattleship = new CanvasBattleship();
private JButton CreateButton = new JButton("Создать");; private JButton CreateButton = new JButton("Создание линкора");;
private JButton CreateShipButton = new JButton("Создание корабля");
private JButton UpButton = new JButton(); private JButton UpButton = new JButton();
private JButton DownButton = new JButton();; private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();; private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton(); private JButton RightButton = new JButton();
private AbstractStrategy _strategy;
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
private JButton ButtonStrategy = new JButton("Шаг");
public FormBattleship(String title, Dimension dimension) { public FormBattleship(String title, Dimension dimension) {
this.title = title; this.title = title;
this.dimension = dimension; this.dimension = dimension;
} }
private void CreateObject(String typeOfClass) {
int StartPositionX = (int)(Math.random() * 90 + 10);
int StartPositionY = (int)(Math.random() * 90 + 10);
int speed = (int)(Math.random() * 300 + 100);
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
switch (typeOfClass) {
case "DrawingWarhip":
canvasBattleship._drawingWarship = new DrawingWarship(speed, weight, bodyColor);
canvasBattleship._drawingWarship.SetPictureSize(Width, Height);
canvasBattleship._drawingWarship.SetPosition(StartPositionX, StartPositionY);
canvasBattleship.repaint();
break;
case "DrawingBattleship":
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
boolean bodyDeck = new Random().nextBoolean();
boolean compartment = new Random().nextBoolean();;
boolean tower = new Random().nextBoolean();;
canvasBattleship._drawingWarship = new DrawingBattleship(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower);
canvasBattleship._drawingWarship.SetPictureSize(Width, Height);
canvasBattleship._drawingWarship.SetPosition(StartPositionX, StartPositionY);
canvasBattleship.repaint();
break;
default: return;
}
_strategy = null;
ComboBoxStrategy.setEnabled(true);
}
public void Init() { public void Init() {
setTitle(title); setTitle(title);
setMinimumSize(dimension); setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 15;
Height = getHeight() - 35; Width = getWidth() - 10;
Height = getHeight() - 34;
_strategy = null;
CreateButton.setName("CREATE"); CreateButton.setName("CREATE");
CreateShipButton.setName("CREATESHIPBUTTON");
Icon iconUp = new ImageIcon("src/res/arrowUp.png"); Icon iconUp = new ImageIcon("src/res/arrowUp.png");
UpButton.setIcon(iconUp); UpButton.setIcon(iconUp);
UpButton.setName("Up"); UpButton.setName("UP");
DownButton.setName("Down"); DownButton.setName("DOWN");
Icon iconDown = new ImageIcon("src/res/arrowDown.png"); Icon iconDown = new ImageIcon("src/res/arrowDown.png");
DownButton.setIcon(iconDown); DownButton.setIcon(iconDown);
LeftButton.setName("Left"); LeftButton.setName("LEFT");
Icon iconLeft = new ImageIcon("src/res/arrowLeft.png"); Icon iconLeft = new ImageIcon("src/res/arrowLeft.png");
LeftButton.setIcon(iconLeft); LeftButton.setIcon(iconLeft);
RightButton.setName("Right"); RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("src/res/arrowRight.png"); Icon iconRight = new ImageIcon("src/res/arrowRight.png");
RightButton.setIcon(iconRight); RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() { CreateButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int StartPositionX = (int)(Math.random() * 90 + 10); CreateObject("DrawingBattleship");
int StartPositionY = (int)(Math.random() * 90 + 10); }
int speed = (int)(Math.random() * 300 + 100); });
double weight = (double)(Math.random() * 3000 + 1000);
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); CreateShipButton.addActionListener(new ActionListener() {
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; @Override
boolean bodyDeck = new Random().nextBoolean(); public void actionPerformed(ActionEvent e) {
boolean compartment = new Random().nextBoolean();; CreateObject("DrawingWarhip");
boolean tower = new Random().nextBoolean(); }
int blocksNum = (int)(Math.random() * 6) + 1; });
canvasBattleship._drawingBattleship = new DrawingBattleship();
canvasBattleship._drawingBattleship.Init(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower, blocksNum); ButtonStrategy.addActionListener(new ActionListener() {
canvasBattleship._drawingBattleship.SetPictureSize(Width, Height); @Override
canvasBattleship._drawingBattleship.SetPosition(StartPositionX, StartPositionY); public void actionPerformed(ActionEvent e) {
canvasBattleship.repaint(); if (canvasBattleship._drawingWarship == null) return;
if (ComboBoxStrategy.isEnabled())
{
int index = ComboBoxStrategy.getSelectedIndex();
switch(index)
{
case 0:
_strategy = new MoveToCenter();
break;
case 1:
_strategy = new MoveToBorder();
break;
default:
_strategy = null;
break;
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableWarship(canvasBattleship._drawingWarship), Width, Height);
}
if (_strategy == null)
{
return;
}
ComboBoxStrategy.setEnabled(false);
_strategy.MakeStep();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
ComboBoxStrategy.setEnabled(true);
_strategy = null;
}
} }
}); });
ActionListener actionListener = new ActionListener() { ActionListener actionListener = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
if (canvasBattleship._drawingBattleship == null) return; if (canvasBattleship._drawingWarship == null) return;
boolean result = false; boolean result = false;
switch ((((JButton)(event.getSource())).getName())) { switch ((((JButton)(event.getSource())).getName())) {
case "Up": case "UP":
result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Up); result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Up);
break; break;
case "Down": case "DOWN":
result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Down); result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Down);
break; break;
case "Left": case "LEFT":
result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Left); result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Left);
break; break;
case "Right": case "RIGHT":
result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Right); result = canvasBattleship._drawingWarship.MoveTransport(DirectionType.Right);
break; break;
} }
if (result) { if (result) {
@ -94,31 +167,40 @@ public class FormBattleship extends JFrame {
setSize(dimension.width,dimension.height); setSize(dimension.width,dimension.height);
setLayout(null); setLayout(null);
canvasBattleship.setBounds(0,0, getWidth(), getHeight()); canvasBattleship.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40); CreateButton.setBounds(10, getHeight() - 90, 140, 40);
CreateShipButton.setBounds(160, getHeight() - 90, 140, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
add(CreateButton); add(CreateButton);
add(CreateShipButton);
add(UpButton); add(UpButton);
add(DownButton); add(DownButton);
add(RightButton); add(RightButton);
add(LeftButton); add(LeftButton);
add(ComboBoxStrategy);
add(ButtonStrategy);
add(canvasBattleship); add(canvasBattleship);
setVisible(true); setVisible(true);
//обработка события изменения размеров окна //обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() { addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
Width = getWidth() - 15; Width = getWidth() - 10;
Height = getHeight() - 35; Height = getHeight() - 34;
if (canvasBattleship._drawingBattleship != null) if (canvasBattleship._drawingWarship != null)
canvasBattleship._drawingBattleship.SetPictureSize(Width, Height); canvasBattleship._drawingWarship.SetPictureSize(Width, Height);
canvasBattleship.setBounds(0,0, getWidth(), getHeight()); canvasBattleship.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 40); CreateButton.setBounds(10, getHeight() - 90, 140, 40);
CreateShipButton.setBounds(160, getHeight() - 90, 140, 40);
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
} }
}); });
} }

View File

@ -0,0 +1,61 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObjects _moveableObjects;
private StrategyStatus _state = StrategyStatus.NotIInit;
protected int FieldWidth;
protected int FieldHeight;
public StrategyStatus GetStatus() { return _state; }
public void SetData(IMoveableObjects moveableObjects, int width, int height)
{
if (moveableObjects == null)
{
_state = StrategyStatus.NotIInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObjects = moveableObjects;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != StrategyStatus.InProgress) return;
if (IsTargetDestination())
{
_state = StrategyStatus.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() {return MoveTo(MovementDirection.Left);} ;
protected boolean MoveRight() {return MoveTo(MovementDirection.Right);} ;
protected boolean MoveUp() {return MoveTo(MovementDirection.Up);};
protected boolean MoveDown() {return MoveTo(MovementDirection.Down);}
protected ObjectParameters GetObjectParameters(){
return _moveableObjects.GetObjectPosition();
}
protected Integer GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObjects.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestination();
private boolean MoveTo(MovementDirection movementDirection)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
boolean stateTryMoveObject = _moveableObjects.TryMoveObject(movementDirection);
if (stateTryMoveObject) return stateTryMoveObject;
return false; }
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public interface IMoveableObjects {
ObjectParameters GetObjectPosition();
int GetStep();
boolean TryMoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,26 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean IsTargetDestination(){
ObjectParameters objParams = GetObjectParameters();
if (objParams == null){
return false;
}
return objParams.RightBorder + GetStep() >= FieldWidth - GetStep() && objParams.DownBorder + GetStep() >= FieldHeight - GetStep();
}
@Override
protected void MoveToTarget(){
ObjectParameters objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
//реализация в правый нижний угол
int x = objParams.RightBorder;
if (x + GetStep() < FieldWidth) MoveRight();
int y = objParams.DownBorder;
if (y + GetStep() < FieldHeight) MoveDown();
}
}

View File

@ -0,0 +1,48 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean IsTargetDestination() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight /2;
}
@Override
protected void MoveToTarget() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,45 @@
package MovementStrategy;
import Drawings.CanvasBattleship;
import Drawings.DirectionType;
import Drawings.DrawingWarship;
public class MoveableWarship implements IMoveableObjects {
private CanvasBattleship canvas = new CanvasBattleship();
public MoveableWarship(DrawingWarship drawingWarship)
{
canvas._drawingWarship = drawingWarship;
}
@Override
public ObjectParameters GetObjectPosition() {
if (canvas._drawingWarship == null || canvas._drawingWarship.EntityWarship == null ||
canvas._drawingWarship.GetPosX() == null || canvas._drawingWarship.GetPosY() == null)
{
return null;
}
return new ObjectParameters(canvas._drawingWarship.GetPosX(), canvas._drawingWarship.GetPosY(),
canvas._drawingWarship.GetWidth(), canvas._drawingWarship.GetHeight());
}
@Override
public int GetStep() {
return (int)(canvas._drawingWarship.EntityWarship.Step);
}
@Override
public boolean TryMoveObject(MovementDirection direction) {
if (canvas._drawingWarship == null || canvas._drawingWarship.EntityWarship == null)
{
return false;
}
return canvas._drawingWarship.MoveTransport(GetDirectionType(direction));
}
private static DirectionType GetDirectionType(MovementDirection direction)
{
switch (direction) {
case Left: return DirectionType.Left;
case Right: return DirectionType.Right;
case Up: return DirectionType.Up;
case Down: return DirectionType.Down;
default: return DirectionType.Unknow;
}
}
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public enum MovementDirection {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,27 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
public int LeftBorder = _x;
public int TopBorder = _y;
public int RightBorder = _x + _width;
public int DownBorder = _y + _height;
public int ObjectMiddleHorizontal = _x + _width / 2;
public int ObjectMiddleVertical = _y + _height / 2;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
LeftBorder = _x;
TopBorder = _y;
RightBorder = _x + _width;
DownBorder = _y + _height;
ObjectMiddleHorizontal = _x + _width / 2;
ObjectMiddleVertical = _y + _height / 2;
}
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public enum StrategyStatus {
NotIInit,
InProgress,
Finish
}

View File

@ -1,5 +0,0 @@
public enum NumberBlock {
Two,
Four,
Six
}