140 lines
4.0 KiB
Java
140 lines
4.0 KiB
Java
|
import javax.swing.*;
|
||
|
import java.awt.*;
|
||
|
import java.util.Random;
|
||
|
|
||
|
public class DrawningBoat extends JPanel {
|
||
|
|
||
|
private EntityBoat Boat;
|
||
|
public EntityBoat GetBoat(){
|
||
|
return Boat;
|
||
|
}
|
||
|
|
||
|
private int _startPosX;
|
||
|
private int _startPosY;
|
||
|
public DrawningOars OarsDraw;
|
||
|
public Integer _pictureWidth = null;
|
||
|
public Integer _pictureHeight = null;
|
||
|
private final int _BoatWidth = 80;
|
||
|
private final int _BoatHeight = 50;
|
||
|
|
||
|
public void SetOars() {
|
||
|
Random r = new Random();
|
||
|
int numIllum = r.nextInt(1,4);
|
||
|
OarsDraw.SetOarsCount(numIllum);
|
||
|
}
|
||
|
|
||
|
public void Init(int speed, float weight, Color bodyColor)
|
||
|
{
|
||
|
Boat = new EntityBoat();
|
||
|
Boat.Init(speed, weight, bodyColor);
|
||
|
OarsDraw = new DrawningOars();
|
||
|
SetOars();
|
||
|
}
|
||
|
|
||
|
public void SetPosition(int x, int y, int width, int height)
|
||
|
{
|
||
|
if (x >= 0 && x + _BoatWidth <= width && y >= 0 && y + _BoatHeight <= height) {
|
||
|
_startPosX = x;
|
||
|
_startPosY = y;
|
||
|
_pictureWidth = width;
|
||
|
_pictureHeight = height;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void MoveTransport(Direction direction)
|
||
|
{
|
||
|
if (_pictureWidth == null || _pictureHeight == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
switch (direction)
|
||
|
{
|
||
|
case Right:
|
||
|
if (_startPosX + _BoatWidth + Boat.Step < _pictureWidth)
|
||
|
{
|
||
|
_startPosX += Boat.Step;
|
||
|
}
|
||
|
break;
|
||
|
case Left:
|
||
|
if (_startPosX - Boat.Step > 0)
|
||
|
{
|
||
|
_startPosX -= Boat.Step;
|
||
|
}
|
||
|
break;
|
||
|
case Up:
|
||
|
if (_startPosY - Boat.Step > 0)
|
||
|
{
|
||
|
_startPosY -= Boat.Step;
|
||
|
}
|
||
|
break;
|
||
|
case Down:
|
||
|
if (_startPosY + _BoatHeight + Boat.Step < _pictureHeight)
|
||
|
{
|
||
|
_startPosY += Boat.Step;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void DrawTransport() {
|
||
|
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
|
||
|
return;
|
||
|
}
|
||
|
repaint();
|
||
|
}
|
||
|
@Override
|
||
|
public void paintComponent(Graphics g) {
|
||
|
if (GetBoat() == null) {
|
||
|
return;
|
||
|
}
|
||
|
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
|
||
|
return;
|
||
|
}
|
||
|
super.paintComponent(g);
|
||
|
Graphics2D g2d = (Graphics2D) g;
|
||
|
|
||
|
g2d.setColor(Color.BLACK);
|
||
|
//границы
|
||
|
g2d.drawRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight);
|
||
|
|
||
|
int[] xpoints = {(int)(_startPosX + _BoatWidth * 3 / 4), (int)(_startPosX + _BoatWidth), (int)(_startPosX + _BoatWidth * 3 / 4) };
|
||
|
int[] ypoints = {(int)_startPosY, (int)(_startPosY + _BoatHeight / 2), (int)(_startPosY + _BoatHeight)};
|
||
|
|
||
|
g2d.drawPolygon(xpoints, ypoints, 3);
|
||
|
|
||
|
g2d.setPaint(Boat.GetBodyColor());
|
||
|
|
||
|
g2d.fillRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight);
|
||
|
g2d.fillPolygon(xpoints, ypoints, 3);
|
||
|
|
||
|
// середина
|
||
|
g2d.setColor(Color.BLACK);
|
||
|
g2d.drawOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11);
|
||
|
|
||
|
g2d.setPaint(Color.CYAN);
|
||
|
g2d.fillOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11);
|
||
|
|
||
|
OarsDraw.DrawOars(g, _startPosX, _startPosY);
|
||
|
}
|
||
|
|
||
|
public void ChangeBorders(int width, int height)
|
||
|
{
|
||
|
_pictureWidth = width;
|
||
|
_pictureHeight = height;
|
||
|
if (_pictureWidth <= _BoatWidth || _pictureHeight <= _BoatHeight)
|
||
|
{
|
||
|
_pictureWidth = null;
|
||
|
_pictureHeight = null;
|
||
|
return;
|
||
|
}
|
||
|
if (_startPosX + _BoatWidth > _pictureWidth)
|
||
|
{
|
||
|
_startPosX = _pictureWidth - _BoatWidth;
|
||
|
}
|
||
|
if (_startPosY + _BoatHeight > _pictureHeight)
|
||
|
{
|
||
|
_startPosY = _pictureHeight - _BoatHeight;
|
||
|
}
|
||
|
}
|
||
|
}
|