Borschevskaya A.A. Lab Work 2 Hard #2

Merged
eegov merged 16 commits from lab2 into lab1 2022-11-07 10:48:13 +04:00
19 changed files with 907 additions and 38 deletions

View File

@ -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<String, Float> 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<String, Float> 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);
}

View File

@ -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[]{
Review

Требовалось создание объекта, а не массива объектов

Требовалось создание объекта, а не массива объектов
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<String, Float> GetCurrentPosition()
{
HashMap<String, Float> hashMap = new HashMap<String, Float>();
hashMap.put("Left", startPosX);
hashMap.put("Right", startPosX + carWidth);
hashMap.put("Top", startPosY);
hashMap.put("Bottom", startPosY + carHeight);
return hashMap;
}
}

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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<String, Float> GetCurrentPosition() {
return armoredCar.GetCurrentPosition();
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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;
}
}

View File

@ -22,7 +22,7 @@
<border type="none"/>
<children/>
</grid>
<grid id="7c72e" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="7c72e" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true"/>
@ -40,7 +40,7 @@
</component>
<component id="f4d6e" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Right.png"/>
@ -49,7 +49,7 @@
</component>
<component id="12974" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
@ -60,7 +60,7 @@
</component>
<component id="da478" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Down.png"/>
@ -69,7 +69,7 @@
</component>
<component id="43d8e" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Left.png"/>
@ -77,6 +77,14 @@
<visible value="true"/>
</properties>
</component>
<component id="64b34" class="javax.swing.JButton" binding="buttonCreate_Modif">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
</children>
</grid>
<component id="2cd31" class="javax.swing.JLabel" binding="labelSpeed">

View File

@ -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());
}
}

129
src/main/java/FormMap.form Normal file
View File

@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMap">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="564" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="7cc9f" binding="drawPanel" custom-create="true" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="1" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<background color="-1"/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="e4749" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="true"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="30a94" class="javax.swing.JButton" binding="buttonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="e6875" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Right.png"/>
<text value=""/>
</properties>
</component>
<component id="81a7c" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<hideActionText value="true"/>
<icon value="Up.png"/>
<text value=""/>
</properties>
</component>
<component id="87a25" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Down.png"/>
<text value=""/>
</properties>
</component>
<component id="fee79" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Left.png"/>
<text value=""/>
<visible value="true"/>
</properties>
</component>
<component id="2d428" class="javax.swing.JButton" binding="buttonCreate_Modif">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
</children>
</grid>
<component id="54172" class="javax.swing.JLabel" binding="labelSpeed">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="725a5" class="javax.swing.JLabel" binding="labelWeight">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="46832" class="javax.swing.JLabel" binding="labelColor">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="625" class="javax.swing.JComboBox" binding="mapSelectorComboBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Карта 1"/>
<item value="Карта 2"/>
</model>
</properties>
</component>
<hspacer id="9af30">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</form>

149
src/main/java/FormMap.java Normal file
View File

@ -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());
}
}

View File

@ -0,0 +1,7 @@
import java.awt.*;
public interface IDrawingCaterpillar {
void setNumRinks(int n);
void DrawCaterpillar(Graphics2D g2d, int startPosX, int startPosY);
}

View File

@ -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<String, Float> GetCurrentPosition();
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -1,5 +1,5 @@
public class Program {
public static void main(String[] args) {
new FormArmoredCar();
new FormMap();
}
}

View File

@ -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);
}
}