Переделал реализацию отрисовки и вроде вообще всё сделал

This commit is contained in:
antoc0der 2023-10-27 22:28:14 +03:00
parent 88a324c1b6
commit 19b99066a2
20 changed files with 809 additions and 308 deletions

View File

@ -1,3 +1,4 @@
package src;
public enum DirectionType {
Up,
Down,

View File

@ -1,171 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningAirplaneWithRadar {
JPanel AirplaneWithRadarPanel;
private EntityAirplaneWithRadar EntityAirplaneWithRadar;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX = 0;
private int _startPosY = 0;
private int _airplaneWidth = 200;
private int _airplaneHeight = 78;
private DrawningIlluminators DrawningIlluminators;
public EntityAirplaneWithRadar EntityAirplaneWithRadar(){
return EntityAirplaneWithRadar;
}
public boolean Init(int speed, double weight, Color bodyColor, Color illuminatorColor, Color additionalColor, int illuminatorNumb,
int width, int height, boolean radar, boolean dopBak, JPanel airplaneWithRadarPanel){
if(width <= _airplaneWidth || height <= _airplaneHeight)
return false;
_startPosY=0;
_startPosX = 0;
airplaneWithRadarPanel.setSize(width, height);
AirplaneWithRadarPanel = airplaneWithRadarPanel;
airplaneWithRadarPanel.paint(AirplaneWithRadarPanel.getGraphics());
_pictureWidth = width;
_pictureHeight = height;
EntityAirplaneWithRadar = new EntityAirplaneWithRadar();
EntityAirplaneWithRadar.Init(speed, weight, bodyColor, illuminatorColor, additionalColor, illuminatorNumb, radar, dopBak);
DrawningIlluminators = new DrawningIlluminators();
DrawningIlluminators.Init(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY,illuminatorColor,airplaneWithRadarPanel);
Random rand = new Random();
DrawningIlluminators.ChangeIlluminatorNumb(rand.nextInt(1, 5));
return true;
}
public void SetPosition(int x, int y){
if (x < 0 || y < 0 || x + _airplaneWidth >= _pictureWidth || y + _airplaneHeight >= _pictureHeight)
{
x = y = 10;
_startPosX = x;
_startPosY = y;
}
_startPosX = x;
_startPosY = y;
DrawningIlluminators.CurX = _startPosX;
DrawningIlluminators.CurY = _startPosY;
}
public void MoveTransport(DirectionType direction){
if (EntityAirplaneWithRadar == null)
return;
AirplaneWithRadarPanel.paint(AirplaneWithRadarPanel.getGraphics());
switch (direction)
{
case Left:
if (_startPosX - EntityAirplaneWithRadar.Step() >= 0)
_startPosX -= (int)EntityAirplaneWithRadar.Step();
else
_startPosX = 0;
break;
case Up:
if (_startPosY - EntityAirplaneWithRadar.Step() >= 0)
_startPosY -= (int)EntityAirplaneWithRadar.Step();
else
_startPosY = 0;
break;
case Right:
if (_startPosX + EntityAirplaneWithRadar.Step() + _airplaneWidth < _pictureWidth)
_startPosX += (int)EntityAirplaneWithRadar.Step();
else
_startPosX = _pictureWidth - _airplaneWidth;
break;
case Down:
if (_startPosY + EntityAirplaneWithRadar.Step() + _airplaneHeight < _pictureHeight)
_startPosY += (int)EntityAirplaneWithRadar.Step();
else
_startPosY = _pictureHeight - _airplaneHeight;
break;
}
DrawningIlluminators.CurX = _startPosX;
DrawningIlluminators.CurY = _startPosY;
}
public void DrawAirplaneWithRadar(){
Graphics2D g2d = (Graphics2D)AirplaneWithRadarPanel.getGraphics();
if (EntityAirplaneWithRadar == null)
return;
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(3));
// корпус
g2d.setColor(EntityAirplaneWithRadar.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);
//дополнительный бак
if (EntityAirplaneWithRadar().DopBak()) {
g2d.setColor(EntityAirplaneWithRadar.AdditionalColor());
g2d.fillOval( _startPosX, _startPosY + 45, 40, 20);
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX, _startPosY + 45, 40, 20);
}
//радар
if (EntityAirplaneWithRadar.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(EntityAirplaneWithRadar.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);
}
DrawningIlluminators.DrawIlluminators();
}
}

