PIbd-11 Karakozov_AK LabWork01 Hard #1

Closed
insideq wants to merge 1 commits from LabWork01 into main
13 changed files with 429 additions and 5 deletions
Showing only changes of commit d8bc8f2549 - Show all commits

BIN
res/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 B

BIN
res/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 270 B

BIN
res/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 B

BIN
res/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 304 B

16
src/CanvasExcavator.java Normal file
View File

@ -0,0 +1,16 @@
import javax.swing.*;
import java.awt.*;
public class CanvasExcavator extends JComponent{
public DrawingExcavator _drawingExcavator;
public CanvasExcavator(){}
public void paintComponent(Graphics g){
if(_drawingExcavator == null){
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
_drawingExcavator.DrawTransport(g2d);
super.repaint();
}
}

6
src/DirectionType.java Normal file
View File

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

207
src/DrawingExcavator.java Normal file
View File

@ -0,0 +1,207 @@
import java.awt.*;
import java.util.Random;
public class DrawingExcavator {
private EntityExcavator EntityExcavator; // Класс-сущность
public DrawingRollers drawingRollers = null;
private Integer _startPosX; // Левая координата отрисовки
private Integer _startPosY; // Верхняя координата отрисовки
private Integer _pictureWidth; // Ширина окна отрисовки
private Integer _pictureHeight; // Высота окна отрисовки
private final int _drawingExcWidth = 120; // Ширина отрисовки трактора
private final int _drawingExcHeight = 70; // Высота отрисовки трактора
// Инициализация свойств
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){
EntityExcavator = new EntityExcavator();
EntityExcavator.Init(speed, weight, bodyColor, additionalColor, prop, ladle);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
drawingRollers = new DrawingRollers();
drawingRollers.setRollersCount((int)(Math.random() * 4));
}
// Смена границ формы отрисовки
public void SetPictureSize(int width, int height)
{
if (_drawingExcWidth > width || _drawingExcHeight > height)
{
return;
}
if (_startPosX != null && _startPosY != null) {
if (_startPosX < 0) { _startPosX = 0; }
if (_startPosX + _drawingExcWidth > width) { _startPosX = width - _drawingExcWidth; }
if (_startPosY < 0) { _startPosY = 0; }
if (_startPosY + _drawingExcHeight > height) { _startPosY = height - _drawingExcHeight; }
}
_pictureWidth = width;
_pictureHeight = height;
}
//Установка позиции Экскаватора
public void SetPosition(int x, int y){
if (_pictureWidth == null || _pictureHeight == null)
{
return;
}
if (x + _drawingExcWidth > _pictureWidth || x < 0) {
Random random = new Random();
_startPosX = random.nextInt(_pictureWidth - _drawingExcWidth);
}
else {
_startPosX = x;
}
if (y + _drawingExcHeight > _pictureHeight || y < 0) {
Random random = new Random();
_startPosY = random.nextInt(_pictureHeight - _drawingExcHeight);
}
else {
_startPosY = y;
}
}
// Изменение направления движения
public boolean MoveTransport(DirectionType direction){
if (EntityExcavator == null || _startPosX == null || _startPosY == null){
return false;
}
switch (direction) {
case Left:
if (_startPosX - EntityExcavator.Step > 0) {
_startPosX -= (int) EntityExcavator.Step;
}
return true;
case Up:
if (_startPosY - EntityExcavator.Step > 0) {
_startPosY -= (int) EntityExcavator.Step;
}
return true;
case Right:
if (_startPosX + EntityExcavator.Step < _pictureWidth - _drawingExcWidth) {
_startPosX += (int) EntityExcavator.Step;
}
return true;
case Down:
if (_startPosY + EntityExcavator.Step < _pictureHeight - _drawingExcHeight) {
_startPosY += (int) EntityExcavator.Step;
}
return true;
default:
return false;
}
}
// Отрисовка Экскаватора
public void DrawTransport(Graphics2D g){
if (EntityExcavator == null || _startPosX == null || _startPosY == null){
return;
}
// Границы экскаватора
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 20, _startPosY + 25, 60, 20); // главная нижняя
g.drawRect(_startPosX + 35, _startPosY + 10, 5, 15);
g.drawRect(_startPosX + 55, _startPosY + 3, 22, 22); // кабина
g.setColor(EntityExcavator.getBodyColor());
g.fillRect(_startPosX + 21, _startPosY + 26, 59, 19);
g.fillRect(_startPosX + 36, _startPosY + 11, 4, 14);
g.setColor(Color.cyan);
g.fillRect(_startPosX + 56, _startPosY + 4, 21, 21);
// Гусеница
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 24, _startPosY + 47, 48, 17);
// Основные катки
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 15, _startPosY + 47, 17, 17);
g.drawOval(_startPosX + 63, _startPosY + 47, 17, 17);
g.setColor(EntityExcavator.getBodyColor());
g.fillOval(_startPosX + 15, _startPosY + 47, 17, 17);
g.fillOval(_startPosX + 63, _startPosY + 47, 17, 17);
// Малые катки
g.setColor(Color.BLACK);
g.drawOval(_startPosX + 40, _startPosY + 48, 5, 5);
g.drawOval(_startPosX + 50, _startPosY + 48, 5, 5);
g.setColor(EntityExcavator.getBodyColor());
g.fillOval(_startPosX + 40, _startPosY + 48, 5, 5);
g.fillOval(_startPosX + 50, _startPosY + 48, 5, 5);
if (EntityExcavator.getProp()){
g.setColor(Color.BLACK);
//Опоры
//справа
g.drawRect(_startPosX + 80, _startPosY + 40, 11, 3);
g.drawRect(_startPosX + 87, _startPosY + 43, 5, 25);
g.drawRect(_startPosX + 84, _startPosY + 68, 11, 3);
//слева
g.drawRect(_startPosX + 6, _startPosY + 40, 13, 3);
g.drawRect(_startPosX + 4, _startPosY + 43, 5, 25);
g.drawRect(_startPosX + 1, _startPosY + 68, 11, 3);
g.setColor(EntityExcavator.getAdditionalColor());
//покраска справа
g.fillRect(_startPosX + 81, _startPosY + 41, 10, 2);
g.fillRect(_startPosX + 88, _startPosY + 44, 4, 24);
g.fillRect(_startPosX + 85, _startPosY + 69, 10, 2);
//покраска слева
g.fillRect(_startPosX + 7, _startPosY + 41, 12, 2);
g.fillRect(_startPosX + 5, _startPosY + 44, 4, 24);
g.fillRect(_startPosX + 2, _startPosY + 69, 10, 2);
}
if(EntityExcavator.getLadle()){
g.setColor(Color.BLACK);
//Ковш
//ковш(стрела)
g.drawRect(_startPosX + 77, _startPosY + 17, 14, 8);
g.drawRect(_startPosX + 91, _startPosY + 9, 20, 7);
g.drawRect(_startPosX + 111, _startPosY + 17, 9, 20);
int[] pointsLadleX = {_startPosX + 120, _startPosX + 104, _startPosX + 120};
int[] pointsLadleY = {_startPosY + 37, _startPosY + 54, _startPosY + 54};
Polygon Ladle = new Polygon(pointsLadleX, pointsLadleY, 3);
g.drawPolygon(Ladle);
g.setColor(EntityExcavator.getAdditionalColor());
g.fillPolygon(Ladle);
//покраска
g.fillRect(_startPosX + 78, _startPosY + 18, 13, 7);
g.fillRect(_startPosX + 92, _startPosY + 10, 19, 6);
g.fillRect(_startPosX + 112, _startPosY + 18, 8, 19);
}
int x = _startPosX;
if (drawingRollers.getRollersCount() != null){
switch (drawingRollers.getRollersCount()){
case OneRoller:
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
break;
case TwoRollers:
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
drawingRollers.DrawRollers(g,x + 44, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
break;
case ThreeRollers:
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
drawingRollers.DrawRollers(g,x + 44, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
drawingRollers.DrawRollers(g,x + 54, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
break;
}
}
}
}

24
src/DrawingRollers.java Normal file
View File

@ -0,0 +1,24 @@
import java.awt.*;
public class DrawingRollers {
Review

rollersCount должен был участвовать в прорисовке

rollersCount должен был участвовать в прорисовке
private RollersCount rollersCount;
public void setRollersCount(int numOfRoller){
for (RollersCount numofenum : RollersCount.values()){
if (numofenum.getNumOfRollers() == numOfRoller){
rollersCount = numofenum;
return;
}
}
}
public RollersCount getRollersCount(){
return rollersCount;
}
public void DrawRollers(Graphics g, int x, int y, int width, int height, Color bodyColor){
g.setColor(bodyColor);
g.fillOval(x, y, width, height);
g.setColor(Color.BLACK);
g.drawOval(x, y, width, height);
g.setColor(bodyColor);
}
}

32
src/EntityExcavator.java Normal file
View File

@ -0,0 +1,32 @@
import java.awt.*;
public class EntityExcavator {
private Integer Speed;
private Double Weight;
private Color BodyColor;
public Color getBodyColor() {
return BodyColor;
}
private Color AdditionalColor;
public Color getAdditionalColor(){
return AdditionalColor;
}
private boolean Prop;
public boolean getProp(){
return Prop;
}
private boolean Ladle;
public boolean getLadle(){
return Ladle;
}
public double Step;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Prop = prop;
Ladle = ladle;
Step = Speed * 100 / Weight;
}
}

