Ihonkina E.S. Hard LabWork01 #2

Closed
k_ihnkn wants to merge 2 commits from LabWork01 into master
19 changed files with 569 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
ProjectMotorBoatHard.iml

View File

@ -0,0 +1,9 @@
<?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$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

7
.idea/encodings.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/FormBoat.java" charset="UTF-8" />
<file url="PROJECT" charset="windows-1251" />
</component>
</project>

6
.idea/misc.xml Normal file
View File

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

8
.idea/modules.xml 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$/ProjectMotorBoatHard.iml" filepath="$PROJECT_DIR$/ProjectMotorBoatHard.iml" />
</modules>
</component>
</project>

7
.idea/vcs.xml Normal file
View File

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

11
ProjectMotorBoatHard.iml Normal file
View File

@ -0,0 +1,11 @@
<?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$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,13 @@
public enum AdditionalDirection {
OnePaddle(1),
TwoPaddle(2),
ThreePaddle(3);
private final int count;
AdditionalDirection(int value){
count = value;
}
public int getCount() {
return count;
}
}

6
src/Direction.java Normal file
View File

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

119
src/DrawingBoat.java Normal file
View File

@ -0,0 +1,119 @@
import javax.swing.*;
import java.awt.*;
public class DrawingBoat extends JComponent {
public EntityBoat Boat;
private float _startPosX;
private float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private final int _boatWidth = 90;
private final int _boatHeight = 80;
private DrawingPaddle _paddles;
public DrawingBoat() {
super();
}
public void Init(int speed, float weight, Color bodyColor, int paddleCount)
{
Boat = new EntityBoat();
Boat.Init(speed, weight, bodyColor);
_paddles = new DrawingPaddle();
_paddles.SetPaddlesAmount(paddleCount);
}
public void SetPosition(int x, int y, int width, int height)
{
if (width <= _boatWidth + x || height <= _boatHeight + y || x<0 || y<0)
{
_pictureWidth = null;
_pictureHeight = null;
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 + _boatWidth + Boat.Step() < _pictureWidth) {
_startPosX += Boat.Step();
}
break;
}
case Left:
if (_startPosX - Boat.Step() > 0)
{
_startPosX -= Boat.Step();
}
break;
case Up:
if (_startPosY - Boat.Step() > 0)
{
_startPosY -= Boat.Step();
}
break;
case Down:
if (_startPosY + _boatHeight + Boat.Step() < _pictureHeight)
{
_startPosY += Boat.Step();
}
break;
}
}
public void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g=(Graphics2D)gr;
if (_startPosX < 0 || _startPosY < 0
|| _pictureHeight==null || _pictureWidth==null)
{
return;
}
Color pen = new Color(0,0,0);
//границы лодки
int [] pointsX = new int[]{(int)(_startPosX),(int)(_startPosX + 50),(int)(_startPosX + 70),(int)(_startPosX + 50),(int)(_startPosX)};
int [] pointsY = new int[]{(int)(_startPosY),(int)(_startPosY),(int)(_startPosY + 20),(int)(_startPosY + 40),(int)(_startPosY+40)};
try { pen = Boat.BodyColor(); }
catch (Exception e) {}
g.setPaint(pen);
g.fillPolygon(pointsX,pointsY,5);
g.setPaint(Color.black);
g.drawPolygon(pointsX,pointsY,5);
g.setPaint(Color.yellow);
g.fillOval( (int)(_startPosX + 5),(int)( _startPosY + 10), 50, 20);
g.setPaint(Color.black);
g.drawOval( (int)(_startPosX + 5),(int)( _startPosY + 10), 50, 20);
_paddles.DrawPaddles(gr, (int)_startPosX, (int)_startPosY, pen);
super.repaint();
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _boatWidth || _pictureHeight <= _boatHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _boatWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _boatWidth;
}
if (_startPosY + _boatHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _boatHeight;
}
}
}

40
src/DrawingPaddle.java Normal file
View File

