diff --git a/ProjectStormtrooper/src/CanvasStormtrooper.java b/ProjectStormtrooper/src/CanvasStormtrooper.java new file mode 100644 index 0000000..dd6af9e --- /dev/null +++ b/ProjectStormtrooper/src/CanvasStormtrooper.java @@ -0,0 +1,16 @@ +import javax.swing.*; +import java.awt.*; + +public class CanvasStormtrooper extends JComponent { + public DrawingStormtrooper _drawingStormtrooper; + public CanvasStormtrooper(){} + public void paintComponent(Graphics g) { + if (_drawingStormtrooper == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawingStormtrooper.DrawTransport(g2d); + super.repaint(); + } +} \ No newline at end of file diff --git a/ProjectStormtrooper/src/DirectionType.java b/ProjectStormtrooper/src/DirectionType.java new file mode 100644 index 0000000..8cf052e --- /dev/null +++ b/ProjectStormtrooper/src/DirectionType.java @@ -0,0 +1,12 @@ +import java.awt.*; + +public enum DirectionType { + //Вверх + Up, + //Вниз + Down, + //Влево + Left, + //Вправо + Right +} diff --git a/ProjectStormtrooper/src/DrawingEngines.java b/ProjectStormtrooper/src/DrawingEngines.java new file mode 100644 index 0000000..c48a84a --- /dev/null +++ b/ProjectStormtrooper/src/DrawingEngines.java @@ -0,0 +1,47 @@ +import java.awt.*; + +public class DrawingEngines { + private NumberOfEngines numberOfEngines; + public NumberOfEngines getNumberOfEngines() { + return numberOfEngines; + } + public void setAmountOfEngines(int amount){ + if(NumberOfEngines.contains(amount)) { + numberOfEngines = NumberOfEngines.getNumber(amount); + } + } + public void DrawEngines(Graphics g, int x, int y, Color bodyColor) { + g.setColor(bodyColor); + g.fillRect(x, y, 10, 10); + g.setColor(Color.black); + g.drawRect(x, y, 10, 10); + } + + private void drawTwoEngines(Graphics g, int x, int y, Color bodyColor){ + DrawEngines(g,x + 65, y + 50, bodyColor); + DrawEngines(g,x + 65, y + 81,bodyColor); + } + private void drawFourEngines(Graphics g, int x, int y, Color bodyColor){ + DrawEngines(g,x + 62, y + 10,bodyColor); + DrawEngines(g,x + 64, y + 101,bodyColor); + DrawEngines(g,x + 64, y + 30,bodyColor); + DrawEngines(g,x + 62, y + 121,bodyColor); + } + private void drawSixEngines(Graphics g, int x, int y, Color bodyColor){ + drawFourEngines(g,x,y,bodyColor); + drawTwoEngines(g,x,y,bodyColor); + } + public void SwitchDrawEngines(Graphics g, int x, int y, Color bodyColor){ + switch(getNumberOfEngines()){ + case TWO: + drawTwoEngines(g,x,y,bodyColor); + break; + case FOUR: + drawFourEngines(g,x,y,bodyColor); + break; + case SIX: + drawSixEngines(g,x,y,bodyColor); + break; + } + } +} diff --git a/ProjectStormtrooper/src/DrawingStormtrooper.java b/ProjectStormtrooper/src/DrawingStormtrooper.java new file mode 100644 index 0000000..f4d23c4 --- /dev/null +++ b/ProjectStormtrooper/src/DrawingStormtrooper.java @@ -0,0 +1,250 @@ +import java.awt.*; + +public class DrawingStormtrooper { + public EntityStormtrooper EntityStormtrooper; + public DrawingEngines drawingEngines = null; + /// + /// Ширина окна + /// + private Integer _pictureWidth; + + /// + /// Высота окна + /// + private Integer _pictureHeight; + + /// + /// Левая координата прорисовки бомбардировщика + /// + private Integer _startPosX; + + /// + /// Верхняя кооридната прорисовки бомбардировщика + /// + private Integer _startPosY; + + /// + /// Ширина прорисовки бомбардировщика + /// + private final Integer _drawningStormtrooperWidth = 140; + + /// + /// Высота прорисовки бомбардировщика + /// + private final Integer _drawningStormtrooperHeight = 140; + + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия двигателей + /// Признак наличия бомб + /// Признак наличия ракет + public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean rockets, boolean bombs, boolean engines) { + EntityStormtrooper = new EntityStormtrooper(); + EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rockets, bombs, engines); + if (engines == true) { + drawingEngines = new DrawingEngines(); + drawingEngines.setAmountOfEngines((int) ((Math.random() * 3) + 1) * 2); + ; + } + _startPosX = null; + _startPosY = null; + _pictureWidth = null; + _pictureHeight = null; + } + + /// + /// Установка границ поля + /// + /// Ширина поля + /// Высота поля + /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах + public boolean SetPictureSize(int width, int height) { + // TODO проверка, что объект "влезает" в размеры поля + // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена + if (width < _drawningStormtrooperWidth || height < _drawningStormtrooperHeight) return false; + _pictureWidth = width; + _pictureHeight = height; + if (_startPosX != null || _startPosY != null) { + if (_startPosX + _drawningStormtrooperWidth > _pictureWidth) { + _startPosX = -_drawningStormtrooperWidth + _pictureWidth; + } else if (_startPosX < 0) { + _startPosX = 0; + } + if (_startPosY + _drawningStormtrooperHeight > _pictureHeight) { + _startPosY = -_drawningStormtrooperHeight + _pictureHeight; + } else if (_startPosY < 0) { + _startPosY = 0; + } + } + return true; + } + + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) { + if (!(_pictureHeight != null && _pictureWidth != null)) return; + // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы + // то надо изменить координаты, чтобы он оставался в этих границах + if (x + _drawningStormtrooperWidth > _pictureWidth) { + _startPosX = x - (x + _drawningStormtrooperWidth - _pictureWidth); + } else if (x < 0) { + _startPosX = 0; + } else { + _startPosX = x; + } + if (y + _drawningStormtrooperHeight > _pictureHeight) { + _startPosY = y - (y + _drawningStormtrooperHeight - _pictureHeight); + } else if (y < 0) { + _startPosY = 0; + } else { + _startPosY = y; + } + } + + /// + /// Изменение направления перемещения + /// + /// Направление + /// true - перемещене выполнено, false - перемещение невозможно + public boolean MoveTransport(DirectionType direction) { + if (EntityStormtrooper == null || _startPosX == null || _startPosY == null) { + return false; + } + switch (direction) { + //влево + case DirectionType.Left: + if (_startPosX - EntityStormtrooper.Step > 0) { + _startPosX -= (int) EntityStormtrooper.Step; + } + return true; + //вверх + case DirectionType.Up: + if (_startPosY - EntityStormtrooper.Step > 0) { + _startPosY -= (int) EntityStormtrooper.Step; + } + return true; + // вправо + case DirectionType.Right: + //TODO прописать логику сдвига в право + if (_startPosX + _drawningStormtrooperWidth + EntityStormtrooper.Step < _pictureWidth) { + _startPosX += (int) EntityStormtrooper.Step; + } + ; + return true; + //вниз + case DirectionType.Down: + //TODO прописать логику сдвига в вниз + if (_startPosY + _drawningStormtrooperHeight + EntityStormtrooper.Step < _pictureHeight) { + _startPosY += (int) EntityStormtrooper.Step; + } + return true; + default: + return false; + } + } + + /// + /// Прорисовка объекта + /// + /// + public void DrawTransport(Graphics g) { + if (EntityStormtrooper == null || _startPosX == null || _startPosY == null) { + return; + } + + g.setColor(Color.black); + + + //нос штурмовика + + + g.setColor(EntityStormtrooper.getBodyColor()); + Point[] Nose = new Point[3]; + int[] arrX = {_startPosX + 20, _startPosX, _startPosX + 20}; + int[] arrY = {_startPosY + 80, _startPosY + 70, _startPosY + 60}; + Polygon poly = new Polygon(arrX, arrY, 3); + g.fillPolygon(poly); + g.setColor(Color.black); + g.drawPolygon(poly); + + + //Заднии крылья штурмовика + g.setColor(EntityStormtrooper.getBodyColor()); + + Point[] pflybtwings = new Point[6]; + int[] arrX1 = {_startPosX + 120, _startPosX + 120, _startPosX + 140, _startPosX + 140, _startPosX + 120, _startPosX + 120}; + int[] arrY1 = {_startPosY + 60, _startPosY + 50, _startPosY + 30, _startPosY + 110, _startPosY + 90, _startPosY + 80}; + Polygon poly1 = new Polygon(arrX1, arrY1, 6); + g.fillPolygon(poly1); + g.setColor(Color.black); + g.drawPolygon(poly1); + + + //Тело штурмовика + g.setColor(EntityStormtrooper.getBodyColor()); + + + g.fillRect(_startPosX + 20, _startPosY + 60, 120, 20); + g.setColor(Color.black); + g.drawRect(_startPosX + 20, _startPosY + 60, 120, 20); + + + //Крылья штурмовика + g.setColor(EntityStormtrooper.getBodyColor()); + + + Point[] frontwings = new Point[4]; + int[] arrX2 = {_startPosX + 50, _startPosX + 50, _startPosX + 60, _startPosX + 70}; + int[] arrY2 = {_startPosY + 60, _startPosY, _startPosY, _startPosY + 60}; + Polygon poly2 = new Polygon(arrX2, arrY2, 4); + g.fillPolygon(poly2); + g.setColor(Color.black); + g.drawPolygon(poly2); + + + g.setColor(EntityStormtrooper.getBodyColor()); + Point[] frontwings2 = new Point[4]; + int[] arrX3 = {_startPosX + 50, _startPosX + 50, _startPosX + 60, _startPosX + 70}; + int[] arrY3 = {_startPosY + 80, _startPosY + 140, _startPosY + 140, _startPosY + 80}; + Polygon poly3 = new Polygon(arrX3, arrY3, 4); + g.fillPolygon(poly3); + g.setColor(Color.black); + g.drawPolygon(poly3); + + + //Ракеты штурмовика + if (EntityStormtrooper.getRockets()) { + g.setColor(EntityStormtrooper.getAdditionalColor()); + g.fillRect(_startPosX + 45, _startPosY + 20, 15, 5); + g.fillRect(_startPosX + 45, _startPosY + 110, 15, 5); + g.setColor(Color.black); + g.drawRect(_startPosX + 45, _startPosY + 20, 15, 5); + g.drawRect(_startPosX + 45, _startPosY + 110, 15, 5); + + + } + //Бомбы штурмовика + if (EntityStormtrooper.getBombs()) { + g.setColor(EntityStormtrooper.getAdditionalColor()); + g.fillRect(_startPosX + 50, _startPosY + 40, 10, 10); + g.fillRect(_startPosX + 50, _startPosY + 90, 10, 10); + g.setColor(Color.black); + g.drawRect(_startPosX + 50, _startPosY + 40, 10, 10); + g.drawRect(_startPosX + 50, _startPosY + 90, 10, 10); + + + } + if (EntityStormtrooper.getEngines() && drawingEngines != null) { + drawingEngines.SwitchDrawEngines(g, _startPosX, _startPosY, EntityStormtrooper.getBodyColor()); + } + + } +} diff --git a/ProjectStormtrooper/src/EntityStormtrooper.java b/ProjectStormtrooper/src/EntityStormtrooper.java new file mode 100644 index 0000000..7080372 --- /dev/null +++ b/ProjectStormtrooper/src/EntityStormtrooper.java @@ -0,0 +1,42 @@ +import java.awt.*; + +public class EntityStormtrooper { + private int Speed; + public int getSpeed() { + return Speed; + } + private float Weight; + public float getWeight() { + return Weight; + } + private Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + private Color AdditionalColor; + public Color getAdditionalColor() { + return AdditionalColor; + } + private boolean Rockets; + public boolean getRockets() { + return Rockets; + } + private boolean Bombs; + public boolean getBombs() { + return Bombs; + } + private boolean Engines; + public boolean getEngines() {return Engines;} + public float Step; + public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean rockets, boolean bombs, boolean engines) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + Rockets = rockets; + Bombs = bombs; + Engines = engines; + Step = Speed * 100 / Weight; + } +} diff --git a/ProjectStormtrooper/src/FormStormtrooper.java b/ProjectStormtrooper/src/FormStormtrooper.java new file mode 100644 index 0000000..ab5565c --- /dev/null +++ b/ProjectStormtrooper/src/FormStormtrooper.java @@ -0,0 +1,113 @@ +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.util.Random; + +public class FormStormtrooper extends JFrame { + private final String title; + private final Dimension dimension; + private int Width; + private int Height; + private final CanvasStormtrooper canvasStormtrooper = new CanvasStormtrooper(); + private final JButton CreateButton = new JButton("Создать"); + private final JButton UpButton = new JButton(); + private final JButton DownButton = new JButton(); + private final JButton LeftButton = new JButton(); + private final JButton RightButton = new JButton(); + public FormStormtrooper(String title, Dimension dimension) { + this.title = title; + this.dimension = dimension; + } + public void Init() { + setTitle(title); + setMinimumSize(dimension); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + Width = getWidth() - 13; + Height = getHeight() - 30; + CreateButton.setName("createButton"); + Icon iconUp = new ImageIcon("C:\\Users\\User\\IdeaProjects\\PIBD-14_Khalikova.A.R_Stormtrooper___Hard\\ProjectStormtrooper\\Resources\\arrowUp.jpg"); + UpButton.setIcon(iconUp); + UpButton.setName("UP"); + DownButton.setName("DOWN"); + Icon iconDown = new ImageIcon("C:\\Users\\User\\IdeaProjects\\PIBD-14_Khalikova.A.R_Stormtrooper___Hard\\ProjectStormtrooper\\Resources\\arrowDown.jpg"); + DownButton.setIcon(iconDown); + LeftButton.setName("LEFT"); + Icon iconLeft = new ImageIcon("C:\\Users\\User\\IdeaProjects\\PIBD-14_Khalikova.A.R_Stormtrooper___Hard\\ProjectStormtrooper\\Resources\\arrowLeft.jpg"); + LeftButton.setIcon(iconLeft); + RightButton.setName("RIGHT"); + Icon iconRight = new ImageIcon("C:\\Users\\User\\IdeaProjects\\PIBD-14_Khalikova.A.R_Stormtrooper___Hard\\ProjectStormtrooper\\Resources\\arrowRight.jpg"); + RightButton.setIcon(iconRight); + CreateButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int StartPositionX = (int)(Math.random() * 90 + 10); + int StartPositionY = (int)(Math.random() * 90 + 10); + int speed = (int)(Math.random() * 300 + 100); + float weight = (float) (Math.random() * 3000 + 1000); + Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); + Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); + boolean rockets = new Random().nextBoolean(); + boolean bombs = new Random().nextBoolean(); + boolean engines = new Random().nextBoolean(); + canvasStormtrooper._drawingStormtrooper = new DrawingStormtrooper(); + canvasStormtrooper._drawingStormtrooper.Init(speed, weight, bodyColor, additionalColor,rockets, bombs, engines); + canvasStormtrooper._drawingStormtrooper.SetPictureSize(Width, Height); + canvasStormtrooper._drawingStormtrooper.SetPosition( StartPositionX, StartPositionY); + canvasStormtrooper.repaint(); + } + }); + + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + if (canvasStormtrooper._drawingStormtrooper == null) return; + boolean result = switch ((((JButton) (event.getSource())).getName())) { + case "UP" -> canvasStormtrooper._drawingStormtrooper.MoveTransport(DirectionType.Up); + case "DOWN" -> canvasStormtrooper._drawingStormtrooper.MoveTransport(DirectionType.Down); + case "LEFT" -> canvasStormtrooper._drawingStormtrooper.MoveTransport(DirectionType.Left); + case "RIGHT" -> canvasStormtrooper._drawingStormtrooper.MoveTransport(DirectionType.Right); + default -> false; + }; + if (result) { + canvasStormtrooper.repaint(); + } + } + }; + UpButton.addActionListener(actionListener); + DownButton.addActionListener(actionListener); + LeftButton.addActionListener(actionListener); + RightButton.addActionListener(actionListener); + setSize(dimension.width,dimension.height); + setLayout(null); + canvasStormtrooper.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 130, 40); + UpButton.setBounds(getWidth() - 180, getHeight() - 210, 70, 70); + DownButton.setBounds(getWidth() - 180, getHeight() - 140, 70, 70); + RightButton.setBounds(getWidth() - 110, getHeight() - 140, 70, 70); + LeftButton.setBounds(getWidth() - 250, getHeight() - 140, 70, 70); + add(CreateButton); + add(UpButton); + add(DownButton); + add(RightButton); + add(LeftButton); + add(canvasStormtrooper); + setVisible(true); + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + Width = getWidth() - 13; + Height = getHeight() - 30; + if (canvasStormtrooper._drawingStormtrooper != null)canvasStormtrooper._drawingStormtrooper.SetPictureSize(Width, Height); + canvasStormtrooper.setBounds(0,0, getWidth(), getHeight()); + CreateButton.setBounds(10, getHeight() - 90, 130, 40); + UpButton.setBounds(getWidth() - 180, getHeight() - 210, 70, 70); + DownButton.setBounds(getWidth() - 180, getHeight() - 140, 70, 70); + RightButton.setBounds(getWidth() - 110, getHeight() - 140, 70, 70); + LeftButton.setBounds(getWidth() - 250, getHeight() - 140, 70, 70); + } + }); + } +} + diff --git a/ProjectStormtrooper/src/Main.java b/ProjectStormtrooper/src/Main.java index 3e59c38..8e0284d 100644 --- a/ProjectStormtrooper/src/Main.java +++ b/ProjectStormtrooper/src/Main.java @@ -1,5 +1,9 @@ +import java.awt.*; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + FormStormtrooper form = new FormStormtrooper("Штурмовик", new Dimension(800,800)); + form.Init(); } } \ No newline at end of file diff --git a/ProjectStormtrooper/src/NumberOfEngines.java b/ProjectStormtrooper/src/NumberOfEngines.java new file mode 100644 index 0000000..c1f567d --- /dev/null +++ b/ProjectStormtrooper/src/NumberOfEngines.java @@ -0,0 +1,32 @@ +public enum NumberOfEngines { + TWO(2), + FOUR(4), + SIX(6); + + private final int value; + + NumberOfEngines(int value) { + this.value = value; + } + + public static NumberOfEngines getNumber(int amount) { + NumberOfEngines[] num = NumberOfEngines.values(); + for (int i = 0; i < num.length; i++) { + if (amount == num[i].value) { + return num[i]; + } + } + return null; + } + + public static boolean contains(int amount) { + NumberOfEngines[] num = NumberOfEngines.values(); + for (int i = 0; i < num.length; i++) { + if (amount == num[i].value) { + return true; + } + } + return false; + } +} +