работа 2 сдана

This commit is contained in:
bekodeg 2023-11-22 09:18:12 +04:00
parent 0d75f13101
commit f9a8216e8c
9 changed files with 151 additions and 43 deletions

View File

@ -8,13 +8,13 @@ import java.awt.*;
public class DrawningElectricLocomotive extends DrawningLocomotive {
public DrawningElectricLocomotive(int speed, double weight,
Color bodyColor, Color additionalColor,
int countWheels,
int typeWheels, int countWheels,
boolean horns, boolean battery,
int width, int height){
super(speed, weight, bodyColor, countWheels, width, height, 120, 70);
super(speed, weight, bodyColor, typeWheels, countWheels, width, height, 120, 70);
if (entityLocomotive != null){
entityLocomotive = new EntityElectricLocomotive(
speed, weight, bodyColor, additionalColor, countWheels, horns, battery);
speed, weight, bodyColor, additionalColor, horns, battery);
}
}
@Override

View File

@ -11,9 +11,10 @@ public class DrawningLocomotive {
protected int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected int _locomotiveWidth = 120;
protected int _locomotiveHeight = 70;
protected int _locomotiveWidth = 140;
protected int _locomotiveHeight = 75;
protected IDrawWheels wheels;
public int GetPosX() {
return _startPosX;
@ -32,18 +33,33 @@ public class DrawningLocomotive {
}
public DrawningLocomotive(int speed, double weight,
Color bodyColor, int countWheels,
Color bodyColor,
int typeWheels, int countWheels,
int width, int height) {
if (width < _locomotiveWidth || height < _locomotiveHeight) {
return;
}
_pictureWidth = width;
_pictureHeight = height;
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor, countWheels);
switch (typeWheels){
case 0:
wheels = new DrawningWheels();
break;
case 1:
wheels = new DrawningWheelsPolygon();
break;
case 2:
wheels = new DrawningWheelsStar();
break;
}
wheels.setWheelsCount(countWheels);
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
public DrawningLocomotive(int speed, double weight,
Color bodyColor, int countWheels,
Color bodyColor,
int typeWheels, int countWheels,
int width, int height,
int locomotiveWidth, int locomotiveHeight) {
if (width < locomotiveWidth || height < locomotiveHeight) {
@ -53,7 +69,20 @@ public class DrawningLocomotive {
_pictureHeight = height;
_locomotiveWidth = locomotiveWidth;
_locomotiveHeight = locomotiveHeight;
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor, countWheels);
switch (typeWheels){
case 0:
wheels = new DrawningWheels();
break;
case 1:
wheels = new DrawningWheelsPolygon();
break;
case 2:
wheels = new DrawningWheelsStar();
break;
}
wheels.setWheelsCount(countWheels);
entityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
public void SetPosition(int x, int y) {
@ -75,8 +104,8 @@ public class DrawningLocomotive {
int[] posX = {
_startPosX,
_startPosX + 10,
_startPosX + 120,
_startPosX + 120,
_startPosX + 140,
_startPosX + 140,
_startPosX
};
@ -100,7 +129,7 @@ public class DrawningLocomotive {
g.fillRect(_startPosX + 95, _startPosY + 24, 11, 11);
// колёса
entityLocomotive.DrawningWheels.DrawWheels(g, _startPosX, _startPosY);
wheels.DrawWheels(g, _startPosX, _startPosY);
}
public void SetPictureSize(int width, int height) {

View File

@ -2,29 +2,24 @@ package DrawningObjects;
import java.awt.*;
public class DrawningWheels {
private CountEnum countWhils;
public class DrawningWheels implements IDrawWheels {
private CountEnum _wheelsCount;
final protected int _wheelsSize = 18;
public void setCount(int count){
count = Math.min(4, count);
count = Math.max(2, count);
switch (count) {
case 2:
countWhils = CountEnum.Min;
return;
case 3:
countWhils = CountEnum.Mid;
return;
case 4:
countWhils = CountEnum.Max;
return;
}
@Override
public CountEnum WheelsCount() {
return _wheelsCount;
}
protected void drawWheel(Graphics g, int posX, int posY){
g.fillOval(posX, posY, _wheelsSize, _wheelsSize);
}
void DrawWheels(Graphics g, int posX, int posY){
@Override
public void DrawWheels(Graphics g, int posX, int posY){
g.setColor(Color.black);
int count = 0;
switch (countWhils){
switch (_wheelsCount){
case Min -> {
count = 2;
posX += 15;
@ -38,8 +33,28 @@ public class DrawningWheels {
}
}
for (int i = 0; i < count; ++i){
g.fillOval(posX + (i * 15), posY + 55, 15, 15);
g.fillOval(posX + 60 + (i * 15), posY + 55, 15, 15);
drawWheel(g, posX + (i * 18), posY + 55);
drawWheel(g, posX + 75 + (i * 18), posY + 55);
}
}
@Override
public void setWheelsCount(int count) {
count = Math.min(4, count);
count = Math.max(2, count);
switch (count) {
case 2:
_wheelsCount = CountEnum.Min;
break;
case 3:
_wheelsCount = CountEnum.Mid;
break;
case 4:
_wheelsCount = CountEnum.Max;
break;
}
}
public String toString(){
return "Обычное колёса";
}
}

View File

@ -0,0 +1,22 @@
package DrawningObjects;
import java.awt.*;
public class DrawningWheelsPolygon extends DrawningWheels {
private final int _segmentsCount = 6;
private final double _angle = Math.PI * 2 / _segmentsCount;
@Override
protected void drawWheel(Graphics g, int posX, int posY){
g.drawOval(posX, posY, _wheelsSize, _wheelsSize);
int[] pointsPosX = new int[_segmentsCount];
int[] pointsPosY = new int[_segmentsCount];
for (int i = 0; i < _segmentsCount; ++i){
pointsPosX[i] = (int)((Math.cos(_angle * i) + 1) * _wheelsSize / 2) + posX;
pointsPosY[i] = (int)((Math.sin(_angle * i) + 1) * _wheelsSize / 2) + posY;
}
g.drawPolygon(pointsPosX, pointsPosY, _segmentsCount);
}
public String toString(){
return "колёса с узором многоугольник";
}
}

View File

@ -0,0 +1,26 @@
package DrawningObjects;
import java.awt.*;
public class DrawningWheelsStar extends DrawningWheels {
private final int _segmentsCount = 8;
private final double _angle = Math.PI * 2 / _segmentsCount;
@Override
protected void drawWheel(Graphics g, int posX, int posY){
g.drawOval(posX, posY, _wheelsSize, _wheelsSize);
int[] pointsPosX = new int[_segmentsCount];
int[] pointsPosY = new int[_segmentsCount];
for (int i = 0; i < _segmentsCount; ++i){
pointsPosX[i] = (int)((Math.cos(_angle * i) + 1) * _wheelsSize / 2) + posX;
pointsPosY[i] = (int)((Math.sin(_angle * i) + 1) * _wheelsSize / 2) + posY;
}
for (int i = 0; i < _segmentsCount; ++i){
int inversePoint = (i + _segmentsCount / 2 + 1) % _segmentsCount;
g.drawLine(pointsPosX[i], pointsPosY[i],
pointsPosX[inversePoint], pointsPosY[inversePoint]);
}
}
public String toString(){
return "колёса с узором звезда";
}
}

View File

@ -0,0 +1,9 @@
package DrawningObjects;
import java.awt.*;
public interface IDrawWheels {
public CountEnum WheelsCount();
public void DrawWheels(Graphics g, int posX, int posY);
public void setWheelsCount(int count);
}

View File

@ -1,5 +1,7 @@
package Entities;
import DrawningObjects.IDrawWheels;
import java.awt.*;
import java.lang.FunctionalInterface;
@ -9,9 +11,8 @@ public class EntityElectricLocomotive extends EntityLocomotive {
public boolean Battery;
public EntityElectricLocomotive(int speed, double weight,
Color bodyColor, Color additionalColor,
int countWheels,
boolean horns, boolean battery) {
super(speed, weight, bodyColor, countWheels);
super(speed, weight, bodyColor);
Horns = horns;
Battery = battery;
}

View File

@ -1,6 +1,6 @@
package Entities;
import DrawningObjects.DrawningWheels;
import DrawningObjects.IDrawWheels;
import java.awt.*;
@ -9,14 +9,11 @@ public class EntityLocomotive {
public double Weight;
public Color BodyColor;
public double Step;
public DrawningWheels DrawningWheels;
public EntityLocomotive(int speed, double weight, Color bodyColor, int countWheels)
public EntityLocomotive(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
Step = Speed * 100.0f / Weight;
DrawningWheels = new DrawningWheels();
DrawningWheels.setCount(countWheels);
}
}

View File

@ -22,6 +22,7 @@ public class FormElectricLocomotive extends JFrame {
private JButton buttonStep;
private JTextField numberField;
private JComboBox<AbstractStrategy> comboBoxStrategy;
private JComboBox<IDrawWheels> comboBoxWheels;
public void Draw() {
if (_drawningLocomotive == null)
{
@ -41,9 +42,13 @@ public class FormElectricLocomotive extends JFrame {
comboBoxStrategy = new JComboBox<>();
comboBoxStrategy.addItem(new MoveToCenter());
comboBoxStrategy.addItem(new MoveToBorder());
comboBoxWheels = new JComboBox<>();
comboBoxWheels.addItem(new DrawningWheels());
comboBoxWheels.addItem(new DrawningWheelsPolygon());
comboBoxWheels.addItem(new DrawningWheelsStar());
buttonUp = new JButton();
buttonUp.setBorderPainted(false);
buttonUp.setFocusPainted(false);
@ -90,9 +95,10 @@ public class FormElectricLocomotive extends JFrame {
buttonDown.setBounds(50, 420, 40, 40);
buttonRight.setBounds(90, 420, 40, 40);
buttonLeft.setBounds(10, 420, 40, 40);
numberField.setBounds(220, 520, 40, 40);
numberField.setBounds(220, 470, 40, 40);
buttonStep.setBounds(770, 50, 200, 30);
comboBoxStrategy.setBounds(770, 10, 200, 30);
comboBoxWheels.setBounds(220, 520, 250, 40);
add(buttonCreateElectricLocomotive);
@ -104,6 +110,7 @@ public class FormElectricLocomotive extends JFrame {
add(numberField);
add(buttonStep);
add(comboBoxStrategy);
add(comboBoxWheels);
add(canvas);
}
private void InitializeLogic(){
@ -116,6 +123,8 @@ public class FormElectricLocomotive extends JFrame {
} catch (Exception ex) {
countWheels = 0;
}
IDrawWheels _drawWheels = (IDrawWheels) comboBoxWheels.getSelectedItem();
_drawWheels.setWheelsCount(countWheels);
System.out.println(e.getActionCommand());
Random random = new Random();
_drawningLocomotive = new DrawningElectricLocomotive(
@ -126,7 +135,7 @@ public class FormElectricLocomotive extends JFrame {
new Color(random.nextInt(0, 256),
random.nextInt(0, 256),
random.nextInt(0, 256)),
countWheels,
comboBoxWheels.getSelectedIndex(), countWheels,
random.nextInt(0, 2) == 1, random.nextInt(0, 2) == 1,
canvas.getWidth(), canvas.getHeight()
);
@ -152,7 +161,7 @@ public class FormElectricLocomotive extends JFrame {
new Color(random.nextInt(0, 256),
random.nextInt(0, 256),
random.nextInt(0, 256)),
countWheels,
comboBoxWheels.getSelectedIndex(), countWheels,
canvas.getWidth(), canvas.getHeight()
);
_drawningLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));