From a6fede83c9c9f18409806762c9ad75dbbee8a5e9 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sun, 29 Oct 2023 02:01:32 +0400 Subject: [PATCH 1/3] Lab1 --- src/Main.java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/Main.java diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..fb7039d --- /dev/null +++ b/src/Main.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class Main { +} -- 2.25.1 From 2856775babeb6d3b9afea74941cfdd96e9b7f31c Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sun, 29 Oct 2023 02:06:47 +0400 Subject: [PATCH 2/3] Done --- .idea/.gitignore | 8 ++ .idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml | 12 ++ .idea/misc.xml | 6 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 7 + img/down.png | Bin 0 -> 282 bytes img/left.png | Bin 0 -> 295 bytes img/right.png | Bin 0 -> 283 bytes img/up.png | Bin 0 -> 276 bytes src/DirectionType.java | 6 + src/DoorsNumber.java | 5 + src/DrawingDoors.java | 23 ++++ src/DrawingTrolleybus.java | 125 ++++++++++++++++++ src/EntityTrolleybus.java | 37 ++++++ src/FrameTrolleybus.java | 109 +++++++++++++++ 15 files changed, 346 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 img/down.png create mode 100644 img/left.png create mode 100644 img/right.png create mode 100644 img/up.png create mode 100644 src/DirectionType.java create mode 100644 src/DoorsNumber.java create mode 100644 src/DrawingDoors.java create mode 100644 src/DrawingTrolleybus.java create mode 100644 src/EntityTrolleybus.java create mode 100644 src/FrameTrolleybus.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml b/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml new file mode 100644 index 0000000..6e2c775 --- /dev/null +++ b/.idea/PIbd-23_Yunusov.N.N_Trolleybus_Hard.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..81fb09f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..8306744 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/img/down.png b/img/down.png new file mode 100644 index 0000000000000000000000000000000000000000..50d531f7fa0581929210920755f68be9a46fb1fe GIT binary patch literal 282 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jljKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!Kt1ujv*f2Z?7HXYf#{5NPPMC{>>XHGdmWr z&HbZadSQLq&ZarVS&^s zCQ`v_v&GFad{lhIH@|$_a{pxDQ*RS%yQR|6Lh-kstnE7hbRb`9glC$sFM}44%>l$9 Sa4C2)i1Kvxb6Mw<&;$U)hwI~R%2G00>U-Gnv3#Xav zug$Y=ojAts=yYc3lqcHHYk23Z|MRN&+S^OTIZqbN4ZA)kt~k?4e|O47!RdDX9v7B= zT=iIwH>1&op0O1}z|)1BgN3Qt)ID<>~6@vd$@?2>_=OapnL3 literal 0 HcmV?d00001 diff --git a/img/right.png b/img/right.png new file mode 100644 index 0000000000000000000000000000000000000000..8af2e6f63fc5cb95a79c8c5301c34f6807bef4ed GIT binary patch literal 283 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jljKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!D*f@jv*f2S1&m7H7M|~2EMD$bdl(A;hK7l zHD7)I%vQx$Ej|3QS4&@-?OC_x{fn+INq+S#YD=Y}TdXg&7wcM7~&J~MT8<~Gf{f&JG-zuukxLiZT=n|T773=i8s0$s?L8sVAd>&u`8WOD#9 T2wV!D45B<;{an^LB{Ts5VTEL9 literal 0 HcmV?d00001 diff --git a/img/up.png b/img/up.png new file mode 100644 index 0000000000000000000000000000000000000000..7b3dfedeaed55141011278580faa8e70c04d507d GIT binary patch literal 276 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1SGcvS$+jljKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!G2E{#}E(iw^tAHwJ7ko2EO|LzgSgMP!fIU}H&<`>~I#h(4y!N9`x6V@({xjThh?DDeiJ30$wm9E%Y#e8GmW%6li z@wpkk&Fij6o4rsBHwrhgedzV}QS-bq)va+ar>`q<=la{J9m-XIhi7W!W@zSIcMG+$o^Eg+i%h(X{|@MI9> M>FVdQ&MBb@0K?2>S^xk5 literal 0 HcmV?d00001 diff --git a/src/DirectionType.java b/src/DirectionType.java new file mode 100644 index 0000000..0d664bb --- /dev/null +++ b/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + UP, + DOWN, + LEFT, + RIGHT +} \ No newline at end of file diff --git a/src/DoorsNumber.java b/src/DoorsNumber.java new file mode 100644 index 0000000..cba85c9 --- /dev/null +++ b/src/DoorsNumber.java @@ -0,0 +1,5 @@ +public enum DoorsNumber { + THREE, + FOUR, + FIVE +} \ No newline at end of file diff --git a/src/DrawingDoors.java b/src/DrawingDoors.java new file mode 100644 index 0000000..3ab1642 --- /dev/null +++ b/src/DrawingDoors.java @@ -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); + } + } +} \ No newline at end of file diff --git a/src/DrawingTrolleybus.java b/src/DrawingTrolleybus.java new file mode 100644 index 0000000..7e4b69b --- /dev/null +++ b/src/DrawingTrolleybus.java @@ -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); + } +} \ No newline at end of file diff --git a/src/EntityTrolleybus.java b/src/EntityTrolleybus.java new file mode 100644 index 0000000..c468cd9 --- /dev/null +++ b/src/EntityTrolleybus.java @@ -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 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; + } +} \ No newline at end of file diff --git a/src/FrameTrolleybus.java b/src/FrameTrolleybus.java new file mode 100644 index 0000000..c684551 --- /dev/null +++ b/src/FrameTrolleybus.java @@ -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(); + } +} \ No newline at end of file -- 2.25.1 From 66bb3f0907d9665f975f07f2975eba0b3257b88b Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sun, 29 Oct 2023 02:13:51 +0400 Subject: [PATCH 3/3] Done++ --- src/Main.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index fb7039d..dabb840 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,2 +1,4 @@ -package PACKAGE_NAME;public class Main { +import java.io.IOException; +public class Main { + public static void main(String[] args) throws IOException { new FrameTrolleybus(); } } -- 2.25.1