Compare commits

..

7 Commits
main ... lab3

Author SHA1 Message Date
dyakonovr
bc16f35409 Точно all done 2023-12-17 00:02:56 +04:00
dyakonovr
4021abccde All done 2023-12-17 00:01:35 +04:00
dyakonovr
422b56827d Изменил название класса 2023-12-02 18:35:45 +04:00
dyakonovr
abb0e68974 All done 2023-12-02 18:30:58 +04:00
dyakonovr
f1da26658d Откатил файлы от второй лабы 2023-12-02 18:16:57 +04:00
dyakonovr
07b767fd61 All done 2023-12-02 17:54:59 +04:00
dyakonovr
c26db1d45e All done 2023-11-18 19:17:07 +04:00
33 changed files with 1377 additions and 2 deletions

37
.gitignore vendored
View File

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

View File

@ -0,0 +1,8 @@
package ProjectTankHard;
public enum DirectionType {
Up,
Down,
Left,
Right
}

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 + 2, _startPosY + 16, _startPosY + 16, _startPosY + 2};
g2d.drawPolygon(xRight, yRight, xRight.length);
}
}
}

View File

@ -0,0 +1,135 @@
package ProjectTankHard.DrawningObjects;
import ProjectTankHard.DirectionType;
import ProjectTankHard.Entities.EntityTankBase;
import ProjectTankHard.MovementStrategy.DrawningObjectTank;
import ProjectTankHard.MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.util.Random;
public class DrawningTankBase {
protected EntityTankBase EntityTankBase;
public int _pictureWidth;
public 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 IMoveableObject GetMoveableObject(){ return new DrawningObjectTank(this); }
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 + 2, _startPosY + 65, _tankWidth - 4, 33, 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

@ -0,0 +1,60 @@
package ProjectTankHard.DrawningObjects;
import ProjectTankHard.WheelQuantityType;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class DrawningWheels implements IDraw {
private WheelQuantityType WheelsQuantity;
private Color Color;
private int Width, Height;
public int currentX, currentY;
public DrawningWheels(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));
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));
}
}

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,23 @@
package ProjectTankHard.Entities;
import java.awt.*;
public class EntityTankBase {
private int Speed;
private double Weight, Step;
private Color BodyColor;
public double Weight(){ return Weight; }
public int Speed(){ return Speed; }
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,77 @@
package ProjectTankHard;
import ProjectTankHard.DrawningObjects.DrawningTankBase;
import ProjectTankHard.DrawningObjects.DrawningWheels;
import ProjectTankHard.DrawningObjects.IDraw;
import ProjectTankHard.Entities.EntityTankBase;
import ProjectTankHard.Generics.HardGeneric;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormHard {
private Canvas canv;
private int pictureBoxWidth;
private int pictureBoxHeight;
private EntityTankBase createEntity(){
Random random = new Random();
return new EntityTankBase(random.nextInt(100, 300),
random.nextDouble(1000, 3000),
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)));
}
void Draw(){
if(canv == null) return;
canv.repaint();
}
private IDraw makeIDraw(){
Random random = new Random();
return new DrawningWheels(pictureBoxWidth, pictureBoxHeight, 0, 0,
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)));
}
public FormHard(){
Random rand = new Random();
int hardGenericSize = rand.nextInt(1, 10);
JFrame HardFrame = new JFrame();
HardFrame.setSize(700, 400);
HardFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
canv = new Canvas();
canv.setBounds(0,0,pictureBoxWidth,pictureBoxHeight);
JButton MakeObjectButton = new JButton("Создать");
MakeObjectButton.setBounds(0, 0, 100, 40);
HardFrame.setContentPane(canv);
HardFrame.setVisible(true);
canv.add(MakeObjectButton);
pictureBoxHeight = canv.getHeight();
pictureBoxWidth = canv.getWidth();
HardGeneric<EntityTankBase, IDraw> hardGeneric = new HardGeneric<>(hardGenericSize, hardGenericSize, pictureBoxWidth, pictureBoxHeight);
for(int i = 0; i < hardGenericSize; i++){
EntityTankBase entity = createEntity();
hardGeneric.InsertFirst(entity);
hardGeneric.InsertSecond(makeIDraw());
}
MakeObjectButton.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawningTankBase DrawningTankBase = hardGeneric.MakeObject();
DrawningTankBase.SetPosition(pictureBoxWidth / 2 - DrawningTankBase.GetWidth()/2,
pictureBoxHeight / 2 - DrawningTankBase.GetHeight()/2);
canv.DrawningTankBase = DrawningTankBase;
Draw();
}
}
);
}
}

