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

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,38 +1,50 @@
import java.awt.*;
public class DrawingEngines {
public class DrawingEngines implements IDrawningEngines{
private DirectionEnginesOnStormtrooper enginesCount;
public void SetNewEngines(int countEngines){
if (countEngines == 4) {
enginesCount = DirectionEnginesOnStormtrooper.FOUR;
} else if (countEngines == 6) {
enginesCount = DirectionEnginesOnStormtrooper.SIX;
}
else {
enginesCount = DirectionEnginesOnStormtrooper.TWO;
}
public DrawingEngines(int count) {
SetNewEngines(count);
}
public void Draw(Graphics2D g, int x, int y, Color color) {
g.setColor(color != null ? color : Color.BLACK);
switch (enginesCount) {
case TWO:
g.fillRect(x + 50, y, 20, 5);
g.fillRect(x + 50, y+95, 20, 5);
break;
case FOUR:
g.fillRect(x + 50, y, 20, 5);
g.fillRect(x + 50, y+10, 20, 5);
g.fillRect(x + 50, y+85, 20, 5);
g.fillRect(x + 50, y+95, 20, 5);
break;
case SIX:
g.fillRect(x + 50, y, 20, 5);
g.fillRect(x + 50, y+10, 20, 5);
g.fillRect(x + 50, y+20, 20, 5);
g.fillRect(x + 50, y+75, 20, 5);
g.fillRect(x + 50, y+85, 20, 5);
g.fillRect(x + 50, y+95, 20, 5);
break;
}
@Override
public void Draw(Graphics g, int x, int y, Color bodycolor) {
g.setColor(bodycolor);
switch (enginesCount) {
case TWO:
g.fillRect(x + 50, y, 20, 5);
g.fillRect(x + 50, y+95, 20, 5);
break;
case FOUR:
g.fillRect(x + 50, y, 20, 5);
g.fillRect(x + 50, y+10, 20, 5);
g.fillRect(x + 50, y+85, 20, 5);
g.fillRect(x + 50, y+95, 20, 5);
break;
case SIX:
g.fillRect(x + 50, y, 20, 5);
g.fillRect(x + 50, y+10, 20, 5);
g.fillRect(x + 50, y+20, 20, 5);
g.fillRect(x + 50, y+75, 20, 5);
g.fillRect(x + 50, y+85, 20, 5);
g.fillRect(x + 50, y+95, 20, 5);
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 EntityStormtrooper Stormtrooper;
public EntityStormtrooper getStormtrooper() {
return Stormtrooper;
}
public DrawingEngines drawingEngines;
private IDrawningEngines iDrawingEngines;
public float _startPosX;
public float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private static final int _StormWidth = 135;
private static final int _StormHeight = 100;
private int _StormWidth = 135;
private int _StormHeight = 100;
public void Init(int speed, float weight, Color bodyColor, int numberOfEngines){
Stormtrooper = new EntityStormtrooper();
drawingEngines = new DrawingEngines();
Stormtrooper.Init(speed, weight, bodyColor);
System.out.println(numberOfEngines + "");
drawingEngines.SetNewEngines(numberOfEngines);
public DrawingStormtrooper(int speed, float weight, Color bodyColor){
Random random = new Random();
int[] ArrayEngines = new int[]{2, 4, 6};
switch (random.nextInt(3)){
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() {
return Stormtrooper;
protected DrawingStormtrooper(int speed, float weight, Color bodyColor, int StormWidth, int StormHeight)
{
this (speed, weight, bodyColor);
_StormWidth = StormWidth;
_StormHeight = StormHeight;
}
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);
//отрисовка крыла
@ -148,7 +167,7 @@ public class DrawingStormtrooper {
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);
@ -177,4 +196,8 @@ public class DrawingStormtrooper {
_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;
}
public void Init(int speed, float weight, Color bodyColor){
public EntityStormtrooper(int speed, float weight, Color bodyColor){
Random rnd = new Random();
Speed = speed <= 0 ? rnd.nextInt(50 + 1) +50 : speed; //генерация в диапазоне от 50 до 100
Weight = weight <= 0 ? rnd.nextInt(50 + 1) +50 : weight;