PIbd-14 Antonova_A.A. LabWork02 Hard #2

Closed
Anitonchik wants to merge 2 commits from LabWork02-2 into LabWork01-2
24 changed files with 901 additions and 253 deletions

View File

@ -0,0 +1,47 @@
package DifferenceOfWheels;
import java.awt.*;
public class DrawningOrnamentRectangle implements IOrnaments{
int CountWeel;
@Override
public void SetCount(int n) {
CountWeel = n;
}
@Override
public int get_count_weels() {
return CountWeel;
}
@Override
public void DrawOrnament(Graphics2D g, int x, int y) {
g.setColor(Color.BLACK);
g.fillOval(x, y, 25, 25);
g.setColor(Color.PINK);
g.fillRect(x + 5, y + 5, 15, 15);
}
@Override
public void DrawWeels(Graphics2D g, int x, int y) {
switch (CountWeel){
case 2:
DrawOrnament(g, x + 10, y + 67);
DrawOrnament(g, x + 85, y + 67);
break;
case 3:
DrawOrnament(g, x + 10, y + 67);
DrawOrnament(g, x + 38, y + 67);
DrawOrnament(g, x + 85, y + 67);
break;
case 4:
DrawOrnament(g, x + 5, y + 67);
DrawOrnament(g, x + 32, y + 67);
DrawOrnament(g, x + 61, y + 67);
DrawOrnament(g, x + 88, y + 67);
break;
}
}
}

View File

@ -0,0 +1,57 @@
package DifferenceOfWheels;
import java.awt.*;
public class DrawningOrnamentStar implements IOrnaments{
int CountWeel;
@Override
public void SetCount(int n) {
CountWeel = n;
}
@Override
public int get_count_weels() {
return CountWeel;
}
@Override
public void DrawOrnament(Graphics2D g, int x, int y) {
g.setColor(Color.BLACK);
g.fillOval(x, y, 25, 25);
g.setColor(Color.CYAN);
Polygon elements = new Polygon();
elements.addPoint(x + 12, y + 2);
elements.addPoint(x + 15, y + 8);
elements.addPoint(x + 21, y + 10);
elements.addPoint(x + 15, y + 13);
elements.addPoint(x + 19, y + 19);
elements.addPoint(x + 12, y + 15);
elements.addPoint(x + 5, y + 19);
elements.addPoint(x + 9, y + 13);
elements.addPoint(x + 3, y + 10);
elements.addPoint(x + 10, y + 8);
g.fillPolygon(elements);
}
@Override
public void DrawWeels(Graphics2D g, int x, int y) {
switch (CountWeel){
case 2:
DrawOrnament(g, x + 10, y + 67);
DrawOrnament(g, x + 85, y + 67);
break;
case 3:
DrawOrnament(g, x + 10, y + 67);
DrawOrnament(g, x + 38, y + 67);
DrawOrnament(g, x + 85, y + 67);
break;
case 4:
DrawOrnament(g, x + 5, y + 67);
DrawOrnament(g, x + 32, y + 67);
DrawOrnament(g, x + 61, y + 67);
DrawOrnament(g, x + 88, y + 67);
break;
}
}
}

View File

@ -0,0 +1,51 @@
package DifferenceOfWheels;
import java.awt.*;
public class DrawningOrnamentTriangle implements IOrnaments{
int CountWeel;
@Override
public void SetCount(int n) {
CountWeel = n;
}
@Override
public int get_count_weels() {
return CountWeel;
}
@Override
public void DrawOrnament(Graphics2D g, int x, int y) {
g.setColor(Color.BLACK);
g.fillOval(x, y, 25, 25);
g.setColor(Color.MAGENTA);
Polygon elements = new Polygon();
elements.addPoint(x + 7, y + 6);
elements.addPoint(x + 20, y + 6);
elements.addPoint(x + 7, y + 18);
g.fillPolygon(elements);
}
@Override
public void DrawWeels(Graphics2D g, int x, int y) {
switch (CountWeel){
case 2:
DrawOrnament(g, x + 10, y + 67);
DrawOrnament(g, x + 85, y + 67);
break;
case 3:
DrawOrnament(g, x + 10, y + 67);
DrawOrnament(g, x + 38, y + 67);
DrawOrnament(g, x + 85, y + 67);
break;
case 4:
DrawOrnament(g, x + 5, y + 67);
DrawOrnament(g, x + 32, y + 67);
DrawOrnament(g, x + 61, y + 67);
DrawOrnament(g, x + 88, y + 67);
break;
}
}
}

