PIbd-23 Dyakonov R.R. Lab. 2 Hard #2

Closed
dyakonovr wants to merge 2 commits from lab2 into lab1
23 changed files with 869 additions and 273 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ out/
.idea
ProjectTankHard.iml
PIbd-23_Dyakonov_R_R_Tank_Hard.iml
### Eclipse ###
.apt_generated

View File

@ -1,131 +0,0 @@
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);
}
}
}

View File

@ -1,30 +0,0 @@
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;
}
}

View File

@ -1,105 +0,0 @@
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

@ -1,3 +1,5 @@
package ProjectTankHard;
public enum DirectionType {
Up,
Down,

View File

@ -0,0 +1,47 @@
package ProjectTankHard.DrawningObjects;
import ProjectTankHard.Entities.EntityTank;
import java.awt.*;
public class DrawningTank extends DrawningTankBase {
public DrawningTank(int speed, double weight, Color bodyColor, Color additionalColor,
boolean isAntiAirforceGun, boolean isTankTower, int width, int height){
super(speed, weight, bodyColor, width, height);
if(EntityTankBase() != null){
EntityTankBase = new EntityTank(speed, weight, bodyColor, additionalColor, isAntiAirforceGun, isTankTower);
}
}
@Override
public void DrawTank(Graphics2D g2d){
if (!(EntityTankBase instanceof EntityTank)) return;
super.DrawTank(g2d);
EntityTank _tank = (EntityTank) EntityTankBase;
g2d.setColor(_tank.AdditionalColor());
// дуло
if (_tank.IsTankTower()) {
g2d.fillRect(_startPosX + 15, _startPosY + 37, 30, 7);
}
if (_tank.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);
}
}
}

View File

@ -0,0 +1,132 @@
package ProjectTankHard.DrawningObjects;
import ProjectTankHard.DirectionType;
import ProjectTankHard.Entities.EntityTankBase;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.util.Random;
public class DrawningTankBase {
protected EntityTankBase EntityTankBase;
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX = 0;
protected int _startPosY = 0;
private int _tankWidth = 150;
private int _tankHeight = 100;
protected IDraw DrawningWheels;
public EntityTankBase EntityTankBase(){
return EntityTankBase;
}
public DrawningTankBase(int speed, double weight, Color bodyColor, int width, int height){
if(width <= _tankWidth || height <= _tankHeight) return;
_pictureWidth = width;
_pictureHeight = height;
EntityTankBase = new EntityTankBase(speed, weight, bodyColor);
Random rand = new Random();
int variant = rand.nextInt(0, 2 + 1);
switch (variant) {
case 0:
DrawningWheels = new DrawningWheels(_tankWidth, _tankHeight, _startPosX, _startPosY, bodyColor);
break;
case 1:
DrawningWheels = new DrawningWheelsOrn(_tankWidth, _tankHeight, _startPosX, _startPosY, bodyColor);
break;
case 2:
DrawningWheels = new DrawningWheelsOrnSecond(_tankWidth, _tankHeight, _startPosX, _startPosY, bodyColor);
break;
}
DrawningWheels.ChangeWheelsQuantity(rand.nextInt(4, 6 + 1));
}
public void SetPosition(int x, int y){
if (EntityTankBase == null)
return;
_startPosX = x;
_startPosY = y;
if (x + _tankWidth >= _pictureWidth || y + _tankHeight >= _pictureHeight || x < 0 || y < 0){
_startPosX = 0;
_startPosY = 0;
}
DrawningWheels.ChangeX(_startPosX);
DrawningWheels.ChangeY(_startPosY);
}
public int GetPosX(){return _startPosX;}
public int GetPosY(){return _startPosY;}
public int GetWidth(){return _tankWidth;}
public int GetHeight(){return _tankHeight;}
public boolean CanMove(DirectionType direction)
{
if (EntityTankBase == null)
return false;
switch (direction)
{
case Left:
return _startPosX - EntityTankBase.Step() >= 0;
case Right:
return _startPosX + EntityTankBase.Step() + _tankWidth< _pictureWidth;
case Down:
return _startPosY + EntityTankBase.Step() + _tankHeight < _pictureHeight;
case Up:
return _startPosY - EntityTankBase.Step() >= 0;
};
return false;
}
public void MoveTransport(DirectionType direction){
if (!CanMove(direction) || EntityTankBase == null) return;
switch (direction)
{
case Left:
if (_startPosX - EntityTankBase.Step() >= 0)
_startPosX -= (int) EntityTankBase.Step();
break;
case Up:
if (_startPosY - EntityTankBase.Step() >= 0)
_startPosY -= (int) EntityTankBase.Step();
break;
case Right:
if (_startPosX + EntityTankBase.Step() + _tankWidth <= _pictureWidth)
_startPosX += (int) EntityTankBase.Step();
break;
case Down:
if (_startPosY + EntityTankBase.Step() + _tankHeight <= _pictureHeight)
_startPosY += (int) EntityTankBase.Step();
break;
}
DrawningWheels.ChangeX(_startPosX);
DrawningWheels.ChangeY(_startPosY);
}
public void DrawTank(Graphics2D g2d) {
if (EntityTankBase == null) return;
g2d.setColor(EntityTankBase.BodyColor());
// гусеница
RoundRectangle2D caterpillars = new RoundRectangle2D.Double(_startPosX, _startPosY + 65, _tankWidth, 35, 20, 20);
g2d.draw(caterpillars);
// колеса
DrawningWheels.DrawWheels(g2d);
// башня
g2d.setColor(EntityTankBase.BodyColor());
g2d.fillRect(_startPosX + 45, _startPosY + 30, 60, 25);
g2d.fillRect(_startPosX + 5, _startPosY + 55 + 1, _tankWidth - 10, 9);
}
}

