diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..ef4f9fb --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,103 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawingObject _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 Image createMap(int width, int height, IDrawingObject drawingObject) { + _width = width; + _height = height; + _drawingObject = drawingObject; + do { + generateMap(); + } while (!setObjectOnMap()); + return drawMapWithObject(); + } + + public Image moveObject(Direction direction) { + _drawingObject.moveObject(direction); + if (objectIntersects()) { + switch (direction) { + case Left -> _drawingObject.moveObject(Direction.Right); + case Right -> _drawingObject.moveObject(Direction.Left); + case Up -> _drawingObject.moveObject(Direction.Down); + case Down -> _drawingObject.moveObject(Direction.Up); + } + } + return drawMapWithObject(); + } + + private boolean setObjectOnMap() { + if (_drawingObject == null || _map == null) + { + return false; + } + + for (int i = 2; i < _map.length; i++) + { + for (int j = 2; j < _map[i].length; j++) + { + _drawingObject.setObject((int) (i * _size_x), (int) (j * _size_y), _width, _height); + if (!objectIntersects()) return true; + } + } + + return true; + } + + private boolean objectIntersects() { + float[] location = _drawingObject.getCurrentPosition(); + Rectangle self = new Rectangle((int) location[0], (int) location[2], (int) location[1] - (int) location[0], (int) location[3] - (int) location[2]); + for (int i = 0; i < _map.length; i++) + { + for (int j = 0; j < _map[i].length; j++) + { + if (_map[i][j] == _barrier) + { + if (self.intersects(new Rectangle(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y))) + { + return true; + } + } + } + } + + return false; + } + + private Image drawMapWithObject() { + Image img = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_ARGB); + if (_drawingObject == null || _map == null) { + return img; + } + Graphics2D g = (Graphics2D) img.getGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[i].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + drawRoadPart(g, i, j); + } else if (_map[i][j] == _barrier) + { + drawBarrierPart(g, i, j); + } + } + } + _drawingObject.drawingObject(g); + return img; + } + + protected abstract void generateMap(); + protected abstract void drawRoadPart(Graphics2D g, int i, int j); + protected abstract void drawBarrierPart(Graphics2D g, int i, int j); +} \ No newline at end of file diff --git a/DecksType.java b/DecksType.java new file mode 100644 index 0000000..b4e326c --- /dev/null +++ b/DecksType.java @@ -0,0 +1,17 @@ +import java.awt.*; +import java.util.Random; + +public enum DecksType { + Standard, + Round, + Triangular; + + public static IDrawingDecks random(int decksCount, Color bodyColor) { + return switch (new Random().nextInt(DecksType.values().length)) { + case 0 -> new DrawingDecks(decksCount, bodyColor); + case 1 -> new DrawingRoundDecks(decksCount, bodyColor); + case 2 -> new DrawingTriDecks(decksCount, bodyColor); + default -> null; + }; + } +} \ No newline at end of file diff --git a/DrawingDecks.java b/DrawingDecks.java index 96d1d02..8a14d20 100644 --- a/DrawingDecks.java +++ b/DrawingDecks.java @@ -1,15 +1,15 @@ import java.awt.*; -public class DrawingDecks { +public class DrawingDecks implements IDrawingDecks { private DecksCount deckCount; private Color color; - public void Init(int deckCount, Color bodyColor) { - setdecksCount(deckCount); + public DrawingDecks(int deckCount, Color bodyColor) { + setDecksCount(deckCount); color = bodyColor; } - public void setdecksCount(int num) { + public void setDecksCount(int num) { if (num <= 1) { deckCount = DecksCount.One; } else if (num >= 3) { diff --git a/DrawingMotorShip.java b/DrawingMotorShip.java new file mode 100644 index 0000000..2dcf528 --- /dev/null +++ b/DrawingMotorShip.java @@ -0,0 +1,24 @@ +import java.awt.*; + +public class DrawingMotorShip extends DrawingShip { + public DrawingMotorShip(int speed, float weight, Color bodyColor, int rollersCount, Color dopColor, boolean pipes, boolean fueltank) { + super(speed, weight, bodyColor, rollersCount, 80, 50); + ship = new EntityMotorShip(speed, weight, bodyColor, dopColor, pipes, fueltank); + } + + @Override + public void drawTransport(Graphics2D g) { + if (!(ship instanceof EntityMotorShip motorShip)) { + return; + } + + g.setColor(motorShip.getDopColor()); + if (motorShip.getPipes()) { + g.setStroke(new BasicStroke(8)); + } + g.setStroke(new BasicStroke(6)); + if (motorShip.getFuelTank()) { + } + super.drawTransport(g); + } +} \ No newline at end of file diff --git a/DrawingObjectShip.java b/DrawingObjectShip.java new file mode 100644 index 0000000..484256d --- /dev/null +++ b/DrawingObjectShip.java @@ -0,0 +1,37 @@ +import java.awt.*; + +public class DrawingObjectShip implements IDrawingObject { + private DrawingShip _ship = null; + + public DrawingObjectShip(DrawingShip ship) { + _ship = ship; + } + + public float getStep() { + if (_ship != null && _ship.ship != null) { + return _ship.ship.getStep(); + } + return 0; + } + + public float[] getCurrentPosition() { + if (_ship != null) { + return _ship.getCurrentPosition(); + } + return new float[] { 0, 0, 0, 0 }; + } + + public void moveObject(Direction direction) { + if (_ship != null) { + _ship.moveTransport(direction); + } + } + + public void setObject(int x, int y, int width, int height) { + _ship.SetPosition(x, y, width, height); + } + + public void drawingObject(Graphics2D g) { + _ship.drawTransport(g); + } +} \ No newline at end of file diff --git a/DrawingRoundDecks.java b/DrawingRoundDecks.java new file mode 100644 index 0000000..5d784c3 --- /dev/null +++ b/DrawingRoundDecks.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class DrawingRoundDecks implements IDrawingDecks { + private DecksCount decksCount; + private Color color; + + public DrawingRoundDecks(int decksCount, Color bodyColor) { + setDecksCount(decksCount); + color = bodyColor; + } + + public void setDecksCount(int num) { + if (num <= 1) { + decksCount = DecksCount.One; + } else if (num >= 3) { + decksCount = DecksCount.Three; + } + else { + decksCount = DecksCount.Two; + } + } + + public void draw(Graphics2D g, int x, int y, int shipWidth, int shipHeight) { + g.setColor(color != null ? color : Color.BLACK); + switch (decksCount) { + case Two: { + g.fillRect(x, y + 5, 15, 5); + g.fillPolygon(new int[] {x, x, x + 5}, new int[] {y + 5, y + 10, y + 10}, 3); + } + case Three: { + g.fillRect(x + shipWidth - 20, y, 20, 10); + g.fillPolygon(new int[] {x + shipWidth - 20, x + shipWidth - 20, x + shipWidth - 25}, new int[] {y, y + 10, y + 10}, 3); + } + } + } +} \ No newline at end of file diff --git a/DrawingShip.java b/DrawingShip.java index 5fc3804..884f5d8 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -1,24 +1,28 @@ import java.awt.*; public class DrawingShip { - private EntityShip ship; - private DrawingDecks drawingDecks; - private float _startPosX; - private float _startPosY; + protected EntityShip ship; + protected IDrawingDecks drawingDecks; + protected float _startPosX; + protected float _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; - private final int _shipWidth = 80; - private final int _shipHeight = 30; + protected int _shipWidth = 80; + protected int _shipHeight = 30; public EntityShip getShip() { return ship; } - public void Init(int speed, float weight, Color bodyColor, int decksCount) { - ship = new EntityShip(); - ship.Init(speed, weight, bodyColor); - drawingDecks = new DrawingDecks(); - drawingDecks.Init(decksCount, bodyColor); + public DrawingShip(int speed, float weight, Color bodyColor, int decksCount) { + ship = new EntityShip(speed, weight, bodyColor); + drawingDecks = DecksType.random(decksCount, bodyColor); + } + + protected DrawingShip(int speed, float weight, Color bodyColor, int decksCount, int shipWidth, int shipHeight) { + this(speed, weight, bodyColor, decksCount); + _shipWidth = shipWidth; + _shipHeight = shipHeight; } public void SetPosition(int x, int y, int width, int height) { @@ -108,4 +112,8 @@ public class DrawingShip { _startPosY = _pictureHeight - _shipHeight; } } + + public float[] getCurrentPosition() { + return new float[] { _startPosX, _startPosX + _shipWidth - 1, _startPosY, _startPosY + _shipHeight -1 }; + } } \ No newline at end of file diff --git a/DrawingTriDecks.java b/DrawingTriDecks.java new file mode 100644 index 0000000..132b5bc --- /dev/null +++ b/DrawingTriDecks.java @@ -0,0 +1,36 @@ +import java.awt.*; + +public class DrawingTriDecks implements IDrawingDecks { + private DecksCount decksCount; + private Color color; + + public DrawingTriDecks(int rollersCount, Color bodyColor) { + setDecksCount(rollersCount); + color = bodyColor; + } + + public void setDecksCount(int num) { + if (num <= 1) { + decksCount = DecksCount.One; + } else if (num >= 3) { + decksCount = DecksCount.Three; + } + else { + decksCount = DecksCount.Two; + } + } + + public void draw(Graphics2D g, int x, int y, int shipWidth, int shipHeight) { + g.setColor(color != null ? color : Color.BLACK); + switch (decksCount) { + case Two: { + g.fillRect(x, y + 5, 15, 5); + g.fillPolygon(new int[] {x, x, x + 5}, new int[] {y + 5, y + 10, y + 10}, 3); + } + case Three: { + g.fillRect(x + shipWidth - 20, y, 20, 10); + g.fillPolygon(new int[] {x + shipWidth - 20, x + shipWidth - 20, x + shipWidth - 25}, new int[] {y, y + 10, y + 10}, 3); + } + } + } +} \ No newline at end of file diff --git a/EntityMotorShip.java b/EntityMotorShip.java new file mode 100644 index 0000000..b973991 --- /dev/null +++ b/EntityMotorShip.java @@ -0,0 +1,26 @@ +import java.awt.*; + +public class EntityMotorShip extends EntityShip { + private Color dopColor; + private boolean pipes; + private boolean fueltank; + + public EntityMotorShip(int speed, float weight, Color bodyColor, Color dopColor, boolean pipes, boolean fueltank) { + super(speed, weight, bodyColor); + this.dopColor = dopColor; + this.pipes = pipes; + this.fueltank = fueltank; + } + + public Color getDopColor() { + return dopColor; + } + + public boolean getPipes() { + return pipes; + } + + public boolean getFuelTank() { + return fueltank; + } +} \ No newline at end of file diff --git a/EntityShip.java b/EntityShip.java index 51fecd8..e7b954c 100644 --- a/EntityShip.java +++ b/EntityShip.java @@ -6,7 +6,7 @@ public class EntityShip { private float weight; private Color bodyColor; - public void Init(int speed, float weight, Color bodyColor) { + public EntityShip(int speed, float weight, Color bodyColor) { Random rnd = new Random(); this.speed = speed <= 0 ? rnd.nextInt(100) + 50 : speed; this.weight = weight <= 0 ? rnd.nextInt(30) + 40 : weight; diff --git a/FormMap.form b/FormMap.form new file mode 100644 index 0000000..a89f94f --- /dev/null +++ b/FormMap.form @@ -0,0 +1,158 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..a593e1f --- /dev/null +++ b/FormMap.java @@ -0,0 +1,110 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.util.Random; + +public class FormMap extends JFrame { + private JPanel shipPane; + private JLabel speedLabel; + private JLabel weightLabel; + private JLabel colorLabel; + private JPanel pictureBox; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + private JButton createButton; + private JButton createAdvancedButton; + private JComboBox comboBoxSelectorMap; + + private AbstractMap _abstractMap; + private Image bufferedImage; + + public FormMap() { + this.setTitle("Artillery"); + this.setContentPane(shipPane); + + _abstractMap = new SimpleMap(); + + createButton.addActionListener(e -> { + Random rnd = new Random(); + var ship = new DrawingShip( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextInt(4, 6 + 1) + ); + setData(ship); + }); + buttonLeft.setForeground(new Color(0, 0, 0, 0)); + buttonRight.setForeground(new Color(0, 0, 0, 0)); + buttonUp.setForeground(new Color(0, 0, 0, 0)); + buttonDown.setForeground(new Color(0, 0, 0, 0)); + buttonLeft.addActionListener(e -> { + if (_abstractMap != null) { + bufferedImage = _abstractMap.moveObject(Direction.Left); + repaint(); + } + }); + buttonRight.addActionListener(e -> { + if (_abstractMap != null) { + bufferedImage = _abstractMap.moveObject(Direction.Right); + repaint(); + } + }); + buttonUp.addActionListener(e -> { + if (_abstractMap != null) { + bufferedImage = _abstractMap.moveObject(Direction.Up); + repaint(); + } + }); + buttonDown.addActionListener(e -> { + if (_abstractMap != null) { + bufferedImage = _abstractMap.moveObject(Direction.Down); + repaint(); + } + }); + createAdvancedButton.addActionListener(e -> { + Random rnd = new Random(); + var ship = new DrawingMotorShip( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextInt(4, 6 + 1), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), + rnd.nextBoolean() + ); + setData(ship); + }); + comboBoxSelectorMap.addItemListener(e -> { + if (e.getStateChange() == ItemEvent.SELECTED) { + switch (e.getItem().toString()) { + case "Простая карта" -> _abstractMap = new SimpleMap(); + case "Лесная карта" -> _abstractMap = new WaterMap(); + } + } + }); + } + + private void setData(DrawingShip ship) { + Random rnd = new Random(); + ship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight()); + speedLabel.setText(String.format("Скорость: %d", ship.ship.getSpeed())); + weightLabel.setText(String.format("Вес: %f", ship.ship.getWeight())); + colorLabel.setText(String.format("Цвет: %x", ship.getShip().getBodyColor().getRGB())); + bufferedImage = _abstractMap.createMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawingObjectShip(ship)); + repaint(); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + + if (bufferedImage != null) { + pictureBox.paintComponents(bufferedImage.getGraphics()); + pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null); + } + } +} \ No newline at end of file diff --git a/FormShip.form b/FormShip.form index b20580f..675ccc4 100644 --- a/FormShip.form +++ b/FormShip.form @@ -53,7 +53,7 @@ - + @@ -61,11 +61,6 @@ - - - - - @@ -73,12 +68,12 @@ - + - + @@ -93,7 +88,7 @@ - + @@ -107,7 +102,7 @@ - + @@ -122,7 +117,7 @@ - + @@ -137,7 +132,7 @@ - + @@ -145,6 +140,19 @@ + + + + + + + + + + + + + diff --git a/FormShip.java b/FormShip.java index 8a06582..54dbc3e 100644 --- a/FormShip.java +++ b/FormShip.java @@ -15,6 +15,7 @@ public class FormShip extends JFrame { private JButton buttonDown; private JButton buttonLeft; private JButton buttonRight; + private JButton createAdvancedButton; private DrawingShip _ship; @@ -23,15 +24,14 @@ public class FormShip extends JFrame { this.setContentPane(shipPane); createButton.addActionListener(e -> { Random rnd = new Random(); - _ship = new DrawingShip(); - _ship.Init( - rnd.nextInt(200) + 100, - rnd.nextInt(1000) + 1000, + _ship = new DrawingShip( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), new Color( - rnd.nextInt(256), - rnd.nextInt(256), - rnd.nextInt(256)), - rnd.nextInt(3) + 1 + rnd.nextInt(0, 256), + rnd.nextInt(0, 256), + rnd.nextInt(0, 256)), + rnd.nextInt(4, 7) ); _ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); speedLabel.setText(String.format("Speed: %s", _ship.getShip().getSpeed())); @@ -62,12 +62,35 @@ public class FormShip extends JFrame { if (_ship != null) _ship.moveTransport(Direction.Down); repaint(); }); + createAdvancedButton.addActionListener(e -> { + Random rnd = new Random(); + _ship = new DrawingMotorShip( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color( + rnd.nextInt(0, 256), + rnd.nextInt(0, 256), + rnd.nextInt(0, 256)), + rnd.nextInt(4, 7), + new Color( + rnd.nextInt(0, 256), + rnd.nextInt(0, 256), + rnd.nextInt(0, 256)), + rnd.nextBoolean(), + rnd.nextBoolean() + ); + _ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); + speedLabel.setText(String.format("Скорость: %s", _ship.getShip().getSpeed())); + weightLabel.setText(String.format("Вес: %s", _ship.getShip().getWeight())); + colorLabel.setText(String.format("Цвет: %x", _ship.getShip().getBodyColor().getRGB())); + repaint(); + }); } @Override public void paint(Graphics g) { super.paint(g); - Graphics2D g2d = (Graphics2D) shipPane.getGraphics(); + Graphics2D g2d = (Graphics2D) pictureBox.getGraphics(); if (_ship != null) { _ship.drawTransport(g2d); } diff --git a/IDrawingDecks.java b/IDrawingDecks.java new file mode 100644 index 0000000..28f82f0 --- /dev/null +++ b/IDrawingDecks.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawingDecks { + void setDecksCount(int num); + void draw(Graphics2D g, int x, int y, int shipWidth, int shipHeight); +} \ No newline at end of file diff --git a/IDrawingObject.java b/IDrawingObject.java new file mode 100644 index 0000000..0977fc0 --- /dev/null +++ b/IDrawingObject.java @@ -0,0 +1,9 @@ +import java.awt.*; + +public interface IDrawingObject { + float getStep(); + void setObject(int x, int y, int width, int height); + void moveObject(Direction direction); + void drawingObject(Graphics2D g); + float[] getCurrentPosition(); +} \ No newline at end of file diff --git a/Program.java b/Program.java index 5f19d46..e5b9eb9 100644 --- a/Program.java +++ b/Program.java @@ -2,7 +2,7 @@ import javax.swing.*; public class Program { public static void main(String[] args) { - FormShip form = new FormShip(); + FormMap form = new FormMap(); form.setSize(640, 480); form.setVisible(true); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..6057544 --- /dev/null +++ b/SimpleMap.java @@ -0,0 +1,40 @@ +import java.awt.*; +import java.util.Arrays; + +public class SimpleMap extends AbstractMap { + private final Color barrierColor = Color.black; + private final Color roadColor = Color.gray; + + @Override + protected void drawBarrierPart(Graphics2D g, int i, int j) { + g.setColor(barrierColor); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); + } + + @Override + protected void drawRoadPart(Graphics2D g, int i, int j) { + g.setColor(roadColor); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); + } + + @Override + protected void generateMap() { + _map = new int[100][100]; + _size_x = (float)_width / _map[0].length; + _size_y = (float)_height / _map.length; + int counter = 0; + for (int[] row : _map) { + Arrays.fill(row, _freeRoad); + } + while (counter < 50) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[y][x] == _freeRoad) + { + _map[y][x] = _barrier; + counter++; + } + } + } +} \ No newline at end of file diff --git a/WaterMap.java b/WaterMap.java new file mode 100644 index 0000000..1947d49 --- /dev/null +++ b/WaterMap.java @@ -0,0 +1,36 @@ +import java.awt.*; +import java.util.Arrays; + +public class WaterMap extends AbstractMap { + private final Color barrierColor = Color.green; + private final Color roadColor = new Color(165, 42, 42, 255); + + @Override + protected void drawBarrierPart(Graphics2D g, int i, int j) + { + g.setColor(barrierColor); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); + } + + @Override + protected void drawRoadPart(Graphics2D g, int i, int j) + { + g.setColor(roadColor); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); + } + + @Override + protected void generateMap() { + _map = new int[50][50]; + _size_x = (float)_width / _map[0].length; + _size_y = (float)_height / _map.length; + int counter = 0; + for (int[] row : _map) { + Arrays.fill(row, _freeRoad); + } + while (counter < 10) + { + + } + } +} \ No newline at end of file