diff --git a/src/main/java/AbstractMap.java b/src/main/java/AbstractMap.java new file mode 100644 index 0000000..773a30e --- /dev/null +++ b/src/main/java/AbstractMap.java @@ -0,0 +1,188 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.HashMap; +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) + { + this.width = width; + this.height = height; + this.drawingObject = drawingObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + public Image MoveObject(Direction direction) + { + if (drawingObject == null) + return null; + boolean flag = true; + float step = drawingObject.getStep(); + + HashMap hashMap = drawingObject.GetCurrentPosition(); + float left = hashMap.get("Left"); + float right = hashMap.get("Right"); + float top = hashMap.get("Top"); + float bottom = hashMap.get("Bottom"); + + int x1_obj_next = (int)((left - step) / size_x); + int y1_obj_next = (int)((top - step) / size_y); + int x2_obj_next = (int)((right + step) / size_x); + int y2_obj_next = (int)((bottom + step) / size_y); + + int x1_obj_current = (int)(left / size_x); + int y1_obj_current = (int)(top / size_y); + int x2_obj_current = (int)(right / size_x); + int y2_obj_current = (int)(bottom / size_y); + + switch (direction) + { + case Left: + { + if (x1_obj_next < 0) + flag = false; + else + { + for (int i = x1_obj_next; i <= x1_obj_current; i++) + { + for (int j = y1_obj_current; j <= y2_obj_current; j++) + { + if (map[i][j] == _barrier) + flag = false; + } + } + } + break; + } + case Right: + { + if (x2_obj_next >= map.length) + flag = false; + else + { + for (int i = x2_obj_current; i <= x2_obj_next; i++) + { + for (int j = y1_obj_current; j <= y2_obj_current; j++) + { + if (map[i][j] == _barrier) + flag = false; + } + } + } + break; + } + case Up: + { + if (y1_obj_next < 0) + flag = false; + else + { + for (int i = x1_obj_current; i <= x2_obj_current; i++) + { + for (int j = y1_obj_next; j <= y1_obj_current; j++) + { + if (map[i][j] == _barrier) + flag = false; + } + } + } + break; + } + case Down: + { + if (y2_obj_next >= map.length) + flag = false; + else + { + for (int i = x1_obj_current; i <= x2_obj_current; i++) + { + for (int j = y2_obj_current; j <= y2_obj_next; j++) + { + if (map[i][j] == _barrier) + flag = false; + } + } + } + break; + } + } + + if (flag) + { + drawingObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + 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); + + HashMap hashMap = drawingObject.GetCurrentPosition(); + float left = hashMap.get("Left"); + float right = hashMap.get("Right"); + float top = hashMap.get("Top"); + float bottom = hashMap.get("Bottom"); + + for (int i = (int)(x / size_x); i <= (int) (right / size_x); i++) + { + for (int j = (int)(y / size_y); j <= (int) (bottom / size_y); j++) + { + if (map[i][j] == _barrier) + { + return false; + } + } + } + return true; + } + private Image DrawMapWithObject() + { + BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); + if (drawingObject == null || map == null) + return img; + + Graphics2D gr = img.createGraphics(); + + 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); + } + } + } + + drawingObject.DrawingObject(gr); + 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); +} diff --git a/src/main/java/DrawingArmoredCar.java b/src/main/java/DrawingArmoredCar.java index b4fa674..70b39aa 100644 --- a/src/main/java/DrawingArmoredCar.java +++ b/src/main/java/DrawingArmoredCar.java @@ -1,33 +1,44 @@ import java.awt.*; +import java.util.HashMap; import java.util.Random; public class DrawingArmoredCar { - private EntityArmoredCar armoredCar; + protected EntityArmoredCar armoredCar; - private float startPosX; + protected float startPosX; - private float startPosY; + protected float startPosY; private int pictureWidth; private int pictureHeight; - private static final int carWidth = 80; + private int carWidth = 80; - private static final int carHeight = 50; + private int carHeight = 50; - private DrawingCaterpillar drawingCaterpillar; + protected IDrawingCaterpillar drawingCaterpillar; public EntityArmoredCar getArmoredCar() { return armoredCar; } - public void Init(int speed, float weight, Color bodyColor) { - this.armoredCar = new EntityArmoredCar(); - this.armoredCar.init(speed, weight, bodyColor); + public DrawingArmoredCar(int speed, float weight, Color bodyColor) { + this.armoredCar = new EntityArmoredCar(speed, weight, bodyColor); + IDrawingCaterpillar[] realisations = new IDrawingCaterpillar[]{ + new DrawingCaterpillar(bodyColor), + new DrawingCrossCaterpillar(bodyColor), + new DrawingDoubleCaterpillar(bodyColor)}; Random r = new Random(); - this.drawingCaterpillar = new DrawingCaterpillar(); - this.drawingCaterpillar.Init(r.nextInt(4, 7), bodyColor); + int num = r.nextInt(4, 7); + this.drawingCaterpillar = realisations[r.nextInt(3)]; + drawingCaterpillar.setNumRinks(num); + } + + protected DrawingArmoredCar(int speed, float weight, Color bodyColor, int carWidth, int carHeight) { + this(speed, weight, bodyColor); + this.carWidth = carWidth; + this.carHeight = carHeight; } public void SetPosition(int x, int y, int width, int height) { @@ -84,17 +95,19 @@ public class DrawingArmoredCar { { return; } - // отрисовка корпуса и гусеницы g2d.setPaint(armoredCar.getBodyColor()); - g2d.fillRect((int ) startPosX + 20, (int) startPosY, 40, 20); + // отрисовка корпуса и гусеницы + int new_startPosX = Math.round(startPosX); + int new_startPosY = Math.round(startPosY); + g2d.fillRect(new_startPosX + 20, new_startPosY, 40, 20); g2d.setPaint(Color.LIGHT_GRAY); - g2d.fillRect((int ) startPosX, (int ) startPosY + 20, 80, 20); + g2d.fillRect(new_startPosX, new_startPosY + 20, 80, 20); - g2d.fillOval((int ) startPosX, (int ) startPosY + 30, 20, 20); - g2d.fillOval((int ) startPosX + 80 - 20, (int ) startPosY + 30, 20, 20); - g2d.fillRect((int ) startPosX + 15, (int ) startPosY + 20, 60, 30); + g2d.fillOval(new_startPosX, new_startPosY + 30, 20, 20); + g2d.fillOval(new_startPosX + 80 - 20, new_startPosY + 30, 20, 20); + g2d.fillRect(new_startPosX + 15, new_startPosY + 20, 60, 30); // отрисовка катков в гусенице - drawingCaterpillar.DrawCaterpillar(g2d, (int)startPosX, (int)startPosY); + drawingCaterpillar.DrawCaterpillar(g2d, new_startPosX, new_startPosY); } public void ChangeBorders(int width, int height) @@ -116,4 +129,14 @@ public class DrawingArmoredCar { startPosY = pictureHeight - carHeight; } } + + public HashMap GetCurrentPosition() + { + HashMap hashMap = new HashMap(); + hashMap.put("Left", startPosX); + hashMap.put("Right", startPosX + carWidth); + hashMap.put("Top", startPosY); + hashMap.put("Bottom", startPosY + carHeight); + return hashMap; + } } diff --git a/src/main/java/DrawingCaterpillar.java b/src/main/java/DrawingCaterpillar.java index ae2fb0c..6cf873a 100644 --- a/src/main/java/DrawingCaterpillar.java +++ b/src/main/java/DrawingCaterpillar.java @@ -1,14 +1,13 @@ import java.awt.*; -public class DrawingCaterpillar { +public class DrawingCaterpillar implements IDrawingCaterpillar { private NumRinks numRinks = NumRinks.Four; private Color color; - public void Init(int n, Color color) { - setNumRinks(n); + public DrawingCaterpillar(Color color) { this.color = color; } - + @Override public void setNumRinks(int n) { switch (n) { case 4 -> numRinks = NumRinks.Four; @@ -22,7 +21,7 @@ public class DrawingCaterpillar { } } } - + @Override public void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY) { color = color != null ? color : Color.YELLOW; diff --git a/src/main/java/DrawingCrossCaterpillar.java b/src/main/java/DrawingCrossCaterpillar.java new file mode 100644 index 0000000..98d7238 --- /dev/null +++ b/src/main/java/DrawingCrossCaterpillar.java @@ -0,0 +1,42 @@ +import java.awt.*; + +public class DrawingCrossCaterpillar implements IDrawingCaterpillar{ + private NumRinks numRinks = NumRinks.Four; + private Color color; + + public DrawingCrossCaterpillar(Color color) { + this.color = color; + } + + @Override + public void setNumRinks(int n) { + switch (n) { + case 4 -> numRinks = NumRinks.Four; + + case 5 -> numRinks = NumRinks.Five; + + case 6 -> numRinks = NumRinks.Six; + + default -> { + break; + } + } + } + + @Override + public void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY) { + color = color != null ? color : Color.YELLOW; + + int size = numRinks == NumRinks.Four ? 15 : 10; + int dist = numRinks == NumRinks.Four ? 20 : 13; + startPosX = numRinks == NumRinks.Five ? startPosX + 5 : startPosX; + for (int i = 0; i < numRinks.val(); i++) { + g2d.setPaint(color); + g2d.fillOval(startPosX + dist * i, startPosY + 30, size, size); + g2d.setColor(Color.BLACK); + g2d.setStroke(new BasicStroke(3)); + g2d.drawLine(startPosX + dist * i, startPosY + 30, startPosX + dist * i + size, startPosY + 30 + size); + g2d.drawLine(startPosX + dist * i + size, startPosY + 30, startPosX + dist * i, startPosY + 30 + size); + } + } +} diff --git a/src/main/java/DrawingDoubleCaterpillar.java b/src/main/java/DrawingDoubleCaterpillar.java new file mode 100644 index 0000000..ed7da51 --- /dev/null +++ b/src/main/java/DrawingDoubleCaterpillar.java @@ -0,0 +1,39 @@ +import java.awt.*; + +public class DrawingDoubleCaterpillar implements IDrawingCaterpillar{ + private NumRinks numRinks = NumRinks.Four; + private Color color; + + public DrawingDoubleCaterpillar(Color color) { + this.color = color; + } + + @Override + public void setNumRinks(int n) { + switch (n) { + case 4 -> numRinks = NumRinks.Four; + + case 5 -> numRinks = NumRinks.Five; + + case 6 -> numRinks = NumRinks.Six; + + default -> { + break; + } + } + } + + @Override + public void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY) { + color = color != null ? color : Color.YELLOW; + int size = numRinks == NumRinks.Four ? 15 : 10; + int dist = numRinks == NumRinks.Four ? 20 : 13; + startPosX = numRinks == NumRinks.Five ? startPosX + 5 : startPosX; + for (int i = 0; i < numRinks.val(); i++) { + g2d.setPaint(color); + g2d.fillOval(startPosX + dist * i, startPosY + 30, size, size); + g2d.setPaint(Color.WHITE); + g2d.fillOval(startPosX + dist * i + size/4, startPosY + 30 + size/4, size/2, size/2); + } + } +} diff --git a/src/main/java/DrawingObjectArmoredCar.java b/src/main/java/DrawingObjectArmoredCar.java new file mode 100644 index 0000000..6868e1e --- /dev/null +++ b/src/main/java/DrawingObjectArmoredCar.java @@ -0,0 +1,40 @@ +import java.awt.*; +import java.util.HashMap; + +public class DrawingObjectArmoredCar implements IDrawingObject{ + private DrawingArmoredCar armoredCar = null; + + public DrawingObjectArmoredCar(DrawingArmoredCar armoredCar) { + this.armoredCar = armoredCar; + } + + @Override + public float getStep() { + if (armoredCar != null) + return armoredCar.armoredCar.step; + return 0; + } + + @Override + public void SetObject(int x, int y, int width, int height) { + if (armoredCar != null) + armoredCar.SetPosition(x, y, width, height); + } + + @Override + public void MoveObject(Direction direction) { + if (armoredCar != null) + armoredCar.MoveTransport(direction); + } + + @Override + public void DrawingObject(Graphics2D g) { + if (armoredCar != null) + armoredCar.DrawTransport(g); + } + + @Override + public HashMap GetCurrentPosition() { + return armoredCar.GetCurrentPosition(); + } +} diff --git a/src/main/java/DrawingTank.java b/src/main/java/DrawingTank.java new file mode 100644 index 0000000..225a19e --- /dev/null +++ b/src/main/java/DrawingTank.java @@ -0,0 +1,30 @@ +import java.awt.*; + +public class DrawingTank extends DrawingArmoredCar{ + public DrawingTank(int speed, float weight, Color bodyColor, Color dopColor, + boolean towerWeapon, boolean AMachineGun) { + super(speed, weight, bodyColor, 80, 60); + armoredCar = new EntityTank(speed, weight, bodyColor, dopColor, towerWeapon, AMachineGun); + } + @Override + public void DrawTransport(Graphics2D g) { + if (!(armoredCar instanceof EntityTank tank)) { + return; + } + + if (tank.isTowerWeapon()) { + g.setColor(tank.getDopColor()); + g.fillRect((int)startPosX + 40, (int)startPosY, 5, 20); + g.fillRect((int)startPosX + 40, (int)startPosY, 40, 5); + g.fillOval((int)startPosX + 27, (int)startPosY + 7, 30, 10); + } + if (tank.isAMachineGun()) { + g.setColor(tank.getDopColor()); + g.fillRect((int)startPosX + 60, (int)startPosY + 15, 7, 10); + g.fillRect((int)startPosX + 60, (int)startPosY + 17, 20, 5); + } + startPosY += 5; + super.DrawTransport(g); + startPosY -= 5; + } +} diff --git a/src/main/java/EntityArmoredCar.java b/src/main/java/EntityArmoredCar.java index f1d54e5..4ad1d9f 100644 --- a/src/main/java/EntityArmoredCar.java +++ b/src/main/java/EntityArmoredCar.java @@ -7,7 +7,7 @@ public class EntityArmoredCar { private Color bodyColor; public float step; - public void init(int speed, float weight, Color bodyColor) { + public EntityArmoredCar(int speed, float weight, Color bodyColor) { this.speed = speed; this.weight = weight; this.bodyColor = bodyColor; diff --git a/src/main/java/EntityTank.java b/src/main/java/EntityTank.java new file mode 100644 index 0000000..e8b0211 --- /dev/null +++ b/src/main/java/EntityTank.java @@ -0,0 +1,26 @@ +import java.awt.*; + +public class EntityTank extends EntityArmoredCar{ + private Color dopColor; + private boolean towerWeapon; + private boolean AMachineGun; + + public Color getDopColor() { + return dopColor; + } + + public boolean isTowerWeapon() { + return towerWeapon; + } + + public boolean isAMachineGun() { + return AMachineGun; + } + + public EntityTank(int speed, float weight, Color bodyColor, Color dopColor, boolean towerWeapon, boolean AMachineGun) { + super(speed, weight, bodyColor); + this.dopColor = dopColor; + this.towerWeapon = towerWeapon; + this.AMachineGun = AMachineGun; + } +} diff --git a/src/main/java/FormArmoredCar.form b/src/main/java/FormArmoredCar.form index e82df6d..5cbc8f9 100644 --- a/src/main/java/FormArmoredCar.form +++ b/src/main/java/FormArmoredCar.form @@ -22,7 +22,7 @@ - + @@ -40,7 +40,7 @@ - + @@ -49,7 +49,7 @@ - + @@ -60,7 +60,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -77,6 +77,14 @@ + + + + + + + + diff --git a/src/main/java/FormArmoredCar.java b/src/main/java/FormArmoredCar.java index 045cdaa..cfea465 100644 --- a/src/main/java/FormArmoredCar.java +++ b/src/main/java/FormArmoredCar.java @@ -17,6 +17,7 @@ public class FormArmoredCar extends JFrame{ private JLabel labelSpeed; private JLabel labelWeight; private JLabel labelColor; + private JButton buttonCreate_Modif; private DrawingArmoredCar armoredCar; @@ -42,16 +43,10 @@ public class FormArmoredCar extends JFrame{ @Override public void actionPerformed(ActionEvent actionEvent) { Random rnd = new Random(); - armoredCar = new DrawingArmoredCar(); - armoredCar.Init(rnd.nextInt(100,300), rnd.nextInt(1000, 2000), + armoredCar = new DrawingArmoredCar(rnd.nextInt(100,300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); - armoredCar.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), - drawPanel.getWidth(), drawPanel.getHeight()); - - labelSpeed.setText("Скорость: " + armoredCar.getArmoredCar().getSpeed()); - labelWeight.setText("Вес: " + armoredCar.getArmoredCar().getWeight()); - labelColor.setText("Цвет: " + armoredCar.getArmoredCar().getBodyColor().getRGB()); + setData(); } }); @@ -94,6 +89,20 @@ public class FormArmoredCar extends JFrame{ setContentPane(mainPanel); setVisible(true); + buttonCreate_Modif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + armoredCar = new DrawingTank(rnd.nextInt(100, 300), rnd.nextInt(1000, + 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, + 256)), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, + 256)), + 1==rnd.nextInt(0, 2), 1==rnd.nextInt(0, 2)); + setData(); + } + }); } private void createUIComponents() { @@ -109,4 +118,13 @@ public class FormArmoredCar extends JFrame{ } }; } + + private void setData() + { + Random rnd = new Random(); + armoredCar.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), drawPanel.getWidth(), drawPanel.getHeight()); + labelSpeed.setText("Скорость: " + armoredCar.getArmoredCar().getSpeed()); + labelWeight.setText("Вес: " + armoredCar.getArmoredCar().getWeight()); + labelColor.setText("Цвет: " + armoredCar.getArmoredCar().getBodyColor().getRGB()); + } } \ No newline at end of file diff --git a/src/main/java/FormMap.form b/src/main/java/FormMap.form new file mode 100644 index 0000000..5dcacd8 --- /dev/null +++ b/src/main/java/FormMap.form @@ -0,0 +1,129 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/FormMap.java b/src/main/java/FormMap.java new file mode 100644 index 0000000..8d70e44 --- /dev/null +++ b/src/main/java/FormMap.java @@ -0,0 +1,149 @@ +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.awt.image.BufferedImage; +import java.util.Objects; +import java.util.Random; + +public class FormMap extends JFrame { + private JPanel drawPanel; + private JButton buttonCreate; + private JButton buttonRight; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonCreate_Modif; + private JLabel labelSpeed; + private JLabel labelWeight; + private JLabel labelColor; + private JComboBox mapSelectorComboBox; + private JPanel mainPanel; + + private AbstractMap abstractMap; + private Image img; + public FormMap() { + super("Карта"); + abstractMap = new SimpleMap(); + + setBounds(100, 100, 700, 700); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + labelSpeed.setText("Скорость: "); + labelWeight.setText("Вес: "); + labelColor.setText("Цвет: "); + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + Random rnd = new Random(); + DrawingArmoredCar armoredCar = new DrawingArmoredCar(rnd.nextInt(100,300), rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + setData(armoredCar); + } + }); + + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (abstractMap != null) { + img = abstractMap.MoveObject(Direction.Up); + drawPanel.repaint(); + } + } + }); + + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (abstractMap != null) { + img = abstractMap.MoveObject(Direction.Down); + drawPanel.repaint(); + } + } + }); + + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (abstractMap != null) { + img = abstractMap.MoveObject(Direction.Left); + drawPanel.repaint(); + } + } + }); + + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (abstractMap != null) { + img = abstractMap.MoveObject(Direction.Right); + drawPanel.repaint(); + } + } + }); + + setContentPane(mainPanel); + setVisible(true); + buttonCreate_Modif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random rnd = new Random(); + DrawingTank tank = new DrawingTank(rnd.nextInt(100, 300), rnd.nextInt(1000, + 2000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, + 256)), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, + 256)), + 1==rnd.nextInt(0, 2), 1==rnd.nextInt(0, 2)); + setData(tank); + } + }); + mapSelectorComboBox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String name = (String) mapSelectorComboBox.getSelectedItem(); + switch (name) + { + case "Простая карта": + abstractMap = new SimpleMap(); + break; + case "Карта 1": + abstractMap = new MyMapWooden(); + break; + case "Карта 2": + abstractMap = new MyMapLabyrinth(); + break; + } + } + }); + } + + private void createUIComponents() { + drawPanel = new JPanel() { + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(img, 0, 0, this); + super.repaint(); + } + }; + } + + private void setData(DrawingArmoredCar drawingArmoredCar) + { + if (abstractMap != null) + img = abstractMap.CreateMap(drawPanel.getWidth(), drawPanel.getHeight(), new DrawingObjectArmoredCar(drawingArmoredCar)); + + labelSpeed.setText("Скорость: " + drawingArmoredCar.getArmoredCar().getSpeed()); + labelWeight.setText("Вес: " + drawingArmoredCar.getArmoredCar().getWeight()); + labelColor.setText("Цвет: " + drawingArmoredCar.getArmoredCar().getBodyColor().getRGB()); + } +} + + + + + diff --git a/src/main/java/IDrawingCaterpillar.java b/src/main/java/IDrawingCaterpillar.java new file mode 100644 index 0000000..50bda28 --- /dev/null +++ b/src/main/java/IDrawingCaterpillar.java @@ -0,0 +1,7 @@ +import java.awt.*; + +public interface IDrawingCaterpillar { + void setNumRinks(int n); + + void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY); +} diff --git a/src/main/java/IDrawingObject.java b/src/main/java/IDrawingObject.java new file mode 100644 index 0000000..cf65865 --- /dev/null +++ b/src/main/java/IDrawingObject.java @@ -0,0 +1,14 @@ +import java.awt.*; +import java.util.HashMap; + +public interface IDrawingObject { + float getStep(); + + void SetObject(int x, int y, int width, int height); + + void MoveObject(Direction direction); + + void DrawingObject(Graphics2D g); + + HashMap GetCurrentPosition(); +} diff --git a/src/main/java/MyMapLabyrinth.java b/src/main/java/MyMapLabyrinth.java new file mode 100644 index 0000000..a832924 --- /dev/null +++ b/src/main/java/MyMapLabyrinth.java @@ -0,0 +1,65 @@ +import java.awt.*; + +public class MyMapLabyrinth extends AbstractMap{ + private final Color barrierColor = Color.BLUE; + private final Color roadColor = Color.BLACK; + @Override + protected void GenerateMap() { + map = new int[50][50]; + 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 < 20) + { + int x = random.nextInt(0, 50); + int y = random.nextInt(0, 50); + int number = random.nextInt(1, 16); + if (counter < 5) { + if (number + x > map.length) + { + number = map.length - x - 1; + } + for (int i = x; i < x + number; ++i) + { + map[i][y] = _barrier; + } + } else + { + if (number + y > map[0].length) + { + number = map[0].length - y - 1; + } + for (int i = y; i < y + number; ++i) + { + map[x][i] = _barrier; + } + } + counter++; + + } + } + + @Override + protected void DrawRoadPart(Graphics2D g, int i, int j) { + g.setColor(roadColor); + int new_size_x = Math.round(size_x); + int new_size_y = Math.round(size_y); + g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y); + } + + @Override + protected void DrawBarrierPart(Graphics2D g, int i, int j) { + g.setColor(barrierColor); + int new_size_x = Math.round(size_x); + int new_size_y = Math.round(size_y); + g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y); + } +} diff --git a/src/main/java/MyMapWooden.java b/src/main/java/MyMapWooden.java new file mode 100644 index 0000000..224ad4d --- /dev/null +++ b/src/main/java/MyMapWooden.java @@ -0,0 +1,46 @@ +import java.awt.*; + +public class MyMapWooden extends AbstractMap{ + private final Color barrierColor = new Color(45, 77, 44); + private final Color roadColor = new Color(105, 70, 35); + @Override + protected void GenerateMap() { + map = new int[10][10]; + 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(10); + int y = random.nextInt(10); + if (map[x][y] == _freeRoad) + { + map[x][y] = _barrier; + counter++; + } + } + } + + @Override + protected void DrawRoadPart(Graphics2D g, int i, int j) { + g.setColor(roadColor); + int new_size_x = Math.round(size_x); + int new_size_y = Math.round(size_y); + g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y); + } + + @Override + protected void DrawBarrierPart(Graphics2D g, int i, int j) { + g.setColor(barrierColor); + int new_size_x = Math.round(size_x); + int new_size_y = Math.round(size_y); + g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y); + } +} diff --git a/src/main/java/Program.java b/src/main/java/Program.java index 2d1f91a..ff7ede6 100644 --- a/src/main/java/Program.java +++ b/src/main/java/Program.java @@ -1,5 +1,5 @@ public class Program { public static void main(String[] args) { - new FormArmoredCar(); + new FormMap(); } } diff --git a/src/main/java/SimpleMap.java b/src/main/java/SimpleMap.java new file mode 100644 index 0000000..0cc26ec --- /dev/null +++ b/src/main/java/SimpleMap.java @@ -0,0 +1,46 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap{ + private final Color barrierColor = Color.BLACK; + private final Color roadColor = Color.DARK_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 < 50) + { + int x = random.nextInt(100); + int y = random.nextInt(100); + if (map[x][y] == _freeRoad) + { + map[x][y] = _barrier; + counter++; + } + } + } + + @Override + protected void DrawRoadPart(Graphics2D g, int i, int j) { + g.setColor(roadColor); + int new_size_x = Math.round(size_x); + int new_size_y = Math.round(size_y); + g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y); + } + + @Override + protected void DrawBarrierPart(Graphics2D g, int i, int j) { + g.setColor(barrierColor); + int new_size_x = Math.round(size_x); + int new_size_y = Math.round(size_y); + g.fillRect(i * new_size_x, j * new_size_y, new_size_x, new_size_y); + } +}