View File

@ -1,21 +1,28 @@
package ProjectTankHard.DrawningObjects;
import ProjectTankHard.WheelQuantityType;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class DrawningWheels {
JPanel TankPanel;
public class DrawningWheels implements IDraw {
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){
public DrawningWheels(int width, int height, int x, int y, Color color){
Width = width;
Height = height;
currentX = x;
currentY = y;
Color = color;
TankPanel = tankPanel;
return true;
}
public void ChangeX(int x){
currentX = x;
}
public void ChangeY(int y){
currentY = y;
}
public void ChangeWheelsQuantity(int x){
if(x <= 4)
@ -25,8 +32,7 @@ public class DrawningWheels {
if(x >= 6)
WheelsQuantity = WheelsQuantity.Six;
}
public void DrawWheels(){
Graphics2D g2d = (Graphics2D)TankPanel.getGraphics();
public void DrawWheels(Graphics2D g2d){
g2d.setColor(Color);
g2d.fill(new Ellipse2D.Double(currentX + 1, currentY + Height - 5 - 25, 25, 25));

View File

@ -0,0 +1,90 @@
package ProjectTankHard.DrawningObjects;
import ProjectTankHard.WheelQuantityType;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class DrawningWheelsOrn implements IDraw {
private WheelQuantityType WheelsQuantity;
private Color Color;
private int Width, Height;
public int currentX, currentY;
public DrawningWheelsOrn(int width, int height, int x, int y, Color color){
Width = width;
Height = height;
currentX = x;
currentY = y;
Color = color;
}
public void ChangeX(int x){
currentX = x;
}
public void ChangeY(int y){
currentY = y;
}
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){
g2d.setColor(Color);
g2d.fill(new Ellipse2D.Double(currentX + 1, currentY + Height - 5 - 25, 25, 25));
DrawOrnament(g2d, currentX + 1, currentY + Height - 5 - 25, 25);
int x, y;
int diameter = 20;
if (WheelsQuantity == WheelQuantityType.Four) {
for (int i = 1; i <= 2; i++) {
x = currentX + 40 + (5 * i) + (35 * (i - 1));
y = currentY + Height - 5 - 17;
g2d.fill(new Ellipse2D.Double(x, y, diameter, diameter));
DrawOrnament(g2d, x, y, diameter);
}
}
if (WheelsQuantity == WheelQuantityType.Five) {
for (int i = 1; i <= 3; i++) {
x = currentX + 30 + (5 * i) + (25 * (i - 1));
y = currentY + Height - 5 - 20;
g2d.fill(new Ellipse2D.Double(x, y, diameter, diameter));
DrawOrnament(g2d, x, y, diameter);
}
}
if (WheelsQuantity == WheelQuantityType.Six) {
diameter = 15;
for (int i = 1; i <= 4; i++) {
x = currentX + 30 + (5 * i) + (15 * (i - 1));
y = currentY + Height - 5 - 15;
g2d.fill(new Ellipse2D.Double(x, y, diameter, diameter));
DrawOrnament(g2d, x, y, diameter);
}
}
g2d.fill(new Ellipse2D.Double(currentX + Width - 25 - 1, currentY + Height - 5 - 25, 25, 25));
DrawOrnament(g2d, currentX + Width - 25 - 1, currentY + Height - 5 - 25, 25);
}
private void DrawOrnament(Graphics2D g2d, int x, int y, int diameter) {
int middleX = x + (diameter / 2);
int middleY = y + (diameter / 2);
g2d.setColor(Color.BLACK);
g2d.drawLine(middleX, y, middleX, y + diameter);
g2d.drawLine(x, middleY, x + diameter, middleY);
g2d.setColor(Color);
}
}

View File

@ -0,0 +1,90 @@
package ProjectTankHard.DrawningObjects;
import ProjectTankHard.WheelQuantityType;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class DrawningWheelsOrnSecond implements IDraw {
private WheelQuantityType WheelsQuantity;
private Color Color;
private int Width, Height;
public int currentX, currentY;
public DrawningWheelsOrnSecond(int width, int height, int x, int y, Color color){
Width = width;
Height = height;
currentX = x;
currentY = y;
Color = color;
}
public void ChangeX(int x){
currentX = x;
}
public void ChangeY(int y){
currentY = y;
}
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){
g2d.setColor(Color);
g2d.fill(new Ellipse2D.Double(currentX + 1, currentY + Height - 5 - 25, 25, 25));
DrawOrnament(g2d, currentX + 1, currentY + Height - 5 - 25, 25);
int x, y;
int diameter = 20;
if (WheelsQuantity == WheelQuantityType.Four) {
for (int i = 1; i <= 2; i++) {
x = currentX + 40 + (5 * i) + (35 * (i - 1));
y = currentY + Height - 5 - 17;
g2d.fill(new Ellipse2D.Double(x, y, diameter, diameter));
DrawOrnament(g2d, x, y, diameter);
}
}
if (WheelsQuantity == WheelQuantityType.Five) {
for (int i = 1; i <= 3; i++) {
x = currentX + 30 + (5 * i) + (25 * (i - 1));
y = currentY + Height - 5 - 20;
g2d.fill(new Ellipse2D.Double(x, y, diameter, diameter));
DrawOrnament(g2d, x, y, diameter);
}
}
if (WheelsQuantity == WheelQuantityType.Six) {
diameter = 15;
for (int i = 1; i <= 4; i++) {
x = currentX + 30 + (5 * i) + (15 * (i - 1));
y = currentY + Height - 5 - 15;
g2d.fill(new Ellipse2D.Double(x, y, diameter, diameter));
DrawOrnament(g2d, x, y, diameter);
}
}
g2d.fill(new Ellipse2D.Double(currentX + Width - 25 - 1, currentY + Height - 5 - 25, 25, 25));
DrawOrnament(g2d, currentX + Width - 25 - 1, currentY + Height - 5 - 25, 25);
}
private void DrawOrnament(Graphics2D g2d, int x, int y, int diameter) {
int centerX = x + diameter / 2;
int centerY = y + diameter / 2;
g2d.setColor(Color.RED);
g2d.fill(new Ellipse2D.Double(centerX - 8 / 2, centerY - 8 / 2, 8, 8));
/*g2d.setColor(Color.BLACK);
g2d.fillRect(centerX - 1, centerY - 1, 3, 3);*/
g2d.setColor(Color);
}
}

View File

@ -0,0 +1,10 @@
package ProjectTankHard.DrawningObjects;
import java.awt.*;
public interface IDraw {
public void ChangeWheelsQuantity(int x);
public void DrawWheels(Graphics2D g2d);
public void ChangeX(int x);
public void ChangeY(int y);
}

View File

@ -0,0 +1,18 @@
package ProjectTankHard.Entities;
import java.awt.*;
public class EntityTank extends EntityTankBase {
private Color AdditionalColor;
private boolean IsAntiAirforceGun;
private boolean IsTankTower;
public Color AdditionalColor(){return AdditionalColor;};
public boolean IsAntiAirforceGun(){return IsAntiAirforceGun;};
public boolean IsTankTower(){return IsTankTower;};
public EntityTank(int speed, double weight, Color bodyColor, Color additionalColor, boolean isAntiAirforceGun, boolean isTankTower) {
super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
IsAntiAirforceGun = isAntiAirforceGun;
IsTankTower = isTankTower;
}
}

View File

@ -0,0 +1,21 @@
package ProjectTankHard.Entities;
import java.awt.*;
public class EntityTankBase {
private int Speed;
private double Weight, Step;
private Color BodyColor;
public double Step(){
return Step;
}
public Color BodyColor(){
return BodyColor;
}
public EntityTankBase(int speed, double weight, Color bodyColor) {
Speed = speed;
Weight = weight;
Step = (double)Speed * 100 / Weight;
BodyColor = bodyColor;
}
}

View File

@ -0,0 +1,191 @@
package ProjectTankHard;
import ProjectTankHard.DrawningObjects.DrawningTank;
import ProjectTankHard.DrawningObjects.DrawningTankBase;
import ProjectTankHard.DrawningObjects.DrawningWheels;
import ProjectTankHard.MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormTank {
public DrawningTankBase DrawningTankBase;
private AbstractStrategy _abstractStrategy;
Canvas canv;
static int pictureBoxWidth = 882;
static int pictureBoxHeight = 453;
public void Draw(){
canv.repaint();
}
public FormTank() {
JFrame TankFrame = new JFrame();
JButton RightButton = new JButton(new ImageIcon("resources/RightButton.png"));
JButton LeftButton = new JButton(new ImageIcon("resources/LeftButton.png"));
JButton UpButton = new JButton(new ImageIcon("resources/UpButton.png"));
JButton DownButton = new JButton(new ImageIcon("resources/DownButton.png"));
JButton CreateTankBaseButton = new JButton("Создать (прост.)");
JButton CreateTankButton = new JButton("Создать (продв.)");
JButton StepButton = new JButton("Шаг");
JComboBox StrategyComboBox = new JComboBox(new String[]{ "Довести до центра", "Довести до края"});
TankFrame.setSize (900, 500);
TankFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
TankFrame.setLayout(null);
canv = new Canvas();
canv.setSize(pictureBoxWidth, pictureBoxHeight);
CreateTankBaseButton.setBounds(198, 401, 180, 40);
CreateTankButton.setBounds(12, 401, 180, 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);
StrategyComboBox.setBounds(719,12,151,28);
StepButton.setBounds(768, 46, 94, 29);
TankFrame.add(canv);
TankFrame.add(CreateTankBaseButton);
TankFrame.add(CreateTankButton);
TankFrame.add(RightButton);
TankFrame.add(LeftButton);
TankFrame.add(UpButton);
TankFrame.add(DownButton);
TankFrame.add(StrategyComboBox);
TankFrame.add(StepButton);
StepButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (DrawningTankBase == null) return;
if (StrategyComboBox.isEnabled())
{
switch (StrategyComboBox.getSelectedIndex())
{
case 0:
_abstractStrategy = new MoveToCenter();
break;
case 1:
_abstractStrategy = new MoveToBorder();
break;
default:
_abstractStrategy = null;
break;
};
if (_abstractStrategy == null) return;
_abstractStrategy.SetData(new DrawningObjectTank(DrawningTankBase), pictureBoxWidth, pictureBoxHeight);
StrategyComboBox.setEnabled(false);
}
if (_abstractStrategy == null) return;
_abstractStrategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
StrategyComboBox.setEnabled(true);
_abstractStrategy = null;
}
}
}
);
Random random = new Random();
CreateTankBaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningTankBase = new DrawningTankBase(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
pictureBoxWidth, pictureBoxHeight);
DrawningTankBase.SetPosition(random.nextInt(0, 100), random.nextInt(0, 100));
canv.DrawningTankBase = DrawningTankBase;
StrategyComboBox.enable(true);
Draw();
}
});
CreateTankButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningTankBase = new DrawningTank(random.nextInt(100, 300), random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
random.nextBoolean(), random.nextBoolean(), pictureBoxWidth, pictureBoxHeight);
DrawningTankBase.SetPosition(random.nextInt(0, 100), random.nextInt(0, 100));
canv.DrawningTankBase = DrawningTankBase;
StrategyComboBox.enable(true);
Draw();
}
});
RightButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (DrawningTankBase.EntityTankBase() == null)
return;
DrawningTankBase.MoveTransport(DirectionType.Right);
Draw();
}
});
LeftButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (DrawningTankBase.EntityTankBase() == null)
return;
DrawningTankBase.MoveTransport(DirectionType.Left);
Draw();
}
});
UpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (DrawningTankBase.EntityTankBase() == null)
return;
DrawningTankBase.MoveTransport(DirectionType.Up);
Draw();
}
});
DownButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (DrawningTankBase.EntityTankBase() == null)
return;
DrawningTankBase.MoveTransport(DirectionType.Down);
Draw();
}
});
TankFrame.setVisible(true);
}
}
class Canvas extends JComponent{
public DrawningTankBase DrawningTankBase;
public Canvas(){
}
public void paintComponent (Graphics g){
if (DrawningTankBase == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
DrawningTankBase.DrawTank(g2d);
super.repaint();
}
}

View File

@ -0,0 +1,9 @@
package ProjectTankHard;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
FormTank form = new FormTank();
}
}

