Готовая 2 усложненная лабораторная работа

This commit is contained in:
Stranni15k 2022-12-22 23:31:43 +04:00
parent 863029954f
commit 495500860c
5 changed files with 52 additions and 27 deletions

View File

@ -2,5 +2,6 @@ public enum Direction {
Up, Up,
Down, Down,
Left, Left,
Right Right,
None
} }

View File

@ -1,24 +1,35 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Random;
public class DrawingLocomotive extends JComponent { public class DrawingLocomotive extends JComponent {
public EntityLocomotive Locomotive; public EntityLocomotive Locomotive;
private float _startPosX; public float _startPosX;
private float _startPosY; public float _startPosY;
private Integer _pictureWidth = null; private Integer _pictureWidth = null;
private Integer _pictureHeight = null; private Integer _pictureHeight = null;
private final int _locomotiveWidth = 150; private int _locomotiveWidth = 150;
private final int _locomotiveHeight = 100; private int _locomotiveHeight = 100;
private DrawingWheel _wheel; private IDrawningObjectWheel _wheel;
public DrawingLocomotive() { public DrawingLocomotive(int speed, float weight, Color bodyColor, int wheelCount)
super();
}
public void Init(int speed, float weight, Color bodyColor, int wheelCount)
{ {
Locomotive = new EntityLocomotive(); Random random = new Random();
Locomotive.Init(speed, weight, bodyColor); Locomotive = new EntityLocomotive(speed, weight, bodyColor);
int randomPattern=random.nextInt(3);
if (randomPattern==0)
_wheel = new DrawingWheel(); _wheel = new DrawingWheel();
else if(randomPattern==1)
_wheel = new DrawingWheelCircle();
else
_wheel = new DrawningWheelTriangle();
_wheel.SetWheelAmount(wheelCount); _wheel.SetWheelAmount(wheelCount);
} }
protected DrawingLocomotive(int speed, float weight, Color bodyColor, int whelcount, int locomotiveWidth, int locomotiveHeight)
{
this(speed, weight, bodyColor, whelcount);
_locomotiveWidth = locomotiveWidth;
_locomotiveHeight = locomotiveHeight;
}
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
{ {
if (width <= _locomotiveWidth + x || height <= _locomotiveHeight + y || x<0 || y<0) if (width <= _locomotiveWidth + x || height <= _locomotiveHeight + y || x<0 || y<0)
@ -119,4 +130,12 @@ public class DrawingLocomotive extends JComponent {
_startPosY = _pictureHeight - _locomotiveHeight; _startPosY = _pictureHeight - _locomotiveHeight;
} }
} }
public float[] GetCurrentPosition(){
float[] tuple = new float[4];
tuple[0] = _startPosX;
tuple[1] =_startPosY;
tuple[2] = _startPosX + _locomotiveWidth;
tuple[3] = _startPosY + _locomotiveHeight;
return tuple;
}
} }

View File

@ -1,7 +1,8 @@
import javax.swing.*;
import java.awt.*; import java.awt.*;
public class DrawingWheel extends JComponent { public class DrawingWheel implements IDrawningObjectWheel {
private AdditionalDirection _wheel; private AdditionalDirection _wheel;
@Override
public void SetWheelAmount(int wheelAmount) { public void SetWheelAmount(int wheelAmount) {
for (AdditionalDirection item: _wheel.values()) { for (AdditionalDirection item: _wheel.values()) {
if (item.getCount() == wheelAmount) { if (item.getCount() == wheelAmount) {
@ -10,8 +11,10 @@ public class DrawingWheel extends JComponent {
} }
} }
} }
@Override
public void DrawWheel(Graphics gr, int _startPosWheelX, int _startPosWheelY, Color pen) { public void DrawWheel(Graphics gr, int _startPosWheelX, int _startPosWheelY, Color pen) {
super.paintComponent(gr); // super.paintComponent(gr);
Graphics2D g=(Graphics2D)gr; Graphics2D g=(Graphics2D)gr;
if (_wheel.getCount() >= 2) { if (_wheel.getCount() >= 2) {
@ -24,7 +27,7 @@ public class DrawingWheel extends JComponent {
if (_wheel.getCount() >= 4) { if (_wheel.getCount() >= 4) {
paintWheel(g, _startPosWheelX +25, _startPosWheelY, pen); paintWheel(g, _startPosWheelX +25, _startPosWheelY, pen);
} }
super.repaint(); // super.repaint();
} }
protected void paintWheel(Graphics2D g, int _startPosX1, int _startPosY, Color pen){ protected void paintWheel(Graphics2D g, int _startPosX1, int _startPosY, Color pen){
try { g.setPaint(pen); } try { g.setPaint(pen); }

View File

@ -14,7 +14,7 @@ public class EntityLocomotive {
} }
public float Step() public float Step()
{ return Speed() * 20 / Weight(); } { return Speed() * 20 / Weight(); }
public void Init(int speed, float weight, Color bodyColor) public EntityLocomotive(int speed, float weight, Color bodyColor)
{ {
Random random = new Random(); Random random = new Random();
this.speed = speed <= 0 ? random.nextInt(50, 150) : speed; this.speed = speed <= 0 ? random.nextInt(50, 150) : speed;

View File

@ -50,16 +50,9 @@ public class FormLocomotive extends JFrame {
} catch (Exception c) { } catch (Exception c) {
} }
Random random = new Random(); Random random = new Random();
_locomotive = new DrawingLocomotive(); _locomotive = new DrawingLocomotive(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256),
_locomotive.Init(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256),
random.nextInt(0, 256), random.nextInt(0, 256)), GetWheelAmount()); random.nextInt(0, 256), random.nextInt(0, 256)), GetWheelAmount());
ChangePictureBoxLocomotiveBorders(); SetData();
_locomotive.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxLocomotiveWidth, pictureBoxLocomotiveHeight);
toolStripStatusLabelSpeed.setText("Скорость: " + _locomotive.Locomotive.Speed());
toolStripStatusLabelWeight.setText("Вес: " + _locomotive.Locomotive.Weight());
toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_locomotive.Locomotive.BodyColor().getRGB()));
pictureBoxLocomotive.add(_locomotive, BorderLayout.CENTER);
} }
}); });
addComponentListener(new ComponentAdapter() { addComponentListener(new ComponentAdapter() {
@ -123,4 +116,13 @@ public class FormLocomotive extends JFrame {
return 4; return 4;
} }
} }
private void SetData() {
Random random = new Random();
ChangePictureBoxLocomotiveBorders();
_locomotive.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxLocomotiveWidth, pictureBoxLocomotiveHeight);
toolStripStatusLabelSpeed.setText("Скорость: " + _locomotive.Locomotive.Speed());
toolStripStatusLabelWeight.setText("Вес: " + _locomotive.Locomotive.Weight());
toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_locomotive.Locomotive.BodyColor().getRGB()));
pictureBoxLocomotive.add(_locomotive, BorderLayout.CENTER);
}
} }