Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
48bd006597 | ||
|
82bad5c255 | ||
|
5f914ddb22 | ||
|
f5247f6ce8 | ||
|
19b99066a2 | ||
|
88a324c1b6 | ||
|
435ed2cad9 | ||
|
88f057c2fa |
7
src/DirectionType.java
Normal file
7
src/DirectionType.java
Normal file
@ -0,0 +1,7 @@
|
||||
package src;
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
199
src/DrawningObjects/DrawningAirplane.java
Normal file
199
src/DrawningObjects/DrawningAirplane.java
Normal file
@ -0,0 +1,199 @@
|
||||
package src.DrawningObjects;
|
||||
import src.DirectionType;
|
||||
import src.Entities.EntityAirplane;
|
||||
import src.MovementStrategy.DrawningObjectAirplane;
|
||||
import src.MovementStrategy.IMoveableObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
public class DrawningAirplane {
|
||||
protected EntityAirplane EntityAirplane;
|
||||
private int _pictureWidth;
|
||||
private int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected IDraw DrawningIlluminators;
|
||||
private int _airplaneWidth = 200;
|
||||
private int _airplaneHeight = 78;
|
||||
public EntityAirplane EntityAirplane(){
|
||||
return EntityAirplane;
|
||||
}
|
||||
public DrawningAirplane(int speed, double weight, Color bodyColor, int width, int height){
|
||||
if(width <= _airplaneWidth || height <= _airplaneHeight)
|
||||
return;
|
||||
_startPosY=0;
|
||||
_startPosX = 0;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
|
||||
DrawningIlluminators = new DrawningIlluminators(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
Random rand = new Random();
|
||||
int variant = rand.nextInt(0,3);
|
||||
if(variant ==0){
|
||||
DrawningIlluminators = new DrawningIlluminators(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
} else{
|
||||
if(variant ==1){
|
||||
DrawningIlluminators = new DrawningIlluminatorsQuad(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
}
|
||||
else if(variant==2){
|
||||
DrawningIlluminators = new DrawningIlluminatorsCirc(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
}}
|
||||
DrawningIlluminators.ChangeIlluminatorNumb(rand.nextInt(1, 5));
|
||||
}
|
||||
protected DrawningAirplane(int speed, double weight, Color bodyColor, int width, int height,int airplaneWidth, int airplaneHeight){
|
||||
if(width <= _airplaneWidth || height <= _airplaneHeight)
|
||||
return;
|
||||
_startPosY=0;
|
||||
_startPosX = 0;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
_airplaneWidth = airplaneWidth;
|
||||
_airplaneHeight = airplaneHeight;
|
||||
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
|
||||
DrawningIlluminators = new DrawningIlluminators(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
Random rand = new Random();
|
||||
int variant = rand.nextInt(0,3);
|
||||
if(variant ==0){
|
||||
DrawningIlluminators = new DrawningIlluminators(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
} else{
|
||||
if(variant ==1){
|
||||
DrawningIlluminators = new DrawningIlluminatorsQuad(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
} else if(variant==2){
|
||||
DrawningIlluminators = new DrawningIlluminatorsCirc(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
}
|
||||
}
|
||||
DrawningIlluminators.ChangeIlluminatorNumb(rand.nextInt(1, 5));
|
||||
}
|
||||
public void SetPosition(int x, int y){
|
||||
if(EntityAirplane == null)
|
||||
return;
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
DrawningIlluminators.ChangeX(_startPosX);
|
||||
DrawningIlluminators.ChangeY(_startPosY);
|
||||
if (x < 0 || y < 0 || x + _airplaneWidth >= _pictureWidth || y + _airplaneHeight >= _pictureHeight) {
|
||||
x = y = 10;
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
}
|
||||
public IMoveableObject GetMoveableObject(){
|
||||
return new DrawningObjectAirplane(this);
|
||||
}
|
||||
public int GetPosX(){return _startPosX;}
|
||||
public int GetPosY(){return _startPosY;}
|
||||
public int GetWidth(){return _airplaneWidth;}
|
||||
public int GetHeight(){return _airplaneHeight;}
|
||||
public boolean CanMove(DirectionType direction)
|
||||
{
|
||||
if (EntityAirplane == null)
|
||||
return false;
|
||||
boolean can = false;
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
can = _startPosX - EntityAirplane.Step() >= 0;
|
||||
break;
|
||||
case Right:
|
||||
can = _startPosX + EntityAirplane.Step() + _airplaneWidth< _pictureWidth;
|
||||
break;
|
||||
case Down:
|
||||
can = _startPosY + EntityAirplane.Step() + _airplaneHeight < _pictureHeight;
|
||||
break;
|
||||
case Up:
|
||||
can = _startPosY - EntityAirplane.Step() >= 0;
|
||||
break;
|
||||
};
|
||||
return can;
|
||||
}
|
||||
public void MoveTransport(DirectionType direction){
|
||||
if (!CanMove(direction) || EntityAirplane == null)
|
||||
return;
|
||||
switch (direction)
|
||||
{
|
||||
case Left:
|
||||
if (_startPosX - EntityAirplane.Step() >= 0)
|
||||
_startPosX -= (int)EntityAirplane.Step();
|
||||
else
|
||||
_startPosX = 0;
|
||||
break;
|
||||
case Up:
|
||||
if (_startPosY - EntityAirplane.Step() >= 0)
|
||||
_startPosY -= (int)EntityAirplane.Step();
|
||||
else
|
||||
_startPosY = 0;
|
||||
break;
|
||||
case Right:
|
||||
if (_startPosX + EntityAirplane.Step() + _airplaneWidth < _pictureWidth)
|
||||
_startPosX += (int)EntityAirplane.Step();
|
||||
else
|
||||
_startPosX = _pictureWidth - _airplaneWidth;
|
||||
break;
|
||||
case Down:
|
||||
if (_startPosY + EntityAirplane.Step() + _airplaneHeight < _pictureHeight)
|
||||
_startPosY += (int)EntityAirplane.Step();
|
||||
else
|
||||
_startPosY = _pictureHeight - _airplaneHeight;
|
||||
break;
|
||||
}
|
||||
DrawningIlluminators.ChangeX(_startPosX);
|
||||
DrawningIlluminators.ChangeY(_startPosY);
|
||||
}
|
||||
public void DrawAirplane(Graphics2D g2d){
|
||||
if (EntityAirplane == null)
|
||||
return;
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.setStroke(new BasicStroke(3));
|
||||
// корпус
|
||||
g2d.setColor(EntityAirplane.BodyColor());
|
||||
g2d.fillOval(_startPosX, _startPosY + 25, 180, 30);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(_startPosX, _startPosY + 25, 180, 30);
|
||||
// крыло
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.fillOval(_startPosX + 70, _startPosY + 35, 80, 10);
|
||||
// стекла
|
||||
g2d.setColor(Color.BLUE);
|
||||
int[] curvePointsX =
|
||||
{
|
||||
_startPosX + 170,
|
||||
_startPosX + 200,
|
||||
_startPosX + 170,
|
||||
};
|
||||
int[] curvePointsY =
|
||||
{
|
||||
_startPosY + 30,
|
||||
_startPosY + 40,
|
||||
_startPosY + 50,
|
||||
};
|
||||
g2d.fillPolygon(curvePointsX,curvePointsY,curvePointsY.length);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawPolygon(curvePointsX,curvePointsY,curvePointsY.length);
|
||||
g2d.drawLine(_startPosX + 170, _startPosY + 40, _startPosX + 200, _startPosY + 40);
|
||||
// хвост
|
||||
g2d.setColor(Color.BLUE);
|
||||
int[] curvePoints2x =
|
||||
{
|
||||
_startPosX,
|
||||
_startPosX,
|
||||
_startPosX + 30,
|
||||
};
|
||||
int[] curvePoints2y =
|
||||
{
|
||||
_startPosY + 35,
|
||||
_startPosY + 5,
|
||||
_startPosY + 35,
|
||||
};
|
||||
g2d.fillPolygon(curvePoints2x,curvePoints2y, curvePoints2x.length);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawPolygon(curvePoints2x,curvePoints2y, curvePoints2x.length);
|
||||
// шасси
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawLine(_startPosX + 50, _startPosY + 55, _startPosX + 50, _startPosY + 70);
|
||||
g2d.drawLine(_startPosX + 150, _startPosY + 51, _startPosX + 150, _startPosY + 70);
|
||||
g2d.fillOval(_startPosX + 40, _startPosY + 65, 10, 10);
|
||||
g2d.fillOval(_startPosX + 50, _startPosY + 65, 10, 10);
|
||||
g2d.fillOval(_startPosX + 145, _startPosY + 65, 10, 10);
|
||||
DrawningIlluminators.DrawIlluminators(g2d);
|
||||
}
|
||||
}
|
54
src/DrawningObjects/DrawningAirplaneWithRadar.java
Normal file
54
src/DrawningObjects/DrawningAirplaneWithRadar.java
Normal file
@ -0,0 +1,54 @@
|
||||
package src.DrawningObjects;
|
||||
import src.Entities.EntityAirplane;
|
||||
import src.Entities.EntityAirplaneWithRadar;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
public class DrawningAirplaneWithRadar extends DrawningAirplane {
|
||||
public DrawningAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
boolean radar, boolean dopBak,int width,int height ){
|
||||
super(speed,weight,bodyColor,width,height);
|
||||
if(EntityAirplane!=null){
|
||||
EntityAirplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, additionalColor, radar,dopBak);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void DrawAirplane(Graphics2D g2d){
|
||||
if(!(EntityAirplane instanceof EntityAirplaneWithRadar)){
|
||||
return;
|
||||
}
|
||||
super.DrawAirplane(g2d);
|
||||
EntityAirplaneWithRadar _airplaneWithRadar =(EntityAirplaneWithRadar) EntityAirplane;
|
||||
|
||||
//дополнительный бак
|
||||
if (_airplaneWithRadar.DopBak()) {
|
||||
g2d.setColor(_airplaneWithRadar.AdditionalColor());
|
||||
g2d.fillOval( _startPosX, _startPosY + 45, 40, 20);
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawOval(_startPosX, _startPosY + 45, 40, 20);
|
||||
}
|
||||
//радар
|
||||
if (_airplaneWithRadar.Radar())
|
||||
{
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawLine(_startPosX + 60, _startPosY + 25, _startPosX + 60, _startPosY + 15);
|
||||
g2d.drawLine( _startPosX + 60, _startPosY + 15, _startPosX + 67, _startPosY + 11);
|
||||
g2d.setColor(_airplaneWithRadar.AdditionalColor());
|
||||
Point point7 = new Point(_startPosX + 60, _startPosY + 15);
|
||||
Point point8 = new Point(_startPosX + 60, _startPosY + 5);
|
||||
Point point9 = new Point(_startPosX + 70, _startPosY + 25);
|
||||
int[] curvePoints3x =
|
||||
{
|
||||
_startPosX + 60,
|
||||
_startPosX + 60,
|
||||
_startPosX + 70,
|
||||
};
|
||||
int[] curvePoints3y =
|
||||
{
|
||||
_startPosY + 15,
|
||||
_startPosY + 5,
|
||||
_startPosY + 25,
|
||||
};
|
||||
g2d.fillPolygon(curvePoints3x, curvePoints3y,curvePoints3x.length);
|
||||
}
|
||||
}
|
||||
}
|
62
src/DrawningObjects/DrawningIlluminators.java
Normal file
62
src/DrawningObjects/DrawningIlluminators.java
Normal file
@ -0,0 +1,62 @@
|
||||
package src.DrawningObjects;
|
||||
|
||||
import src.NumberType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
public class DrawningIlluminators implements IDraw{
|
||||
private NumberType IlluminatorNumb;
|
||||
private Color IlluminatorColor;
|
||||
private int Width, Height;
|
||||
protected int CurX, CurY;
|
||||
public DrawningIlluminators(int width, int height, int curX, int curY){
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
}
|
||||
public void ChangeX(int x){
|
||||
CurX = x;
|
||||
}
|
||||
public void ChangeY(int y){
|
||||
CurY = y;
|
||||
}
|
||||
public void ChangeIlluminatorNumb(int x){
|
||||
if(x <= 2)
|
||||
IlluminatorNumb = NumberType.Ten;
|
||||
if(x == 3)
|
||||
IlluminatorNumb = NumberType.Twenty;
|
||||
if(x >= 4)
|
||||
IlluminatorNumb = NumberType.Thirty;
|
||||
}
|
||||
public NumberType IlluminatorNumb(){
|
||||
return IlluminatorNumb;
|
||||
}
|
||||
public void DrawIlluminators(Graphics2D g2d){
|
||||
g2d.setColor(Color.BLUE);
|
||||
int x = CurX;
|
||||
int y = CurY;
|
||||
for( int i =0; i<10;i++){
|
||||
g2d.fillOval( x+34 , y+29 , 5, 5);
|
||||
x+=7;
|
||||
}
|
||||
//20 иллюминаторов
|
||||
if (IlluminatorNumb == NumberType.Twenty || IlluminatorNumb == NumberType.Thirty)
|
||||
{
|
||||
x = CurX;
|
||||
for( int i =0; i<10;i++){
|
||||
g2d.fillOval( x+34 , y+37 , 5, 5);
|
||||
x+=7;
|
||||
}
|
||||
}
|
||||
//30 иллюминаторов
|
||||
if (IlluminatorNumb == NumberType.Thirty)
|
||||
{
|
||||
x = CurX;
|
||||
for( int i =0; i<10;i++){
|
||||
g2d.fillOval( x+34 , y+45 , 5, 5);
|
||||
x+=7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
61
src/DrawningObjects/DrawningIlluminatorsCirc.java
Normal file
61
src/DrawningObjects/DrawningIlluminatorsCirc.java
Normal file
@ -0,0 +1,61 @@
|
||||
package src.DrawningObjects;
|
||||
import src.NumberType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
public class DrawningIlluminatorsCirc implements IDraw {
|
||||
private NumberType IlluminatorNumb;
|
||||
private Color IlluminatorColor;
|
||||
private int Width, Height;
|
||||
protected int CurX, CurY;
|
||||
public DrawningIlluminatorsCirc(int width, int height, int curX, int curY){
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
}
|
||||
public void ChangeX(int x){
|
||||
CurX = x;
|
||||
}
|
||||
public void ChangeY(int y){
|
||||
CurY = y;
|
||||
}
|
||||
public void ChangeIlluminatorNumb(int x){
|
||||
if(x <= 2)
|
||||
IlluminatorNumb = NumberType.Ten;
|
||||
if(x == 3)
|
||||
IlluminatorNumb = NumberType.Twenty;
|
||||
if(x >= 4)
|
||||
IlluminatorNumb = NumberType.Thirty;
|
||||
}
|
||||
public NumberType IlluminatorNumb(){
|
||||
return IlluminatorNumb;
|
||||
}
|
||||
public void DrawIlluminators(Graphics2D g2d){
|
||||
g2d.setColor(Color.BLUE);
|
||||
int x = CurX;
|
||||
int y = CurY;
|
||||
for( int i =0; i<10;i++){
|
||||
g2d.drawOval( x+34 , y+29 , 5, 5);
|
||||
x+=7;
|
||||
}
|
||||
//20 иллюминаторов
|
||||
if (IlluminatorNumb == NumberType.Twenty || IlluminatorNumb == NumberType.Thirty)
|
||||
{
|
||||
x = CurX;
|
||||
for( int i =0; i<10;i++){
|
||||
g2d.drawOval( x+34 , y+37 , 5, 5);
|
||||
x+=7;
|
||||
}
|
||||
}
|
||||
//30 иллюминаторов
|
||||
if (IlluminatorNumb == NumberType.Thirty)
|
||||
{
|
||||
x = CurX;
|
||||
for( int i =0; i<10;i++){
|
||||
g2d.drawOval( x+34 , y+45 , 5, 5);
|
||||
x+=7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
61
src/DrawningObjects/DrawningIlluminatorsQuad.java
Normal file
61
src/DrawningObjects/DrawningIlluminatorsQuad.java
Normal file
@ -0,0 +1,61 @@
|
||||
package src.DrawningObjects;
|
||||
|
||||
import src.NumberType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningIlluminatorsQuad implements IDraw {
|
||||
private NumberType IlluminatorNumb;
|
||||
private Color IlluminatorColor;
|
||||
private int Width, Height;
|
||||
protected int CurX, CurY;
|
||||
public DrawningIlluminatorsQuad(int width, int height, int curX, int curY){
|
||||
Width = width;
|
||||
Height = height;
|
||||
CurX = curX;
|
||||
CurY = curY;
|
||||
}
|
||||
public void ChangeIlluminatorNumb(int x){
|
||||
if(x <= 2)
|
||||
IlluminatorNumb = NumberType.Ten;
|
||||
if(x == 3)
|
||||
IlluminatorNumb = NumberType.Twenty;
|
||||
if(x >= 4)
|
||||
IlluminatorNumb = NumberType.Thirty;
|
||||
}
|
||||
public void ChangeX(int x){
|
||||
CurX = x;
|
||||
}
|
||||
public void ChangeY(int y){
|
||||
CurY = y;
|
||||
}
|
||||
public NumberType IlluminatorNumb(){
|
||||
return IlluminatorNumb;
|
||||
}
|
||||
public void DrawIlluminators(Graphics2D g2d) {
|
||||
g2d.setColor(Color.BLUE);
|
||||
int x = CurX;
|
||||
int y = CurY;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
g2d.fillRect(x + 34, y + 29, 5, 5);
|
||||
x += 7;
|
||||
}
|
||||
//20 иллюминаторов
|
||||
if (IlluminatorNumb == NumberType.Twenty || IlluminatorNumb == NumberType.Thirty) {
|
||||
x = CurX;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
g2d.fillRect(x + 34, y + 37, 5, 5);
|
||||
x += 7;
|
||||
}
|
||||
}
|
||||
//30 иллюминаторов
|
||||
if (IlluminatorNumb == NumberType.Thirty) {
|
||||
x = CurX;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
g2d.fillRect(x + 34, y + 45, 5, 5);
|
||||
x += 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
src/DrawningObjects/IDraw.java
Normal file
11
src/DrawningObjects/IDraw.java
Normal file
@ -0,0 +1,11 @@
|
||||
package src.DrawningObjects;
|
||||
import src.NumberType;
|
||||
import java.awt.*;
|
||||
public interface IDraw {
|
||||
public void ChangeIlluminatorNumb(int x);
|
||||
//public NumberType IlluminatorNumb();
|
||||
public void DrawIlluminators(Graphics2D g2d);
|
||||
//public void DrawIlluminatorsQuad(Graphics2D g2d);
|
||||
public void ChangeX(int x);
|
||||
public void ChangeY(int y);
|
||||
}
|
25
src/Entities/EntityAirplane.java
Normal file
25
src/Entities/EntityAirplane.java
Normal file
@ -0,0 +1,25 @@
|
||||
package src.Entities;
|
||||
import java.awt.*;
|
||||
public class EntityAirplane {
|
||||
private int Speed;
|
||||
private double Weight, Step;
|
||||
private Color BodyColor;
|
||||
public int Speed(){
|
||||
return Speed;
|
||||
}
|
||||
public double Weight(){
|
||||
return Weight;
|
||||
}
|
||||
public double Step(){
|
||||
return Step;
|
||||
}
|
||||
public Color BodyColor(){
|
||||
return BodyColor;
|
||||
}
|
||||
public EntityAirplane(int speed, double weight, Color bodyColor){
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
Step = (double)Speed * 100 / Weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
18
src/Entities/EntityAirplaneWithRadar.java
Normal file
18
src/Entities/EntityAirplaneWithRadar.java
Normal file
@ -0,0 +1,18 @@
|
||||
package src.Entities;
|
||||
import java.awt.*;
|
||||
public class EntityAirplaneWithRadar extends EntityAirplane {
|
||||
private Color AdditionalColor;
|
||||
private boolean Radar;
|
||||
private boolean DopBak;
|
||||
public Color AdditionalColor(){return AdditionalColor;}
|
||||
public boolean Radar(){return Radar;}
|
||||
public boolean DopBak(){return DopBak;}
|
||||
|
||||
public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor,
|
||||
boolean radar, boolean dopBak ){
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor=additionalColor;
|
||||
Radar =radar;
|
||||
DopBak=dopBak;
|
||||
}
|
||||
}
|
122
src/FormAirplaneCollection.java
Normal file
122
src/FormAirplaneCollection.java
Normal file
@ -0,0 +1,122 @@
|
||||
package src;
|
||||
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.Generics.AirplaneGenericCollection;
|
||||
import src.MovementStrategy.DrawningObjectAirplane;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
|
||||
public class FormAirplaneCollection {
|
||||
private final AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes;
|
||||
private int pictureBoxWidth = 630;
|
||||
private int pictureBoxHeight = 426;
|
||||
CollectionCanvas canv;
|
||||
void Draw(){
|
||||
if(canv == null)
|
||||
return;
|
||||
canv.repaint();
|
||||
}
|
||||
public FormAirplaneCollection(){
|
||||
_airplanes = new AirplaneGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
|
||||
canv = new CollectionCanvas();
|
||||
JPanel toolBox = new JPanel();
|
||||
JFrame collectionFrame = new JFrame();
|
||||
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
collectionFrame.setSize(880,497);
|
||||
toolBox.setBounds(623, 12, 227, 426);
|
||||
canv.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
|
||||
JButton addButton = new JButton("Добавить");
|
||||
JButton removeButton = new JButton("Удалить");
|
||||
JButton refreshButton = new JButton("Обновить");
|
||||
JTextField airplaneNumb = new JTextField();
|
||||
GridLayout lay = new GridLayout(4,1);
|
||||
toolBox.setLayout(lay);
|
||||
toolBox.add(addButton);
|
||||
toolBox.add(airplaneNumb);
|
||||
toolBox.add(removeButton);
|
||||
toolBox.add(refreshButton);
|
||||
collectionFrame.add(toolBox);
|
||||
collectionFrame.add(canv);
|
||||
collectionFrame.setVisible(true);
|
||||
|
||||
addButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(_airplanes == null) {
|
||||
return;
|
||||
}
|
||||
FormAirplaneWithRadar form = new FormAirplaneWithRadar();
|
||||
form.buttonSelect.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_airplanes.Insert(form.SelectedAirplane()) != -1)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
Draw();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
canv._airplanes = _airplanes;
|
||||
form.AirplaneFrame.dispose();
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
removeButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(_airplanes == null) {
|
||||
return;
|
||||
}
|
||||
String tmp = airplaneNumb.getText();
|
||||
int numb;
|
||||
|
||||
try{
|
||||
numb = Integer.parseInt(tmp);
|
||||
}
|
||||
catch(Exception ex){
|
||||
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
return;
|
||||
}
|
||||
_airplanes.Remove(numb);
|
||||
_airplanes.ShowAirplanes();
|
||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
|
||||
refreshButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(_airplanes == null) {
|
||||
return;
|
||||
}
|
||||
_airplanes.ShowAirplanes();
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class CollectionCanvas extends JComponent {
|
||||
public AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes;
|
||||
public CollectionCanvas(){
|
||||
}
|
||||
@Override
|
||||
public void paintComponent (Graphics g){
|
||||
if (_airplanes == null){
|
||||
return;
|
||||
}
|
||||
super.paintComponents (g) ;
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
g2d.drawImage(_airplanes.ShowAirplanes(), 0, 0, this);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
218
src/FormAirplaneWithRadar.java
Normal file
218
src/FormAirplaneWithRadar.java
Normal file
@ -0,0 +1,218 @@
|
||||
package src;
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.MovementStrategy.*;
|
||||
import src.DrawningObjects.DrawningAirplaneWithRadar;
|
||||
import org.w3c.dom.ranges.DocumentRange;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormAirplaneWithRadar {
|
||||
private DrawningAirplane DrawningAirplane;
|
||||
private AbstractStrategy _abstractStrategy;
|
||||
Canvas canv;
|
||||
static int pictureBoxWidth = 882;
|
||||
static int pictureBoxHeight = 453;
|
||||
public JButton buttonSelect;
|
||||
public JFrame AirplaneFrame;
|
||||
public void Draw(){
|
||||
canv.repaint();
|
||||
}
|
||||
public Color ChooseColor(JFrame MonorailFrame){
|
||||
JColorChooser dialog = new JColorChooser();
|
||||
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.WHITE);
|
||||
return res;
|
||||
}
|
||||
public DrawningAirplane SelectedAirplane(){
|
||||
return DrawningAirplane;
|
||||
}
|
||||
public FormAirplaneWithRadar(){
|
||||
AirplaneFrame =new JFrame ();
|
||||
JButton buttonCreate = new JButton("Создать");
|
||||
JButton buttonCreateLocomotive = new JButton("Создать самолет с радаром");
|
||||
JButton buttonStep = new JButton("Шаг");
|
||||
buttonSelect = new JButton ("Выбрать");
|
||||
JComboBox comboBoxStrategy = new JComboBox(
|
||||
new String[]{
|
||||
"Довести до центра",
|
||||
"Довести до края",
|
||||
});
|
||||
JButton UpButton = new JButton();
|
||||
UpButton.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\улгту\\2 курс\\РПП\\PIbd-23_Nasyrov_A_G_AirplaneWithRadar_hard\\src\\UpButton.jpg"));
|
||||
JButton DownButton = new JButton();
|
||||
DownButton.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\улгту\\2 курс\\РПП\\PIbd-23_Nasyrov_A_G_AirplaneWithRadar_hard\\src\\DownButton.jpg"));
|
||||
JButton LeftButton = new JButton();
|
||||
LeftButton.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\улгту\\2 курс\\РПП\\PIbd-23_Nasyrov_A_G_AirplaneWithRadar_hard\\src\\LeftButton.jpg"));
|
||||
JButton RightButton = new JButton();
|
||||
RightButton.setIcon(new ImageIcon("C:\\Users\\1\\Desktop\\улгту\\2 курс\\РПП\\PIbd-23_Nasyrov_A_G_AirplaneWithRadar_hard\\src\\RightButton.jpg"));
|
||||
buttonStep.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
if (DrawningAirplane == 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
|
||||
DrawningObjectAirplane(DrawningAirplane), pictureBoxWidth,
|
||||
pictureBoxHeight);
|
||||
comboBoxStrategy.setEnabled(false);
|
||||
}
|
||||
if (_abstractStrategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_abstractStrategy.MakeStep();
|
||||
Draw();
|
||||
if (_abstractStrategy.GetStatus() == Status.Finish)
|
||||
{
|
||||
comboBoxStrategy.setEnabled(true);
|
||||
_abstractStrategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonCreate.addActionListener(
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Random random = new Random();
|
||||
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color choosen = ChooseColor(AirplaneFrame);
|
||||
if(choosen != null){
|
||||
color = choosen;
|
||||
}
|
||||
DrawningAirplane = new DrawningAirplane(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||
color,
|
||||
pictureBoxWidth, pictureBoxHeight);
|
||||
canv.DrawningAirplane = DrawningAirplane;
|
||||
comboBoxStrategy.enable(true);
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonCreateLocomotive.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
Random random = new Random();
|
||||
Color color = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
|
||||
Color choosen = ChooseColor(AirplaneFrame);
|
||||
if(choosen != null){
|
||||
color = choosen;
|
||||
}
|
||||
choosen = ChooseColor(AirplaneFrame);
|
||||
if(choosen != null){
|
||||
additionalColor = choosen;
|
||||
}
|
||||
DrawningAirplane = new DrawningAirplaneWithRadar(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||
color,
|
||||
additionalColor,
|
||||
random.nextBoolean(), random.nextBoolean(),
|
||||
pictureBoxWidth, pictureBoxHeight);
|
||||
canv.DrawningAirplane = DrawningAirplane;
|
||||
comboBoxStrategy.enable(true);
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
RightButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningAirplane.EntityAirplane() == null) {
|
||||
return;
|
||||
}
|
||||
DrawningAirplane.MoveTransport(DirectionType.Right);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
LeftButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningAirplane.EntityAirplane() == null)
|
||||
return;
|
||||
DrawningAirplane.MoveTransport(DirectionType.Left);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
UpButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningAirplane.EntityAirplane() == null)
|
||||
return;
|
||||
DrawningAirplane.MoveTransport(DirectionType.Up);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
DownButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(DrawningAirplane.EntityAirplane() == null)
|
||||
return;
|
||||
DrawningAirplane.MoveTransport(DirectionType.Down);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
|
||||
AirplaneFrame.setSize (900, 500);
|
||||
AirplaneFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
|
||||
AirplaneFrame.setLayout(null);
|
||||
canv = new Canvas();
|
||||
canv.setSize(pictureBoxWidth, pictureBoxHeight);
|
||||
buttonSelect.setBounds(383,401, 180, 40);
|
||||
buttonCreate.setBounds(198, 401, 180, 40);
|
||||
buttonCreateLocomotive.setBounds(12, 401, 180, 40);
|
||||
RightButton.setBounds(840,411,30,30);
|
||||
LeftButton.setBounds(768,411,30,30);
|
||||
UpButton.setBounds(804,375,30,30);
|
||||
DownButton.setBounds(804,411,30,30);
|
||||
comboBoxStrategy.setBounds(719,12,151,28);
|
||||
buttonStep.setBounds(768, 46, 94, 29);
|
||||
AirplaneFrame.add(canv);
|
||||
AirplaneFrame.add(buttonCreate);
|
||||
AirplaneFrame.add(buttonCreateLocomotive);
|
||||
AirplaneFrame.add(UpButton);
|
||||
AirplaneFrame.add(DownButton);
|
||||
AirplaneFrame.add(LeftButton);
|
||||
AirplaneFrame.add(RightButton);
|
||||
AirplaneFrame.add(comboBoxStrategy);
|
||||
AirplaneFrame.add(buttonStep);
|
||||
AirplaneFrame.add(buttonSelect);
|
||||
AirplaneFrame.setVisible(true);
|
||||
}
|
||||
}
|
||||
class Canvas extends JComponent{
|
||||
public DrawningAirplane DrawningAirplane;
|
||||
public Canvas(){
|
||||
}
|
||||
public void paintComponent (Graphics g){
|
||||
if (DrawningAirplane == null){
|
||||
return;
|
||||
}
|
||||
super.paintComponents (g) ;
|
||||
Graphics2D g2d = (Graphics2D)g;
|
||||
DrawningAirplane.DrawAirplane(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
|
70
src/FormForHard.java
Normal file
70
src/FormForHard.java
Normal file
@ -0,0 +1,70 @@
|
||||
package src;
|
||||
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.DrawningObjects.DrawningIlluminators;
|
||||
import src.DrawningObjects.IDraw;
|
||||
import src.Entities.EntityAirplane;
|
||||
import src.Generics.HardGeneric;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormForHard {
|
||||
private Canvas canv;
|
||||
private int pictureBoxWidth;
|
||||
private int pictureBoxHeight;
|
||||
private EntityAirplane makeEntity(){
|
||||
Random rand = new Random();
|
||||
return new EntityAirplane(rand.nextInt(100, 300), rand.nextDouble(1000,3000),
|
||||
Color.getHSBColor(rand.nextInt(0, 301), rand.nextInt(0, 301), rand.nextInt(0, 301)));
|
||||
}
|
||||
void Draw(){
|
||||
if(canv == null)
|
||||
return;
|
||||
canv.repaint();
|
||||
}
|
||||
private IDraw makeIDraw(EntityAirplane entity){
|
||||
Random rand = new Random();
|
||||
return new DrawningIlluminators(pictureBoxWidth, pictureBoxHeight, 0, 0);
|
||||
}
|
||||
public FormForHard(){
|
||||
Random rand = new Random();
|
||||
int sz = rand.nextInt(1, 10);
|
||||
JFrame HardFrame = new JFrame();
|
||||
HardFrame.setSize(700, 400);
|
||||
HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
canv = new Canvas();
|
||||
canv.setBounds(0,0,pictureBoxWidth,pictureBoxHeight);
|
||||
JButton makeObject = new JButton("Создать");
|
||||
makeObject.setBounds(0, 0, 100, 40);
|
||||
canv.add(makeObject);
|
||||
HardFrame.setContentPane(canv);
|
||||
HardFrame.setVisible(true);
|
||||
pictureBoxHeight = canv.getHeight();
|
||||
pictureBoxWidth = canv.getWidth();
|
||||
HardGeneric<EntityAirplane, IDraw> toDraw= new HardGeneric<>(sz,
|
||||
sz, pictureBoxWidth, pictureBoxHeight);
|
||||
for(int i = 0; i < sz; i++){
|
||||
EntityAirplane ent = makeEntity();
|
||||
toDraw.InsertFirst(ent);
|
||||
toDraw.InsertSecond(makeIDraw(ent));
|
||||
}
|
||||
makeObject.addActionListener(
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
DrawningAirplane DrawningAirplane = toDraw.MakeObject();
|
||||
DrawningAirplane.SetPosition(pictureBoxWidth / 2 - DrawningAirplane.GetWidth()/2,
|
||||
pictureBoxHeight / 2 - DrawningAirplane.GetHeight()/2);
|
||||
canv.DrawningAirplane = DrawningAirplane;
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
74
src/Generics/AirplaneGenericCollection.java
Normal file
74
src/Generics/AirplaneGenericCollection.java
Normal file
@ -0,0 +1,74 @@
|
||||
package src.Generics;
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.MovementStrategy.IMoveableObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class AirplaneGenericCollection<T extends DrawningAirplane, U extends IMoveableObject> {
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
private final int _placeSizeWidth = 210;
|
||||
private final int _placeSizeHeight = 78;
|
||||
private final SetGeneric<T> _collection;
|
||||
public AirplaneGenericCollection(int picWidth, int picHeight){
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
public int Insert(T obj){
|
||||
if (obj == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return _collection.Insert(obj);
|
||||
}
|
||||
public boolean Remove(int position){
|
||||
return _collection.Remove(position);
|
||||
}
|
||||
public U GetU(int pos){
|
||||
T ans = _collection.Get(pos);
|
||||
if(ans == null)
|
||||
return null;
|
||||
return (U)ans.GetMoveableObject();
|
||||
}
|
||||
private void DrawBackground(Graphics g)
|
||||
{
|
||||
g.setColor(Color.BLACK);
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
|
||||
1; ++j)
|
||||
{
|
||||
g.drawLine(i * _placeSizeWidth, j *
|
||||
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
|
||||
_placeSizeHeight);
|
||||
}
|
||||
g.drawLine(i * _placeSizeWidth, 0, i *
|
||||
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
private void DrawObjects(Graphics g)
|
||||
{
|
||||
for (int i = 0; i < _collection.Count; i++)
|
||||
{
|
||||
DrawningAirplane airplane = _collection.Get(i);
|
||||
if (airplane != null)
|
||||
{
|
||||
int inRow = _pictureWidth / _placeSizeWidth;
|
||||
airplane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight );
|
||||
airplane.DrawAirplane((Graphics2D) g);
|
||||
}
|
||||
}
|
||||
}
|
||||
public BufferedImage ShowAirplanes()
|
||||
{
|
||||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
|
||||
Graphics gr = bmp.createGraphics();
|
||||
DrawBackground(gr);
|
||||
DrawObjects(gr);
|
||||
return bmp;
|
||||
}
|
||||
}
|
56
src/Generics/HardGeneric.java
Normal file
56
src/Generics/HardGeneric.java
Normal file
@ -0,0 +1,56 @@
|
||||
package src.Generics;
|
||||
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.DrawningObjects.IDraw;
|
||||
import src.Entities.EntityAirplane;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class HardGeneric <T extends EntityAirplane,U extends IDraw>{
|
||||
T[] arrAirplanes;
|
||||
U[] arrIlluminators;
|
||||
private int curSz;
|
||||
private int CountFirst;
|
||||
private int CountSecond;
|
||||
private int pictureBoxWidth;
|
||||
private int pictureBoxHeight;
|
||||
public HardGeneric(int countFirst, int countSecond, int width, int height){
|
||||
curSz = 0;
|
||||
CountFirst = countFirst;
|
||||
CountSecond = countSecond;
|
||||
arrAirplanes = (T[]) new EntityAirplane[CountFirst];
|
||||
arrIlluminators = (U[]) new IDraw[CountSecond];
|
||||
pictureBoxHeight = height;
|
||||
pictureBoxWidth = width;
|
||||
}
|
||||
|
||||
public int InsertFirst(T entityAirplane){
|
||||
if(arrAirplanes[CountFirst-1] != null)
|
||||
return -1;
|
||||
for(int i = curSz -1; i>= 0; i--) {
|
||||
arrAirplanes[i + 1] = arrAirplanes[i];
|
||||
arrIlluminators[i + 1] = arrIlluminators[i];
|
||||
}
|
||||
curSz++;
|
||||
arrAirplanes[0] = entityAirplane;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int InsertSecond(U inter){
|
||||
if(arrIlluminators[CountSecond-1] != null)
|
||||
return -1;
|
||||
arrIlluminators[0] = inter;
|
||||
return 0;
|
||||
}
|
||||
|
||||
public DrawningAirplane MakeObject(){
|
||||
Random rand = new Random();
|
||||
int indFirst = rand.nextInt(0, curSz);
|
||||
int indSecond = rand.nextInt(0,curSz);
|
||||
EntityAirplane entity = arrAirplanes[indFirst];
|
||||
IDraw inter = arrIlluminators[indSecond];
|
||||
DrawningAirplane airplane = new DrawningAirplane(entity.Speed(), entity.Weight(), entity.BodyColor(),
|
||||
pictureBoxWidth, pictureBoxHeight);
|
||||
return airplane;
|
||||
}
|
||||
}
|
47
src/Generics/SetGeneric.java
Normal file
47
src/Generics/SetGeneric.java
Normal file
@ -0,0 +1,47 @@
|
||||
package src.Generics;
|
||||
|
||||
public class SetGeneric <T extends Object>{
|
||||
private final Object[] _places;
|
||||
public int Count;
|
||||
public SetGeneric(int count){
|
||||
_places = new Object[count];
|
||||
Count = count;
|
||||
}
|
||||
public int Insert(T airplane){
|
||||
return Insert(airplane, 0);
|
||||
}
|
||||
public int Insert(T airplane, int position){
|
||||
if(!(position >= 0 && position < Count))
|
||||
return -1;
|
||||
if(_places[position] == null){
|
||||
_places[position] = airplane;
|
||||
}
|
||||
else{
|
||||
int place = -1;
|
||||
for(int i = position; i < Count; i++){
|
||||
if(_places[i] == null){
|
||||
place = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(place == -1)
|
||||
return -1;
|
||||
|
||||
for(int i = place - 1; i >= position; i--)
|
||||
_places[i+1] = _places[i];
|
||||
_places[position] = airplane;
|
||||
}
|
||||
return position;
|
||||
}
|
||||
public boolean Remove(int position){
|
||||
if(!(position >= 0 && position < Count))
|
||||
return false;
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
public T Get(int position){
|
||||
if(!(position >= 0 && position < Count))
|
||||
return null;
|
||||
return (T)_places[position];
|
||||
}
|
||||
}
|
19
src/Main.java
Normal file
19
src/Main.java
Normal file
@ -0,0 +1,19 @@
|
||||
package src;
|
||||
import src.DrawningObjects.DrawningAirplaneWithRadar;
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.MovementStrategy.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
FormForHard form = new FormForHard();
|
||||
}
|
||||
}
|
69
src/MovementStrategy/AbstractStrategy.java
Normal file
69
src/MovementStrategy/AbstractStrategy.java
Normal file
@ -0,0 +1,69 @@
|
||||
package src.MovementStrategy;
|
||||
import src.DirectionType;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObject _moveableObject;
|
||||
private Status _state = Status.NotInit;
|
||||
private int FieldWidth;
|
||||
protected int FieldWidth(){return FieldWidth;}
|
||||
private int FieldHeight;
|
||||
protected int FieldHeight(){return FieldHeight;}
|
||||
public Status GetStatus() { return _state; }
|
||||
public void SetData(IMoveableObject moveableObject, int width, int
|
||||
height)
|
||||
{
|
||||
if (moveableObject == null)
|
||||
{
|
||||
_state = Status.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = Status.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsTargetDestination())
|
||||
{
|
||||
_state = Status.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
protected boolean MoveLeft() {return MoveTo(DirectionType.Left);}
|
||||
protected boolean MoveRight() {return MoveTo(DirectionType.Right);}
|
||||
protected boolean MoveUp() {return MoveTo(DirectionType.Up);}
|
||||
protected boolean MoveDown() {return MoveTo(DirectionType.Down);}
|
||||
protected ObjectParameters GetObjectParameters(){
|
||||
if(_moveableObject != null)
|
||||
return _moveableObject.GetObjectParameters();
|
||||
else return null;
|
||||
}
|
||||
protected Integer GetStep()
|
||||
{
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject.GetStep();
|
||||
}
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract boolean IsTargetDestination();
|
||||
private boolean MoveTo(DirectionType directionType) {
|
||||
if (_state != Status.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_moveableObject.CheckCanMove(directionType))
|
||||
{
|
||||
_moveableObject.MoveObject(directionType);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
31
src/MovementStrategy/DrawningObjectAirplane.java
Normal file
31
src/MovementStrategy/DrawningObjectAirplane.java
Normal file
@ -0,0 +1,31 @@
|
||||
package src.MovementStrategy;
|
||||
import src.DirectionType;
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
|
||||
public class DrawningObjectAirplane implements IMoveableObject{
|
||||
private final DrawningAirplane _drawningAirplane;
|
||||
public DrawningObjectAirplane(DrawningAirplane drawningAirplane){
|
||||
_drawningAirplane = drawningAirplane;
|
||||
}
|
||||
public ObjectParameters GetObjectParameters(){
|
||||
if(_drawningAirplane == null || _drawningAirplane.EntityAirplane() == null)
|
||||
return null;
|
||||
return new ObjectParameters(_drawningAirplane.GetPosX(), _drawningAirplane.GetPosY(),
|
||||
_drawningAirplane.GetWidth(), _drawningAirplane.GetHeight());
|
||||
}
|
||||
public int GetStep(){
|
||||
if(_drawningAirplane.EntityAirplane() == null)
|
||||
return 0;
|
||||
return (int)_drawningAirplane.EntityAirplane().Step();
|
||||
}
|
||||
public boolean CheckCanMove(DirectionType direction){
|
||||
if(_drawningAirplane == null)
|
||||
return false;
|
||||
return _drawningAirplane.CanMove(direction);
|
||||
}
|
||||
public void MoveObject(DirectionType direction){
|
||||
if(_drawningAirplane == null)
|
||||
return;
|
||||
_drawningAirplane.MoveTransport(direction);
|
||||
}
|
||||
}
|
10
src/MovementStrategy/IMoveableObject.java
Normal file
10
src/MovementStrategy/IMoveableObject.java
Normal file
@ -0,0 +1,10 @@
|
||||
package src.MovementStrategy;
|
||||
|
||||
import src.DirectionType;
|
||||
|
||||
public interface IMoveableObject {
|
||||
public ObjectParameters GetObjectParameters();
|
||||
public int GetStep();
|
||||
boolean CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
36
src/MovementStrategy/MoveToBorder.java
Normal file
36
src/MovementStrategy/MoveToBorder.java
Normal file
@ -0,0 +1,36 @@
|
||||
package src.MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy {
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return false;
|
||||
}
|
||||
int a = FieldWidth();
|
||||
int b = FieldHeight();
|
||||
int q = GetStep();
|
||||
return objParams.RightBorder <= FieldWidth() && objParams.RightBorder + GetStep() >= FieldWidth() &&
|
||||
objParams.DownBorder <= FieldHeight() && objParams.DownBorder + GetStep() >= FieldHeight();
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null) {
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.RightBorder - FieldWidth();
|
||||
if (Math.abs(diffX) >= GetStep()) {
|
||||
if (diffX < 0) {
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.DownBorder - FieldHeight();
|
||||
if (Math.abs(diffY) >= GetStep()) {
|
||||
if (diffY < 0) {
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
52
src/MovementStrategy/MoveToCenter.java
Normal file
52
src/MovementStrategy/MoveToCenter.java
Normal file
@ -0,0 +1,52 @@
|
||||
package src.MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestination(){
|
||||
var objParams = GetObjectParameters();
|
||||
if(objParams == null)
|
||||
return false;
|
||||
return ((objParams.ObjectMiddleHorizontal <= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth() / 2)
|
||||
||(objParams.ObjectMiddleHorizontal >= FieldWidth() / 2 &&
|
||||
objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth() / 2)) &&
|
||||
|
||||
((objParams.ObjectMiddleVertical <= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight() / 2) ||
|
||||
(objParams.ObjectMiddleVertical >= FieldHeight() / 2 &&
|
||||
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight() / 2));
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget()
|
||||
{
|
||||
var objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth() / 2;
|
||||
if (Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical - FieldHeight() / 2;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
27
src/MovementStrategy/ObjectParameters.java
Normal file
27
src/MovementStrategy/ObjectParameters.java
Normal file
@ -0,0 +1,27 @@
|
||||
package src.MovementStrategy;
|
||||
|
||||
public class ObjectParameters {
|
||||
private final int _x;
|
||||
private final int _y;
|
||||
private final int _width;
|
||||
private final int _height;
|
||||
public int LeftBorder;
|
||||
public int TopBorder;
|
||||
public int RightBorder;
|
||||
public int DownBorder;
|
||||
public int ObjectMiddleHorizontal;
|
||||
public int ObjectMiddleVertical;
|
||||
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;
|
||||
}
|
||||
}
|
7
src/MovementStrategy/Status.java
Normal file
7
src/MovementStrategy/Status.java
Normal file
@ -0,0 +1,7 @@
|
||||
package src.MovementStrategy;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
6
src/NumberType.java
Normal file
6
src/NumberType.java
Normal file
@ -0,0 +1,6 @@
|
||||
package src;
|
||||
public enum NumberType {
|
||||
Ten,
|
||||
Twenty,
|
||||
Thirty
|
||||
}
|
Loading…
Reference in New Issue
Block a user