@ -0,0 +1,40 @@
import javax.swing.*;
import java.awt.*;
public class DrawingPaddle extends JComponent {
private AdditionalDirection _paddle;
public void SetPaddlesAmount(int rpaddlesAmount) {
for (AdditionalDirection item: _paddle.values()) {
if (item.getCount() == rpaddlesAmount) {
_paddle = item;
return;
}
}
}
public void DrawPaddles(Graphics gr, int _startPosPaddlesX, int _startPosPaddlessY, Color pen) {
super.paintComponent(gr);
Graphics2D g=(Graphics2D)gr;
_startPosPaddlesX +=40;
_startPosPaddlessY +=33;
if (_paddle.getCount() >= 1) {
paintPaddle(g, _startPosPaddlesX, _startPosPaddlessY, _startPosPaddlessY + 16,pen);
}
if (_paddle.getCount() >= 2) {
paintPaddle(g, _startPosPaddlesX -10, _startPosPaddlessY -41, _startPosPaddlessY -51,pen);
}
if (_paddle.getCount() >= 3) {
paintPaddle(g, _startPosPaddlesX -20, _startPosPaddlessY, _startPosPaddlessY + 16,pen);
}
}
protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1,int _startPosY2, Color pen){
try { g.setPaint(pen); }
catch (Exception e) {
g.setPaint(Color.black);
}
g.fillRect(_startPosX,_startPosY1,4,17);
g.fillOval(_startPosX-1, _startPosY2, 6, 10);
g.setPaint(Color.black);
g.drawRect(_startPosX,_startPosY1,4,17);
g.drawOval(_startPosX-1, _startPosY2, 6, 10);
}
}

24
src/EntityBoat.java Normal file
View File

@ -0,0 +1,24 @@
import java.awt.*;
import java.util.Random;
public class EntityBoat {
private int speed;
public int Speed() { return speed; }
private float weight;
public float Weight() {
return weight;
}
private Color bodyColor;
public Color BodyColor() {
return bodyColor;
}
public float Step()
{ return Speed() * 20 / Weight(); }
public void Init(int speed, float weight, Color bodyColor)
{
Random random = new Random();
this.speed = speed <= 0 ? random.nextInt(50, 150) : speed;
this.weight = weight <= 0 ? random.nextInt(40, 70) : weight;
this.bodyColor = bodyColor;
}
}

190
src/FormBoat.form Normal file
View File

@ -0,0 +1,190 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormBoat">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="5" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="766" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="bbded" binding="pictureBoxBoat" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="2" col-span="6" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-5185294"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="59ac9" binding="radioButtonsBox" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="95721" class="javax.swing.JRadioButton" binding="radioButtonPaddle1">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="true"/>
<text value="Одно весло"/>
</properties>
</component>
<component id="58261" class="javax.swing.JRadioButton" binding="radioButtonPaddle2">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Два весла"/>
</properties>
</component>
<component id="fe956" class="javax.swing.JRadioButton" binding="radioButtonPaddle3">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Три весла"/>
</properties>
</component>
<grid id="ba39f" binding="statusStrip" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="7fb0a" class="javax.swing.JLabel" binding="toolStripStatusLabelBodyColor">
<constraints>
<grid row="0" column="2" row-span="4" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Цвет: "/>
</properties>
</component>
<component id="4180a" class="javax.swing.JLabel" binding="toolStripStatusLabelSpeed">
<constraints>
<grid row="0" column="0" row-span="4" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость: "/>
</properties>
</component>
<hspacer id="42ce">
<constraints>
<grid row="1" column="3" row-span="3" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="2ba60" class="javax.swing.JLabel" binding="toolStripStatusLabelWeight">
<constraints>
<grid row="0" column="1" row-span="4" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес: "/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<vspacer id="93048">
<constraints>
<grid row="1" column="6" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<hspacer id="8d179">
<constraints>
<grid row="4" column="1" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="1de1f">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="d05dd" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="120" height="40"/>
<preferred-size width="120" height="40"/>
<maximum-size width="120" height="40"/>
</grid>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<grid id="8ef30" binding="buttonsBox" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="4" row-span="2" col-span="2" vsize-policy="3" hsize-policy="3" anchor="10" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="150" height="150"/>
<preferred-size width="150" height="150"/>
<maximum-size width="150" height="150"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="68b27" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/right.png"/>
<text value=""/>
</properties>
</component>
<component id="80f7e" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/left.png"/>
<text value=""/>
</properties>
</component>
<component id="f431c" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/up.png"/>
<text value=""/>
</properties>
</component>
<component id="2c574" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="40" height="40"/>
<preferred-size width="40" height="40"/>
<maximum-size width="40" height="40"/>
</grid>
</constraints>
<properties>
<icon value="images/down.png"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

