Compare commits
No commits in common. "lab2_hard" and "main" have entirely different histories.
@ -1,7 +0,0 @@
|
||||
package src;
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
@ -1,193 +0,0 @@
|
||||
package src.DrawningObjects;
|
||||
|
||||
import src.DirectionType;
|
||||
import src.Entities.EntityAirplane;
|
||||
|
||||
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;
|
||||
if (x < 0 || y < 0 || x + _airplaneWidth >= _pictureWidth || y + _airplaneHeight >= _pictureHeight) {
|
||||
x = y = 10;
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
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);
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,193 +0,0 @@
|
||||
package src;
|
||||
|
||||
import src.DrawningObjects.DrawningAirplane;
|
||||
|
||||
import src.MovementStrategy.*;
|
||||
import src.DrawningObjects.DrawningAirplaneWithRadar;
|
||||
|
||||
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 void Draw(){
|
||||
canv.repaint();
|
||||
}
|
||||
|
||||
public FormAirplaneWithRadar(){
|
||||
JFrame AirplaneFrame =new JFrame ();
|
||||
JButton buttonCreate = new JButton("Создать");
|
||||
JButton buttonCreateLocomotive = new JButton("Создать самолет с радаром");
|
||||
JButton buttonStep = 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();
|
||||
DrawningAirplane = new DrawningAirplane(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
pictureBoxWidth, pictureBoxHeight);
|
||||
canv.DrawningAirplane = DrawningAirplane;
|
||||
comboBoxStrategy.enable(true);
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
);
|
||||
buttonCreateLocomotive.addActionListener(
|
||||
new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e){
|
||||
Random random = new Random();
|
||||
DrawningAirplane = new DrawningAirplaneWithRadar(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
||||
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.EXIT_ON_CLOSE);
|
||||
AirplaneFrame.setLayout(null);
|
||||
canv = new Canvas();
|
||||
canv.setSize(pictureBoxWidth, pictureBoxHeight);
|
||||
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.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();
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +0,0 @@
|
||||
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 {
|
||||
FormAirplaneWithRadar form = new FormAirplaneWithRadar();
|
||||
}
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package src.MovementStrategy;
|
||||
|
||||
import src.DirectionType;
|
||||
|
||||
public interface IMoveableObject {
|
||||
public ObjectParameters GetObjectParameters();
|
||||
public int GetStep();
|
||||
boolean CheckCanMove(DirectionType direction);
|
||||
void MoveObject(DirectionType direction);
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,52 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package src.MovementStrategy;
|
||||
|
||||
public enum Status {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package src;
|
||||
public enum NumberType {
|
||||
Ten,
|
||||
Twenty,
|
||||
Thirty
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user