220 lines
7.5 KiB
Java
220 lines
7.5 KiB
Java
package laba1Loco;
|
||
|
||
import java.awt.*;
|
||
import java.util.*;
|
||
import javax.swing.*;
|
||
import javax.swing.Timer;
|
||
import java.awt.event.*;
|
||
|
||
public class DrawingTrain {
|
||
public IMoveableObject GetMoveableObject() { return new DrawningObjectTrain(this);}
|
||
|
||
protected IWheelDrawing wheelDrawing;
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityTrain EntityTrain;
|
||
/// <summary>
|
||
/// Ширина окна
|
||
/// </summary>
|
||
public int _pictureWidth;
|
||
/// <summary>
|
||
/// Высота окна
|
||
/// </summary>
|
||
public int _pictureHeight;
|
||
/// <summary>
|
||
/// Левая координата прорисовки локомотива
|
||
/// </summary>
|
||
protected int _startPosX;
|
||
/// <summary>
|
||
/// Верхняя кооридната прорисовки локомотива
|
||
/// </summary>
|
||
protected int _startPosY;
|
||
/// <summary>
|
||
/// Ширина прорисовки локомотива
|
||
/// </summary>
|
||
protected int _locoWidth = 83;
|
||
/// <summary>
|
||
/// Высота прорисовки локомотива
|
||
/// </summary>
|
||
protected int _locoHeight = 41;
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Цвет кузова</param>
|
||
/// <param name="width">Ширина картинки</param>
|
||
/// <param name="height">Высота картинки</param>
|
||
public DrawingTrain(int speed, double weight, Color bodyColor, int _numWheel, int width, int height)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
if (_pictureHeight < _locoHeight || _pictureWidth < _locoWidth)
|
||
return;
|
||
EntityTrain = new EntityTrain(speed, weight, bodyColor, _numWheel);
|
||
Random random = new Random();
|
||
switch(random.nextInt(0, 3)){
|
||
case 0:
|
||
wheelDrawing = new WheelDrawingSimple();
|
||
break;
|
||
case 1:
|
||
wheelDrawing = new WheelDrawingBalls();
|
||
break;
|
||
case 2:
|
||
wheelDrawing = new WheelDrawingDavidStar();
|
||
break;
|
||
default:
|
||
wheelDrawing = new WheelDrawingSimple();
|
||
break;
|
||
}
|
||
wheelDrawing.setNumWheel(_numWheel);
|
||
}
|
||
// конструктор для 3 сложной лабы
|
||
public DrawingTrain(EntityTrain train, IWheelDrawing _wheelDrawing, int width, int height ){
|
||
if (height < _locoHeight || width < _locoWidth)
|
||
return;
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
EntityTrain = train;
|
||
wheelDrawing = _wheelDrawing;
|
||
wheelDrawing.setNumWheel(EntityTrain.numWheel);
|
||
}
|
||
/// <summary>
|
||
/// Установка позиции
|
||
/// </summary>
|
||
/// <param name="x">Координата X</param>
|
||
/// <param name="y">Координата Y</param>
|
||
public void SetPosition(int x, int y)
|
||
{
|
||
_startPosX = Math.min(x, _pictureWidth - _locoWidth);
|
||
_startPosY = Math.min(y, _pictureHeight - _locoHeight);
|
||
}
|
||
/// <summary>
|
||
/// Координата X объекта
|
||
/// </summary>
|
||
public int GetPosX (){return _startPosX;}
|
||
/// <summary>
|
||
/// Координата Y объекта
|
||
/// </summary>
|
||
public int GetPosY (){return _startPosY;}
|
||
/// <summary>
|
||
/// Ширина объекта
|
||
/// </summary>
|
||
public int GetWidth (){return _locoWidth;}
|
||
/// <summary>
|
||
/// Высота объекта
|
||
/// </summary>
|
||
public int GetHeight (){return _locoHeight;}
|
||
/// <summary>
|
||
/// Проверка, что объект может переместится по указанному направлению
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
/// <returns>true - можно переместится по указанному направлению</returns>
|
||
public boolean CanMove(Direction direction)
|
||
{
|
||
if (EntityTrain == null)
|
||
{
|
||
return false;
|
||
}
|
||
switch (direction)
|
||
{
|
||
case Left:
|
||
return _startPosX - EntityTrain.Step > 0;
|
||
case Right:
|
||
return _startPosX + _locoWidth + EntityTrain.Step < _pictureWidth;
|
||
case Up:
|
||
return _startPosY - EntityTrain.Step > 0;
|
||
case Down:
|
||
return _startPosY + _locoHeight + EntityTrain.Step < _pictureHeight;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
public void MoveTransport(Direction direction)
|
||
{
|
||
if (!CanMove(direction) || EntityTrain == null)
|
||
{
|
||
return;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case Left:
|
||
_startPosX -= (int)EntityTrain.Step;
|
||
break;
|
||
//вверх
|
||
case Up:
|
||
_startPosY -= (int)EntityTrain.Step;
|
||
break;
|
||
// вправо
|
||
case Right:
|
||
_startPosX += (int)EntityTrain.Step;
|
||
break;
|
||
//вниз
|
||
case Down:
|
||
_startPosY += (int)EntityTrain.Step;
|
||
break;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public void DrawTransport(Graphics2D g2d)
|
||
{
|
||
|
||
if (EntityTrain == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
// body
|
||
g2d.setColor(EntityTrain.BodyColor);
|
||
g2d.drawPolyline(new int[]{
|
||
_startPosX + 8, _startPosX + 79, _startPosX + 79, _startPosX + 4, _startPosX + 4, _startPosX + 8,
|
||
}, new int[]{
|
||
_startPosY+10, _startPosY+10, _startPosY+32, _startPosY+32, _startPosY+20, _startPosY+10
|
||
}, 6);
|
||
g2d.drawPolyline(new int[]{
|
||
_startPosX + 4, _startPosX + 29, _startPosX + 29, _startPosX + 37, _startPosX + 37, _startPosX + 79, _startPosX + 37, _startPosX + 37, _startPosX + 29, _startPosX + 29,
|
||
}, new int[]{
|
||
_startPosY+21, _startPosY+21, _startPosY+14, _startPosY+14, _startPosY+21, _startPosY+21, _startPosY+21, _startPosY+29, _startPosY+29, _startPosY+21
|
||
}, 10);
|
||
|
||
// trucks
|
||
g2d.fillPolygon(new int[]{
|
||
_startPosX + 0,_startPosX + 5,_startPosX + 32,_startPosX + 36,
|
||
}, new int[]{
|
||
_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37
|
||
}, 4);
|
||
g2d.fillPolygon(new int[]{
|
||
_startPosX + 44,_startPosX + 49,_startPosX + 76,_startPosX + 80,
|
||
}, new int[]{
|
||
_startPosY+37,_startPosY+33,_startPosY+33,_startPosY+37
|
||
}, 4);
|
||
|
||
//back
|
||
g2d.fillPolygon(new int[]{
|
||
_startPosX + 79,_startPosX + 82,_startPosX + 82,_startPosX + 79,
|
||
}, new int[]{
|
||
_startPosY+12,_startPosY+12,_startPosY+30,_startPosY+30
|
||
}, 4);
|
||
|
||
|
||
//windows
|
||
g2d.setColor(Color.BLUE);
|
||
g2d.drawRect(_startPosX + 10, _startPosY + 12, 6, 7);
|
||
g2d.drawRect(_startPosX + 19, _startPosY + 12, 6, 7);
|
||
g2d.drawRect(_startPosX + 72, _startPosY + 12, 6, 7);
|
||
|
||
//wheels
|
||
wheelDrawing.Draw(_startPosX, _startPosY, false, EntityTrain.BodyColor, g2d);
|
||
}
|
||
}
|