125
src/FormBoat.java Normal file
View File

@ -0,0 +1,125 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FormBoat extends JFrame {
public static void main(String[] args) {
FormBoat window = new FormBoat();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
private JPanel mainPanel;
private JPanel statusStrip;
private JLabel toolStripStatusLabelSpeed;
private JLabel toolStripStatusLabelBodyColor;
private JLabel toolStripStatusLabelWeight;
private JPanel buttonsBox;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonCreate;
private JRadioButton radioButtonPaddle1;
private JRadioButton radioButtonPaddle2;
private JRadioButton radioButtonPaddle3;
private JPanel radioButtonsBox;
private JPanel pictureBoxBoat;
private DrawingBoat _boat;
private int pictureBoxBoatWidth;
private int pictureBoxBoatHeight;
ButtonGroup buttonGroupPaddlesRadBut;
public FormBoat() {
super("Моторная лодка");
buttonGroupPaddlesRadBut = new ButtonGroup();
buttonGroupPaddlesRadBut.add(radioButtonPaddle1);
buttonGroupPaddlesRadBut.add(radioButtonPaddle2);
buttonGroupPaddlesRadBut.add(radioButtonPaddle3);
setPreferredSize(new Dimension(1000, 700));
getContentPane().add(mainPanel);
buttonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
pictureBoxBoat.remove(_boat);
} catch (Exception c) {
}
Random random = new Random();
_boat = new DrawingBoat();
_boat.Init(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256),
random.nextInt(0, 256), random.nextInt(0, 256)), GetRollersAmount());
ChangePictureBoxBoatBorders();
_boat.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxBoatWidth, pictureBoxBoatHeight);
toolStripStatusLabelSpeed.setText("Скорость: " + _boat.Boat.Speed());
toolStripStatusLabelWeight.setText("Вес: " + _boat.Boat.Weight());
toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_boat.Boat.BodyColor().getRGB()));
pictureBoxBoat.add(_boat, BorderLayout.CENTER);
}
});
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
ChangePictureBoxBoatBorders();
if (_boat == null) return;
_boat.ChangeBorders(pictureBoxBoatWidth, pictureBoxBoatHeight);
try {
pictureBoxBoat.remove(_boat);
} catch (Exception c) {
}
pictureBoxBoat.add(_boat, BorderLayout.CENTER);
}
});
//джижение
ButtonsMove buttonsMove = new ButtonsMove();
buttonUp.setName("Up");
buttonLeft.setName("Left");
buttonRight.setName("Right");
buttonDown.setName("Down");
buttonUp.addActionListener(buttonsMove);
buttonLeft.addActionListener(buttonsMove);
buttonRight.addActionListener(buttonsMove);
buttonDown.addActionListener(buttonsMove);
}
class ButtonsMove implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (_boat == null) return;
JButton temp = (JButton) e.getSource();
String name = temp.getName();
switch (name) {
case "Up" -> _boat.MoveTransport(Direction.Up);
case "Right" -> _boat.MoveTransport(Direction.Right);
case "Left" -> _boat.MoveTransport(Direction.Left);
case "Down" -> _boat.MoveTransport(Direction.Down);
}
}
}
private void ChangePictureBoxBoatBorders() {
char[] temp = pictureBoxBoat.getSize().toString().toCharArray();
for (int i = 0; i < temp.length; i++) {
if (!Character.isDigit(temp[i])) {
temp[i] = ' ';
}
}
String width = new String(temp);
String[] parameters = width.split("\\s*(\\s|,|!|\\.)\\s*", 4);
pictureBoxBoatWidth = Integer.parseInt(parameters[1]);
pictureBoxBoatHeight = Integer.parseInt(parameters[2]);
}
private int GetRollersAmount() {
if (radioButtonPaddle1.isSelected()) {
return 1;
}
else if (radioButtonPaddle2.isSelected()) {
return 2;
} else {
return 3;
}
}
}

BIN
src/images/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B

BIN
src/images/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 463 B

BIN
src/images/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 B

BIN
src/images/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 413 B