View File

@ -0,0 +1,214 @@
package ProjectTankHard;
import ProjectTankHard.DrawningObjects.DrawningTank;
import ProjectTankHard.DrawningObjects.DrawningTankBase;
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;
public JButton SelectButton;
public JFrame TankFrame;
Canvas canv;
static int pictureBoxWidth = 882;
static int pictureBoxHeight = 453;
public void Draw(){
if(DrawningTankBase == null) return;
canv.repaint();
}
public DrawningTankBase SelectedTank() {
return DrawningTankBase;
}
public Color ChooseColor(JFrame MonorailFrame) {
JColorChooser dialog = new JColorChooser();
Color res = JColorChooser.showDialog(MonorailFrame, "Выберите цвет", Color.BLACK);
return res;
}
public FormTank() {
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("Создать (продв.)");
SelectButton = 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);
SelectButton.setBounds(383, 401, 180, 40);
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);
TankFrame.add(SelectButton);
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) {
Color color = Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301));
Color choosenColor = ChooseColor(TankFrame);
if (choosenColor != null) color = choosenColor;
DrawningTankBase = new DrawningTankBase(random.nextInt(100, 300), random.nextDouble(1000, 3000),
color, 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) {
Color bodyColor = Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301));
Color additionalColor = Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301));
Color choosenColor = ChooseColor(TankFrame);
if (choosenColor != null) bodyColor = choosenColor;
choosenColor = ChooseColor(TankFrame);
if (choosenColor != null) additionalColor = choosenColor;
DrawningTankBase = new DrawningTank(random.nextInt(100, 300), random.nextDouble(1000, 3000),
bodyColor, additionalColor, 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,128 @@
package ProjectTankHard;
import ProjectTankHard.DrawningObjects.DrawningTankBase;
import ProjectTankHard.Generics.TanksGenericCollection;
import ProjectTankHard.MovementStrategy.DrawningObjectTank;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormTankCollection {
private final TanksGenericCollection<DrawningTankBase, DrawningObjectTank> _tanks;
private int pictureBoxWidth = 605;
private int pictureBoxHeight = 426;
CollectionCanvas canv;
void Draw(){
if(canv == null) return;
canv.repaint();
}
public FormTankCollection(){
_tanks = new TanksGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
canv = new CollectionCanvas();
canv.setBounds(12,12, pictureBoxWidth, pictureBoxHeight);
JPanel toolBox = new JPanel();
JFrame collectionFrame = new JFrame();
JButton addButton = new JButton("Добавить");
JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить");
JTextField tankNumber = new JTextField();
GridLayout gridLayout = new GridLayout(4,1);
toolBox.setLayout(gridLayout);
toolBox.add(addButton);
toolBox.add(tankNumber);
toolBox.add(removeButton);
toolBox.add(refreshButton);
toolBox.setBounds(623, 12, 227, 426);
collectionFrame.add(toolBox);
collectionFrame.add(canv);
collectionFrame.setVisible(true);
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880,497);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_tanks == null) return;
FormTank form = new FormTank();
form.SelectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_tanks.Insert(form.SelectedTank()) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form.SelectedTank()._pictureWidth = pictureBoxWidth;
form.SelectedTank()._pictureHeight = pictureBoxHeight;
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
canv._tanks = _tanks;
form.TankFrame.dispose();
Draw();
}
});
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_tanks == null) return;
String tmp = tankNumber.getText();
int numb;
try {
numb = Integer.parseInt(tmp);
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_tanks.Remove(numb);
_tanks.ShowTanks();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_tanks == null) return;
_tanks.ShowTanks();
Draw();
}
});
}
}
class CollectionCanvas extends JComponent {
public TanksGenericCollection<DrawningTankBase, DrawningObjectTank> _tanks;
public CollectionCanvas() {}
@Override
public void paintComponent (Graphics g){
if (_tanks == null) return;
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(_tanks.ShowTanks(), 0, 0, this);
super.repaint();
}
}

View File

