PIBD-13_Fomichev_V.S._LabWork02_Hard #2

Closed
slavaxom9k wants to merge 6 commits from labwork02 into labwork01
25 changed files with 796 additions and 356 deletions

View File

@ -1,5 +0,0 @@
public enum CountRollers {
Four,
Five,
Six;
}

View File

@ -1,6 +0,0 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

View File

@ -1,153 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningAntiAircraftGun extends JPanel{
public EntityAntiAircraftGun EntityAntiAircraftGun;
public DrawningRollers _rollers = null;
private Integer picture_width;
private Integer picture_height;
private Integer _StartPosX;
private Integer _StartPosY;
private int drawningCarWidth = 120;
private int drawningCarHeight = 90;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean tower, boolean radar) {
EntityAntiAircraftGun = new EntityAntiAircraftGun();
EntityAntiAircraftGun.Init(speed, weight, bodycolor, additionalcolor, tower, radar);
picture_width = null;
picture_height = null;
_StartPosX = null;
_StartPosY = null;
_rollers = new DrawningRollers();
Random rand = new Random();
int randomNum = rand.nextInt(4);
_rollers.SetCount(randomNum);
}
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;
}
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 (EntityAntiAircraftGun == null || _StartPosX == null || _StartPosY == null) return false;
switch (direction) {
case Left:
if (_StartPosX - EntityAntiAircraftGun.Step > 0) {
_StartPosX -= (int)EntityAntiAircraftGun.Step;
}
return true;
case Up:
if (_StartPosY - EntityAntiAircraftGun.Step > 0)
{
_StartPosY -= (int)EntityAntiAircraftGun.Step;
}
return true;
case Right:
if (_StartPosX + drawningCarWidth + (int)EntityAntiAircraftGun.Step < picture_width - EntityAntiAircraftGun.Step)
{
_StartPosX += (int)EntityAntiAircraftGun.Step;
}
return true;
case Down:
if (_StartPosY + drawningCarHeight + (int)EntityAntiAircraftGun.Step < picture_height - EntityAntiAircraftGun.Step)
{
_StartPosY += (int)EntityAntiAircraftGun.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g) {
if (EntityAntiAircraftGun == 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(EntityAntiAircraftGun.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.setColor(Color.GRAY);
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(EntityAntiAircraftGun.getBodyColor());
g.fillRect(_StartPosX + 10, _StartPosY+ 65, 120, 13);
_rollers.Draw(g,_StartPosX, _StartPosY);
//отрисовки башни
if (EntityAntiAircraftGun.getIsTower())
{
g.setColor(EntityAntiAircraftGun.getAdditionalColor());
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);
}
//отрисовка радара
if (EntityAntiAircraftGun.getIsRadar())
{;
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);
}
}
}

View File

@ -1,52 +0,0 @@
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 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.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);
}
}
}

View File

@ -0,0 +1,14 @@
package DrawningRollers;
public enum CountRollers {
FourRollers(4),
FiveRollers(5),
SixRollers(6);
private int numberofrollers;
CountRollers(int numberofrollers) {
this.numberofrollers = numberofrollers;
}
public int getCountRollers() {
return numberofrollers;
}
}

View File

@ -0,0 +1,12 @@
package DrawningRollers;
import java.awt.*;
public class DrawningRollersCross extends DrawningRollersTriangle {
protected void drawRollers(Graphics2D g, int posX, int posY) {
g.setColor(Color.BLACK);
g.drawLine(posX+3,posY+3,posX+12,posY+12);
g.drawLine(posX+12,posY+3,posX+3,posY+12);
}
}

View File

@ -0,0 +1,13 @@
package DrawningRollers;
import java.awt.*;
public class DrawningRollersSquare extends DrawningRollersTriangle {
protected void drawRollers(Graphics2D g, int posX, int posY) {
g.setColor(Color.BLACK);
g.drawLine(posX+3,posY+3,posX+3,posY+12);
g.drawLine(posX+3,posY+12,posX+12,posY+12);
g.drawLine(posX+12,posY+12,posX+12,posY+3);
g.drawLine(posX+12,posY+3,posX+3,posY+3);
}
}

View File

@ -0,0 +1,69 @@
package DrawningRollers;
import java.awt.*;
public class DrawningRollersTriangle implements IDifferentRollers {
private CountRollers _rollers;
public CountRollers getCount() {
return _rollers;
}
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.BLACK);
g.drawLine(posX+7,posY,posX+3,posY+12);
g.drawLine(posX+3,posY+12,posX+12,posY+12);
g.drawLine(posX+12,posY+12,posX+7,posY);
}
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);
drawRollers(g,_startPosX + 40,_startPosY + 90);
drawRollers(g,_startPosX + 55,_startPosY + 90);
if (_rollers != CountRollers.FourRollers) {
g.drawOval(_startPosX + 70, _startPosY + 90, 15, 15);
drawRollers(g,_startPosX+70,_startPosY+90);
}
if (_rollers == CountRollers.SixRollers) {
g.drawOval(_startPosX + 85, _startPosY + 90, 15, 15);
drawRollers(g,_startPosX+85,_startPosY+90);
}
}
}

