PIbd-23. Yunusov N.N. Lab work 01 Hard #1

Closed
Yunusov_Niyaz wants to merge 3 commits from Lab1 into main
16 changed files with 350 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml" filepath="$PROJECT_DIR$/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml" />
</modules>
</component>
</project>

7
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

BIN
img/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 B

BIN
img/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 295 B

BIN
img/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 B

BIN
img/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 276 B

6
src/DirectionType.java Normal file
View File

@ -0,0 +1,6 @@
public enum DirectionType {
UP,
DOWN,
LEFT,
RIGHT
}

5
src/DoorsNumber.java Normal file
View File

@ -0,0 +1,5 @@
public enum DoorsNumber {
THREE,
FOUR,
FIVE
}

23
src/DrawingDoors.java Normal file
View File

@ -0,0 +1,23 @@
import java.awt.*;
public class DrawingDoors {
private DoorsNumber number;
public void getNumber(int x){
if(x <= 3)
number = DoorsNumber.THREE;
if(x == 4)
number = DoorsNumber.FOUR;
if(x >= 5)
number = DoorsNumber.FIVE;
}
public void drawDoors(Graphics2D graphics2D, int _startX, int _startY){
graphics2D.fillRect(_startX+52, _startY+81, 25, 40);
graphics2D.fillRect(_startX+85, _startY+81, 25, 40);
graphics2D.fillRect(_startX+118, _startY+81, 25, 40);
if (number == DoorsNumber.FOUR || number == DoorsNumber.FIVE){
graphics2D.fillRect(_startX+151, _startY+81, 25, 40);
}
if (number == DoorsNumber.FIVE){
graphics2D.fillRect(_startX+19, _startY+81, 25, 40);
}
}
}

125
src/DrawingTrolleybus.java Normal file
View File