View File

@ -0,0 +1,187 @@
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,2);
if(variant ==0){
DrawningIlluminators = new DrawningIlluminators(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
} else{
if(variant ==1){
DrawningIlluminators = new DrawningIlluminatorsQuad(_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,2);
if(variant ==0){
DrawningIlluminators = new DrawningIlluminators(_airplaneWidth, _airplaneHeight,_startPosX,_startPosY);
} else{
if(variant ==1){
DrawningIlluminators = new DrawningIlluminatorsQuad(_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);
}
}

View File

@ -0,0 +1,54 @@
package src.DrawningObjects;
import src.Entities.EntityAirplane;
import src.Entities.EntityAirplaneWithRadar;
import javax.swing.*;
import java.awt.*;
public class DrawningAirplaneWithRadar extends DrawningAirplane {
public DrawningAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor,
boolean radar, boolean dopBak,int width,int height ){
super(speed,weight,bodyColor,width,height);
if(EntityAirplane!=null){
EntityAirplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, additionalColor, radar,dopBak);
}
}
@Override
public void DrawAirplane(Graphics2D g2d){
if(!(EntityAirplane instanceof EntityAirplaneWithRadar)){
return;
}
super.DrawAirplane(g2d);
EntityAirplaneWithRadar _airplaneWithRadar =(EntityAirplaneWithRadar) EntityAirplane;
//дополнительный бак
if (_airplaneWithRadar.DopBak()) {
g2d.setColor(_airplaneWithRadar.AdditionalColor());
g2d.fillOval( _startPosX, _startPosY + 45, 40, 20);
g2d.setColor(Color.BLACK);
g2d.drawOval(_startPosX, _startPosY + 45, 40, 20);
}
//радар
if (_airplaneWithRadar.Radar())
{
g2d.setColor(Color.BLACK);
g2d.drawLine(_startPosX + 60, _startPosY + 25, _startPosX + 60, _startPosY + 15);
g2d.drawLine( _startPosX + 60, _startPosY + 15, _startPosX + 67, _startPosY + 11);
g2d.setColor(_airplaneWithRadar.AdditionalColor());
Point point7 = new Point(_startPosX + 60, _startPosY + 15);
Point point8 = new Point(_startPosX + 60, _startPosY + 5);
Point point9 = new Point(_startPosX + 70, _startPosY + 25);
int[] curvePoints3x =
{
_startPosX + 60,
_startPosX + 60,
_startPosX + 70,
};
int[] curvePoints3y =
{
_startPosY + 15,
_startPosY + 5,
_startPosY + 25,
};
g2d.fillPolygon(curvePoints3x, curvePoints3y,curvePoints3x.length);
}
}
}

View File

@ -1,19 +1,25 @@
package src.DrawningObjects;
import src.NumberType;
import javax.swing.*;
import java.awt.*;
public class DrawningIlluminators {
JPanel AirplaneWithRadarPanel;
public class DrawningIlluminators implements IDraw{
private NumberType IlluminatorNumb;
private Color IlluminatorColor;
private int Width, Height;
public int CurX, CurY;
boolean Init(int width, int height, int curX, int curY, Color illuminatorColor, JPanel airplaneWithRadarPanel){
protected int CurX, CurY;
public DrawningIlluminators(int width, int height, int curX, int curY){
Width = width;
Height = height;
CurX = curX;
CurY = curY;
IlluminatorColor = illuminatorColor;
AirplaneWithRadarPanel = airplaneWithRadarPanel;
return true;
}
public void ChangeX(int x){
CurX = x;
}
public void ChangeY(int y){
CurY = y;
}
public void ChangeIlluminatorNumb(int x){
if(x <= 2)
@ -26,9 +32,8 @@ public class DrawningIlluminators {
public NumberType IlluminatorNumb(){
return IlluminatorNumb;
}
public void DrawIlluminators(){
Graphics2D g2d = (Graphics2D)AirplaneWithRadarPanel.getGraphics();
g2d.setColor(IlluminatorColor);
public void DrawIlluminators(Graphics2D g2d){
g2d.setColor(Color.BLUE);
int x = CurX;
int y = CurY;
for( int i =0; i<10;i++){

View File

@ -0,0 +1,61 @@
package src.DrawningObjects;
import src.NumberType;
import javax.swing.*;
import java.awt.*;
public class DrawningIlluminatorsQuad implements IDraw {
private NumberType IlluminatorNumb;
private Color IlluminatorColor;
private int Width, Height;
protected int CurX, CurY;
public DrawningIlluminatorsQuad(int width, int height, int curX, int curY){
Width = width;
Height = height;
CurX = curX;
CurY = curY;
}
public void ChangeIlluminatorNumb(int x){
if(x <= 2)
IlluminatorNumb = NumberType.Ten;
if(x == 3)
IlluminatorNumb = NumberType.Twenty;
if(x >= 4)
IlluminatorNumb = NumberType.Thirty;
}
public void ChangeX(int x){
CurX = x;
}
public void ChangeY(int y){
CurY = y;
}
public NumberType IlluminatorNumb(){
return IlluminatorNumb;
}
public void DrawIlluminators(Graphics2D g2d) {
g2d.setColor(Color.BLUE);
int x = CurX;
int y = CurY;
for (int i = 0; i < 10; i++) {
g2d.fillRect(x + 34, y + 29, 5, 5);
x += 7;
}
//20 иллюминаторов
if (IlluminatorNumb == NumberType.Twenty || IlluminatorNumb == NumberType.Thirty) {
x = CurX;
for (int i = 0; i < 10; i++) {
g2d.fillRect(x + 34, y + 37, 5, 5);
x += 7;
}
}
//30 иллюминаторов
if (IlluminatorNumb == NumberType.Thirty) {
x = CurX;
for (int i = 0; i < 10; i++) {
g2d.fillRect(x + 34, y + 45, 5, 5);
x += 7;
}
}
}
}

View 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);
}

View File

@ -0,0 +1,25 @@
package src.Entities;
import java.awt.*;
public class EntityAirplane {
private int Speed;
private double Weight, Step;
private Color BodyColor;
public int Speed(){
return Speed;
}
public double Weight(){
return Weight;
}
public double Step(){
return Step;
}
public Color BodyColor(){
return BodyColor;
}
public EntityAirplane(int speed, double weight, Color bodyColor){
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,18 @@
package src.Entities;
import java.awt.*;
public class EntityAirplaneWithRadar extends EntityAirplane {
private Color AdditionalColor;
private boolean Radar;
private boolean DopBak;
public Color AdditionalColor(){return AdditionalColor;}
public boolean Radar(){return Radar;}
public boolean DopBak(){return DopBak;}
public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor,
boolean radar, boolean dopBak ){
super(speed, weight, bodyColor);
AdditionalColor=additionalColor;
Radar =radar;
DopBak=dopBak;
}
}

View File

@ -1,43 +0,0 @@
import java.awt.*;
public class EntityAirplaneWithRadar {
private int Speed, IlluminatorNumb;
private double Weight, Step;
private Color BodyColor, AdditionalColor, IlluminatorColor;
private boolean Radar;
private boolean DopBak;
public int Speed(){
return Speed;
}
public int IlluminatorNumb(){
return IlluminatorNumb;
}
public double Weight(){
return Weight;
}
public double Step(){
return Step;
}
public Color BodyColor(){
return BodyColor;
}
public Color AdditionalColor(){
return AdditionalColor;
}
public Color IlluminatorColor(){
return IlluminatorColor;
}
public boolean Radar(){return Radar;}
public boolean DopBak(){return DopBak;}
public void Init(int speed, double weight, Color bodyColor, Color illuminatorColor, Color additionalColor, int illuminatorNumb,
boolean radar, boolean dopBak ){
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
IlluminatorColor=illuminatorColor;
AdditionalColor = additionalColor;
IlluminatorNumb = illuminatorNumb;
Radar = radar;
DopBak = dopBak;
}
}

View File

@ -0,0 +1,193 @@
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 MonorailFrame =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();
}
});
MonorailFrame.setSize (900, 500);
MonorailFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
MonorailFrame.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);
MonorailFrame.add(canv);
MonorailFrame.add(buttonCreate);
MonorailFrame.add(buttonCreateLocomotive);
MonorailFrame.add(UpButton);
MonorailFrame.add(DownButton);
MonorailFrame.add(LeftButton);
MonorailFrame.add(RightButton);
MonorailFrame.add(comboBoxStrategy);
MonorailFrame.add(buttonStep);
MonorailFrame.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();
}
}

View File

@ -1,92 +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) {
JFrame AirplaneWithRadarFrame = new JFrame();
JPanel AirplaneWithRadarPanel = new JPanel();
AirplaneWithRadarFrame.setLayout(new BorderLayout());
AirplaneWithRadarFrame.setSize(900, 500);
AirplaneWithRadarFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AirplaneWithRadarFrame.setLayout(new BorderLayout(1,1));
AirplaneWithRadarFrame.setResizable(false);
DrawningAirplaneWithRadar DrawningEntityAirplaneWithRadar = new DrawningAirplaneWithRadar();
AirplaneWithRadarPanel.setLayout(null);
BufferedImage RightIcon = ImageIO.read(new File("RightButton.png"));
BufferedImage LeftIcon = ImageIO.read(new File("LeftButton.png"));
BufferedImage UpIcon = ImageIO.read(new File("UpButton.png"));
BufferedImage DownIcon = ImageIO.read(new File("DownButton.png"));
JButton RightButton = new JButton(new ImageIcon(RightIcon));
JButton LeftButton = new JButton(new ImageIcon(LeftIcon));
JButton UpButton = new JButton(new ImageIcon(UpIcon));
JButton DownButton = new JButton(new ImageIcon(DownIcon));
JButton CreateButton = new JButton();
CreateButton.setText("Создать");
CreateButton.setBounds(12, 401, 90, 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);
AirplaneWithRadarPanel.add(CreateButton);
AirplaneWithRadarPanel.add(RightButton);
AirplaneWithRadarPanel.add(LeftButton);
AirplaneWithRadarPanel.add(UpButton);
AirplaneWithRadarPanel.add(DownButton);
AirplaneWithRadarFrame.add(AirplaneWithRadarPanel, BorderLayout.CENTER);
Random random = new Random();
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningEntityAirplaneWithRadar.Init(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)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
random.nextInt(1, 4)*10,
AirplaneWithRadarPanel.getWidth(), AirplaneWithRadarPanel.getHeight(), random.nextBoolean(), random.nextBoolean(), AirplaneWithRadarPanel);
DrawningEntityAirplaneWithRadar.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
//просто коммент
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningEntityAirplaneWithRadar.EntityAirplaneWithRadar() == null)
return;
DrawningEntityAirplaneWithRadar.MoveTransport(DirectionType.Right);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningEntityAirplaneWithRadar.EntityAirplaneWithRadar() == null)
return;
DrawningEntityAirplaneWithRadar.MoveTransport(DirectionType.Left);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningEntityAirplaneWithRadar.EntityAirplaneWithRadar() == null)
return;
DrawningEntityAirplaneWithRadar.MoveTransport(DirectionType.Up);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningEntityAirplaneWithRadar.EntityAirplaneWithRadar() == null)
return;
DrawningEntityAirplaneWithRadar.MoveTransport(DirectionType.Down);
DrawningEntityAirplaneWithRadar.DrawAirplaneWithRadar();
}
});
AirplaneWithRadarFrame.setVisible(true);
public class Main {
public static void main(String[] args) throws IOException {
FormAirplaneWithRadar form = new FormAirplaneWithRadar();
}
}

View 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;
}
}

View 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);
}
}

View 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);
}

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

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

View 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;
}
}

View File

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

View File

@ -1,3 +1,4 @@
package src;
public enum NumberType {
Ten,
Twenty,