Создание конструкторов.

This commit is contained in:
Артем Харламов 2022-12-20 17:17:11 +04:00
parent f4320eecc1
commit ac0f95e205
3 changed files with 79 additions and 44 deletions

View File

@ -1,19 +1,13 @@
import java.awt.*; import java.awt.*;
public class DrawingEngines { public class DrawingEngines implements IDrawningEngines{
private DirectionEnginesOnStormtrooper enginesCount; private DirectionEnginesOnStormtrooper enginesCount;
public void SetNewEngines(int countEngines){ public DrawingEngines(int count) {
if (countEngines == 4) { SetNewEngines(count);
enginesCount = DirectionEnginesOnStormtrooper.FOUR;
} else if (countEngines == 6) {
enginesCount = DirectionEnginesOnStormtrooper.SIX;
} }
else { @Override
enginesCount = DirectionEnginesOnStormtrooper.TWO; public void Draw(Graphics g, int x, int y, Color bodycolor) {
} g.setColor(bodycolor);
}
public void Draw(Graphics2D g, int x, int y, Color color) {
g.setColor(color != null ? color : Color.BLACK);
switch (enginesCount) { switch (enginesCount) {
case TWO: case TWO:
g.fillRect(x + 50, y, 20, 5); g.fillRect(x + 50, y, 20, 5);
@ -35,4 +29,22 @@ public class DrawingEngines {
break; break;
} }
} }
@Override
public void SetNewEngines(int count) {
switch(count)
{
case 2:
enginesCount = DirectionEnginesOnStormtrooper.TWO;
break;
case 4:
enginesCount = DirectionEnginesOnStormtrooper.FOUR;
break;
case 6:
enginesCount = DirectionEnginesOnStormtrooper.SIX;
break;
default:
break;
}
}
} }

View File

@ -3,24 +3,42 @@ import java.util.Random;
public class DrawingStormtrooper { public class DrawingStormtrooper {
public EntityStormtrooper Stormtrooper; public EntityStormtrooper Stormtrooper;
public EntityStormtrooper getStormtrooper() {
return Stormtrooper;
}
public DrawingEngines drawingEngines; public DrawingEngines drawingEngines;
private IDrawningEngines iDrawingEngines;
public float _startPosX; public float _startPosX;
public float _startPosY; public float _startPosY;
private Integer _pictureWidth = null; private Integer _pictureWidth = null;
private Integer _pictureHeight = null; private Integer _pictureHeight = null;
private static final int _StormWidth = 135; private int _StormWidth = 135;
private static final int _StormHeight = 100; private int _StormHeight = 100;
public void Init(int speed, float weight, Color bodyColor, int numberOfEngines){ public DrawingStormtrooper(int speed, float weight, Color bodyColor){
Stormtrooper = new EntityStormtrooper(); Random random = new Random();
drawingEngines = new DrawingEngines(); int[] ArrayEngines = new int[]{2, 4, 6};
Stormtrooper.Init(speed, weight, bodyColor);
System.out.println(numberOfEngines + ""); switch (random.nextInt(3)){
drawingEngines.SetNewEngines(numberOfEngines); case 0:
iDrawingEngines = new DrawingEngines(ArrayEngines[random.nextInt(0, 3)]);
break;
case 1:
iDrawingEngines = new DrawningOvalEngines(ArrayEngines[random.nextInt(0, 3)]);
break;
case 2:
iDrawingEngines = new DrawningTriangleEngines(ArrayEngines[random.nextInt(0, 3)]);
break;
}
Stormtrooper = new EntityStormtrooper(speed,weight, bodyColor);
} }
public EntityStormtrooper getStormtrooper() { protected DrawingStormtrooper(int speed, float weight, Color bodyColor, int StormWidth, int StormHeight)
return Stormtrooper; {
this (speed, weight, bodyColor);
_StormWidth = StormWidth;
_StormHeight = StormHeight;
} }
public void SetPosition(int x, int y, int width, int height){ public void SetPosition(int x, int y, int width, int height){
@ -86,6 +104,7 @@ public class DrawingStormtrooper {
//фюзеляж самолёта //фюзеляж самолёта
g.setColor(Color.BLACK);
g.fillRect(_startPosXInt + 20, _startPosYInt + 40, 100, 20); g.fillRect(_startPosXInt + 20, _startPosYInt + 40, 100, 20);
//отрисовка крыла //отрисовка крыла
@ -148,7 +167,7 @@ public class DrawingStormtrooper {
g.fillPolygon(stabX, stabY, stabX.length); g.fillPolygon(stabX, stabY, stabX.length);
//отрисовка двигателей //отрисовка двигателей
drawingEngines.Draw(g, (int) _startPosX, (int) _startPosY,Stormtrooper.getBodyColor()); iDrawingEngines.Draw(g, (int) _startPosX, (int) _startPosY, Stormtrooper.getBodyColor());
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
@ -177,4 +196,8 @@ public class DrawingStormtrooper {
_startPosY = _pictureHeight - _StormHeight; _startPosY = _pictureHeight - _StormHeight;
} }
} }
public float[] GetCurrentPosition()
{
return new float[]{_startPosX, _startPosX + _StormWidth, _startPosY, _startPosY + _StormHeight};
}
} }

View File

@ -22,7 +22,7 @@ public class EntityStormtrooper {
return BodyColor; return BodyColor;
} }
public void Init(int speed, float weight, Color bodyColor){ public EntityStormtrooper(int speed, float weight, Color bodyColor){
Random rnd = new Random(); Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50 + 1) +50 : speed; //генерация в диапазоне от 50 до 100 Speed = speed <= 0 ? rnd.nextInt(50 + 1) +50 : speed; //генерация в диапазоне от 50 до 100
Weight = weight <= 0 ? rnd.nextInt(50 + 1) +50 : weight; Weight = weight <= 0 ? rnd.nextInt(50 + 1) +50 : weight;