PIbd-23. Ivanov V.N. Lab Work 01. Hard #1

Closed
Vyacheslav wants to merge 2 commits from LabWork1 into main
10 changed files with 381 additions and 0 deletions

BIN
Resources/DownArrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 414 B

BIN
Resources/LeftArrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 439 B

BIN
Resources/RightArrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 377 B

BIN
Resources/UpArrow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 439 B

6
src/DirectionType.java Normal file
View File

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

5
src/DoorNumberType.java Normal file
View File

@ -0,0 +1,5 @@
public enum DoorNumberType {
Three,
Four,
Five
}

57
src/DrawningDoor.java Normal file
View File

@ -0,0 +1,57 @@
import javax.swing.*;
import java.awt.*;
public class DrawningDoor {
JPanel DoubleDeckerBusPanel;
private DoorNumberType DoorNumberType;
private Color blackColor;
private int Width;
private int Height;
public int CurX;
public int CurY;
public boolean Init(int width, int height, int curX, int curY, JPanel doubleDeckerBusPanel) {
Width = width;
Height = height;
CurX = curX;
CurY = curY;
blackColor = Color.BLACK;
DoubleDeckerBusPanel = doubleDeckerBusPanel;
return true;
}
public void ChangeDoorsNumber(int x) {
if (x <= 3) {
DoorNumberType = DoorNumberType.Three;
}
if (x == 4) {
DoorNumberType = DoorNumberType.Four;
}
if (x >= 5) {
DoorNumberType = DoorNumberType.Five;
}
}
public DoorNumberType DoorNumberType() {
return DoorNumberType;
}
public void DrawDoors() {
Graphics2D g2d = (Graphics2D) DoubleDeckerBusPanel.getGraphics();
g2d.setColor(blackColor);
g2d.fillRect(CurX + 15, CurY + 40, 10, 20);
g2d.fillRect(CurX + 30, CurY + 40, 10, 20);
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
g2d.fillRect(CurX + 45, CurY + 40, 10, 20);
}
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
g2d.fillRect(CurX + 60, CurY + 40, 10, 20);
}
if (DoorNumberType() == DoorNumberType.Five) {
g2d.fillRect(CurX + 75, CurY + 40, 10, 20);
}
}
}

View File

