Лабораторная работа №2
This commit is contained in:
parent
ee561695c1
commit
4c5a29427d
@ -0,0 +1,32 @@
|
||||
package DifferentRollers;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingRollersCross implements IDifferentRollers{
|
||||
private RollersCount rollersCount;
|
||||
@Override
|
||||
public void setRollersCount(int numOfRoller){
|
||||
for (RollersCount numofenum : RollersCount.values()){
|
||||
if (numofenum.getNumOfRollers() == numOfRoller){
|
||||
rollersCount = numofenum;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public RollersCount getRollersCount(){
|
||||
return rollersCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawRollers(Graphics2D g, int x, int y, int width, int height, Color bodyColor){
|
||||
g.setColor(bodyColor);
|
||||
g.fillOval(x, y, width, height);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawLine(x+2, y+2, x+6, y+6);
|
||||
g.drawLine(x+2, y+6, x+6, y+2);
|
||||
g.drawOval(x, y, width, height);
|
||||
g.setColor(bodyColor);
|
||||
}
|
||||
}
|
@ -1,7 +1,10 @@
|
||||
package DifferentRollers;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingRollers {
|
||||
public class DrawingRollersPlus implements IDifferentRollers{
|
||||
private RollersCount rollersCount;
|
||||
@Override
|
||||
public void setRollersCount(int numOfRoller){
|
||||
for (RollersCount numofenum : RollersCount.values()){
|
||||
if (numofenum.getNumOfRollers() == numOfRoller){
|
||||
@ -11,13 +14,18 @@ public class DrawingRollers {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public RollersCount getRollersCount(){
|
||||
return rollersCount;
|
||||
}
|
||||
public void DrawRollers(Graphics g, int x, int y, int width, int height, Color bodyColor){
|
||||
|
||||
@Override
|
||||
public void DrawRollers(Graphics2D g, int x, int y, int width, int height, Color bodyColor){
|
||||
g.setColor(bodyColor);
|
||||
g.fillOval(x, y, width, height);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawLine(x+4, y, x+4, y+8);
|
||||
g.drawLine(x, y+4, x+8, y+4);
|
||||
g.drawOval(x, y, width, height);
|
||||
g.setColor(bodyColor);
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package DifferentRollers;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingRollersStar implements IDifferentRollers{
|
||||
private RollersCount rollersCount;
|
||||
@Override
|
||||
public void setRollersCount(int numOfRoller){
|
||||
for (RollersCount numofenum : RollersCount.values()){
|
||||
if (numofenum.getNumOfRollers() == numOfRoller){
|
||||
rollersCount = numofenum;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public RollersCount getRollersCount(){
|
||||
return rollersCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawRollers(Graphics2D g, int x, int y, int width, int height, Color bodyColor){
|
||||
g.setColor(bodyColor);
|
||||
g.fillOval(x, y, width, height);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawLine(x+4, y, x+4, y+8);
|
||||
g.drawLine(x, y+4, x+8, y+4);
|
||||
g.drawLine(x+2, y+2, x+6, y+6);
|
||||
g.drawLine(x+2, y+6, x+6, y+2);
|
||||
g.drawOval(x, y, width, height);
|
||||
g.setColor(bodyColor);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package DifferentRollers;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDifferentRollers {
|
||||
void setRollersCount(int numOfRoller);
|
||||
RollersCount getRollersCount();
|
||||
void DrawRollers(Graphics2D g, int x, int y, int width, int height, Color bodyColor);
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package DifferentRollers;
|
||||
|
||||
public enum RollersCount {
|
||||
OneRoller(1),
|
||||
TwoRollers(2),
|
@ -1,16 +1,18 @@
|
||||
package Drawings;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class CanvasExcavator extends JComponent{
|
||||
public DrawingExcavator _drawingExcavator;
|
||||
public DrawingBulldozer _drawingBulldozer;
|
||||
public CanvasExcavator(){}
|
||||
public void paintComponent(Graphics g){
|
||||
if(_drawingExcavator == null){
|
||||
if(_drawingBulldozer == null){
|
||||
return;
|
||||
}
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_drawingExcavator.DrawTransport(g2d);
|
||||
_drawingBulldozer.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
@ -1,4 +1,7 @@
|
||||
package Drawings;
|
||||
|
||||
public enum DirectionType {
|
||||
Unknow,
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
@ -1,30 +1,59 @@
|
||||
package Drawings;
|
||||
|
||||
import DifferentRollers.DrawingRollersCross;
|
||||
import DifferentRollers.DrawingRollersPlus;
|
||||
import DifferentRollers.DrawingRollersStar;
|
||||
import DifferentRollers.IDifferentRollers;
|
||||
import Entities.EntityBulldozer;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawingExcavator {
|
||||
private EntityExcavator EntityExcavator; // Класс-сущность
|
||||
public DrawingRollers drawingRollers = null;
|
||||
|
||||
private Integer _startPosX; // Левая координата отрисовки
|
||||
private Integer _startPosY; // Верхняя координата отрисовки
|
||||
public class DrawingBulldozer extends JPanel {
|
||||
public Entities.EntityBulldozer EntityBulldozer;
|
||||
public IDifferentRollers drawingRollers;
|
||||
public Integer _startPosX; // Левая координата отрисовки
|
||||
public Integer _startPosY; // Верхняя координата отрисовки
|
||||
private Integer _pictureWidth; // Ширина окна отрисовки
|
||||
private Integer _pictureHeight; // Высота окна отрисовки
|
||||
private final int _drawingExcWidth = 120; // Ширина отрисовки трактора
|
||||
private final int _drawingExcHeight = 70; // Высота отрисовки трактора
|
||||
protected int _drawingExcWidth = 120; // Ширина отрисовки трактора
|
||||
protected int _drawingExcHeight = 70; // Высота отрисовки трактора
|
||||
public Integer GetPosX(){return _startPosX;}
|
||||
public Integer GetPosY(){return _startPosY;}
|
||||
public Integer GetWidth(){return _drawingExcWidth;}
|
||||
public Integer GetHeight(){return _drawingExcHeight;}
|
||||
|
||||
|
||||
|
||||
// Инициализация свойств
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){
|
||||
EntityExcavator = new EntityExcavator();
|
||||
EntityExcavator.Init(speed, weight, bodyColor, additionalColor, prop, ladle);
|
||||
protected DrawingBulldozer(){
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
drawingRollers = new DrawingRollers();
|
||||
drawingRollers.setRollersCount((int)(Math.random() * 4));
|
||||
}
|
||||
|
||||
protected void SetTypeRollers(){
|
||||
int rollersCount = (int)(Math.random()*4);
|
||||
switch ((int)(Math.random()*3+1)){
|
||||
case 1:
|
||||
drawingRollers = new DrawingRollersCross();
|
||||
break;
|
||||
case 2:
|
||||
drawingRollers = new DrawingRollersPlus();
|
||||
break;
|
||||
case 3:
|
||||
drawingRollers = new DrawingRollersStar();
|
||||
break;
|
||||
default:
|
||||
rollersCount = 0;
|
||||
break;
|
||||
}
|
||||
drawingRollers.setRollersCount(rollersCount);
|
||||
}
|
||||
|
||||
public DrawingBulldozer(int speed, double weight, Color bodyColor){
|
||||
super();
|
||||
EntityBulldozer = new EntityBulldozer(speed, weight, bodyColor);
|
||||
SetTypeRollers();
|
||||
}
|
||||
|
||||
// Смена границ формы отрисовки
|
||||
@ -71,28 +100,28 @@ public class DrawingExcavator {
|
||||
|
||||
// Изменение направления движения
|
||||
public boolean MoveTransport(DirectionType direction){
|
||||
if (EntityExcavator == null || _startPosX == null || _startPosY == null){
|
||||
if (EntityBulldozer == null || _startPosX == null || _startPosY == null){
|
||||
return false;
|
||||
}
|
||||
switch (direction) {
|
||||
case Left:
|
||||
if (_startPosX - EntityExcavator.Step > 0) {
|
||||
_startPosX -= (int) EntityExcavator.Step;
|
||||
if (_startPosX - EntityBulldozer.Step > 0) {
|
||||
_startPosX -= (int) EntityBulldozer.Step;
|
||||
}
|
||||
return true;
|
||||
case Up:
|
||||
if (_startPosY - EntityExcavator.Step > 0) {
|
||||
_startPosY -= (int) EntityExcavator.Step;
|
||||
if (_startPosY - EntityBulldozer.Step > 0) {
|
||||
_startPosY -= (int) EntityBulldozer.Step;
|
||||
}
|
||||
return true;
|
||||
case Right:
|
||||
if (_startPosX + EntityExcavator.Step < _pictureWidth - _drawingExcWidth) {
|
||||
_startPosX += (int) EntityExcavator.Step;
|
||||
if (_startPosX + EntityBulldozer.Step < _pictureWidth - _drawingExcWidth) {
|
||||
_startPosX += (int) EntityBulldozer.Step;
|
||||
}
|
||||
return true;
|
||||
case Down:
|
||||
if (_startPosY + EntityExcavator.Step < _pictureHeight - _drawingExcHeight) {
|
||||
_startPosY += (int) EntityExcavator.Step;
|
||||
if (_startPosY + EntityBulldozer.Step < _pictureHeight - _drawingExcHeight) {
|
||||
_startPosY += (int) EntityBulldozer.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
@ -102,7 +131,7 @@ public class DrawingExcavator {
|
||||
|
||||
// Отрисовка Экскаватора
|
||||
public void DrawTransport(Graphics2D g){
|
||||
if (EntityExcavator == null || _startPosX == null || _startPosY == null){
|
||||
if (EntityBulldozer == null || _startPosX == null || _startPosY == null){
|
||||
return;
|
||||
}
|
||||
|
||||
@ -112,7 +141,7 @@ public class DrawingExcavator {
|
||||
g.drawRect(_startPosX + 35, _startPosY + 10, 5, 15);
|
||||
g.drawRect(_startPosX + 55, _startPosY + 3, 22, 22); // кабина
|
||||
|
||||
g.setColor(EntityExcavator.getBodyColor());
|
||||
g.setColor(EntityBulldozer.getBodyColor());
|
||||
g.fillRect(_startPosX + 21, _startPosY + 26, 59, 19);
|
||||
g.fillRect(_startPosX + 36, _startPosY + 11, 4, 14);
|
||||
|
||||
@ -128,7 +157,7 @@ public class DrawingExcavator {
|
||||
g.drawOval(_startPosX + 15, _startPosY + 47, 17, 17);
|
||||
g.drawOval(_startPosX + 63, _startPosY + 47, 17, 17);
|
||||
|
||||
g.setColor(EntityExcavator.getBodyColor());
|
||||
g.setColor(EntityBulldozer.getBodyColor());
|
||||
g.fillOval(_startPosX + 15, _startPosY + 47, 17, 17);
|
||||
g.fillOval(_startPosX + 63, _startPosY + 47, 17, 17);
|
||||
|
||||
@ -137,69 +166,24 @@ public class DrawingExcavator {
|
||||
g.drawOval(_startPosX + 40, _startPosY + 48, 5, 5);
|
||||
g.drawOval(_startPosX + 50, _startPosY + 48, 5, 5);
|
||||
|
||||
g.setColor(EntityExcavator.getBodyColor());
|
||||
g.setColor(EntityBulldozer.getBodyColor());
|
||||
g.fillOval(_startPosX + 40, _startPosY + 48, 5, 5);
|
||||
g.fillOval(_startPosX + 50, _startPosY + 48, 5, 5);
|
||||
|
||||
if (EntityExcavator.getProp()){
|
||||
g.setColor(Color.BLACK);
|
||||
//Опоры
|
||||
//справа
|
||||
g.drawRect(_startPosX + 80, _startPosY + 40, 11, 3);
|
||||
g.drawRect(_startPosX + 87, _startPosY + 43, 5, 25);
|
||||
g.drawRect(_startPosX + 84, _startPosY + 68, 11, 3);
|
||||
|
||||
//слева
|
||||
g.drawRect(_startPosX + 6, _startPosY + 40, 13, 3);
|
||||
g.drawRect(_startPosX + 4, _startPosY + 43, 5, 25);
|
||||
g.drawRect(_startPosX + 1, _startPosY + 68, 11, 3);
|
||||
|
||||
g.setColor(EntityExcavator.getAdditionalColor());
|
||||
//покраска справа
|
||||
g.fillRect(_startPosX + 81, _startPosY + 41, 10, 2);
|
||||
g.fillRect(_startPosX + 88, _startPosY + 44, 4, 24);
|
||||
g.fillRect(_startPosX + 85, _startPosY + 69, 10, 2);
|
||||
|
||||
//покраска слева
|
||||
g.fillRect(_startPosX + 7, _startPosY + 41, 12, 2);
|
||||
g.fillRect(_startPosX + 5, _startPosY + 44, 4, 24);
|
||||
g.fillRect(_startPosX + 2, _startPosY + 69, 10, 2);
|
||||
}
|
||||
|
||||
if(EntityExcavator.getLadle()){
|
||||
g.setColor(Color.BLACK);
|
||||
//Ковш
|
||||
//ковш(стрела)
|
||||
g.drawRect(_startPosX + 77, _startPosY + 17, 14, 8);
|
||||
g.drawRect(_startPosX + 91, _startPosY + 9, 20, 7);
|
||||
g.drawRect(_startPosX + 111, _startPosY + 17, 9, 20);
|
||||
|
||||
int[] pointsLadleX = {_startPosX + 120, _startPosX + 104, _startPosX + 120};
|
||||
int[] pointsLadleY = {_startPosY + 37, _startPosY + 54, _startPosY + 54};
|
||||
|
||||
Polygon Ladle = new Polygon(pointsLadleX, pointsLadleY, 3);
|
||||
g.drawPolygon(Ladle);
|
||||
g.setColor(EntityExcavator.getAdditionalColor());
|
||||
g.fillPolygon(Ladle);
|
||||
//покраска
|
||||
g.fillRect(_startPosX + 78, _startPosY + 18, 13, 7);
|
||||
g.fillRect(_startPosX + 92, _startPosY + 10, 19, 6);
|
||||
g.fillRect(_startPosX + 112, _startPosY + 18, 8, 19);
|
||||
}
|
||||
int x = _startPosX;
|
||||
if (drawingRollers.getRollersCount() != null){
|
||||
switch (drawingRollers.getRollersCount()){
|
||||
case OneRoller:
|
||||
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
|
||||
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityBulldozer.getBodyColor());
|
||||
break;
|
||||
case TwoRollers:
|
||||
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
|
||||
drawingRollers.DrawRollers(g,x + 44, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
|
||||
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityBulldozer.getBodyColor());
|
||||
drawingRollers.DrawRollers(g,x + 44, _startPosY + 55, 8, 8, EntityBulldozer.getBodyColor());
|
||||
break;
|
||||
case ThreeRollers:
|
||||
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
|
||||
drawingRollers.DrawRollers(g,x + 44, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
|
||||
drawingRollers.DrawRollers(g,x + 54, _startPosY + 55, 8, 8, EntityExcavator.getBodyColor());
|
||||
drawingRollers.DrawRollers(g,x + 34, _startPosY + 55, 8, 8, EntityBulldozer.getBodyColor());
|
||||
drawingRollers.DrawRollers(g,x + 44, _startPosY + 55, 8, 8, EntityBulldozer.getBodyColor());
|
||||
drawingRollers.DrawRollers(g,x + 54, _startPosY + 55, 8, 8, EntityBulldozer.getBodyColor());
|
||||
break;
|
||||
}
|
||||
}
|
67
ProjectExcavator/src/Drawings/DrawingExcavator.java
Normal file
67
ProjectExcavator/src/Drawings/DrawingExcavator.java
Normal file
@ -0,0 +1,67 @@
|
||||
package Drawings;
|
||||
|
||||
import Entities.EntityExcavator;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingExcavator extends DrawingBulldozer{
|
||||
|
||||
public DrawingExcavator(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){
|
||||
EntityBulldozer = new EntityExcavator(speed, weight, bodyColor, additionalColor, prop, ladle);
|
||||
SetTypeRollers();
|
||||
}
|
||||
|
||||
// Отрисовка Экскаватора
|
||||
@Override
|
||||
public void DrawTransport(Graphics2D g){
|
||||
if (EntityBulldozer == null || !(EntityBulldozer instanceof EntityExcavator excavator) || _startPosX == null || _startPosY == null){
|
||||
return;
|
||||
}
|
||||
super.DrawTransport(g);
|
||||
if (excavator.Prop){
|
||||
g.setColor(Color.BLACK);
|
||||
//Опоры
|
||||
//справа
|
||||
g.drawRect(_startPosX + 80, _startPosY + 40, 11, 3);
|
||||
g.drawRect(_startPosX + 87, _startPosY + 43, 5, 25);
|
||||
g.drawRect(_startPosX + 84, _startPosY + 68, 11, 3);
|
||||
|
||||
//слева
|
||||
g.drawRect(_startPosX + 6, _startPosY + 40, 13, 3);
|
||||
g.drawRect(_startPosX + 4, _startPosY + 43, 5, 25);
|
||||
g.drawRect(_startPosX + 1, _startPosY + 68, 11, 3);
|
||||
|
||||
g.setColor(excavator.getAdditionalColor());
|
||||
//покраска справа
|
||||
g.fillRect(_startPosX + 81, _startPosY + 41, 10, 2);
|
||||
g.fillRect(_startPosX + 88, _startPosY + 44, 4, 24);
|
||||
g.fillRect(_startPosX + 85, _startPosY + 69, 10, 2);
|
||||
|
||||
//покраска слева
|
||||
g.fillRect(_startPosX + 7, _startPosY + 41, 12, 2);
|
||||
g.fillRect(_startPosX + 5, _startPosY + 44, 4, 24);
|
||||
g.fillRect(_startPosX + 2, _startPosY + 69, 10, 2);
|
||||
}
|
||||
|
||||
if(excavator.Ladle){
|
||||
g.setColor(Color.BLACK);
|
||||
//Ковш
|
||||
//ковш(стрела)
|
||||
g.drawRect(_startPosX + 77, _startPosY + 17, 14, 8);
|
||||
g.drawRect(_startPosX + 91, _startPosY + 9, 20, 7);
|
||||
g.drawRect(_startPosX + 111, _startPosY + 17, 9, 20);
|
||||
|
||||
int[] pointsLadleX = {_startPosX + 120, _startPosX + 104, _startPosX + 120};
|
||||
int[] pointsLadleY = {_startPosY + 37, _startPosY + 54, _startPosY + 54};
|
||||
|
||||
Polygon Ladle = new Polygon(pointsLadleX, pointsLadleY, 3);
|
||||
g.drawPolygon(Ladle);
|
||||
g.setColor(excavator.getAdditionalColor());
|
||||
g.fillPolygon(Ladle);
|
||||
//покраска
|
||||
g.fillRect(_startPosX + 78, _startPosY + 18, 13, 7);
|
||||
g.fillRect(_startPosX + 92, _startPosY + 10, 19, 6);
|
||||
g.fillRect(_startPosX + 112, _startPosY + 18, 8, 19);
|
||||
}
|
||||
}
|
||||
}
|
20
ProjectExcavator/src/Entities/EntityBulldozer.java
Normal file
20
ProjectExcavator/src/Entities/EntityBulldozer.java
Normal file
@ -0,0 +1,20 @@
|
||||
package Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityBulldozer {
|
||||
private int Speed;
|
||||
private double Weight;
|
||||
private Color BodyColor;
|
||||
|
||||
public Color getBodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
public double Step;
|
||||
public EntityBulldozer(int speed, double weight, Color bodyColor){
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
Step = Speed * 100 / Weight;
|
||||
}
|
||||
}
|
18
ProjectExcavator/src/Entities/EntityExcavator.java
Normal file
18
ProjectExcavator/src/Entities/EntityExcavator.java
Normal file
@ -0,0 +1,18 @@
|
||||
package Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityExcavator extends EntityBulldozer{
|
||||
private Color AdditionalColor;
|
||||
public Color getAdditionalColor(){
|
||||
return AdditionalColor;
|
||||
}
|
||||
public boolean Prop;
|
||||
public boolean Ladle;
|
||||
public EntityExcavator(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){
|
||||
super(speed, weight, bodyColor);
|
||||
AdditionalColor = additionalColor;
|
||||
Prop = prop;
|
||||
Ladle = ladle;
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityExcavator {
|
||||
private int Speed;
|
||||
private double Weight;
|
||||
private Color BodyColor;
|
||||
|
||||
public Color getBodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
private Color AdditionalColor;
|
||||
public Color getAdditionalColor(){
|
||||
return AdditionalColor;
|
||||
}
|
||||
private boolean Prop;
|
||||
public boolean getProp(){
|
||||
return Prop;
|
||||
}
|
||||
private boolean Ladle;
|
||||
public boolean getLadle(){
|
||||
return Ladle;
|
||||
}
|
||||
public double Step;
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean prop, boolean ladle){
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Prop = prop;
|
||||
Ladle = ladle;
|
||||
Step = Speed * 100 / Weight;
|
||||
}
|
||||
}
|
@ -1,3 +1,9 @@
|
||||
import Drawings.CanvasExcavator;
|
||||
import Drawings.DirectionType;
|
||||
import Drawings.DrawingBulldozer;
|
||||
import Drawings.DrawingExcavator;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
@ -11,23 +17,58 @@ public class FormExcavator extends JFrame {
|
||||
private Dimension dimension;
|
||||
private int Width, Height;
|
||||
private CanvasExcavator canvasExcavator = new CanvasExcavator();
|
||||
private JButton CreateButton = new JButton("Создать");
|
||||
private JButton CreateButton = new JButton("Создать экскаватор");
|
||||
private JButton CreateBullButton = new JButton("Создать бульдозер");
|
||||
private JButton UpButton = new JButton();
|
||||
private JButton DownButton = new JButton();
|
||||
private JButton LeftButton = new JButton();
|
||||
private JButton RightButton = new JButton();
|
||||
private AbstractStrategy _strategy;
|
||||
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
|
||||
private JButton ButtonStrategy = new JButton("Шаг");
|
||||
public FormExcavator(String title, Dimension dimension) {
|
||||
this.title = title;
|
||||
this.dimension = dimension;
|
||||
}
|
||||
|
||||
private void CreateObject(String typeOfClass) {
|
||||
int StartPositionX = (int)(Math.random() * 90 + 10);
|
||||
int StartPositionY = (int)(Math.random() * 90 + 10);
|
||||
int speed = (int)(Math.random() * 300 + 100);
|
||||
double weight = (double)(Math.random() * 3000 + 1000);
|
||||
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||||
switch (typeOfClass) {
|
||||
case "DrawingBulldozer":
|
||||
canvasExcavator._drawingBulldozer = new DrawingBulldozer(speed, weight, bodyColor);
|
||||
canvasExcavator._drawingBulldozer.SetPictureSize(Width, Height);
|
||||
canvasExcavator._drawingBulldozer.SetPosition(StartPositionX, StartPositionY);
|
||||
canvasExcavator.repaint();
|
||||
break;
|
||||
case "DrawingExcavator":
|
||||
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||||
boolean sheepPipes = new Random().nextBoolean();
|
||||
boolean fuelTank = new Random().nextBoolean();
|
||||
canvasExcavator._drawingBulldozer = new DrawingExcavator(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
|
||||
canvasExcavator._drawingBulldozer.SetPictureSize(Width, Height);
|
||||
canvasExcavator._drawingBulldozer.SetPosition(StartPositionX, StartPositionY);
|
||||
canvasExcavator.repaint();
|
||||
break;
|
||||
default: return;
|
||||
}
|
||||
_strategy = null;
|
||||
ComboBoxStrategy.setEnabled(true);
|
||||
}
|
||||
|
||||
public void Init() {
|
||||
setTitle(title);
|
||||
setMinimumSize(dimension);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
_strategy = null;
|
||||
|
||||
CreateButton.setName("CREATE");
|
||||
CreateBullButton.setName("CREATEBULLBUTTON");
|
||||
Icon iconUp = new ImageIcon("ProjectExcavator/res/up.png");
|
||||
UpButton.setIcon(iconUp);
|
||||
UpButton.setName("UP");
|
||||
@ -44,39 +85,73 @@ public class FormExcavator extends JFrame {
|
||||
CreateButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int StartPositionX = (int)(Math.random() * 90 + 10);
|
||||
int StartPositionY = (int)(Math.random() * 90 + 10);
|
||||
int speed = (int)(Math.random() * 300 + 100);
|
||||
double weight = (Math.random() * 3000 + 1000);
|
||||
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||||
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
||||
boolean sheepPipes = new Random().nextBoolean();
|
||||
boolean fuelTank = new Random().nextBoolean();
|
||||
canvasExcavator._drawingExcavator = new DrawingExcavator();
|
||||
canvasExcavator._drawingExcavator.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank);
|
||||
canvasExcavator._drawingExcavator.SetPictureSize(Width, Height);
|
||||
canvasExcavator._drawingExcavator.SetPosition( StartPositionX, StartPositionY);
|
||||
canvasExcavator.repaint();
|
||||
CreateObject("DrawingExcavator");
|
||||
}
|
||||
});
|
||||
|
||||
CreateBullButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawingBulldozer");
|
||||
}
|
||||
});
|
||||
|
||||
ButtonStrategy.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (canvasExcavator._drawingBulldozer == null) return;
|
||||
if (ComboBoxStrategy.isEnabled())
|
||||
{
|
||||
int index = ComboBoxStrategy.getSelectedIndex();
|
||||
switch(index)
|
||||
{
|
||||
case 0:
|
||||
_strategy = new MoveToCenter();
|
||||
break;
|
||||
case 1:
|
||||
_strategy = new MoveToBorder();
|
||||
break;
|
||||
default:
|
||||
_strategy = null;
|
||||
break;
|
||||
}
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableExcavator(canvasExcavator._drawingBulldozer), Width, Height);
|
||||
}
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ComboBoxStrategy.setEnabled(false);
|
||||
_strategy.MakeStep();
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
ComboBoxStrategy.setEnabled(true);
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ActionListener actionListener = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if (canvasExcavator._drawingExcavator == null) return;
|
||||
if (canvasExcavator._drawingBulldozer == null) return;
|
||||
boolean result = false;
|
||||
switch ((((JButton)(event.getSource())).getName())) {
|
||||
case "UP":
|
||||
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Up);
|
||||
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "DOWN":
|
||||
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Down);
|
||||
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "LEFT":
|
||||
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Left);
|
||||
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "RIGHT":
|
||||
result = canvasExcavator._drawingExcavator.MoveTransport(DirectionType.Right);
|
||||
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result) {
|
||||
@ -92,16 +167,22 @@ public class FormExcavator extends JFrame {
|
||||
setSize(dimension.width,dimension.height);
|
||||
setLayout(null);
|
||||
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 100, 35);
|
||||
CreateButton.setBounds(10, getHeight() - 85, 160, 35);
|
||||
CreateBullButton.setBounds(180, getHeight() - 85, 160, 35);
|
||||
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
|
||||
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
|
||||
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
|
||||
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
|
||||
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 25);
|
||||
ButtonStrategy.setBounds(getWidth() - 130, 45, 100, 25);
|
||||
add(CreateButton);
|
||||
add(CreateBullButton);
|
||||
add(UpButton);
|
||||
add(DownButton);
|
||||
add(RightButton);
|
||||
add(LeftButton);
|
||||
add(ButtonStrategy);
|
||||
add(ComboBoxStrategy);
|
||||
add(canvasExcavator);
|
||||
setVisible(true);
|
||||
//обработка события изменения размеров окна
|
||||
@ -109,14 +190,17 @@ public class FormExcavator extends JFrame {
|
||||
public void componentResized(ComponentEvent e) {
|
||||
Width = getWidth() - 15;
|
||||
Height = getHeight() - 35;
|
||||
if (canvasExcavator._drawingExcavator != null)
|
||||
canvasExcavator._drawingExcavator.SetPictureSize(Width, Height);
|
||||
if (canvasExcavator._drawingBulldozer != null)
|
||||
canvasExcavator._drawingBulldozer.SetPictureSize(Width, Height);
|
||||
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
|
||||
CreateButton.setBounds(10, getHeight() - 90, 100, 35);
|
||||
CreateButton.setBounds(10, getHeight() - 85, 160, 35);
|
||||
CreateBullButton.setBounds(180, getHeight() - 85, 160, 35);
|
||||
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
|
||||
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
|
||||
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
|
||||
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
|
||||
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 25);
|
||||
ButtonStrategy.setBounds(getWidth() - 130, 45, 100, 25);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
56
ProjectExcavator/src/MovementStrategy/AbstractStrategy.java
Normal file
56
ProjectExcavator/src/MovementStrategy/AbstractStrategy.java
Normal file
@ -0,0 +1,56 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public abstract class AbstractStrategy {
|
||||
private IMoveableObjects _moveableObject;
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
public int FieldWidth;
|
||||
public int FieldHeight;
|
||||
public StrategyStatus GetStatus() {return _state;}
|
||||
public void SetData(IMoveableObjects moveableObjects, int width, int height)
|
||||
{
|
||||
if (moveableObjects == null)
|
||||
{
|
||||
_state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
_state = StrategyStatus.InProgress;
|
||||
_moveableObject = moveableObjects;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress) return;
|
||||
if (IsTargetDestination())
|
||||
{
|
||||
_state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
MoveToTarget();
|
||||
}
|
||||
protected boolean MoveLeft() {return MoveTo(MovementDirection.Left);}
|
||||
protected boolean MoveRight() {return MoveTo(MovementDirection.Right);}
|
||||
protected boolean MoveUp() {return MoveTo(MovementDirection.Up);}
|
||||
protected boolean MoveDown() {return MoveTo(MovementDirection.Down);}
|
||||
protected ObjectParameters GetObjectParameters() {return _moveableObject.GetObjectPosition();}
|
||||
protected Integer GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject.GetStep();
|
||||
}
|
||||
protected abstract void MoveToTarget();
|
||||
protected abstract boolean IsTargetDestination();
|
||||
private boolean MoveTo(MovementDirection movementDirection)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
boolean stateTryMoveObject = _moveableObject.TryMoveObject(movementDirection);
|
||||
if (stateTryMoveObject) return stateTryMoveObject;
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public interface IMoveableObjects {
|
||||
ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
boolean TryMoveObject(MovementDirection direction);
|
||||
}
|
26
ProjectExcavator/src/MovementStrategy/MoveToBorder.java
Normal file
26
ProjectExcavator/src/MovementStrategy/MoveToBorder.java
Normal file
@ -0,0 +1,26 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class MoveToBorder extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestination(){
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
if (objParams == null){
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder + GetStep() >= FieldWidth - GetStep() && objParams.DownBorder + GetStep() >= FieldHeight - GetStep();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void MoveToTarget(){
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//реализация в правый нижний угол
|
||||
int x = objParams.RightBorder;
|
||||
if (x + GetStep() < FieldWidth) MoveRight();
|
||||
int y = objParams.DownBorder;
|
||||
if (y + GetStep() < FieldHeight) MoveDown();
|
||||
}
|
||||
}
|
48
ProjectExcavator/src/MovementStrategy/MoveToCenter.java
Normal file
48
ProjectExcavator/src/MovementStrategy/MoveToCenter.java
Normal file
@ -0,0 +1,48 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class MoveToCenter extends AbstractStrategy{
|
||||
@Override
|
||||
protected boolean IsTargetDestination() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 &&
|
||||
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight /2;
|
||||
}
|
||||
@Override
|
||||
protected void MoveToTarget() {
|
||||
ObjectParameters objParams = GetObjectParameters();
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
|
||||
if (Math.abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
if (Math.abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
45
ProjectExcavator/src/MovementStrategy/MoveableExcavator.java
Normal file
45
ProjectExcavator/src/MovementStrategy/MoveableExcavator.java
Normal file
@ -0,0 +1,45 @@
|
||||
package MovementStrategy;
|
||||
|
||||
import Drawings.CanvasExcavator;
|
||||
import Drawings.DrawingBulldozer;
|
||||
import Drawings.DirectionType;
|
||||
|
||||
public class MoveableExcavator implements IMoveableObjects{
|
||||
private CanvasExcavator canvas = new CanvasExcavator();
|
||||
public MoveableExcavator(DrawingBulldozer drawingBulldozer){
|
||||
canvas._drawingBulldozer = drawingBulldozer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectParameters GetObjectPosition() {
|
||||
if (canvas._drawingBulldozer == null || canvas._drawingBulldozer.EntityBulldozer == null ||
|
||||
canvas._drawingBulldozer.GetPosX() == null || canvas._drawingBulldozer.GetPosY() == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(canvas._drawingBulldozer.GetPosX(), canvas._drawingBulldozer.GetPosY(),
|
||||
canvas._drawingBulldozer.GetWidth(), canvas._drawingBulldozer.GetHeight());
|
||||
}
|
||||
@Override
|
||||
public int GetStep() {
|
||||
return (int)(canvas._drawingBulldozer.EntityBulldozer.Step);
|
||||
}
|
||||
@Override
|
||||
public boolean TryMoveObject(MovementDirection direction) {
|
||||
if (canvas._drawingBulldozer == null || canvas._drawingBulldozer.EntityBulldozer == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return canvas._drawingBulldozer.MoveTransport(GetDirectionType(direction));
|
||||
}
|
||||
private static DirectionType GetDirectionType(MovementDirection direction)
|
||||
{
|
||||
switch (direction) {
|
||||
case Left: return DirectionType.Left;
|
||||
case Right: return DirectionType.Right;
|
||||
case Up: return DirectionType.Up;
|
||||
case Down: return DirectionType.Down;
|
||||
default: return DirectionType.Unknow;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum MovementDirection {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
27
ProjectExcavator/src/MovementStrategy/ObjectParameters.java
Normal file
27
ProjectExcavator/src/MovementStrategy/ObjectParameters.java
Normal file
@ -0,0 +1,27 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public class ObjectParameters {
|
||||
private int _x;
|
||||
private int _y;
|
||||
private int _width;
|
||||
private int _height;
|
||||
public int LeftBorder = _x;
|
||||
public int TopBorder = _y;
|
||||
public int RightBorder = _x + _width;
|
||||
public int DownBorder = _y + _height;
|
||||
public int ObjectMiddleHorizontal = _x + _width / 2;
|
||||
public int ObjectMiddleVertical = _y + _height / 2;
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum StrategyStatus {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user