diff --git a/Tank/.gitignore b/Tank/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/Tank/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Tank/.idea/.gitignore b/Tank/.idea/.gitignore new file mode 100644 index 0000000..1c2fda5 --- /dev/null +++ b/Tank/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Tank/.idea/misc.xml b/Tank/.idea/misc.xml new file mode 100644 index 0000000..80cafc3 --- /dev/null +++ b/Tank/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Tank/.idea/modules.xml b/Tank/.idea/modules.xml new file mode 100644 index 0000000..8d39350 --- /dev/null +++ b/Tank/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Tank/.idea/vcs.xml b/Tank/.idea/vcs.xml new file mode 100644 index 0000000..2e3f692 --- /dev/null +++ b/Tank/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Tank/Resources/KeyDown.png b/Tank/Resources/KeyDown.png new file mode 100644 index 0000000..ff09b70 Binary files /dev/null and b/Tank/Resources/KeyDown.png differ diff --git a/Tank/Resources/KeyLeft.png b/Tank/Resources/KeyLeft.png new file mode 100644 index 0000000..b64110a Binary files /dev/null and b/Tank/Resources/KeyLeft.png differ diff --git a/Tank/Resources/KeyRight.png b/Tank/Resources/KeyRight.png new file mode 100644 index 0000000..478e541 Binary files /dev/null and b/Tank/Resources/KeyRight.png differ diff --git a/Tank/Resources/KeyUp.png b/Tank/Resources/KeyUp.png new file mode 100644 index 0000000..0e4bf6f Binary files /dev/null and b/Tank/Resources/KeyUp.png differ diff --git a/Tank/Tank.iml b/Tank/Tank.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/Tank/Tank.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Tank/src/CountWheels.java b/Tank/src/CountWheels.java new file mode 100644 index 0000000..5d064d7 --- /dev/null +++ b/Tank/src/CountWheels.java @@ -0,0 +1,14 @@ +public enum CountWheels { + Two(2), + Three(3), + Four(4), + Five(5); + private final int Value; + CountWheels(int Count){ + Value=Count; + } + public int getCountWheels(){ + return Value; + } + public int setCountWheels() { return Value; } +} diff --git a/Tank/src/Direction.java b/Tank/src/Direction.java new file mode 100644 index 0000000..a641b80 --- /dev/null +++ b/Tank/src/Direction.java @@ -0,0 +1,6 @@ +public enum Direction { + Up, + Down, + Left, + Right +} diff --git a/Tank/src/DrawingField.java b/Tank/src/DrawingField.java new file mode 100644 index 0000000..5f782e8 --- /dev/null +++ b/Tank/src/DrawingField.java @@ -0,0 +1,54 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawingField extends JPanel { + private final FormTank field; + DrawingTank _Tank; + public DrawingField(FormTank field) { + this.field = field; + } + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 =(Graphics2D)g; + if (_Tank!=null) + _Tank.DrawTransport(g2); + else return; + } + public void UpButtonAction(){ + if (_Tank!=null) + _Tank.MoveTransport(Direction.Up); + else + return; + } + public void DownButtonAction(){ + if (_Tank!=null) + _Tank.MoveTransport(Direction.Down); + else + return; + } + public void RightButtonAction(){ + if (_Tank!=null) + _Tank.MoveTransport(Direction.Right); + else + return; + } + public void LeftButtonAction(){ + if (_Tank!=null) + _Tank.MoveTransport(Direction.Left); + else + return; + } + public void CreateButtonAction(){ + Random rnd=new Random(); + _Tank=new DrawingTank(); + _Tank.Init(rnd.nextInt(50)+10,rnd.nextInt(100)+500,new Color(rnd.nextInt(256),rnd.nextInt(256),rnd.nextInt(256))); + _Tank.SetPosition(rnd.nextInt(100)+10,rnd.nextInt(100)+10,getWidth(),getHeight()); + } + public void ResizeField(){ + if (_Tank!=null) + _Tank.ChangeBorders(getWidth(),getHeight()); + else return; + } +} diff --git a/Tank/src/DrawingTank.java b/Tank/src/DrawingTank.java new file mode 100644 index 0000000..1f363ce --- /dev/null +++ b/Tank/src/DrawingTank.java @@ -0,0 +1,143 @@ +import java.awt.*; + +public class DrawingTank { + public EntityTank Tank; + public EntityTank getTank() { + return Tank; + } + public DrawingWheels Wheels; + private int _startPosX; + private int _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private final int _TankWidth = 175; + private final int _TankHeight = 100; + + public void Init(int speed, float weight, Color bodyColor) + { + Tank = new EntityTank(); + Tank.Init(speed, weight, bodyColor); + Wheels = new DrawingWheels(); + Wheels.SetCountWheels((int)(2 + Math.random() + Math.random()*2)); + } + + public void SetPosition(int x, int y, int width, int height) + { + if (x >= 0 && x+_TankWidth <= width && y >= 0 && y+_TankHeight <= height) + { + _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 + _TankWidth + Tank.Step < _pictureWidth) + { + _startPosX += Tank.Step; + } + break; + + case Left: + if (_startPosX- Tank.Step >= 0) + { + _startPosX -= Tank.Step; + } + break; + + case Up: + if (_startPosY - Tank.Step >= 0) + { + _startPosY -= Tank.Step; + } + break; + + case Down: + if (_startPosY + _TankHeight + Tank.Step < _pictureHeight) + { + _startPosY += Tank.Step; + } + break; + } + } + public void DrawTransport(Graphics g) + { + if (_startPosX < 0 || _startPosY < 0 || _pictureHeight== null || _pictureWidth== null) + { + return; + } + + Graphics2D g2 = (Graphics2D) g; + + //Гусеница + Color Gray = new Color(128, 128, 128); + g2.setColor(Gray); + g2.drawOval(_startPosX + 10, _startPosY + 30, 120, 30); + + // Ведущие колёса танка (в размере чуть меньше) + g2.setColor(Color.BLACK); + g2.drawOval(_startPosX + 113, _startPosY + 41, 11, 11); + g2.fillOval(_startPosX + 113, _startPosY + 41, 11, 11); + g2.drawOval(_startPosX + 13, _startPosY + 40, 11, 11); + g2.fillOval(_startPosX + 13, _startPosY + 40, 11, 11); + Wheels.DrawWheels(g2, _startPosX, _startPosY); + + // Корпус танка + g2.setColor(Color.DARK_GRAY); + int[] xPoints = {_startPosX + 5, _startPosX + 140, _startPosX + 130,_startPosX + 12}; + int[] yPoints = {_startPosY + 30, _startPosY + 30, _startPosY + 42, _startPosY + 42}; + int nPoints = 4; + g2.drawPolygon(xPoints,yPoints,nPoints); + g2.fillPolygon(xPoints,yPoints,nPoints); + + // Башня + int[] xPointsBody = {_startPosX + 52, _startPosX + 52, _startPosX + 40, _startPosX + 15,_startPosX + 15, _startPosX + 60,_startPosX + 90,_startPosX + 120,_startPosX + 100,_startPosX + 95, _startPosX + 90}; + int[] yPointsBody = {_startPosY + 30, _startPosY + 27, _startPosY + 23, _startPosY + 18,_startPosY + 15, _startPosY + 11,_startPosY + 11,_startPosY + 20,_startPosY + 25,_startPosY + 27,_startPosY + 30}; + int nPointsBody = 11; + + g2.drawPolygon(xPointsBody,yPointsBody,nPointsBody); + g2.fillPolygon(xPointsBody,yPointsBody,nPointsBody); + + // Орудие + g2.drawRect(_startPosX + 112, _startPosY+17, 50, 5); + g2.fillRect(_startPosX + 112, _startPosY+17, 50, 5); + + // Зенитное орудие + int[] xPointsGun = {_startPosX + 45, _startPosX + 45, _startPosX + 41, _startPosX + 41, _startPosX + 42, _startPosX + 41, _startPosX + 44,_startPosX + 50 ,_startPosX + 52,_startPosX + 53, _startPosX + 58}; + int[] yPointsGun = {_startPosY + 12, _startPosY + 10, _startPosY + 8, _startPosY + 7, _startPosY + 5, _startPosY + 4,_startPosY + 3,_startPosY + 3,_startPosY + 5,_startPosY + 7,_startPosY + 10}; + int nPointsGun = 11; + + g2.fillRect(_startPosX + 50, _startPosY+5, 20, 2); + g2.drawPolygon(xPointsGun,yPointsGun,nPointsGun); + g2.fillPolygon(xPointsGun,yPointsGun,nPointsGun); + } + + public void ChangeBorders(int width,int height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_pictureWidth<=_TankWidth||_pictureHeight<=_TankHeight) + { + _pictureWidth = null; + _pictureHeight = null; + return; + } + if (_startPosX + _TankWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _TankWidth; + } + if (_startPosY + _TankHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _TankHeight; + } + } +} diff --git a/Tank/src/DrawingWheels.java b/Tank/src/DrawingWheels.java new file mode 100644 index 0000000..397c052 --- /dev/null +++ b/Tank/src/DrawingWheels.java @@ -0,0 +1,50 @@ +import java.awt.*; + +public class DrawingWheels { + + private CountWheels _wheels; + + public void SetCountWheels(int Count){ + for (CountWheels temp: CountWheels.values()) + if (temp.getCountWheels() == Count){ + _wheels=temp; + return; + } + } + + public void DrawWheels(Graphics2D g,int _startPosX, int _startPosY) { + switch (_wheels.getCountWheels()) + { + case 2: + // Отрисовка 2 катков + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 30, _startPosY + 42, 15, 15); + g.fillOval(_startPosX + 30, _startPosY + 42, 15, 15); + g.drawOval(_startPosX + 50, _startPosY + 45, 15, 15); + g.fillOval(_startPosX + 50, _startPosY + 45, 15, 15); + break; + case 3: + // Отрисовка 3 катков + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 30, _startPosY + 42, 15, 15); + g.fillOval(_startPosX + 30, _startPosY + 42, 15, 15); + g.drawOval(_startPosX + 50, _startPosY + 45, 15, 15); + g.fillOval(_startPosX + 50, _startPosY + 45, 15, 15); + g.drawOval(_startPosX + 75, _startPosY + 45, 15, 15); + g.fillOval(_startPosX + 75, _startPosY + 45, 15, 15); + break; + case 4: + // Отрисовка 4 катков + g.setColor(Color.BLACK); + g.drawOval(_startPosX + 30, _startPosY + 42, 15, 15); + g.fillOval(_startPosX + 30, _startPosY + 42, 15, 15); + g.drawOval(_startPosX + 50, _startPosY + 45, 15, 15); + g.fillOval(_startPosX + 50, _startPosY + 45, 15, 15); + g.drawOval(_startPosX + 75, _startPosY + 45, 15, 15); + g.fillOval(_startPosX + 75, _startPosY + 45, 15, 15); + g.drawOval(_startPosX + 95, _startPosY + 42, 15, 15); + g.fillOval(_startPosX + 95, _startPosY + 42, 15, 15); + break; + } + } +} diff --git a/Tank/src/EntityTank.java b/Tank/src/EntityTank.java new file mode 100644 index 0000000..37840a1 --- /dev/null +++ b/Tank/src/EntityTank.java @@ -0,0 +1,27 @@ +import java.awt.*; +import java.util.Random; + +public class EntityTank { + private int Speed; + public int getSpeed() { + return Speed; + } + private float Weight; + public float getWeight() { + return Weight; + } + private Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + public float Step; + + public void Init(int speed, float weight, Color bodyColor){ + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(50)+10 : speed; + Weight = weight <= 0 ? rnd.nextInt(100)+500 : weight; + BodyColor = bodyColor; + Step = Speed * 600 / (int)Weight; + } +} + diff --git a/Tank/src/FormTank.java b/Tank/src/FormTank.java new file mode 100644 index 0000000..f094dbe --- /dev/null +++ b/Tank/src/FormTank.java @@ -0,0 +1,135 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +public class FormTank extends JFrame{ + private int Width; + private int Height; + + JPanel BottomPanel = new JPanel(); + JPanel CreatePanel = new JPanel(); + JPanel BottomAndCreatePanel = new JPanel(); + JPanel DimentionPanel = new JPanel(); + JPanel UpPanel = new JPanel(); + JPanel DownPanel = new JPanel(); + JPanel LRPanel = new JPanel(); + + DrawingField field = new DrawingField(this); + + JButton ButtonCreate=new JButton("Создать"); + Icon iconUp = new ImageIcon("Resources/KeyUp.png"); + JButton ButtonUp=new JButton(iconUp); + + Icon iconDown = new ImageIcon("Resources/KeyDown.png"); + JButton ButtonDown=new JButton(iconDown); + + Icon iconRight = new ImageIcon("Resources/KeyRight.png"); + JButton ButtonRight=new JButton(iconRight); + + Icon iconLeft = new ImageIcon("Resources/KeyLeft.png"); + JButton ButtonLeft=new JButton(iconLeft); + public FormTank(){ + super("Tank"); + setSize(800,600); + Width=getWidth(); + Height=getHeight(); + ShowWindow(); + RefreshWindow(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } + + public void ShowWindow(){ + + Dimension dimen=new Dimension(30,30); + + // Обработка нажатия кнопки + ButtonUp.setPreferredSize(dimen); + ButtonUp.addActionListener(e->{ + field.UpButtonAction(); + repaint(); + }); + + // Обработка нажатия кнопки + ButtonDown.setPreferredSize(dimen); + ButtonDown.addActionListener(e->{ + field.DownButtonAction(); + repaint(); + }); + + // Обработка нажатия кнопки + ButtonRight.setPreferredSize(dimen); + ButtonRight.addActionListener(e->{ + field.RightButtonAction(); + repaint(); + }); + + // Обработка нажатия кнопки + ButtonLeft.setPreferredSize(dimen); + ButtonLeft.addActionListener(e->{ + field.LeftButtonAction(); + repaint(); + }); + + // Добавление кнопок на панель (Левая и правая стрелки) + LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0)); + LRPanel.setBackground(new Color(0,0,0,0)); + LRPanel.add(ButtonLeft); + LRPanel.add(ButtonRight); + + // Добавление кнопки (Стрелка вверх) + UpPanel.setLayout(new FlowLayout()); + UpPanel.setBackground(new Color(0,0,0,0)); + UpPanel.add(ButtonUp); + + // Добавление кнопки (Стрелка вниз) + DownPanel.setLayout(new FlowLayout()); + DownPanel.setBackground(new Color(0,0,0,0)); + DownPanel.add(ButtonDown); + + DimentionPanel.setLayout(new BoxLayout(DimentionPanel,BoxLayout.Y_AXIS)); + DimentionPanel.setBackground(new Color(0,0,0,0)); + DimentionPanel.add(UpPanel); + DimentionPanel.add(LRPanel); + DimentionPanel.add(DownPanel); + add(DimentionPanel); + + // нажатие кнопки + CreatePanel.setLayout(new FlowLayout()); + CreatePanel.setBackground(new Color(0,0,0,0)); + CreatePanel.add(ButtonCreate); + ButtonCreate.addActionListener(e->{ + field.CreateButtonAction(); + repaint(); + }); + + BottomPanel.setLayout(new FlowLayout()); + BottomPanel.setBackground(new Color(0,0,0,0)); + + BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel,BoxLayout.Y_AXIS)); + BottomAndCreatePanel.setBackground(new Color(0,0,0,0)); + BottomAndCreatePanel.add(CreatePanel); + BottomAndCreatePanel.add(BottomPanel); + + add(BottomAndCreatePanel); + add(field); + + addComponentListener(new ComponentAdapter() { + @Override + public void componentResized(ComponentEvent e) { + super.componentResized(e); + Width=getWidth(); + Height=getHeight(); + + field.ResizeField(); + repaint(); + RefreshWindow(); + } + }); + } + public void RefreshWindow(){ + field.setBounds(0,0,Width,Height); + BottomAndCreatePanel.setBounds(-320,Height-110,Width,80); + DimentionPanel.setBounds(Width-170,Height-170,190,140); + } +} diff --git a/Tank/src/Main.java b/Tank/src/Main.java new file mode 100644 index 0000000..7c7e57b --- /dev/null +++ b/Tank/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args){ + new FormTank(); + } +}