Kharlamov A.A. Lab Work 02 Hard #2

Closed
Kharlamov wants to merge 4 commits from Lab02 into Lab01
11 changed files with 676 additions and 0 deletions
Showing only changes of commit 8dc6882d21 - Show all commits

158
AbstractMap.java Normal file
View File

@ -0,0 +1,158 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.util.Random;
public abstract class AbstractMap {
private IDrawningObject _drawningObject = 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 drawningObject){
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public BufferedImage MoveObject(Direction direction)
{
int _objWidth = (int)(_drawningObject.GetCurrentPosition()[1] / _size_x);
int _startX = (int)(_drawningObject.GetCurrentPosition()[0] / _size_x);
int _startY = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y);
int _objHeight = (int)(_drawningObject.GetCurrentPosition()[3] / _size_y);
boolean isMove = true;
switch (direction)
{
case LEFT:
for (int i = _startX; i >= Math.abs(_startX - (int)(_drawningObject.getStep() / _size_x)); i--)
{
for (int j = _startY; j <= _objHeight && j<_map.length; j++)
{
if (_map[i][j] == _barrier)
{
isMove = false;
break;
}
}
}
break;
case RIGHT:
for (int i = _objWidth; i <=_objWidth + (int)(_drawningObject.getStep() / _size_x); i++)
{
for (int j = _startY; j <= _objHeight && j<_map.length; j++)
{
if (_map[i][j] == _barrier)
{
isMove = false;
break;
}
}
}
break;
case DOWN:
for (int i = _startX; i <= _objWidth && i < _map[0].length; i++)
{
for (int j = _objHeight; j <= _objHeight + (int)(_drawningObject.getStep() / _size_y) && j<_map.length; j++)
{
if (_map[i][j] == _barrier)
{
isMove = false;
break;
}
}
}
break;
case UP:
for (int i = _startX; i <= _objWidth && i < _map[0].length; i++)
{
for (int j = _startY; j >= Math.abs(_startY - (int)(_drawningObject.getStep() / _size_y)) ; j--)
{
if (_map[i][j] == _barrier)
{
isMove = false;
break;
}
}
}
break;
}
if (isMove)
{
_drawningObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private boolean SetObjectOnMap()
{
if (_drawningObject == null || _map == null)
{
return false;
}
int x = _random.nextInt(10, 15);
int y = _random.nextInt(10, 15);
_drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[0].length; ++j)
{
if (i * _size_x >= x && j * _size_y >= y &&
i * _size_x <= x + _drawningObject.GetCurrentPosition()[1] &&
j * _size_y <= y + _drawningObject.GetCurrentPosition()[3] && _map[i][j] == _barrier)
{
return false;
}
}
}
return true;
}
private BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
if (_drawningObject == null || _map == null)
{
return bmp;
}
Graphics gr = bmp.getGraphics();
gr.setColor(DrawRoadPart());
gr.fillRect(0,0,_width, _height);
for (int i = 0; i < _map.length; i++)
{
for (int j = 0; j < _map[0].length; j++)
{
if (_map[i][j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawningObject.DrawningObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract Color DrawRoadPart();
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}

View File

@ -0,0 +1,36 @@
import java.awt.*;
public class DrawingMilitaryStormtrooper extends DrawingStormtrooper {
public DrawingMilitaryStormtrooper(int speed, float weight, Color bodyColor, Color dopColor, boolean bomb, boolean propeller) {
super(speed, weight, bodyColor, 135, 100);
Stormtrooper = new EntityMilitaryStormtrooper(speed, weight, bodyColor, dopColor, bomb, propeller);
}
@Override
public void DrawTransport(Graphics2D g) {
if(!(Stormtrooper instanceof EntityMilitaryStormtrooper storm))
{
return;
}
_startPosX += 2;
_startPosX -= 2;
super.DrawTransport(g);
EntityMilitaryStormtrooper entityStorm = (EntityMilitaryStormtrooper) Stormtrooper;
if (entityStorm.propeller)
{
g.setColor(entityStorm.dopColor);
g.fillOval((int)_startPosX - 2, (int)_startPosY +30, 5, 20);
g.fillOval((int) _startPosX - 2, (int)_startPosY + 50, 5, 20);
}
if (entityStorm.bomb)
{
g.setColor(Color.RED);
g.fillRect((int)_startPosX+95, (int)_startPosY + 30, 20, 10);
g.fillRect((int)_startPosX+95,(int) _startPosY + 60, 20, 10);
}
}
}

View File

@ -0,0 +1,38 @@
import java.awt.*;
public class DrawningObjectStormtrooper implements IDrawningObject {
private DrawingStormtrooper _stormtrooper = null;
public DrawningObjectStormtrooper(DrawingStormtrooper storm){
_stormtrooper = storm;
}
@Override
public float getStep() {
if (_stormtrooper.Stormtrooper != null) {
return _stormtrooper.Stormtrooper.Step;
}
return 0;
}
@Override
public void SetObject(int x, int y, int width, int height) {
if (_stormtrooper != null) _stormtrooper.SetPosition(x, y, width, height);
}
@Override
public void MoveObject(Direction direction) {
if (_stormtrooper!= null) _stormtrooper.MoveTransport(direction);
}
@Override
public void DrawningObject(Graphics g) {
if (_stormtrooper != null) _stormtrooper.DrawTransport((Graphics2D) g);
}
@Override
public float[] GetCurrentPosition() {
if (_stormtrooper != null) {return _stormtrooper.GetCurrentPosition();}
return null;
}
}

51
DrawningOvalEngines.java Normal file
View File

@ -0,0 +1,51 @@
import java.awt.*;
public class DrawningOvalEngines implements IDrawningEngines{
private DirectionEnginesOnStormtrooper enginesCount;
@Override
public void Draw(Graphics g, int x, int y, Color bodyColor) {
g.setColor(bodyColor);
switch (enginesCount) {
case TWO:
g.fillOval(x + 50, y, 20, 10);
g.fillOval(x + 50, y+90, 20, 10);
break;
case FOUR:
g.fillOval(x + 50, y, 20, 10);
g.fillOval(x + 50, y+15, 20, 10);
g.fillOval(x + 50, y+75, 20, 10);
g.fillOval(x + 50, y+90, 20, 10);
break;
case SIX:
g.fillOval(x + 50, y, 20, 10);
g.fillOval(x + 50, y+15, 20, 10);
g.fillOval(x + 50, y+30, 20, 10);
g.fillOval(x + 50, y+60, 20, 10);
g.fillOval(x + 50, y+75, 20, 10);
g.fillOval(x + 50, y+90, 20, 10);
break;
}
}
@Override
public void SetNewEngines(int count) {
switch(count)
{
case 2:
enginesCount = DirectionEnginesOnStormtrooper.TWO;
break;
case 4:
enginesCount = DirectionEnginesOnStormtrooper.FOUR;
break;
case 6:
enginesCount = DirectionEnginesOnStormtrooper.SIX;
break;
default:
break;
}
}
public DrawningOvalEngines(int count){
SetNewEngines(count);
}
}

View File

@ -0,0 +1,51 @@
import java.awt.*;
public class DrawningTriangleEngines implements IDrawningEngines{
private DirectionEnginesOnStormtrooper enginesCount;
@Override
public void Draw(Graphics g, int x, int y, Color bodyColor) {
g.setColor(bodyColor);
switch (enginesCount) {
case TWO:
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+5,y, y +10 }, 3);
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+95,y+90, y +100 }, 3);
break;
case FOUR:
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+5,y, y +10 }, 3);
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+20,y+15, y +25 }, 3);
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+80,y+75, y +85 }, 3);
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+95,y+90, y +100 }, 3);
break;
case SIX:
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+5,y, y +10 }, 3);
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+20,y+15, y +25 }, 3);
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+35,y+30, y +40 }, 3);
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+65,y+60, y +70 }, 3);
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+80,y+75, y +85 }, 3);
g.fillPolygon(new int[]{x + 50,x+60,x+60}, new int[]{y+95,y+90, y +100 }, 3);
break;
}
}
@Override
public void SetNewEngines(int count) {
switch(count)
{
case 2:
enginesCount = DirectionEnginesOnStormtrooper.TWO;
break;
case 4:
enginesCount = DirectionEnginesOnStormtrooper.FOUR;
break;
case 6:
enginesCount = DirectionEnginesOnStormtrooper.SIX;
break;
default:
break;
}
}
public DrawningTriangleEngines(int count){
SetNewEngines(count);
}
}

