diff --git a/.idea/misc.xml b/.idea/misc.xml index bc26385..018dc79 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - - + + \ No newline at end of file diff --git a/src/BlockDirection.java b/src/BlockDirection.java new file mode 100644 index 0000000..8d7250c --- /dev/null +++ b/src/BlockDirection.java @@ -0,0 +1,5 @@ +public enum BlockDirection { + TwoBlocks, + FourBlocks, + SixBlocks; +} diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..5b70d63 --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right; +} diff --git a/src/DrawingBlocks.java b/src/DrawingBlocks.java new file mode 100644 index 0000000..12e68d0 --- /dev/null +++ b/src/DrawingBlocks.java @@ -0,0 +1,45 @@ +import java.awt.*; + +public class DrawingBlocks { + BlockDirection blockDirection; + public void DrawBlocks(Graphics2D g2, int _startPosX, int _startPosY){ + switch(blockDirection){ + case TwoBlocks: + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); + break; + case FourBlocks: + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10); + break; + case SixBlocks: + g2.setColor(Color.GRAY); + g2.fillRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.fillRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 25, _startPosY + 20, 10, 10); + g2.fillRect(_startPosX + 35, _startPosY + 10, 10, 10); + g2.fillRect(_startPosX + 35, _startPosY + 20, 10, 10); + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 15, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 15, _startPosY + 20, 10, 10); + g2.drawRect(_startPosX + 25, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 25, _startPosY + 20, 10, 10); + g2.drawRect(_startPosX + 35, _startPosY + 10, 10, 10); + g2.drawRect(_startPosX + 35, _startPosY + 20, 10, 10); + break; + } + } +} diff --git a/src/DrawingComponents.java b/src/DrawingComponents.java new file mode 100644 index 0000000..bd909a7 --- /dev/null +++ b/src/DrawingComponents.java @@ -0,0 +1,22 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawingComponents extends JComponent { + public DrawingWarship warship; + public DrawingComponents(){ + super(); + } + public void SetDrawingWarship(DrawingWarship warship){ + this.warship = warship; + } + + @Override + public void paintComponent(Graphics g){ + super.paintComponent(g); + Graphics2D g2 = (Graphics2D) g; + if(warship != null){ + warship.DrawTransport(g2); + } + repaint(); + } +} diff --git a/src/DrawingWarship.java b/src/DrawingWarship.java new file mode 100644 index 0000000..04c5463 --- /dev/null +++ b/src/DrawingWarship.java @@ -0,0 +1,138 @@ +import java.awt.*; +import java.util.Random; +public class DrawingWarship { + private EntityWarship Warship; + public EntityWarship GetWarship(){return Warship;} + public DrawingBlocks Blocks; + + private int _startPosX; + private int _startPosY; + + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + + private final int _warshipWidth = 114; + private final int _warshipHeight = 40; + + public void Init(int speed, float weight, Color bodyColor) + { + Warship = new EntityWarship(); + Warship.Init(speed, weight, bodyColor); + Blocks = new DrawingBlocks(); + Blocks.blockDirection = BlockRandom(); + } + + public void SetPosition(int x, int y, int width, int height) + { + if (width >= x + _warshipWidth && height >= y + _warshipHeight && x >= 0 && y >= 0) + { + _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 + _warshipWidth + Warship.Step < _pictureWidth) + { + _startPosX += Warship.Step; + } + break; + //влево + case Left: + if(_startPosX - Warship.Step > 0) + { + _startPosX -= Warship.Step; + } + break; + //вверх + case Up: + if (_startPosY - Warship.Step > 0) + { + _startPosY -= Warship.Step; + } + break; + //вниз + case Down: + if (_startPosY + _warshipHeight + Warship.Step < _pictureHeight) + { + _startPosY += Warship.Step; + } + break; + } + } + public void DrawTransport(Graphics2D g2){ + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) + { + return; + } + //главная палуба + int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4}; + int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40}; + g2.setColor(Warship.GetBodyColor()); + g2.fillPolygon(pointXWarship, pointYWarship, 5); + g2.setColor(Color.BLACK); + g2.drawPolygon(pointXWarship, pointYWarship, 5); + + //мачта + g2.setColor(Color.WHITE); + g2.fillOval(_startPosX + 80, _startPosY + 13, 15, 15); + //границы мачты + g2.setColor(Color.BLACK); + g2.drawOval( _startPosX + 80, _startPosY + 13, 15, 15); + + //палуба + g2.setColor(Color.WHITE); + g2.fillRect(_startPosX + 70, _startPosY + 10, 8, 18); + g2.fillRect(_startPosX + 55, _startPosY + 15, 15, 8); + //границы палубы + g2.setColor(Color.BLACK); + g2.drawRect(_startPosX + 70, _startPosY + 10, 8, 18); + g2.drawRect(_startPosX + 55, _startPosY + 15, 15, 8); + + //двигатели + g2.fillRect(_startPosX, _startPosY + 5, 4, 10); + g2.fillRect(_startPosX, _startPosY + 23, 4, 10); + + //блоки + Blocks.DrawBlocks(g2,_startPosX, _startPosY); + } + + public BlockDirection BlockRandom(){ + Random rand = new Random(); + int resRand = rand.nextInt(3); + if(resRand == 0) return BlockDirection.TwoBlocks; + if(resRand == 1) return BlockDirection.FourBlocks; + if(resRand == 2) return BlockDirection.SixBlocks; + return null; + } + + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _warshipWidth || _pictureHeight <= _warshipHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _warshipWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _warshipWidth; + } + if (_startPosY + _warshipHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _warshipHeight; + } + } +} diff --git a/src/EntityWarship.java b/src/EntityWarship.java new file mode 100644 index 0000000..092a65b --- /dev/null +++ b/src/EntityWarship.java @@ -0,0 +1,23 @@ +import java.awt.*; +import java.util.Random; +public class EntityWarship { + 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(100) + 50 : speed; + Weight = weight <= 0 ? rnd.nextInt(30)+40 : weight; + BodyColor= bodyColor; + Step = Speed * 100 / Weight; + } + +} diff --git a/src/FormWarship.form b/src/FormWarship.form new file mode 100644 index 0000000..12eb718 --- /dev/null +++ b/src/FormWarship.form @@ -0,0 +1,112 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormWarship.java b/src/FormWarship.java new file mode 100644 index 0000000..404303a --- /dev/null +++ b/src/FormWarship.java @@ -0,0 +1,106 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.util.Random; + +public class FormWarship extends JFrame { + private DrawingWarship _warship; + public DrawingComponents drawingComponents; + private JPanel mainPanel; + private JButton buttonCreate; + private JButton buttonLeft; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonRight; + private JToolBar toolBar; + private JLabel toolBarLabelSpeed; + private JLabel toolBarLabelWieght; + private JLabel toolBarLabelColor; + private JPanel drawPanel; + + public FormWarship(){ + InitializeComponent(); + } + private void Draw(){ + drawingComponents.repaint(); + } + + private void InitializeComponent(){ + setContentPane(mainPanel); + setTitle("Warship"); + setSize(900,700); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setVisible(true); + + Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg"); + buttonUp.setIcon(iconUp); + Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg"); + buttonDown.setIcon(iconDown); + Icon iconLeft = new ImageIcon("src\\Images\\ArrowLeft.jpg"); + buttonLeft.setIcon(iconLeft); + Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg"); + buttonRight.setIcon(iconRight); + + drawingComponents = new DrawingComponents(); + drawPanel.add(drawingComponents); + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + _warship = new DrawingWarship(); + _warship.Init(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256))); + _warship.SetPosition(rnd.nextInt(90) + 10, rnd.nextInt(90) + 10, drawPanel.getWidth(), drawPanel.getHeight()); + toolBarLabelSpeed.setText("Color: " + _warship.GetWarship().GetSpeed() + " "); + toolBarLabelWieght.setText("Weight: " + _warship.GetWarship().GetWeight() + " "); + toolBarLabelColor.setText("Color: " + _warship.GetWarship().GetBodyColor().getRed() + " " + + _warship.GetWarship().GetBodyColor().getGreen() + " " + _warship.GetWarship().GetBodyColor().getBlue()); + drawingComponents.SetDrawingWarship(_warship); + Draw(); + } + }); + + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_warship != null) _warship.MoveTransport(Direction.Left); + Draw(); + } + }); + + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_warship != null) _warship.MoveTransport(Direction.Right); + Draw(); + } + }); + + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_warship != null) _warship.MoveTransport(Direction.Up); + Draw(); + } + }); + + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if(_warship != null) _warship.MoveTransport(Direction.Down); + Draw(); + } + }); + + drawPanel.addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + _warship.ChangeBorders(drawPanel.getWidth(),drawPanel.getHeight()); + Draw(); + } + }); + } +} diff --git a/src/Images/ArrowDown.jpg b/src/Images/ArrowDown.jpg new file mode 100644 index 0000000..4813a5b Binary files /dev/null and b/src/Images/ArrowDown.jpg differ diff --git a/src/Images/ArrowLeft.jpg b/src/Images/ArrowLeft.jpg new file mode 100644 index 0000000..0f9441d Binary files /dev/null and b/src/Images/ArrowLeft.jpg differ diff --git a/src/Images/ArrowRight.jpg b/src/Images/ArrowRight.jpg new file mode 100644 index 0000000..cea4be5 Binary files /dev/null and b/src/Images/ArrowRight.jpg differ diff --git a/src/Images/ArrowUp.jpg b/src/Images/ArrowUp.jpg new file mode 100644 index 0000000..80d3aa0 Binary files /dev/null and b/src/Images/ArrowUp.jpg differ diff --git a/src/Main.java b/src/Main.java index 978fcd5..2603a8a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,6 @@ public class Main { public static void main(String[] args) { + new FormWarship(); } + }