Compare commits
3 Commits
main
...
LabWork02-
Author | SHA1 | Date | |
---|---|---|---|
|
c9a77159bf | ||
90cd0c8afb | |||
2041097d1c |
17
CanvasDumpTruck.java
Normal file
17
CanvasDumpTruck.java
Normal file
@ -0,0 +1,17 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class CanvasDumpTruck extends JComponent {
|
||||
public DrawningDumpTruck _drawningDumpTruck;
|
||||
public CanvasDumpTruck() {}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
if (_drawningDumpTruck == null) {
|
||||
return;
|
||||
}
|
||||
super.paintComponents(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
_drawningDumpTruck.DrawTransport(g2d);
|
||||
super.repaint();
|
||||
}
|
||||
}
|
47
DifferenceOfWheels/DrawningOrnamentRectangle.java
Normal file
47
DifferenceOfWheels/DrawningOrnamentRectangle.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
57
DifferenceOfWheels/DrawningOrnamentStar.java
Normal file
57
DifferenceOfWheels/DrawningOrnamentStar.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
51
DifferenceOfWheels/DrawningOrnamentTriangle.java
Normal file
51
DifferenceOfWheels/DrawningOrnamentTriangle.java
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
53
DifferenceOfWheels/DrawningWheels.java
Normal file
53
DifferenceOfWheels/DrawningWheels.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
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; }
|
||||
}
|
10
DifferenceOfWheels/IOrnaments.java
Normal file
10
DifferenceOfWheels/IOrnaments.java
Normal 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);
|
||||
}
|
33
DrawningWeels.java
Normal file
33
DrawningWeels.java
Normal file
@ -0,0 +1,33 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningWeels {
|
||||
private int count;
|
||||
DrawningWeels (int n){
|
||||
count = n;
|
||||
}
|
||||
public int get_countWheels(){
|
||||
return count;
|
||||
}
|
||||
|
||||
public void DrawningWeel (Graphics g, int x, int y){
|
||||
g.setColor(Color.BLACK);
|
||||
|
||||
switch (get_countWheels()){
|
||||
case 2:
|
||||
g.fillOval(x + 10, y + 67, 25, 25);
|
||||
g.fillOval(x + 85, y + 67, 25, 25);
|
||||
break;
|
||||
case 3:
|
||||
g.fillOval(x + 10, y + 67, 25, 25);
|
||||
g.fillOval(x + 38, y + 67, 25, 25);
|
||||
g.fillOval(x + 85, y + 67, 25, 25);
|
||||
break;
|
||||
case 4:
|
||||
g.fillOval(x + 5, y + 67, 25, 25);
|
||||
g.fillOval(x + 32, y + 67, 25, 25);
|
||||
g.fillOval(x + 61, y + 67, 25, 25);
|
||||
g.fillOval(x + 88, y + 67, 25, 25);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
18
Drawnings/CanvasDumpTruck.java
Normal file
18
Drawnings/CanvasDumpTruck.java
Normal 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();
|
||||
}
|
||||
}
|
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
|
||||
}
|
58
Drawnings/DrawningDumpTruck.java
Normal file
58
Drawnings/DrawningDumpTruck.java
Normal 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);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
208
Drawnings/DrawningTruck.java
Normal file
208
Drawnings/DrawningTruck.java
Normal 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();
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
26
Entities/EntityDumpTruck.java
Normal file
26
Entities/EntityDumpTruck.java
Normal 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
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;
|
||||
}
|
||||
}
|
217
FormDumpTruck.java
Normal file
217
FormDumpTruck.java
Normal file
@ -0,0 +1,217 @@
|
||||
import Drawnings.CanvasDumpTruck;
|
||||
import Drawnings.DirectionType;
|
||||
import Drawnings.DrawningDumpTruck;
|
||||
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);
|
||||
|
||||
CreateTruckButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
CreateObject("DrawningTruck");
|
||||
}
|
||||
});
|
||||
|
||||
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._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, 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);
|
||||
// кнокпи для стратегий
|
||||
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);
|
||||
|
||||
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, 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println();
|
||||
FormDumpTruck formDumpTruck = new FormDumpTruck("Самосвал", new Dimension(900, 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;
|
||||
}
|
||||
|
||||
}
|
7
MovementStrategy/StrategyStatus.java
Normal file
7
MovementStrategy/StrategyStatus.java
Normal file
@ -0,0 +1,7 @@
|
||||
package MovementStrategy;
|
||||
|
||||
public enum StrategyStatus {
|
||||
NotInit,
|
||||
InProgress,
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user