View File

@ -0,0 +1,9 @@
package DrawningRollers;
import java.awt.*;
public interface IDifferentRollers {
public CountRollers getCount();
public void SetCount (int count);
void Draw(Graphics2D g, int _startPosX, int _startPoxY);
}

View File

@ -1,15 +1,19 @@
package Drawnings;
import Drawnings.DrawningAntiAircraftGun;
import javax.swing.*;
import java.awt.*;
public class CanvasAntiAircraftGun extends JComponent {
public DrawningAntiAircraftGun _drawingAntiAircraftGun;
public DrawningArmoredCar _drawingArmoredCar;
public CanvasAntiAircraftGun(){}
public void paintComponent(Graphics g) {
if (_drawingAntiAircraftGun == null) {
if (_drawingArmoredCar == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawingAntiAircraftGun.DrawTransport(g2d);
_drawingArmoredCar.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,52 @@
package Drawnings;
import Entities.*;
import Entities.EntityArmoredCar;
import javax.swing.*;
import javax.swing.text.html.parser.Entity;
import java.awt.*;
import java.util.Random;
public class DrawningAntiAircraftGun extends DrawningArmoredCar{
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) {
return;
}
g.setColor(Color.BLACK);
super.DrawTransport(g);
Color additionalColor = ((EntityAntiAircraftGun) entityArmoredCar).getAdditionalColor();
//отрисовки башни
if (((EntityAntiAircraftGun) entityArmoredCar).IsTower())
{
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);
}
//отрисовка радара
if (((EntityAntiAircraftGun) entityArmoredCar).IsRadar())
{;
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);
}
}
}

View File

@ -0,0 +1,147 @@
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 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;
}
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();
switch (random.nextInt(0, 3)) {
case 0:
_rollers = new DrawningRollersTriangle();
break;
case 1:
_rollers = new DrawningRollersCross();
break;
case 2:
_rollers = new DrawningRollersSquare();
break;
default:
_rollers = new DrawningRollersTriangle();
break;
}
_rollers.SetCount(countRollers);
}
public boolean CanMove(MovementDirection direction) {
if (entityArmoredCar == null) {
return false;
}
switch (direction) {
case Left:
return _startPosX - entityArmoredCar.Step > 5;
case Right:
return _startPosX + _carWidth + entityArmoredCar.Step < _pictureWidth;
case Up:
return _startPosY - entityArmoredCar.Step > 0;
case Down:
return _startPosY + _carHeight + entityArmoredCar.Step < _pictureHeight;
default:
return false;
}
}
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;
}
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(Color.BLACK);
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.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);
_rollers.Draw(g,_startPosX, _startPosY);
}
}

View File

@ -0,0 +1,19 @@
package Entities;
import java.awt.*;
public class EntityAntiAircraftGun extends EntityArmoredCar{
private Color AdditionalColor;
private boolean IsTower;
private 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; }
}

View File

@ -0,0 +1,25 @@
package Entities;
import java.awt.*;
public class EntityArmoredCar {
private int Speed;
private float Weight;
private 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;
BodyColor = bodycolor;
Step = Speed * 100 / Weight;
}
}

View File

@ -1,24 +0,0 @@
import java.awt.*;
public class EntityAntiAircraftGun {
private int Speed;
private double Weight;
private Color BodyColor;
public Color getBodyColor() {return BodyColor;}
private Color AdditionalColor;
public Color getAdditionalColor() {return AdditionalColor;}
private boolean IsTower;
public boolean getIsTower() {return IsTower;}
private boolean IsRadar;
public boolean getIsRadar() {return IsRadar;}
public double Step;
public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean isTower, boolean isRadar)
{
Speed = speed;
Weight = weight;
BodyColor = bodycolor;
AdditionalColor = additionalcolor;
IsTower = isTower;
IsRadar = isRadar;
Step = Speed * 100 / Weight;
}
}

View File