184
FormMap.java Normal file
View File

@ -0,0 +1,184 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.util.Random;
public class FormMap extends JComponent {
private AbstractMap _abstractMap;
private DrawingStormtrooper _stormtrooper;
private BufferedImage bufferedImage;
public static void main(String[] args) {
FormMap formMap = new FormMap();
}
public FormMap() {
JFrame form = new JFrame("Карта");
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setSize(1000, 500);
form.setVisible(true);
form.setLocationRelativeTo(null);
Panel statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new GridBagLayout());
setLayout(new BorderLayout());
form.add(statusPanel, BorderLayout.SOUTH);
Label speedLabel = new Label("Скорость: ");
Label weightLabel = new Label("Вес: ");
Label colorLabel = new Label("Цвет: ");
String[] maps = {
"Простая карта",
"Ясное небо",
"Пасмурное небо"
};
JComboBox mapSelectComboBox = new JComboBox(maps);
mapSelectComboBox.setEditable(true);
mapSelectComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String item = (String)mapSelectComboBox.getSelectedItem();
if (item == null) return;
switch (item) {
case ("Простая карта"):
_abstractMap = new SimpleMap();
break;
case ("Ясное небо"):
_abstractMap = new SecondMap();
break;
case ("Пасмурное небо"):
_abstractMap = new ThirdMap();
break;
}
}
});
JButton createButton = new JButton("Создать");
createButton.addActionListener(e -> {
Random rnd = new Random();
var stormtrop = new DrawingStormtrooper(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256),
rnd.nextInt(0, 256)));
speedLabel.setText("Скорость: " + stormtrop.Stormtrooper.getSpeed());
weightLabel.setText("Вес: " + (int)stormtrop.Stormtrooper.getWeight());
colorLabel.setText("Цвет: " + stormtrop.Stormtrooper.getBodyColor().getRed() + " " +
stormtrop.Stormtrooper.getBodyColor().getGreen() + " " + stormtrop.Stormtrooper.getBodyColor().getBlue() );
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(form.getWidth(), form.getHeight() - 80, new DrawningObjectStormtrooper(stormtrop));
repaint();
});
JButton modifiedButton = new JButton("Модификация");
modifiedButton.addActionListener(e -> {
Random rnd = new Random();
Color colorFirst = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
Color colorSecond = new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256));
var militarystorm = new DrawingMilitaryStormtrooper(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
colorFirst,
colorSecond,
rnd.nextBoolean(),
rnd.nextBoolean());
militarystorm.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 80);
speedLabel.setText("Скорость: " + militarystorm.Stormtrooper.getSpeed());
weightLabel.setText("Вес: " + militarystorm.Stormtrooper.getWeight());
colorLabel.setText("Цвет: " + militarystorm.Stormtrooper.getBodyColor().getRed() + " " + militarystorm.Stormtrooper.getBodyColor().getGreen() + " " + militarystorm.Stormtrooper.getBodyColor().getBlue() );
if (_abstractMap != null) bufferedImage = _abstractMap.CreateMap(form.getWidth(), form.getHeight() - 80, new DrawningObjectStormtrooper(militarystorm));
repaint();
});
GridBagConstraints c = new GridBagConstraints();//заполнение statuspanel
statusPanel.add(mapSelectComboBox);
statusPanel.add(createButton);
statusPanel.add(modifiedButton);
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
statusPanel.add(mapSelectComboBox,c);
c.gridx = 1;
c.gridy = 1;
statusPanel.add(createButton,c);
c.gridx = 2;
c.gridy = 1;
statusPanel.add(modifiedButton,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
statusPanel.add(speedLabel,c);
c.gridx = 4;
c.gridy = 1;
statusPanel.add(weightLabel,c);
c.gridx = 5;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(colorLabel,c);
JButton upButton = new JButton("");
JButton rightButton = new JButton("");
JButton leftButton = new JButton("");
JButton downButton = new JButton("");
upButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.UP);
repaint();
});
rightButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.RIGHT);
repaint();
});
leftButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.LEFT);
repaint();
});
downButton.addActionListener(e -> {
if(bufferedImage != null) bufferedImage = _abstractMap.MoveObject(Direction.DOWN);
repaint();
});
c.fill = GridBagConstraints.NONE;
c.gridx = 8;
c.gridy = 0;
c.gridwidth = 1;
statusPanel.add(upButton,c);
c.gridx = 7;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(leftButton,c);
c.gridx = 9;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(rightButton,c);
c.gridx = 8;
c.gridy = 1;
c.gridwidth = 1;
statusPanel.add(downButton,c);
form.getContentPane().add(this);
super.repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (bufferedImage != null) g2.drawImage(bufferedImage, 0,0,1000,420,null);
super.repaint();
}
}