@ -0,0 +1,125 @@
import java.awt.*;
public class DrawingTrolleybus {
private EntityTrolleybus entityTrolleybus;
public EntityTrolleybus getEntityTrolleybus() {
return entityTrolleybus;
}
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private final int trolleybusWidth = 200;
private final int trolleybusHeight = 135;
private DrawingDoors drawingDoors;
public boolean init(int speed, double weight, Color bodyColor, Color additionalColor,
boolean roga, boolean battery, int width, int height, int doorsNumber)
{
if (width < trolleybusWidth || height < trolleybusHeight)
return false;
_pictureWidth = width;
_pictureHeight = height;
entityTrolleybus = new EntityTrolleybus();
entityTrolleybus.init(speed, weight, bodyColor, additionalColor, roga, battery);
drawingDoors = new DrawingDoors();
drawingDoors.getNumber(doorsNumber);
return true;
}
public void setPosition(int x, int y)
{
if (x < 0 || y < 0 || x + trolleybusWidth >= _pictureWidth || y + trolleybusHeight >= _pictureHeight) {
x = 0;
y = 0;
}
_startPosX = x;
_startPosY = y;
}
public void moveTransport(DirectionType direction) {
if (entityTrolleybus == null)
return;
switch (direction) {
//влево
case LEFT:
if (_startPosX - entityTrolleybus.step.get().intValue() > 0)
_startPosX -= entityTrolleybus.step.get().intValue();
break;
//вверх
case UP:
if (_startPosY - entityTrolleybus.step.get().intValue() > 0)
_startPosY -= entityTrolleybus.step.get().intValue();
break;
// вправо
case RIGHT:
if (_startPosX + trolleybusWidth + entityTrolleybus.step.get().intValue() < _pictureWidth)
_startPosX += entityTrolleybus.step.get().intValue();
break;
//вниз
case DOWN:
if (_startPosY + trolleybusHeight + entityTrolleybus.step.get().intValue() < _pictureHeight)
_startPosY += entityTrolleybus.step.get().intValue();
break;
}
}
public void drawTransport(Graphics2D graphics2D) {
if (entityTrolleybus == null)
return;
BasicStroke pen = new BasicStroke(2);
graphics2D.setStroke(pen);
Color bodyColor = entityTrolleybus.getBodyColor();
Color additionalColor = entityTrolleybus.getAdditionalColor();
//колеса
graphics2D.setPaint(Color.BLACK);
graphics2D.fillOval(_startPosX + 31, _startPosY + 106, 30, 30);
graphics2D.fillOval(_startPosX + 151, _startPosY + 106, 30, 30);
//кузов
graphics2D.setPaint(bodyColor);
graphics2D.fillRect(_startPosX + 6, _startPosY + 31, 200, 90);
//стекла
graphics2D.setPaint(Color.BLUE);
graphics2D.fillRect(_startPosX + 186, _startPosY + 40, 20, 40);
graphics2D.fillOval(_startPosX + 151, _startPosY + 35, 30, 40);
graphics2D.fillOval(_startPosX + 118, _startPosY + 35, 30, 40);
graphics2D.fillOval(_startPosX + 85, _startPosY + 35, 30, 40);
graphics2D.fillOval(_startPosX + 52, _startPosY + 35, 30, 40);
graphics2D.fillOval(_startPosX + 19, _startPosY + 35, 30, 40);
//двери
graphics2D.setPaint(Color.BLACK);
drawingDoors.drawDoors(graphics2D, _startPosX, _startPosY);
//рога
graphics2D.setPaint(Color.BLACK);
if (entityTrolleybus.getRoga()) {
graphics2D.setPaint(Color.BLACK);
graphics2D.drawLine(_startPosX + 186, _startPosY + 31, _startPosX + 86, _startPosY + 1);
graphics2D.drawLine(_startPosX + 86, _startPosY + 1, _startPosX + 126, _startPosY + 31);
graphics2D.drawLine(_startPosX + 146, _startPosY + 31, _startPosX + 46, _startPosY + 1);
graphics2D.drawLine(_startPosX + 46, _startPosY + 1, _startPosX + 86, _startPosY + 31);
}
//батарея
if (entityTrolleybus.getBattery()) {
graphics2D.setPaint(additionalColor);
graphics2D.fillRect(_startPosX + 176, _startPosY + 101, 15, 20);
graphics2D.setPaint(Color.YELLOW);
graphics2D.drawLine(_startPosX + 183, _startPosY + 103, _startPosX + 178, _startPosY + 111);
graphics2D.drawLine(_startPosX + 178, _startPosY + 111, _startPosX + 189, _startPosY + 111);
graphics2D.drawLine(_startPosX + 189, _startPosY + 111, _startPosX + 183, _startPosY + 119);
}
//границы троллейбуса
graphics2D.setPaint(Color.BLACK);
graphics2D.drawRect(_startPosX + 6, _startPosY + 31, 200, 90);
graphics2D.drawOval(_startPosX + 151, _startPosY + 35, 30, 40);
graphics2D.drawOval(_startPosX + 118, _startPosY + 35, 30, 40);
graphics2D.drawOval(_startPosX + 85, _startPosY + 35, 30, 40);
graphics2D.drawOval(_startPosX + 52, _startPosY + 35, 30, 40);
graphics2D.drawOval(_startPosX + 19, _startPosY + 35, 30, 40);
graphics2D.drawRect(_startPosX + 186, _startPosY + 40, 20, 40);
//задние фары
graphics2D.setPaint(Color.RED);
graphics2D.fillRect(_startPosX + 6, _startPosY + 91, 10, 20);
graphics2D.setPaint(Color.BLACK);
graphics2D.drawRect(_startPosX + 6, _startPosY + 91, 10, 20);
//передние фары
graphics2D.setPaint(Color.YELLOW);
graphics2D.fillRect(_startPosX + 196, _startPosY + 91, 10, 20);
graphics2D.setPaint(Color.BLACK);
graphics2D.drawRect(_startPosX + 196, _startPosY + 91, 10, 20);
}
}

37
src/EntityTrolleybus.java Normal file
View File

