This commit is contained in:
Ilya Ryabov 2024-03-31 22:44:49 +04:00
parent 51864f5752
commit 78392f4692
23 changed files with 749 additions and 322 deletions

View File

@ -1,16 +1,18 @@
import Drawnings.DrawingBaseStormtrooper;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
public class CanvasStormtrooper extends JComponent { public class CanvasStormtrooper extends JComponent {
public DrawingStormtrooper _drawingStormtrooper; public DrawingBaseStormtrooper _drawingBaseStormtrooper;
public CanvasStormtrooper(){} public CanvasStormtrooper(){}
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
if (_drawingStormtrooper == null) { if (_drawingBaseStormtrooper == null) {
return; return;
} }
super.paintComponents(g); super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g; Graphics2D g2d = (Graphics2D) g;
_drawingStormtrooper.DrawTransport(g2d); _drawingBaseStormtrooper.DrawTransport(g2d);
super.repaint(); super.repaint();
} }
} }

View File

@ -1,10 +0,0 @@
public enum DirectionType {
///Вверх
Up,
///Вниз
Down,
///Влево
Left,
///Вправо
Right
}

View File

@ -1,230 +0,0 @@
import java.awt.*;
import java.util.Random;
public class DrawingStormtrooper {
public EntityStormtrooper EntityStormtrooper;
public DrawingEngines drawingEngines =null;
/// <summary>
/// Ширина окна
/// </summary>
private Integer _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private Integer _pictureHeight;
/// <summary>
/// Левая координата прорисовки бомбардировщика
/// </summary>
private Integer _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки бомбардировщика
/// </summary>
private Integer _startPosY;
/// <summary>
/// Ширина прорисовки бомбардировщика
/// </summary>
private final Integer _drawningStormtrooperWidth = 140;
/// <summary>
/// Высота прорисовки бомбардировщика
/// </summary>
private final Integer _drawningStormtrooperHeight = 135;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="engines">Признак наличия двигателей</param>
/// <param name="bombs">Признак наличия бомб</param>
/// <param name="rockets">Признак наличия ракет</param>
public void Init(int speed, float weight, Color bodyColor, Color additionalColor,boolean rockets, boolean bombs, boolean engines)
{
EntityStormtrooper = new EntityStormtrooper();
EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rockets,bombs ,engines);
if(engines==true){
drawingEngines = new DrawingEngines();
drawingEngines.setAmountOfEngines((int)((Math.random()*3)+1)*2);;
}
_startPosX=null;
_startPosY=null;
_pictureWidth = null;
_pictureHeight = null;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns> true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public boolean SetPictureSize(int width, int height)
{
// TODO проверка, что объект "влезает" в размеры поля
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
if (width < _drawningStormtrooperWidth || height < _drawningStormtrooperHeight) return false;
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX !=null || _startPosY !=null)
{
if (_startPosX + _drawningStormtrooperWidth > _pictureWidth)
{
_startPosX = -_drawningStormtrooperWidth + _pictureWidth;
}
else if (_startPosX < 0)
{
_startPosX = 0;
}
if (_startPosY + _drawningStormtrooperHeight > _pictureHeight)
{
_startPosY = -_drawningStormtrooperHeight + _pictureHeight;
}
else if (_startPosY < 0)
{
_startPosY = 0;
}
}
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!(_pictureHeight != null && _pictureWidth != null)) return;
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
// то надо изменить координаты, чтобы он оставался в этих границах
if (x + _drawningStormtrooperWidth > _pictureWidth)
{
_startPosX = x - (x + _drawningStormtrooperWidth - _pictureWidth);
}
else if (x < 0)
{
_startPosX = 0;
}
else
{
_startPosX = x;
}
if (y + _drawningStormtrooperHeight > _pictureHeight)
{
_startPosY = y - (y + _drawningStormtrooperHeight - _pictureHeight);
}
else if (y < 0)
{
_startPosY = 0;
}
else
{
_startPosY = y;
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public boolean MoveTransport(DirectionType direction)
{
if (EntityStormtrooper == null || _startPosX==null || _startPosY==null)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityStormtrooper.Step > 0)
{
_startPosX -= (int)EntityStormtrooper.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY - EntityStormtrooper.Step > 0)
{
_startPosY -= (int)EntityStormtrooper.Step;
}
return true;
// вправо
case DirectionType.Right:
//TODO прописать логику сдвига в право
if (_startPosX + _drawningStormtrooperWidth + EntityStormtrooper.Step < _pictureWidth)
{
_startPosX += (int)EntityStormtrooper.Step;
};
return true;
//вниз
case DirectionType.Down:
//TODO прописать логику сдвига в вниз
if (_startPosY + _drawningStormtrooperHeight + EntityStormtrooper.Step < _pictureHeight)
{
_startPosY += (int)EntityStormtrooper.Step;
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityStormtrooper == null || _startPosX==null || _startPosY==null)
{
return;
}
g.setColor(Color.black);
//Brush bodyColorBrush = new SolidBrush(EntityStormtrooper.BodyColor);
//Brush additionalBrush = new SolidBrush(EntityStormtrooper.AdditionalColor);
//Тело бомбардировщика
g.drawRect(_startPosX + 20, _startPosY + 60, 120, 20);
//Задние крылья бомбардировщика
g.drawLine(_startPosX+ 140, _startPosY+ 30, _startPosX+ 140, _startPosY+ 110);
g.drawLine(_startPosX+ 120, _startPosY+ 90, _startPosX+ 120, _startPosY+ 80);
g.drawLine(_startPosX+ 140, _startPosY+ 110, _startPosX + 120, _startPosY + 90);
g.drawLine(_startPosX+ 140, _startPosY+ 30, _startPosX+ 120, _startPosY+ 50);
g.drawLine(_startPosX+ 120, _startPosY+ 50, _startPosX+ 120, _startPosY+ 60);
//Крылья бомбардировщика
g.drawLine(_startPosX+ 50, _startPosY, _startPosX + 50, _startPosY + 60);
g.drawLine(_startPosX+ 50, _startPosY + 80, _startPosX + 50, _startPosY + 135);
g.drawLine(_startPosX+ 50, _startPosY + 135, _startPosX + 60, _startPosY + 135);
g.drawLine(_startPosX+ 60, _startPosY + 135, _startPosX + 65, _startPosY + 80);
g.drawLine(_startPosX+ 50, _startPosY, _startPosX + 60, _startPosY);
g.drawLine(_startPosX+ 60, _startPosY, _startPosX + 65, _startPosY + 60);
///Нос бомбардировщика
g.setColor(EntityStormtrooper.getBodyColor());
Point[] Nose = new Point[3];
int[] arrX = {_startPosX + 20, _startPosX,_startPosX+20};
int[] arrY = {_startPosY + 80,_startPosY + 70,_startPosY + 60};
Polygon poly = new Polygon(arrX,arrY,3);
g.fillPolygon(poly);
//Ракеты бомбардировщика
if (EntityStormtrooper.getRockets())
{
g.setColor(EntityStormtrooper.getAdditionalColor());
g.fillRect( _startPosX + 35, _startPosY + 20, 15, 5);
g.fillRect( _startPosX + 35, _startPosY + 110, 15, 5);
}
//Бомбы бомбардировщика
if (EntityStormtrooper.getBombs())
{
g.setColor(EntityStormtrooper.getAdditionalColor());
g.fillRect(_startPosX + 40, _startPosY + 40, 10, 10);
g.fillRect(_startPosX + 40, _startPosY + 90, 10, 10);
}
if(EntityStormtrooper.getEngines() && drawingEngines!=null){
drawingEngines.SwitchDrawEngines(g, _startPosX, _startPosY, EntityStormtrooper.getBodyColor());
}
}
}

