Первая лабораторная работа

This commit is contained in:
allllen4a 2023-12-01 10:48:37 +03:00
parent 1c2cfd8fe3
commit f5d4842b5d
7 changed files with 355 additions and 0 deletions

1
.gitignore vendored
View File

@ -23,4 +23,5 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
replay_pid* replay_pid*
.idea

6
DirectionType.java Normal file
View File

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

5
DoorNumberType.java Normal file
View File

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

57
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 + 55, CurY + 65, 10, 15);
g2d.fillRect(CurX + 70, CurY + 65, 10, 15);
if (DoorNumberType() == DoorNumberType.Three || DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
g2d.fillRect(CurX + 85, CurY + 65, 10, 15);
}
if (DoorNumberType() == DoorNumberType.Four || DoorNumberType() == DoorNumberType.Five) {
g2d.fillRect(CurX + 100, CurY + 65, 10, 15);
}
if (DoorNumberType() == DoorNumberType.Five) {
g2d.fillRect(CurX + 115, CurY + 65, 10, 15);
}
}
}

View File

@ -0,0 +1,137 @@
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 = 160;
private final int _busHeight = 100;
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 tailpipe, 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, tailpipe);
DrawningDoor = new DrawningDoor();
DrawningDoor.Init(_busWidth, _busHeight, _startPosX, _startPosY, doubleDeckerBusPanel);
Random random = new Random();
DrawningDoor.ChangeDoorsNumber(random.nextInt(5) + 2);
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 + 40, _startPosY + 40, 120, 40);
// Колеса
g2d.setColor(Color.black);
g2d.fillOval(_startPosX + 45, _startPosY + 75, 20, 20);
g2d.fillOval(_startPosX + 127, _startPosY + 75, 20, 20);
// Окна
g2d.setColor(Color.blue);
g2d.fillOval(_startPosX + 50, _startPosY + 45, 20, 20);
g2d.fillOval(_startPosX + 75, _startPosY + 45, 20, 20);
g2d.fillOval(_startPosX + 100, _startPosY + 45, 20, 20);
g2d.fillOval(_startPosX + 125, _startPosY + 45, 20, 20);
// Двери
DrawningDoor.DrawDoors();
// Второй этаж
if (EntityDoubleDeckerBus.SecondFloor()) {
g2d.setColor(EntityDoubleDeckerBus.AdditionalColor());
// Границы второго этажа автобуса
g2d.fillRect(_startPosX + 40, _startPosY, 120, 40);
// Окна второго этажа
g2d.setColor(Color.blue);
g2d.fillOval(_startPosX + 50, _startPosY +10,20, 20);
g2d.fillOval(_startPosX + 75, _startPosY +10, 20, 20);
g2d.fillOval(_startPosX + 100, _startPosY +10,20, 20);
g2d.fillOval(_startPosX + 125, _startPosY +10,20,20);
}
if (EntityDoubleDeckerBus.Tailpipe()) {
g2d.setColor(EntityDoubleDeckerBus.AdditionalColor());
g2d.fillRect(_startPosX, _startPosY + 65, 40, 15);
}
}
}

View File

@ -0,0 +1,55 @@
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 Tailpipe;
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 Tailpipe() {
return Tailpipe;
}
public double Step() {
return Step;
}
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,
int doorNumber, boolean secondFloor ,boolean tailpipe) {
Speed = speed;
Weight = weight;
Step = (double) Speed * 100 / Weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
SecondFloor = secondFloor;
DoorNumber = doorNumber;
Tailpipe = tailpipe;
}
}

94
FormDoubleDeckerBus.java Normal file
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("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/right.png"));
BufferedImage LeftIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/left.png"));
BufferedImage UpIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/up.png"));
BufferedImage DownIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/down.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("Create");
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(201) + 100, random.nextDouble() * 2000 + 1000,
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
Color.getHSBColor(random.nextInt(301), random.nextInt(301), random.nextInt(301)),
random.nextInt(4) + 2, DoubleDeckerBusPanel.getWidth(), DoubleDeckerBusPanel.getHeight(),
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);
}
}