PIbd-21 Belianin N.N. LabWork01 #1
29
Tank/.gitignore
vendored
Normal file
29
Tank/.gitignore
vendored
Normal file
@ -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
|
8
Tank/.idea/.gitignore
vendored
Normal file
8
Tank/.idea/.gitignore
vendored
Normal file
@ -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
|
6
Tank/.idea/misc.xml
Normal file
6
Tank/.idea/misc.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
Tank/.idea/modules.xml
Normal file
8
Tank/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Tank.iml" filepath="$PROJECT_DIR$/Tank.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
Tank/.idea/vcs.xml
Normal file
6
Tank/.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
BIN
Tank/Resources/KeyDown.png
Normal file
BIN
Tank/Resources/KeyDown.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
BIN
Tank/Resources/KeyLeft.png
Normal file
BIN
Tank/Resources/KeyLeft.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
BIN
Tank/Resources/KeyRight.png
Normal file
BIN
Tank/Resources/KeyRight.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
BIN
Tank/Resources/KeyUp.png
Normal file
BIN
Tank/Resources/KeyUp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
11
Tank/Tank.iml
Normal file
11
Tank/Tank.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
14
Tank/src/CountWheels.java
Normal file
14
Tank/src/CountWheels.java
Normal file
@ -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; }
|
||||
}
|
6
Tank/src/Direction.java
Normal file
6
Tank/src/Direction.java
Normal file
@ -0,0 +1,6 @@
|
||||
public enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
54
Tank/src/DrawingField.java
Normal file
54
Tank/src/DrawingField.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
142
Tank/src/DrawingTank.java
Normal file
142
Tank/src/DrawingTank.java
Normal file
@ -0,0 +1,142 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
50
Tank/src/DrawingWheels.java
Normal file
50
Tank/src/DrawingWheels.java
Normal file
@ -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);
|
||||
eegov
commented
Имеется дублирующийся код Имеется дублирующийся код
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
28
Tank/src/EntityTank.java
Normal file
28
Tank/src/EntityTank.java
Normal file
@ -0,0 +1,28 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
135
Tank/src/FormTank.java
Normal file
135
Tank/src/FormTank.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
5
Tank/src/Main.java
Normal file
5
Tank/src/Main.java
Normal file
@ -0,0 +1,5 @@
|
||||
public class Main {
|
||||
public static void main(String[] args){
|
||||
new FormTank();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Имеется дублирующийся код