7
IDrawningEngines.java Normal file
View File

@ -0,0 +1,7 @@
import java.awt.*;
public interface IDrawningEngines {
void Draw(Graphics g, int x, int y, Color bodyColor);
void SetNewEngines(int count);
}

9
IDrawningObject.java Normal file
View File

@ -0,0 +1,9 @@
import java.awt.*;
public interface IDrawningObject {
float getStep();
void SetObject(int x, int y, int width, int height);
void MoveObject(Direction direction);
void DrawningObject(Graphics g);
float[] GetCurrentPosition();
}

42
SecondMap.java Normal file
View File

@ -0,0 +1,42 @@
import java.awt.*;
public class SecondMap extends AbstractMap {
Color roadColor = Color.BLUE;
Color barrierColor = Color.WHITE;
@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(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
@Override
protected Color DrawRoadPart() {
return roadColor;
}
@Override
protected void DrawBarrierPart(Graphics g, int i, int j) {
g.setColor(barrierColor);
g.fillRect( (int)(i * _size_x), (int)(j * _size_y),(int) _size_x,(int) _size_y);
}
}

42
SimpleMap.java Normal file
View File

@ -0,0 +1,42 @@
import java.awt.*;
public class SimpleMap extends AbstractMap {
Color roadColor = Color.GRAY;
Color barrierColor = Color.BLACK;
@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(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
@Override
protected Color DrawRoadPart() {
return roadColor;
}
@Override
protected void DrawBarrierPart(Graphics g, int i, int j) {
g.setColor(barrierColor);
g.fillRect( (int)(i * _size_x), (int)(j * _size_y),(int) _size_x,(int) _size_y);
}
}

58
ThirdMap.java Normal file
View File

@ -0,0 +1,58 @@
import java.awt.*;
public class ThirdMap extends AbstractMap {
Color roadColor = Color.LIGHT_GRAY;
Color barrierColor = 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 < 60)
{
int x = _random.nextInt(0, 20);
int y = _random.nextInt(40, 60);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
x = _random.nextInt(80, 100);
y = _random.nextInt(0, 20);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
x = _random.nextInt(40, 60);
y = _random.nextInt(50, 80);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
@Override
protected Color DrawRoadPart() {
return roadColor;
}
@Override
protected void DrawBarrierPart(Graphics g, int i, int j) {
g.setColor(barrierColor);
g.fillRect( (int)(i * _size_x), (int)(j * _size_y),(int) _size_x,(int) _size_y);
}
}