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/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 index 6899b28..04c5463 100644 --- a/src/DrawingWarship.java +++ b/src/DrawingWarship.java @@ -2,8 +2,8 @@ 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; @@ -18,6 +18,8 @@ public class DrawingWarship { { 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) @@ -68,12 +70,11 @@ public class DrawingWarship { break; } } - public void DrawTransport(Graphics g){ + public void DrawTransport(Graphics2D g2){ if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) { return; } - Graphics2D g2 = (Graphics2D) g; //главная палуба int[] pointXWarship = {_startPosX + 4, _startPosX + 94, _startPosX + 114, _startPosX + 94, _startPosX + 4}; int[] pointYWarship = {_startPosY, _startPosY, _startPosY + 20, _startPosY + 40, _startPosY + 40}; @@ -102,7 +103,19 @@ public class DrawingWarship { 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; diff --git a/src/EntityWarship.java b/src/EntityWarship.java index 22b22c2..092a65b 100644 --- a/src/EntityWarship.java +++ b/src/EntityWarship.java @@ -17,7 +17,7 @@ public class EntityWarship { Speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed; Weight = weight <= 0 ? rnd.nextInt(30)+40 : weight; BodyColor= bodyColor; - Step = Speed * 2000 / Weight; + Step = Speed * 100 / Weight; } } diff --git a/src/FormWarship.form b/src/FormWarship.form index ab0eb6c..12eb718 100644 --- a/src/FormWarship.form +++ b/src/FormWarship.form @@ -3,7 +3,7 @@ - + @@ -21,24 +21,19 @@ - - - - - - - - + + + - + - + @@ -46,44 +41,72 @@ - + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - + + + - + - - - + + + - + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/FormWarship.java b/src/FormWarship.java index dbd6b87..404303a 100644 --- a/src/FormWarship.java +++ b/src/FormWarship.java @@ -1,32 +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 statusStrip; + 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