View File

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

View File

@ -0,0 +1,133 @@
package Drawnings;
import Entities.*;
import java.awt.*;
public class DrawingBaseStormtrooper {
public EntityBaseStormtrooper EntityBaseStormtrooper;
protected Integer _pictureWidth;
protected Integer _pictureHeight;
protected Integer _startPosX;
protected Integer _startPosY;
public Integer getPosX(){return _startPosX;}
public Integer getPosY(){return _startPosY;}
private Integer _drawningStormtrooperWidth = 140;
public Integer getWidth(){return _drawningStormtrooperWidth;}
private Integer _drawningStormtrooperHeight = 135;
public Integer getHeight(){return _drawningStormtrooperHeight;}
protected DrawingBaseStormtrooper(){
_pictureWidth=null;
_pictureHeight=null;
_startPosX=null;
_startPosY=null;
}
public DrawingBaseStormtrooper(int speed, float weight, Color bodyColor) {
super();
EntityBaseStormtrooper = new EntityBaseStormtrooper(speed, weight, bodyColor);
}
public boolean SetPictureSize(int width, int height) {
// TODO проверка, что объект "влезает" в размеры поля
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
if (width < _drawningStormtrooperWidth || height < _drawningStormtrooperHeight) return false;
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null) {
if (_startPosX + _drawningStormtrooperWidth > _pictureWidth) {
_startPosX = -_drawningStormtrooperWidth + _pictureWidth;
} else if (_startPosX < 0) {
_startPosX = 0;
}
if (_startPosY + _drawningStormtrooperHeight > _pictureHeight) {
_startPosY = -_drawningStormtrooperHeight + _pictureHeight;
} else if (_startPosY < 0) {
_startPosY = 0;
}
}
return true;
}
public void SetPosition(int x, int y) {
if (!(_pictureHeight != null && _pictureWidth != null)) return;
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
// то надо изменить координаты, чтобы он оставался в этих границах
if (x + _drawningStormtrooperWidth > _pictureWidth) {
_startPosX = x - (x + _drawningStormtrooperWidth - _pictureWidth);
} else if (x < 0) {
_startPosX = 0;
} else {
_startPosX = x;
}
if (y + _drawningStormtrooperHeight > _pictureHeight) {
_startPosY = y - (y + _drawningStormtrooperHeight - _pictureHeight);
} else if (y < 0) {
_startPosY = 0;
} else {
_startPosY = y;
}
}
public boolean MoveTransport(DirectionType direction) {
if (EntityBaseStormtrooper == null || _startPosX == null || _startPosY == null) {
return false;
}
switch (direction) {
//влево
case DirectionType.Left:
if (_startPosX - EntityBaseStormtrooper.Step > 0) {
_startPosX -= (int) EntityBaseStormtrooper.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY - EntityBaseStormtrooper.Step > 0) {
_startPosY -= (int) EntityBaseStormtrooper.Step;
}
return true;
// вправо
case DirectionType.Right:
//TODO прописать логику сдвига в право
if (_startPosX + _drawningStormtrooperWidth + EntityBaseStormtrooper.Step < _pictureWidth) {
_startPosX += (int) EntityBaseStormtrooper.Step;
}
return true;
//вниз
case DirectionType.Down:
//TODO прописать логику сдвига в вниз
if (_startPosY + _drawningStormtrooperHeight + EntityBaseStormtrooper.Step < _pictureHeight) {
_startPosY += (int) EntityBaseStormtrooper.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics g) {
if (EntityBaseStormtrooper == null || _startPosX == null || _startPosY == null) {
return;
}
g.setColor(Color.black);
//Тело бомбардировщика
g.drawRect(_startPosX + 20, _startPosY + 60, 120, 20);
//Задние крылья бомбардировщика
g.drawLine(_startPosX + 140, _startPosY + 30, _startPosX + 140, _startPosY + 110);
g.drawLine(_startPosX + 120, _startPosY + 90, _startPosX + 120, _startPosY + 80);
g.drawLine(_startPosX + 140, _startPosY + 110, _startPosX + 120, _startPosY + 90);
g.drawLine(_startPosX + 140, _startPosY + 30, _startPosX + 120, _startPosY + 50);
g.drawLine(_startPosX + 120, _startPosY + 50, _startPosX + 120, _startPosY + 60);
//Крылья бомбардировщика
g.drawLine(_startPosX + 50, _startPosY, _startPosX + 50, _startPosY + 60);
g.drawLine(_startPosX + 50, _startPosY + 80, _startPosX + 50, _startPosY + 135);
g.drawLine(_startPosX + 50, _startPosY + 135, _startPosX + 60, _startPosY + 135);
g.drawLine(_startPosX + 60, _startPosY + 135, _startPosX + 65, _startPosY + 80);
g.drawLine(_startPosX + 50, _startPosY, _startPosX + 60, _startPosY);
g.drawLine(_startPosX + 60, _startPosY, _startPosX + 65, _startPosY + 60);
///Нос бомбардировщика
g.setColor(EntityBaseStormtrooper.getBodyColor());
Point[] Nose = new Point[3];
int[] arrX = {_startPosX + 20, _startPosX, _startPosX + 20};
int[] arrY = {_startPosY + 80, _startPosY + 70, _startPosY + 60};
Polygon poly = new Polygon(arrX, arrY, 3);
g.fillPolygon(poly);
}
}

View File

@ -0,0 +1,63 @@
package Drawnings;
import Drawnings.Engines.DrawingEngines;
import Drawnings.Engines.DrawingOvalEngines;
import Drawnings.Engines.DrawingTriangleEngines;
import Drawnings.Engines.IDrawingEngines;
import Entities.*;
import java.awt.*;
public class DrawingStormtrooper extends DrawingBaseStormtrooper{
public IDrawingEngines drawingEngines =null;
public DrawingStormtrooper(int speed, float weight, Color bodyColor, Color additionalColor,boolean rockets, boolean bombs, boolean engines, int typeOfEngines) {
EntityBaseStormtrooper = new EntityStormtrooper(speed, weight, bodyColor, additionalColor, rockets, bombs, engines);
if(engines){
switch(typeOfEngines){
case 1:
drawingEngines = new DrawingEngines();
drawingEngines.setAmountOfEngines((int)((Math.random()*3)+1)*2);
break;
case 2:
drawingEngines = new DrawingTriangleEngines();
drawingEngines.setAmountOfEngines((int)((Math.random()*3)+1)*2);
break;
case 3:
drawingEngines = new DrawingOvalEngines();
drawingEngines.setAmountOfEngines((int)((Math.random()*3)+1)*2);
break;
}
}
_startPosX=null;
_startPosY=null;
_pictureWidth = null;
_pictureHeight = null;
}
@Override
public void DrawTransport(Graphics g)
{
if (EntityBaseStormtrooper == null ||!(EntityBaseStormtrooper instanceof EntityStormtrooper entityStormtrooper) || _startPosX==null || _startPosY==null)
{
return;
}
super.DrawTransport(g);
//Ракеты бомбардировщика
if (entityStormtrooper.getRockets())
{
g.setColor(entityStormtrooper.getAdditionalColor());
g.setColor(entityStormtrooper.getAdditionalColor());
g.fillRect( _startPosX + 35, _startPosY + 20, 15, 5);
g.fillRect( _startPosX + 35, _startPosY + 110, 15, 5);
}
//Бомбы бомбардировщика
if (entityStormtrooper.getBombs())
{
g.setColor(entityStormtrooper.getAdditionalColor());
g.fillRect(_startPosX + 40, _startPosY + 40, 10, 10);
g.fillRect(_startPosX + 40, _startPosY + 90, 10, 10);
}
if(entityStormtrooper.getEngines() && drawingEngines!=null){
drawingEngines.SwitchDrawEngines(g, _startPosX, _startPosY, EntityBaseStormtrooper.getBodyColor());
}
}
}

View File

@ -1,43 +1,50 @@
package Drawnings.Engines;
import java.awt.*; import java.awt.*;
public class DrawingEngines { public class DrawingEngines implements IDrawingEngines {
private NumberOfEngines numberOfEngines; private NumberOfEngines numberOfEngines;
@Override
public NumberOfEngines getNumberOfEngines() { public NumberOfEngines getNumberOfEngines() {
return numberOfEngines; return numberOfEngines;
} }
@Override
public void setAmountOfEngines(int amount){ public void setAmountOfEngines(int amount){
if(NumberOfEngines.contains(amount)) { if(NumberOfEngines.contains(amount)) {
numberOfEngines = NumberOfEngines.getNumber(amount); numberOfEngines = NumberOfEngines.getNumber(amount);
} }
} }
@Override
public void DrawEngines(Graphics g, int x, int y, Color bodyColor) { public void DrawEngines(Graphics g, int x, int y, Color bodyColor) {
g.setColor(bodyColor); g.setColor(bodyColor);
g.fillRect(x, y, 10, 10); g.fillRect(x, y, 10, 10);
} }
@Override
private void drawTwoEngines(Graphics g, int x, int y, Color bodyColor){ public void drawTwoEngines(Graphics g, int x, int y, Color bodyColor){
DrawEngines(g,x + 65, y + 50, bodyColor); DrawEngines(g,x + 65, y + 50, bodyColor);
DrawEngines(g,x + 65, y + 81,bodyColor); DrawEngines(g,x + 65, y + 81,bodyColor);
} }
private void drawFourEngines(Graphics g, int x, int y, Color bodyColor){ @Override
public void drawFourEngines(Graphics g, int x, int y, Color bodyColor){
DrawEngines(g,x + 62, y + 10,bodyColor); DrawEngines(g,x + 62, y + 10,bodyColor);
DrawEngines(g,x + 64, y + 101,bodyColor); DrawEngines(g,x + 64, y + 101,bodyColor);
DrawEngines(g,x + 64, y + 30,bodyColor); DrawEngines(g,x + 64, y + 30,bodyColor);
DrawEngines(g,x + 62, y + 121,bodyColor); DrawEngines(g,x + 62, y + 121,bodyColor);
} }
private void drawSixEngines(Graphics g, int x, int y, Color bodyColor){ @Override
public void drawSixEngines(Graphics g, int x, int y, Color bodyColor){
drawFourEngines(g,x,y,bodyColor); drawFourEngines(g,x,y,bodyColor);
drawTwoEngines(g,x,y,bodyColor); drawTwoEngines(g,x,y,bodyColor);
} }
@Override
public void SwitchDrawEngines(Graphics g, int x, int y, Color bodyColor){ public void SwitchDrawEngines(Graphics g, int x, int y, Color bodyColor){
switch(getNumberOfEngines()){ switch(getNumberOfEngines()){
case TWO: case NumberOfEngines.TWO:
drawTwoEngines(g,x,y,bodyColor); drawTwoEngines(g,x,y,bodyColor);
break; break;
case FOUR: case NumberOfEngines.FOUR:
drawFourEngines(g,x,y,bodyColor); drawFourEngines(g,x,y,bodyColor);
break; break;
case SIX: case NumberOfEngines.SIX:
drawSixEngines(g,x,y,bodyColor); drawSixEngines(g,x,y,bodyColor);
break; break;
} }

View File

@ -0,0 +1,53 @@
package Drawnings.Engines;
import java.awt.*;
public class DrawingOvalEngines implements IDrawingEngines {
private NumberOfEngines numberOfEngines;
@Override
public NumberOfEngines getNumberOfEngines() {
return numberOfEngines;
}
@Override
public void setAmountOfEngines(int amount){
if(NumberOfEngines.contains(amount)) {
numberOfEngines = NumberOfEngines.getNumber(amount);
}
}
@Override
public void DrawEngines(Graphics g, int x, int y, Color bodyColor) {
g.setColor(bodyColor);
g.fillOval(x, y, 10, 10);
}
@Override
public void drawTwoEngines(Graphics g, int x, int y, Color bodyColor){
DrawEngines(g,x + 65, y + 50, bodyColor);
DrawEngines(g,x + 65, y + 81,bodyColor);
}
public @Override
void drawFourEngines(Graphics g, int x, int y, Color bodyColor){
DrawEngines(g,x + 62, y + 10,bodyColor);
DrawEngines(g,x + 64, y + 101,bodyColor);
DrawEngines(g,x + 64, y + 30,bodyColor);
DrawEngines(g,x + 62, y + 121,bodyColor);
}
@Override
public void drawSixEngines(Graphics g, int x, int y, Color bodyColor){
drawFourEngines(g,x,y,bodyColor);
drawTwoEngines(g,x,y,bodyColor);
}
@Override
public void SwitchDrawEngines(Graphics g, int x, int y, Color bodyColor){
switch(getNumberOfEngines()){
case NumberOfEngines.TWO:
drawTwoEngines(g,x,y,bodyColor);
break;
case NumberOfEngines.FOUR:
drawFourEngines(g,x,y,bodyColor);
break;
case NumberOfEngines.SIX:
drawSixEngines(g,x,y,bodyColor);
break;
}
}
}

View File

@ -0,0 +1,57 @@
package Drawnings.Engines;
import java.awt.*;
public class DrawingTriangleEngines implements IDrawingEngines {
private NumberOfEngines numberOfEngines;
@Override
public NumberOfEngines getNumberOfEngines() {
return numberOfEngines;
}
@Override
public void setAmountOfEngines(int amount){
if(NumberOfEngines.contains(amount)) {
numberOfEngines = NumberOfEngines.getNumber(amount);
}
}
@Override
public void DrawEngines(Graphics g, int x, int y, Color bodyColor) {
g.setColor(bodyColor);
Point[] Nose = new Point[3];
int[] arrX = {x,x,x+10};
int[] arrY = {y,y+12,y+6};
Polygon poly = new Polygon(arrX, arrY, 3);
g.fillPolygon(poly);
}
@Override
public void drawTwoEngines(Graphics g, int x, int y, Color bodyColor){
DrawEngines(g,x + 65, y + 50, bodyColor);
DrawEngines(g,x + 65, y + 81,bodyColor);
}
public @Override
void drawFourEngines(Graphics g, int x, int y, Color bodyColor){
DrawEngines(g,x + 62, y + 10,bodyColor);
DrawEngines(g,x + 64, y + 101,bodyColor);
DrawEngines(g,x + 64, y + 30,bodyColor);
DrawEngines(g,x + 62, y + 121,bodyColor);
}
@Override
public void drawSixEngines(Graphics g, int x, int y, Color bodyColor){
drawFourEngines(g,x,y,bodyColor);
drawTwoEngines(g,x,y,bodyColor);
}
public void SwitchDrawEngines(Graphics g, int x, int y, Color bodyColor){
switch(getNumberOfEngines()){
case NumberOfEngines.TWO:
drawTwoEngines(g,x,y,bodyColor);
break;
case NumberOfEngines.FOUR:
drawFourEngines(g,x,y,bodyColor);
break;
case NumberOfEngines.SIX:
drawSixEngines(g,x,y,bodyColor);
break;
}
}
}

View File

@ -0,0 +1,17 @@
package Drawnings.Engines;
import java.awt.*;
public interface IDrawingEngines {
void setAmountOfEngines(int amount);
NumberOfEngines getNumberOfEngines();
void DrawEngines(Graphics g, int x, int y, Color bodyColor);
void drawTwoEngines(Graphics g, int x, int y, Color bodyColor);
void drawFourEngines(Graphics g, int x, int y, Color bodyColor);
void drawSixEngines(Graphics g, int x, int y, Color bodyColor);
void SwitchDrawEngines(Graphics g, int startPosX, int startPosY, Color bodyColor);
}

View File

@ -1,3 +1,5 @@
package Drawnings.Engines;
public enum NumberOfEngines { public enum NumberOfEngines {
TWO(2), TWO(2),
FOUR(4), FOUR(4),

View File

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

View File

@ -0,0 +1,27 @@
package Entities;
import java.awt.*;
public class EntityStormtrooper extends EntityBaseStormtrooper{
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
private boolean Rockets;
public boolean getRockets() {
return Rockets;
}
private boolean Bombs;
public boolean getBombs() {
return Bombs;
}
private boolean Engines;
public boolean getEngines() {return Engines;}
public EntityStormtrooper(int speed, float weight, Color bodyColor, Color additionalColor, boolean rockets, boolean bombs, boolean engines)
{
super(speed,weight,bodyColor);
AdditionalColor = additionalColor;
Rockets = rockets;
Bombs = bombs;
Engines = engines;
}
}

View File

@ -1,41 +0,0 @@
import java.awt.*;
public class EntityStormtrooper {
private int Speed;
public int getSpeed() {
return Speed;
}
private float Weight;
public float getWeight() {
return Weight;
}
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
private Color AdditionalColor;
public Color getAdditionalColor() {
return AdditionalColor;
}
private boolean Rockets;
public boolean getRockets() {
return Rockets;
}
private boolean Bombs;
public boolean getBombs() {
return Bombs;
}
private boolean Engines;
public boolean getEngines() {return Engines;}
public float Step;
public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean rockets, boolean bombs, boolean engines)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Rockets = rockets;
Bombs = bombs;
Engines = engines;
Step = Speed * 100 / Weight;
}
}

View File

@ -6,13 +6,22 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.util.Random; import java.util.Random;
import Drawnings.DirectionType;
import Drawnings.DrawingBaseStormtrooper;
import Drawnings.DrawingStormtrooper;
import MovementStrategy.*;
public class FormStormtrooper extends JFrame { public class FormStormtrooper extends JFrame {
private AbstractStrategy _strategy;
private final String title; private final String title;
private final Dimension dimension; private final Dimension dimension;
private int Width; private int Width;
private int Height; private int Height;
private final CanvasStormtrooper canvasStormtrooper = new CanvasStormtrooper(); private final CanvasStormtrooper canvasStormtrooper = new CanvasStormtrooper();
private final JButton CreateButton = new JButton("Создать"); private JComboBox ComboBoxStrategy = new JComboBox(new String []{"К центру", "К краю"});
private JButton ButtonStrategy = new JButton("Шаг");
private final JButton CreateBaseStormtrooper = new JButton("Создать базовый бомбардировщик");
private final JButton CreateStormtrooper = new JButton("Создать бомбардировщик");
private final JButton UpButton = new JButton(); private final JButton UpButton = new JButton();
private final JButton DownButton = new JButton(); private final JButton DownButton = new JButton();
private final JButton LeftButton = new JButton(); private final JButton LeftButton = new JButton();
@ -21,13 +30,45 @@ public class FormStormtrooper extends JFrame {
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);
float weight = (float) (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 "Drawnings.DrawingBaseStormtrooper":
canvasStormtrooper._drawingBaseStormtrooper = new DrawingBaseStormtrooper(speed, weight, bodyColor);
canvasStormtrooper._drawingBaseStormtrooper.SetPictureSize(Width, Height);
canvasStormtrooper._drawingBaseStormtrooper.SetPosition( StartPositionX, StartPositionY);
canvasStormtrooper.repaint();
break;
case "Drawnings.DrawingStormtrooper":
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
boolean rockets = new Random().nextBoolean();
boolean bombs = new Random().nextBoolean();
boolean engines = new Random().nextBoolean();
int typeOfEngine = ((int)((Math.random()*3)+1));
canvasStormtrooper._drawingBaseStormtrooper = new DrawingStormtrooper(speed, weight, bodyColor, additionalColor,rockets, bombs, engines, typeOfEngine);
canvasStormtrooper._drawingBaseStormtrooper.SetPictureSize(Width, Height);
canvasStormtrooper._drawingBaseStormtrooper.SetPosition( StartPositionX, StartPositionY);
canvasStormtrooper.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() - 13; Width = getWidth()-10;
Height = getHeight() - 30; Height = getHeight() - 34;
CreateButton.setName("createButton"); _strategy=null;
CreateStormtrooper.setName("createStormtrooperButton");
CreateBaseStormtrooper.setName("createBaseStormtrooperButton");
Icon iconUp = new ImageIcon("Resources\\arrowUp.jpg"); Icon iconUp = new ImageIcon("Resources\\arrowUp.jpg");
UpButton.setIcon(iconUp); UpButton.setIcon(iconUp);
UpButton.setName("UP"); UpButton.setName("UP");
@ -40,35 +81,68 @@ public class FormStormtrooper extends JFrame {
RightButton.setName("RIGHT"); RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("Resources\\arrowRight.jpg"); Icon iconRight = new ImageIcon("Resources\\arrowRight.jpg");
RightButton.setIcon(iconRight); RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() { ButtonStrategy.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int StartPositionX = (int)(Math.random() * 90 + 10); if (canvasStormtrooper._drawingBaseStormtrooper == null) return;
int StartPositionY = (int)(Math.random() * 90 + 10); if (ComboBoxStrategy.isEnabled())
int speed = (int)(Math.random() * 300 + 100); {
float weight = (float) (Math.random() * 3000 + 1000); int index = ComboBoxStrategy.getSelectedIndex();
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); switch(index)
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); {
boolean rockets = new Random().nextBoolean(); case 1:
boolean bombs = new Random().nextBoolean(); _strategy = new MoveToBorder();
boolean engines = new Random().nextBoolean(); break;
canvasStormtrooper._drawingStormtrooper = new DrawingStormtrooper(); case 0:
canvasStormtrooper._drawingStormtrooper.Init(speed, weight, bodyColor, additionalColor,rockets, bombs, engines); _strategy = new MoveToCenter();
canvasStormtrooper._drawingStormtrooper.SetPictureSize(Width, Height); break;
canvasStormtrooper._drawingStormtrooper.SetPosition( StartPositionX, StartPositionY); default:
canvasStormtrooper.repaint(); _strategy = null;
break;
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableStormtrooper(canvasStormtrooper._drawingBaseStormtrooper), Width, Height);
}
if (_strategy == null)
{
return;
}
ComboBoxStrategy.setEnabled(false);
_strategy.MakeStep();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
ComboBoxStrategy.setEnabled(true);
_strategy = null;
}
} }
}); });
CreateBaseStormtrooper.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("Drawnings.DrawingBaseStormtrooper");
}
});
CreateStormtrooper.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("Drawnings.DrawingStormtrooper");
}
});
ActionListener actionListener = new ActionListener() { ActionListener actionListener = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
if (canvasStormtrooper._drawingStormtrooper == null) return; if (canvasStormtrooper._drawingBaseStormtrooper == null) return;
boolean result = switch ((((JButton) (event.getSource())).getName())) { boolean result = switch ((((JButton) (event.getSource())).getName())) {
case "UP" -> canvasStormtrooper._drawingStormtrooper.MoveTransport(DirectionType.Up); case "UP" -> canvasStormtrooper._drawingBaseStormtrooper.MoveTransport(DirectionType.Up);
case "DOWN" -> canvasStormtrooper._drawingStormtrooper.MoveTransport(DirectionType.Down); case "DOWN" -> canvasStormtrooper._drawingBaseStormtrooper.MoveTransport(DirectionType.Down);
case "LEFT" -> canvasStormtrooper._drawingStormtrooper.MoveTransport(DirectionType.Left); case "LEFT" -> canvasStormtrooper._drawingBaseStormtrooper.MoveTransport(DirectionType.Left);
case "RIGHT" -> canvasStormtrooper._drawingStormtrooper.MoveTransport(DirectionType.Right); case "RIGHT" -> canvasStormtrooper._drawingBaseStormtrooper.MoveTransport(DirectionType.Right);
default -> false; default -> false;
}; };
if (result) { if (result) {
@ -83,12 +157,18 @@ public class FormStormtrooper extends JFrame {
setSize(dimension.width,dimension.height); setSize(dimension.width,dimension.height);
setLayout(null); setLayout(null);
canvasStormtrooper.setBounds(0,0, getWidth(), getHeight()); canvasStormtrooper.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 130, 40); CreateBaseStormtrooper.setBounds(10, getHeight() - 90, 130, 40);
CreateStormtrooper.setBounds(160,getHeight()-90,130,40);
UpButton.setBounds(getWidth() - 180, getHeight() - 210, 70, 70); UpButton.setBounds(getWidth() - 180, getHeight() - 210, 70, 70);
DownButton.setBounds(getWidth() - 180, getHeight() - 140, 70, 70); DownButton.setBounds(getWidth() - 180, getHeight() - 140, 70, 70);
RightButton.setBounds(getWidth() - 110, getHeight() - 140, 70, 70); RightButton.setBounds(getWidth() - 110, getHeight() - 140, 70, 70);
LeftButton.setBounds(getWidth() - 250, getHeight() - 140, 70, 70); LeftButton.setBounds(getWidth() - 250, getHeight() - 140, 70, 70);
add(CreateButton); ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
add(CreateBaseStormtrooper);
add(CreateStormtrooper);
add(ComboBoxStrategy);
add(ButtonStrategy);
add(UpButton); add(UpButton);
add(DownButton); add(DownButton);
add(RightButton); add(RightButton);
@ -97,15 +177,18 @@ public class FormStormtrooper extends JFrame {
setVisible(true); setVisible(true);
addComponentListener(new ComponentAdapter() { addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
Width = getWidth() - 13; Width = getWidth() -10;
Height = getHeight() - 30; Height = getHeight() - 34;
if (canvasStormtrooper._drawingStormtrooper != null)canvasStormtrooper._drawingStormtrooper.SetPictureSize(Width, Height); if (canvasStormtrooper._drawingBaseStormtrooper != null)canvasStormtrooper._drawingBaseStormtrooper.SetPictureSize(Width, Height);
canvasStormtrooper.setBounds(0,0, getWidth(), getHeight()); canvasStormtrooper.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 130, 40); CreateBaseStormtrooper.setBounds(10, getHeight() - 90, 130, 40);
CreateStormtrooper.setBounds(160,getHeight()-90,130,40);
UpButton.setBounds(getWidth() - 180, getHeight() - 210, 70, 70); UpButton.setBounds(getWidth() - 180, getHeight() - 210, 70, 70);
DownButton.setBounds(getWidth() - 180, getHeight() - 140, 70, 70); DownButton.setBounds(getWidth() - 180, getHeight() - 140, 70, 70);
RightButton.setBounds(getWidth() - 110, getHeight() - 140, 70, 70); RightButton.setBounds(getWidth() - 110, getHeight() - 140, 70, 70);
LeftButton.setBounds(getWidth() - 250, getHeight() - 140, 70, 70); LeftButton.setBounds(getWidth() - 250, getHeight() - 140, 70, 70);
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
} }
}); });
} }