@ -0,0 +1,157 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawningDoubleDeckerBus {
JPanel DoubleDeckerBusPanel;
private EntityDoubleDeckerBus EntityDoubleDeckerBus;
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX = 0;
private int _startPosY = 0;
private final int _busWidth = 110;
private final int _busHeight = 70;
private DrawningDoor DrawningDoor;
public EntityDoubleDeckerBus EntityDoubleDeckerBus() {
return EntityDoubleDeckerBus;
}
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, int doorNumber,
int width, int height, boolean secondFloor, boolean ladder, boolean lineBetweenFloor, JPanel doubleDeckerBusPanel) {
if (width <= _busWidth || height <= _busHeight) {
return false;
}
_startPosX = 0;
_startPosY = 0;
doubleDeckerBusPanel.setSize(width, height);
DoubleDeckerBusPanel = doubleDeckerBusPanel;
doubleDeckerBusPanel.paint(DoubleDeckerBusPanel.getGraphics());
_pictureWidth = width;
_pictureHeight = height;
EntityDoubleDeckerBus = new EntityDoubleDeckerBus();
EntityDoubleDeckerBus.Init(speed, weight, bodyColor, additionalColor, doorNumber, secondFloor, ladder, lineBetweenFloor);
DrawningDoor = new DrawningDoor();
DrawningDoor.Init(_busWidth, _busHeight, _startPosX, _startPosY, doubleDeckerBusPanel);
Random random = new Random();
DrawningDoor.ChangeDoorsNumber(random.nextInt(2,6));
return true;
}
public void SetPosition(int x, int y){
if (EntityDoubleDeckerBus == null) {
return;
}
_startPosX = x;
_startPosY = y;
if (x + _busWidth <= _pictureWidth || y + _busHeight <= _pictureHeight) {
_startPosX = 0;
_startPosY = 0;
}
}
public void MoveTransport(DirectionType direction){
if (EntityDoubleDeckerBus == null)
return;
DoubleDeckerBusPanel.paint(DoubleDeckerBusPanel.getGraphics());
switch (direction)
{
case Left:
if (_startPosX - EntityDoubleDeckerBus.Step() >= 0)
_startPosX -= (int)EntityDoubleDeckerBus.Step();
else
_startPosX = 0;
break;
case Up:
if (_startPosY - EntityDoubleDeckerBus.Step() >= 0)
_startPosY -= (int)EntityDoubleDeckerBus.Step();
else
_startPosY = 0;
break;
case Right:
if (_startPosX + EntityDoubleDeckerBus.Step() + _busWidth < _pictureWidth)
_startPosX += (int)EntityDoubleDeckerBus.Step();
else
_startPosX = _pictureWidth - _busWidth;
break;
case Down:
if (_startPosY + EntityDoubleDeckerBus.Step() + _busHeight < _pictureHeight)
_startPosY += (int)EntityDoubleDeckerBus.Step();
else
_startPosY = _pictureHeight - _busHeight;
break;
}
DrawningDoor.CurX = _startPosX;
DrawningDoor.CurY = _startPosY;
}
public void DrawTransport() {
Graphics2D g2d = (Graphics2D) DoubleDeckerBusPanel.getGraphics();
if (EntityDoubleDeckerBus == null) {
return;
}
// Границы первого этажа автобуса
g2d.setColor(EntityDoubleDeckerBus.BodyColor());
g2d.fillRect(_startPosX, _startPosY + 30, 100, 30);
// Колеса
g2d.setColor(Color.black);
g2d.fillOval(_startPosX + 7, _startPosY + 55, 10, 10);
g2d.fillOval(_startPosX + 77, _startPosY + 55, 10, 10);
// Окна
g2d.setColor(Color.blue);
g2d.fillOval(_startPosX + 10, _startPosY + 35, 10, 15);
g2d.fillOval(_startPosX + 50, _startPosY + 35, 10, 15);
g2d.fillOval(_startPosX + 70, _startPosY + 35, 10, 15);
g2d.fillOval(_startPosX + 90, _startPosY + 35, 10, 15);
// двери
DrawningDoor.DrawDoors();
// второй этаж
if (EntityDoubleDeckerBus.SecondFloor())
{
g2d.setColor(EntityDoubleDeckerBus.BodyColor());
// Границы второго этажа автобуса
g2d.setColor(EntityDoubleDeckerBus.AdditionalColor());
g2d.fillRect(_startPosX, _startPosY, 100, 30);
// Окна второго этажа
g2d.setColor(Color.BLUE);
g2d.fillOval(_startPosX + 12, _startPosY + 5, 10, 15);
g2d.fillOval(_startPosX + 30, _startPosY + 5, 10, 15);
g2d.fillOval(_startPosX + 50, _startPosY + 5, 10, 15);
g2d.fillOval(_startPosX + 70, _startPosY + 5, 10, 15);
g2d.fillOval(_startPosX + 90, _startPosY + 5, 10, 15);
}
// лестница
if (EntityDoubleDeckerBus.Ladder())
{
if (EntityDoubleDeckerBus.SecondFloor()) {
//Вертикальные прямые
g2d.setColor(Color.black);
g2d.drawLine(_startPosX, _startPosY + 55, _startPosX, _startPosY + 25);
g2d.drawLine(_startPosX + 10, _startPosY + 55, _startPosX + 10, _startPosY + 25);
//Горизонтальные прямые
g2d.drawLine(_startPosX, _startPosY + 35, _startPosX + 10, _startPosY + 35);
g2d.drawLine(_startPosX, _startPosY + 45, _startPosX + 10, _startPosY + 45);
g2d.drawLine(_startPosX, _startPosY + 55, _startPosX + 10, _startPosY + 55);
}
}
// полоса между этажами
if (EntityDoubleDeckerBus.LineBetweenFloor())
{
g2d.setColor(Color.black);
g2d.fillRect(_startPosX, _startPosY + 30, 100, 3);
}
}
}

View File