@ -0,0 +1,53 @@
package ProjectTankHard.Generics;
import ProjectTankHard.DrawningObjects.DrawningTankBase;
import ProjectTankHard.DrawningObjects.IDraw;
import ProjectTankHard.Entities.EntityTankBase;
import java.util.Random;
public class HardGeneric <T extends EntityTankBase, U extends IDraw>{
T[] firstArray;
U[] secondArray;
private int currentSize;
private int CountFirst;
private int CountSecond;
private int pictureBoxWidth;
private int pictureBoxHeight;
public HardGeneric(int countFirst, int countSecond, int width, int height){
currentSize = 0;
CountFirst = countFirst;
CountSecond = countSecond;
firstArray = (T[]) new EntityTankBase[CountFirst];
secondArray = (U[]) new IDraw[CountSecond];
pictureBoxHeight = height;
pictureBoxWidth = width;
}
public int InsertFirst(T entityTankBase){
if(firstArray[CountFirst - 1] != null) return -1;
for(int i = currentSize - 1; i >= 0; i--) {
firstArray[i + 1] = firstArray[i];
secondArray[i + 1] = secondArray[i];
}
currentSize++;
firstArray[0] = entityTankBase;
return 0;
}
public int InsertSecond(U inter){
if(secondArray[CountSecond-1] != null) return -1;
secondArray[0] = inter;
return 0;
}
public DrawningTankBase MakeObject(){
Random rand = new Random();
int indFirst = rand.nextInt(0, currentSize);
EntityTankBase entity = firstArray[indFirst];
DrawningTankBase tank = new DrawningTankBase(entity.Speed(), entity.Weight(), entity.BodyColor(),
pictureBoxWidth, pictureBoxHeight);
return tank;
}
}

View File

@ -0,0 +1,50 @@
package ProjectTankHard.Generics;
public class SetGeneric<T extends Object>{
private final Object[] _places;
public int Count;
public SetGeneric(int count){
_places = new Object[count];
Count = count;
}
public int Insert(T monorail){
return Insert(monorail, 0);
}
public int Insert(T monorail, int position){
if(!(position >= 0 && position < Count)) return -1;
if (_places[position] == null) {
_places[position] = monorail;
}
else {
int place = -1;
for (int i = position; i < Count; i++) {
if (_places[i] == null) {
place = i;
break;
}
}
if (place == -1) return -1;
for (int i = place - 1; i >= position; i--) {
_places[i+1] = _places[i];
}
_places[position] = monorail;
}
return position;
}
public boolean Remove(int position){
if (!(position >= 0 && position < Count)) return false;
_places[position] = null;
return true;
}
public T Get(int position){
if (!(position >= 0 && position < Count)) return null;
return (T)_places[position];
}
}

View File

@ -0,0 +1,80 @@
package ProjectTankHard.Generics;
import ProjectTankHard.DrawningObjects.DrawningTankBase;
import ProjectTankHard.MovementStrategy.IMoveableObject;
import java.awt.*;
import java.awt.image.BufferedImage;
public class TanksGenericCollection<T extends DrawningTankBase, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 150;
private final int _placeSizeHeight = 100;
private final SetGeneric<T> _collection;
public TanksGenericCollection(int picWidth, int picHeight){
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Insert(T obj){
if (obj == null) return -1;
return _collection.Insert(obj);
}
public boolean Remove(int position){
return _collection.Remove(position);
}
public U GetU(int pos){
T ans = _collection.Get(pos);
if(ans == null)
return null;
return (U)ans.GetMoveableObject();
}
private void DrawBackground(Graphics g)
{
g.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight,
i * _placeSizeWidth + _placeSizeWidth / 2,
j * _placeSizeHeight);
}
g.drawLine(i * _placeSizeWidth, 0,
i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
{
DrawningTankBase tank = _collection.Get(i);
if (tank != null)
{
int columnsCount = _pictureHeight / _placeSizeHeight;
int colIndex = i % columnsCount;
int rowIndex = i / columnsCount;
tank.SetPosition(colIndex * _placeSizeWidth, rowIndex * _placeSizeHeight);
tank.DrawTank((Graphics2D) g);
}
}
}
public BufferedImage ShowTanks()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
Graphics gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
}

View File

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

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

@ -0,0 +1,7 @@
package ProjectTankHard;
public enum WheelQuantityType {
Four,
Five,
Six
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 B

BIN
src/resources/UpButton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 B