View File

@ -0,0 +1,74 @@
package ProjectTankHard.MovementStrategy;
import ProjectTankHard.DirectionType;
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private Status _state = Status.NotInit;
private int FieldWidth;
protected int FieldWidth(){return FieldWidth;}
private int FieldHeight;
protected int FieldHeight(){return FieldHeight;}
public Status GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = Status.NotInit;
return;
}
_state = Status.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != Status.InProgress)
{
return;
}
if (IsTargetDestination())
{
_state = Status.Finish;
return;
}
MoveToTarget();
}
protected boolean MoveLeft() {return MoveTo(DirectionType.Left);}
protected boolean MoveRight() {return MoveTo(DirectionType.Right);}
protected boolean MoveUp() {return MoveTo(DirectionType.Up);}
protected boolean MoveDown() {return MoveTo(DirectionType.Down);}
protected ObjectParameters GetObjectParameters(){
if(_moveableObject != null)
return _moveableObject.GetObjectParameters();
else return null;
}
protected Integer GetStep()
{
if (_state != Status.InProgress)
{
return null;
}
return _moveableObject.GetStep();
}
protected abstract void MoveToTarget();
protected abstract boolean IsTargetDestination();
private boolean MoveTo(DirectionType directionType) {
if (_state != Status.InProgress)
{
return false;
}
if (_moveableObject.CheckCanMove(directionType))
{
_moveableObject.MoveObject(directionType);
return true;
}
return false;
}
}