View File

@ -0,0 +1,60 @@
package MovementStrategy;
public abstract class AbstractStrategy
{
private IMoveableObject _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
public int FieldWidth;
public int FieldHeight;
public StrategyStatus GetStatus (){ return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != StrategyStatus.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_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 _moveableObject.GetObjectPosition();}
protected int GetStep(){
if (_state != StrategyStatus.InProgress)
{
return -1;
}
return _moveableObject.getStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestinaion();
private boolean MoveTo(MovementDirection movementDirection) {
if (_state != StrategyStatus.InProgress) {
return false;
}
if (_moveableObject.TryMoveObject(movementDirection)) {
return _moveableObject.TryMoveObject(movementDirection);
}
return false;
}
}

View File

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

View File

@ -0,0 +1,29 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy
{
@Override
protected boolean IsTargetDestinaion()
{
ObjectParameters objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.LeftBorder - GetStep() <= 0 &&
objParams.RightBorder + GetStep() >= FieldWidth &&
objParams.TopBorder - GetStep() <= 0
&& objParams.ObjectMiddleVertical + GetStep() >= FieldHeight;
}
@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,51 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy
{
@Override
protected boolean IsTargetDestinaion()
{
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,46 @@
package MovementStrategy;
import Drawnings.*;
import Entities.EntityBaseStormtrooper;
public class MoveableStormtrooper implements IMoveableObject
{
private DrawingBaseStormtrooper _stormtrooper ;
public MoveableStormtrooper(DrawingBaseStormtrooper stormtrooper)
{
_stormtrooper = stormtrooper;
}
@Override
public ObjectParameters GetObjectPosition() {
if (_stormtrooper == null || _stormtrooper.EntityBaseStormtrooper == null || _stormtrooper.getPosX()==null || _stormtrooper.getPosY()==null) {
return null;
}
return new ObjectParameters(_stormtrooper.getPosX(), _stormtrooper.getPosY(), _stormtrooper.getWidth(), _stormtrooper.getHeight());
}
@Override
public int getStep(){ return (int)(_stormtrooper.EntityBaseStormtrooper.Step);}
@Override
public boolean TryMoveObject(MovementDirection direction)
{
if (_stormtrooper == null || _stormtrooper.EntityBaseStormtrooper == null)
{
return false;
}
return _stormtrooper.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,12 @@
package MovementStrategy;
public enum MovementDirection {
///Вверх
Up,
///Вниз
Down,
///Влево
Left,
///Вправо
Right
}

View File

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

View File

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