diff --git a/src/res/arrowDown.png b/src/res/arrowDown.png new file mode 100644 index 0000000..f612502 Binary files /dev/null and b/src/res/arrowDown.png differ diff --git a/src/res/arrowLeft.png b/src/res/arrowLeft.png new file mode 100644 index 0000000..dcdea7e Binary files /dev/null and b/src/res/arrowLeft.png differ diff --git a/src/res/arrowRight.png b/src/res/arrowRight.png new file mode 100644 index 0000000..f8bb58a Binary files /dev/null and b/src/res/arrowRight.png differ diff --git a/src/res/arrowUp.png b/src/res/arrowUp.png new file mode 100644 index 0000000..5f88837 Binary files /dev/null and b/src/res/arrowUp.png differ diff --git a/src/src/CanvasBattleship.java b/src/src/CanvasBattleship.java new file mode 100644 index 0000000..509257e --- /dev/null +++ b/src/src/CanvasBattleship.java @@ -0,0 +1,16 @@ +import javax.swing.*; +import java.awt.*; + +public class CanvasBattleship extends JComponent { + public DrawingBattleship _drawingBattleship; + public CanvasBattleship(){} + public void paintComponent(Graphics g) { + if (_drawingBattleship == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawingBattleship.DrawTransport(g2d); + super.repaint(); + } +} diff --git a/src/src/DirectionType.java b/src/src/DirectionType.java new file mode 100644 index 0000000..bcc73bd --- /dev/null +++ b/src/src/DirectionType.java @@ -0,0 +1,12 @@ + +public enum DirectionType +{ + Up, + + Down, + + Left, + + Right, + +} diff --git a/src/src/DrawingBattleship.java b/src/src/DrawingBattleship.java new file mode 100644 index 0000000..c4eb403 --- /dev/null +++ b/src/src/DrawingBattleship.java @@ -0,0 +1,177 @@ +import java.awt.*; + +public class DrawingBattleship +{ + public EntityBattleship EntityBattleship; + + private DrawingBlocks drawingBlocks = null; + + private Integer _pictureWidth; + + private Integer _pictureHeight; + + private Integer _startPosX; + + private Integer _startPosY; + + private int _drawingBattleshipWidth = 120; + + private int _drawingBattleshipHeight = 80; + + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower, int numBlocks) + { + EntityBattleship = new EntityBattleship(); + EntityBattleship.Init(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower); + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + drawingBlocks = new DrawingBlocks(); + drawingBlocks.setNumBlocks(numBlocks); + } + + public boolean SetPictureSize(int width, int height) + { + if (width < _drawingBattleshipWidth || height < _drawingBattleshipHeight) + { + return false; + } + _pictureWidth = width; + _pictureHeight = height; + if (_startPosX != null || _startPosY != null) + { + + if (_startPosX + _drawingBattleshipWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _drawingBattleshipWidth; + } + else if (_startPosX < 0) _startPosX = 0; + if (_startPosY + _drawingBattleshipHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _drawingBattleshipHeight; + } + else if (_startPosY < 0) _startPosY = 0; + } + return true; + } + + public void SetPosition(int x, int y) { + if (!(_pictureWidth != null && _pictureHeight != null)) return; + if (x + _drawingBattleshipWidth > _pictureWidth) + { + _startPosX = x - (x + _drawingBattleshipWidth - _pictureWidth); + } + else if (x < 0) _startPosX = 0; + else _startPosX = x; + if (y + _drawingBattleshipHeight > _pictureHeight) + { + _startPosY = y - (y + _drawingBattleshipHeight - _pictureHeight); + } + else if (y < 0) _startPosY = 0; + else _startPosY = y; + } + + public boolean MoveTransport(DirectionType direction) { + if (EntityBattleship == null || _startPosX == null || _startPosY == null) return false; + switch (direction) { + case Left: + if (_startPosX - EntityBattleship.Step > 0) { + _startPosX -= (int)EntityBattleship.Step; + } + return true; + case Up: + if (_startPosY - EntityBattleship.Step > 0) + { + _startPosY -= (int)EntityBattleship.Step; + } + return true; + case Right: + if (_startPosX + _drawingBattleshipWidth + (int)EntityBattleship.Step < _pictureWidth - EntityBattleship.Step) + { + _startPosX += (int)EntityBattleship.Step; + } + return true; + case Down: + if (_startPosY + _drawingBattleshipHeight + (int)EntityBattleship.Step < _pictureHeight - EntityBattleship.Step) + { + _startPosY += (int)EntityBattleship.Step; + } + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics2D g) + { + if (EntityBattleship == null || _startPosX == null || _startPosY == null) + { + return; + } + int y = _startPosY; + + Color bodyColor = EntityBattleship.getBodyColor(); + Color additionalColor = EntityBattleship.getAdditionalColor(); + + //границы линкора + g.drawRect(_startPosX + 10, _startPosY, 80, 40); //ширина высота (сама палуба) + //заливка линкора + g.setColor(EntityBattleship.getBodyColor()); + g.fillRect(_startPosX + 10, _startPosY, 80, 40); + + //границы двигателей + g.drawRect(_startPosX + 5, _startPosY + 7, 5, 10); //двигатель верхний + g.drawRect(_startPosX + 5, _startPosY + 22, 5, 10); //двигатель нижний + //заливка двигателей (2 фигни сзади) + g.setColor(Color.BLACK); + g.fillRect(_startPosX + 5, _startPosY + 7, 5, 10); //верхний + g.fillRect(_startPosX + 5, _startPosY + 22, 5, 10); //нижний + + // блоки + drawingBlocks.drawBlocks(g, Color.BLACK, _startPosX, _startPosY); + + //носик палубы + int[] xPoints = {_startPosX + 90,_startPosX + 120, _startPosX + 90}; + int[] yPoints = {_startPosY,_startPosY + 20,_startPosY + 40}; + Polygon Points = new Polygon(xPoints, yPoints, 3); + g.drawPolygon(Points); + g.setColor(EntityBattleship.BodyColor); + g.fillPolygon(Points); + + + //круг на палубе + g.drawOval(_startPosX + 78, _startPosY + 12, 15, 15); + //заливка круга + g.setColor(Color.BLUE); + g.fillOval(_startPosX + 78, _startPosY + 12, 15, 15); + + //ПРОВЕРКА + + if (EntityBattleship.Compartment) + { + //отсеки для ракет + //заливка отсеков + g.setColor(EntityBattleship.getAdditionalColor()); + g.fillRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек для ракет + g.fillRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек для ракет + + //границы отсеков + g.drawRect(_startPosX + 15, _startPosY + 5, 27, 5); //верхний отсек + g.drawRect(_startPosX + 15, _startPosY + 28, 27, 5); //нижний отсек + + } + + if (EntityBattleship.Tower) + { + //границы башни + g.drawRect(_startPosX + 107, _startPosY + 17, 30, 5); + g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10); + + //заливка башни + g.setColor(EntityBattleship.getAdditionalColor()); + g.fillOval(_startPosX + 98, _startPosY + 15, 10, 10); + g.fillRect(_startPosX + 107, _startPosY + 17, 30, 5); + + } + } +} diff --git a/src/src/DrawingBlocks.java b/src/src/DrawingBlocks.java new file mode 100644 index 0000000..27d1e5c --- /dev/null +++ b/src/src/DrawingBlocks.java @@ -0,0 +1,96 @@ +import java.awt.*; + +public class DrawingBlocks { + private NumberBlock numBlocks; + + //определяет количество блоков + public void setNumBlocks(int number) { + switch (number) { + case 1: + numBlocks = NumberBlock.Two; + break; + case 2: + numBlocks = NumberBlock.Four; + break; + case 3: + numBlocks = NumberBlock.Six; + break; + default: + numBlocks = NumberBlock.Four; + } + } + + public void drawBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + switch (numBlocks) { + case Two: + drawTwoBlocks(g2D, color, _startPosX, _startPosY); + break; + case Four: + drawFourBlocks(g2D, color, _startPosX, _startPosY); + break; + case Six: + drawSixBlocks(g2D, color, _startPosX, _startPosY); + break; + } + } + + private void drawTwoBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + //прямоугольник левый на палубе (горизонтальный) + g2D.setColor(Color.LIGHT_GRAY); + g2D.drawRect(_startPosX + 32, _startPosY + 13, 21, 12); + g2D.fillRect(_startPosX + 32, _startPosY + 13, 21, 12); + + //прямоугольник правый на палубе (вертикальный) + g2D.setColor(Color.GRAY); + g2D.drawRect(_startPosX + 53, _startPosY + 7, 16, 24); + g2D.fillRect(_startPosX + 53, _startPosY + 7, 16, 24); + } + + private void drawFourBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + + //прямоугольник левый на палубе (горизонтальный) + g2D.setColor(Color.magenta); + g2D.drawRect(_startPosX + 32, _startPosY + 13, 8, 12); + g2D.fillRect(_startPosX + 32, _startPosY + 13, 8, 12); + + //прямоугольник правый на палубе (вертикальный) + g2D.setColor(Color.GREEN); + g2D.drawRect(_startPosX + 53, _startPosY + 8, 16, 8); + g2D.fillRect(_startPosX + 53, _startPosY + 8, 16, 8); + + g2D.setColor(Color.GREEN); + g2D.drawRect(_startPosX + 53, _startPosY + 23, 16, 8); + g2D.fillRect(_startPosX + 53, _startPosY + 23, 16, 8); + + g2D.setColor(Color.DARK_GRAY); + g2D.drawRect(_startPosX + 19, _startPosY + 13, 8, 12); + g2D.fillRect(_startPosX + 19, _startPosY + 13, 8, 12); + + } + + private void drawSixBlocks(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(Color.GRAY); + g2D.drawRect(_startPosX + 19, _startPosY + 13, 10, 13); + g2D.fillRect(_startPosX + 19, _startPosY + 13, 10, 13); + + g2D.setColor(Color.LIGHT_GRAY); + g2D.drawRect(_startPosX + 31, _startPosY + 15, 12, 9); + g2D.fillRect(_startPosX + 31, _startPosY + 15, 12, 9); + + g2D.setColor(Color.PINK); + g2D.drawRect(_startPosX + 46, _startPosY + 13, 10, 13); + g2D.fillRect(_startPosX + 46, _startPosY + 13, 10, 13); + + g2D.setColor(Color.GRAY); + g2D.drawRect(_startPosX + 64, _startPosY + 4, 7, 8); + g2D.fillRect(_startPosX + 64, _startPosY + 4, 7, 8); + + g2D.setColor(Color.GRAY); + g2D.drawRect(_startPosX + 64, _startPosY + 15, 7, 8); + g2D.fillRect(_startPosX + 64, _startPosY + 15, 7, 8); + + g2D.setColor(Color.GRAY); + g2D.drawRect(_startPosX + 64, _startPosY + 28, 7, 8); + g2D.fillRect(_startPosX + 64, _startPosY + 28, 7, 8); + } +} diff --git a/src/src/EntityBattleship.java b/src/src/EntityBattleship.java new file mode 100644 index 0000000..4b5b2b0 --- /dev/null +++ b/src/src/EntityBattleship.java @@ -0,0 +1,28 @@ +import java.awt.*; +import java.util.Random; + +public class EntityBattleship +{ + public int Speed; + public double Weight; + public Color BodyColor; + public Color AdditionalColor; + public boolean BodyDeck; + public boolean Compartment; + public boolean Tower; + public double Step; + public Color getBodyColor() { return BodyColor; } + public Color getAdditionalColor() { return AdditionalColor; } + public double getStep() { return Speed * 100 / Weight; } + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyDeck, boolean compartment, boolean tower) { + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; + Weight = weight <= 0 ? rnd.nextInt(40,70) : weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + BodyDeck = bodyDeck; + Compartment = compartment; + Tower = tower; + Step = Speed * 100 / Weight; + } +} diff --git a/src/src/FormBattleship.java b/src/src/FormBattleship.java new file mode 100644 index 0000000..f6b0df2 --- /dev/null +++ b/src/src/FormBattleship.java @@ -0,0 +1,125 @@ +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 FormBattleship extends JFrame { + private String title; + private Dimension dimension; + private int Width, Height; + private CanvasBattleship canvasBattleship = new CanvasBattleship(); + private JButton CreateButton = new JButton("Создать");; + private JButton UpButton = new JButton(); + private JButton DownButton = new JButton();; + private JButton LeftButton = new JButton();; + private JButton RightButton = new JButton(); + public FormBattleship(String title, Dimension dimension) { + this.title = title; + this.dimension = dimension; + } + public void Init() { + setTitle(title); + setMinimumSize(dimension); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + Width = getWidth() - 15; + Height = getHeight() - 35; + + CreateButton.setName("CREATE"); + Icon iconUp = new ImageIcon("src/res/arrowUp.png"); + UpButton.setIcon(iconUp); + UpButton.setName("Up"); + DownButton.setName("Down"); + Icon iconDown = new ImageIcon("src/res/arrowDown.png"); + DownButton.setIcon(iconDown); + LeftButton.setName("Left"); + Icon iconLeft = new ImageIcon("src/res/arrowLeft.png"); + LeftButton.setIcon(iconLeft); + RightButton.setName("Right"); + Icon iconRight = new ImageIcon("src/res/arrowRight.png"); + RightButton.setIcon(iconRight); + + CreateButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int StartPositionX = (int)(Math.random() * 90 + 10); + int StartPositionY = (int)(Math.random() * 90 + 10); + int speed = (int)(Math.random() * 300 + 100); + double weight = (double)(Math.random() * 3000 + 1000); + Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); + Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; + boolean bodyDeck = new Random().nextBoolean(); + boolean compartment = new Random().nextBoolean();; + boolean tower = new Random().nextBoolean(); + int blocksNum = (int)(Math.random() * 6) + 1; + canvasBattleship._drawingBattleship = new DrawingBattleship(); + canvasBattleship._drawingBattleship.Init(speed, weight, bodyColor, additionalColor, bodyDeck, compartment, tower, blocksNum); + canvasBattleship._drawingBattleship.SetPictureSize(Width, Height); + canvasBattleship._drawingBattleship.SetPosition(StartPositionX, StartPositionY); + canvasBattleship.repaint(); + } + }); + + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + if (canvasBattleship._drawingBattleship == null) return; + boolean result = false; + switch ((((JButton)(event.getSource())).getName())) { + case "Up": + result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Up); + break; + case "Down": + result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Down); + break; + case "Left": + result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Left); + break; + case "Right": + result = canvasBattleship._drawingBattleship.MoveTransport(DirectionType.Right); + break; + } + if (result) { + canvasBattleship.repaint(); + } + } + }; + UpButton.addActionListener(actionListener); + DownButton.addActionListener(actionListener); + LeftButton.addActionListener(actionListener); + RightButton.addActionListener(actionListener); + + setSize(dimension.width,dimension.height); + setLayout(null); + canvasBattleship.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 100, 40); + UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); + DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); + RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); + LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); + add(CreateButton); + add(UpButton); + add(DownButton); + add(RightButton); + add(LeftButton); + add(canvasBattleship); + setVisible(true); + //обработка события изменения размеров окна + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + Width = getWidth() - 15; + Height = getHeight() - 35; + if (canvasBattleship._drawingBattleship != null) + canvasBattleship._drawingBattleship.SetPictureSize(Width, Height); + canvasBattleship.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 100, 40); + UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); + DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); + RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); + LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); + } + }); + } +} diff --git a/src/src/Main.java b/src/src/Main.java index 3e59c38..2d4032c 100644 --- a/src/src/Main.java +++ b/src/src/Main.java @@ -1,5 +1,9 @@ +import java.awt.*; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + FormBattleship form = new FormBattleship("Линкор", new Dimension(700,500)); + form.Init(); + } } \ No newline at end of file diff --git a/src/src/NumberBlock.java b/src/src/NumberBlock.java new file mode 100644 index 0000000..4efd0c6 --- /dev/null +++ b/src/src/NumberBlock.java @@ -0,0 +1,5 @@ +public enum NumberBlock { + Two, + Four, + Six +} \ No newline at end of file