Drawing класс

This commit is contained in:
Данил Лопатин 2024-02-19 12:12:43 +03:00
parent fbec28cbb1
commit 490b9151ce
3 changed files with 143 additions and 2 deletions

View File

@ -1,3 +1,3 @@
public enum DirectionType {
Up, Down, Right, Left
};
}

View File

@ -0,0 +1,142 @@
import java.awt.*;
public class DrawingWarmlyLocomotive {
private EntityWarmlyLocomotive EntityWarmlyLocomotive;
public EntityWarmlyLocomotive getEntityWarmlyLocomotive(){ return EntityWarmlyLocomotive; }
private Integer _pictureWidth;
private Integer _pictureHeight;
private Integer _startPosX;
private Integer _startPosY;
private final int _drawningLocomotiveWidth = 150;
private final int _drawningLocomotiveHeight = 100;
public void Init(int speed, double weight, Color bodyColor,
Color additionalColor, boolean tube, boolean fuelTank)
{
EntityWarmlyLocomotive = new EntityWarmlyLocomotive();
EntityWarmlyLocomotive.Init(speed, weight, bodyColor, additionalColor,
tube, fuelTank);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public boolean SetPictureSize(int width, int height)
{
if (width >= _drawningLocomotiveWidth && height >= _drawningLocomotiveHeight) {
_pictureWidth = width;
_pictureHeight = height;
return true;
}
return false;
}
public void SetPosition(int x, int y){
if (_pictureHeight.equals(null) || _pictureWidth.equals(null)){
return;
}
if ((y + _drawningLocomotiveHeight) > _pictureHeight){
_startPosY = y - _drawningLocomotiveHeight;
}
else{
_startPosY = y;
}
if ((x + _drawningLocomotiveWidth) > _pictureWidth){
_startPosX = x - _drawningLocomotiveWidth;
}
else{
_startPosX = x;
}
}
public boolean MoveTransport(DirectionType direction){
if (EntityWarmlyLocomotive.equals(null) || _startPosX.equals(null) || _startPosY.equals(null)){
return false;
}
switch (direction)
{
case Up:
{
if (_startPosY - EntityWarmlyLocomotive.Step() > 0){
_startPosY -= (int)EntityWarmlyLocomotive.Step();
}
return true;
}
case Left:
{
if (_startPosX - EntityWarmlyLocomotive.Step() > 0){
_startPosX -= (int)EntityWarmlyLocomotive.Step();
}
return true;
}
case Right:
{
if (_startPosX + EntityWarmlyLocomotive.Step() + _drawningLocomotiveWidth < _pictureWidth){
_startPosX += (int)EntityWarmlyLocomotive.Step();
}
return true;
}
case Down:
{
if (_startPosY + EntityWarmlyLocomotive.Step() + _drawningLocomotiveHeight < _pictureHeight){
_startPosY += (int)EntityWarmlyLocomotive.Step();
}
}
default:
return false;
}
}
public void DrawTransport(Graphics g){
if (EntityWarmlyLocomotive.equals(null) || _startPosX.equals(null) || _startPosY.equals(null))
{
return;
}
g.setColor(Color.BLACK);
if (EntityWarmlyLocomotive.getTube())
{
g.drawRect(_startPosX + 40, _startPosY, 20, 30);
g.setColor(EntityWarmlyLocomotive.getAdditionColor());
g.fillRect(_startPosX + 40, _startPosY, 20, 40);
}
g.setColor(Color.BLACK);
g.drawRect(_startPosX, _startPosY + 60, 140, 20);
g.setColor(EntityWarmlyLocomotive.getBodyColor());
g.fillRect(_startPosX, _startPosY + 60, 140, 20);
g.setColor(Color.BLACK);
Polygon body = new Polygon();
body.addPoint(_startPosX, _startPosY + 60);
body.addPoint(_startPosX + 20, _startPosY + 30);
body.addPoint(_startPosX + 140, _startPosY + 30);
body.addPoint(_startPosX + 140, _startPosY + 60);
g.drawPolygon(body);
g.setColor(EntityWarmlyLocomotive.getBodyColor());
g.fillPolygon(body);
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 140, _startPosY + 40, 10, 40);
g.setColor(EntityWarmlyLocomotive.getBodyColor());
g.fillRect(_startPosX + 140, _startPosY + 40, 10, 40);
g.setColor(Color.BLACK);
g.drawOval(_startPosX, _startPosY + 80, 20, 20);
g.fillOval(_startPosX, _startPosY + 80, 20, 20);
g.drawOval(_startPosX + 30, _startPosY + 80, 20, 20);
g.fillOval(_startPosX + 30, _startPosY + 80, 20, 20);
g.drawOval(_startPosX + 90, _startPosY + 80, 20, 20);
g.fillOval(_startPosX + 90, _startPosY + 80, 20, 20);
g.drawOval(_startPosX + 120, _startPosY + 80, 20, 20);
g.fillOval(_startPosX + 120, _startPosY + 80, 20, 20);
if (EntityWarmlyLocomotive.getFuelTank()){
g.setColor(Color.BLACK);
g.drawRect(_startPosX + 80, _startPosY + 40, 50, 40);
g.setColor(EntityWarmlyLocomotive.getAdditionColor());
g.fillRect(_startPosX + 80, _startPosY + 40, 50, 40);
}
}
}

View File

@ -52,7 +52,6 @@ public class FormWarmlyLocomotive extends JFrame {
pictureBox.add(leftButton);
repaint();
add(pictureBox);
}
}