View File

@ -0,0 +1,53 @@
package DifferenceOfWheels;
import java.awt.*;
import java.util.Random;
public class DrawningWheels implements IOrnaments {
int CountWeel;
Random rnd = new Random();
int ranOrnament = rnd.nextInt(0, 3);
@Override
public void SetCount(int n) {
for (EnumWheels count: EnumWheels.values()) {
if (count.get_number() == n) {
CountWeel = n;
return;
}
}
}
@Override
public int get_count_weels () {
return CountWeel;
}
@Override
public void DrawOrnament (Graphics2D g,int x, int y) {
g.setColor(Color.BLACK);
g.fillOval(x, y, 25, 25);
}
@Override
public void DrawWeels(Graphics2D g, int x, int y) {
switch (CountWeel){
case 2:
DrawOrnament(g, x + 10, y + 67);
DrawOrnament(g, x + 85, y + 67);
break;
case 3:
DrawOrnament(g, x + 10, y + 67);
DrawOrnament(g, x + 38, y + 67);
DrawOrnament(g, x + 85, y + 67);
break;
case 4:
DrawOrnament(g, x + 5, y + 67);
DrawOrnament(g, x + 32, y + 67);
DrawOrnament(g, x + 61, y + 67);
DrawOrnament(g, x + 88, y + 67);
break;
}
}
}

View File

