Классы объекта и отрисовки + перечисления

This commit is contained in:
prodigygirl 2022-10-07 19:26:22 +04:00
parent 4a49097d98
commit 620e76136b
5 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,6 @@
public enum Direction {
Up,
Down,
Left,
Right
}

View File

@ -0,0 +1,119 @@
import java.awt.*;
import java.util.Random;
public class DrawingArmoredCar {
public EntityArmoredCar armoredCar;
private float startPosX;
private float startPosY;
private int pictureWidth;
private int pictureHeight;
private static final int carWidth = 80;
private static final int carHeight = 50;
private DrawningCaterpillar drawningCaterpillar;
public void Init(int speed, float weight, Color bodyColor) {
this.armoredCar = new EntityArmoredCar();
this.armoredCar.init(speed, weight, bodyColor);
Random r = new Random();
this.drawningCaterpillar = new DrawningCaterpillar();
this.drawningCaterpillar.Init(r.nextInt(4) + 3, bodyColor);
}
public void SetPosition(int x, int y, int width, int height) {
if (x > 0 && y > 0 && x + carWidth < width && y + carHeight < height) {
startPosX = x;
startPosY = y;
pictureWidth = width;
pictureHeight = height;
}
}
public void MoveTransport(Direction direction) {
if (pictureWidth < 1|| pictureHeight < 1)
{
return;
}
switch (direction)
{
// вправо
case Right:
if (startPosX + carWidth + armoredCar.step < pictureWidth)
{
startPosX += armoredCar.step;
}
break;
//влево
case Left:
if (startPosX - armoredCar.step > 0)
{
startPosX -= armoredCar.step;
}
break;
//вверх
case Up:
if (startPosY - armoredCar.step > 0)
{
startPosY -= armoredCar.step;
}
break;
//вниз
case Down:
if (startPosY + carHeight + armoredCar.step < pictureHeight)
{
startPosY += armoredCar.step;
}
break;
}
}
public void DrawTransport(Graphics2D g2d)
{
if (startPosX < 0 || startPosY < 0
|| pictureHeight < 1 || pictureWidth < 1)
{
return;
}
// отрисовка корпуса и гусеницы
g2d.setPaint(armoredCar.getBodyColor());
g2d.fillRect((int ) startPosX + 20, (int) startPosY, 40, 20);
g2d.setPaint(Color.LIGHT_GRAY);
g2d.fillRect((int ) startPosX, (int ) startPosY + 20, 80, 20);
g2d.fillOval((int ) startPosX, (int ) startPosY + 30, 20, 20);
g2d.fillOval((int ) startPosX + 80 - 20, (int ) startPosY + 30, 20, 20);
g2d.fillRect((int ) startPosX + 15, (int ) startPosY + 20, 60, 30);
// отрисовка катков в гусенице
drawningCaterpillar.DrawCaterpillar(g2d, (int)startPosX, (int)startPosY);
}
public void ChangeBorders(int width, int height)
{
pictureWidth = width;
pictureHeight = height;
if (pictureWidth <= carWidth || pictureHeight <= carHeight)
{
pictureWidth = 0;
pictureHeight = 0;
return;
}
if (startPosX + carWidth > pictureWidth)
{
startPosX = pictureWidth - carWidth;
}
if (startPosY + carHeight > pictureHeight)
{
startPosY = pictureHeight - carHeight;
}
}
}

View File

@ -0,0 +1,38 @@
import java.awt.*;
public class DrawningCaterpillar {
private NumRinks numRinks = NumRinks.Four;
private Color color;
public void Init(int n, Color color) {
setNumRinks(n);
this.color = color;
}
public void setNumRinks(int n) {
switch (n) {
case 4 -> numRinks = NumRinks.Four;
case 5 -> numRinks = NumRinks.Five;
case 6 -> numRinks = NumRinks.Six;
default -> {
break;
}
}
}
public void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY)
{
color = color != null ? color : Color.YELLOW;
g2d.setPaint(color);
int size = numRinks == NumRinks.Four ? 15 : 10;
int dist = numRinks == NumRinks.Four ? 20 : 13;
startPosX = numRinks == NumRinks.Five ? startPosX + 5 : startPosX;
for (int i = 0; i < numRinks.val(); i++) {
g2d.fillOval(startPosX + dist * i, startPosY + 30, size, size);
}
}
}

View File

@ -0,0 +1,40 @@
import java.awt.*;
public class EntityArmoredCar {
private int speed;
private float weight;
private Color bodyColor;
public float step;
public void init(int speed, float weight, Color bodyColor) {
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
this.step = speed * 100 / weight;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public Color getBodyColor() {
return bodyColor;
}
public void setBodyColor(Color bodyColor) {
this.bodyColor = bodyColor;
}
}

View File

@ -0,0 +1,6 @@
public enum NumRinks {
Four{public int val() {return 4;}},
Five{public int val() {return 5;}},
Six{public int val() {return 6;}};
public abstract int val();
}