diff --git a/src/AbstractMap.java b/src/AbstractMap.java new file mode 100644 index 0000000..c586f74 --- /dev/null +++ b/src/AbstractMap.java @@ -0,0 +1,139 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; +public abstract class AbstractMap { + private IDrawningObject _drawingObject = null; + protected int[][] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected final Random _random = new Random(); + protected final int _freeRoad = 0; + protected final int _barrier = 1; + public BufferedImage CreateMap(int width, int height, IDrawningObject drawingObject) + { + _width = width; + _height = height; + _drawingObject = drawingObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + public BufferedImage MoveObject(Direction direction) + { + _drawingObject.MoveObject(direction); + float[] tuple = _drawingObject.GetCurrentPosition(); + if (CheckBarrier(tuple[0],tuple[1],tuple[2],tuple[3])) + { + _drawingObject.MoveObject(MoveObjectBack(direction)); + } + return DrawMapWithObject(); + } + private Direction MoveObjectBack(Direction direction) + { + switch (direction) + { + case Up: + return Direction.Down; + case Down: + return Direction.Up; + case Left: + return Direction.Right; + case Right: + return Direction.Left; + } + return Direction.None; + } + private boolean CheckBarrier(float Left, float Right, float Top, float Bottom) + { + int startX = (int)(Left / _size_x); + int startY = (int)(Right / _size_y); + int endX = (int)(Top / _size_x); + int endY = (int)(Bottom / _size_y); + + if (startX < 0 || startY < 0 || endX >= _map[0].length || endY >= _map.length) + { + return true; + } + for (int i = startX; i <= endX; i++) + { + for (int j = startY; j <= endY; j++) + { + if (_map[i][j] == _barrier) + { + return true; + } + } + } + return false; + } + private boolean SetObjectOnMap() + { + if (_drawingObject == null || _map == null) + { + return false; + } + int x = _random.nextInt(0, 10); + int y = _random.nextInt(0, 10); + _drawingObject.SetObject(x, y, _width, _height); + + float[] tuple = _drawingObject.GetCurrentPosition(); + + if (!CheckBarrier(tuple[0], tuple[1], tuple[2], tuple[3])) return true; + float startX = tuple[0]; + float startY = tuple[1]; + float lengthX = tuple[2] - tuple[0]; + float lengthY = tuple[3] - tuple[1]; + while (CheckBarrier(startX, startY, startX + lengthX, startY + lengthY)) + { + boolean result; + do + { + result = CheckBarrier(startX, startY, startX + lengthX, startY + lengthY); + if (!result) + { + _drawingObject.SetObject((int)startX, (int)startY, _width, _height); + return true; + } + else + { + startX += _size_x; + } + } while (result); + startX = x; + startY += _size_y; + } + return false; + } + private BufferedImage DrawMapWithObject() + { + BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); + if (_drawingObject == null || _map == null) + { + return bmp; + } + Graphics gr = bmp.getGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart(gr, i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + return bmp; + } + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics g, int i, int j); + protected abstract void DrawBarrierPart(Graphics g, int i, int j); +} diff --git a/src/Direction.java b/src/Direction.java index 996463a..5269225 100644 --- a/src/Direction.java +++ b/src/Direction.java @@ -1,4 +1,5 @@ public enum Direction { + None, Up, Down, Left, diff --git a/src/DrawingBoat.java b/src/DrawingBoat.java index d8d8def..0726732 100644 --- a/src/DrawingBoat.java +++ b/src/DrawingBoat.java @@ -1,23 +1,34 @@ import javax.swing.*; import java.awt.*; +import java.util.Random; + public class DrawingBoat extends JComponent { public EntityBoat Boat; - private float _startPosX; - private float _startPosY; + protected float _startPosX; + protected float _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; - private final int _boatWidth = 90; - private final int _boatHeight = 80; - private DrawingPaddle _paddles; - public DrawingBoat() { - super(); - } - public void Init(int speed, float weight, Color bodyColor, int paddleCount) + private int _boatWidth = 90; + private int _boatHeight = 80; + private IDrawningObjectPaddle _paddles; + public DrawingBoat(int speed, float weight, Color bodyColor, int paddleCount) { - Boat = new EntityBoat(); - Boat.Init(speed, weight, bodyColor); - _paddles = new DrawingPaddle(); - _paddles.SetPaddlesAmount(paddleCount); + Random random = new Random(); + Boat = new EntityBoat(speed, weight, bodyColor); + int randomPattern=random.nextInt(3); + if (randomPattern==0) + _paddles = new DrawingPaddleTriangle(); + else if(randomPattern==1) + _paddles = new DrawingPaddleSquare(); + else + _paddles = new DrawingPaddle(); + _paddles.SetPaddlesCount(paddleCount); + } + protected DrawingBoat(int speed, float weight, Color bodyColor, int paddleCount, int boatWidth, int boatHeight) + { + this(speed, weight, bodyColor, paddleCount); + _boatWidth = boatWidth; + _boatHeight = boatHeight; } public void SetPosition(int x, int y, int width, int height) { @@ -27,8 +38,8 @@ public class DrawingBoat extends JComponent { _pictureHeight = null; return; } - _startPosX = x; - _startPosY = y; + _startPosX = x+20; + _startPosY = y+20; _pictureWidth = width; _pictureHeight = height; } @@ -43,28 +54,29 @@ public class DrawingBoat extends JComponent { case Right: { if (_startPosX + _boatWidth + Boat.Step() < _pictureWidth) { + System.out.println(Boat.Step()); _startPosX += Boat.Step(); } break; } - case Left: - if (_startPosX - Boat.Step() > 0) - { + case Left: { + if (_startPosX - Boat.Step() > 0) { _startPosX -= Boat.Step(); } break; - case Up: - if (_startPosY - Boat.Step() > 0) - { + } + case Up: { + if (_startPosY - Boat.Step() > 0) { _startPosY -= Boat.Step(); } break; - case Down: - if (_startPosY + _boatHeight + Boat.Step() < _pictureHeight) - { + } + case Down: { + if (_startPosY + _boatHeight + Boat.Step() < _pictureHeight) { _startPosY += Boat.Step(); } break; + } } } public void paintComponent(Graphics gr) @@ -77,7 +89,6 @@ public class DrawingBoat extends JComponent { return; } Color pen = new Color(0,0,0); - //границы лодки int [] pointsX = new int[]{(int)(_startPosX),(int)(_startPosX + 50),(int)(_startPosX + 70),(int)(_startPosX + 50),(int)(_startPosX)}; int [] pointsY = new int[]{(int)(_startPosY),(int)(_startPosY),(int)(_startPosY + 20),(int)(_startPosY + 40),(int)(_startPosY+40)}; @@ -116,4 +127,12 @@ public class DrawingBoat extends JComponent { _startPosY = _pictureHeight - _boatHeight; } } + public float[] GetCurrentPosition(){ + float[] tuple = new float[4]; + tuple[0] = _startPosX; + tuple[1] =_startPosY; + tuple[2] = _startPosX + _boatWidth; + tuple[3] = _startPosY + _boatHeight; + return tuple; + } } diff --git a/src/DrawingPaddle.java b/src/DrawingPaddle.java index d4327b3..a47b64b 100644 --- a/src/DrawingPaddle.java +++ b/src/DrawingPaddle.java @@ -1,8 +1,8 @@ -import javax.swing.*; import java.awt.*; -public class DrawingPaddle extends JComponent { +public class DrawingPaddle implements IDrawningObjectPaddle { private AdditionalDirection _paddle; - public void SetPaddlesAmount(int rpaddlesAmount) { + @Override + public void SetPaddlesCount(int rpaddlesAmount) { for (AdditionalDirection item: _paddle.values()) { if (item.getCount() == rpaddlesAmount) { _paddle = item; @@ -10,10 +10,9 @@ public class DrawingPaddle extends JComponent { } } } + @Override public void DrawPaddles(Graphics gr, int _startPosPaddlesX, int _startPosPaddlessY, Color pen) { - super.paintComponent(gr); Graphics2D g=(Graphics2D)gr; - _startPosPaddlesX +=40; _startPosPaddlessY +=33; if (_paddle.getCount() >= 1) { @@ -28,9 +27,7 @@ public class DrawingPaddle extends JComponent { } protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1,int _startPosY2, Color pen){ try { g.setPaint(pen); } - catch (Exception e) { - g.setPaint(Color.black); - } + catch (Exception e) { g.setPaint(Color.black); } g.fillRect(_startPosX,_startPosY1,4,17); g.fillOval(_startPosX-1, _startPosY2, 6, 10); g.setPaint(Color.black); diff --git a/src/DrawingPaddleSquare.java b/src/DrawingPaddleSquare.java new file mode 100644 index 0000000..fccf6f7 --- /dev/null +++ b/src/DrawingPaddleSquare.java @@ -0,0 +1,47 @@ +import java.awt.*; + +public class DrawingPaddleSquare implements IDrawningObjectPaddle { + private AdditionalDirection _paddleDirect; + + @Override + public void SetPaddlesCount(int paddlesCount) { + for (AdditionalDirection item : _paddleDirect.values()) { + if (item.getCount() == paddlesCount) { + _paddleDirect = item; + return; + } + } + } + @Override + public void DrawPaddles(Graphics gr, int _startPosPaddlesX, int _startPosPaddlessY, Color mainBrush) { + Graphics2D g = (Graphics2D) gr; + Color br = new Color(0, 0, 0); + try { + br = mainBrush; + } catch (Exception e) { + } + _startPosPaddlesX += 40; + _startPosPaddlessY += 33; + if (_paddleDirect.getCount() >= 1) { + paintPaddle(g, _startPosPaddlesX, _startPosPaddlessY, _startPosPaddlessY + 16, mainBrush); + } + if (_paddleDirect.getCount() >= 2) { + paintPaddle(g, _startPosPaddlesX - 10, _startPosPaddlessY - 41, _startPosPaddlessY - 51, mainBrush); + } + if (_paddleDirect.getCount() >= 3) { + paintPaddle(g, _startPosPaddlesX - 20, _startPosPaddlessY, _startPosPaddlessY + 16, mainBrush); + } + } + protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1, int _startPosY2, Color pen) { + try { + g.setPaint(pen); + } catch (Exception e) { + g.setPaint(Color.black); + } + g.fillRect(_startPosX, _startPosY1, 4, 17); + g.fillRect(_startPosX - 5, _startPosY2, 13, 13); + g.setPaint(Color.black); + g.drawRect(_startPosX, _startPosY1, 4, 17); + g.drawRect(_startPosX - 5, _startPosY2, 13, 13); + } +} diff --git a/src/DrawingPaddleTriangle.java b/src/DrawingPaddleTriangle.java new file mode 100644 index 0000000..2cd084b --- /dev/null +++ b/src/DrawingPaddleTriangle.java @@ -0,0 +1,70 @@ +import java.awt.*; + +public class DrawingPaddleTriangle implements IDrawningObjectPaddle { + private AdditionalDirection _paddleDirect; + + @Override + public void SetPaddlesCount(int paddlesCount) { + for (AdditionalDirection item : _paddleDirect.values()) { + if (item.getCount() == paddlesCount) { + _paddleDirect = item; + return; + } + } + } + + @Override + public void DrawPaddles(Graphics gr, int _startPosPaddlesX, int _startPosPaddlessY, Color mainBrush) { + Graphics2D g = (Graphics2D) gr; + + Color br = new Color(0, 0, 0); + try { + br = mainBrush; + } catch (Exception e) { + } + _startPosPaddlesX += 40; + _startPosPaddlessY += 33; + if (_paddleDirect.getCount() >= 1) { + paintPaddleTriangle(g, _startPosPaddlesX - 3, _startPosPaddlessY + 5, _startPosPaddlessY + 16 + 5, mainBrush, false); + paintPaddle(g, _startPosPaddlesX, _startPosPaddlessY, _startPosPaddlessY + 16, mainBrush); + } + if (_paddleDirect.getCount() >= 2) { + paintPaddleTriangle(g, _startPosPaddlesX - 10 - 3, _startPosPaddlessY - 41 + 5, _startPosPaddlessY - 51 + 5, mainBrush, true); + paintPaddle(g, _startPosPaddlesX - 10, _startPosPaddlessY - 41, _startPosPaddlessY - 51, mainBrush); + } + if (_paddleDirect.getCount() >= 3) { + paintPaddleTriangle(g, _startPosPaddlesX - 20 - 3, _startPosPaddlessY + 5, _startPosPaddlessY + 16 + 5, mainBrush, false); + paintPaddle(g, _startPosPaddlesX - 20, _startPosPaddlessY, _startPosPaddlessY + 16, mainBrush); + } + } + + protected void paintPaddleTriangle(Graphics2D g, int _startPosX, int _startPosY1, int _startPosY2, Color pen, boolean need) { + try { + g.setPaint(pen); + } catch (Exception e) { + g.setPaint(Color.black); + } + if (need) { + g.fillPolygon(new int[]{_startPosX, _startPosX + 5, _startPosX + 10}, new int[]{_startPosY2, _startPosY2 - 10, _startPosY2}, 3); + g.setPaint(Color.black); + g.drawPolygon(new int[]{_startPosX, _startPosX + 5, _startPosX + 10}, new int[]{_startPosY2, _startPosY2 - 10, _startPosY2}, 3); + } else { + g.fillPolygon(new int[]{_startPosX, _startPosX + 5, _startPosX + 10}, new int[]{_startPosY2, _startPosY2 + 10, _startPosY2}, 3); + g.setPaint(Color.black); + g.drawPolygon(new int[]{_startPosX, _startPosX + 5, _startPosX + 10}, new int[]{_startPosY2, _startPosY2 + 10, _startPosY2}, 3); + } + } + + protected void paintPaddle(Graphics2D g, int _startPosX, int _startPosY1, int _startPosY2, Color pen) { + try { + g.setPaint(pen); + } catch (Exception e) { + g.setPaint(Color.black); + } + g.fillRect(_startPosX, _startPosY1, 4, 17); + g.fillOval(_startPosX - 1, _startPosY2, 6, 10); + g.setPaint(Color.black); + g.drawRect(_startPosX, _startPosY1, 4, 17); + g.drawOval(_startPosX - 1, _startPosY2, 6, 10); + } +} \ No newline at end of file diff --git a/src/DrawningMotorBoat.java b/src/DrawningMotorBoat.java new file mode 100644 index 0000000..cecb9cd --- /dev/null +++ b/src/DrawningMotorBoat.java @@ -0,0 +1,61 @@ +import java.awt.*; + +public class DrawningMotorBoat extends DrawingBoat { + public DrawningMotorBoat(int speed, float weight, Color bodyColor, int paddleCount, Color dopColor, boolean nose, boolean side) { + super(speed, weight, bodyColor, paddleCount, 138, 78); + Boat = new EntityMotorBoat(speed, weight, bodyColor, dopColor, nose, side); + } + + @Override + public void paintComponent(Graphics gr) { + boolean check = Boat instanceof EntityMotorBoat; + if (!check) { + return; + } + EntityMotorBoat boat = (EntityMotorBoat) Boat; + Graphics2D g = (Graphics2D) gr; + Color dopBrush = boat.DopColor(); + + gr.setColor(dopBrush); + g.fillOval((int) _startPosX - 17, (int) _startPosY - 24, 14, 5); + gr.setColor(Color.BLACK); + g.drawOval((int) _startPosX - 17, (int) _startPosY - 24, 14, 5); + gr.setColor(dopBrush); + g.fillRect((int) _startPosX - 10, (int) _startPosY - 30, 10, 20); + gr.setColor(Color.BLACK); + g.drawRect((int) _startPosX - 10, (int) _startPosY - 30, 10, 20); + + int[] pointsx; + int[] pointsy; + + if (boat.Side()) { + pointsx = new int[]{(int) (_startPosX), (int) (_startPosX), (int) (_startPosX + 50), (int) (_startPosX + 50)}; + pointsy = new int[]{(int) (_startPosY - 50), (int) (_startPosY + 10), (int) (_startPosY), (int) (_startPosY - 40)}; + gr.setColor(dopBrush); + g.fillPolygon(pointsx, pointsy, 4); + gr.setColor(Color.BLACK); + g.drawPolygon(pointsx, pointsy, 4); + } + if (boat.Nose()) { + pointsx = new int[]{(int) (_startPosX) + 50, (int) (_startPosX) + 80, (int) (_startPosX + 50)}; + pointsy = new int[]{(int) (_startPosY - 40), (int) (_startPosY - 20), (int) (_startPosY)}; + gr.setColor(dopBrush); + g.fillPolygon(pointsx, pointsy, 3); + gr.setColor(Color.BLACK); + g.drawPolygon(pointsx, pointsy, 3); + } + _startPosY -= 40; + super.paintComponent(gr); + _startPosY += 40; + + pointsx = new int[]{(int) (_startPosX) + 50, (int) (_startPosX) + 57, (int) (_startPosX + 50)}; + pointsy = new int[]{(int) (_startPosY - 34), (int) (_startPosY - 20), (int) (_startPosY - 6)}; + gr.setColor(Color.CYAN); + g.fillPolygon(pointsx, pointsy, 3); + gr.setColor(Color.BLACK); + ((Graphics2D) gr).setStroke(new BasicStroke(1)); + g.drawPolygon(pointsx, pointsy, 3); + + } +} + diff --git a/src/DrawningObjectBoat.java b/src/DrawningObjectBoat.java new file mode 100644 index 0000000..f276617 --- /dev/null +++ b/src/DrawningObjectBoat.java @@ -0,0 +1,34 @@ +import java.awt.*; + +public class DrawningObjectBoat implements IDrawningObject{ + private DrawingBoat _boat = null; + public DrawningObjectBoat(DrawingBoat boat) { + _boat = boat; + } + @Override + public float Step() { + if (_boat!=null && _boat.Boat != null) { + return _boat.Boat.Step(); + } + return 0; + } + @Override + public void SetObject(int x, int y, int width, int height) { + _boat.SetPosition(x+20, y+50, width, height); + } + @Override + public void MoveObject(Direction direction) { + _boat.MoveTransport(direction); + } + @Override + public void DrawingObject(Graphics g) { + _boat.paintComponent(g); + } + @Override + public float[] GetCurrentPosition() + { + if(_boat!=null) + return _boat.GetCurrentPosition(); + return null; + } +} diff --git a/src/EntityBoat.java b/src/EntityBoat.java index 1b9d07b..42dc614 100644 --- a/src/EntityBoat.java +++ b/src/EntityBoat.java @@ -3,7 +3,9 @@ import java.util.Random; public class EntityBoat { private int speed; - public int Speed() { return speed; } + public int Speed() { + return speed; + } private float weight; public float Weight() { return weight; @@ -12,10 +14,10 @@ public class EntityBoat { public Color BodyColor() { return bodyColor; } - public float Step() - { return Speed() * 20 / Weight(); } - public void Init(int speed, float weight, Color bodyColor) - { + public float Step() { + return Speed() * 20 / Weight(); + } + public EntityBoat(int speed, float weight, Color bodyColor) { Random random = new Random(); this.speed = speed <= 0 ? random.nextInt(50, 150) : speed; this.weight = weight <= 0 ? random.nextInt(40, 70) : weight; diff --git a/src/EntityMotorBoat.java b/src/EntityMotorBoat.java new file mode 100644 index 0000000..39949a1 --- /dev/null +++ b/src/EntityMotorBoat.java @@ -0,0 +1,16 @@ +import java.awt.*; +public class EntityMotorBoat extends EntityBoat{ + private Color dopColor; + public Color DopColor() { return dopColor; } + private boolean nose; + public boolean Nose() { return nose; } + private boolean side; + public boolean Side() { return side; } + public EntityMotorBoat(int speed, float weight, Color bodyColor, Color dopColor, boolean nose, boolean side) + { + super(speed*100, weight, bodyColor); + this.dopColor = dopColor; + this.nose = nose; + this.side = side; + } +} diff --git a/src/FormBoat.form b/src/FormBoat.form index 89f3a52..aa8d755 100644 --- a/src/FormBoat.form +++ b/src/FormBoat.form @@ -1,6 +1,6 @@
- + @@ -21,7 +21,7 @@ - + @@ -99,17 +99,17 @@ - + - + - + @@ -122,7 +122,7 @@ - + @@ -185,6 +185,18 @@ + + + + + + + + + + + + diff --git a/src/FormBoat.java b/src/FormBoat.java index 37f65ac..6765d3a 100644 --- a/src/FormBoat.java +++ b/src/FormBoat.java @@ -3,15 +3,6 @@ import java.awt.*; import java.awt.event.*; import java.util.Random; public class FormBoat extends JFrame { - public static void main(String[] args) { - - FormBoat window = new FormBoat(); - window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - window.pack(); - window.setLocationRelativeTo(null); - window.setVisible(true); - } - private JPanel mainPanel; private JPanel statusStrip; private JLabel toolStripStatusLabelSpeed; @@ -28,6 +19,7 @@ public class FormBoat extends JFrame { private JRadioButton radioButtonPaddle3; private JPanel radioButtonsBox; private JPanel pictureBoxBoat; + private JButton buttonCreateModif; private DrawingBoat _boat; private int pictureBoxBoatWidth; private int pictureBoxBoatHeight; @@ -40,7 +32,6 @@ public class FormBoat extends JFrame { buttonGroupPaddlesRadBut.add(radioButtonPaddle3); setPreferredSize(new Dimension(1000, 700)); getContentPane().add(mainPanel); - buttonCreate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -49,16 +40,24 @@ public class FormBoat extends JFrame { } catch (Exception c) { } Random random = new Random(); - _boat = new DrawingBoat(); - _boat.Init(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256), - random.nextInt(0, 256), random.nextInt(0, 256)), GetRollersAmount()); - ChangePictureBoxBoatBorders(); - _boat.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxBoatWidth, pictureBoxBoatHeight); - toolStripStatusLabelSpeed.setText("Скорость: " + _boat.Boat.Speed()); - toolStripStatusLabelWeight.setText("Вес: " + _boat.Boat.Weight()); - - toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_boat.Boat.BodyColor().getRGB())); - pictureBoxBoat.add(_boat, BorderLayout.CENTER); + _boat = new DrawingBoat(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256), + random.nextInt(0, 256), random.nextInt(0, 256)), GetPaddleCount()); + SetData(); + } + }); + buttonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + pictureBoxBoat.remove(_boat); + } catch (Exception c) { + } + Random random = new Random(); + _boat = new DrawningMotorBoat(random.nextInt(30, 50), random.nextInt(1000, 2000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), GetPaddleCount(), new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean()); + SetData(); } }); addComponentListener(new ComponentAdapter() { @@ -75,7 +74,6 @@ public class FormBoat extends JFrame { pictureBoxBoat.add(_boat, BorderLayout.CENTER); } }); - //джижение ButtonsMove buttonsMove = new ButtonsMove(); buttonUp.setName("Up"); buttonLeft.setName("Left"); @@ -85,6 +83,15 @@ public class FormBoat extends JFrame { buttonLeft.addActionListener(buttonsMove); buttonRight.addActionListener(buttonsMove); buttonDown.addActionListener(buttonsMove); + } + private void SetData() { + Random random = new Random(); + ChangePictureBoxBoatBorders(); + _boat.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxBoatWidth, pictureBoxBoatHeight); + toolStripStatusLabelSpeed.setText("Скорость: " + _boat.Boat.Speed()); + toolStripStatusLabelWeight.setText("Вес: " + _boat.Boat.Weight()); + toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_boat.Boat.BodyColor().getRGB())); + pictureBoxBoat.add(_boat, BorderLayout.CENTER); } class ButtonsMove implements ActionListener { public void actionPerformed(ActionEvent e) { @@ -99,7 +106,6 @@ public class FormBoat extends JFrame { } } } - private void ChangePictureBoxBoatBorders() { char[] temp = pictureBoxBoat.getSize().toString().toCharArray(); for (int i = 0; i < temp.length; i++) { @@ -112,7 +118,7 @@ public class FormBoat extends JFrame { pictureBoxBoatWidth = Integer.parseInt(parameters[1]); pictureBoxBoatHeight = Integer.parseInt(parameters[2]); } - private int GetRollersAmount() { + private int GetPaddleCount() { if (radioButtonPaddle1.isSelected()) { return 1; } diff --git a/src/FormMap.form b/src/FormMap.form new file mode 100644 index 0000000..e5f9191 --- /dev/null +++ b/src/FormMap.form @@ -0,0 +1,213 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/FormMap.java b/src/FormMap.java new file mode 100644 index 0000000..60717ad --- /dev/null +++ b/src/FormMap.java @@ -0,0 +1,150 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.image.BufferedImage; +import java.util.Random; +public class FormMap extends JFrame { + public static void main(String[] args) { + FormMap window = new FormMap(); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.pack(); + window.setLocationRelativeTo(null); + window.setVisible(true); + } + private JPanel mainPanel; + private JPanel statusStrip; + private JLabel toolStripStatusLabelSpeed; + private JLabel toolStripStatusLabelBodyColor; + private JLabel toolStripStatusLabelWeight; + private JPanel buttonsBox; + private JButton buttonLeft; + private JButton buttonRight; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonCreate; + private JRadioButton radioButtonPaddle1; + private JRadioButton radioButtonPaddle2; + private JRadioButton radioButtonPaddle3; + private JPanel radioButtonsBox; + private JPanel pictureBoxBoat; + private JComboBox comboBoxSelectorMap; + private JButton buttonCreateModif; + private DrawingBoat _boat; + private int pictureBoxBoatWidth; + private int pictureBoxBoatHeight; + private AbstractMap _abstractMap; + private JLabel pictureLabel; + private DrawningObjectBoat drawningObjectBoat; + ButtonGroup buttonGroupPaddlesRadBut; + public FormMap() { + super("Моторная лодка"); + buttonGroupPaddlesRadBut = new ButtonGroup(); + buttonGroupPaddlesRadBut.add(radioButtonPaddle1); + buttonGroupPaddlesRadBut.add(radioButtonPaddle2); + buttonGroupPaddlesRadBut.add(radioButtonPaddle3); + setPreferredSize(new Dimension(1000, 700)); + getContentPane().add(mainPanel); + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + var _boat = new DrawingBoat(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256), + random.nextInt(0, 256), random.nextInt(0, 256)), GetPaddleCount()); + SetData(_boat); + } + }); + buttonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + var _boat = new DrawningMotorBoat(random.nextInt(30, 50), random.nextInt(1000, 2000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), GetPaddleCount(), new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), random.nextBoolean(), random.nextBoolean()); + SetData(_boat); + } + }); + comboBoxSelectorMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + comboBoxSelectorMap = (JComboBox)e.getSource(); + String item = String.valueOf(comboBoxSelectorMap.getSelectedItem()); + System.out.println(item); + switch (item) { + case "Простая карта"-> + _abstractMap = new SimpleMap(); + case "Морская карта" -> + _abstractMap = new SeaMap(); + + } + } + }); + ButtonsMove buttonsMove = new ButtonsMove(); + buttonUp.setName("Up"); + buttonLeft.setName("Left"); + buttonRight.setName("Right"); + buttonDown.setName("Down"); + buttonUp.addActionListener(buttonsMove); + buttonLeft.addActionListener(buttonsMove); + buttonRight.addActionListener(buttonsMove); + buttonDown.addActionListener(buttonsMove); + } + private void SetData(DrawingBoat _boat) { + if (_abstractMap == null) return; + try { + pictureBoxBoat.remove(pictureLabel); + } catch (Exception c) { } + ChangePictureBoxBoatBorders(); + toolStripStatusLabelSpeed.setText("Скорость: " + _boat.Boat.Speed()); + toolStripStatusLabelWeight.setText("Вес: " + _boat.Boat.Weight()); + toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_boat.Boat.BodyColor().getRGB())); + drawningObjectBoat = new DrawningObjectBoat(_boat); + BufferedImage picture = _abstractMap.CreateMap(pictureBoxBoatWidth, pictureBoxBoatHeight, + drawningObjectBoat); + drawningObjectBoat.DrawingObject(picture.getGraphics()); + pictureLabel = new JLabel(new ImageIcon(picture)); + pictureBoxBoat.add(pictureLabel, BorderLayout.CENTER); + } + class ButtonsMove implements ActionListener { + public void actionPerformed(ActionEvent e) { + if (_abstractMap == null) return; + pictureBoxBoat.remove(pictureLabel); + pictureBoxBoat.revalidate(); + pictureBoxBoat.repaint(); + JButton temp = (JButton) e.getSource(); + String name = temp.getName(); + BufferedImage picture = _abstractMap.MoveObject(Direction.None); + switch (name) { + case "Up" -> picture=_abstractMap.MoveObject(Direction.Up); + case "Right" ->picture= _abstractMap.MoveObject(Direction.Right); + case "Left" -> picture=_abstractMap.MoveObject(Direction.Left); + case "Down" -> picture=_abstractMap.MoveObject(Direction.Down); + } + drawningObjectBoat.DrawingObject(picture.getGraphics()); + pictureLabel = new JLabel(new ImageIcon(picture)); + pictureBoxBoat.add(pictureLabel, BorderLayout.CENTER); + } + } + private void ChangePictureBoxBoatBorders() { + char[] temp = pictureBoxBoat.getSize().toString().toCharArray(); + for (int i = 0; i < temp.length; i++) { + if (!Character.isDigit(temp[i])) { + temp[i] = ' '; + } + } + String width = new String(temp); + String[] parameters = width.split("\\s*(\\s|,|!|\\.)\\s*", 4); + pictureBoxBoatWidth = Integer.parseInt(parameters[1]); + pictureBoxBoatHeight = Integer.parseInt(parameters[2]); + } + private int GetPaddleCount() { + if (radioButtonPaddle1.isSelected()) { + return 1; + } + else if (radioButtonPaddle2.isSelected()) { + return 2; + } else { + return 3; + } + } +} diff --git a/src/IDrawningObject.java b/src/IDrawningObject.java new file mode 100644 index 0000000..7f47ba9 --- /dev/null +++ b/src/IDrawningObject.java @@ -0,0 +1,9 @@ +import java.awt.*; + +public interface IDrawningObject { + public float Step(); + void SetObject(int x, int y, int width, int height); + void MoveObject(Direction direction); + void DrawingObject(Graphics g); + float[] GetCurrentPosition(); +} \ No newline at end of file diff --git a/src/IDrawningObjectPaddle.java b/src/IDrawningObjectPaddle.java new file mode 100644 index 0000000..c844b6a --- /dev/null +++ b/src/IDrawningObjectPaddle.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawningObjectPaddle { + void SetPaddlesCount(int paddleCount); + void DrawPaddles(Graphics gr, int _startPosPadX, int _startPosPadY, Color mainBrush); +} diff --git a/src/SeaMap.java b/src/SeaMap.java new file mode 100644 index 0000000..1b15a11 --- /dev/null +++ b/src/SeaMap.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class SeaMap extends SimpleMap { + private final Color barrierColor = Color.BLUE; + private final Color roadColor = Color.CYAN; + @Override + protected void GenerateMap() { + _map = new int[100][100]; + _size_x = (float) _width / _map.length; + _size_y = (float) _height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) { + for (int j = 0; j < _map[0].length; ++j) { + _map[i][j] = _freeRoad; + } + } + while (counter < 10) { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) { + _map[x][y] = _barrier; + counter++; + } + } + } + @Override + protected void DrawRoadPart(Graphics g, int i, int j) { + g.setColor(roadColor); + g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) { + g.setColor(barrierColor); + g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } +} \ No newline at end of file diff --git a/src/SimpleMap.java b/src/SimpleMap.java new file mode 100644 index 0000000..01617ad --- /dev/null +++ b/src/SimpleMap.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap { + private final Color barrierColor = Color.BLACK; + private final Color roadColor = Color.GRAY; + @Override + protected void GenerateMap() { + _map = new int[100][100]; + _size_x = (float) _width / _map.length; + _size_y = (float) _height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) { + for (int j = 0; j < _map[0].length; ++j) { + _map[i][j] = _freeRoad; + } + } + while (counter < 10) { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) { + _map[x][y] = _barrier; + counter++; + } + } + } + @Override + protected void DrawRoadPart(Graphics g, int i, int j) { + g.setColor(roadColor); + g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) { + g.setColor(barrierColor); + g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1))); + } +} \ No newline at end of file