diff --git a/.idea/PIbd-22_Bondarenko_M.S._WarmlyShip_Hard.iml b/.idea/PIbd-22_Bondarenko_M.S._WarmlyShip_Hard.iml index d6ebd48..b107a2d 100644 --- a/.idea/PIbd-22_Bondarenko_M.S._WarmlyShip_Hard.iml +++ b/.idea/PIbd-22_Bondarenko_M.S._WarmlyShip_Hard.iml @@ -2,7 +2,9 @@ - + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index 639900d..dd53264 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Deck.java b/Deck.java new file mode 100644 index 0000000..6ca8e0f --- /dev/null +++ b/Deck.java @@ -0,0 +1,22 @@ +public enum Deck { + One(1), + Two(2), + Three(3); + + Deck(int i) { + } + + public static Deck GetDeck(int i){ + switch (i) + { + case 1: + return One; + case 2: + return Two; + case 3: + return Three; + } + + return null; + } +} diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..8bbcfd5 --- /dev/null +++ b/Direction.java @@ -0,0 +1,9 @@ +public enum Direction { + Left(1), //Влево + Up(2), //Вверх + Right(3), //Вправо + Down(4); //Вниз + + Direction(int i) { + } +} diff --git a/DrawDeck.java b/DrawDeck.java new file mode 100644 index 0000000..3ec0087 --- /dev/null +++ b/DrawDeck.java @@ -0,0 +1,33 @@ +import java.awt.*; + +public class DrawDeck { + private Deck deckCount; + + public void SetDeckCount(int count){ + deckCount = Deck.GetDeck(count); + } + + public void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor){ + switch (deckCount) + { + case One: + break; + + case Two: + g2d.setColor(bodyColor); + g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20, _warmlyShipWidth * 3 / 5, 20); + g2d.setColor(Color.BLACK); + g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20, _warmlyShipWidth * 3 / 5, 20); + break; + + case Three: + for (int i = 1; i < 3; ++i){ + g2d.setColor(bodyColor); + g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20); + g2d.setColor(Color.BLACK); + g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20); + } + break; + } + } +} diff --git a/DrawingShip.java b/DrawingShip.java new file mode 100644 index 0000000..b2c2188 --- /dev/null +++ b/DrawingShip.java @@ -0,0 +1,119 @@ +import javax.swing.*; +import java.awt.*; +import java.util.*; + +public class DrawingShip extends JPanel { + private EntityWarmlyShip warmlyShip; //Класс-сущность + public EntityWarmlyShip GetWarmlyShip(){return warmlyShip;} + public float _startPosX; //Координаты отрисовки по оси x + public float _startPosY; //Координаты отрисовки по оси y + private Integer _pictureWidth = null; //Ширина окна + private Integer _pictureHeight = null; //Высота окна + private final int _warmlyShipWidth = 125; //Ширина отрисовки корабля + private final int _warmlyShipHeight = 50; //Высота отрисовки корабля + + private int deckCount = 1; + private DrawDeck dd = new DrawDeck(); + + //Инициализация + public void Init(int speed, float weight, Color bodyColor) + { + warmlyShip = new EntityWarmlyShip(); + warmlyShip.Init(speed, weight, bodyColor); + Random random = new Random(); + dd.SetDeckCount(random.nextInt(1, 4)); + } + + //Начальные коордитанты + public void SetPosition(int x, int y, int width, int height) + { + if (width < _warmlyShipWidth || height < _warmlyShipHeight) return; + Random random = new Random(); + _startPosX = x < 0 || x + _warmlyShipWidth > width ? random.nextFloat(0, width - _warmlyShipWidth) : x; + _startPosY = y < 0 || y + _warmlyShipHeight > height ? random.nextFloat(0, height - _warmlyShipHeight) : y; + _pictureWidth = width; + _pictureHeight = height; + } + + //Движение транспорта по координатам + public void MoveTransport(Direction direction) + { + if (_pictureWidth == null || _pictureHeight == null) return; + switch (direction) + { + case Left: //Влево + if (_startPosX - warmlyShip.GetStep() > 0) _startPosX -= warmlyShip.GetStep(); + break; + case Up: //Вверх + if (_startPosY - warmlyShip.GetStep() > 0) _startPosY -= warmlyShip.GetStep(); + break; + case Right: //Вправо + if (_startPosX + _warmlyShipWidth + warmlyShip.GetStep() < _pictureWidth) _startPosX += warmlyShip.GetStep(); + break; + case Down: //Вниз + if (_startPosY + _warmlyShipHeight + warmlyShip.GetStep() < _pictureHeight) _startPosY += warmlyShip.GetStep(); + break; + } + } + + //Отрисовка транспорта + public void DrawTransport() + { + if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) + { + return; + } + repaint(); + } + + @Override + public void paintComponent(Graphics g){ + if (GetWarmlyShip() == null) return; + + if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) + { + return; + } + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(warmlyShip.GetBodyColor()); + int[] xPol = new int[] {(int)(_startPosX), (int)(_startPosX + _warmlyShipWidth), (int)(_startPosX + _warmlyShipWidth - 25), (int)(_startPosX + 25)}; + int[] yPol = new int[] {(int)(_startPosY + 20), (int)(_startPosY + 20), (int)(_startPosY + _warmlyShipHeight), (int)(_startPosY + _warmlyShipHeight)}; + g2d.fillPolygon(new Polygon(xPol, yPol, xPol.length)); + g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY, _warmlyShipWidth * 3 / 5, 20); + g2d.setColor(Color.CYAN); + g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20); + g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20); + g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(new Polygon(xPol, yPol, xPol.length)); + g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)(_startPosY), _warmlyShipWidth * 3 / 5, 20); + g2d.setColor(Color.BLUE); + g2d.drawOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20); + g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20); + g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20); + dd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor()); + } + + //Изменение границ отрисовки + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _warmlyShipWidth || _pictureHeight <= _warmlyShipHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _warmlyShipWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _warmlyShipWidth; + } + if (_startPosY + _warmlyShipHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _warmlyShipHeight; + } + + } +} diff --git a/EntityWarmlyShip.java b/EntityWarmlyShip.java new file mode 100644 index 0000000..81136c8 --- /dev/null +++ b/EntityWarmlyShip.java @@ -0,0 +1,35 @@ +import java.awt.*; +import java.util.*; + +public class EntityWarmlyShip { + private int Speed; //Скорость + private float Weight; //Вес + private Color BodyColor; //Цвет + private float Step; //Шаг при перемещении + + //Инициализация + public void Init(int speed, float weight, Color bodyColor) + { + Random random = new Random(); + Speed = speed <= 0 ? random.nextInt(50, 150) : speed; + Weight = weight <= 0 ? random.nextInt(50, 150) : weight; + BodyColor = bodyColor; + Step = Speed * 100 / Weight; + } + + public int GetSpeed(){ + return Speed; + } + + public float GetWeight(){ + return Weight; + } + + public Color GetBodyColor(){ + return BodyColor; + } + + public float GetStep(){ + return Step; + } +} diff --git a/FormShip.form b/FormShip.form new file mode 100644 index 0000000..ee8a98c --- /dev/null +++ b/FormShip.form @@ -0,0 +1,107 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormShip.java b/FormShip.java new file mode 100644 index 0000000..5967181 --- /dev/null +++ b/FormShip.java @@ -0,0 +1,114 @@ +import javax.imageio.*; +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.image.*; +import java.util.*; + +public class FormShip { + private JToolBar statusStrip; + public JPanel Mainpanel; + private DrawingShip pictureShip; + private JButton buttonRight; + private JButton buttonCreate; + private JButton buttonLeft; + private JButton buttonUp; + private JButton buttonDown; + private JLabel JLabelSpeed = new JLabel(); + private JLabel JLabelWeight = new JLabel(); + private JLabel JLabelColor = new JLabel(); + + private void Draw() + { + if (pictureShip.GetWarmlyShip() == null) return; + pictureShip.DrawTransport(); + } + + private void ButtonMove_Click(String name) + { + if (pictureShip == null) return; + switch (name) + { + case "buttonLeft": + pictureShip.MoveTransport(Direction.Left); + break; + case "buttonUp": + pictureShip.MoveTransport(Direction.Up); + break; + case "buttonRight": + pictureShip.MoveTransport(Direction.Right); + break; + case "buttonDown": + pictureShip.MoveTransport(Direction.Down); + break; + } + Draw(); + } + + public FormShip() { + 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(FormShip.class.getResource("/Images/totop.png")); + buttonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormShip.class.getResource("/Images/toleft.png")); + buttonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormShip.class.getResource("/Images/todown.png")); + buttonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormShip.class.getResource("/Images/toright.png")); + buttonRight.setIcon(new ImageIcon(img)); + } catch (Exception ex) { + System.out.println(ex); + } + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + pictureShip.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); + pictureShip.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureShip.getWidth(), pictureShip.getHeight()); + JLabelSpeed.setText("Cкорость: " + pictureShip.GetWarmlyShip().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + pictureShip.GetWarmlyShip().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + pictureShip.GetWarmlyShip().GetBodyColor() + " ")); + Draw(); + } + }); + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonUp"); + } + }); + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonLeft"); + } + }); + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonDown"); + } + }); + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonRight"); + } + }); + pictureShip.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + pictureShip.ChangeBorders(pictureShip.getWidth(), pictureShip.getHeight()); + Draw(); + } + }); + } +} diff --git a/Images/todown.png b/Images/todown.png new file mode 100644 index 0000000..6ebb44d Binary files /dev/null and b/Images/todown.png differ diff --git a/Images/toleft.png b/Images/toleft.png new file mode 100644 index 0000000..45dd05f Binary files /dev/null and b/Images/toleft.png differ diff --git a/Images/toright.png b/Images/toright.png new file mode 100644 index 0000000..1f21a2f Binary files /dev/null and b/Images/toright.png differ diff --git a/Images/totop.png b/Images/totop.png new file mode 100644 index 0000000..4a5d4b7 Binary files /dev/null and b/Images/totop.png differ diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..56bf4cb --- /dev/null +++ b/Main.java @@ -0,0 +1,13 @@ +import javax.swing.*; + +public class Main { + public static void main(String[] args) { + JFrame frame = new JFrame("Hard №1"); + frame.setContentPane(new FormShip().Mainpanel); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLocation(500, 200); + frame.pack(); + frame.setSize(800, 600); + frame.setVisible(true); + } +}