@ -1,11 +1,13 @@
public enum DopEnum {
package DifferenceOfWheels;
public enum EnumWheels {
Two (2),
Three (3),
Four (4);
private int number;
DopEnum(int n){
EnumWheels(int n){
this.number = n;
}

View File

@ -0,0 +1,10 @@
package DifferenceOfWheels;
import java.awt.*;
public interface IOrnaments {
void SetCount(int n);
int get_count_weels();
void DrawOrnament(Graphics2D g, int x, int y);
void DrawWeels(Graphics2D g, int x, int y);
}

View File

@ -1,191 +0,0 @@
import java.awt.*;
import java.util.Random;
//import javax.swing.*;
public class DrawningDumpTruck {
public EntityDumpTruck EntityDumpTruck;
private EntityDumpTruck get_EntityDumpTruck() { return EntityDumpTruck; }
public DrawningWeels _drawingWeels = null;
// Ширина окна
private Integer _pictureWidth;
// Высота окна
private Integer _pictureHeight;
// Левая координата прорисовки автомобиля
private Integer _startPosX;
// Верхняя кооридната прорисовки автомобиля
private Integer _startPosY;
// Ширина прорисовки самосвала
private final int _drawningDumpTruckWidth = 116;
// Высота прорисовки самосвала
private final int _drawingDumpTruckHeight = 100;
Random random = new Random();
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent)
{
EntityDumpTruck = new EntityDumpTruck();
EntityDumpTruck.Init(speed, weight, bodyColor, additionalColor, body, tent);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
_drawingWeels = new DrawningWeels(random.nextInt(2, 5));
}
public Boolean SetPictureSize(int width, int height)
{
if (_drawningDumpTruckWidth > width || _drawingDumpTruckHeight > height)
{
return false;
}
if (_pictureWidth != null && width != _pictureWidth || _pictureHeight != null && height != _pictureHeight)
{
if (_startPosX + _drawningDumpTruckWidth > width)
{
_startPosX -= _drawningDumpTruckWidth;
}
if (_startPosY + _drawingDumpTruckHeight > height)
{
_startPosY -= _drawingDumpTruckHeight;
}
}
_pictureWidth = width;
_pictureHeight = height;
return true;
}
public void SetPosition(int x, int y)
{
if (_pictureHeight == null|| _pictureWidth == null)
{
return;
}
if (x < 0)
{
x = 0;
}
if (x + _drawningDumpTruckWidth > _pictureWidth)
{
x = _pictureWidth - _drawningDumpTruckWidth;
}
if (y < 0)
{
y = 0;
}
if (y + _drawingDumpTruckHeight > _pictureHeight)
{
y = _pictureHeight - _drawingDumpTruckHeight;
}
_startPosX = x;
_startPosY = y;
}
public Boolean MoveTransport(DirectionType direction)
{
if (EntityDumpTruck == null || _startPosX == null || _startPosY == null)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - _drawningDumpTruckWidth > 0)
{
_startPosX -= (int)EntityDumpTruck.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY - _drawingDumpTruckHeight > 0)
{
_startPosY -= (int)EntityDumpTruck.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX + _drawningDumpTruckWidth + (int)EntityDumpTruck.Step < _pictureWidth)
{
_startPosX += (int)EntityDumpTruck.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY + _drawingDumpTruckHeight + (int)EntityDumpTruck.Step < _pictureHeight)
{
_startPosY += (int)EntityDumpTruck.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g)
{
if (EntityDumpTruck == null || _startPosX == null || _startPosY == null)
{
return;
}
g.setPaint(Color.BLACK);
if (EntityDumpTruck.Body)
{
int[] x_b = {_startPosX, _startPosX + 77, _startPosX + 77, _startPosX + 20};
int[] y_b = {_startPosY + 15, _startPosY + 15, _startPosY + 50, _startPosY + 50};
g.setPaint(Color.BLACK);
g.drawPolygon(x_b, y_b, 4);
g.setPaint(EntityDumpTruck.AdditionalColor);
g.fillPolygon(x_b, y_b, 4);
}
if (EntityDumpTruck.Tent && EntityDumpTruck.Body)
{
int[] x_t = {_startPosX, _startPosX + 39, _startPosX + 77};
int[] y_t = {_startPosY + 13, _startPosY, _startPosY + 13};
g.setPaint(Color.BLACK);
g.drawPolygon(x_t, y_t, 3);
g.setPaint(EntityDumpTruck.AdditionalColor);
g.fillPolygon(x_t, y_t, 3);
}
// Колёса
g.setPaint(Color.BLACK);
if (_drawingWeels != null){
_drawingWeels.DrawningWeel(g, _startPosX, _startPosY);
}
//Границы самосвала
g.drawRect( _startPosX + 10, _startPosY + 53, 100, 13);
g.drawRect( _startPosX + 80, _startPosY + 15, 30, 36);
g.drawRect(_startPosX + 85, _startPosY + 20, 20, 20);
// Кузов
g.setPaint(EntityDumpTruck.BodyColor);
g.fillRect(_startPosX + 10, _startPosY + 53, 100, 13);
g.fillRect(_startPosX + 80, _startPosY + 15, 30, 36);
//Окно
g.setPaint(Color.CYAN);
g.fillRect(_startPosX + 85, _startPosY + 20, 20, 20);
}
}

View File

@ -1,7 +1,6 @@
import java.awt.*;
public class DrawningWeels {
private int count;
DrawningWeels (int n){
count = n;
@ -12,7 +11,6 @@ public class DrawningWeels {
public void DrawningWeel (Graphics g, int x, int y){
g.setColor(Color.BLACK);
//g.fillOval(x, y, 25, 25);
switch (get_countWheels()){
case 2:

View File

@ -0,0 +1,18 @@
package Drawnings;
import javax.swing.*;
import java.awt.*;
public class CanvasDumpTruck extends JComponent {
public DrawningTruck _drawningTruck;
public CanvasDumpTruck() {}
public void paintComponent(Graphics g) {
if (_drawningTruck == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
_drawningTruck.DrawTransport(g2d);
super.repaint();
}
}

View File

@ -1,6 +1,9 @@
package Drawnings;
public enum DirectionType {
Up,
Down,
Left,
Right,
Unknow
}

View File

@ -0,0 +1,58 @@
package Drawnings;
import Entities.EntityDumpTruck;
import java.awt.*;
public class DrawningDumpTruck extends DrawningTruck{
public DrawningDumpTruck(int speed, double weight, Color bodyColor,
Color additionalColor, Boolean body, Boolean tent)
{
super(speed, weight, bodyColor);
//super(110, 100);
Review

Закомментированного кода быть не должно

Закомментированного кода быть не должно
EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, additionalColor, body, tent);
_ornament.SetCount(random.nextInt(2, 5));
}
public DrawningDumpTruck(){
super(110, 100);
}
public void DrawTransport(Graphics2D g)
{
if (EntityTruck == null || !(EntityTruck instanceof EntityDumpTruck entityTruck) || _startPosX == null || _startPosY == null)
{
return;
}
g.setPaint(Color.BLACK);
_startPosX += 5;
super.DrawTransport(g);
_startPosX -= 5;
if (entityTruck.Body)
{
int[] x_b = {_startPosX, _startPosX + 77, _startPosX + 77, _startPosX + 20};
int[] y_b = {_startPosY + 15, _startPosY + 15, _startPosY + 50, _startPosY + 50};
g.setPaint(Color.BLACK);
g.drawPolygon(x_b, y_b, 4);
g.setPaint(entityTruck.AdditionalColor);
g.fillPolygon(x_b, y_b, 4);
}
if (entityTruck.Tent && entityTruck.Body)
{
int[] x_t = {_startPosX, _startPosX + 39, _startPosX + 77};
int[] y_t = {_startPosY + 13, _startPosY, _startPosY + 13};
g.setPaint(Color.BLACK);
g.drawPolygon(x_t, y_t, 3);
g.setPaint(entityTruck.AdditionalColor);
g.fillPolygon(x_t, y_t, 3);
}
}
}

View File

@ -0,0 +1,208 @@
package Drawnings;
import java.awt.*;
import java.util.Random;
import DifferenceOfWheels.*;
import Entities.EntityTruck;
public class DrawningTruck {
public EntityTruck EntityTruck;
protected EntityTruck get_EntityTruck() { return EntityTruck; }
public IOrnaments _ornament = new DrawningWheels();
int CountOfWheels = 0;
int ornament_type;
// Ширина окна
private Integer _pictureWidth;
// Высота окна
private Integer _pictureHeight;
// Левая координата прорисовки автомобиля
protected Integer _startPosX;
// Верхняя кооридната прорисовки автомобиля
protected Integer _startPosY;
// Ширина прорисовки самосвала
private int _drawningTruckWidth = 110; //100
// Высота прорисовки самосвала
private int _drawningTruckHeight = 110; //92
public Integer GetPosX() { return _startPosX;}
public Integer GetPosY() { return _startPosY;}
public Integer GetWidth() { return _drawningTruckWidth;}
public Integer GetHeight() { return _drawningTruckHeight;}
Random random = new Random();
private DrawningTruck()
{
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public DrawningTruck(int speed, double weight, Color bodyColor)
{
EntityTruck = new EntityTruck(speed, weight, bodyColor);
CountOfWheels = random.nextInt(2, 5);
ornament_type = random.nextInt(0, 3);
}
protected DrawningTruck(int drawningTruckWidth, int drawningTruckHeight)
{
_drawningTruckWidth = drawningTruckWidth;
_drawningTruckHeight = drawningTruckHeight;
}
protected void SetOrnamentWheel(){
switch (ornament_type){
case 0:
_ornament = new DrawningOrnamentStar();
_ornament.SetCount(CountOfWheels);
break;
case 1:
_ornament = new DrawningOrnamentTriangle();
_ornament.SetCount(CountOfWheels);
break;
case 2:
_ornament = new DrawningOrnamentRectangle();
_ornament.SetCount(CountOfWheels);
break;
}
}
public Boolean SetPictureSize(int width, int height)
{
if (_drawningTruckWidth > width || _drawningTruckHeight > height)
{
return false;
}
if (_pictureWidth != null && width != _pictureWidth || _pictureHeight != null && height != _pictureHeight)
{
if (_startPosX + _drawningTruckWidth > width)
{
_startPosX -= _drawningTruckWidth;
}
if (_startPosY + _drawningTruckHeight > height)
{
_startPosY -= _drawningTruckHeight;
}
}
_pictureWidth = width;
_pictureHeight = height;
return true;
}
public void SetPosition(int x, int y)
{
if (_pictureHeight == null|| _pictureWidth == null)
{
return;
}
if (x < 0)
{
x = 0;
}
if (x + _drawningTruckWidth > _pictureWidth)
{
x = (int)_pictureWidth - _drawningTruckWidth;
}
if (y < 0)
{
y = 0;
}
if (y + _drawningTruckHeight > _pictureHeight)
{
y = (int)_pictureHeight - _drawningTruckHeight;
}
_startPosX = x;
_startPosY = y;
}
public Boolean MoveTransport(DirectionType direction)
{
if (EntityTruck == null || _startPosX == null || _startPosY == null)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - _drawningTruckWidth > 0)
{
_startPosX -= (int)EntityTruck.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY - _drawningTruckHeight > 0)
{
_startPosY -= (int)EntityTruck.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX + _drawningTruckWidth + (int)EntityTruck.Step < _pictureWidth)
{
_startPosX += (int)EntityTruck.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY + _drawningTruckHeight + (int)EntityTruck.Step < _pictureHeight)
{
_startPosY += (int)EntityTruck.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics2D g)
{
if (EntityTruck == null || _startPosX == null || _startPosY == null)
{
return;
}
g.setPaint(Color.BLACK);
// Колёса
g.setPaint(Color.BLACK);
SetOrnamentWheel();
Review

Надо вызывать в конструкторе

Надо вызывать в конструкторе
if (_ornament != null){
_ornament.DrawWeels(g, _startPosX - 5, _startPosY);
}
//Границы самосвала
g.drawRect( _startPosX + 5, _startPosY + 53, 100, 13);
g.drawRect( _startPosX + 75, _startPosY + 15, 30, 36);
g.drawRect(_startPosX + 80, _startPosY + 20, 20, 20);
// Кузов
g.setPaint(EntityTruck.BodyColor);
g.fillRect(_startPosX + 5, _startPosY + 53, 100, 13);
g.fillRect(_startPosX + 75, _startPosY + 15, 30, 36);
//Окно
g.setPaint(Color.CYAN);
g.fillRect(_startPosX + 80, _startPosY + 20, 20, 20);
}
}

View File

@ -0,0 +1,26 @@
package Entities;
import java.awt.*;
public class EntityDumpTruck extends EntityTruck {
public Color AdditionalColor;
private Color get_AdditionalColor(){ return AdditionalColor; }
public Boolean Body;
private Boolean get_Body() { return Body; }
public Boolean Tent;
private Boolean get_Tent() { return Tent; }
public double Step;
public EntityDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent)
{
super (speed, weight, bodyColor);
AdditionalColor = additionalColor;
Body = body;
Tent = tent;
}
}

24
Entities/EntityTruck.java Normal file
View File

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

View File

@ -1,35 +0,0 @@
import java.awt.*;
public class EntityDumpTruck {
public int Speed;
private int get_Speed(){ return Speed; }
public double Weight;
private double get_Weight(){return Weight; }
public Color BodyColor;
private Color get_BodyColor(){ return BodyColor; }
public Color AdditionalColor;
private Color get_AdditionalColor(){ return AdditionalColor; }
public Boolean Body;
private Boolean get_Body() { return Body; }
public Boolean Tent;
private Boolean get_Tent() { return Tent; }
public double Step;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent)
{
Speed = speed;
Weight = weight;
BodyColor = bodyColor;
AdditionalColor = additionalColor;
Body = body;
Tent = tent;
Step = speed * 100 / weight;
}
}

View File

@ -1,3 +1,8 @@
import Drawnings.CanvasDumpTruck;
import Drawnings.DirectionType;
import Drawnings.DrawningDumpTruck;
import MovementStrategy.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -10,24 +15,58 @@ public class FormDumpTruck extends JFrame{
private CanvasDumpTruck _canvasDumpTruck = new CanvasDumpTruck();
private AbstractStrategy _strategy;
// Размер формы
private Dimension dimension;
// Название формы
private String title;
private JButton CreateButton = new JButton("Создать самосвал");
private JButton CreateDumpTruckButton = new JButton("Создать грузовик");
private JButton CreateTruckButton = new JButton("Создать самосвал");
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton();
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
private JButton ButtonStrategy = new JButton("Шаг");
public FormDumpTruck(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
private void CreateObject(String type){
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
switch(type){
case "DrawningTruck":
_canvasDumpTruck._drawningTruck = new Drawnings.DrawningTruck(random.nextInt(100, 300), random.nextInt(1000, 3000),
bodyColor);
_canvasDumpTruck._drawningTruck.SetPictureSize(getWidth(), getHeight());
_canvasDumpTruck._drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
_canvasDumpTruck.repaint();
break;
case "DrawningDumpTruck":
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
_canvasDumpTruck._drawningTruck = new DrawningDumpTruck();
_canvasDumpTruck._drawningTruck = new DrawningDumpTruck(random.nextInt(100, 300), random.nextInt(1000, 3000),
bodyColor, additionalColor,
random.nextBoolean(), random.nextBoolean());
_canvasDumpTruck._drawningTruck.SetPictureSize(getWidth(), getHeight());
_canvasDumpTruck._drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
_canvasDumpTruck.repaint();
break;
default:
return;
}
_strategy = null;
ComboBoxStrategy.setEnabled(true);
}
public void Form() {
CreateButton.setName("CREATE");
CreateDumpTruckButton.setName("Создать самосвал");
CreateTruckButton.setName("Создать грузовик");
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@ -47,40 +86,76 @@ public class FormDumpTruck extends JFrame{
Icon iconRight = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowRight.png");
RightButton.setIcon(iconRight);
CreateButton.addActionListener(new ActionListener() {
CreateTruckButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
_canvasDumpTruck._drawningDumpTruck = new DrawningDumpTruck();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
_canvasDumpTruck._drawningDumpTruck.Init(random.nextInt(1000, 3000), random.nextInt(1000, 3000),
bodyColor, additionalColor,
random.nextBoolean(), random.nextBoolean());
CreateObject("DrawningTruck");
}
});
_canvasDumpTruck._drawningDumpTruck.SetPictureSize(getWidth(), getHeight());
_canvasDumpTruck._drawningDumpTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
_canvasDumpTruck.repaint();
CreateDumpTruckButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawningDumpTruck");
}
});
ButtonStrategy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_canvasDumpTruck._drawningTruck == 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 MoveableTruck(_canvasDumpTruck._drawningTruck), getWidth() - 23, getHeight()-23);
}
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 (_canvasDumpTruck._drawningDumpTruck == null) return;
if (_canvasDumpTruck._drawningTruck == null) return;
boolean result = false;
switch ((((JButton) (event.getSource())).getName())) {
case "UP":
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Up);
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Up);
break;
case "DOWN":
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Down);
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Down);
break;
case "LEFT":
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Left);
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Left);
break;
case "RIGHT":
result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Right);
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Right);
break;
}
if (result) {
@ -97,17 +172,27 @@ public class FormDumpTruck extends JFrame{
setSize(dimension.width, dimension.height);
setLayout(null);
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 160, 40);
// кнопки создаиния самосвала и грузовика
CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40);
CreateTruckButton.setBounds(160, getHeight() - 90, 160, 40);
// кнопки направлений
UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 60, 60);
RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60);
LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60);
add(CreateButton);
// кнокпи для стратегий
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
// добавление кнопок
add(CreateDumpTruckButton);
add(CreateTruckButton);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(_canvasDumpTruck);
add(ComboBoxStrategy);
add(ButtonStrategy);
pack();
setVisible(true);
@ -115,14 +200,17 @@ public class FormDumpTruck extends JFrame{
public void componentResized(ComponentEvent e) {
int Width = getWidth();
int Height = getHeight();
if (_canvasDumpTruck._drawningDumpTruck != null)
_canvasDumpTruck._drawningDumpTruck.SetPictureSize(Width, Height);
if (_canvasDumpTruck._drawningTruck != null)
_canvasDumpTruck._drawningTruck.SetPictureSize(Width, Height);
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
CreateButton.setBounds(10, getHeight() - 90, 160, 40);
CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40);
CreateTruckButton.setBounds(180, getHeight() - 90, 160, 40);
UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60);
DownButton.setBounds(getWidth() - 150, getHeight() - 100, 60, 60);
RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60);
LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60);
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
}
});
}