@ -0,0 +1,37 @@
import java.awt.*;
import java.util.function.Supplier;
public class EntityTrolleybus {
private int speed;
public int getSpeed(){
return speed;
}
private double weight;
public double getWeight(){
return weight;
}
private Color bodyColor;
public Color getBodyColor(){
return bodyColor;
}
private Color additionalColor;
public Color getAdditionalColor(){return additionalColor;}
private boolean roga;
public boolean getRoga() {
return roga;
}
private boolean battery;
public boolean getBattery() {
return battery;
}
public Supplier<Double> step = () -> (double) speed * 100 / weight;
public void init(int speed, double weight, Color bodyColor, Color
additionalColor, boolean roga, boolean battery) {
this.speed = speed;
this.weight = weight;
this.bodyColor = bodyColor;
this.additionalColor = additionalColor;
this.roga = roga;
this.battery = battery;
}
}

109
src/FrameTrolleybus.java Normal file
View File

@ -0,0 +1,109 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class FrameTrolleybus extends JFrame {
private DrawingTrolleybus drawingTrolleybus;
private final JComponent pictureBoxTrolleybus;
public FrameTrolleybus() throws IOException {
super("Троллейбус");
setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxTrolleybus = new JComponent(){
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
if (drawingTrolleybus != null) drawingTrolleybus.drawTransport(graphics2D);
super.repaint();
}
};
JButton createButton = new JButton("Создать");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("img/right.png"))));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("img/left.png"))));
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("img/up.png"))));
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("img/down.png"))));
pictureBoxTrolleybus.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
createButton.addActionListener(e -> buttonCreateTrolleybus());
rightButton.setActionCommand("right");
rightButton.addActionListener(this::buttonMoveClick);
leftButton.setActionCommand("left");
leftButton.addActionListener(this::buttonMoveClick);
upButton.setActionCommand("up");
upButton.addActionListener(this::buttonMoveClick);
downButton.setActionCommand("down");
downButton.addActionListener(this::buttonMoveClick);
setLayout(new BorderLayout());
JPanel panelTrolleybus = new JPanel(new BorderLayout());
JPanel createPanel = new JPanel(new BorderLayout());
createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
createPanel.add(createButton, BorderLayout.SOUTH);
JPanel movementPanel = new JPanel(new GridBagLayout());
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.add(movementPanel, BorderLayout.SOUTH);
rightButton.setPreferredSize(new Dimension(30,30));
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 2;
constraints.gridy = 1;
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
movementPanel.add(rightButton, constraints);
leftButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 0;
constraints.gridy = 1;
movementPanel.add(leftButton, constraints);
upButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 1;
constraints.gridy = 0;
movementPanel.add(upButton, constraints);
downButton.setPreferredSize(new Dimension(30,30));
constraints.gridx = 1;
constraints.gridy = 1;
movementPanel.add(downButton, constraints);
add(pictureBoxTrolleybus);
panelTrolleybus.add(rightPanel, BorderLayout.EAST);
panelTrolleybus.add(createPanel, BorderLayout.WEST);
add(panelTrolleybus,BorderLayout.CENTER);
setVisible(true);
}
private void buttonCreateTrolleybus() {
Random random = new Random();
drawingTrolleybus = new DrawingTrolleybus();
pictureBoxTrolleybus.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
drawingTrolleybus.init(random.nextInt(200) + 100, random.nextInt(2000) + 1000,
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
random.nextBoolean(), random.nextBoolean(), pictureBoxTrolleybus.getWidth(), pictureBoxTrolleybus.getHeight(),
(random.nextInt(3)+1)*2);
drawingTrolleybus.setPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
private void buttonMoveClick(ActionEvent event) {
if(drawingTrolleybus == null || drawingTrolleybus.getEntityTrolleybus() == null)
return;
switch (event.getActionCommand())
{
case "left":
drawingTrolleybus.moveTransport(DirectionType.LEFT);
break;
case "right":
drawingTrolleybus.moveTransport(DirectionType.RIGHT);
break;
case "up":
drawingTrolleybus.moveTransport(DirectionType.UP);
break;
case "down":
drawingTrolleybus.moveTransport(DirectionType.DOWN);
break;
}
draw();
}
private void draw() {
if (drawingTrolleybus == null)
{
return;
}
pictureBoxTrolleybus.repaint();
}
}

4
src/Main.java Normal file
View File

@ -0,0 +1,4 @@
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException { new FrameTrolleybus(); }
}