From 523e7d8412e3d3b38ed6317fd4cc15f47d89b189 Mon Sep 17 00:00:00 2001 From: asakky Date: Sat, 8 Jun 2024 06:17:18 +0400 Subject: [PATCH] LabWork01 --- ProjectMotorBoat/.idea/vcs.xml | 6 + ProjectMotorBoat/src/CanvasMotorBoat.java | 17 +++ ProjectMotorBoat/src/DirectionType.java | 6 + ProjectMotorBoat/src/DrawingMotorBoat.java | 145 +++++++++++++++++++++ ProjectMotorBoat/src/DrawingPaddles.java | 39 ++++++ ProjectMotorBoat/src/EntityMotorBoat.java | 25 ++++ ProjectMotorBoat/src/FormMotorBoat.java | 123 +++++++++++++++++ ProjectMotorBoat/src/Main.java | 5 +- ProjectMotorBoat/src/NumberOfPaddles.java | 11 ++ ProjectMotorBoat/src/images/down.png | Bin 0 -> 407 bytes ProjectMotorBoat/src/images/left.png | Bin 0 -> 463 bytes ProjectMotorBoat/src/images/right.png | Bin 0 -> 446 bytes ProjectMotorBoat/src/images/up.png | Bin 0 -> 413 bytes 13 files changed, 376 insertions(+), 1 deletion(-) create mode 100644 ProjectMotorBoat/.idea/vcs.xml create mode 100644 ProjectMotorBoat/src/CanvasMotorBoat.java create mode 100644 ProjectMotorBoat/src/DirectionType.java create mode 100644 ProjectMotorBoat/src/DrawingMotorBoat.java create mode 100644 ProjectMotorBoat/src/DrawingPaddles.java create mode 100644 ProjectMotorBoat/src/EntityMotorBoat.java create mode 100644 ProjectMotorBoat/src/FormMotorBoat.java create mode 100644 ProjectMotorBoat/src/NumberOfPaddles.java create mode 100644 ProjectMotorBoat/src/images/down.png create mode 100644 ProjectMotorBoat/src/images/left.png create mode 100644 ProjectMotorBoat/src/images/right.png create mode 100644 ProjectMotorBoat/src/images/up.png diff --git a/ProjectMotorBoat/.idea/vcs.xml b/ProjectMotorBoat/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/ProjectMotorBoat/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ProjectMotorBoat/src/CanvasMotorBoat.java b/ProjectMotorBoat/src/CanvasMotorBoat.java new file mode 100644 index 0000000..02088e5 --- /dev/null +++ b/ProjectMotorBoat/src/CanvasMotorBoat.java @@ -0,0 +1,17 @@ +import javax.swing.*; +import java.awt.*; + +public class CanvasMotorBoat extends JComponent { + public DrawingMotorBoat _drawingMotorBoat; + public CanvasMotorBoat(){} + public void paintComponent(Graphics g) { + if (_drawingMotorBoat == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawingMotorBoat.DrawTransport(g2d); + super.repaint(); + } +} + diff --git a/ProjectMotorBoat/src/DirectionType.java b/ProjectMotorBoat/src/DirectionType.java new file mode 100644 index 0000000..35657f0 --- /dev/null +++ b/ProjectMotorBoat/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} diff --git a/ProjectMotorBoat/src/DrawingMotorBoat.java b/ProjectMotorBoat/src/DrawingMotorBoat.java new file mode 100644 index 0000000..25a58c7 --- /dev/null +++ b/ProjectMotorBoat/src/DrawingMotorBoat.java @@ -0,0 +1,145 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawingMotorBoat extends JPanel { + public EntityMotorBoat EntityMotorBoat; + public DrawingPaddles drawingPaddles = null; + private Integer picture_width; + private Integer picture_height; + private Integer _StartPosX; + private Integer _StartPosY; + private int drawingBoatWidth = 140; + private int drawingBoatHeight = 110; + public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean glass, boolean engine) { + EntityMotorBoat = new EntityMotorBoat(); + EntityMotorBoat.Init(speed, weight, bodycolor, additionalcolor, glass, engine); + picture_width = null; + picture_height = null; + _StartPosX = null; + _StartPosY = null; + drawingPaddles = new DrawingPaddles(); + drawingPaddles.setNumberOfPaddles((int)(Math.random() * 4 + 0)); + } + public boolean SetPictureSize(int width, int height) { + if (width < drawingBoatWidth || height < drawingBoatHeight) return false; + picture_width = width; + picture_height = height; + if (_StartPosX != null || _StartPosY != null) { + if (_StartPosX + drawingBoatWidth > picture_width) + { + _StartPosX = _StartPosX - (_StartPosX + drawingBoatWidth - picture_width); + } + else if (_StartPosX < 10) _StartPosX = 10; + if (_StartPosY + drawingBoatHeight + 15 > picture_height) + { + _StartPosY = 15+_StartPosY - (_StartPosY + drawingBoatHeight - picture_height); + } + else if (_StartPosY < 10) _StartPosY = 10; + } + return true; + } + public void SetPosition(int x, int y) { + if (!(picture_width != null && picture_height != null)) return; + if (x + drawingBoatWidth > picture_width) + { + _StartPosX = x - (x + drawingBoatWidth - picture_width); + } + else if (x < 10) _StartPosX = 10; + else _StartPosX = x; + if (y + drawingBoatHeight + 15 > picture_height) + { + _StartPosY = 15 + y - (y + drawingBoatHeight - picture_height); + } + else if (y < 10) _StartPosY = 15; + else _StartPosY = y; + } + public boolean MoveTransport(DirectionType direction) { + if (EntityMotorBoat == null || _StartPosX == null || _StartPosY == null) return false; + switch (direction) { + case Left: + if (_StartPosX - EntityMotorBoat.Step > 0) { + _StartPosX -= (int)EntityMotorBoat.Step; + } + return true; + case Up: + if (_StartPosY - (EntityMotorBoat.Step+15) > 0) + { + _StartPosY -= (int)EntityMotorBoat.Step; + } + return true; + case Right: + if (_StartPosX + drawingBoatWidth + (int)EntityMotorBoat.Step < picture_width - EntityMotorBoat.Step) + { + _StartPosX += (int)EntityMotorBoat.Step; + } + return true; + case Down: + if (_StartPosY + drawingBoatHeight + (int)EntityMotorBoat.Step < picture_height - EntityMotorBoat.Step) + { + _StartPosY += (int)EntityMotorBoat.Step; + } + return true; + default: + return false; + } + } + public void DrawTransport(Graphics2D g) { + if (EntityMotorBoat == null || _StartPosX == null || _StartPosY == null) return; + int y = _StartPosY; + + g.setColor(EntityMotorBoat.getBodyColor()); + if (drawingPaddles.getNumberOfPaddles() != null) { + switch (drawingPaddles.getNumberOfPaddles()) { + case OnePaddle: + drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityMotorBoat.getBodyColor()); + + break; + case TwoPaddles: + drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityMotorBoat.getBodyColor()); + drawingPaddles.DrawPaddles(g, _StartPosX + 45, y , 100, 15, EntityMotorBoat.getBodyColor()); + + break; + case ThreePaddles: + drawingPaddles.DrawPaddles(g, _StartPosX + 15, y, 100, 15, EntityMotorBoat.getBodyColor()); + drawingPaddles.DrawPaddles(g, _StartPosX + 30, y , 100, 15, EntityMotorBoat.getBodyColor()); + drawingPaddles.DrawPaddles(g, _StartPosX + 45, y , 100, 15, EntityMotorBoat.getBodyColor()); + + break; + } + } + + Point[] hull = new Point[] { + new Point(_StartPosX + 5, _StartPosY), + new Point(_StartPosX + 100, _StartPosY), + new Point(_StartPosX + 140, _StartPosY + 35), + new Point(_StartPosX + 100, _StartPosY + 70), + new Point(_StartPosX + 5, _StartPosY + 70), + }; + + Polygon hullPolygon = new Polygon(); + for (Point point : hull) { + hullPolygon.addPoint(point.x, point.y); + } + g.setColor(EntityMotorBoat.getBodyColor()); + g.fillPolygon(hullPolygon); + g.setColor(Color.BLACK); + g.drawPolygon(hullPolygon); + + + if (EntityMotorBoat.getGlass()) { + //стекло + g.setColor(Color.blue); + g.fillRect(_StartPosX + 20, _StartPosY + 15, 80, 40); + g.setColor(Color.BLACK); + g.drawRect(_StartPosX + 20, _StartPosY + 15, 80, 40); + } + + // Двигатель + if (EntityMotorBoat.getEngine()) { + g.setColor(EntityMotorBoat.getAdditionalColor()); + g.fillRect(_StartPosX, _StartPosY + 10, 5, 50); + g.setColor(Color.BLACK); + g.drawRect(_StartPosX, _StartPosY + 10, 5, 50); + } + } +} diff --git a/ProjectMotorBoat/src/DrawingPaddles.java b/ProjectMotorBoat/src/DrawingPaddles.java new file mode 100644 index 0000000..b79efda --- /dev/null +++ b/ProjectMotorBoat/src/DrawingPaddles.java @@ -0,0 +1,39 @@ +import java.awt.*; + +public class DrawingPaddles { + private NumberOfPaddles numberOfPaddles; + public void setNumberOfPaddles(int numberofpaddle) { + for (NumberOfPaddles numofenum : NumberOfPaddles.values()) { + if (numofenum.getNumpaddles() == numberofpaddle) { + numberOfPaddles = numofenum; + return; + } + } + } + public NumberOfPaddles getNumberOfPaddles() { + return numberOfPaddles; + } + public void DrawPaddles(Graphics g, int x, int y, int width, int height, Color bodyColor) { + + g.setColor(Color.BLACK); + // Весло слева + int paddleLeftX1 = x + 35; + int paddleLeftY1 = y + 10; + int paddleLeftX2 = x + 35; + int paddleLeftY2 = y - 10; + g.drawLine(paddleLeftX1, paddleLeftY1, paddleLeftX2, paddleLeftY2); + + // Весло справа + int paddleRightX1 = x + 35; + int paddleRightY1 = y + 60; + int paddleRightX2 = x + 35; + int paddleRightY2 = y + 80; + g.drawLine(paddleRightX1, paddleRightY1, paddleRightX2, paddleRightY2); + + // Лопасти весел + g.setColor(Color.BLACK); + g.fillOval(paddleLeftX2 - 5, paddleLeftY2 - 5, 10, 10); + g.fillOval(paddleRightX2 - 5, paddleRightY2 - 5, 10, 10); + } +} + diff --git a/ProjectMotorBoat/src/EntityMotorBoat.java b/ProjectMotorBoat/src/EntityMotorBoat.java new file mode 100644 index 0000000..7150968 --- /dev/null +++ b/ProjectMotorBoat/src/EntityMotorBoat.java @@ -0,0 +1,25 @@ +import java.awt.*; +public class EntityMotorBoat { + private int Speed; + private double Weight; + private Color BodyColor; + public Color getBodyColor() {return BodyColor;} + private Color AdditionalColor; + public Color getAdditionalColor() {return AdditionalColor;} + private boolean Glass; + public boolean getGlass() {return Glass;} + private boolean Engine; + public boolean getEngine() {return Engine;} + public double Step; + public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean glass, boolean engine) + { + Speed = speed; + Weight = weight; + BodyColor = bodycolor; + AdditionalColor = additionalcolor; + Glass = glass; + Engine = engine; + Step = Speed * 100 / Weight; + } +} + diff --git a/ProjectMotorBoat/src/FormMotorBoat.java b/ProjectMotorBoat/src/FormMotorBoat.java new file mode 100644 index 0000000..ef5a983 --- /dev/null +++ b/ProjectMotorBoat/src/FormMotorBoat.java @@ -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 FormMotorBoat extends JFrame { + private String title; + private Dimension dimension; + private int Width, Height; + private CanvasMotorBoat canvasMotorBoat = new CanvasMotorBoat(); + private JButton CreateButton = new JButton("Create");; + private JButton UpButton = new JButton(); + private JButton DownButton = new JButton();; + private JButton LeftButton = new JButton();; + private JButton RightButton = new JButton(); + public FormMotorBoat(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("src\\images\\up.png"); + UpButton.setIcon(iconUp); + UpButton.setName("UP"); + DownButton.setName("DOWN"); + Icon iconDown = new ImageIcon("src\\images\\down.png"); + DownButton.setIcon(iconDown); + LeftButton.setName("LEFT"); + Icon iconLeft = new ImageIcon("src\\images\\left.png"); + LeftButton.setIcon(iconLeft); + RightButton.setName("RIGHT"); + Icon iconRight = new ImageIcon("src\\images\\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 = (double)(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 engine = new Random().nextBoolean(); + boolean glass = new Random().nextBoolean();; + canvasMotorBoat._drawingMotorBoat = new DrawingMotorBoat(); + canvasMotorBoat._drawingMotorBoat.Init(speed, weight, bodyColor, additionalColor, glass, engine); + canvasMotorBoat._drawingMotorBoat.SetPictureSize(Width, Height); + canvasMotorBoat._drawingMotorBoat.SetPosition( StartPositionX, StartPositionY); + canvasMotorBoat.repaint(); + } + }); + + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + if (canvasMotorBoat._drawingMotorBoat == null) return; + boolean result = false; + switch ((((JButton)(event.getSource())).getName())) { + case "UP": + result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Up); + break; + case "DOWN": + result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Down); + break; + case "LEFT": + result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Left); + break; + case "RIGHT": + result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Right); + break; + } + if (result) { + canvasMotorBoat.repaint(); + } + } + }; + UpButton.addActionListener(actionListener); + DownButton.addActionListener(actionListener); + LeftButton.addActionListener(actionListener); + RightButton.addActionListener(actionListener); + + setSize(dimension.width,dimension.height); + setLayout(null); + canvasMotorBoat.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 100, 40); + UpButton.setBounds(getWidth() - 140, getHeight() - 160, 35, 34); + DownButton.setBounds(getWidth() - 140, getHeight() - 100, 35, 34); + RightButton.setBounds(getWidth() - 80, getHeight() - 100, 35, 34); + LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 35, 34); + add(CreateButton); + add(UpButton); + add(DownButton); + add(RightButton); + add(LeftButton); + add(canvasMotorBoat); + setVisible(true); + //обработка события изменения размеров окна + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + Width = getWidth() - 15; + Height = getHeight() - 35; + if (canvasMotorBoat._drawingMotorBoat != null) + canvasMotorBoat._drawingMotorBoat.SetPictureSize(Width, Height); + canvasMotorBoat.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 100, 40); + UpButton.setBounds(getWidth() - 140, getHeight() - 160, 35, 34); + DownButton.setBounds(getWidth() - 140, getHeight() - 100, 35, 34); + RightButton.setBounds(getWidth() - 80, getHeight() - 100, 35, 34); + LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 35, 34); + } + }); + } +} diff --git a/ProjectMotorBoat/src/Main.java b/ProjectMotorBoat/src/Main.java index 3e59c38..0389743 100644 --- a/ProjectMotorBoat/src/Main.java +++ b/ProjectMotorBoat/src/Main.java @@ -1,5 +1,8 @@ +import java.awt.*; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + FormMotorBoat form = new FormMotorBoat("Моторная лодка", new Dimension(500,500)); + form.Init(); } } \ No newline at end of file diff --git a/ProjectMotorBoat/src/NumberOfPaddles.java b/ProjectMotorBoat/src/NumberOfPaddles.java new file mode 100644 index 0000000..ed81e78 --- /dev/null +++ b/ProjectMotorBoat/src/NumberOfPaddles.java @@ -0,0 +1,11 @@ +public enum NumberOfPaddles { + OnePaddle(1), + TwoPaddles(2), + ThreePaddles(3); + private int numberofpaddles; + NumberOfPaddles(int numberofpaddles) { + this.numberofpaddles = numberofpaddles; + } + public int getNumpaddles() {return numberofpaddles;} +} + diff --git a/ProjectMotorBoat/src/images/down.png b/ProjectMotorBoat/src/images/down.png new file mode 100644 index 0000000000000000000000000000000000000000..b1e74ad26da962539bbc909637518ea6f876350e GIT binary patch literal 407 zcmV;I0cie-P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0W3*GK~z{r-IzfR zf-n$8iwos4UV%$^A@^LvYp~#2CPe4!Z>$1krlk>m$%I;_!%MLRt!{$b06QMPj%h}! zs$$EA<`U)^IZL3dHnbGIHCG-r6cst}sG<0mwP_l*;uII@`<|^h#YME3M2hZx*heg2 zh%8|=7Q<5hH*bvv>mL07GXmt+v$f1NX9Q2zsR-%1jx9UU+Rw%F0$o_17n}+L{XR6y zH_S(%&&S^P1-ySENYd^VDe)-*#yvt^*N@~9GE)*@x|ggiGbaJ&&j@YXJ_fHLgF3@7 zu<{tmih}~VGQLKP_*s%8qmcm186-tUD*<#_#3Eyn0QyX#k+DhuTQ-Twq(}h!2r?&= zA_1~~g!_k<$hf1#Ng}me)@wj`(Hse|k-sIJy8{~6Dc&11rpo{T002ovPDHLkV1n6A BqO|}3 literal 0 HcmV?d00001 diff --git a/ProjectMotorBoat/src/images/left.png b/ProjectMotorBoat/src/images/left.png new file mode 100644 index 0000000000000000000000000000000000000000..2ec2c67a64604d70524350b264ba2f685d82f4f1 GIT binary patch literal 463 zcmV;=0WkiFP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0c1%;K~z{r&6Znk zgD?<94L=e4=sNgeVQs%zOP9dDP$ZQ44h#xo#@Not$&s#j`Ef7?+&nz4kfv$e3)K&_ z+cq0WSduN$bsb6kY{yn)i_G&(5?||~9hf62jN^#O@bD6t6_kwXmn5}>4y6QARYa4Z z7!qwQCSf@mKy58L5qKDxAA;RDg(H@6YM7P9#rj`Vviqw=q&P3>OVLpJ^bG1aBBiQ_5w^Ful5A1IZ ztu3%qfwK|W|Gv0@0?0c!ECbl`9WudsBvpn-Po|X9g;A;u%5fMe_ME$qhPpyam0_Nx zWct3RGB+Qsy~|ZR|9fO1oV}=;g*qY(cP8(|PGRLz1VCn4mOzF_%Yq~k(6%iJ*>!y+ zS%hI3Ogu}nMF3=CuVM*BB7i5c6~!XJQsyNQOFl%zxqodq<>j|csbByA002ovPDHLk FV1o2Qx$*!2 literal 0 HcmV?d00001 diff --git a/ProjectMotorBoat/src/images/right.png b/ProjectMotorBoat/src/images/right.png new file mode 100644 index 0000000000000000000000000000000000000000..95973828540ce25a865870f3dc0c5ae968b4455d GIT binary patch literal 446 zcmV;v0YUzWP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0aHmtK~z{r#gm#)sPXZ}I^NC#>lQ_Hwa0_3=ka6E~s6K`< zLK3G50I8NMajJmupb&{B0-qG`TbD}0nC=NdiK*GbzyN zNhNZVgKB~O?2jY|ssfUHo@X9SQ~geer{_-X7X=AmLO7g_%71|*ABMqvKY~gHKL1eMRf##mH+?%07*qoM6N<$g26$tSO5S3 literal 0 HcmV?d00001 diff --git a/ProjectMotorBoat/src/images/up.png b/ProjectMotorBoat/src/images/up.png new file mode 100644 index 0000000000000000000000000000000000000000..8fceff9519e5004103da2be36dbe6a05909befd0 GIT binary patch literal 413 zcmV;O0b>4%P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0WwKMK~z{r-IzfR zf-n$8!G+-<-h%}flQn1Y2yD2O3!*b+hCnUtblN81OMYl@TArm^wEh6q3AoWwMP1ib z55Z$9Y7xBni`q!C4BnbpOt(#*5ImH_J zx~`+vf@^HdmZayAoQOqoJpA~$-i#pnRTJhkGlHvEgeesUKf