View File

@ -0,0 +1,34 @@
package ProjectTankHard.MovementStrategy;
import ProjectTankHard.DirectionType;
import ProjectTankHard.DrawningObjects.DrawningTankBase;
public class DrawningObjectTank implements IMoveableObject{
private final DrawningTankBase _drawningTankBase;
public DrawningObjectTank(DrawningTankBase drawningTankBase){
_drawningTankBase = drawningTankBase;
}
public ObjectParameters GetObjectParameters(){
if(_drawningTankBase == null || _drawningTankBase.EntityTankBase() == null)
return null;
return new ObjectParameters(_drawningTankBase.GetPosX(), _drawningTankBase.GetPosY(),
_drawningTankBase.GetWidth(), _drawningTankBase.GetHeight());
}
public int GetStep(){
if(_drawningTankBase.EntityTankBase() == null)
return 0;
return (int)_drawningTankBase.EntityTankBase().Step();
}
public boolean CheckCanMove(DirectionType direction){
if(_drawningTankBase == null)
return false;
return _drawningTankBase.CanMove(direction);
}
public void MoveObject(DirectionType direction){
if(_drawningTankBase == null)
return;
_drawningTankBase.MoveTransport(direction);
}
}

View File

@ -0,0 +1,10 @@
package ProjectTankHard.MovementStrategy;
import ProjectTankHard.DirectionType;
public interface IMoveableObject {
public ObjectParameters GetObjectParameters();
public int GetStep();
boolean CheckCanMove(DirectionType direction);
void MoveObject(DirectionType direction);
}

