diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..444d0e9 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +ProjectMotorBoatHard.iml \ No newline at end of file diff --git a/.idea/PIbd-21_Ihonkina_E.S._MotorBoat._Hard.iml b/.idea/PIbd-21_Ihonkina_E.S._MotorBoat._Hard.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/PIbd-21_Ihonkina_E.S._MotorBoat._Hard.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e1f11fa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..09e91bf --- /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..b5fbbd9 --- /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..288b36b --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/ProjectMotorBoatHard.iml b/ProjectMotorBoatHard.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/ProjectMotorBoatHard.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/AdditionalDirection.java b/src/AdditionalDirection.java new file mode 100644 index 0000000..a266a65 --- /dev/null +++ b/src/AdditionalDirection.java @@ -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; + } +} diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..996463a --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} \ No newline at end of file diff --git a/src/DrawingBoat.java b/src/DrawingBoat.java new file mode 100644 index 0000000..d8d8def --- /dev/null +++ b/src/DrawingBoat.java @@ -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; + } + } +} diff --git a/src/DrawingPaddle.java b/src/DrawingPaddle.java new file mode 100644 index 0000000..d4327b3 --- /dev/null +++ b/src/DrawingPaddle.java @@ -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); + } +} diff --git a/src/EntityBoat.java b/src/EntityBoat.java new file mode 100644 index 0000000..1b9d07b --- /dev/null +++ b/src/EntityBoat.java @@ -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; + } +} diff --git a/src/FormBoat.form b/src/FormBoat.form new file mode 100644 index 0000000..89f3a52 --- /dev/null +++ b/src/FormBoat.form @@ -0,0 +1,190 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormBoat.java b/src/FormBoat.java new file mode 100644 index 0000000..37f65ac --- /dev/null +++ b/src/FormBoat.java @@ -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; + } + } +} diff --git a/src/images/down.png b/src/images/down.png new file mode 100644 index 0000000..b1e74ad Binary files /dev/null and b/src/images/down.png differ diff --git a/src/images/left.png b/src/images/left.png new file mode 100644 index 0000000..2ec2c67 Binary files /dev/null and b/src/images/left.png differ diff --git a/src/images/right.png b/src/images/right.png new file mode 100644 index 0000000..9597382 Binary files /dev/null and b/src/images/right.png differ diff --git a/src/images/up.png b/src/images/up.png new file mode 100644 index 0000000..8fceff9 Binary files /dev/null and b/src/images/up.png differ