готовая лаба 2
This commit is contained in:
parent
35c8287d32
commit
b1d21a1556
57
src/DrawningExcavator.java
Normal file
57
src/DrawningExcavator.java
Normal file
@ -0,0 +1,57 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawningExcavator extends DrawningTracktor {
|
||||
public DrawningExcavator(int speed, float weight, Color bodyColor, int countRollers, Color dopColor, boolean bucket, boolean supports){
|
||||
super(speed, weight, bodyColor, countRollers, 130, 87);
|
||||
entityTracktor = new EntityExcavator(speed, weight, bodyColor, dopColor, bucket, supports);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawTransport(Graphics2D g){
|
||||
if (!(entityTracktor instanceof EntityExcavator excavator))
|
||||
{
|
||||
return;
|
||||
}
|
||||
Color pen;
|
||||
Color dopBrush = excavator.getDopColor();
|
||||
if (excavator.getBucket())
|
||||
{
|
||||
pen = excavator.getDopColor();
|
||||
g.setStroke(new BasicStroke(5));
|
||||
g.setColor(pen);
|
||||
g.drawLine((int)_startPosX + 1, (int)_startPosY + 90, (int)_startPosX + 15, (int)_startPosY + 70);
|
||||
g.drawLine((int)_startPosX + 15, (int)_startPosY + 72, (int)_startPosX + 15, (int)_startPosY + 50);
|
||||
g.drawLine((int)_startPosX + 15, (int)_startPosY + 52, (int)_startPosX + 10, (int)_startPosY + 45);
|
||||
g.drawLine((int)_startPosX + 15, (int)_startPosY + 60, (int)_startPosX + 40, (int)_startPosY + 50);
|
||||
g.setStroke(new BasicStroke(1));
|
||||
}
|
||||
_startPosX += 20;
|
||||
_startPosY += 5;
|
||||
super.DrawTransport(g);
|
||||
_startPosX -= 20;
|
||||
_startPosY -= 5;
|
||||
if (excavator.getSupports())
|
||||
{
|
||||
pen = Color.BLACK;
|
||||
g.setColor(dopBrush);
|
||||
g.fillRect((int)_startPosX + 100, (int)_startPosY + 50, 10, 42);
|
||||
g.setColor(pen);
|
||||
g.drawRect((int)_startPosX + 100, (int)_startPosY + 50, 10, 42);
|
||||
|
||||
g.setColor(dopBrush);
|
||||
g.fillRect((int)_startPosX + 90, (int)_startPosY + 82, 30, 10);
|
||||
g.setColor(pen);
|
||||
g.drawRect((int)_startPosX + 90, (int)_startPosY + 82, 30, 10);
|
||||
|
||||
g.setColor(dopBrush);
|
||||
g.fillRect((int)_startPosX + 45, (int)_startPosY + 50, 10, 42);
|
||||
g.setColor(pen);
|
||||
g.drawRect((int)_startPosX + 45, (int)_startPosY + 50, 10, 42);
|
||||
|
||||
g.setColor(dopBrush);
|
||||
g.fillRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10);
|
||||
g.setColor(pen);
|
||||
g.drawRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +1,10 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class DrawningRollers {
|
||||
public class DrawningRollers implements IDrawningRollers {
|
||||
private RollersCount rollersCount;
|
||||
private Color colorRollers;
|
||||
|
||||
void setRollersCount(int count){
|
||||
public void setRollersCount(int count){
|
||||
switch (count) {
|
||||
case 4 -> rollersCount = RollersCount.Four;
|
||||
case 5 -> rollersCount = RollersCount.Five;
|
||||
@ -13,13 +12,12 @@ public class DrawningRollers {
|
||||
default -> rollersCount = RollersCount.Four;
|
||||
}
|
||||
}
|
||||
|
||||
public void Init(int count, Color colorRollers){
|
||||
public DrawningRollers(int count, Color colorRollers){
|
||||
setRollersCount(count);
|
||||
this.colorRollers = colorRollers;
|
||||
}
|
||||
|
||||
public void DrawRollers(Graphics g, float _startPosX, float _startPosY){
|
||||
public void DrawRollers(Graphics2D g, float _startPosX, float _startPosY){
|
||||
Color penColor = Color.BLACK;
|
||||
Color mainColor = colorRollers==null ? Color.LIGHT_GRAY : colorRollers;
|
||||
|
||||
|
@ -1,24 +1,53 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
public class DrawningTracktor {
|
||||
private EntityTracktor Tracktor; // Класс-сущность
|
||||
private float _startPosX; // Левая координата отрисовки трактора
|
||||
private float _startPosY; // Верхняя кооридната отрисовки трактора
|
||||
private Integer _pictureWidth = null; // Ширина окна отрисовки
|
||||
private Integer _pictureHeight = null; // Высота окна отрисовки
|
||||
private final int _tracktorWidth = 110; // Ширина отрисовки трактора
|
||||
private final int _tracktorHeight = 87; // Высота отрисовки трактора
|
||||
|
||||
private DrawningRollers drawningRollers;
|
||||
protected EntityTracktor entityTracktor; // Класс-сущность
|
||||
public EntityTracktor getTracktor(){
|
||||
return entityTracktor;
|
||||
}
|
||||
protected int _startPosX; // Левая координата отрисовки трактора
|
||||
protected int _startPosY; // Верхняя кооридната отрисовки трактора
|
||||
private Integer _pictureWidth; // Ширина окна отрисовки
|
||||
private Integer _pictureHeight; // Высота окна отрисовки
|
||||
protected int _tracktorWidth = 110; // Ширина отрисовки трактора
|
||||
protected int _tracktorHeight = 87; // Высота отрисовки трактора
|
||||
public int getPosX(){
|
||||
return _startPosX;
|
||||
}
|
||||
public int getPosY(){
|
||||
return _startPosY;
|
||||
}
|
||||
public int getWidth(){
|
||||
return _tracktorWidth;
|
||||
}
|
||||
public int getHeight(){
|
||||
return _tracktorHeight;
|
||||
}
|
||||
private IDrawningRollers drawningRollers;
|
||||
|
||||
// Инициализация свойств
|
||||
public void Init(int speed, float weight, Color bodyColor, int countRollers)
|
||||
public DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers)
|
||||
{
|
||||
Tracktor = new EntityTracktor();
|
||||
Tracktor.Init(speed, weight, bodyColor);
|
||||
drawningRollers = new DrawningRollers();
|
||||
drawningRollers.Init(countRollers, bodyColor);
|
||||
entityTracktor = new EntityTracktor(speed, weight, bodyColor);
|
||||
drawningRollers = RollersType.random(countRollers, bodyColor);
|
||||
}
|
||||
protected DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers, int tracktorWidth, int tracktorHeight){
|
||||
this(speed, weight, bodyColor, countRollers);
|
||||
_tracktorWidth = tracktorWidth;
|
||||
_tracktorHeight = tracktorHeight;
|
||||
}
|
||||
public boolean CanMove(Direction direction){
|
||||
if(entityTracktor == null)
|
||||
return false;
|
||||
return switch (direction){
|
||||
case Left -> _startPosY - entityTracktor.getStep() > 0;
|
||||
case Up -> _startPosY - entityTracktor.getStep() > 0;
|
||||
case Right -> _startPosX + _tracktorHeight + entityTracktor.getStep() < _pictureWidth;
|
||||
case Down -> _startPosX + _tracktorWidth + entityTracktor.getStep() < _pictureHeight;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
// Установка позиции Трактора
|
||||
@ -48,37 +77,37 @@ public class DrawningTracktor {
|
||||
{
|
||||
// вправо
|
||||
case Right:
|
||||
if (_startPosX + _tracktorWidth + Tracktor.getStep() < _pictureWidth)
|
||||
if (_startPosX + _tracktorWidth + entityTracktor.getStep() < _pictureWidth)
|
||||
{
|
||||
_startPosX += Tracktor.getStep();
|
||||
_startPosX += entityTracktor.getStep();
|
||||
}
|
||||
break;
|
||||
//влево
|
||||
case Left:
|
||||
if (_startPosX - Tracktor.getStep() > 0)
|
||||
if (_startPosX - entityTracktor.getStep() > 0)
|
||||
{
|
||||
_startPosX -= Tracktor.getStep();
|
||||
_startPosX -= entityTracktor.getStep();
|
||||
}
|
||||
break;
|
||||
//вверх
|
||||
case Up:
|
||||
if (_startPosY - Tracktor.getStep() > 0)
|
||||
if (_startPosY - entityTracktor.getStep() > 0)
|
||||
{
|
||||
_startPosY -= Tracktor.getStep();
|
||||
_startPosY -= entityTracktor.getStep();
|
||||
}
|
||||
break;
|
||||
//вниз
|
||||
case Down:
|
||||
if (_startPosY + _tracktorHeight + Tracktor.getStep() < _pictureHeight)
|
||||
if (_startPosY + _tracktorHeight + entityTracktor.getStep() < _pictureHeight)
|
||||
{
|
||||
_startPosY += Tracktor.getStep();
|
||||
_startPosY += entityTracktor.getStep();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Отрисовка Трактора
|
||||
public void DrawTransport(Graphics g)
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
||||
{
|
||||
@ -87,7 +116,7 @@ public class DrawningTracktor {
|
||||
Color penColor = Color.BLACK;
|
||||
|
||||
// корпус
|
||||
Color br = Tracktor!=null ? Tracktor.getBodyColor() : Color.GRAY ;
|
||||
Color br = entityTracktor!=null ? entityTracktor.getBodyColor() : Color.GRAY ;
|
||||
g.setColor(br);
|
||||
g.fillRect((int)_startPosX + 10, (int)_startPosY + 30, 90, 25);
|
||||
g.setColor(penColor);
|
||||
@ -134,6 +163,7 @@ public class DrawningTracktor {
|
||||
drawningRollers.DrawRollers(g,_startPosX, _startPosY);
|
||||
}
|
||||
|
||||
|
||||
// Смена границ формы отрисовки
|
||||
public void ChangeBorders(int width, int height)
|
||||
{
|
||||
@ -154,4 +184,7 @@ public class DrawningTracktor {
|
||||
_startPosY = _pictureHeight - _tracktorHeight;
|
||||
}
|
||||
}
|
||||
public int[] getCurrentPosition(){
|
||||
return new int[]{_startPosX,_startPosX + _tracktorWidth - 1, _startPosY, _startPosY - _tracktorHeight -1};
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,4 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
// Класс-сущность "Трактор"
|
||||
public class EntityTracktor {
|
||||
private int Speed;
|
||||
@ -16,16 +14,15 @@ public class EntityTracktor {
|
||||
public Color getBodyColor(){
|
||||
return BodyColor;
|
||||
}
|
||||
public float getStep(){
|
||||
return Speed * 100 / Weight;
|
||||
public double getStep(){
|
||||
return (double) Speed * 100 / Weight;
|
||||
}
|
||||
|
||||
// Инициализация полей объекта-класса Трактора
|
||||
public void Init(int speed, float weight, Color bodyColor)
|
||||
public EntityTracktor(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
Speed = speed <= 0 ? rnd.nextInt(50, 150) : speed;
|
||||
Weight = weight <= 0 ? rnd.nextInt(40, 70) : weight;
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,17 @@
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="eb389" class="javax.swing.JButton" binding="buttonCreate">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Создать"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="7a016">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="1" column="2" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<grid id="2fbf8" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
@ -70,12 +78,12 @@
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="eb389" class="javax.swing.JButton" binding="buttonCreate">
|
||||
<component id="a11cc" class="javax.swing.JButton" binding="buttonCreateModif">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Создать"/>
|
||||
<text value="Модификация"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
|
@ -15,6 +15,7 @@ public class FormTracktor extends JFrame {
|
||||
private JButton buttonRight;
|
||||
private JButton buttonUp;
|
||||
private JPanel pictureBox;
|
||||
private JButton buttonCreateModif;
|
||||
|
||||
private DrawningTracktor _tracktor;
|
||||
|
||||
@ -26,21 +27,28 @@ public class FormTracktor extends JFrame {
|
||||
// Обработка нажатия кнопки "Создать"
|
||||
buttonCreate.addActionListener(e->{
|
||||
Random rnd = new Random();
|
||||
_tracktor = new DrawningTracktor();
|
||||
|
||||
_tracktor.Init(
|
||||
_tracktor = new DrawningTracktor(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
|
||||
rnd.nextInt(3,8)
|
||||
);
|
||||
setData();
|
||||
});
|
||||
|
||||
_tracktor.SetPosition(
|
||||
rnd.nextInt(10, 100),
|
||||
rnd.nextInt(10, 100),
|
||||
pictureBox.getWidth(), pictureBox.getHeight()
|
||||
// Обработка нажатия кнопки "Модификация"
|
||||
buttonCreateModif.addActionListener(e->{
|
||||
Random rnd = new Random();
|
||||
_tracktor = new DrawningExcavator(
|
||||
rnd.nextInt(100, 300),
|
||||
rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
|
||||
rnd.nextInt(3,8),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
|
||||
rnd.nextBoolean(),
|
||||
rnd.nextBoolean()
|
||||
);
|
||||
repaint();
|
||||
setData();
|
||||
});
|
||||
|
||||
buttonUp.addActionListener(e->{
|
||||
@ -83,12 +91,18 @@ public class FormTracktor extends JFrame {
|
||||
});
|
||||
}
|
||||
|
||||
private void setData() {
|
||||
Random rnd = new Random();
|
||||
_tracktor.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight());
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(Graphics g){
|
||||
super.paint(g);
|
||||
g = pictureBox.getGraphics();
|
||||
Graphics2D g2d = (Graphics2D)pictureBox.getGraphics();
|
||||
if (_tracktor != null){
|
||||
_tracktor.DrawTransport(g);
|
||||
_tracktor.DrawTransport(g2d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
6
src/IDrawningRollers.java
Normal file
6
src/IDrawningRollers.java
Normal file
@ -0,0 +1,6 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawningRollers {
|
||||
void setRollersCount(int count);
|
||||
void DrawRollers(Graphics2D g, float _startPosX, float _startPosY);
|
||||
}
|
Loading…
Reference in New Issue
Block a user