123
src/FormExcavator.java Normal file
View File

@ -0,0 +1,123 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormExcavator extends JFrame {
private String title;
private Dimension dimension;
private int Width, Height;
private CanvasExcavator canvasExcavator = new CanvasExcavator();
private JButton CreateButton = new JButton("Создать");
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();
private JButton LeftButton = new JButton();
private JButton RightButton = new JButton();
public FormExcavator(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init() {
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Width = getWidth() - 15;
Height = getHeight() - 35;
CreateButton.setName("Create");
Icon iconUp = new ImageIcon("res/up.png");
UpButton.setIcon(iconUp);
UpButton.setName("Up");
DownButton.setName("Down");
Icon iconDown = new ImageIcon("res/down.png");
DownButton.setIcon(iconDown);
LeftButton.setName("Left");
Icon iconLeft = new ImageIcon("res/left.png");
LeftButton.setIcon(iconLeft);
RightButton.setName("Right");
Icon iconRight = new ImageIcon("res/right.png");
RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int StartPositionX = (int)(Math.random() * 90 + 10);
int StartPositionY = (int)(Math.random() * 90 + 10);
int speed = (int)(Math.random() * 300 + 100);
double weight = (Math.random() * 3000 + 1000);
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
boolean sheepPipes = new Random().nextBoolean();
boolean fuelTank = new Random().nextBoolean();
canvasExcavator._drawingExcavator = new DrawingExcavator();
canvasExcavator._drawingExcavator.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
canvasExcavator._drawingExcavator.SetPictureSize(Width, Height);
canvasExcavator._drawingExcavator.SetPosition( StartPositionX, StartPositionY);
canvasExcavator.repaint();
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasExcavator._drawingExcavator == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "Up":
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Up);
break;
case "Down":
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Down);
break;
case "Left":
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Left);
break;
case "Right":
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Right);
break;
}
if (result) {
canvasExcavator.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width,dimension.height);
setLayout(null);
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 35);
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
add(CreateButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(canvasExcavator);
setVisible(true);
//обработка события изменения размеров окна
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
Width = getWidth() - 15;
Height = getHeight() - 35;
if (canvasExcavator._drawingExcavator != null)
canvasExcavator._drawingExcavator.SetPictureSize(Width, Height);
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 100, 35);
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
}
});
}
}

View File

@ -1,5 +0,0 @@
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

9
src/Program.java Normal file
View File

@ -0,0 +1,9 @@
import java.awt.*;
public class Program {
public static void main(String[] args) {
FormExcavator form = new FormExcavator("Экскаватор", new Dimension(700, 500));
form.Init();
form.setLocationRelativeTo(null);
}
}

12
src/RollersCount.java Normal file
View File

@ -0,0 +1,12 @@
public enum RollersCount {
OneRoller(1),
TwoRollers(2),
ThreeRollers(3);
private int numOfRollers;
RollersCount(int numOfRollers){
this.numOfRollers = numOfRollers;
}
public int getNumOfRollers(){
return numOfRollers;
}
}