@ -0,0 +1,62 @@
import java.awt.*;
public class EntityDoubleDeckerBus {
private int Speed;
private int DoorNumber;
private double Weight;
private Color BodyColor;
private Color AdditionalColor;
private boolean SecondFloor;
private boolean Ladder;
private boolean LineBetweenFloor;
private double Step;
public int Speed() {
return Speed;
}
public int DoorNumber() {
return DoorNumber;
}
public double Weight() {
return Weight;
}
public Color BodyColor() {
return BodyColor;
}
public Color AdditionalColor() {
return AdditionalColor;
}
public boolean SecondFloor() {
return SecondFloor;
}
public boolean Ladder() {
return Ladder;
}
public boolean LineBetweenFloor() {
return LineBetweenFloor;
}
public double Step() {
return Step;
}
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
int doorNumber, boolean secondFloor ,boolean ladder, boolean lineBetweenFloor) {
Speed = speed;
Weight = weight;
Step = (double) Speed * 100 / Weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
SecondFloor = secondFloor;
DoorNumber = doorNumber;
Ladder = ladder;
LineBetweenFloor = lineBetweenFloor;
}
}

View File

@ -0,0 +1,94 @@
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;
public class FormDoubleDeckerBus {
public static void main(String[] args) throws IOException {
JFrame DoubleDeckerBusFrame = new JFrame();
JPanel DoubleDeckerBusPanel = new JPanel();
DoubleDeckerBusFrame.setLayout(new BorderLayout());
DoubleDeckerBusFrame.setSize(900, 500);
DoubleDeckerBusFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DoubleDeckerBusFrame.setLayout(new BorderLayout(1,1));
DrawningDoubleDeckerBus DrawningDoubleDeckerBus = new DrawningDoubleDeckerBus();
DoubleDeckerBusPanel.setLayout(null);
BufferedImage RightIcon = ImageIO.read(new File("Resources/RightArrow.png"));
BufferedImage LeftIcon = ImageIO.read(new File("Resources/LeftArrow.png"));
BufferedImage UpIcon = ImageIO.read(new File("Resources/UpArrow.png"));
BufferedImage DownIcon = ImageIO.read(new File("Resources/DownArrow.png"));
JButton RightButton = new JButton(new ImageIcon(RightIcon));
JButton LeftButton = new JButton(new ImageIcon(LeftIcon));
JButton UpButton = new JButton(new ImageIcon(UpIcon));
JButton DownButton = new JButton(new ImageIcon(DownIcon));
JButton CreateButton = new JButton();
CreateButton.setText("Создать");
CreateButton.setBounds(12, 401, 90, 40);
RightButton.setBounds(840,411,30,30);
LeftButton.setBounds(768,411,30,30);
UpButton.setBounds(804,375,30,30);
DownButton.setBounds(804,411,30,30);
DoubleDeckerBusPanel.add(CreateButton);
DoubleDeckerBusPanel.add(RightButton);
DoubleDeckerBusPanel.add(LeftButton);
DoubleDeckerBusPanel.add(UpButton);
DoubleDeckerBusPanel.add(DownButton);
DoubleDeckerBusFrame.add(DoubleDeckerBusPanel, BorderLayout.CENTER);
Random random = new Random();
CreateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningDoubleDeckerBus.Init(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
random.nextInt(2, 5), DoubleDeckerBusPanel.getWidth(), DoubleDeckerBusPanel.getHeight(),
random.nextBoolean(), random.nextBoolean(), random.nextBoolean(), DoubleDeckerBusPanel);
DrawningDoubleDeckerBus.DrawTransport();
}
});
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null)
return;
DrawningDoubleDeckerBus.MoveTransport(DirectionType.Right);
DrawningDoubleDeckerBus.DrawTransport();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null)
return;
DrawningDoubleDeckerBus.MoveTransport(DirectionType.Left);
DrawningDoubleDeckerBus.DrawTransport();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null)
return;
DrawningDoubleDeckerBus.MoveTransport(DirectionType.Up);
DrawningDoubleDeckerBus.DrawTransport();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(DrawningDoubleDeckerBus.EntityDoubleDeckerBus() == null)
return;
DrawningDoubleDeckerBus.MoveTransport(DirectionType.Down);
DrawningDoubleDeckerBus.DrawTransport();
}
});
DoubleDeckerBusFrame.setVisible(true);
}
}