View File

@ -0,0 +1,60 @@
package MovementStrategy;
public abstract class AbstractStrategy {
private IMoveableObject _moveableObject;
private StrategyStatus _state = StrategyStatus.NotInit;
protected int FieldWidth;
protected int FieldHeight;
public StrategyStatus GetStatus() { return _state; }
public void SetData(IMoveableObject moveableObject, int width, int height)
{
if (moveableObject == null)
{
_state = StrategyStatus.NotInit;
return;
}
_state = StrategyStatus.InProgress;
_moveableObject = moveableObject;
FieldWidth = width;
FieldHeight = height;
}
public void MakeStep()
{
if (_state != StrategyStatus.InProgress)
{
return;
}
if (IsTargetDestinaion())
{
_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 IsTargetDestinaion();
private boolean MoveTo(MovementDirection movementDirection)
{
if (_state != StrategyStatus.InProgress)
{
return false;
}
if (_moveableObject.TryMoveObject(movementDirection))
return true;
else
return false;
}
}

View File

@ -0,0 +1,7 @@
package MovementStrategy;
public interface IMoveableObject {
ObjectParameters GetObjectPosition();
int GetStep();
Boolean TryMoveObject(MovementDirection direction);
}

View File

@ -0,0 +1,36 @@
package MovementStrategy;
public class MoveToBorder extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion() {
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 diffX = FieldWidth - objParams.RightBorder;
if (Math.abs(diffX) > GetStep())
{
MoveRight();
}
int diffY = FieldHeight - objParams.DownBorder;
if (Math.abs(diffY) > GetStep())
{
MoveDown();
}
}
}

View File

@ -0,0 +1,38 @@
package MovementStrategy;
public class MoveToCenter extends AbstractStrategy{
@Override
protected boolean IsTargetDestinaion() {
ObjectParameters objParams = GetObjectParameters();
if (objParams == null) {
return false;
}
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 - GetStep() &&
objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 - GetStep() &&
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 - GetStep() &&
objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2 - GetStep() ;
}
@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();
}
}
}
}

