2023-12-19 19:39:16 +04:00

171 lines
5.4 KiB
Java

package DrawningObjects;
import java.awt.*;
import javax.swing.JPanel;
import Directions.Direction;
import Entities.*;
import java.util.Random;
public class DrawningMonorail extends JPanel {
private EntityMonorail entity;
private int pictureWidth;
private int pictureHeight;
private int _startPosX;
private int _startPos ;
private int rollVar;
public int relWidth = 150;
private int relHeight = 46;
Random rd = new Random();
public DrawingWheels _drawingWheels;
public DrawningMonorail(int wheelCount ,int rollVar,int speed, double weight, Color bodyColor, int width, int height){
if (relWidth >= width || relHeight >= height)
{
return;
}
this.rollVar = rollVar;
pictureWidth = width;
pictureHeight = height;
entity = new EntityMonorail(wheelCount, speed, weight, bodyColor);
_drawingWheels = new DrawingWheels(entity.getBodyColor(), wheelCount);
}
public EntityMonorail getEntityMonorail(){
return entity;
}
protected void setEntityMonorail(EntityMonorail entity){
this.entity = entity;
}
protected DrawningMonorail(int wheelCount, int rollVar, int speed, double weight, Color bodyColor, int width,
int height, int carWidth, int carHeight){
if (relWidth >= width || relHeight >= height)
{
return;
}
this.rollVar = rollVar;
pictureWidth = width;
pictureHeight = height;
relWidth = carWidth;
relHeight = carHeight;
entity = new EntityMonorail(wheelCount, speed, weight, bodyColor);
}
public void setPosition(int x, int y) {
if (x < 0 || x > pictureWidth - relWidth)
{
x = 0;
}
if (y < 0 || y > pictureHeight - relHeight)
{
y = 0;
}
_startPosX = x;
_startPos = y;
}
public void moveTransport(Direction dr) {
if (entity == null) {
return;
}
switch (dr) {
case LEFT:
_startPosX -= (int)entity.getStep();
break;
case RIGHT:
_startPosX += (int)entity.getStep();
break;
case UP:
_startPos -= (int)entity.getStep();
break;
case DOWN:
_startPos += (int)entity.getStep();
break;
}
}
public int getPosX(){
return _startPosX;
}
public int getPosY(){
return _startPos;
}
public int getHeight(){
return relHeight;
}
public int getWidth(){
return relWidth;
}
public boolean canMove(Direction direction)
{
if (entity == null)
{
return false;
}
switch (direction) {
case LEFT:
return _startPosX - entity.getStep() > 0;
case UP:
return _startPos - entity.getStep() > 0;
case RIGHT:
return _startPosX + entity.getStep() < pictureWidth - relWidth;
case DOWN:
return _startPos + entity.getStep() < pictureHeight - relHeight;
default:
return false;
}
}
public void DrawTransport(Graphics g) {
if (entity == null) {
return;
}
relWidth = 150;
//Колёса
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 22, _startPos + 36, 40, 8);
g.drawRect(_startPosX+82, _startPos + 36, 40, 8);
g.drawOval(_startPosX + 3, _startPos + 37, 30, 8);
g.drawOval(_startPosX + 110, _startPos + 37, 29, 8);
g.fillRect(_startPosX + 22, _startPos + 36, 40, 8);
g.fillRect(_startPosX + 82, _startPos + 36, 40, 8);
g.fillOval( _startPosX + 3, _startPos + 37, 30, 8);
g.fillOval( _startPosX + 110, _startPos + 37, 29, 8);
_drawingWheels.Draw(g, _startPosX, _startPos);
//Кабина
g.setColor(entity.getBodyColor());
g.fillRect(_startPosX + 10, _startPos + 20, 120, 16);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 10, _startPos + 20, 120, 16);
int[] xp1 = new int[]{_startPosX+10,_startPosX+130,_startPosX+130,_startPosX+13};
int[] yp1 = new int[]{_startPos + 20,_startPos + 20,_startPos + 5,_startPos + 5};
Polygon pol1 = new Polygon(xp1, yp1, yp1.length);
g.setColor(entity.getBodyColor());
g.fillPolygon(pol1);
g.setColor(Color.BLACK);
g.drawPolygon(pol1);
//Дверь
g.setColor(Color.WHITE);
g.fillRect(_startPosX + 49, _startPos + 9, 12, 22);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 49, _startPos + 9, 12, 22);
//Окна и прочее
g.setColor(Color.CYAN);
g.drawRect(_startPosX + 20, _startPos + 8, 10, 10);
g.drawRect(_startPosX + 35, _startPos + 8, 10, 10);
g.drawRect(_startPosX + 117, _startPos + 8, 10, 10);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 130, _startPos + 10, 2, 22);
g.fillRect(_startPosX + 130, _startPos + 10, 2, 22);
}
}