From ac171b277014b8f0902f73d87158ecbf1f9354e6 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Sat, 5 Nov 2022 22:34:57 +0400 Subject: [PATCH 1/3] =?UTF-8?q?LabWork01:=20=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=87=D0=B0=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Direction.java | 6 ++ DrawningTracktor.java | 192 +++++++++++++++++++++++++++++++++++++++ EntityTracktor.java | 31 +++++++ FormTracktor.form | 107 ++++++++++++++++++++++ FormTracktor.java | 82 +++++++++++++++++ Program.java | 9 ++ Resources/arrowDown.png | Bin 0 -> 1265 bytes Resources/arrowLeft.png | Bin 0 -> 1244 bytes Resources/arrowRight.png | Bin 0 -> 1246 bytes Resources/arrowUp.png | Bin 0 -> 1194 bytes 10 files changed, 427 insertions(+) create mode 100644 Direction.java create mode 100644 DrawningTracktor.java create mode 100644 EntityTracktor.java create mode 100644 FormTracktor.form create mode 100644 FormTracktor.java create mode 100644 Program.java create mode 100644 Resources/arrowDown.png create mode 100644 Resources/arrowLeft.png create mode 100644 Resources/arrowRight.png create mode 100644 Resources/arrowUp.png diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..a641b80 --- /dev/null +++ b/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} diff --git a/DrawningTracktor.java b/DrawningTracktor.java new file mode 100644 index 0000000..cb25994 --- /dev/null +++ b/DrawningTracktor.java @@ -0,0 +1,192 @@ +import java.awt.*; + +// Класс, отвечающий за прорисовку и перемещение объекта-сущности +public class DrawningTracktor { + private EntityTracktor Tracktor; // Класс-сущность + public EntityTracktor getTracktor(){ + return Tracktor; + } + private float _startPosX; // Левая координата отрисовки трактора + private float _startPosY; // Верхняя кооридната отрисовки трактора + private Integer _pictureWidth = null; // Ширина окна отрисовки + private Integer _pictureHeight = null; // Высота окна отрисовки + private final int _tracktorWidth = 110; // Ширина отрисовки трактора + private final int _tracktorHeight = 87; // Высота отрисовки трактора + + // Инициализация свойств + public void Init(int speed, float weight, Color bodyColor) + { + Tracktor = new EntityTracktor(); + Tracktor.Init(speed, weight, bodyColor); + } + + // Установка позиции Трактора + public void SetPosition(int x, int y, int width, int height) + { + if (x + _tracktorWidth > width || + x < 0 || + y + _tracktorHeight > height || + y < 0) + { + return; + } + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + + // Изменение направления перемещения + public void MoveTransport(Direction direction) + { + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + switch (direction) + { + // вправо + case Right: + if (_startPosX + _tracktorWidth + Tracktor.getStep() < _pictureWidth) + { + _startPosX += Tracktor.getStep(); + } + break; + //влево + case Left: + if (_startPosX - Tracktor.getStep() > 0) + { + _startPosX -= Tracktor.getStep(); + } + break; + //вверх + case Up: + if (_startPosY - Tracktor.getStep() > 0) + { + _startPosY -= Tracktor.getStep(); + } + break; + //вниз + case Down: + if (_startPosY + _tracktorHeight + Tracktor.getStep() < _pictureHeight) + { + _startPosY += Tracktor.getStep(); + } + break; + } + } + // Отрисовка Трактора + public void DrawTransport(Graphics g) + { + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) + { + return; + } + Color penColor = Color.BLACK; + + // корпус + Color br = Tracktor!=null ? Tracktor.getBodyColor() : Color.GRAY ; + g.setColor(br); + g.fillRect((int)_startPosX + 10, (int)_startPosY + 30, 90, 25); + g.setColor(penColor); + g.drawRect((int)_startPosX + 10, (int)_startPosY + 30, 90, 25); + + // окно + g.setColor(Color.CYAN); + g.fillRect((int)_startPosX + 65, (int)_startPosY + 1, 30, 29); + g.setColor(penColor); + g.drawRect((int)_startPosX + 65, (int)_startPosY + 1, 30, 29); + + // труба + g.setColor(Color.RED); + g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 20); + g.setColor(penColor); + g.drawRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 20); + + // гусеница + g.setColor(Color.DARK_GRAY); + g.fillOval((int)_startPosX + 1, (int)_startPosY + 57, 20, 20); + g.setColor(penColor); + g.drawOval((int)_startPosX + 1, (int)_startPosY + 57, 20, 20); + g.setColor(Color.DARK_GRAY); + g.fillOval((int)_startPosX + 1, (int)_startPosY + 65, 20, 20); + g.setColor(penColor); + g.drawOval((int)_startPosX + 1, (int)_startPosY + 65, 20, 20); + g.setColor(Color.DARK_GRAY); + g.fillOval((int)_startPosX + 90, (int)_startPosY + 57, 20, 20); + g.setColor(penColor); + g.drawOval((int)_startPosX + 90, (int)_startPosY + 57, 20, 20); + g.setColor(Color.DARK_GRAY); + g.fillOval((int)_startPosX + 90, (int)_startPosY + 65, 20, 20); + g.setColor(penColor); + g.drawOval((int)_startPosX + 90, (int)_startPosY + 65, 20, 20); + g.setColor(Color.DARK_GRAY); + g.fillRect((int)_startPosX + 10, (int)_startPosY + 57, 90, 30); + g.fillRect((int)_startPosX + 1, (int)_startPosY + 65, 110, 10); + g.setColor(penColor); + g.drawLine((int)_startPosX + 10, (int)_startPosY + 57, (int)_startPosX + 100, (int)_startPosY + 57); + g.drawLine((int)_startPosX + 10, (int)_startPosY + 86, (int)_startPosX + 100, (int)_startPosY + 86); + g.drawLine((int)_startPosX + 1, (int)_startPosY + 65, (int)_startPosX + 1, (int)_startPosY + 75); + g.drawLine((int)_startPosX + 110, (int)_startPosY + 65, (int)_startPosX + 110, (int)_startPosY + 75); + + // Крупные катки + g.setColor(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22); + g.setColor(penColor); + g.drawOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22); + g.setColor(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22); + g.setColor(penColor); + g.drawOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22); + + // Средние катки + g.setColor(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10); + g.setColor(penColor); + g.drawOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10); + g.setColor(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 50, (int)_startPosY + 73, 10, 10); + g.setColor(penColor); + g.drawOval((int)_startPosX + 50, (int)_startPosY + 73, 10, 10); + g.setColor(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10); + g.setColor(penColor); + g.drawOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10); + + // Малые катки + g.setColor(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6); + g.setColor(penColor); + g.drawOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6); + g.setColor(Color.LIGHT_GRAY); + g.fillOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6); + g.setColor(penColor); + g.drawOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6); + + // Центры крупных катков + g.setColor(Color.BLACK); + g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); + g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); + } + + // Смена границ формы отрисовки + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _tracktorWidth || _pictureHeight <= _tracktorHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _tracktorWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _tracktorWidth; + } + if (_startPosY + _tracktorHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _tracktorHeight; + } + } +} diff --git a/EntityTracktor.java b/EntityTracktor.java new file mode 100644 index 0000000..f6c85e9 --- /dev/null +++ b/EntityTracktor.java @@ -0,0 +1,31 @@ +import java.awt.*; +import java.util.Random; + +// Класс-сущность "Трактор" +public class EntityTracktor { + private int Speed; + private float Weight; + private Color BodyColor; + + public int getSpeed() { + return Speed; + } + public float getWeight(){ + return Weight; + } + public Color getBodyColor(){ + return BodyColor; + } + public float getStep(){ + return Speed * 100 / Weight; + } + + // Инициализация полей объекта-класса Трактора + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; + Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight; + BodyColor = bodyColor; + } +} diff --git a/FormTracktor.form b/FormTracktor.form new file mode 100644 index 0000000..d6e43fa --- /dev/null +++ b/FormTracktor.form @@ -0,0 +1,107 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormTracktor.java b/FormTracktor.java new file mode 100644 index 0000000..b0d7973 --- /dev/null +++ b/FormTracktor.java @@ -0,0 +1,82 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class FormTracktor extends JFrame { + private JPanel ContentPanel; + private JButton buttonCreate; + private JLabel speedLabel; + private JLabel weightLabel; + private JLabel colorLabel; + private JButton buttonLeft; + private JButton buttonDown; + private JButton buttonRight; + private JButton buttonUp; + private JPanel pictureBox; + + private DrawningTracktor _tracktor; + + public FormTracktor(){ + setTitle("Трактор"); + setContentPane(ContentPanel); + setSize(800, 500); + + // Обработка нажатия кнопки "Создать" + buttonCreate.addActionListener(e->{ + Random rnd = new Random(); + _tracktor = new DrawningTracktor(); + _tracktor.Init( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)) + ); + _tracktor.SetPosition( + rnd.nextInt(10, 100), + rnd.nextInt(10, 100), + pictureBox.getWidth(), pictureBox.getHeight() + ); + speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed()); + weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight()); + colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor())); + + repaint(); + }); + + buttonUp.addActionListener(e->{ + if (_tracktor != null){ + _tracktor.MoveTransport(Direction.Up); + repaint(); + } + }); + + buttonLeft.addActionListener(e->{ + if (_tracktor != null){ + _tracktor.MoveTransport(Direction.Left); + repaint(); + } + }); + + buttonDown.addActionListener(e->{ + if (_tracktor != null){ + _tracktor.MoveTransport(Direction.Down); + repaint(); + } + }); + + buttonRight.addActionListener(e->{ + if (_tracktor != null){ + _tracktor.MoveTransport(Direction.Right); + repaint(); + } + }); + } + + @Override + public void paint(Graphics g){ + super.paint(g); + g = pictureBox.getGraphics(); + if (_tracktor != null){ + _tracktor.DrawTransport(g); + } + } +} diff --git a/Program.java b/Program.java new file mode 100644 index 0000000..3f5ddf6 --- /dev/null +++ b/Program.java @@ -0,0 +1,9 @@ +import javax.swing.*; + +public class Program { + public static void main(String[] args){ + FormTracktor formTracktor = new FormTracktor(); + formTracktor.setVisible(true); + formTracktor.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/Resources/arrowDown.png b/Resources/arrowDown.png new file mode 100644 index 0000000000000000000000000000000000000000..cbb69710d90b8f4e73ccd036ef50f7e1076d61c6 GIT binary patch literal 1265 zcmYk5c~Fx_7{q(FU^_NAWSXprzw~9awb@kNBmX&mA=~i^F}kg-=l;5} z()yOY0Ygo_l?laipG_>U-fH7Y5iy^ULd?8SE9n#-VgA58YcTqm*?y3(#oL@vk9pSd%d+KVLRH|2l8h_kD%WD}YOJMI&3EbS`$mCZqZ28@4d()m5or zTI1G!-MADn&Lmyq!)WYN%;X-y=-zm1Y^_eV*^-^ovVF#D$vzan%;edf>zzZLCw{K_B{&0q;I4^@j48zX6K{_q!%l=>vgQU;TPyC0KVq3+>a{4dgu0 z%Dq7O{tFYa70BDI-a{&>=H_TkRuCu$Y9&uy0WD5rT^@kq6U}KYU>$ol|1k>efA;~o zHe~~eD$6Uea}03JIsQqb82BbNeAN}oD79~n&q%7RAW`RP9qhBPQMqcW5#*!f4D)@N z;DAx4732fQ$5|;&+dw`p_7e$eU@+kI_MQ$H)J662b^rr$?)|+45R_i~aw;1 z;z2VqCmk??^n z`nu1xJ24DV#e9Z+Vah2Ta;%=kDJUaAJNfPhVPrjZxwt$ZX6aNh`E_?VlufP3^fd~$ z>scm6Uf3B`5Mu*Hzb|a|WRX!OPaOgy!PM-USh=E%$+Z(a-3+q|RIdl3AebW4A(eV1 zr=W)b%_j0yo*v84&wTA+m`$UO)};0;%0%4Z15HUVJAgVVIoRmw;erMdN<43q(c1B! z6z?re#T@@Vz4n5jUlY?62bSv7gbzy;0n#s>YI?;xdqLA64-Y?DQ?-1McSAvu`X@rP z$hvfulQMNXY_bqPVh2TjaTJ`J?6-v_46dOGWx^|d{+4!S4RwgCT16$!&CQu7mp~_4 LtUrAG|1bIvDa20g literal 0 HcmV?d00001 diff --git a/Resources/arrowLeft.png b/Resources/arrowLeft.png new file mode 100644 index 0000000000000000000000000000000000000000..62da8098e03555e677c063d83f0b31782bab3f5f GIT binary patch literal 1244 zcmX|>e^^pi7{@QESb(!Ny{4jVEnNc6eoTrmm!^qQEL0S;Of*Z8nkA1p?a8z*CjMwe z(z5jfH_}iLP3>9cG(?wLsY@Jc*&dr|l5?X+b4|9r{IfsKd){;I=l!1VeeZd0(QZzd z1Hp}eAc#XaDwv#(dOagwhzl*^pHYJ*O9J&S!Frt_~UGpSD|#JQe)Ch98;8$ zJ@t7%o8}@szIv)(dzDia@jc5Y#(8$@9(hX7cx#wRyro~*R2^MNUrG8kJ0Q5_*-g1y zkAE|b$f=S={xzfi{q5fwo5>fuSIyV=7+2|+c3r#aGj*5a!HCvB$k=>lF2|RbkW2Fp zUn1-NZK5zG@cO0Rj^eb6*CjbNG8fUy^6wl92iqPFq^|LrzmudtN;ZnT@I+NC*N$N4XSdH_ly}`ER5itL=T}!=A|)!M z{|pg{yv8^eTJ%86h(ICD5&|?*X9tkBSGzm(&|N%{CvG4@I>`i7d@BmlSf&J{2&WKK zRju9%QZL7J!OnAy4n*GIFrF68&8q{*+fsm08m4hX-T~vhnKS6BO=UUq184tG-mp~g zjjL-C@-X9S89j?8;^vL^GMlS!ALbL{2cN`S<5N!><|{NIavka~Cb0#@nk)k3`=)8I zzva0dFye-EtyA;V&PxIx(SX1g^3=8+u-_QP1STie1>#7p3shMiS#??u*nrNpji;5` z(9h#^k;cm0E+}FRcR>0PWYSG=M9c&p9#Ka5pycoV9$Qe8xG~X3#7kd6!_<+FpqN*6 z%L;$y4lstcR}$3uo&6Ed8;UUXQ}v>A67^{DahfRQu*F=azNju#YRmmxpy0xU9TeOo z-;Xp#$oxQ^am=1Apr-cOAJ5E{9;sW=Ww{){sm=X%|I{KlkX}g#Cg=?~peEtK0Y=4x z13s3o)H+wQK(Nw@1cGPBLD0OP1@Ply0$b3VnGgN=!Iutie;tF zJHSyCfH^&QfVVFL*w^n9P;OFUN^O(&A;2-^WVvqWf*Bf9v40uBvYI7qLHa=qZ0xE* z0cMm#Bm5OofOo_?0o*?gp50>806wh(&*>rU?+EcPaVDl{7+)J^+(6>D>Eyij9EW&s zT>8hNIs4ift-)10CQ)BI)#TD#VKWqD6cYJAZ5`DOcQ1~`vJ}Zl17DvCwRyL&;8=f$ PQIg#B%KF#(|I+^l;wUhr literal 0 HcmV?d00001 diff --git a/Resources/arrowRight.png b/Resources/arrowRight.png new file mode 100644 index 0000000000000000000000000000000000000000..1e902aea039ae0f21761a89962cb0f3a31a9bce6 GIT binary patch literal 1246 zcmYk4X;4#F6vy)-VJ(XegcMn(f+Kq=LUDmnmXJV0iXjPM(;z4kA~E-9#M3MwiGjSs2W| zY~4Z@&pYJ7f%>qJKsx!0fw}>C@kxu&(zqx20=|W5$=~?7KW*r-v$t5OKYb~anG%wM z&b~Bq-f-u>^Il&jUiI5JaQkZ&g>3d-bKbh-98a%)7SO%SX%@itJ-{EX9nMtyxP{@x}QgF=y_3A07VPwnsp__xa<^m&(oU z{I$E|-x31*5(yT>y*VE)(1r1e=;uDBW~F;_?0q~#BNsKf+Ty3FMB(>$XVwFfG9rYQ zNf`{`FX?{diOvM2R9cXv-hQtUyjkFS5nlIOefCXu$E?4Eyt?AQD7H+c@7ib7JQ~d7 zde#rqT*Ta-xbArk+BTvs;7@UgQ*O?p(N|tKDQFZ{M0?OHkGo>KQX8h0jgmgV`&w4)I854ljtb87^2&MnaO2O!_<+hh|~@^ljzJfvG|)?y&(iv^27v(!>$Qb zn2Z(?qvzwB%-#DZn_VH_rbuVGq*^R~t5XqhmkcIYnbvH5-m*xYEHA8M`hvu1SmJBn^6z>Q%HLuiB|J3~w;GTTa1Nf!-a zSF-xds4K3mypk#5G2}*4%XW@0G{kIdZUK0l0`S3xws6Glt2dDOV1@wT zr!r`#wz~x2%x*}`d{76Ohw2Of_HklMhJNma@VWzpVVM#*8@xzwIqqKiDak{-d&$qg ztFsSX2u?rYKuTQpH^;Gw&$OrZv{h^Gt1xb-KaoFF88dZF)|A5>+$crRPRHGgnWj1(5^8hl)QR9J5LLiqoG`#-0wDMSDO literal 0 HcmV?d00001 diff --git a/Resources/arrowUp.png b/Resources/arrowUp.png new file mode 100644 index 0000000000000000000000000000000000000000..b4c95a16324fa09303b1a1566048fe37cb33fc5d GIT binary patch literal 1194 zcmeAS@N?(olHy`uVBq!ia0vp^av;pX1|+Qw)-3{3jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85o30K$!7fntTONgO#U?V~B_M+v&EOTpUE)ZfmQiF@95;e52v& zjfM^vwP00O1u@SPQw|8}EL#5limB=0ua}McPptg4s)WDxSKUpGm;?J3~gPsN;LNAolvsWtleTi|R8nj`$0>=lDS)m{GgE|&z z*zh>pBvAZ}&?cU>czl`3k(g#sy|F@LJ9)DzUY^qG(V}sM13QXHKvX?I`6?!yJyd4;{ z44$rjF6*2Uns!}%XSH@;a&N%Z#R`Z!UVJNv+@b+f8J zOgkjCZPn^+a{FiQ=VD*~Dol9t){lKlS4I2AENBUWWy-*@}1xA~cS^M=qhj*w>enMu%sz+IgoZW|lM7K}-^1(_cwD5hZ zs6LQ;{>*kmpa*7{2cNjSA>^pgd8 Date: Sat, 5 Nov 2022 23:24:05 +0400 Subject: [PATCH 2/3] =?UTF-8?q?LabWork01:=20=D0=97=D0=B0=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D1=88=D0=B5=D0=BD=D0=BE,=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE?= =?UTF-8?q?=D0=B6=D0=BD=D1=8B=20=D0=BC=D0=B5=D0=BB=D0=BA=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF=D0=BE?= =?UTF-8?q?=20=D0=BE=D1=84=D0=BE=D1=80=D0=BC=D0=BB=D0=B5=D0=BD=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningRollers.java | 65 +++++++++++++++++++++++++++++++++++++++++++ DrawningTracktor.java | 45 ++++-------------------------- FormTracktor.java | 19 +++++++++++-- RollersCount.java | 5 ++++ 4 files changed, 93 insertions(+), 41 deletions(-) create mode 100644 DrawningRollers.java create mode 100644 RollersCount.java diff --git a/DrawningRollers.java b/DrawningRollers.java new file mode 100644 index 0000000..10d8a2e --- /dev/null +++ b/DrawningRollers.java @@ -0,0 +1,65 @@ +import java.awt.*; +import java.util.Random; + +public class DrawningRollers { + private RollersCount rollersCount; + private Color colorRollers; + + public void setRollersCount(int count){ + switch (count) { + case 4 -> rollersCount = RollersCount.Four; + case 5 -> rollersCount = RollersCount.Five; + case 6 -> rollersCount = RollersCount.Six; + default -> rollersCount = RollersCount.Four; + } + } + + public void Init(int count, Color colorRollers){ + setRollersCount(count); + this.colorRollers = colorRollers; + } + + public void DrawRollers(Graphics g, float _startPosX, float _startPosY){ + Color penColor = Color.BLACK; + Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers; + + // Крупные катки - всегда + g.setColor(mainColor); + g.fillOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22); + g.setColor(penColor); + g.drawOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22); + g.setColor(mainColor); + g.fillOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22); + g.setColor(penColor); + g.drawOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22); + + // Малые катки - всегда + g.setColor(mainColor); + g.fillOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6); + g.setColor(penColor); + g.drawOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6); + g.setColor(mainColor); + g.fillOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6); + g.setColor(penColor); + g.drawOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6); + + // Средние катки - не всегда + switch (rollersCount){ + case Six: + g.setColor(mainColor); + g.fillOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10); + g.setColor(penColor); + g.drawOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10); + case Five: + g.setColor(mainColor); + g.fillOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10); + g.setColor(penColor); + g.drawOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10); + } + + // Центры крупных катков + g.setColor(Color.BLACK); + g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); + g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); + } +} diff --git a/DrawningTracktor.java b/DrawningTracktor.java index cb25994..e06d3b6 100644 --- a/DrawningTracktor.java +++ b/DrawningTracktor.java @@ -13,11 +13,15 @@ public class DrawningTracktor { private final int _tracktorWidth = 110; // Ширина отрисовки трактора private final int _tracktorHeight = 87; // Высота отрисовки трактора + private DrawningRollers drawningRollers; + // Инициализация свойств - public void Init(int speed, float weight, Color bodyColor) + public void Init(int speed, float weight, Color bodyColor, int countRollers) { Tracktor = new EntityTracktor(); Tracktor.Init(speed, weight, bodyColor); + drawningRollers = new DrawningRollers(); + drawningRollers.Init(countRollers, bodyColor); } // Установка позиции Трактора @@ -129,44 +133,7 @@ public class DrawningTracktor { g.drawLine((int)_startPosX + 1, (int)_startPosY + 65, (int)_startPosX + 1, (int)_startPosY + 75); g.drawLine((int)_startPosX + 110, (int)_startPosY + 65, (int)_startPosX + 110, (int)_startPosY + 75); - // Крупные катки - g.setColor(Color.LIGHT_GRAY); - g.fillOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22); - g.setColor(penColor); - g.drawOval((int)_startPosX + 5, (int)_startPosY + 60, 22, 22); - g.setColor(Color.LIGHT_GRAY); - g.fillOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22); - g.setColor(penColor); - g.drawOval((int)_startPosX + 83, (int)_startPosY + 60, 22, 22); - - // Средние катки - g.setColor(Color.LIGHT_GRAY); - g.fillOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10); - g.setColor(penColor); - g.drawOval((int)_startPosX + 33, (int)_startPosY + 73, 10, 10); - g.setColor(Color.LIGHT_GRAY); - g.fillOval((int)_startPosX + 50, (int)_startPosY + 73, 10, 10); - g.setColor(penColor); - g.drawOval((int)_startPosX + 50, (int)_startPosY + 73, 10, 10); - g.setColor(Color.LIGHT_GRAY); - g.fillOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10); - g.setColor(penColor); - g.drawOval((int)_startPosX + 68, (int)_startPosY + 73, 10, 10); - - // Малые катки - g.setColor(Color.LIGHT_GRAY); - g.fillOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6); - g.setColor(penColor); - g.drawOval((int)_startPosX + 43, (int)_startPosY + 58, 6, 6); - g.setColor(Color.LIGHT_GRAY); - g.fillOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6); - g.setColor(penColor); - g.drawOval((int)_startPosX + 61, (int)_startPosY + 58, 6, 6); - - // Центры крупных катков - g.setColor(Color.BLACK); - g.fillOval((int)_startPosX + 13, (int)_startPosY + 68, 6, 6); - g.fillOval((int)_startPosX + 91, (int)_startPosY + 68, 6, 6); + drawningRollers.DrawRollers(g,_startPosX, _startPosY); } // Смена границ формы отрисовки diff --git a/FormTracktor.java b/FormTracktor.java index b0d7973..3d092b2 100644 --- a/FormTracktor.java +++ b/FormTracktor.java @@ -1,5 +1,9 @@ import javax.swing.*; import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.util.Random; public class FormTracktor extends JFrame { @@ -28,7 +32,8 @@ public class FormTracktor extends JFrame { _tracktor.Init( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)) + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + rnd.nextInt(3,8) ); _tracktor.SetPosition( rnd.nextInt(10, 100), @@ -38,7 +43,6 @@ public class FormTracktor extends JFrame { speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed()); weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight()); colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor())); - repaint(); }); @@ -69,6 +73,17 @@ public class FormTracktor extends JFrame { repaint(); } }); + + pictureBox.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + if (_tracktor != null){ + _tracktor.ChangeBorders(e.getComponent().getWidth(), e.getComponent().getHeight()); + repaint(); + } + } + }); } @Override diff --git a/RollersCount.java b/RollersCount.java new file mode 100644 index 0000000..7b9cdbe --- /dev/null +++ b/RollersCount.java @@ -0,0 +1,5 @@ +public enum RollersCount { + Four, + Five, + Six +} -- 2.25.1 From 92a11ea687ef37c126ef8407f4e2c5ca7ec533e3 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Sat, 5 Nov 2022 23:32:00 +0400 Subject: [PATCH 3/3] =?UTF-8?q?LabWork01:=20=D0=97=D0=B0=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D1=88=D0=B5=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningTracktor.java | 1 + FormTracktor.java | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/DrawningTracktor.java b/DrawningTracktor.java index e06d3b6..81d5ab1 100644 --- a/DrawningTracktor.java +++ b/DrawningTracktor.java @@ -79,6 +79,7 @@ public class DrawningTracktor { break; } } + // Отрисовка Трактора public void DrawTransport(Graphics g) { diff --git a/FormTracktor.java b/FormTracktor.java index 3d092b2..74b9158 100644 --- a/FormTracktor.java +++ b/FormTracktor.java @@ -2,8 +2,6 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; import java.util.Random; public class FormTracktor extends JFrame { @@ -29,17 +27,20 @@ public class FormTracktor extends JFrame { buttonCreate.addActionListener(e->{ Random rnd = new Random(); _tracktor = new DrawningTracktor(); + _tracktor.Init( rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), rnd.nextInt(3,8) ); + _tracktor.SetPosition( rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight() ); + speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed()); weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight()); colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor())); -- 2.25.1