View File

@ -0,0 +1,48 @@
package MovementStrategy;
import Drawnings.CanvasDumpTruck;
import Drawnings.DirectionType;
import Drawnings.DrawningTruck;
public class MoveableTruck implements IMoveableObject {
private CanvasDumpTruck canvas = new CanvasDumpTruck();
public MoveableTruck(DrawningTruck drawningTruck)
{
canvas._drawningTruck = drawningTruck;
}
@Override
public ObjectParameters GetObjectPosition() {
if (canvas._drawningTruck == null || canvas._drawningTruck.EntityTruck == null ||
canvas._drawningTruck.GetPosX() == null || canvas._drawningTruck.GetPosY() == null)
{
return null;
}
return new ObjectParameters(canvas._drawningTruck.GetPosX(), canvas._drawningTruck.GetPosY(),
canvas._drawningTruck.GetWidth(), canvas._drawningTruck.GetHeight());
}
@Override
public int GetStep() {
return (int)(canvas._drawningTruck.EntityTruck.Step);
}
@Override
public Boolean TryMoveObject(MovementDirection direction) {
if (canvas._drawningTruck == null || canvas._drawningTruck.EntityTruck == null)
{
return false;
}
return canvas._drawningTruck.MoveTransport(GetDirectionType(direction));
}
private static DirectionType GetDirectionType(MovementDirection direction)
{
switch (direction) {
case MovementDirection.Left: return DirectionType.Left;
case MovementDirection.Right: return DirectionType.Right;
case MovementDirection.Up: return DirectionType.Up;
case MovementDirection.Down: return DirectionType.Down;
default: return DirectionType.Unknow;
}
}
}

View File

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

View 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;
RightBorder = _x + _width;
DownBorder = _y + _height;
ObjectMiddleHorizontal = _x + _width / 2;
ObjectMiddleVertical = _y + _height / 2;
}
}

View File

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