diff --git a/.gitignore b/.gitignore index 9154f4c..3e03c33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,36 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +.idea +ProjectTankHard.iml + +### 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 + # ---> Java # Compiled class file *.class @@ -22,5 +55,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* -replay_pid* - +replay_pid* \ No newline at end of file diff --git a/resources/DownButton.png b/resources/DownButton.png new file mode 100644 index 0000000..47ad2f8 Binary files /dev/null and b/resources/DownButton.png differ diff --git a/resources/LeftButton.png b/resources/LeftButton.png new file mode 100644 index 0000000..bf2c7bf Binary files /dev/null and b/resources/LeftButton.png differ diff --git a/resources/RightButton.png b/resources/RightButton.png new file mode 100644 index 0000000..4161f84 Binary files /dev/null and b/resources/RightButton.png differ diff --git a/resources/UpButton.png b/resources/UpButton.png new file mode 100644 index 0000000..7c0cae0 Binary files /dev/null and b/resources/UpButton.png differ diff --git a/src/DirectionType.java b/src/DirectionType.java new file mode 100644 index 0000000..35657f0 --- /dev/null +++ b/src/DirectionType.java @@ -0,0 +1,6 @@ +public enum DirectionType { + Up, + Down, + Left, + Right +} diff --git a/src/DrawningTank.java b/src/DrawningTank.java new file mode 100644 index 0000000..5009783 --- /dev/null +++ b/src/DrawningTank.java @@ -0,0 +1,131 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.geom.Ellipse2D; +import java.awt.geom.RoundRectangle2D; + +public class DrawningTank { + JPanel TankPanel; + private EntityTank EntityTank; + private int _pictureWidth; + private int _pictureHeight; + private int _startPosX = 0; + private int _startPosY = 0; + private int _tankWidth = 150; + private int _tankHeight = 100; + private DrawningWheels DrawningWheels; + public EntityTank EntityTank(){ + return EntityTank; + } + + public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, int wheelQuantity, + int width, int height, boolean isAntiAircraftGun, boolean isTankTower, JPanel tankPanel){ + if(width <= _tankWidth || height <= _tankHeight) + return false; + + tankPanel.setSize(width, height); + TankPanel = tankPanel; + tankPanel.paint(TankPanel.getGraphics()); + _pictureWidth = width; + _pictureHeight = height; + + EntityTank = new EntityTank(); + EntityTank.Init(speed, weight, bodyColor, additionalColor, isAntiAircraftGun, isTankTower); + + DrawningWheels = new DrawningWheels(); + DrawningWheels.Init(_tankWidth, _tankHeight, _startPosX, _startPosY, additionalColor, tankPanel); + DrawningWheels.ChangeWheelsQuantity(wheelQuantity); + + return true; + } + + public void SetPosition(int x, int y){ + if (EntityTank == null) + return; + + _startPosX = x; + _startPosY = y; + + if (x + _tankWidth >= _pictureWidth || y + _tankHeight >= _pictureHeight){ + _startPosX = 0; + _startPosY = 0; + } + + DrawningWheels.currentX = _startPosX; + DrawningWheels.currentY = _startPosY; + } + public void MoveTransport(DirectionType direction){ + if (EntityTank == null) + return; + TankPanel.paint(TankPanel.getGraphics()); + switch (direction) + { + case Left: + if (_startPosX - EntityTank.Step() >= 0) + _startPosX -= (int)EntityTank.Step(); + else + _startPosX = 0; + break; + case Up: + if (_startPosY - EntityTank.Step() >= 0) + _startPosY -= (int)EntityTank.Step(); + else + _startPosY = 0; + break; + case Right: + if (_startPosX + EntityTank.Step() + _tankWidth < _pictureWidth) + _startPosX += (int)EntityTank.Step(); + else + _startPosX = _pictureWidth - _tankWidth; + break; + case Down: + if (_startPosY + EntityTank.Step() + _tankHeight < _pictureHeight) + _startPosY += (int)EntityTank.Step(); + else + _startPosY = _pictureHeight - _tankHeight; + break; + } + + DrawningWheels.currentX = _startPosX; + DrawningWheels.currentY = _startPosY; + } + + public void DrawTank() { + Graphics2D g2d = (Graphics2D) TankPanel.getGraphics(); + if (EntityTank == null) return; + + g2d.setColor(EntityTank.BodyColor()); + + // гусеница + RoundRectangle2D caterpillars = new RoundRectangle2D.Double(_startPosX, _startPosY + 65, _tankWidth, 35, 20, 20); + g2d.draw(caterpillars); + + // колеса + DrawningWheels.DrawWheels(); + + // дуло + if (EntityTank.IsTankTower()) { + g2d.fillRect(_startPosX + 15, _startPosY + 37, 30, 7); + } + + // башня + g2d.setColor(EntityTank.AdditionalColor()); + g2d.fillRect(_startPosX + 45, _startPosY + 30, 60, 25); + g2d.fillRect(_startPosX + 5, _startPosY + 55 + 1, _tankWidth - 10, 9); + + if (EntityTank.IsAntiAirforceGun()) { + // зенитное орудие + g2d.drawRect(_startPosX + 65, _startPosY + 18, 8, 12); + g2d.drawRect(_startPosX + 65 + 8, _startPosY + 16, 8, 14); + + int[] xLeft = {_startPosX + 52, _startPosX + 67, _startPosX + 67 + 5, _startPosX + 57}; + int[] yLeft = {_startPosY + 5, _startPosY + 18, _startPosY + 18, _startPosY + 5}; + + g2d.drawPolygon(xLeft, yLeft, xLeft.length); + + int[] xRight = {_startPosX + 59, _startPosX + 74, _startPosX + 74 + 5, _startPosX + 66}; + int[] yRight = {_startPosY, _startPosY + 16, _startPosY + 16, _startPosY}; + + g2d.drawPolygon(xRight, yRight, xRight.length); + } + } +} diff --git a/src/DrawningWheels.java b/src/DrawningWheels.java new file mode 100644 index 0000000..aa863c1 --- /dev/null +++ b/src/DrawningWheels.java @@ -0,0 +1,54 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.geom.Ellipse2D; + +public class DrawningWheels { + JPanel TankPanel; + private WheelQuantityType WheelsQuantity; + private Color Color; + private int Width, Height; + public int currentX, currentY; + boolean Init(int width, int height, int x, int y, Color color, JPanel tankPanel){ + Width = width; + Height = height; + currentX = x; + currentY = y; + Color = color; + TankPanel = tankPanel; + return true; + } + public void ChangeWheelsQuantity(int x){ + if(x <= 4) + WheelsQuantity = WheelsQuantity.Four; + if(x == 5) + WheelsQuantity = WheelsQuantity.Five; + if(x >= 6) + WheelsQuantity = WheelsQuantity.Six; + } + public void DrawWheels(){ + Graphics2D g2d = (Graphics2D)TankPanel.getGraphics(); + g2d.setColor(Color); + + g2d.fill(new Ellipse2D.Double(currentX + 1, currentY + Height - 5 - 25, 25, 25)); + + if (WheelsQuantity == WheelQuantityType.Four) { + for (int i = 1; i <= 2; i++) { + g2d.fill(new Ellipse2D.Double(currentX + 40 + (5 * i) + (35 * (i - 1)), currentY + Height - 5 - 17, 20, 20)); + } + } + + if (WheelsQuantity == WheelQuantityType.Five) { + for (int i = 1; i <= 3; i++) { + g2d.fill(new Ellipse2D.Double(currentX + 30 + (5 * i) + (25 * (i - 1)), currentY + Height - 5 - 20, 20, 20)); + } + } + + if (WheelsQuantity == WheelQuantityType.Six) { + for (int i = 1; i <= 4; i++) { + g2d.fill(new Ellipse2D.Double(currentX + 30 + (5 * i) + (15 * (i - 1)), currentY + Height - 5 - 15, 15, 15)); + } + } + + g2d.fill(new Ellipse2D.Double(currentX + Width - 25 - 1, currentY + Height - 5 - 25, 25, 25)); + } +} \ No newline at end of file diff --git a/src/EntityTank.java b/src/EntityTank.java new file mode 100644 index 0000000..c18ef8b --- /dev/null +++ b/src/EntityTank.java @@ -0,0 +1,30 @@ +import java.awt.*; + +public class EntityTank { + private int Speed; + private double Weight, Step; + private Color BodyColor, AdditionalColor; + private boolean IsAntiAirforceGun; + private boolean IsTankTower; + public double Step(){ + return Step; + } + public Color BodyColor(){ + return BodyColor; + } + public Color AdditionalColor(){ + return AdditionalColor; + } + public boolean IsAntiAirforceGun(){return IsAntiAirforceGun;} + public boolean IsTankTower(){return IsTankTower;} + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, + boolean isAntiAirforceGun, boolean isTankTower){ + Speed = speed; + Weight = weight; + Step = (double)Speed * 100 / Weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + IsAntiAirforceGun = isAntiAirforceGun; + IsTankTower = isTankTower; + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..9ef5134 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,105 @@ +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.Random; +import javax.imageio.ImageIO; +import javax.swing.*; + +public class Main { + public static void main(String[] args) throws IOException { + JFrame TankFrame = new JFrame(); + JPanel TankPanel = new JPanel(); + + TankFrame.setLayout(new BorderLayout()); + TankFrame.setSize(900, 500); + TankFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + TankFrame.setLayout(new BorderLayout(1,1)); + + DrawningTank DrawningTank = new DrawningTank(); + + TankPanel.setLayout(null); + + BufferedImage RightIcon = ImageIO.read(new File("resources/RightButton.png")); + BufferedImage LeftIcon = ImageIO.read(new File("resources/LeftButton.png")); + BufferedImage UpIcon = ImageIO.read(new File("resources/UpButton.png")); + BufferedImage DownIcon = ImageIO.read(new File("resources/DownButton.png")); + + JButton RightButton = new JButton(new ImageIcon(RightIcon)); + JButton LeftButton = new JButton(new ImageIcon(LeftIcon)); + JButton UpButton = new JButton(new ImageIcon(UpIcon)); + JButton DownButton = new JButton(new ImageIcon(DownIcon)); + JButton CreateButton = new JButton("Создать"); + + CreateButton.setBounds(12, 401, 90, 40); + RightButton.setBounds(840,411,30,30); + LeftButton.setBounds(768,411,30,30); + UpButton.setBounds(804,375,30,30); + DownButton.setBounds(804,411,30,30); + + TankPanel.add(CreateButton); + TankPanel.add(RightButton); + TankPanel.add(LeftButton); + TankPanel.add(UpButton); + TankPanel.add(DownButton); + TankFrame.add(TankPanel, BorderLayout.CENTER); + + Random random = new Random(); + CreateButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + DrawningTank.Init(random.nextInt(300 - 100 + 1) + 100, random.nextDouble() * (3000 - 1000) + 1000, + Color.getHSBColor(random.nextInt(302), random.nextInt(302), random.nextInt(302)), + Color.getHSBColor(random.nextInt(302), random.nextInt(302), random.nextInt(302)), + random.nextInt(6 - 4 + 1) + 4, TankPanel.getWidth(), TankPanel.getHeight(), + random.nextBoolean(), random.nextBoolean(), TankPanel); + DrawningTank.SetPosition(random.nextInt(100 - 10 + 1) + 10, random.nextInt(100 - 10 + 1) + 10); + DrawningTank.DrawTank(); + } + }); + + RightButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (DrawningTank.EntityTank() == null) + return; + DrawningTank.MoveTransport(DirectionType.Right); + DrawningTank.DrawTank(); + } + }); + + LeftButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (DrawningTank.EntityTank() == null) + return; + DrawningTank.MoveTransport(DirectionType.Left); + DrawningTank.DrawTank(); + } + }); + + UpButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (DrawningTank.EntityTank() == null) + return; + DrawningTank.MoveTransport(DirectionType.Up); + DrawningTank.DrawTank(); + } + }); + + DownButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (DrawningTank.EntityTank() == null) + return; + DrawningTank.MoveTransport(DirectionType.Down); + DrawningTank.DrawTank(); + } + }); + + TankFrame.setVisible(true); + } +} diff --git a/src/WheelQuantityType.java b/src/WheelQuantityType.java new file mode 100644 index 0000000..3d1fcbb --- /dev/null +++ b/src/WheelQuantityType.java @@ -0,0 +1,5 @@ +public enum WheelQuantityType { + Four, + Five, + Six +}