LabWork02
This commit is contained in:
parent
21173c2c4e
commit
c9f315463b
26
DifferenceOfWheels/DrawningOrnamentRectangle.java
Normal file
26
DifferenceOfWheels/DrawningOrnamentRectangle.java
Normal file
@ -0,0 +1,26 @@
|
||||
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);
|
||||
}
|
||||
}
|
36
DifferenceOfWheels/DrawningOrnamentStar.java
Normal file
36
DifferenceOfWheels/DrawningOrnamentStar.java
Normal file
@ -0,0 +1,36 @@
|
||||
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);
|
||||
}
|
||||
}
|
30
DifferenceOfWheels/DrawningOrnamentTriangle.java
Normal file
30
DifferenceOfWheels/DrawningOrnamentTriangle.java
Normal file
@ -0,0 +1,30 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
32
DifferenceOfWheels/DrawningWheels.java
Normal file
32
DifferenceOfWheels/DrawningWheels.java
Normal file
@ -0,0 +1,32 @@
|
||||
package DifferenceOfWheels;
|
||||
|
||||
import java.util.Random;
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWheels implements IOrnaments{
|
||||
int CountWeel;
|
||||
private int count = 0;
|
||||
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);
|
||||
}
|
||||
}
|
15
DifferenceOfWheels/EnumWheels.java
Normal file
15
DifferenceOfWheels/EnumWheels.java
Normal file
@ -0,0 +1,15 @@
|
||||
package DifferenceOfWheels;
|
||||
|
||||
public enum EnumWheels {
|
||||
Two (2),
|
||||
Three (3),
|
||||
Four (4);
|
||||
|
||||
private int number;
|
||||
|
||||
EnumWheels(int n){
|
||||
this.number = n;
|
||||
}
|
||||
|
||||
public int get_number() { return number; }
|
||||
}
|
8
DifferenceOfWheels/IOrnaments.java
Normal file
8
DifferenceOfWheels/IOrnaments.java
Normal file
@ -0,0 +1,8 @@
|
||||
package DifferenceOfWheels;
|
||||
import java.awt.*;
|
||||
|
||||
public interface IOrnaments {
|
||||
void SetCount(int n);
|
||||
int get_count_weels();
|
||||
void DrawOrnament(Graphics2D g, int x, int y);
|
||||
}
|
21
Drawnings/CanvasDumpTruck.java
Normal file
21
Drawnings/CanvasDumpTruck.java
Normal file
@ -0,0 +1,21 @@
|
||||
package Drawnings;
|
||||
|
||||
import Drawnings.DrawningDumpTruck;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
9
Drawnings/DirectionType.java
Normal file
9
Drawnings/DirectionType.java
Normal file
@ -0,0 +1,9 @@
|
||||
package Drawnings;
|
||||
|
||||
public enum DirectionType {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
Unknow
|
||||
}
|
95
Drawnings/DrawningDumpTruck.java
Normal file
95
Drawnings/DrawningDumpTruck.java
Normal file
@ -0,0 +1,95 @@
|
||||
package Drawnings;
|
||||
import Entities.EntityDumpTruck;
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
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);
|
||||
EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, additionalColor, body, tent);
|
||||
_ornament.SetCount(random.nextInt(2, 5));
|
||||
|
||||
}
|
||||
|
||||
public DrawningDumpTruck(){
|
||||
super(110, 100);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (EntityTruck == null || !(EntityTruck instanceof EntityDumpTruck entityTruck) || _startPosX == null || _startPosY == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_startPosX += 5;
|
||||
super.DrawTransport(g);
|
||||
_startPosX -= 5;
|
||||
|
||||
g.setPaint(Color.BLACK);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Колёса
|
||||
g.setPaint(Color.BLACK);
|
||||
if (_ornament != null){
|
||||
switch (_ornament.get_count_weels()){
|
||||
case 2:
|
||||
_ornament.DrawOrnament(g, _startPosX + 10, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 85, _startPosY + 67);
|
||||
break;
|
||||
case 3:
|
||||
_ornament.DrawOrnament(g, _startPosX + 10, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 38, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 85, _startPosY + 67);
|
||||
break;
|
||||
case 4:
|
||||
_ornament.DrawOrnament(g, _startPosX + 5, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 32, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 61, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 88, _startPosY + 67);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Границы самосвала
|
||||
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(entityTruck.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);
|
||||
|
||||
}
|
||||
}
|
224
Drawnings/DrawningTruck.java
Normal file
224
Drawnings/DrawningTruck.java
Normal file
@ -0,0 +1,224 @@
|
||||
package Drawnings;
|
||||
|
||||
import DifferenceOfWheels.*;
|
||||
import Entities.EntityDumpTruck;
|
||||
import Entities.EntityTruck;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningTruck{
|
||||
public Entities.EntityTruck EntityTruck;
|
||||
protected Entities.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();
|
||||
if (_ornament != null){
|
||||
switch (_ornament.get_count_weels()){
|
||||
case 2:
|
||||
_ornament.DrawOrnament(g, _startPosX + 5, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 80, _startPosY + 67);
|
||||
break;
|
||||
case 3:
|
||||
_ornament.DrawOrnament(g, _startPosX + 5, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 33, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 80, _startPosY + 67);
|
||||
break;
|
||||
case 4:
|
||||
_ornament.DrawOrnament(g, _startPosX , _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 27, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 56, _startPosY + 67);
|
||||
_ornament.DrawOrnament(g, _startPosX + 83, _startPosY + 67);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Границы самосвала
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
25
Entities/EntityDumpTruck.java
Normal file
25
Entities/EntityDumpTruck.java
Normal file
@ -0,0 +1,25 @@
|
||||
package Entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityDumpTruck extends EntityTruck {
|
||||
|
||||
public Color AdditionalColor;
|
||||
public Color get_AdditionalColor(){ return AdditionalColor; }
|
||||
|
||||
public Boolean Body;
|
||||
private Boolean get_Body() { return Body; }
|
||||
|
||||
public Boolean Tent;
|
||||
private Boolean get_Tent() { return Tent; }
|
||||
|
||||
|
||||
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
24
Entities/EntityTruck.java
Normal 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;
|
||||
}
|
||||
}
|
220
FormDumpTruck.java
Normal file
220
FormDumpTruck.java
Normal file
@ -0,0 +1,220 @@
|
||||
import Drawnings.CanvasDumpTruck;
|
||||
import Drawnings.DirectionType;
|
||||
import Drawnings.DrawningDumpTruck;
|
||||
import Drawnings.DrawningTruck;
|
||||
import MovementStrategy.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
import javax.swing.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class FormDumpTruck extends JFrame{
|
||||
|
||||
private CanvasDumpTruck _canvasDumpTruck = new CanvasDumpTruck();
|
||||
|
||||
private AbstractStrategy _strategy;
|
||||
|
||||
// Размер формы
|
||||
private Dimension dimension;
|
||||
// Название формы
|
||||
private String title;
|
||||
|
||||
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() {
|
||||
CreateDumpTruckButton.setName("Создать самосвал");
|
||||
CreateTruckButton.setName("Создать грузовик");
|
||||
setMinimumSize(dimension);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
UpButton.setName("UP");
|
||||
Icon iconUp = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowUp.png");
|
||||
UpButton.setIcon(iconUp);
|
||||
|
||||
DownButton.setName("DOWN");
|
||||
Icon iconDown = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowDown.png");
|
||||
DownButton.setIcon(iconDown);
|
||||
|
||||
LeftButton.setName("LEFT");
|
||||
Icon iconLeft = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowLeft.png");
|
||||
LeftButton.setIcon(iconLeft);
|
||||
|
||||
RightButton.setName("RIGHT");
|
||||
Icon iconRight = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowRight.png");
|
||||
RightButton.setIcon(iconRight);
|
||||
|
||||
CreateDumpTruckButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawningDumpTruck");
|
||||
}
|
||||
});
|
||||
|
||||
CreateTruckButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawningTruck");
|
||||
}
|
||||
});
|
||||
|
||||
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._drawningTruck == null) return;
|
||||
boolean result = false;
|
||||
switch ((((JButton) (event.getSource())).getName())) {
|
||||
case "UP":
|
||||
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "DOWN":
|
||||
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "LEFT":
|
||||
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "RIGHT":
|
||||
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
if (result) {
|
||||
_canvasDumpTruck.repaint();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
UpButton.addActionListener(actionListener);
|
||||
DownButton.addActionListener(actionListener);
|
||||
LeftButton.addActionListener(actionListener);
|
||||
RightButton.addActionListener(actionListener);
|
||||
|
||||
setSize(dimension.width, dimension.height);
|
||||
setLayout(null);
|
||||
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
|
||||
// кнопки создаиния самосвала и грузовика
|
||||
CreateDumpTruckButton.setBounds(10, getHeight() - 90, 140, 40);
|
||||
CreateTruckButton.setBounds(160, getHeight() - 90, 140, 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);
|
||||
// кнокпи для стратегий
|
||||
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(ComboBoxStrategy);
|
||||
add(ButtonStrategy);
|
||||
add(_canvasDumpTruck);
|
||||
pack();
|
||||
setVisible(true);
|
||||
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
public void componentResized(ComponentEvent e) {
|
||||
int Width = getWidth();
|
||||
int Height = getHeight();
|
||||
if (_canvasDumpTruck._drawningTruck != null)
|
||||
_canvasDumpTruck._drawningTruck.SetPictureSize(Width, Height);
|
||||
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
|
||||
CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40);
|
||||
CreateTruckButton.setBounds(180, getHeight() - 90, 140, 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
8
Main.java
Normal file
8
Main.java
Normal file
@ -0,0 +1,8 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
FormDumpTruck formDumpTruck = new FormDumpTruck("Самосвал", new Dimension(700, 700));
|
||||
formDumpTruck.Form();
|
||||
}
|
||||
}
|
60
MovementStrategy/AbstractStrategy.java
Normal file
60
MovementStrategy/AbstractStrategy.java
Normal 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;
|
||||
}
|
||||
}
|
7
MovementStrategy/IMoveableObject.java
Normal file
7
MovementStrategy/IMoveableObject.java
Normal file
@ -0,0 +1,7 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public interface IMoveableObject {
|
||||
ObjectParameters GetObjectPosition();
|
||||
int GetStep();
|
||||
Boolean TryMoveObject(MovementDirection direction);
|
||||
}
|
36
MovementStrategy/MoveToBorder.java
Normal file
36
MovementStrategy/MoveToBorder.java
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
38
MovementStrategy/MoveToCenter.java
Normal file
38
MovementStrategy/MoveToCenter.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
48
MovementStrategy/MoveableTruck.java
Normal file
48
MovementStrategy/MoveableTruck.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
8
MovementStrategy/MovementDirection.java
Normal file
8
MovementStrategy/MovementDirection.java
Normal file
@ -0,0 +1,8 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum MovementDirection {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
}
|
27
MovementStrategy/ObjectParameters.java
Normal file
27
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;
|
||||
RightBorder = _x + _width;
|
||||
DownBorder = _y + _height;
|
||||
ObjectMiddleHorizontal = _x + _width / 2;
|
||||
ObjectMiddleVertical = _y + _height / 2;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user