@ -1,123 +1,208 @@
import Drawnings.DrawningAntiAircraftGun;
import Drawnings.DrawningArmoredCar;
import MovementStrategy.*;
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 UpButton = new JButton();
private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton();
public FormAntiAircraftGun(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init() {
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 15;
Height = getHeight() - 35;
private int width;
private int height;
private DrawningArmoredCar _drawningArmoredCar;
private AbstractStrategy _abstractStrategy;
private Canvas canvas;
// выбор стратегии
JLabel labelStrategy;
JComboBox comboBoxStrategy;
JButton buttonStrategy;
CreateButton.setName("CREATE");
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);
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
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));
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));;
boolean tower = new Random().nextBoolean();
boolean radar = new Random().nextBoolean();;
canvasAntiAircraftGun._drawingAntiAircraftGun = new DrawningAntiAircraftGun();
canvasAntiAircraftGun._drawingAntiAircraftGun.Init(speed, weight, bodyColor, additionalColor, tower, radar);
canvasAntiAircraftGun._drawingAntiAircraftGun.SetPictureSize(Width, Height);
canvasAntiAircraftGun._drawingAntiAircraftGun.SetPosition( StartPositionX, StartPositionY);
canvasAntiAircraftGun.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasAntiAircraftGun._drawingAntiAircraftGun == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "UP":
result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Up);
break;
case "DOWN":
result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Down);
break;
case "LEFT":
result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Left);
break;
case "RIGHT":
result = canvasAntiAircraftGun._drawingAntiAircraftGun.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasAntiAircraftGun.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
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);
add(CreateButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(canvasAntiAircraftGun);
private JButton buttonCreateAntiAircraftGun;
private JButton buttonCreateArmoredCar;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonLeft;
public FormAntiAircraftGun() {
super("Создание зенитной установки");
InitializeComponent();
setVisible(true);
//обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasAntiAircraftGun._drawingAntiAircraftGun != null)
canvasAntiAircraftGun._drawingAntiAircraftGun.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);
}
private void InitializeComponent() {
canvas = new Canvas();
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, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
buttonCreateAntiAircraftGun.setBounds(12, 500, 146, 33);
buttonCreateArmoredCar.setBounds(182, 500, 146, 33);
labelStrategy.setBounds(630, 20, 146, 33);
comboBoxStrategy.setBounds(630, 50, 146, 20);
buttonStrategy.setBounds(630, 80, 146, 33);
buttonUp.setBounds(679, 413, 48, 44);
buttonRight.setBounds(728, 458, 48, 44);
buttonLeft.setBounds(630, 458, 48, 44);
buttonDown.setBounds(679, 458, 48, 44);
canvas.setBounds(0, 0, 790, 460);
add(buttonCreateAntiAircraftGun);
add(buttonCreateArmoredCar);
add(labelStrategy);
add(comboBoxStrategy);
add(buttonStrategy);
add(buttonUp);
add(buttonRight);
add(buttonDown);
add(buttonLeft);
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) {
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)),
rand.nextInt(4,7),
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) {
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)),
rand.nextInt(4,7),
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 (_abstractStrategy == null) {
return;
}
_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 buttonsMoveListener = new ActionListener() {
// реакция на нажатие
public void actionPerformed(ActionEvent e) {
if (_drawningArmoredCar == null) {
return;
}
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();
}
};
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();
}
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,67 @@
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 moveableObjects, int width, int height)
{
if (moveableObjects == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObjects;
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 Integer GetStep()
{
if (_state != StrategyStatus.InProgress)
{
return null;
}
return _moveableObject.GetStep();
}
// перемещение
protected abstract void MoveToTarget();
// достигнута ли цель
protected abstract boolean IsTargetDestinaion();
// попытка перемещения по направлению
private boolean MoveTo(MovementDirection directionType)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
if (_moveableObject.CheckCanMove(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,8 @@
package MovementStrategy;
public interface IMoveableObject {
ObjectParameters GetObjectPosition();
int GetStep();
boolean CheckCanMove(MovementDirection direction);
void MoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,40 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion() {
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.RightBorder() + GetStep() >= FieldWidth && objParams.DownBorder() + GetStep() >= FieldHeight;
}
// движение к цели
@Override
protected void MoveToTarget() {
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
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();
}
}
}

View File

@ -0,0 +1,48 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion() {
var objParams = GetObjectParameters();
if (objParams == null)
{
return false;
}
return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 &&
objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 &&
objParams.ObjectMiddleVertical() <= 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,29 @@
package MovementStrategy;
import Drawnings.DrawningArmoredCar;
public class MoveableArmoredCar implements IMoveableObject{
private DrawningArmoredCar _drawningArmoredCar = null;
public MoveableArmoredCar(DrawningArmoredCar drawningArmoredCar)
{
_drawningArmoredCar = drawningArmoredCar;
}
public ObjectParameters GetObjectPosition()
{
if (_drawningArmoredCar == null || _drawningArmoredCar.entityArmoredCar == null)
{
return null;
}
return new ObjectParameters(_drawningArmoredCar.GetPosX(), _drawningArmoredCar.GetPosY(), _drawningArmoredCar.GetWidth(), _drawningArmoredCar.GetHeight());
}
public int GetStep() { return (int)_drawningArmoredCar.entityArmoredCar.Step; }
@Override
public boolean CheckCanMove(MovementDirection direction) {
return _drawningArmoredCar.CanMove(direction);
}
@Override
public void MoveObject(MovementDirection direction) {
_drawningArmoredCar.MoveTransport(direction);
}
}

View File

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

View File

@ -0,0 +1,25 @@
package MovementStrategy;
public class ObjectParameters {
private int _x;
private int _y;
private int _width;
private int _height;
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;
}
}

View File

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