Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
bdf2311313 | ||
|
f146a0ff06 | ||
|
146fdcaa94 | ||
|
3a5b2d28bb | ||
|
fc549cc992 | ||
|
b32bd61729 | ||
|
121779e3cb | ||
|
968cc81bc8 | ||
|
1252238fc4 | ||
|
a70ce74c29 | ||
|
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
|
||||
}
|
232
src/DrawningObjects/DrawningAirplane.java
Normal file
232
src/DrawningObjects/DrawningAirplane.java
Normal file
@ -0,0 +1,232 @@
|
||||
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;
|
||||
public int _pictureWidth;
|
||||
public int _pictureHeight;
|
||||
protected int _startPosX;
|
||||
protected int _startPosY;
|
||||
protected IDraw DrawningIlluminators;
|
||||
public int _airplaneWidth = 200;
|
||||
public int _airplaneHeight = 78;
|
||||
public int _illuminatorsNumb;
|
||||
public EntityAirplane EntityAirplane(){
|
||||
return EntityAirplane;
|
||||
}
|
||||
public void ChangeColor(Color col){
|
||||
EntityAirplane.BodyColor = col;
|
||||
}
|
||||
public void ChangeIDraw(IDraw obj){
|
||||
DrawningIlluminators = obj;
|
||||
obj.ChangeIlluminatorNumb(_illuminatorsNumb);
|
||||
obj.ChangeX(_startPosX);
|
||||
obj.ChangeY(_startPosY);
|
||||
}
|
||||
public void ChangeIlluminatorNumb(int numb){
|
||||
_illuminatorsNumb = numb;
|
||||
DrawningIlluminators.ChangeIlluminatorNumb(numb);
|
||||
}
|
||||
public int GetIDrawType(){
|
||||
if(DrawningIlluminators instanceof DrawningIlluminators)
|
||||
return 0;
|
||||
if(DrawningIlluminators instanceof DrawningIlluminatorsCirc)
|
||||
return 1;
|
||||
if(DrawningIlluminators instanceof DrawningIlluminatorsQuad)
|
||||
return 2;
|
||||
return -1;
|
||||
}
|
||||
public void ChangeIDraw(int variant){
|
||||
IDraw cur;
|
||||
if(variant == 0)
|
||||
cur = new DrawningIlluminators(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
else if(variant == 1)
|
||||
cur = new DrawningIlluminatorsCirc(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
else
|
||||
cur = new DrawningIlluminatorsQuad(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
|
||||
ChangeIDraw(cur);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
58
src/DrawningObjects/DrawningAirplaneWithRadar.java
Normal file
58
src/DrawningObjects/DrawningAirplaneWithRadar.java
Normal file
@ -0,0 +1,58 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
public void ChangeAddColor(Color col){
|
||||
((EntityAirplaneWithRadar)EntityAirplane).AdditionalColor = col;
|
||||
}
|
||||
|
||||
@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 <= 10)
|
||||
IlluminatorNumb = NumberType.Ten;
|
||||
if(x == 20)
|
||||
IlluminatorNumb = NumberType.Twenty;
|
||||
if(x >= 30)
|
||||
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 <= 10)
|
||||
IlluminatorNumb = NumberType.Ten;
|
||||
if(x == 20)
|
||||
IlluminatorNumb = NumberType.Twenty;
|
||||
if(x >= 30)
|
||||
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 <= 10)
|
||||
IlluminatorNumb = NumberType.Ten;
|
||||
if(x == 20)
|
||||
IlluminatorNumb = NumberType.Twenty;
|
||||
if(x >= 30)
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
93
src/DrawningObjects/ExtentionDrawningAirplane.java
Normal file
93
src/DrawningObjects/ExtentionDrawningAirplane.java
Normal file
@ -0,0 +1,93 @@
|
||||
package src.DrawningObjects;
|
||||
|
||||
import src.Entities.EntityAirplaneWithRadar;
|
||||
import java.awt.*;
|
||||
public class ExtentionDrawningAirplane {
|
||||
private static String getName(Color col) {
|
||||
if (col.equals(Color.RED))
|
||||
return new String("RED");
|
||||
if (col.equals(Color.GREEN))
|
||||
return new String("GREEN");
|
||||
if (col.equals(Color.BLUE))
|
||||
return new String("BLUE");
|
||||
if (col.equals(Color.YELLOW))
|
||||
return new String("YELLOW");
|
||||
if (col.equals(Color.WHITE))
|
||||
return new String("WHITE");
|
||||
if (col.equals(Color.GRAY))
|
||||
return new String("GRAY");
|
||||
if (col.equals(Color.BLACK))
|
||||
return new String("BLACK");
|
||||
if (col.equals(Color.PINK))
|
||||
return new String("PINK");
|
||||
return null;
|
||||
}
|
||||
private static Color getColor(String col){
|
||||
if(col.equals("RED"))
|
||||
return Color.RED;
|
||||
if(col.equals("GREEN"))
|
||||
return Color.GREEN;
|
||||
if(col.equals("BLUE"))
|
||||
return Color.BLUE;
|
||||
if(col.equals("YELLOW"))
|
||||
return Color.YELLOW;
|
||||
if(col.equals("WHITE"))
|
||||
return Color.WHITE;
|
||||
if(col.equals("GRAY"))
|
||||
return Color.GRAY;
|
||||
if(col.equals("BLACK"))
|
||||
return Color.BLACK;
|
||||
if(col.equals("PINK"))
|
||||
return Color.PINK;
|
||||
return null;
|
||||
}
|
||||
public static DrawningAirplane CreateDrawningAirplane(String info, char separatorForObject, int width, int height){
|
||||
String[] strs = info.split(Character.toString(separatorForObject));
|
||||
if(strs.length == 5){
|
||||
DrawningAirplane toRet = new DrawningAirplane(
|
||||
Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]),
|
||||
getColor(strs[2]),
|
||||
width,
|
||||
height);
|
||||
toRet.ChangeIlluminatorNumb(Integer.parseInt(strs[3]));
|
||||
toRet.ChangeIDraw(Integer.parseInt(strs[4]));
|
||||
return toRet;
|
||||
}
|
||||
if(strs.length == 8){
|
||||
DrawningAirplane toRet = new DrawningAirplaneWithRadar(
|
||||
Integer.parseInt(strs[0]),
|
||||
Integer.parseInt(strs[1]),
|
||||
getColor(strs[2]),
|
||||
getColor(strs[7]),
|
||||
Boolean.parseBoolean(strs[5]),
|
||||
Boolean.parseBoolean(strs[6]),
|
||||
width,
|
||||
height);
|
||||
toRet.ChangeIlluminatorNumb(Integer.parseInt(strs[3]));
|
||||
toRet.ChangeIDraw(Integer.parseInt(strs[4]));
|
||||
return toRet;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String GetDataForSave(DrawningAirplane drawningAirplane, char separatorForObject){
|
||||
var airplane = drawningAirplane.EntityAirplane;
|
||||
if(airplane == null)
|
||||
return null;
|
||||
String colName = getName(airplane.BodyColor);
|
||||
var str = String.format("%d%c%d%c%s%c%d%c%d",
|
||||
airplane.Speed(),
|
||||
separatorForObject, (int)airplane.Weight(),
|
||||
separatorForObject, getName(airplane.BodyColor),
|
||||
separatorForObject, drawningAirplane._illuminatorsNumb,
|
||||
separatorForObject, drawningAirplane.GetIDrawType());
|
||||
if(!(airplane instanceof EntityAirplaneWithRadar)){
|
||||
return str;
|
||||
}
|
||||
var nstr = String.format("%s%c%b%c%b%c%s", str, separatorForObject,
|
||||
((EntityAirplaneWithRadar) airplane).Radar(), separatorForObject,
|
||||
((EntityAirplaneWithRadar) airplane).DopBak(), separatorForObject,
|
||||
getName(((EntityAirplaneWithRadar) airplane).AdditionalColor), separatorForObject);
|
||||
return nstr;
|
||||
}
|
||||
}
|
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;
|
||||
public 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 {
|
||||
public 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;
|
||||
}
|
||||
}
|
311
src/FormAirplaneCollection.java
Normal file
311
src/FormAirplaneCollection.java
Normal file
@ -0,0 +1,311 @@
|
||||
package src;
|
||||
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.Generics.AirplaneGenericCollection;
|
||||
import src.MovementStrategy.DrawningObjectAirplane;
|
||||
import src.Generics.AirplaneGenericStorage;
|
||||
import src.Generics.AirplaneTrashCollection;
|
||||
import src.DrawningObjects.ExtentionDrawningAirplane;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
import javax.swing.filechooser.FileFilter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
class TxtSaveFilter extends FileFilter {
|
||||
@Override
|
||||
public boolean accept(File f) {
|
||||
if (f.isDirectory()) {
|
||||
return false;
|
||||
}
|
||||
String s = f.getName().toLowerCase();
|
||||
return s.endsWith(".txt");
|
||||
}
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "*.txt";
|
||||
}
|
||||
}
|
||||
|
||||
public class FormAirplaneCollection {
|
||||
private final AirplaneGenericStorage _storage;
|
||||
private JList<String> listBoxStorages;
|
||||
private DefaultListModel<String> listBoxModel;
|
||||
private int pictureBoxWidth = 630;
|
||||
private int pictureBoxHeight = 426;
|
||||
CollectionCanvas canv;
|
||||
|
||||
void Draw() {
|
||||
if (canv == null)
|
||||
return;
|
||||
canv.repaint();
|
||||
}
|
||||
|
||||
private void ReloadObjects() {
|
||||
int index = listBoxStorages.getSelectedIndex();
|
||||
listBoxModel.clear();
|
||||
List<String> keys = _storage.Keys();
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
listBoxModel.addElement(keys.get(i));
|
||||
}
|
||||
if (listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size()))
|
||||
listBoxStorages.setSelectedIndex(0);
|
||||
else if (listBoxModel.size() > 0)
|
||||
listBoxStorages.setSelectedIndex(index);
|
||||
}
|
||||
|
||||
public FormAirplaneCollection() {
|
||||
JMenuBar menuFile = new JMenuBar();
|
||||
JMenu file = new JMenu("Файл");
|
||||
menuFile.add(file);
|
||||
JMenuItem saveFile = new JMenuItem("Сохранить");
|
||||
JMenuItem loadFile = new JMenuItem("Загрузить");
|
||||
JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию");
|
||||
JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию");
|
||||
file.add(saveCollection);
|
||||
file.add(loadCollection);
|
||||
file.add(saveFile);
|
||||
file.add(loadFile);
|
||||
AirplaneTrashCollection<DrawningAirplane> _trashCollection = new AirplaneTrashCollection<>();
|
||||
JButton callTrashButton = new JButton("Мусор");
|
||||
_storage = new AirplaneGenericStorage(pictureBoxWidth, pictureBoxHeight);
|
||||
JScrollPane scrollPane = new JScrollPane();
|
||||
canv = new CollectionCanvas();
|
||||
JPanel toolBox = new JPanel();
|
||||
JTextField storageName = new JTextField();
|
||||
JButton addStorageButton = new JButton("Добавить набор");
|
||||
listBoxModel = new DefaultListModel<>();
|
||||
listBoxStorages = new JList<>(listBoxModel);
|
||||
scrollPane.setViewportView(listBoxStorages);
|
||||
JButton delStorageButton = new JButton("Удалить набор");
|
||||
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(9, 1);
|
||||
toolBox.add(storageName);
|
||||
toolBox.add(addStorageButton);
|
||||
toolBox.add(scrollPane);
|
||||
toolBox.add(delStorageButton);
|
||||
toolBox.setLayout(lay);
|
||||
toolBox.add(addButton);
|
||||
toolBox.add(airplaneNumb);
|
||||
toolBox.add(removeButton);
|
||||
toolBox.add(callTrashButton);
|
||||
toolBox.add(refreshButton);
|
||||
collectionFrame.add(toolBox);
|
||||
collectionFrame.setJMenuBar(menuFile);
|
||||
collectionFrame.add(canv);
|
||||
collectionFrame.setVisible(true);
|
||||
canv._storage = _storage;
|
||||
canv.listBoxStorages = listBoxStorages;
|
||||
saveFile.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\1\\Desktop\\свое");
|
||||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||||
int retrieval = fc.showSaveDialog(null);
|
||||
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||||
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||||
try {
|
||||
_storage.SaveData(file);
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
saveCollection.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\1\\Desktop\\свое");
|
||||
fc.addChoosableFileFilter(new TxtSaveFilter());
|
||||
int retrieval = fc.showSaveDialog(null);
|
||||
if (retrieval == JFileChooser.APPROVE_OPTION) {
|
||||
File file = new File(fc.getSelectedFile() + "." + "txt");
|
||||
try {
|
||||
if(listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
_storage._airplaneStorages.get(listBoxStorages.getSelectedValue()).SaveData(file, listBoxStorages.getSelectedValue());
|
||||
ReloadObjects();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
loadFile.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\1\\Desktop\\свое");
|
||||
int ret = fc.showDialog(null, "Открыть файл");
|
||||
if(ret == JFileChooser.APPROVE_OPTION){
|
||||
File file = fc.getSelectedFile();
|
||||
try {
|
||||
_storage.LoadData(file);
|
||||
canv._storage =_storage;
|
||||
ReloadObjects();
|
||||
canv.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
loadCollection.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fc = new JFileChooser("C:\\Users\\1\\Desktop\\свое");
|
||||
int ret = fc.showDialog(null, "Открыть файл");
|
||||
if(ret == JFileChooser.APPROVE_OPTION){
|
||||
File file = fc.getSelectedFile();
|
||||
try {
|
||||
_storage.LoadCollection(file);
|
||||
ReloadObjects();
|
||||
canv.repaint();
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
addStorageButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (storageName.getText() == null)
|
||||
return;
|
||||
_storage.AddSet(storageName.getText());
|
||||
ReloadObjects();
|
||||
}
|
||||
});
|
||||
delStorageButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
_storage.DelSet(listBoxStorages.getSelectedValue(), _trashCollection);
|
||||
ReloadObjects();
|
||||
}
|
||||
});
|
||||
callTrashButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_trashCollection.GetSize() == 0)
|
||||
return;
|
||||
FormAirplaneWithRadar form = new FormAirplaneWithRadar();
|
||||
form.ChangeAirplane(_trashCollection.GetTop());
|
||||
_trashCollection.Pop();
|
||||
}
|
||||
});
|
||||
addButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue());
|
||||
FormAirplaneConfig form = new FormAirplaneConfig();
|
||||
form.addButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (_airplanes.Insert(form._airplane)) {
|
||||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
form._airplane._pictureWidth = pictureBoxWidth;
|
||||
form._airplane._pictureHeight = pictureBoxHeight;
|
||||
Draw();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
|
||||
}
|
||||
form.frameConfig.dispose();
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
form.cancelButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
form.frameConfig.dispose();
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
removeButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue());
|
||||
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;
|
||||
}
|
||||
DrawningAirplane curAirplane = _airplanes.Get(numb);
|
||||
_trashCollection.Push(curAirplane);
|
||||
_airplanes.Remove(numb);
|
||||
_airplanes.ShowAirplanes();
|
||||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
|
||||
refreshButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> _airplanes = _storage.Get(listBoxStorages.getSelectedValue());
|
||||
if (_airplanes == null) {
|
||||
return;
|
||||
}
|
||||
_airplanes.ShowAirplanes();
|
||||
Draw();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
class CollectionCanvas extends JComponent {
|
||||
public AirplaneGenericStorage _storage;
|
||||
public JList<String> listBoxStorages;
|
||||
|
||||
public CollectionCanvas() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowAirplanes(), 0, 0, this);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
||||
}
|
452
src/FormAirplaneConfig.java
Normal file
452
src/FormAirplaneConfig.java
Normal file
@ -0,0 +1,452 @@
|
||||
package src;
|
||||
|
||||
import src.DrawningObjects.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.DataFlavor;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.event.*;
|
||||
import java.io.IOException;
|
||||
|
||||
import static java.lang.Integer.parseInt;
|
||||
|
||||
public class FormAirplaneConfig {
|
||||
private class LabelTransferHandler extends TransferHandler {
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new StringSelection(((JLabel)c).getText());
|
||||
}
|
||||
}
|
||||
|
||||
private class ColorTransferable implements Transferable {
|
||||
private Color color;
|
||||
private static final DataFlavor colorDataFlavor = new DataFlavor(Color.class, "Color");
|
||||
|
||||
public ColorTransferable(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataFlavor[] getTransferDataFlavors() {
|
||||
return new DataFlavor[]{colorDataFlavor};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||
return colorDataFlavor.equals(flavor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return color;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class iDrawTransferable implements Transferable {
|
||||
private IDraw iDrawObject;
|
||||
private static final DataFlavor iDrawDataFlavor = new DataFlavor(IDraw.class, "IDraw");
|
||||
public iDrawTransferable(IDraw iDrawObject) {
|
||||
this.iDrawObject = iDrawObject;
|
||||
}
|
||||
@Override
|
||||
public DataFlavor[] getTransferDataFlavors() {
|
||||
return new DataFlavor[]{iDrawDataFlavor};
|
||||
}
|
||||
@Override
|
||||
public boolean isDataFlavorSupported(DataFlavor flavor) {
|
||||
return iDrawDataFlavor.equals(flavor);
|
||||
}
|
||||
@Override
|
||||
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
|
||||
if (isDataFlavorSupported(flavor)) {
|
||||
return iDrawObject;
|
||||
} else {
|
||||
throw new UnsupportedFlavorException(flavor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class PanelTransferHandler extends TransferHandler {
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new ColorTransferable(((JPanel)c).getBackground());
|
||||
}
|
||||
}
|
||||
|
||||
private class LabelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JLabel)e.getComponent()).getTransferHandler().exportAsDrag(((JLabel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
|
||||
private class PanelMouseAdapter extends MouseAdapter{
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((JPanel)e.getComponent()).getTransferHandler().exportAsDrag(((JPanel)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
private class IDrawComponent extends JComponent{
|
||||
public IDraw obj;
|
||||
|
||||
public IDrawComponent(IDraw obj){
|
||||
this.obj = obj;
|
||||
this.addMouseListener(
|
||||
new MouseAdapter(){
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
((IDrawComponent)e.getComponent()).getTransferHandler().exportAsDrag(((IDrawComponent)e.getComponent()), e, TransferHandler.COPY);
|
||||
}
|
||||
}
|
||||
);
|
||||
this.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public int getSourceActions(JComponent c) {
|
||||
return TransferHandler.COPY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Transferable createTransferable(JComponent c) {
|
||||
return new iDrawTransferable(((IDrawComponent)c).obj);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public JFrame frameConfig;
|
||||
|
||||
public JButton addButton;
|
||||
public JButton cancelButton;
|
||||
public DrawningAirplane _airplane;
|
||||
int _pictureBoxWidth = 218;
|
||||
int _pictureBoxHeight = 190;
|
||||
|
||||
public FormAirplaneConfig(){
|
||||
frameConfig = new JFrame();
|
||||
addButton = new JButton("Добавить");
|
||||
cancelButton = new JButton("Отмена");
|
||||
addButton.setBounds(555,262,94,29);
|
||||
cancelButton.setBounds(679,262,94,29);
|
||||
Canvas canv = new Canvas();
|
||||
canv.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
canv.setBounds(555, 65, _pictureBoxWidth, _pictureBoxHeight);
|
||||
JLabel iDrawLabel = new JLabel("Форма");
|
||||
JLabel colorLabel = new JLabel("Цвет");
|
||||
DrawningAirplane toGetSize = new DrawningAirplane(0,0, null, 0, 0);
|
||||
IDrawComponent usualComp = new IDrawComponent(new DrawningIlluminators(toGetSize._airplaneWidth - toGetSize._airplaneWidth / 10,
|
||||
toGetSize._airplaneHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._airplaneWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._airplaneHeight / 2));
|
||||
IDrawComponent circComp = new IDrawComponent(new DrawningIlluminatorsCirc(toGetSize._airplaneWidth - toGetSize._airplaneWidth / 10,
|
||||
toGetSize._airplaneHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._airplaneWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._airplaneHeight / 2));
|
||||
IDrawComponent quadComp = new IDrawComponent(new DrawningIlluminatorsQuad(toGetSize._airplaneWidth - toGetSize._airplaneWidth / 10,
|
||||
toGetSize._airplaneHeight,
|
||||
_pictureBoxWidth / 2 -toGetSize._airplaneWidth / 2,
|
||||
_pictureBoxHeight / 2 -toGetSize._airplaneHeight / 2));
|
||||
usualComp.setLayout(new GridLayout(1,1));
|
||||
circComp.setLayout(new GridLayout(1,1));
|
||||
quadComp.setLayout(new GridLayout(1,1));
|
||||
iDrawLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
iDrawLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
JLabel cartLabel = new JLabel("Круглые");
|
||||
JLabel ornLabel = new JLabel("Прозрачные");
|
||||
JLabel baseLabel = new JLabel("Квадратные");
|
||||
cartLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
cartLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
ornLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
ornLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
baseLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
baseLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
|
||||
usualComp.add(cartLabel);
|
||||
circComp.add(ornLabel);
|
||||
quadComp.add(baseLabel);
|
||||
usualComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
circComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
quadComp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JLabel addColorLabel = new JLabel("Доп цвет");
|
||||
addColorLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
addColorLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
colorLabel.setBounds(555, 20, 70, 33);
|
||||
addColorLabel.setBounds(629, 20, 70, 33);
|
||||
iDrawLabel.setBounds(703, 20, 70, 33);
|
||||
colorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
addColorLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
iDrawLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
JCheckBox checkRadar = new JCheckBox("Радар");
|
||||
JCheckBox checkDopBak = new JCheckBox("Дополнительный бак");
|
||||
checkRadar.setBounds(6, 132, 159, 24);
|
||||
checkDopBak.setBounds(6, 162, 145, 24);
|
||||
JLabel simpleLabel = new JLabel("Простой");
|
||||
JLabel advancedLabel = new JLabel("Продвинутый");
|
||||
simpleLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
advancedLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
|
||||
simpleLabel.setBounds(171,169, 120, 50);
|
||||
simpleLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
simpleLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
advancedLabel.setBounds(297,169, 120, 50);
|
||||
advancedLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
advancedLabel.setVerticalAlignment(SwingConstants.CENTER);
|
||||
usualComp.setBounds(171,229, 120, 50);
|
||||
circComp.setBounds(297,229, 120, 50);
|
||||
quadComp.setBounds(423,229, 120, 50);
|
||||
JLabel speedLabel = new JLabel ("Скорость");
|
||||
JLabel weightLabel = new JLabel ("Вес");
|
||||
JPanel colorPanel = new JPanel();
|
||||
colorPanel.setBounds(171, 23, 234,143);
|
||||
SpinnerNumberModel speedSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
|
||||
SpinnerNumberModel weightSpinnerModel = new SpinnerNumberModel(100.0, 100.0, 1000.0, 1.0);
|
||||
SpinnerNumberModel illuminatorNumbSpinnerModel = new SpinnerNumberModel(10, 10, 30, 10);
|
||||
JLabel illuminatorLabel = new JLabel ("Количество иллюминаторов");
|
||||
JSpinner illuminatorNumbSpinner = new JSpinner(illuminatorNumbSpinnerModel);
|
||||
JSpinner speedSpinner = new JSpinner(speedSpinnerModel);
|
||||
JSpinner weightSpinner = new JSpinner(weightSpinnerModel);
|
||||
speedSpinner.setBounds(6, 46, 150, 27);
|
||||
speedLabel.setBounds(6, 23, 73, 20);
|
||||
weightSpinner.setBounds(6, 99, 150, 27);
|
||||
weightLabel.setBounds(6, 76, 33, 20);
|
||||
illuminatorLabel.setBounds(6, 200, 150, 27);
|
||||
illuminatorNumbSpinner.setBounds(6, 230, 150, 27);
|
||||
|
||||
JPanel redPanel = new JPanel();
|
||||
JPanel greenPanel = new JPanel();
|
||||
JPanel bluePanel = new JPanel();
|
||||
JPanel yellowPanel = new JPanel();
|
||||
JPanel whitePanel = new JPanel();
|
||||
JPanel grayPanel = new JPanel();
|
||||
JPanel blackPanel = new JPanel();
|
||||
JPanel purplePanel = new JPanel();
|
||||
|
||||
redPanel.setTransferHandler(new PanelTransferHandler());
|
||||
greenPanel.setTransferHandler(new PanelTransferHandler());
|
||||
bluePanel.setTransferHandler(new PanelTransferHandler());
|
||||
yellowPanel.setTransferHandler(new PanelTransferHandler());
|
||||
whitePanel.setTransferHandler(new PanelTransferHandler());
|
||||
grayPanel.setTransferHandler(new PanelTransferHandler());
|
||||
blackPanel.setTransferHandler(new PanelTransferHandler());
|
||||
purplePanel.setTransferHandler(new PanelTransferHandler());
|
||||
|
||||
redPanel.addMouseListener(new PanelMouseAdapter());
|
||||
greenPanel.addMouseListener(new PanelMouseAdapter());
|
||||
bluePanel.addMouseListener(new PanelMouseAdapter());
|
||||
yellowPanel.addMouseListener(new PanelMouseAdapter());
|
||||
whitePanel.addMouseListener(new PanelMouseAdapter());
|
||||
grayPanel.addMouseListener(new PanelMouseAdapter());
|
||||
blackPanel.addMouseListener(new PanelMouseAdapter());
|
||||
purplePanel.addMouseListener(new PanelMouseAdapter());
|
||||
|
||||
redPanel.setName("Красный");
|
||||
greenPanel.setName("Зелёный");
|
||||
bluePanel.setName("Синий");
|
||||
yellowPanel.setName("Жёлтый");
|
||||
whitePanel.setName("Белый");
|
||||
grayPanel.setName("Серый");
|
||||
blackPanel.setName("Чёрный");
|
||||
purplePanel.setName("Фиолетовый");
|
||||
|
||||
|
||||
simpleLabel.setTransferHandler(new LabelTransferHandler());
|
||||
simpleLabel.addMouseListener(new LabelMouseAdapter());
|
||||
|
||||
advancedLabel.setTransferHandler(new LabelTransferHandler());
|
||||
advancedLabel.addMouseListener(new LabelMouseAdapter());
|
||||
|
||||
|
||||
|
||||
redPanel.setBackground(Color.RED);
|
||||
greenPanel.setBackground(Color.GREEN);
|
||||
bluePanel.setBackground(Color.BLUE);
|
||||
yellowPanel.setBackground(Color.YELLOW);
|
||||
whitePanel.setBackground(Color.WHITE);
|
||||
grayPanel.setBackground(Color.GRAY);
|
||||
blackPanel.setBackground(Color.BLACK);
|
||||
purplePanel.setBackground(Color.PINK);
|
||||
|
||||
colorPanel.setLayout(new GridLayout(2, 4));
|
||||
|
||||
colorPanel.add(redPanel);
|
||||
colorPanel.add(greenPanel);
|
||||
colorPanel.add(bluePanel);
|
||||
colorPanel.add(yellowPanel);
|
||||
colorPanel.add(whitePanel);
|
||||
colorPanel.add(grayPanel);
|
||||
colorPanel.add(blackPanel);
|
||||
colorPanel.add(purplePanel);
|
||||
|
||||
|
||||
frameConfig.add(colorLabel);
|
||||
frameConfig.add(addColorLabel);
|
||||
frameConfig.add(iDrawLabel);
|
||||
frameConfig.setLayout(null);
|
||||
frameConfig.setSize(818, 350);
|
||||
frameConfig.add(speedLabel);
|
||||
frameConfig.add(speedSpinner);
|
||||
frameConfig.add(weightLabel);
|
||||
frameConfig.add(illuminatorLabel);
|
||||
frameConfig.add(weightSpinner);
|
||||
frameConfig.add(simpleLabel);
|
||||
frameConfig.add(advancedLabel);
|
||||
frameConfig.add(checkRadar);
|
||||
frameConfig.add(checkDopBak);
|
||||
frameConfig.add(canv);
|
||||
frameConfig.add(addButton);
|
||||
frameConfig.add(cancelButton);
|
||||
frameConfig.add(illuminatorNumbSpinner);
|
||||
|
||||
frameConfig.setVisible(true);
|
||||
frameConfig.add(colorPanel);
|
||||
frameConfig.add(usualComp);
|
||||
frameConfig.add(circComp);
|
||||
frameConfig.add(quadComp);
|
||||
colorPanel.setVisible(true);
|
||||
usualComp.setVisible(true);
|
||||
circComp.setVisible(true);
|
||||
quadComp.setVisible(true);
|
||||
|
||||
canv.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(DataFlavor.stringFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Object speedObj = speedSpinner.getValue();
|
||||
Number speedNumb = (Number) speedObj;
|
||||
int speed = speedNumb.intValue();
|
||||
|
||||
Object weightObj = weightSpinner.getValue();
|
||||
Number weightNumb = (Number) weightObj;
|
||||
int weight = weightNumb.intValue();
|
||||
|
||||
Object illuminatorNumbObj = illuminatorNumbSpinner.getValue();
|
||||
Number illuminatorNumbNumb = (Number) illuminatorNumbObj;
|
||||
int illuminatorNumb = illuminatorNumbNumb.intValue();
|
||||
|
||||
String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
|
||||
switch (data) {
|
||||
case "Простой":
|
||||
_airplane = new DrawningAirplane(speed, weight, Color.WHITE, _pictureBoxWidth, _pictureBoxHeight);
|
||||
_airplane.ChangeIlluminatorNumb(illuminatorNumb);
|
||||
break;
|
||||
case "Продвинутый":
|
||||
_airplane = new DrawningAirplaneWithRadar(speed, weight, Color.WHITE, Color.GRAY, checkRadar.isSelected(), checkDopBak.isSelected(), _pictureBoxWidth, _pictureBoxHeight);
|
||||
_airplane.ChangeIlluminatorNumb(illuminatorNumb);
|
||||
break;
|
||||
}
|
||||
_airplane.SetPosition(_pictureBoxWidth / 2 - _airplane.GetWidth()/2, _pictureBoxHeight / 2 - _airplane.GetHeight()/2);
|
||||
canv.DrawningAirplane = _airplane;
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
iDrawLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(iDrawTransferable.iDrawDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
IDraw obj = (IDraw) support.getTransferable().getTransferData(iDrawTransferable.iDrawDataFlavor);
|
||||
if (_airplane == null)
|
||||
return false;
|
||||
_airplane.ChangeIDraw(obj);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
colorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (_airplane == null)
|
||||
return false;
|
||||
_airplane.ChangeColor(color);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
addColorLabel.setTransferHandler(
|
||||
new TransferHandler(){
|
||||
@Override
|
||||
public boolean canImport(TransferSupport support) {
|
||||
return support.isDataFlavorSupported(ColorTransferable.colorDataFlavor);
|
||||
}
|
||||
@Override
|
||||
public boolean importData(TransferSupport support) {
|
||||
if (canImport(support)) {
|
||||
try {
|
||||
Color color = (Color) support.getTransferable().getTransferData(ColorTransferable.colorDataFlavor);
|
||||
if (_airplane == null || !(_airplane instanceof DrawningAirplaneWithRadar))
|
||||
return false;
|
||||
((DrawningAirplaneWithRadar)_airplane).ChangeAddColor(color);
|
||||
canv.repaint();
|
||||
return true;
|
||||
} catch (UnsupportedFlavorException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
224
src/FormAirplaneWithRadar.java
Normal file
224
src/FormAirplaneWithRadar.java
Normal file
@ -0,0 +1,224 @@
|
||||
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 void ChangeAirplane(DrawningAirplane newAirplane){
|
||||
newAirplane.SetPosition(0,0);
|
||||
DrawningAirplane = newAirplane;
|
||||
canv.DrawningAirplane = 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();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
119
src/Generics/AirplaneGenericCollection.java
Normal file
119
src/Generics/AirplaneGenericCollection.java
Normal file
@ -0,0 +1,119 @@
|
||||
package src.Generics;
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.MovementStrategy.IMoveableObject;
|
||||
import src.DrawningObjects.ExtentionDrawningAirplane;
|
||||
import src.MovementStrategy.DrawningObjectAirplane;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class AirplaneGenericCollection<T extends DrawningAirplane, U extends IMoveableObject> {
|
||||
public static char _separatorRecords = ';';
|
||||
public static char _separatorForObject = ':';
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
private final int _placeSizeWidth = 210;
|
||||
private final int _placeSizeHeight = 78;
|
||||
private SetGeneric<T> _collection;
|
||||
public ArrayList<T> GetAirplanes(){
|
||||
return _collection.GetAirplanes(_collection.Count);
|
||||
}
|
||||
|
||||
public boolean SaveData(File f, String name) throws IOException {
|
||||
if(f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append("AirplaneCollection\n");
|
||||
data.append(String.format("%s\n", name));
|
||||
StringBuilder records = new StringBuilder();
|
||||
for(DrawningAirplane elem : GetAirplanes())
|
||||
{
|
||||
records.append(String.format("%s%c", ExtentionDrawningAirplane.GetDataForSave(elem, _separatorForObject), _separatorRecords));
|
||||
}
|
||||
data.append(records);
|
||||
if(data.length() == 0)
|
||||
return false;
|
||||
FileWriter writer = new FileWriter(f);
|
||||
writer.write(data.toString());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
return true;
|
||||
}
|
||||
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 Size(){
|
||||
return _collection.Count;
|
||||
}
|
||||
public boolean Insert(T obj){
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _collection.Insert(obj);
|
||||
}
|
||||
public boolean Remove(int position){
|
||||
return _collection.Remove(position);
|
||||
}
|
||||
public T Get(int position){
|
||||
if(position < 0 || position >= _collection.Count)
|
||||
return null;
|
||||
return _collection.Get(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;
|
||||
}
|
||||
public void Clear(){
|
||||
_collection = new SetGeneric<>(_pictureWidth * _pictureHeight);
|
||||
}
|
||||
}
|
164
src/Generics/AirplaneGenericStorage.java
Normal file
164
src/Generics/AirplaneGenericStorage.java
Normal file
@ -0,0 +1,164 @@
|
||||
package src.Generics;
|
||||
|
||||
import javax.xml.crypto.dsig.keyinfo.KeyValue;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import src.MovementStrategy.DrawningObjectAirplane;
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import src.DrawningObjects.ExtentionDrawningAirplane;
|
||||
public class AirplaneGenericStorage {
|
||||
public final HashMap<String, AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane>> _airplaneStorages;
|
||||
private static final char _separatorForKeyValue = '|';
|
||||
private final char _separatorRecords = ';';
|
||||
private static final char _separatorForObject = ':';
|
||||
|
||||
public boolean SaveData(File f) throws IOException {
|
||||
if(f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append("AirplaneStorage\n");
|
||||
for(Map.Entry<String, AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane>> record : _airplaneStorages.entrySet()){
|
||||
StringBuilder records = new StringBuilder();
|
||||
for(DrawningAirplane elem : record.getValue().GetAirplanes())
|
||||
{
|
||||
records.append(String.format("%s%c", ExtentionDrawningAirplane.GetDataForSave(elem, _separatorForObject), _separatorRecords));
|
||||
}
|
||||
data.append(String.format("%s%c%s\n", record.getKey(), _separatorForKeyValue, records.toString()));
|
||||
}
|
||||
if(data.length() == 0)
|
||||
return false;
|
||||
FileWriter writer = new FileWriter(f);
|
||||
writer.write(data.toString());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
return true;
|
||||
}
|
||||
public boolean LoadData(File f) throws FileNotFoundException {
|
||||
if(!f.exists())
|
||||
return false;
|
||||
StringBuilder bufferTextFromFile =new StringBuilder();
|
||||
Scanner s = new Scanner(f);
|
||||
while(s.hasNext())
|
||||
bufferTextFromFile.append(s.next() + "\n");
|
||||
s.close();
|
||||
var strs = bufferTextFromFile.toString().split("\n");
|
||||
if(strs == null || strs.length == 0)
|
||||
return false;
|
||||
if (!strs[0].startsWith("AirplaneStorage"))
|
||||
return false;
|
||||
_airplaneStorages.clear();
|
||||
for(String data : strs){
|
||||
String st = new String("\\" + Character.toString( _separatorForKeyValue));
|
||||
String[]record = data.split(st);
|
||||
if (record.length != 2)
|
||||
continue;
|
||||
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> collection =
|
||||
new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
String[] set = record[1].split(Character.toString(_separatorRecords));
|
||||
for(int i = set.length -1; i >=0; i--){
|
||||
String elem = set[i];
|
||||
DrawningAirplane airplane = ExtentionDrawningAirplane.CreateDrawningAirplane(elem,
|
||||
_separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (airplane != null)
|
||||
{
|
||||
if (!(collection.Insert(airplane)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
_airplaneStorages.put(record[0], collection);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean LoadCollection(File f) throws FileNotFoundException {
|
||||
if(!f.exists())
|
||||
return false;
|
||||
StringBuilder bufferTextFromFile =new StringBuilder();
|
||||
Scanner s = new Scanner(f);
|
||||
while(s.hasNext())
|
||||
bufferTextFromFile.append(s.next() + "\n");
|
||||
s.close();
|
||||
var strs = bufferTextFromFile.toString().split("\n");
|
||||
if(strs == null || strs.length == 0)
|
||||
return false;
|
||||
if (!strs[0].startsWith("AirplaneCollection"))
|
||||
return false;
|
||||
String collectionName = strs[1];
|
||||
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> collection = GetCollection(collectionName);
|
||||
if(collection == null)
|
||||
collection = new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
else
|
||||
collection.Clear();
|
||||
String[] airplanesInfo = strs[2].split(Character.toString(AirplaneGenericCollection._separatorRecords));
|
||||
for(int i = airplanesInfo.length-1; i >= 0; i--){
|
||||
String data = airplanesInfo[i];
|
||||
DrawningAirplane airplane = ExtentionDrawningAirplane.CreateDrawningAirplane(data,
|
||||
AirplaneGenericCollection._separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (airplane != null)
|
||||
{
|
||||
if (!(collection.Insert(airplane)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
AddSetFromFile(collectionName, collection);
|
||||
return true;
|
||||
}
|
||||
public List<String> Keys(){
|
||||
if(_airplaneStorages == null)
|
||||
return null;
|
||||
return _airplaneStorages.keySet().stream().collect(Collectors.toList());
|
||||
}
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
|
||||
public AirplaneGenericStorage(int pictureWidth, int pictureHeight){
|
||||
_airplaneStorages = new HashMap<>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
|
||||
public void AddSet(String name){
|
||||
if(_airplaneStorages.containsKey(name))
|
||||
return;
|
||||
_airplaneStorages.put(name, new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
|
||||
public void DelSet(String name, AirplaneTrashCollection<DrawningAirplane> trashBox){
|
||||
if(!_airplaneStorages.containsKey(name))
|
||||
return;
|
||||
AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> cur = _airplaneStorages.get(name);
|
||||
for(int i = 0; i < cur.Size(); i++)
|
||||
trashBox.Push(cur.Get(i));
|
||||
|
||||
_airplaneStorages.remove(name);
|
||||
}
|
||||
|
||||
public AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> Get(String name){
|
||||
if(!_airplaneStorages.containsKey(name))
|
||||
return null;
|
||||
return _airplaneStorages.get(name);
|
||||
}
|
||||
|
||||
public DrawningAirplane Get(String collectionName, int position){
|
||||
return _airplaneStorages.get(collectionName).Get(position);
|
||||
}
|
||||
|
||||
public AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> GetCollection(String collectionName){
|
||||
return _airplaneStorages.get(collectionName);
|
||||
}
|
||||
|
||||
public void AddSetFromFile(String name, AirplaneGenericCollection<DrawningAirplane, DrawningObjectAirplane> toAdd){
|
||||
if(_airplaneStorages.containsKey(name)){
|
||||
_airplaneStorages.remove(name);
|
||||
}
|
||||
_airplaneStorages.put(name, toAdd);
|
||||
}
|
||||
}
|
32
src/Generics/AirplaneTrashCollection.java
Normal file
32
src/Generics/AirplaneTrashCollection.java
Normal file
@ -0,0 +1,32 @@
|
||||
package src.Generics;
|
||||
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
|
||||
public class AirplaneTrashCollection <T extends DrawningAirplane>{
|
||||
Queue <T> _queue;
|
||||
|
||||
public AirplaneTrashCollection(){
|
||||
_queue = new ArrayDeque<>();
|
||||
}
|
||||
|
||||
public void Push(T airplane){
|
||||
_queue.add(airplane);
|
||||
}
|
||||
|
||||
public int GetSize(){
|
||||
return _queue.size();
|
||||
}
|
||||
|
||||
public DrawningAirplane Pop(){
|
||||
if(_queue.size() ==0)
|
||||
return null;
|
||||
|
||||
return _queue.remove();
|
||||
}
|
||||
|
||||
public T GetTop(){
|
||||
return _queue.peek();
|
||||
}
|
||||
}
|
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;
|
||||
}
|
||||
}
|
48
src/Generics/SetGeneric.java
Normal file
48
src/Generics/SetGeneric.java
Normal file
@ -0,0 +1,48 @@
|
||||
package src.Generics;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SetGeneric <T extends Object>{
|
||||
private final List<T> _places;
|
||||
public int Count;
|
||||
private final int _maxCount;
|
||||
public SetGeneric(int count){
|
||||
_maxCount = count;
|
||||
_places = new ArrayList<>();
|
||||
}
|
||||
public boolean Insert(T airplane){
|
||||
if(_places.size() == _maxCount)
|
||||
return false;
|
||||
Insert(airplane, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean Insert(T airplane, int position){
|
||||
if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount))
|
||||
return false;
|
||||
_places.add(position, airplane);
|
||||
Count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean Remove(int position){
|
||||
if(!(position >= 0 && position < _places.size()))
|
||||
return false;
|
||||
_places.remove(position);
|
||||
return true;
|
||||
}
|
||||
public T Get(int position){
|
||||
if(!(position >= 0 && position < _places.size()))
|
||||
return null;
|
||||
return (T)_places.get(position);
|
||||
}
|
||||
public ArrayList<T> GetAirplanes(int maxAirplanes){
|
||||
ArrayList<T> toRet = new ArrayList<>();
|
||||
for(int i = 0; i < _places.size(); i++){
|
||||
toRet.add(_places.get(i));
|
||||
if(i == maxAirplanes)
|
||||
return toRet;
|
||||
}
|
||||
return toRet;
|
||||
}
|
||||
}
|
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 {
|
||||
FormAirplaneCollection form = new FormAirplaneCollection();
|
||||
}
|
||||
}
|
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