This commit is contained in:
dyakonovr 2023-11-18 19:17:07 +04:00
parent 10179a381c
commit c26db1d45e
11 changed files with 365 additions and 2 deletions

34
.gitignore vendored
View File

@ -1,3 +1,36 @@
### IntelliJ IDEA ###
.idea/
out/
!**/src/main/**/out/
!**/src/test/**/out/
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
@ -23,4 +56,3 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

BIN
resources/DownButton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B

BIN
resources/LeftButton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 B

BIN
resources/RightButton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

BIN
resources/UpButton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 B

6
src/DirectionType.java Normal file
View File

@ -0,0 +1,6 @@
public enum DirectionType {
Up,
Down,
Left,
Right
}

131
src/DrawningTank.java Normal file
View File

@ -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);
}
}
}

54
src/DrawningWheels.java Normal file
View File

@ -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));
}
}

30
src/EntityTank.java Normal file
View File

@ -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;
}
}

105
src/Main.java Normal file
View File

@ -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);
}
}

View File

@ -0,0 +1,5 @@
public enum WheelQuantityType {
Four,
Five,
Six
}