diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..5e6d1fa --- /dev/null +++ b/AbstractMap.java @@ -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); + +} diff --git a/DrawingEngines.java b/DrawingEngines.java index fdb7b52..1ff25c0 100644 --- a/DrawingEngines.java +++ b/DrawingEngines.java @@ -1,38 +1,50 @@ import java.awt.*; -public class DrawingEngines { +public class DrawingEngines implements IDrawningEngines{ private DirectionEnginesOnStormtrooper enginesCount; - public void SetNewEngines(int countEngines){ - if (countEngines == 4) { - enginesCount = DirectionEnginesOnStormtrooper.FOUR; - } else if (countEngines == 6) { - enginesCount = DirectionEnginesOnStormtrooper.SIX; - } - else { - enginesCount = DirectionEnginesOnStormtrooper.TWO; - } + public DrawingEngines(int count) { + SetNewEngines(count); } - public void Draw(Graphics2D g, int x, int y, Color color) { - g.setColor(color != null ? color : Color.BLACK); - switch (enginesCount) { - case TWO: - g.fillRect(x + 50, y, 20, 5); - g.fillRect(x + 50, y+95, 20, 5); - break; - case FOUR: - g.fillRect(x + 50, y, 20, 5); - g.fillRect(x + 50, y+10, 20, 5); - g.fillRect(x + 50, y+85, 20, 5); - g.fillRect(x + 50, y+95, 20, 5); - break; - case SIX: - g.fillRect(x + 50, y, 20, 5); - g.fillRect(x + 50, y+10, 20, 5); - g.fillRect(x + 50, y+20, 20, 5); - g.fillRect(x + 50, y+75, 20, 5); - g.fillRect(x + 50, y+85, 20, 5); - g.fillRect(x + 50, y+95, 20, 5); - break; - } + @Override + public void Draw(Graphics g, int x, int y, Color bodycolor) { + g.setColor(bodycolor); + switch (enginesCount) { + case TWO: + g.fillRect(x + 50, y, 20, 5); + g.fillRect(x + 50, y+95, 20, 5); + break; + case FOUR: + g.fillRect(x + 50, y, 20, 5); + g.fillRect(x + 50, y+10, 20, 5); + g.fillRect(x + 50, y+85, 20, 5); + g.fillRect(x + 50, y+95, 20, 5); + break; + case SIX: + g.fillRect(x + 50, y, 20, 5); + g.fillRect(x + 50, y+10, 20, 5); + g.fillRect(x + 50, y+20, 20, 5); + g.fillRect(x + 50, y+75, 20, 5); + g.fillRect(x + 50, y+85, 20, 5); + g.fillRect(x + 50, y+95, 20, 5); + 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; + } + } } diff --git a/DrawingMilitaryStormtrooper.java b/DrawingMilitaryStormtrooper.java new file mode 100644 index 0000000..a50f2f0 --- /dev/null +++ b/DrawingMilitaryStormtrooper.java @@ -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); + + } + } +} diff --git a/DrawingStormtrooper.java b/DrawingStormtrooper.java index 1340d02..9e91bf5 100644 --- a/DrawingStormtrooper.java +++ b/DrawingStormtrooper.java @@ -3,24 +3,42 @@ import java.util.Random; public class DrawingStormtrooper { public EntityStormtrooper Stormtrooper; + public EntityStormtrooper getStormtrooper() { + return Stormtrooper; + } public DrawingEngines drawingEngines; + private IDrawningEngines iDrawingEngines; public float _startPosX; public float _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; - private static final int _StormWidth = 135; - private static final int _StormHeight = 100; + private int _StormWidth = 135; + private int _StormHeight = 100; - public void Init(int speed, float weight, Color bodyColor, int numberOfEngines){ - Stormtrooper = new EntityStormtrooper(); - drawingEngines = new DrawingEngines(); - Stormtrooper.Init(speed, weight, bodyColor); - System.out.println(numberOfEngines + ""); - drawingEngines.SetNewEngines(numberOfEngines); + public DrawingStormtrooper(int speed, float weight, Color bodyColor){ + Random random = new Random(); + int[] ArrayEngines = new int[]{2, 4, 6}; + + switch (random.nextInt(3)){ + case 0: + iDrawingEngines = new DrawingEngines(ArrayEngines[random.nextInt(0, 3)]); + break; + case 1: + iDrawingEngines = new DrawningOvalEngines(ArrayEngines[random.nextInt(0, 3)]); + break; + case 2: + iDrawingEngines = new DrawningTriangleEngines(ArrayEngines[random.nextInt(0, 3)]); + break; + + } + Stormtrooper = new EntityStormtrooper(speed,weight, bodyColor); } - public EntityStormtrooper getStormtrooper() { - return Stormtrooper; + protected DrawingStormtrooper(int speed, float weight, Color bodyColor, int StormWidth, int StormHeight) + { + this (speed, weight, bodyColor); + _StormWidth = StormWidth; + _StormHeight = StormHeight; } public void SetPosition(int x, int y, int width, int height){ @@ -86,6 +104,7 @@ public class DrawingStormtrooper { //фюзеляж самолёта + g.setColor(Color.BLACK); g.fillRect(_startPosXInt + 20, _startPosYInt + 40, 100, 20); //отрисовка крыла @@ -148,7 +167,7 @@ public class DrawingStormtrooper { g.fillPolygon(stabX, stabY, stabX.length); //отрисовка двигателей - drawingEngines.Draw(g, (int) _startPosX, (int) _startPosY,Stormtrooper.getBodyColor()); + iDrawingEngines.Draw(g, (int) _startPosX, (int) _startPosY, Stormtrooper.getBodyColor()); g.setColor(Color.BLACK); @@ -177,4 +196,8 @@ public class DrawingStormtrooper { _startPosY = _pictureHeight - _StormHeight; } } + public float[] GetCurrentPosition() + { + return new float[]{_startPosX, _startPosX + _StormWidth, _startPosY, _startPosY + _StormHeight}; + } } diff --git a/DrawningObjectStormtrooper.java b/DrawningObjectStormtrooper.java new file mode 100644 index 0000000..56cd203 --- /dev/null +++ b/DrawningObjectStormtrooper.java @@ -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; + } + +} diff --git a/DrawningOvalEngines.java b/DrawningOvalEngines.java new file mode 100644 index 0000000..a459b57 --- /dev/null +++ b/DrawningOvalEngines.java @@ -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); + } +} diff --git a/DrawningTriangleEngines.java b/DrawningTriangleEngines.java new file mode 100644 index 0000000..348087d --- /dev/null +++ b/DrawningTriangleEngines.java @@ -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); + } +} diff --git a/EntityMilitaryStormtrooper.java b/EntityMilitaryStormtrooper.java new file mode 100644 index 0000000..09668a6 --- /dev/null +++ b/EntityMilitaryStormtrooper.java @@ -0,0 +1,14 @@ +import java.awt.*; + +public class EntityMilitaryStormtrooper extends EntityStormtrooper { + public final boolean bomb; + public final boolean propeller; + public final Color dopColor; + + public EntityMilitaryStormtrooper(int speed, float weight, Color bodyColor, Color DopColor, boolean Bomb, boolean Propeller) { + super(speed, weight, bodyColor); + dopColor = DopColor; + bomb = Bomb; + propeller = Propeller; + } +} \ No newline at end of file diff --git a/EntityStormtrooper.java b/EntityStormtrooper.java index 4119e90..88c4f11 100644 --- a/EntityStormtrooper.java +++ b/EntityStormtrooper.java @@ -22,7 +22,7 @@ public class EntityStormtrooper { return BodyColor; } - public void Init(int speed, float weight, Color bodyColor){ + public EntityStormtrooper(int speed, float weight, Color bodyColor){ Random rnd = new Random(); Speed = speed <= 0 ? rnd.nextInt(50 + 1) +50 : speed; //генерация в диапазоне от 50 до 100 Weight = weight <= 0 ? rnd.nextInt(50 + 1) +50 : weight; diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..6debd76 --- /dev/null +++ b/FormMap.java @@ -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(); + } + +} diff --git a/FormStormtrooper.java b/FormStormtrooper.java deleted file mode 100644 index 356fd48..0000000 --- a/FormStormtrooper.java +++ /dev/null @@ -1,141 +0,0 @@ -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.util.Random; -public class FormStormtrooper extends JComponent { - private DrawingStormtrooper _stormtrooper; - public static void main(String[] args) { - FormStormtrooper formStormtrooper = new FormStormtrooper(); - } - public FormStormtrooper() { - JFrame form = new JFrame("Штурмовик"); - form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - form.setSize(800, 500); - form.setVisible(true); - form.setLocationRelativeTo(null); - form.addComponentListener(new ComponentListener() { - @Override - public void componentResized(ComponentEvent e) { - if(_stormtrooper != null) _stormtrooper.ChangeBorders(getWidth(), getHeight()); - repaint(); - } - - @Override - public void componentMoved(ComponentEvent e) { - } - - @Override - public void componentShown(ComponentEvent e) { - } - - @Override - public void componentHidden(ComponentEvent e) { - } - }); - 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("Цвет: "); - - - JButton createButton = new JButton("Создать"); - createButton.addActionListener(e -> { - int[] countBlocks = {2, 4, 6}; - Random rnd = new Random(); - _stormtrooper = new DrawingStormtrooper(); - _stormtrooper.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), - Color.getHSBColor(rnd.nextInt(0, 256), rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), countBlocks[rnd.nextInt(0, 3)]); - _stormtrooper.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), form.getWidth(), form.getHeight() - 80); - speedLabel.setText("Скорость: " + _stormtrooper.Stormtrooper.getSpeed()); - weightLabel.setText("Вес: " + (int)_stormtrooper.Stormtrooper.getWeight()); - colorLabel.setText("Цвет: " + _stormtrooper.Stormtrooper.getBodyColor().getRed() + " " + _stormtrooper.Stormtrooper.getBodyColor().getGreen() + " " + _stormtrooper.Stormtrooper.getBodyColor().getBlue() ); - repaint(); - - }); - GridBagConstraints c = new GridBagConstraints(); - - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.CENTER; - c.weightx = 0.5; - c.gridx = 0; - c.gridy = 1; - statusPanel.add(createButton,c); - - c.fill = GridBagConstraints.HORIZONTAL; - c.gridx = 1; - c.gridy = 1; - statusPanel.add(speedLabel,c); - - c.gridx = 2; - c.gridy = 1; - statusPanel.add(weightLabel,c); - - - c.gridx = 3; - 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 (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.UP); - repaint(); - }); - - - rightButton.addActionListener(e -> { - if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.RIGHT); - repaint(); - }); - - - leftButton.addActionListener(e -> { - if (_stormtrooper != null) _stormtrooper.MoveTransport(Direction.LEFT); - repaint(); - }); - - - downButton.addActionListener(e -> { - if (_stormtrooper != null) _stormtrooper.MoveTransport(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); - } - protected void paintComponent(Graphics g) { - super.paintComponent(g); - Graphics2D g2 = (Graphics2D)g; - if (_stormtrooper != null) _stormtrooper.DrawTransport(g2); - super.repaint(); - } - -} diff --git a/IDrawningEngines.java b/IDrawningEngines.java new file mode 100644 index 0000000..446f35b --- /dev/null +++ b/IDrawningEngines.java @@ -0,0 +1,7 @@ +import java.awt.*; + +public interface IDrawningEngines { + void Draw(Graphics g, int x, int y, Color bodyColor); + void SetNewEngines(int count); + +} \ No newline at end of file diff --git a/IDrawningObject.java b/IDrawningObject.java new file mode 100644 index 0000000..af06c5b --- /dev/null +++ b/IDrawningObject.java @@ -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(); +} diff --git a/SecondMap.java b/SecondMap.java new file mode 100644 index 0000000..fa43d99 --- /dev/null +++ b/SecondMap.java @@ -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); + } +} diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..a5d487d --- /dev/null +++ b/SimpleMap.java @@ -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); + } +} diff --git a/ThirdMap.java b/ThirdMap.java new file mode 100644 index 0000000..47b77d9 --- /dev/null +++ b/ThirdMap.java @@ -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); + } +} \ No newline at end of file