diff --git a/.idea/Catamaran_hard.iml b/.idea/Catamaran_hard.iml index d6ebd48..b107a2d 100644 --- a/.idea/Catamaran_hard.iml +++ b/.idea/Catamaran_hard.iml @@ -2,7 +2,9 @@ - + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..104cfef 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..c7b3e68 --- /dev/null +++ b/Direction.java @@ -0,0 +1,7 @@ +public enum Direction { + Up(1), + Down(2), + Left(3), + Right(4); + Direction(int value){} +} diff --git a/DrawningBoat.java b/DrawningBoat.java new file mode 100644 index 0000000..0d820c9 --- /dev/null +++ b/DrawningBoat.java @@ -0,0 +1,139 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawningBoat extends JPanel { + + private EntityBoat Boat; + public EntityBoat GetBoat(){ + return Boat; + } + + private int _startPosX; + private int _startPosY; + public DrawningOars OarsDraw; + public Integer _pictureWidth = null; + public Integer _pictureHeight = null; + private final int _BoatWidth = 80; + private final int _BoatHeight = 50; + + public void SetOars() { + Random r = new Random(); + int numIllum = r.nextInt(1,4); + OarsDraw.SetOarsCount(numIllum); + } + + public void Init(int speed, float weight, Color bodyColor) + { + Boat = new EntityBoat(); + Boat.Init(speed, weight, bodyColor); + OarsDraw = new DrawningOars(); + SetOars(); + } + + public void SetPosition(int x, int y, int width, int height) + { + if (x >= 0 && x + _BoatWidth <= width && y >= 0 && y + _BoatHeight <= height) { + _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 DrawTransport() { + if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { + return; + } + repaint(); + } + @Override + public void paintComponent(Graphics g) { + if (GetBoat() == null) { + return; + } + if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { + return; + } + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + + g2d.setColor(Color.BLACK); + //границы + g2d.drawRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight); + + int[] xpoints = {(int)(_startPosX + _BoatWidth * 3 / 4), (int)(_startPosX + _BoatWidth), (int)(_startPosX + _BoatWidth * 3 / 4) }; + int[] ypoints = {(int)_startPosY, (int)(_startPosY + _BoatHeight / 2), (int)(_startPosY + _BoatHeight)}; + + g2d.drawPolygon(xpoints, ypoints, 3); + + g2d.setPaint(Boat.GetBodyColor()); + + g2d.fillRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight); + g2d.fillPolygon(xpoints, ypoints, 3); + + // середина + g2d.setColor(Color.BLACK); + g2d.drawOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11); + + g2d.setPaint(Color.CYAN); + g2d.fillOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11); + + OarsDraw.DrawOars(g, _startPosX, _startPosY); + } + + 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/DrawningOars.java b/DrawningOars.java new file mode 100644 index 0000000..f44c904 --- /dev/null +++ b/DrawningOars.java @@ -0,0 +1,39 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawningOars extends JComponent { + private OarsCount _Oars; + + public void SetOarsCount(int numOfOars) { + _Oars = OarsCount.GetOarsCount(numOfOars); + } + + public void DrawOars(Graphics g, int _startPosX, int _startPosY) { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.BLACK); + int numOfIlluminator = 0; + switch (_Oars) + { + case One: + numOfIlluminator = 1; + break; + case Two: + numOfIlluminator = 2; + break; + case Three: + numOfIlluminator = 3; + break; + } + + for(int i = numOfIlluminator; i >= 1; --i){ + g2d.setColor(Color.CYAN); + g2d.fillRect(_startPosX + (10 * (i + 1)), _startPosY -15, 5, 15); + //g2d.fillOval(_startPosX + 105 - 3 * i, _startPosY + 35, 3, 3); + g2d.setColor(Color.BLACK); + //g2d.drawOval(_startPosX + 105 - 3 * i, _startPosY + 35, 3, 3); + g2d.drawRect(_startPosX + (10 * (i + 1)), _startPosY - 15, 5, 15); + + } + } +} diff --git a/EntityBoat.java b/EntityBoat.java new file mode 100644 index 0000000..d6a4ba2 --- /dev/null +++ b/EntityBoat.java @@ -0,0 +1,29 @@ +import java.awt.*; +import java.util.Random; + +public class EntityBoat { + private int Speed; + public int GetSpeed() { + return Speed; + } + + private float Weight; + public float GetWeight() { + return Weight; + } + + private Color BodyColor; + public Color GetBodyColor() { + return BodyColor; + } + public float Step; + + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(10,30) : speed; + Weight = weight <= 0 ? rnd.nextInt(40,70) : weight; + BodyColor = bodyColor; + Step = Speed * 100 / (int)Weight; + } +} diff --git a/FormBoat.form b/FormBoat.form new file mode 100644 index 0000000..f35ad4b --- /dev/null +++ b/FormBoat.form @@ -0,0 +1,103 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormBoat.java b/FormBoat.java new file mode 100644 index 0000000..c9f0158 --- /dev/null +++ b/FormBoat.java @@ -0,0 +1,78 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; +public class FormBoat { + public JPanel Mainpanel; + private JButton ButtonCreate; + private JButton ButtonLeft; + private JButton ButtonUp; + private JButton ButtonDown; + private JButton ButtonRight; + protected DrawningBoat PictureBoxBoat; + private JToolBar StatusStrip; + private JLabel JLabelSpeed = new JLabel(); + private JLabel JLabelWeight = new JLabel(); + private JLabel JLabelColor = new JLabel(); + public void Draw() { + if (PictureBoxBoat.GetBoat() == null) { + return; + } + PictureBoxBoat.DrawTransport(); + } + public FormBoat() { + Box LabelBox = Box.createHorizontalBox(); + LabelBox.setMinimumSize(new Dimension(1, 20)); + LabelBox.add(JLabelSpeed); + LabelBox.add(JLabelWeight); + LabelBox.add(JLabelColor); + StatusStrip.add(LabelBox); + try { + Image img = ImageIO.read(FormBoat.class.getResource("/Resource/up.jpg")); + ButtonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormBoat.class.getResource("/Resource/down.jpg")); + ButtonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormBoat.class.getResource("/Resource/left.jpg")); + ButtonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormBoat.class.getResource("/Resource/right.jpg")); + ButtonRight.setIcon(new ImageIcon(img)); + } catch (Exception ex) { + System.out.println(ex); + } + ButtonCreate.addActionListener(e -> { + Random random = new Random(); + PictureBoxBoat.Init(random.nextInt(10, 30), random.nextInt(10, 20), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + PictureBoxBoat.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBoxBoat.getWidth(), PictureBoxBoat.getHeight()); + JLabelSpeed.setText("Cкорость: " + PictureBoxBoat.GetBoat().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + PictureBoxBoat.GetBoat().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + PictureBoxBoat.GetBoat().GetBodyColor() + " ")); + Draw(); + }); + PictureBoxBoat.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + PictureBoxBoat.ChangeBorders(PictureBoxBoat.getWidth(), PictureBoxBoat.getHeight()); + Draw(); + } + }); + ButtonUp.addActionListener(e -> { + PictureBoxBoat.MoveTransport(Direction.Up); + Draw(); + }); + ButtonDown.addActionListener(e -> { + PictureBoxBoat.MoveTransport(Direction.Down); + Draw(); + }); + ButtonRight.addActionListener(e -> { + PictureBoxBoat.MoveTransport(Direction.Right); + Draw(); + }); + ButtonLeft.addActionListener(e -> { + PictureBoxBoat.MoveTransport(Direction.Left); + Draw(); + }); + } +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..5127ef7 --- /dev/null +++ b/Main.java @@ -0,0 +1,14 @@ +import javax.swing.*; + +public class Main { + + public static void main(String[] args) { + JFrame frame = new JFrame("Лодка"); + frame.setContentPane(new FormBoat().Mainpanel); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocation(500, 200); + frame.pack(); + frame.setSize(800, 600); + frame.setVisible(true); + } +} diff --git a/OarsCount.java b/OarsCount.java new file mode 100644 index 0000000..46180f3 --- /dev/null +++ b/OarsCount.java @@ -0,0 +1,22 @@ +public enum OarsCount { + One, + Two, + Three; + + public static OarsCount GetOarsCount(int Value) + { + switch(Value) + { + case 1: + return One; + + case 2: + return Two; + + case 3: + return Three; + + } + return null; + } +} diff --git a/Resource/down.jpg b/Resource/down.jpg new file mode 100644 index 0000000..1841298 Binary files /dev/null and b/Resource/down.jpg differ diff --git a/Resource/left.jpg b/Resource/left.jpg new file mode 100644 index 0000000..6e4bbc5 Binary files /dev/null and b/Resource/left.jpg differ diff --git a/Resource/right.jpg b/Resource/right.jpg new file mode 100644 index 0000000..3260939 Binary files /dev/null and b/Resource/right.jpg differ diff --git a/Resource/up.jpg b/Resource/up.jpg new file mode 100644 index 0000000..b538ee8 Binary files /dev/null and b/Resource/up.jpg differ