PIBD-13_Fomichev_V.S._LabWork02_Hard #2
@ -1,9 +1,9 @@
|
||||
package DrawningRollers;
|
||||
|
||||
public enum CountRollers {
|
||||
Four(4),
|
||||
Five(5),
|
||||
Six(6);
|
||||
FourRollers(4),
|
||||
FiveRollers(5),
|
||||
SixRollers(6);
|
||||
private int numberofrollers;
|
||||
CountRollers(int numberofrollers) {
|
||||
this.numberofrollers = numberofrollers;
|
||||
|
@ -1,53 +0,0 @@
|
||||
package DrawningRollers;
|
||||
|
||||
import java.awt.*;
|
||||
public class DrawningRollers {
|
||||
private CountRollers _rollers;
|
||||
public CountRollers getCount() {
|
||||
return _rollers;
|
||||
}
|
||||
|
||||
public void SetCount(int count) {
|
||||
switch (count) {
|
||||
case 1: // 4
|
||||
_rollers = CountRollers.Four;
|
||||
break;
|
||||
case 2: // 5
|
||||
_rollers = CountRollers.Five;
|
||||
break;
|
||||
case 3: // 6
|
||||
_rollers = CountRollers.Six;
|
||||
break;
|
||||
default:
|
||||
_rollers = CountRollers.Four;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawRollers(Graphics2D g, int _StartPosX, int _StartPosY) {
|
||||
g.setColor(Color.BLACK);
|
||||
if (_rollers == null) {
|
||||
return;
|
||||
}
|
||||
//нижние катки ЦВЕТ
|
||||
g.setColor(Color.GRAY);
|
||||
g.fillOval(_StartPosX + 40, _StartPosY + 90, 15, 15);
|
||||
g.fillOval(_StartPosX + 55, _StartPosY + 90, 15, 15);
|
||||
if (_rollers != CountRollers.Four) {
|
||||
g.fillOval(_StartPosX + 70, _StartPosY + 90, 15, 15);
|
||||
}
|
||||
if (_rollers == CountRollers.Six) {
|
||||
g.fillOval(_StartPosX + 85, _StartPosY + 90, 15, 15);
|
||||
}
|
||||
//нижние катки ОТРИСОВКА
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawOval(_StartPosX + 40, _StartPosY + 90, 15, 15);
|
||||
g.drawOval( _StartPosX + 55, _StartPosY + 90, 15, 15);
|
||||
if (_rollers != CountRollers.Four) {
|
||||
g.drawOval( _StartPosX+ 70, _StartPosY + 90, 15, 15);
|
||||
}
|
||||
if (_rollers == CountRollers.Six) {
|
||||
g.drawOval(_StartPosX + 85, _StartPosY + 90, 15, 15);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,27 +4,56 @@ import java.awt.*;
|
||||
|
||||
public class DrawningRollersCircle implements IDifferentRollers {
|
||||
private CountRollers _rollers;
|
||||
|
||||
@Override
|
||||
public void setNumberRollers(int numberofrollers) {
|
||||
for (CountRollers numofenum : CountRollers.values()) {
|
||||
if (numofenum.getCountRollers() == numberofrollers) {
|
||||
_rollers = numofenum;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CountRollers getNumberOfRollers() {
|
||||
public CountRollers getCount() {
|
||||
return _rollers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawRollers(Graphics2D g, int x, int y, int width, int height, Color bodyColor) {
|
||||
public void SetCount(int count) {
|
||||
switch (count) {
|
||||
case 4:
|
||||
_rollers = CountRollers.FourRollers;
|
||||
break;
|
||||
case 5:
|
||||
_rollers = CountRollers.FiveRollers;
|
||||
break;
|
||||
case 6:
|
||||
_rollers = CountRollers.SixRollers;
|
||||
break;
|
||||
default:
|
||||
_rollers = CountRollers.FourRollers;
|
||||
break;
|
||||
}
|
||||
}
|
||||
protected void drawRollers(Graphics2D g, int posX, int posY) {
|
||||
g.setColor(Color.cyan);
|
||||
g.fillOval(x, y, 3, 3);
|
||||
g.fillOval(posX, posY, 3, 3);
|
||||
g.setColor(Color.black);
|
||||
g.drawOval(x, y, 3, 3);
|
||||
g.drawOval(posX, posY, 3, 3);
|
||||
}
|
||||
public void Draw(Graphics2D g, int _startPosX, int _startPosY) {
|
||||
g.setColor(Color.BLACK);
|
||||
if (_rollers == null) {
|
||||
return;
|
||||
}
|
||||
//нижние катки ЦВЕТ
|
||||
g.setColor(Color.GRAY);
|
||||
g.fillOval(_startPosX + 40, _startPosY + 90, 15, 15);
|
||||
g.fillOval(_startPosX + 55, _startPosY + 90, 15, 15);
|
||||
if (_rollers != CountRollers.FourRollers) {
|
||||
g.fillOval(_startPosX + 70, _startPosY + 90, 15, 15);
|
||||
}
|
||||
if (_rollers == CountRollers.SixRollers) {
|
||||
g.fillOval(_startPosX + 85, _startPosY + 90, 15, 15);
|
||||
}
|
||||
//нижние катки ОТРИСОВКА
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawOval(_startPosX + 40, _startPosY + 90, 15, 15);
|
||||
g.drawOval( _startPosX + 55, _startPosY + 90, 15, 15);
|
||||
if (_rollers != CountRollers.FourRollers) {
|
||||
g.drawOval( _startPosX+ 70, _startPosY + 90, 15, 15);
|
||||
}
|
||||
if (_rollers == CountRollers.SixRollers) {
|
||||
g.drawOval(_startPosX + 85, _startPosY + 90, 15, 15);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,28 +2,11 @@ package DrawningRollers;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningRollersHeart implements IDifferentRollers {
|
||||
private CountRollers _rollers;
|
||||
public class DrawningRollersHeart extends DrawningRollersCircle {
|
||||
|
||||
@Override
|
||||
public void setNumberRollers(int numberofportholes) {
|
||||
for (CountRollers numofenum : CountRollers.values()) {
|
||||
if (numofenum.getCountRollers() == numberofportholes) {
|
||||
_rollers = numofenum;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CountRollers getNumberOfRollers() {
|
||||
return _rollers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawRollers(Graphics2D g, int x, int y, int width, int height, Color bodyColor) {
|
||||
int[] HeartX = {x + 2, x, x, x + 1, x + 2, x + 3, x + 5, x + 5};
|
||||
int[] HeartY = {y + 4, y + 2, y, y, y + 1, y, y, y + 2};
|
||||
protected void drawRollers(Graphics2D g, int posX, int posY) {
|
||||
int[] HeartX = {posX + 2, posX, posX, posX + 1, posX + 2, posX + 3, posX + 5, posX + 5};
|
||||
int[] HeartY = {posY + 4, posY + 2, posY, posY, posY + 1, posY, posY, posY + 2};
|
||||
g.setColor(Color.cyan);
|
||||
g.fillPolygon(HeartX, HeartY, HeartX.length);
|
||||
g.setColor(Color.black);
|
||||
|
@ -2,29 +2,11 @@ package DrawningRollers;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningRollersSquare implements IDifferentRollers{
|
||||
private CountRollers _roller;
|
||||
|
||||
@Override
|
||||
public void setNumberRollers(int numberofportholes) {
|
||||
for (CountRollers numofenum : CountRollers.values()) {
|
||||
if (numofenum.getCountRollers() == numberofportholes) {
|
||||
_roller = numofenum;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CountRollers getNumberOfRollers() {
|
||||
return _roller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawRollers(Graphics2D g, int x, int y, int width, int height, Color bodyColor) {
|
||||
public class DrawningRollersSquare extends DrawningRollersCircle{
|
||||
protected void drawRollers(Graphics2D g, int posX, int posY) {
|
||||
g.setColor(Color.cyan);
|
||||
g.fillRect(x, y, 3, 3);
|
||||
g.fillRect(posX, posY, 3, 3);
|
||||
g.setColor(Color.black);
|
||||
g.drawRect(x, y, 3, 3);
|
||||
g.drawRect(posX, posY, 3, 3);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package DrawningRollers;
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDifferentRollers {
|
||||
void setNumberRollers(int numberofrollers);
|
||||
CountRollers getNumberOfRollers();
|
||||
void DrawRollers(Graphics2D g, int x, int y, int width, int height, Color bodyColor);
|
||||
public CountRollers getCount();
|
||||
public void SetCount (int count);
|
||||
void Draw(Graphics2D g, int _startPosX, int _startPoxY);
|
||||
}
|
||||
|
@ -1,9 +0,0 @@
|
||||
package Drawnings;
|
||||
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
Unknown
|
||||
}
|
@ -8,41 +8,44 @@ import javax.swing.text.html.parser.Entity;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
public class DrawningAntiAircraftGun extends DrawningArmoredCar{
|
||||
public DrawningAntiAircraftGun(int speed, double weight, Color bodycolor, Color additionalcolor, boolean isTower, boolean isRadar) {
|
||||
EntityArmoredCar = new EntityAntiAircraftGun(speed, weight, bodycolor, additionalcolor, isTower, isRadar);
|
||||
SetAmountAndTypeRollers();
|
||||
public DrawningAntiAircraftGun(int speed, float weight, Color bodyColor, int countRollers,Color additionalColor, boolean isTower, boolean isRadar, int width, int height) {
|
||||
super(speed, weight, bodyColor,countRollers , width, height);
|
||||
if (entityArmoredCar != null) {
|
||||
entityArmoredCar = new EntityAntiAircraftGun(speed, weight, bodyColor, additionalColor, isTower, isRadar);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
|
||||
if (EntityArmoredCar == null || !(EntityArmoredCar instanceof EntityAntiAircraftGun AntiAircraftGun)
|
||||
|| _StartPosX == null || _StartPosY == null)
|
||||
if (entityArmoredCar == null) {
|
||||
return;
|
||||
}
|
||||
g.setColor(Color.BLACK);
|
||||
super.DrawTransport(g);
|
||||
Color additionalColor = ((EntityAntiAircraftGun) entityArmoredCar).getAdditionalColor();
|
||||
//отрисовки башни
|
||||
if (EntityAntiAircraftGun.getIsTower())
|
||||
if (((EntityAntiAircraftGun) entityArmoredCar).IsTower())
|
||||
{
|
||||
g.setColor(EntityAntiAircraftGun.getAdditionalColor());
|
||||
g.fillRect(_StartPosX + 35, _StartPosY+ 50, 35, 15);
|
||||
g.setColor(additionalColor);
|
||||
g.fillRect(_startPosX + 35, _startPosY+ 50, 35, 15);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX + 35, _StartPosY + 50, 35, 15);
|
||||
g.drawLine(_StartPosX + 45, _StartPosY + 50, _StartPosX + 90, _StartPosY + 20);
|
||||
g.drawLine( _StartPosX + 60, _StartPosY + 50, _StartPosX + 95, _StartPosY + 27);
|
||||
g.drawLine( _StartPosX + 45, _StartPosY + 50, _StartPosX + 60, _StartPosY + 50);
|
||||
g.drawLine( _StartPosX + 90, _StartPosY + 20, _StartPosX + 95, _StartPosY + 27);
|
||||
g.drawLine( _StartPosX + 90, _StartPosY + 20, _StartPosX + 100, _StartPosY + 20);
|
||||
g.drawLine( _StartPosX + 100, _StartPosY + 20, _StartPosX + 95, _StartPosY + 27);
|
||||
g.drawRect(_startPosX + 35, _startPosY + 50, 35, 15);
|
||||
g.drawLine(_startPosX + 45, _startPosY + 50, _startPosX + 90, _startPosY + 20);
|
||||
g.drawLine( _startPosX + 60, _startPosY + 50, _startPosX + 95, _startPosY + 27);
|
||||
g.drawLine( _startPosX + 45, _startPosY + 50, _startPosX + 60, _startPosY + 50);
|
||||
g.drawLine( _startPosX + 90, _startPosY + 20, _startPosX + 95, _startPosY + 27);
|
||||
g.drawLine( _startPosX + 90, _startPosY + 20, _startPosX + 100, _startPosY + 20);
|
||||
g.drawLine( _startPosX + 100, _startPosY + 20, _startPosX + 95, _startPosY + 27);
|
||||
|
||||
}
|
||||
//отрисовка радара
|
||||
if (EntityAntiAircraftGun.getIsRadar())
|
||||
if (((EntityAntiAircraftGun) entityArmoredCar).IsRadar())
|
||||
{;
|
||||
g.drawLine(_StartPosX + 20, _StartPosY + 65, _StartPosX + 20, _StartPosY + 40);
|
||||
g.drawRect( _StartPosX + 10, _StartPosY + 20, 20, 20);
|
||||
g.setColor(EntityAntiAircraftGun.getAdditionalColor());
|
||||
g.fillRect(_StartPosX + 10, _StartPosY + 20, 20, 20);
|
||||
g.drawLine(_startPosX + 20, _startPosY + 65, _startPosX + 20, _startPosY + 40);
|
||||
g.drawRect( _startPosX + 10, _startPosY + 20, 20, 20);
|
||||
g.setColor(additionalColor);
|
||||
g.fillRect(_startPosX + 10, _startPosY + 20, 20, 20);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,155 +2,146 @@ package Drawnings;
|
||||
|
||||
import DrawningRollers.*;
|
||||
import Entities.*;
|
||||
import MovementStrategy.MovementDirection;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningArmoredCar extends JPanel {
|
||||
public Entities.EntityArmoredCar EntityArmoredCar;
|
||||
public IDifferentRollers drawningRollers;
|
||||
private Integer picture_width;
|
||||
private Integer picture_height;
|
||||
protected Integer _StartPosX;
|
||||
public Integer GetPosX() {return _StartPosX;}
|
||||
protected Integer _StartPosY;
|
||||
public Integer GetPosY() {return _StartPosY;}
|
||||
protected int drawningCarWidth = 120;
|
||||
public Integer GetWidth() {return drawningCarWidth;}
|
||||
protected int drawningCarHeight = 90;
|
||||
public Integer GetHeight() {return drawningCarHeight;}
|
||||
protected DrawningArmoredCar() {
|
||||
picture_width = null;
|
||||
picture_height = null;
|
||||
_StartPosX = null;
|
||||
_StartPosY = null;
|
||||
public EntityArmoredCar entityArmoredCar;
|
||||
public IDifferentRollers _rollers;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
private int _carWidth = 120;
|
||||
private int _carHeight = 90;
|
||||
|
||||
public int GetPosX() {
|
||||
return _startPosX;
|
||||
}
|
||||
|
||||
protected void SetAmountAndTypeRollers() {
|
||||
public int GetPosY() {
|
||||
return _startPosY;
|
||||
}
|
||||
|
||||
public int GetWidth() {
|
||||
return _carWidth;
|
||||
}
|
||||
|
||||
public int GetHeight() {
|
||||
return _carHeight;
|
||||
}
|
||||
|
||||
|
||||
public DrawningArmoredCar(int speed, float weight, Color bodyColor, int countRollers ,int width, int height) {
|
||||
if (width < _carHeight || height < _carWidth)
|
||||
return;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
entityArmoredCar = new EntityArmoredCar(speed, weight, bodyColor);
|
||||
|
||||
Random random = new Random();
|
||||
int numberofrollers = random.nextInt(0,3);
|
||||
switch (numberofrollers) {
|
||||
switch (random.nextInt(0, 3)) {
|
||||
case 0:
|
||||
_rollers = new DrawningRollersCircle();
|
||||
break;
|
||||
case 1:
|
||||
drawningRollers = new DrawningRollersCircle();
|
||||
_rollers = new DrawningRollersHeart();
|
||||
break;
|
||||
case 2:
|
||||
drawningRollers = new DrawningRollersSquare();
|
||||
break;
|
||||
case 3:
|
||||
drawningRollers = new DrawningRollersHeart();
|
||||
_rollers = new DrawningRollersSquare();
|
||||
break;
|
||||
default:
|
||||
numberofrollers = 0;
|
||||
_rollers = new DrawningRollersCircle();
|
||||
break;
|
||||
}
|
||||
drawningRollers.setNumberRollers(numberofrollers);
|
||||
_rollers.SetCount(countRollers);
|
||||
}
|
||||
public DrawningArmoredCar(int speed, double weight, Color bodycolor) {
|
||||
super();
|
||||
EntityArmoredCar = new EntityArmoredCar(speed, weight, bodycolor);
|
||||
SetAmountAndTypeRollers();
|
||||
}
|
||||
public boolean SetPictureSize(int width, int height) {
|
||||
if (width < drawningCarWidth || height < drawningCarHeight) return false;
|
||||
picture_width = width;
|
||||
picture_height = height;
|
||||
if (_StartPosX != null || _StartPosY != null) {
|
||||
if (_StartPosX + drawningCarWidth > picture_width)
|
||||
{
|
||||
_StartPosX = _StartPosX - (_StartPosX + drawningCarWidth - picture_width);
|
||||
}
|
||||
else if (_StartPosX < 0) _StartPosX = 0;
|
||||
if (_StartPosY + drawningCarHeight > picture_height)
|
||||
{
|
||||
_StartPosY = _StartPosY - (_StartPosY + drawningCarHeight - picture_height);
|
||||
}
|
||||
else if (_StartPosY < 0) _StartPosY = 0;
|
||||
public boolean CanMove(MovementDirection direction) {
|
||||
if (entityArmoredCar == null) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public void SetPosition(int x, int y) {
|
||||
if (!(picture_width != null && picture_height != null)) return;
|
||||
if (x + drawningCarWidth > picture_width)
|
||||
{
|
||||
_StartPosX = x - (x + drawningCarWidth - picture_width);
|
||||
}
|
||||
else if (x < 0) _StartPosX = 0;
|
||||
else _StartPosX = x;
|
||||
if (y + drawningCarHeight > picture_height)
|
||||
{
|
||||
_StartPosY = y - (y + drawningCarHeight - picture_height);
|
||||
}
|
||||
else if (y < 0) _StartPosY = 0;
|
||||
else _StartPosY = y;
|
||||
}
|
||||
public boolean MoveTransport(DirectionType direction) {
|
||||
if (EntityArmoredCar == null || _StartPosX == null || _StartPosY == null) return false;
|
||||
switch (direction) {
|
||||
case Left:
|
||||
if (_StartPosX - EntityArmoredCar.Step > 0) {
|
||||
_StartPosX -= (int)EntityArmoredCar.Step;
|
||||
}
|
||||
return true;
|
||||
case Up:
|
||||
if (_StartPosY - EntityArmoredCar.Step > 0)
|
||||
{
|
||||
_StartPosY -= (int)EntityArmoredCar.Step;
|
||||
}
|
||||
return true;
|
||||
return _startPosX - entityArmoredCar.Step > 5;
|
||||
case Right:
|
||||
if (_StartPosX + drawningCarWidth + (int)EntityArmoredCar.Step < picture_width - EntityArmoredCar.Step)
|
||||
{
|
||||
_StartPosX += (int)EntityArmoredCar.Step;
|
||||
}
|
||||
return true;
|
||||
return _startPosX + _carWidth + entityArmoredCar.Step < _pictureWidth;
|
||||
case Up:
|
||||
return _startPosY - entityArmoredCar.Step > 0;
|
||||
case Down:
|
||||
if (_StartPosY + drawningCarHeight + (int)EntityArmoredCar.Step < picture_height - EntityArmoredCar.Step)
|
||||
{
|
||||
_StartPosY += (int)EntityArmoredCar.Step;
|
||||
}
|
||||
return true;
|
||||
return _startPosY + _carHeight + entityArmoredCar.Step < _pictureHeight;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
|
||||
if (EntityArmoredCar == null ) {
|
||||
public void SetPosition(int x, int y) {
|
||||
if (x > _pictureWidth || y > _pictureHeight || x < 0 || y < 0) {
|
||||
_startPosX = 0;
|
||||
_startPosY = 0;
|
||||
} else {
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
}
|
||||
public void MoveTransport(MovementDirection direction) {
|
||||
if (!CanMove(direction) || entityArmoredCar == null) {
|
||||
return;
|
||||
}
|
||||
;
|
||||
|
||||
g.drawOval(_StartPosX + 10, _StartPosY + 75, 30, 30);
|
||||
g.drawOval(_StartPosX + 100, _StartPosY + 75, 30, 30);
|
||||
g.drawRect(_StartPosX + 25, _StartPosY + 75, 90, 30);
|
||||
switch (direction) {
|
||||
case Left:
|
||||
_startPosX -= entityArmoredCar.Step;
|
||||
break;
|
||||
case Right:
|
||||
_startPosX += entityArmoredCar.Step;
|
||||
break;
|
||||
case Up:
|
||||
_startPosY -= entityArmoredCar.Step;
|
||||
break;
|
||||
case Down:
|
||||
_startPosY += entityArmoredCar.Step;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
|
||||
if (entityArmoredCar == null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
g.drawOval(_startPosX + 10, _startPosY + 75, 30, 30);
|
||||
g.drawOval(_startPosX + 100, _startPosY + 75, 30, 30);
|
||||
g.drawRect(_startPosX + 25, _startPosY + 75, 90, 30);
|
||||
|
||||
|
||||
//границы цвет
|
||||
g.setColor(EntityArmoredCar.getBodyColor());
|
||||
g.fillRect(_StartPosX + 35, _StartPosY + 40, 35, 30);
|
||||
g.fillOval(_StartPosX + 10, _StartPosY + 75, 30, 30);
|
||||
g.fillOval(_StartPosX + 100, _StartPosY+ 75, 30, 30);
|
||||
g.fillRect(_StartPosX+ 25, _StartPosY + 75, 90, 30);
|
||||
g.setColor(entityArmoredCar.getBodyColor());
|
||||
g.fillRect(_startPosX + 35, _startPosY + 40, 35, 30);
|
||||
g.fillOval(_startPosX + 10, _startPosY + 75, 30, 30);
|
||||
g.fillOval(_startPosX + 100, _startPosY+ 75, 30, 30);
|
||||
g.fillRect(_startPosX+ 25, _startPosY + 75, 90, 30);
|
||||
|
||||
// границы арт. установки
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect(_StartPosX+ 35, _StartPosY + 40, 35, 30);
|
||||
g.drawRect(_StartPosX+ 10, _StartPosY + 65, 120, 13);
|
||||
g.drawRect(_startPosX+ 35, _startPosY + 40, 35, 30);
|
||||
g.drawRect(_startPosX+ 10, _startPosY + 65, 120, 13);
|
||||
|
||||
|
||||
//Большие катки ЦВЕТ
|
||||
g.setColor(Color.GRAY);
|
||||
g.fillOval(_StartPosX + 13, _StartPosY + 78, 24, 24);
|
||||
g.fillOval( _StartPosX + 103, _StartPosY + 78, 24, 24);
|
||||
g.fillOval(_startPosX + 13, _startPosY + 78, 24, 24);
|
||||
g.fillOval( _startPosX + 103, _startPosY + 78, 24, 24);
|
||||
|
||||
//Большие катки ОТРИСОВКА
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawOval( _StartPosX + 13, _StartPosY + 78, 24, 24);
|
||||
g.drawOval( _StartPosX + 103, _StartPosY + 78, 24, 24);
|
||||
g.setColor(EntityArmoredCar.getBodyColor());
|
||||
g.fillRect(_StartPosX + 10, _StartPosY+ 65, 120, 13);
|
||||
|
||||
g.drawOval( _startPosX + 13, _startPosY + 78, 24, 24);
|
||||
g.drawOval( _startPosX + 103, _startPosY + 78, 24, 24);
|
||||
g.setColor(entityArmoredCar.getBodyColor());
|
||||
g.fillRect(_startPosX + 10, _startPosY+ 65, 120, 13);
|
||||
|
||||
_rollers.Draw(g,_startPosX, _startPosY);
|
||||
}
|
||||
}
|
||||
|
@ -2,18 +2,18 @@ package Entities;
|
||||
|
||||
import java.awt.*;
|
||||
public class EntityAntiAircraftGun extends EntityArmoredCar{
|
||||
private static Color AdditionalColor;
|
||||
public static Color getAdditionalColor() {return AdditionalColor;}
|
||||
private static boolean IsTower;
|
||||
public static boolean getIsTower() {return IsTower;}
|
||||
private static boolean IsRadar;
|
||||
public static boolean getIsRadar() {return IsRadar;}
|
||||
private Color AdditionalColor;
|
||||
private boolean IsTower;
|
||||
private boolean IsRadar;
|
||||
|
||||
public EntityAntiAircraftGun(int speed, double weight, Color bodycolor, Color additionalcolor, boolean isTower, boolean isRadar)
|
||||
public EntityAntiAircraftGun(int speed, float weight, Color bodycolor, Color additionalcolor, boolean isTower, boolean isRadar)
|
||||
{
|
||||
super(speed,weight,bodycolor);
|
||||
AdditionalColor = additionalcolor;
|
||||
IsTower = isTower;
|
||||
IsRadar = isRadar;
|
||||
}
|
||||
public Color getAdditionalColor() { return AdditionalColor; }
|
||||
public boolean IsTower() { return IsTower; }
|
||||
public boolean IsRadar() { return IsRadar; }
|
||||
}
|
||||
|
@ -2,11 +2,20 @@ package Entities;
|
||||
import java.awt.*;
|
||||
public class EntityArmoredCar {
|
||||
private int Speed;
|
||||
private double Weight;
|
||||
private float Weight;
|
||||
private Color BodyColor;
|
||||
public Color getBodyColor() {return BodyColor;}
|
||||
public double Step;
|
||||
public EntityArmoredCar(int speed, double weight, Color bodycolor)
|
||||
public float Step;
|
||||
|
||||
public int getSpeed() {
|
||||
return Speed;
|
||||
}
|
||||
public float getWeight() {
|
||||
return Weight;
|
||||
}
|
||||
public Color getBodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
public EntityArmoredCar(int speed, float weight, Color bodycolor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
|
@ -1,5 +1,3 @@
|
||||
import Drawnings.CanvasAntiAircraftGun;
|
||||
import Drawnings.DirectionType;
|
||||
import Drawnings.DrawningAntiAircraftGun;
|
||||
import Drawnings.DrawningArmoredCar;
|
||||
import MovementStrategy.*;
|
||||
@ -8,193 +6,242 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class FormAntiAircraftGun extends JFrame {
|
||||
private String title;
|
||||
private Dimension dimension;
|
||||
private int Width, Height;
|
||||
private CanvasAntiAircraftGun canvasAntiAircraftGun = new CanvasAntiAircraftGun();
|
||||
private JButton CreateButton = new JButton("Создать зенитную установку");;
|
||||
private JButton CreateArmoredCarButton = new JButton("Создать бронированную машину");
|
||||
private JButton UpButton = new JButton();
|
||||
private JButton DownButton = new JButton();;
|
||||
private JButton LeftButton = new JButton();;
|
||||
private JButton RightButton = new JButton();
|
||||
private AbstractStrategy _strategy;
|
||||
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
|
||||
private JButton ButtonStrategy = new JButton("Шаг");
|
||||
public FormAntiAircraftGun(String title, Dimension dimension) {
|
||||
this.title = title;
|
||||
this.dimension = dimension;
|
||||
private int width;
|
||||
private int height;
|
||||
private DrawningArmoredCar _drawningArmoredCar;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
private Canvas canvas;
|
||||
|
||||
// выбор кол-ва катков
|
||||
JLabel labelCount;
|
||||
private JTextField fieldCount;
|
||||
|
||||
// выбор стратегии
|
||||
JLabel labelStrategy;
|
||||
JComboBox comboBoxStrategy;
|
||||
JButton buttonStrategy;
|
||||
|
||||
private JButton buttonCreateAntiAircraftGun;
|
||||
private JButton buttonCreateArmoredCar;
|
||||
private JButton buttonUp;
|
||||
private JButton buttonDown;
|
||||
private JButton buttonRight;
|
||||
private JButton buttonLeft;
|
||||
public FormAntiAircraftGun() {
|
||||
super("Создание зенитной установки");
|
||||
InitializeComponent();
|
||||
setVisible(true);
|
||||
}
|
||||
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 "DrawningArmoredCar":
|
||||
canvasAntiAircraftGun._drawingArmoredCar = new DrawningArmoredCar(speed, weight, bodyColor);
|
||||
canvasAntiAircraftGun._drawingArmoredCar.SetPictureSize(Width, Height);
|
||||
canvasAntiAircraftGun._drawingArmoredCar.SetPosition(StartPositionX, StartPositionY);
|
||||
canvasAntiAircraftGun.repaint();
|
||||
break;
|
||||
case "DrawningAntiAircraftGun":
|
||||
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
|
||||
boolean IsCompartment = new Random().nextBoolean();
|
||||
boolean isAdditionalEngine = new Random().nextBoolean();;
|
||||
canvasAntiAircraftGun._drawingArmoredCar = new DrawningAntiAircraftGun(speed, weight, bodyColor, additionalColor, IsCompartment , isAdditionalEngine);
|
||||
canvasAntiAircraftGun._drawingArmoredCar.SetPictureSize(Width, Height);
|
||||
canvasAntiAircraftGun._drawingArmoredCar.SetPosition(StartPositionX, StartPositionY);
|
||||
canvasAntiAircraftGun.repaint();
|
||||
break;
|
||||
default: return;
|
||||
}
|
||||
_strategy = null;
|
||||
ComboBoxStrategy.setEnabled(true);
|
||||
}
|
||||
public void Init() {
|
||||
setTitle(title);
|
||||
setMinimumSize(dimension);
|
||||
private void InitializeComponent() {
|
||||
canvas = new Canvas();
|
||||
|
||||
labelCount = new JLabel("Введите число катков:");
|
||||
fieldCount = new JTextField();
|
||||
|
||||
labelStrategy = new JLabel("Шаг стратегии:");
|
||||
comboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
|
||||
buttonStrategy = new JButton("Выбрать стратегию");
|
||||
buttonStrategy.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonCreateAntiAircraftGun = new JButton("Создать зенитную установку");
|
||||
buttonCreateAntiAircraftGun.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonCreateArmoredCar = new JButton("Создать бронемашину");
|
||||
buttonCreateArmoredCar.setMargin(new Insets(0, 0, 0, 0));
|
||||
|
||||
buttonUp = new JButton();
|
||||
buttonUp.setName("up");
|
||||
buttonUp.setIcon(new ImageIcon("src/images/up.jpg"));
|
||||
|
||||
buttonRight = new JButton();
|
||||
buttonRight.setName("right");
|
||||
buttonRight.setIcon(new ImageIcon("src/images/right.jpg"));
|
||||
|
||||
buttonLeft = new JButton();
|
||||
buttonLeft.setName("left");
|
||||
buttonLeft.setIcon(new ImageIcon("src/images/left.jpg"));
|
||||
|
||||
buttonDown = new JButton();
|
||||
buttonDown.setName("down");
|
||||
buttonDown.setIcon(new ImageIcon("src/images/down.jpg"));
|
||||
|
||||
setSize(800, 500);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
_strategy = null;
|
||||
CreateButton.setName("CREATE");
|
||||
CreateArmoredCarButton.setName("CREATE ARMORED CAR BUTTON");
|
||||
Icon iconUp = new ImageIcon("src/images/up.jpg");
|
||||
UpButton.setIcon(iconUp);
|
||||
UpButton.setName("UP");
|
||||
DownButton.setName("DOWN");
|
||||
Icon iconDown = new ImageIcon("src/images/down.jpg");
|
||||
DownButton.setIcon(iconDown);
|
||||
LeftButton.setName("LEFT");
|
||||
Icon iconLeft = new ImageIcon("src/images/left.jpg");
|
||||
LeftButton.setIcon(iconLeft);
|
||||
RightButton.setName("RIGHT");
|
||||
Icon iconRight = new ImageIcon("src/images/right.jpg");
|
||||
RightButton.setIcon(iconRight);
|
||||
setLayout(null);
|
||||
|
||||
CreateButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawningArmoredCar");
|
||||
}
|
||||
});
|
||||
buttonCreateAntiAircraftGun.setBounds(12, 355, 146, 33);
|
||||
buttonCreateArmoredCar.setBounds(182, 355, 146, 33);
|
||||
|
||||
CreateArmoredCarButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawningAntiAircraftGun");
|
||||
labelCount.setBounds(42, 405, 240, 20);
|
||||
fieldCount.setBounds(240, 407, 48, 20);
|
||||
|
||||
labelStrategy.setBounds(630, 20, 146, 33);
|
||||
comboBoxStrategy.setBounds(630, 50, 146, 20);
|
||||
buttonStrategy.setBounds(630, 80, 146, 33);
|
||||
|
||||
buttonUp.setBounds(679, 313, 48, 44);
|
||||
buttonRight.setBounds(728, 358, 48, 44);
|
||||
buttonLeft.setBounds(630, 358, 48, 44);
|
||||
buttonDown.setBounds(679, 358, 48, 44);
|
||||
labelCount.setBounds(12, 405, 240, 20);
|
||||
fieldCount.setBounds(210, 407, 48, 20);
|
||||
canvas.setBounds(0, 0, 790, 460);
|
||||
|
||||
add(buttonCreateAntiAircraftGun);
|
||||
add(buttonCreateArmoredCar);
|
||||
add(labelCount);
|
||||
add(fieldCount);
|
||||
add(labelStrategy);
|
||||
add(comboBoxStrategy);
|
||||
add(buttonStrategy);
|
||||
add(buttonUp);
|
||||
add(buttonRight);
|
||||
add(buttonDown);
|
||||
add(buttonLeft);
|
||||
add(labelCount);
|
||||
add(fieldCount);
|
||||
add(canvas);
|
||||
|
||||
// логика формы
|
||||
buttonCreateAntiAircraftGun.addActionListener(buttonCreateAntiAircraftGunListener);
|
||||
buttonCreateArmoredCar.addActionListener(buttonCreateArmoredCarListener);
|
||||
buttonStrategy.addActionListener(buttonStrategyListener);
|
||||
buttonUp.addActionListener(buttonsMoveListener);
|
||||
buttonRight.addActionListener(buttonsMoveListener);
|
||||
buttonDown.addActionListener(buttonsMoveListener);
|
||||
buttonLeft.addActionListener(buttonsMoveListener);
|
||||
}
|
||||
ActionListener buttonCreateAntiAircraftGunListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int countRollers;
|
||||
try {
|
||||
countRollers = Integer.parseInt(fieldCount.getText());
|
||||
} catch (Exception ex) {
|
||||
countRollers = 0;
|
||||
}
|
||||
});
|
||||
ButtonStrategy.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (canvasAntiAircraftGun._drawingArmoredCar == 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 MoveableArmoredCar(canvasAntiAircraftGun._drawingArmoredCar), Width, Height);
|
||||
if (countRollers != 4 && countRollers != 5 && countRollers != 6) {
|
||||
JOptionPane.showMessageDialog(null, "Число должно быть равно 4, 5 или 6.\nКол-во катков приравнено к 4");
|
||||
countRollers = 4;
|
||||
}
|
||||
|
||||
Random rand = new Random();
|
||||
_drawningArmoredCar = new DrawningArmoredCar(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
|
||||
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
|
||||
countRollers,
|
||||
canvas.getWidth(), canvas.getHeight());
|
||||
|
||||
_drawningArmoredCar.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
canvas.repaint();
|
||||
}
|
||||
};
|
||||
ActionListener buttonCreateArmoredCarListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int countRollers;
|
||||
try {
|
||||
countRollers = Integer.parseInt(fieldCount.getText());
|
||||
} catch (Exception ex) {
|
||||
countRollers = 0;
|
||||
}
|
||||
if (countRollers != 4 && countRollers != 5 && countRollers != 6) {
|
||||
JOptionPane.showMessageDialog(null, "Число должно быть равно 4, 5 или 6.\nКол-во катков приравнено к 4");
|
||||
countRollers = 4;
|
||||
}
|
||||
|
||||
Random rand = new Random();
|
||||
_drawningArmoredCar = new DrawningAntiAircraftGun(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
|
||||
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
|
||||
countRollers,
|
||||
new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)),
|
||||
rand.nextBoolean(), rand.nextBoolean(),
|
||||
canvas.getWidth(), canvas.getHeight());
|
||||
|
||||
_drawningArmoredCar.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
canvas.repaint();
|
||||
}
|
||||
};
|
||||
ActionListener buttonStrategyListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_drawningArmoredCar == null) {
|
||||
return;
|
||||
}
|
||||
if (comboBoxStrategy.isEnabled()) {
|
||||
|
||||
switch (comboBoxStrategy.getSelectedIndex()) {
|
||||
case 0:
|
||||
_abstractStrategy = new MoveToCenter();
|
||||
break;
|
||||
case 1:
|
||||
_abstractStrategy = new MoveToBorder();
|
||||
break;
|
||||
default:
|
||||
_abstractStrategy = null;
|
||||
break;
|
||||
}
|
||||
if (_strategy == null)
|
||||
{
|
||||
;
|
||||
if (_abstractStrategy == null) {
|
||||
return;
|
||||
}
|
||||
ComboBoxStrategy.setEnabled(false);
|
||||
_strategy.MakeStep();
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
ComboBoxStrategy.setEnabled(true);
|
||||
_strategy = null;
|
||||
}
|
||||
_abstractStrategy.SetData(new MoveableArmoredCar(_drawningArmoredCar), canvas.getWidth(), canvas.getHeight());
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
});
|
||||
if (_abstractStrategy == null) {
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
if (_abstractStrategy.GetStatus() == StrategyStatus.Finish) {
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
canvas.repaint();
|
||||
}
|
||||
};
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if (canvasAntiAircraftGun._drawingArmoredCar == null) return;
|
||||
boolean result = false;
|
||||
switch ((((JButton)(event.getSource())).getName())) {
|
||||
case "UP":
|
||||
result = canvasAntiAircraftGun._drawingArmoredCar.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "DOWN":
|
||||
result = canvasAntiAircraftGun._drawingArmoredCar.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "LEFT":
|
||||
result = canvasAntiAircraftGun._drawingArmoredCar.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "RIGHT":
|
||||
result = canvasAntiAircraftGun._drawingArmoredCar.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result) {
|
||||
canvasAntiAircraftGun.repaint();
|
||||
}
|
||||
ActionListener buttonsMoveListener = new ActionListener() {
|
||||
// реакция на нажатие
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_drawningArmoredCar == null) {
|
||||
return;
|
||||
}
|
||||
};
|
||||
UpButton.addActionListener(actionListener);
|
||||
DownButton.addActionListener(actionListener);
|
||||
LeftButton.addActionListener(actionListener);
|
||||
RightButton.addActionListener(actionListener);
|
||||
String command = ((JButton) (e.getSource())).getName();
|
||||
switch (command) {
|
||||
case "up":
|
||||
_drawningArmoredCar.MoveTransport(MovementDirection.Up);
|
||||
break;
|
||||
case "down":
|
||||
_drawningArmoredCar.MoveTransport(MovementDirection.Down);
|
||||
break;
|
||||
case "right":
|
||||
_drawningArmoredCar.MoveTransport(MovementDirection.Right);
|
||||
break;
|
||||
case "left":
|
||||
_drawningArmoredCar.MoveTransport(MovementDirection.Left);
|
||||
break;
|
||||
}
|
||||
canvas.repaint();
|
||||
}
|
||||
};
|
||||
|
||||
setSize(dimension.width,dimension.height);
|
||||
setLayout(null);
|
||||
canvasAntiAircraftGun.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 125, 40);
|
||||
CreateArmoredCarButton.setBounds(160, getHeight() - 90, 125, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
|
||||
RightButton.setBounds(getWidth() - 80, 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(CreateArmoredCarButton);
|
||||
add(UpButton);
|
||||
add(DownButton);
|
||||
add(RightButton);
|
||||
add(LeftButton);
|
||||
add(ComboBoxStrategy);
|
||||
add(ButtonStrategy);
|
||||
add(canvasAntiAircraftGun);
|
||||
setVisible(true);
|
||||
//обработка события изменения размеров окна
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
public void componentResized(ComponentEvent e) {
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
if (canvasAntiAircraftGun._drawingArmoredCar != null)
|
||||
canvasAntiAircraftGun._drawingArmoredCar.SetPictureSize(Width, Height);
|
||||
canvasAntiAircraftGun.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 100, 40);
|
||||
UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50);
|
||||
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50);
|
||||
RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50);
|
||||
LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50);
|
||||
class Canvas extends JComponent {
|
||||
public Canvas() {
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
if (_drawningArmoredCar == null) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_drawningArmoredCar.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ import java.awt.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FormAntiAircraftGun form = new FormAntiAircraftGun("Зенитная установка", new Dimension(600, 600));
|
||||
form.Init();
|
||||
new FormAntiAircraftGun();
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ public abstract class AbstractStrategy {
|
||||
public int FieldWidth;
|
||||
public int FieldHeight;
|
||||
public StrategyStatus GetStatus() {return _state;}
|
||||
//изменить статус, установить поля
|
||||
public void SetData(IMoveableObject moveableObjects, int width, int height)
|
||||
{
|
||||
if (moveableObjects == null)
|
||||
@ -18,6 +19,7 @@ public abstract class AbstractStrategy {
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
// сделать шаг
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress) return;
|
||||
@ -28,11 +30,14 @@ public abstract class AbstractStrategy {
|
||||
}
|
||||
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 Integer GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
@ -41,16 +46,22 @@ public abstract class AbstractStrategy {
|
||||
}
|
||||
return _moveableObject.GetStep();
|
||||
}
|
||||
// перемещение
|
||||
protected abstract void MoveToTarget();
|
||||
// достигнута ли цель
|
||||
protected abstract boolean IsTargetDestinaion();
|
||||
private boolean MoveTo(MovementDirection movementDirection)
|
||||
// попытка перемещения по направлению
|
||||
private boolean MoveTo(MovementDirection directionType)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
boolean stateTryMoveObject = _moveableObject.TryMoveObject(movementDirection);
|
||||
if (stateTryMoveObject) return stateTryMoveObject;
|
||||
if (_moveableObject.CheckCanMove(directionType))
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -3,5 +3,6 @@ package MovementStrategy;
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean TryMoveObject(MovementDirection direction);
|
||||
boolean CheckCanMove(MovementDirection direction);
|
||||
void MoveObject(MovementDirection direction);
|
||||
}
|
||||
|
@ -3,25 +3,38 @@ package MovementStrategy;
|
||||
public class MoveToBorder extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestinaion() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder + GetStep() >= FieldWidth-GetStep() &&
|
||||
objParams.DownBorder + GetStep() >= FieldHeight-GetStep();
|
||||
return objParams.RightBorder() + GetStep() >= FieldWidth && objParams.DownBorder() + GetStep() >= FieldHeight;
|
||||
}
|
||||
// движение к цели
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//реализация в правый нижний угол
|
||||
int x = objParams.RightBorder;
|
||||
if (x + GetStep() < FieldWidth) MoveRight();
|
||||
int y = objParams.DownBorder;
|
||||
if (y + GetStep() < FieldHeight) MoveDown();
|
||||
var diffX = objParams.RightBorder() - FieldWidth;
|
||||
var diffY = objParams.DownBorder() - FieldHeight;
|
||||
if (diffX >= 0)
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
else if (diffY >= 0)
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
else if (Math.abs(diffX) > Math.abs(diffY))
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,15 +3,15 @@ package MovementStrategy;
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestinaion() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
var 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;
|
||||
return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical() <= FieldHeight / 2 &&
|
||||
objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
@ -20,7 +20,7 @@ public class MoveToCenter extends AbstractStrategy{
|
||||
{
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
|
||||
int diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2;
|
||||
if (Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
@ -32,7 +32,7 @@ public class MoveToCenter extends AbstractStrategy{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
int diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
|
@ -1,44 +1,29 @@
|
||||
package MovementStrategy;
|
||||
import Drawnings.CanvasAntiAircraftGun;
|
||||
import Drawnings.DirectionType;
|
||||
import Drawnings.DrawningAntiAircraftGun;
|
||||
import Drawnings.DrawningArmoredCar;
|
||||
public class MoveableArmoredCar implements IMoveableObject{
|
||||
private CanvasAntiAircraftGun canvas = new CanvasAntiAircraftGun();
|
||||
public MoveableArmoredCar(DrawningArmoredCar drawningairplan)
|
||||
private DrawningArmoredCar _drawningArmoredCar = null;
|
||||
public MoveableArmoredCar(DrawningArmoredCar drawningArmoredCar)
|
||||
{
|
||||
canvas._drawingArmoredCar = drawningairplan;
|
||||
_drawningArmoredCar = drawningArmoredCar;
|
||||
}
|
||||
@Override
|
||||
public ObjectParameters GetObjectPosition() {
|
||||
if (canvas._drawingArmoredCar == null || canvas._drawingArmoredCar.EntityArmoredCar == null ||
|
||||
canvas._drawingArmoredCar.GetPosX() == null || canvas._drawingArmoredCar.GetPosY() == null)
|
||||
public ObjectParameters GetObjectPosition()
|
||||
{
|
||||
if (_drawningArmoredCar == null || _drawningArmoredCar.entityArmoredCar == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(canvas._drawingArmoredCar.GetPosX(), canvas._drawingArmoredCar.GetPosY(),
|
||||
canvas._drawingArmoredCar.GetWidth(), canvas._drawingArmoredCar.GetHeight());
|
||||
return new ObjectParameters(_drawningArmoredCar.GetPosX(), _drawningArmoredCar.GetPosY(), _drawningArmoredCar.GetWidth(), _drawningArmoredCar.GetHeight());
|
||||
}
|
||||
|
||||
public int GetStep() { return (int)_drawningArmoredCar.entityArmoredCar.Step; }
|
||||
|
||||
@Override
|
||||
public int GetStep() {
|
||||
return (int)(canvas._drawingArmoredCar.EntityArmoredCar.Step);
|
||||
public boolean CheckCanMove(MovementDirection direction) {
|
||||
return _drawningArmoredCar.CanMove(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean TryMoveObject(MovementDirection direction) {
|
||||
if (canvas._drawingArmoredCar == null || canvas._drawingArmoredCar.EntityArmoredCar == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return canvas._drawingArmoredCar.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.Unknown;
|
||||
}
|
||||
public void MoveObject(MovementDirection direction) {
|
||||
_drawningArmoredCar.MoveTransport(direction);
|
||||
}
|
||||
}
|
||||
|
@ -5,23 +5,21 @@ public class ObjectParameters {
|
||||
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 int LeftBorder() { return _x; }
|
||||
public int TopBorder() { return _y; }
|
||||
public int RightBorder() { return _x + _width; }
|
||||
public int DownBorder() { return _y + _height; }
|
||||
|
||||
|
||||
public int ObjectMiddleHorizontal () { return _x + _width / 2; }
|
||||
public int ObjectMiddleVertical () { return _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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user