diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/ProjectStormtrooperHard.iml b/.idea/ProjectStormtrooperHard.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/.idea/ProjectStormtrooperHard.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8ed5f34 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..da0fe38 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..f6b8502 --- /dev/null +++ b/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + UP, + DOWN, + LEFT, + RIGHT +} diff --git a/DirectionEnginesOnStormtrooper.java b/DirectionEnginesOnStormtrooper.java new file mode 100644 index 0000000..7f33930 --- /dev/null +++ b/DirectionEnginesOnStormtrooper.java @@ -0,0 +1,5 @@ +public enum DirectionEnginesOnStormtrooper { + TWO, + FOUR, + SIX +} diff --git a/DrawingEngines.java b/DrawingEngines.java new file mode 100644 index 0000000..fdb7b52 --- /dev/null +++ b/DrawingEngines.java @@ -0,0 +1,38 @@ +import java.awt.*; + +public class DrawingEngines { + 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 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; + } + } +} diff --git a/DrawingStormtrooper.java b/DrawingStormtrooper.java new file mode 100644 index 0000000..1340d02 --- /dev/null +++ b/DrawingStormtrooper.java @@ -0,0 +1,180 @@ +import java.awt.*; +import java.util.Random; + +public class DrawingStormtrooper { + public EntityStormtrooper Stormtrooper; + public DrawingEngines drawingEngines; + 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; + + 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 EntityStormtrooper getStormtrooper() { + return Stormtrooper; + } + + public void SetPosition(int x, int y, int width, int height){ + if ((x > 0 && y > 0) && (_StormHeight + y < height) && (_StormWidth + x < width)) + { + _startPosX = x; + _startPosY = y; + _pictureWidth = width; + _pictureHeight = height; + } + } + + public void MoveTransport(Direction direction) + { + if (_pictureWidth == null || _pictureHeight == null) + { + return; + } + switch (direction) + { + // вправо + case RIGHT: + if (_startPosX + _StormWidth + Stormtrooper.Step < _pictureWidth) + { + _startPosX += Stormtrooper.Step; + } + break; + //влево + case LEFT: + if (_startPosX - Stormtrooper.Step > 0) + { + _startPosX -= Stormtrooper.Step; + } + break; + //вверх + case UP: + if (_startPosY - Stormtrooper.Step > 0) + { + _startPosY -= Stormtrooper.Step; + } + break; + //вниз + case DOWN: + if (_startPosY + _StormHeight + Stormtrooper.Step < _pictureHeight) + { + _startPosY += Stormtrooper.Step; + } + break; + } + } + + public void DrawTransport(Graphics2D g) + { + if (_startPosX < 0 || _startPosY < 0 + || _pictureHeight == null || _pictureWidth == null) + { + return; + } + + int _startPosXInt = (int)_startPosX; + int _startPosYInt = (int)_startPosY; + + + + //фюзеляж самолёта + g.fillRect(_startPosXInt + 20, _startPosYInt + 40, 100, 20); + + //отрисовка крыла + int[] wingX = + { + _startPosXInt + 60 , + _startPosXInt + 80 , + _startPosXInt+90 , + _startPosXInt+90 , + _startPosXInt+80 , + _startPosXInt + 60 + }; + + int[] wingY = + { + _startPosYInt, + _startPosYInt, + _startPosYInt + 40, + _startPosYInt + 60, + _startPosYInt + 100, + _startPosYInt + 100 + }; + g.setColor(Stormtrooper.getBodyColor()); + g.fillPolygon(wingX, wingY, wingY.length); + + //нос самолёта + int[] noseX = + { + _startPosXInt + 20, + _startPosXInt + 20, + _startPosXInt + }; + + int[] noseY = + { + _startPosYInt + 60, + _startPosYInt + 40, + _startPosYInt +50 + }; + + g.fillPolygon(noseX, noseY, noseX.length); + + //стабилизатор + int[] stabX = + { + _startPosXInt + 120, + _startPosXInt + 135, + _startPosXInt + 135, + _startPosXInt + 120 + }; + + int[] stabY = + { + _startPosYInt + 60, + _startPosYInt + 80, + _startPosYInt + 20, + _startPosYInt + 40 + }; + + g.fillPolygon(stabX, stabY, stabX.length); + + //отрисовка двигателей + drawingEngines.Draw(g, (int) _startPosX, (int) _startPosY,Stormtrooper.getBodyColor()); + + g.setColor(Color.BLACK); + + g.drawRect(_startPosXInt + 20, _startPosYInt + 40, 100, 20); + g.drawPolygon(wingX, wingY, wingX.length); + g.drawPolygon(noseX, noseY, noseX.length); + g.drawPolygon(stabX, stabY, stabX.length); + } + + public void ChangeBorders(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth <= _StormWidth || _pictureHeight <= _StormHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _StormWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _StormWidth; + } + if (_startPosY + _StormHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _StormHeight; + } + } +} diff --git a/EntityStormtrooper.java b/EntityStormtrooper.java new file mode 100644 index 0000000..4119e90 --- /dev/null +++ b/EntityStormtrooper.java @@ -0,0 +1,33 @@ +import java.awt.Color; +import java.util.Random; + +public class EntityStormtrooper { + private int Speed = 0; + + private float Weight = 0; + + private Color BodyColor; + + public float Step; + + public int getSpeed() { + return Speed; + } + + public float getWeight() { + return Weight; + } + + public Color getBodyColor() { + return BodyColor; + } + + public void Init(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; + Step = Speed * 100 / Weight; + BodyColor = bodyColor; + } +} + diff --git a/FormStormtrooper.java b/FormStormtrooper.java new file mode 100644 index 0000000..356fd48 --- /dev/null +++ b/FormStormtrooper.java @@ -0,0 +1,141 @@ +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(); + } + +}