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/PIbd-21_Batylkin_A.O._ElectricLocomotive._Hard.iml b/.idea/PIbd-21_Batylkin_A.O._ElectricLocomotive._Hard.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/PIbd-21_Batylkin_A.O._ElectricLocomotive._Hard.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ce297c6 --- /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..c4cbfd3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ElectricLocomotive.iml b/ElectricLocomotive.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/ElectricLocomotive.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..2450c1a --- /dev/null +++ b/src/AdditionalDirection.java @@ -0,0 +1,13 @@ +public enum AdditionalDirection { + + TwoWheel(2), + ThreeWheel(3), + FourWheel(4); + 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/DrawingLocomotive.java b/src/DrawingLocomotive.java new file mode 100644 index 0000000..697504e --- /dev/null +++ b/src/DrawingLocomotive.java @@ -0,0 +1,122 @@ +import javax.swing.*; +import java.awt.*; +public class DrawingLocomotive extends JComponent { + public EntityLocomotive Locomotive; + private float _startPosX; + private float _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _locomotiveWidth = 150; + private final int _locomotiveHeight = 100; + private DrawingWheel _wheel; + public DrawingLocomotive() { + super(); + } + public void Init(int speed, float weight, Color bodyColor, int wheelCount) + { + Locomotive = new EntityLocomotive(); + Locomotive.Init(speed, weight, bodyColor); + _wheel = new DrawingWheel(); + _wheel.SetWheelAmount(wheelCount); + } + public void SetPosition(int x, int y, int width, int height) + { + if (width <= _locomotiveWidth + x || height <= _locomotiveHeight + 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 + _locomotiveWidth + Locomotive.Step() < _pictureWidth) { + _startPosX += Locomotive.Step(); + } + break; + } + case Left: + if (_startPosX - Locomotive.Step() > 0) + { + _startPosX -= Locomotive.Step(); + } + break; + case Up: + if (_startPosY - Locomotive.Step() > 0) + { + _startPosY -= Locomotive.Step(); + } + break; + case Down: + if (_startPosY + _locomotiveHeight + Locomotive.Step() < _pictureHeight) + { + _startPosY += Locomotive.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 + 10),(int)(_startPosX + 10),(int)(_startPosX+ 110),(int)(_startPosX + 140),(int)(_startPosX+140)}; + int [] pointsY = new int[]{(int)(_startPosY + 90),(int)(_startPosY + 40),(int)(_startPosY + 40),(int)(_startPosY + 60),(int)(_startPosY + 90) }; + + try { pen = Locomotive.BodyColor(); } + catch (Exception e) {} + + g.setPaint(pen); + + g.fillPolygon( pointsX,pointsY,5); + g.setColor(Color.BLACK); + g.fillRect((int) _startPosX + 5, (int)_startPosY + 45, 5, 40); + // Окна + g.setColor(Color.CYAN); + g.drawRect((int) _startPosX + 20, (int)_startPosY + 50, 20, 25); + g.drawRect((int)_startPosX + 50,(int) _startPosY + 50, 20, 25); + // Дверь + g.drawRect((int) _startPosX + 85,(int) _startPosY + 45, 20, 40); + // Локомотив + g.drawPolygon(pointsX,pointsY,5); + _wheel.DrawWheel(gr, (int)_startPosX, (int)_startPosY, pen); + super.repaint(); + } + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _locomotiveWidth || _pictureHeight <= _locomotiveHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _locomotiveWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _locomotiveWidth; + } + if (_startPosY + _locomotiveHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _locomotiveHeight; + } + } +} diff --git a/src/DrawingWheel.java b/src/DrawingWheel.java new file mode 100644 index 0000000..ebad914 --- /dev/null +++ b/src/DrawingWheel.java @@ -0,0 +1,40 @@ +import javax.swing.*; +import java.awt.*; +public class DrawingWheel extends JComponent { + private AdditionalDirection _wheel; + public void SetWheelAmount(int wheelAmount) { + for (AdditionalDirection item: _wheel.values()) { + if (item.getCount() == wheelAmount) { + _wheel = item; + return; + } + } + } + public void DrawWheel(Graphics gr, int _startPosWheelX, int _startPosWheelY, Color pen) { + super.paintComponent(gr); + Graphics2D g=(Graphics2D)gr; + + if (_wheel.getCount() >= 2) { + paintWheel(g, _startPosWheelX, _startPosWheelY,pen); + paintWheel(g, _startPosWheelX +90, _startPosWheelY,pen); + } + if (_wheel.getCount() >= 3) { + paintWheel(g, _startPosWheelX +65, _startPosWheelY,pen); + } + if (_wheel.getCount() >= 4) { + paintWheel(g, _startPosWheelX +25, _startPosWheelY, pen); + } + super.repaint(); + } + protected void paintWheel(Graphics2D g, int _startPosX1, int _startPosY, Color pen){ + try { g.setPaint(pen); } + catch (Exception e) { + g.setPaint(Color.black); + } + g.fillOval((int)_startPosX1+ 17, (int)_startPosY + 90, 20, 20); + + g.setColor(Color.BLACK); + g.drawOval((int) _startPosX1 + 17, (int) _startPosY + 90, 20, 20); + + } +} diff --git a/src/EntityLocomotive.java b/src/EntityLocomotive.java new file mode 100644 index 0000000..92e2e67 --- /dev/null +++ b/src/EntityLocomotive.java @@ -0,0 +1,24 @@ +import java.awt.*; +import java.util.Random; + +public class EntityLocomotive { + 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/FormLocomotive.form b/src/FormLocomotive.form new file mode 100644 index 0000000..42249f1 --- /dev/null +++ b/src/FormLocomotive.form @@ -0,0 +1,190 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormLocomotive.java b/src/FormLocomotive.java new file mode 100644 index 0000000..51cbe14 --- /dev/null +++ b/src/FormLocomotive.java @@ -0,0 +1,126 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.Random; +public class FormLocomotive extends JFrame { + public static void main(String[] args) { + + FormLocomotive window = new FormLocomotive(); + 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 radioButtonWheel3; + private JRadioButton radioButtonWheel4; + private JPanel radioButtonsBox; + private JPanel pictureBoxLocomotive; + private JRadioButton radioButtonWheel2; + private DrawingLocomotive _locomotive; + private int pictureBoxLocomotiveWidth; + private int pictureBoxLocomotiveHeight; + ButtonGroup buttonGroupWheelRadBut; + public FormLocomotive() { + super("Локомотив"); + buttonGroupWheelRadBut = new ButtonGroup(); + buttonGroupWheelRadBut.add(radioButtonWheel2); + buttonGroupWheelRadBut.add(radioButtonWheel3); + buttonGroupWheelRadBut.add(radioButtonWheel4); + setPreferredSize(new Dimension(1000, 700)); + getContentPane().add(mainPanel); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + pictureBoxLocomotive.remove(_locomotive); + } catch (Exception c) { + } + Random random = new Random(); + _locomotive = new DrawingLocomotive(); + _locomotive.Init(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256), + random.nextInt(0, 256), random.nextInt(0, 256)), GetWheelAmount()); + ChangePictureBoxLocomotiveBorders(); + _locomotive.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxLocomotiveWidth, pictureBoxLocomotiveHeight); + toolStripStatusLabelSpeed.setText("Скорость: " + _locomotive.Locomotive.Speed()); + toolStripStatusLabelWeight.setText("Вес: " + _locomotive.Locomotive.Weight()); + + toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_locomotive.Locomotive.BodyColor().getRGB())); + pictureBoxLocomotive.add(_locomotive, BorderLayout.CENTER); + } + }); + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + ChangePictureBoxLocomotiveBorders(); + if (_locomotive == null) return; + _locomotive.ChangeBorders(pictureBoxLocomotiveWidth, pictureBoxLocomotiveHeight); + try { + pictureBoxLocomotive.remove(_locomotive); + } catch (Exception c) { + } + pictureBoxLocomotive.add(_locomotive, 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 (_locomotive == null) return; + JButton temp = (JButton) e.getSource(); + String name = temp.getName(); + switch (name) { + case "Up" -> _locomotive.MoveTransport(Direction.Up); + case "Right" -> _locomotive.MoveTransport(Direction.Right); + case "Left" -> _locomotive.MoveTransport(Direction.Left); + case "Down" -> _locomotive.MoveTransport(Direction.Down); + } + } + } + + private void ChangePictureBoxLocomotiveBorders() { + char[] temp = pictureBoxLocomotive.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); + pictureBoxLocomotiveWidth = Integer.parseInt(parameters[1]); + pictureBoxLocomotiveHeight = Integer.parseInt(parameters[2]); + } + private int GetWheelAmount() { + if (radioButtonWheel2.isSelected()) { + return 2; + } + else if (radioButtonWheel3.isSelected()) { + return 3; + } else { + return 4; + } + } +} diff --git a/src/images/down.jpg b/src/images/down.jpg new file mode 100644 index 0000000..70c2e36 Binary files /dev/null and b/src/images/down.jpg differ diff --git a/src/images/left.jpg b/src/images/left.jpg new file mode 100644 index 0000000..9f7ba42 Binary files /dev/null and b/src/images/left.jpg differ diff --git a/src/images/right.jpg b/src/images/right.jpg new file mode 100644 index 0000000..cffd5da Binary files /dev/null and b/src/images/right.jpg differ diff --git a/src/images/up.jpg b/src/images/up.jpg new file mode 100644 index 0000000..fcd4d45 Binary files /dev/null and b/src/images/up.jpg differ