138 lines
5.0 KiB
Java
138 lines
5.0 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.util.Random;
|
|
|
|
public class DrawningDoubleDeckerBus {
|
|
JPanel DoubleDeckerBusPanel;
|
|
private EntityDoubleDeckerBus EntityDoubleDeckerBus;
|
|
private int _pictureWidth;
|
|
private int _pictureHeight;
|
|
private int _startPosX = 0;
|
|
private int _startPosY = 0;
|
|
private final int _busWidth = 160;
|
|
private final int _busHeight = 100;
|
|
private DrawningDoor DrawningDoor;
|
|
|
|
public EntityDoubleDeckerBus EntityDoubleDeckerBus() {
|
|
return EntityDoubleDeckerBus;
|
|
}
|
|
|
|
public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, int doorNumber,
|
|
int width, int height, boolean secondFloor, boolean tailpipe, JPanel doubleDeckerBusPanel) {
|
|
if (width <= _busWidth || height <= _busHeight) {
|
|
return false;
|
|
}
|
|
|
|
_startPosX = 0;
|
|
_startPosY = 0;
|
|
doubleDeckerBusPanel.setSize(width, height);
|
|
DoubleDeckerBusPanel = doubleDeckerBusPanel;
|
|
doubleDeckerBusPanel.paint(DoubleDeckerBusPanel.getGraphics());
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
EntityDoubleDeckerBus = new EntityDoubleDeckerBus();
|
|
EntityDoubleDeckerBus.Init(speed, weight, bodyColor, additionalColor, doorNumber, secondFloor, tailpipe);
|
|
DrawningDoor = new DrawningDoor();
|
|
DrawningDoor.Init(_busWidth, _busHeight, _startPosX, _startPosY, doubleDeckerBusPanel);
|
|
Random random = new Random();
|
|
DrawningDoor.ChangeDoorsNumber(random.nextInt(5) + 2);
|
|
return true;
|
|
}
|
|
|
|
public void SetPosition(int x, int y){
|
|
if (EntityDoubleDeckerBus == null) {
|
|
return;
|
|
}
|
|
|
|
_startPosX = x;
|
|
_startPosY = y;
|
|
|
|
if (x + _busWidth <= _pictureWidth || y + _busHeight <= _pictureHeight) {
|
|
_startPosX = 0;
|
|
_startPosY = 0;
|
|
}
|
|
}
|
|
|
|
public void MoveTransport(DirectionType direction){
|
|
if (EntityDoubleDeckerBus == null)
|
|
return;
|
|
DoubleDeckerBusPanel.paint(DoubleDeckerBusPanel.getGraphics());
|
|
switch (direction)
|
|
{
|
|
case Left:
|
|
if (_startPosX - EntityDoubleDeckerBus.Step() >= 0)
|
|
_startPosX -= (int)EntityDoubleDeckerBus.Step();
|
|
else
|
|
_startPosX = 0;
|
|
break;
|
|
case Up:
|
|
if (_startPosY - EntityDoubleDeckerBus.Step() >= 0)
|
|
_startPosY -= (int)EntityDoubleDeckerBus.Step();
|
|
else
|
|
_startPosY = 0;
|
|
break;
|
|
case Right:
|
|
if (_startPosX + EntityDoubleDeckerBus.Step() + _busWidth < _pictureWidth)
|
|
_startPosX += (int)EntityDoubleDeckerBus.Step();
|
|
else
|
|
_startPosX = _pictureWidth - _busWidth;
|
|
break;
|
|
case Down:
|
|
if (_startPosY + EntityDoubleDeckerBus.Step() + _busHeight < _pictureHeight)
|
|
_startPosY += (int)EntityDoubleDeckerBus.Step();
|
|
else
|
|
_startPosY = _pictureHeight - _busHeight;
|
|
break;
|
|
}
|
|
DrawningDoor.CurX = _startPosX;
|
|
DrawningDoor.CurY = _startPosY;
|
|
}
|
|
|
|
public void DrawTransport() {
|
|
Graphics2D g2d = (Graphics2D) DoubleDeckerBusPanel.getGraphics();
|
|
|
|
if (EntityDoubleDeckerBus == null) {
|
|
return;
|
|
}
|
|
|
|
// Границы первого этажа автобуса
|
|
g2d.setColor(EntityDoubleDeckerBus.BodyColor());
|
|
g2d.fillRect(_startPosX + 40, _startPosY + 40, 120, 40);
|
|
|
|
// Колеса
|
|
g2d.setColor(Color.black);
|
|
g2d.fillOval(_startPosX + 45, _startPosY + 75, 20, 20);
|
|
g2d.fillOval(_startPosX + 127, _startPosY + 75, 20, 20);
|
|
|
|
// Окна
|
|
g2d.setColor(Color.blue);
|
|
g2d.fillOval(_startPosX + 50, _startPosY + 45, 20, 20);
|
|
g2d.fillOval(_startPosX + 75, _startPosY + 45, 20, 20);
|
|
g2d.fillOval(_startPosX + 100, _startPosY + 45, 20, 20);
|
|
g2d.fillOval(_startPosX + 125, _startPosY + 45, 20, 20);
|
|
|
|
// Двери
|
|
DrawningDoor.DrawDoors();
|
|
|
|
// Второй этаж
|
|
if (EntityDoubleDeckerBus.SecondFloor()) {
|
|
g2d.setColor(EntityDoubleDeckerBus.AdditionalColor());
|
|
// Границы второго этажа автобуса
|
|
g2d.fillRect(_startPosX + 40, _startPosY, 120, 40);
|
|
|
|
// Окна второго этажа
|
|
g2d.setColor(Color.blue);
|
|
g2d.fillOval(_startPosX + 50, _startPosY +10,20, 20);
|
|
g2d.fillOval(_startPosX + 75, _startPosY +10, 20, 20);
|
|
g2d.fillOval(_startPosX + 100, _startPosY +10,20, 20);
|
|
g2d.fillOval(_startPosX + 125, _startPosY +10,20,20);
|
|
}
|
|
|
|
if (EntityDoubleDeckerBus.Tailpipe()) {
|
|
g2d.setColor(EntityDoubleDeckerBus.AdditionalColor());
|
|
g2d.fillRect(_startPosX, _startPosY + 65, 40, 15);
|
|
}
|
|
}
|
|
|
|
}
|