View File

@ -0,0 +1,36 @@
package ProjectTankHard.MovementStrategy;
public class MoveToBorder extends AbstractStrategy {
@Override
protected boolean IsTargetDestination() {
var objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
int a = FieldWidth();
int b = FieldHeight();
int q = GetStep();
return objParams.RightBorder <= FieldWidth() && objParams.RightBorder + GetStep() >= FieldWidth() &&
objParams.DownBorder <= FieldHeight() && objParams.DownBorder + GetStep() >= FieldHeight();
}
@Override
protected void MoveToTarget() {
var objParams = GetObjectParameters();
if (objParams == null) {
return;
}
var diffX = objParams.RightBorder - FieldWidth();
if (Math.abs(diffX) >= GetStep()) {
if (diffX < 0) {
MoveRight();
}
}
var diffY = objParams.DownBorder - FieldHeight();
if (Math.abs(diffY) >= GetStep()) {
if (diffY < 0) {
MoveDown();
}
}
}
}

View File

@ -0,0 +1,53 @@
package ProjectTankHard.MovementStrategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean IsTargetDestination(){
var objParams = GetObjectParameters();
if(objParams == null)
return false;
return ((objParams.ObjectMiddleHorizontal <= FieldWidth() / 2 &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth() / 2)
||(objParams.ObjectMiddleHorizontal >= FieldWidth() / 2 &&
objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth() / 2)) &&
((objParams.ObjectMiddleVertical <= FieldHeight() / 2 &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight() / 2) ||
(objParams.ObjectMiddleVertical >= FieldHeight() / 2 &&
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight() / 2));
}
@Override
protected void MoveToTarget()
{
var objParams = GetObjectParameters();
if (objParams == null)
{
return;
}
var diffX = objParams.ObjectMiddleHorizontal - FieldWidth() / 2;
if (Math.abs(diffX) > GetStep())
{
if (diffX > 0)
{
MoveLeft();
}
else
{
MoveRight();
}
}
var diffY = objParams.ObjectMiddleVertical - FieldHeight() / 2;
if (Math.abs(diffY) > GetStep())
{
if (diffY > 0)
{
MoveUp();
}
else
{
MoveDown();
}
}
}
}

View File

@ -0,0 +1,29 @@
package ProjectTankHard.MovementStrategy;
public class ObjectParameters {
private final int _x;
private final int _y;
private final int _width;
private final int _height;
public int LeftBorder;
public int TopBorder;
public int RightBorder;
public int DownBorder;
public int ObjectMiddleHorizontal;
public int ObjectMiddleVertical;
public ObjectParameters(int x, int y, int width, int height)
{
_x = x;
_y = y;
_width = width;
_height = height;
LeftBorder = _x;
TopBorder = _y;
RightBorder = _x + width;
DownBorder = _y + height;
ObjectMiddleHorizontal = _x + _width / 2;
ObjectMiddleVertical = _y + _height / 2;
}
}

View File

@ -0,0 +1,7 @@
package ProjectTankHard.MovementStrategy;
public enum Status {
NotInit,
InProgress,
Finish
}

View File

@ -1,3 +1,5 @@
package ProjectTankHard;
public enum WheelQuantityType {
Four,
Five,