лаба 3

This commit is contained in:
Алексей Крюков 2023-12-18 20:31:50 +04:00
parent aad0dc3598
commit 7b0ffff8ac
17 changed files with 655 additions and 85 deletions

View File

@ -1,22 +1,34 @@
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);
public DrawningExcavator(int speed, double weight, Color bodyColor, boolean bucket, boolean supports, Color bucketColor , Color supportsColor, int width, int height, int rollersCount)
{
super(speed, weight, bodyColor, width, height, 110, 87, rollersCount);
if (getEntityTracktor() != null)
{
setEntityTracktor(new EntityExcavator(speed, weight, bodyColor,bucketColor,supportsColor, bucket, supports));
}
}
public DrawningExcavator(EntityExcavator entityExcavator, int width, int height, int countRollers, IDrawningRollers iDrawningRollers){
super(entityExcavator,width,height,countRollers,iDrawningRollers);
if(getEntityTracktor() != null){
setEntityTracktor(entityExcavator);
}
}
@Override
public void DrawTransport(Graphics2D g){
if (!(entityTracktor instanceof EntityExcavator excavator))
if (!(getEntityTracktor() instanceof EntityExcavator excavator))
{
return;
}
Color pen;
Color dopBrush = excavator.getDopColor();
Color supportsBrush = excavator.getSupportsColor();
Color bucketBrush = excavator.getBucketColor();
if (excavator.getBucket())
{
pen = excavator.getDopColor();
pen = excavator.getBucketColor();
g.setStroke(new BasicStroke(5));
g.setColor(pen);
g.drawLine((int)_startPosX + 1, (int)_startPosY + 90, (int)_startPosX + 15, (int)_startPosY + 70);
@ -33,22 +45,22 @@ public class DrawningExcavator extends DrawningTracktor {
if (excavator.getSupports())
{
pen = Color.BLACK;
g.setColor(dopBrush);
g.setColor(supportsBrush);
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.setColor(supportsBrush);
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.setColor(supportsBrush);
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.setColor(supportsBrush);
g.fillRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10);
g.setColor(pen);
g.drawRect((int)_startPosX + 35, (int)_startPosY + 82, 30, 10);

View File

@ -9,7 +9,7 @@ public class DrawningObjectExcavator implements IMoveableObject{
@Override
public ObjectParameters getObjectPosition(){
if(_tracktor == null || _tracktor.getTracktor() == null){
if(_tracktor == null || _tracktor.getEntityTracktor() == null){
return null;
}
return new ObjectParameters(_tracktor.getPosX(), _tracktor.getPosY(),_tracktor.getWidth(), _tracktor.getHeight());
@ -19,7 +19,7 @@ public class DrawningObjectExcavator implements IMoveableObject{
if (_tracktor == null) {
return 0;
}
return (int)((_tracktor.getTracktor() != null)? _tracktor.getTracktor().getStep() : 0);
return (int)((_tracktor.getEntityTracktor() != null)? _tracktor.getEntityTracktor().getStep() : 0);
}
@Override
public boolean checkCanMove(Direction direction){

View File

@ -4,6 +4,11 @@ import java.util.Random;
public class DrawningRollers implements IDrawningRollers {
private RollersCount rollersCount;
private Color colorRollers;
public RollersCount getRollersCount(){
return rollersCount;
}
@Override
public void setRollersCount(int count){
switch (count) {
case 4 -> rollersCount = RollersCount.Four;

View File

@ -3,10 +3,16 @@ import java.util.Random;
// Класс, отвечающий за прорисовку и перемещение объекта-сущности
public class DrawningTracktor {
protected EntityTracktor entityTracktor; // Класс-сущность
public EntityTracktor getTracktor(){
private EntityTracktor entityTracktor; // Класс-сущность
public EntityTracktor getEntityTracktor(){
return entityTracktor;
}
protected void setEntityTracktor(EntityTracktor entityTracktor){
this.entityTracktor = entityTracktor;
}
public IMoveableObject GetMoveableObject(){
return new DrawningObjectExcavator(this);
}
protected int _startPosX; // Левая координата отрисовки трактора
protected int _startPosY; // Верхняя кооридната отрисовки трактора
private Integer _pictureWidth; // Ширина окна отрисовки
@ -28,15 +34,51 @@ public class DrawningTracktor {
private IDrawningRollers drawningRollers;
// Инициализация свойств
public DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers)
public DrawningTracktor(int speed, double weight, Color bodyColor,int width,int height, int countRollers)
{
entityTracktor = new EntityTracktor(speed, weight, bodyColor);
drawningRollers = RollersType.random(countRollers, bodyColor);
if(height < _tracktorHeight || width < _tracktorWidth ){
return;
}
_pictureWidth = width;
_pictureHeight = height;
entityTracktor = new EntityTracktor(speed, weight,bodyColor);
Random random = new Random();
drawningRollers = switch (random.nextInt(0, 3)) {
case 0 -> new DrawningRollers(random.nextInt(0,3), bodyColor);
case 1 -> new DrawningSquaredRollers(random.nextInt(0,3), bodyColor);
case 2 -> new DrawningCrossRollers(random.nextInt(0,3), bodyColor);
default -> new DrawningRollers(random.nextInt(0,3), bodyColor);
};
drawningRollers.setRollersCount((countRollers));
}
protected DrawningTracktor(int speed, float weight, Color bodyColor, int countRollers, int tracktorWidth, int tracktorHeight){
this(speed, weight, bodyColor, countRollers);
protected DrawningTracktor(int speed, double weight, Color bodyColor, int width, int height, int tracktorWidth,int tracktorHeight, int countRollers){
if(width < tracktorWidth || height < tracktorHeight)
return;
_pictureWidth = width;
_pictureHeight = height;
_tracktorWidth = tracktorWidth;
_tracktorHeight = tracktorHeight;
_pictureHeight = tracktorHeight;
entityTracktor = new EntityTracktor(speed,weight,bodyColor);
Random random = new Random();
drawningRollers = switch (random.nextInt(0, 3)) {
case 0 -> new DrawningRollers(random.nextInt(0,3), bodyColor);
case 1 -> new DrawningSquaredRollers(random.nextInt(0,3), bodyColor);
case 2 -> new DrawningCrossRollers(random.nextInt(0,3), bodyColor);
default -> new DrawningRollers(random.nextInt(0,3), bodyColor);
};
drawningRollers.setRollersCount(countRollers);
}
public DrawningTracktor (EntityTracktor entityTracktor, int width, int height , int rollersCount, IDrawningRollers iDrawningRollers){
if(height < _tracktorHeight || width < _tracktorWidth){
return;
}
_pictureWidth = width;
_pictureHeight = height;
this.entityTracktor = entityTracktor;
Random random = new Random();
drawningRollers = iDrawningRollers;
drawningRollers.setRollersCount(rollersCount);
}
public boolean CanMove(Direction direction){
if(entityTracktor == null)
@ -51,25 +93,23 @@ public class DrawningTracktor {
}
// Установка позиции Трактора
public void SetPosition(int x, int y, int width, int height)
public void SetPosition(int x, int y)
{
if (x + _tracktorWidth > width ||
x < 0 ||
y + _tracktorHeight > height ||
y < 0)
if (x + _tracktorWidth > _pictureWidth){
x = 0;
}
if( x < 0 || y + _tracktorHeight > _pictureHeight || y < 0)
{
return;
y = 0;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
// Изменение направления перемещения
public void MoveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null)
if (!CanMove(direction) || entityTracktor == null)
{
return;
}
@ -79,28 +119,28 @@ public class DrawningTracktor {
case Right:
if (_startPosX + _tracktorWidth + entityTracktor.getStep() < _pictureWidth)
{
_startPosX += entityTracktor.getStep();
_startPosX += (int)entityTracktor.getStep();
}
break;
//влево
case Left:
if (_startPosX - entityTracktor.getStep() > 0)
{
_startPosX -= entityTracktor.getStep();
_startPosX -= (int)entityTracktor.getStep();
}
break;
//вверх
case Up:
if (_startPosY - entityTracktor.getStep() > 0)
{
_startPosY -= entityTracktor.getStep();
_startPosY -= (int)entityTracktor.getStep();
}
break;
//вниз
case Down:
if (_startPosY + _tracktorHeight + entityTracktor.getStep() < _pictureHeight)
{
_startPosY += entityTracktor.getStep();
_startPosY += (int)entityTracktor.getStep();
}
break;
}
@ -162,29 +202,4 @@ public class DrawningTracktor {
drawningRollers.DrawRollers(g,_startPosX, _startPosY);
}
// Смена границ формы отрисовки
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _tracktorWidth || _pictureHeight <= _tracktorHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _tracktorWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _tracktorWidth;
}
if (_startPosY + _tracktorHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _tracktorHeight;
}
}
public int[] getCurrentPosition(){
return new int[]{_startPosX,_startPosX + _tracktorWidth - 1, _startPosY, _startPosY - _tracktorHeight -1};
}
}

View File

@ -1,21 +1,26 @@
import java.awt.*;
public class EntityExcavator extends EntityTracktor{
// Дополнительный цвет
private Color dopColor;
private Color BucketColor;
private Color SupportsColor;
// Признак наличия ковша
private boolean bucket;
// Признак наличия опор
private boolean supports;
public EntityExcavator(int speed, float weight, Color bodyColor, Color dopColor, boolean bucket, boolean supports){
public EntityExcavator(int speed, double weight, Color bodyColor, Color bucketColor, Color supportsColor, boolean bucket, boolean supports){
super(speed, weight, bodyColor);
this.dopColor = dopColor;
this.BucketColor = bucketColor;
this.SupportsColor = supportsColor;
this.bucket = bucket;
this.supports = supports;
}
public Color getDopColor(){
return dopColor;
public Color getBucketColor(){
return BucketColor;
}
public Color getSupportsColor(){
return SupportsColor;
}
public boolean getBucket(){

View File

@ -2,13 +2,13 @@ import java.awt.*;
// Класс-сущность "Трактор"
public class EntityTracktor {
private int Speed;
private float Weight;
private double Weight;
private Color BodyColor;
public int getSpeed() {
return Speed;
}
public float getWeight(){
public double getWeight(){
return Weight;
}
public Color getBodyColor(){
@ -19,7 +19,7 @@ public class EntityTracktor {
}
// Инициализация полей объекта-класса Трактора
public EntityTracktor(int speed, float weight, Color bodyColor)
public EntityTracktor(int speed, double weight, Color bodyColor)
{
Speed = speed;
Weight = weight;

View File

@ -11,7 +11,7 @@ public class FormTracktor extends JFrame {
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonUp;
private JPanel pictureBox;
public JPanel pictureBox;
private JButton buttonCreateModif;
private JComboBox comboBoxStrategy;
private JButton buttonMakeStep;
@ -31,6 +31,8 @@ public class FormTracktor extends JFrame {
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
pictureBox.getWidth(),
pictureBox.getHeight(),
rnd.nextInt(3,8)
);
setData();
@ -43,10 +45,13 @@ public class FormTracktor extends JFrame {
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()
rnd.nextBoolean(),
new Color(rnd.nextInt(0,256),rnd.nextInt(0,256),rnd.nextInt(0,256)),
new Color(rnd.nextInt(0,256),rnd.nextInt(0,256),rnd.nextInt(0,256)),
pictureBox.getWidth(),
pictureBox.getHeight(),
rnd.nextInt(3,8)
);
setData();
});
@ -80,17 +85,6 @@ public class FormTracktor extends JFrame {
});
pictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
if (_tracktor != null){
_tracktor.ChangeBorders(e.getComponent().getWidth(), e.getComponent().getHeight());
repaint();
}
}
});
buttonMakeStep.addActionListener(e -> {
if (_tracktor == null ) {
return;
@ -121,7 +115,7 @@ 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());
_tracktor.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
repaint();
}

15
src/FrameDop.java Normal file
View File

@ -0,0 +1,15 @@
import javax.swing.*;
public class FrameDop extends JFrame {
private PictureBoxDop pictureBoxDop;
public FrameDop() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxDop = new PictureBoxDop();
add(pictureBoxDop);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

15
src/FrameExcavator.java Normal file
View File

@ -0,0 +1,15 @@
import javax.swing.*;
public class FrameExcavator extends JFrame {
public PictureBoxExcavator pictureBoxExcavator;
public FrameExcavator() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxExcavator = new PictureBoxExcavator();
add(pictureBoxExcavator);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

View File

@ -0,0 +1,12 @@
import javax.swing.*;
public class FrameTruckCollection extends JFrame {
private PictureBoxCollection pictureBoxCollection;
public FrameTruckCollection(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pictureBoxCollection = new PictureBoxCollection();
add(pictureBoxCollection);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}

55
src/GenericDop.java Normal file
View File

@ -0,0 +1,55 @@
import java.util.Random;
public class GenericDop<T extends EntityTracktor, U extends IDrawningRollers> {
private T[] Tracktors;
private U[] Rollers;
private int maxTracktorAmount;
private int tracktorAmount;
private int maxRollersAmount ;
private int rollersAmount;
private Random random;
private int _pictureWidth;
private int _pictureHeight;
public GenericDop(int maxTracktorAmount, int maxRollersAmount, int pictureWidth, int pictureHeight){
this.maxTracktorAmount = maxTracktorAmount;
this.maxRollersAmount = maxRollersAmount;
Tracktors = (T[]) new EntityTracktor[maxTracktorAmount];
Rollers = (U[]) new IDrawningRollers[maxRollersAmount];
tracktorAmount = 0;
rollersAmount = 0;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
random = new Random();
}
public boolean add(T tracktor){
if (tracktor == null || tracktorAmount > maxTracktorAmount)
return false;
Tracktors[tracktorAmount] = tracktor;
tracktorAmount++;
return true;
}
public boolean add(U roller){
if (roller == null || rollersAmount > maxRollersAmount)
return false;
Rollers[rollersAmount] = roller;
rollersAmount++;
return true;
}
public DrawningTracktor DrawingTracktorDop(){
if (tracktorAmount == 0 || rollersAmount == 0)
return null;
T tracktor = Tracktors[random.nextInt(tracktorAmount)];
DrawningTracktor drawingTracktor;
if (tracktor instanceof EntityExcavator){
drawingTracktor = new DrawningExcavator((EntityExcavator) tracktor, _pictureWidth, _pictureHeight, random.nextInt(2, 5), Rollers[random.nextInt(rollersAmount)]);
}
else{
drawingTracktor = new DrawningTracktor(tracktor, _pictureWidth, _pictureHeight, random.nextInt(2, 5), Rollers[random.nextInt(rollersAmount)]);
}
return drawingTracktor;
}
}

View File

@ -0,0 +1,90 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
public class PictureBoxCollection extends JPanel {
public TracktorsGenericCollection<DrawningTracktor,DrawningObjectExcavator> _traktors;
private JLabel labelTools;
private JButton buttonAddTracktor, buttonDeleteTracktor, buttonRefreshCollection, buttonShowDop;
private JTextField textFieldNumber;
public PictureBoxCollection() {
setLayout(null);
setBounds(0, 0, 800, 450);
_traktors = new TracktorsGenericCollection<>(this.getWidth() - 200, this.getHeight());
labelTools = new JLabel("Инструменты");
labelTools.setBounds(660, 10, 150, 30);
add(labelTools);
buttonAddTracktor = new JButton("Добавить экскаватор");
buttonAddTracktor.setFocusable(false);
buttonAddTracktor.setBounds(620, 50, 150, 30);
buttonAddTracktor.addActionListener(e -> {
FrameExcavator frameExcavator = new FrameExcavator();
frameExcavator.pictureBoxExcavator.buttonSelectTracktor.addActionListener(e1 -> {
if (_traktors.Add(frameExcavator.pictureBoxExcavator.drawingTracktor) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
frameExcavator.dispose();
repaint();
}
);
});
add(buttonAddTracktor);
textFieldNumber = new JTextField();
textFieldNumber.setBounds(620, 100, 150, 30);
add(textFieldNumber);
buttonDeleteTracktor = new JButton("Удалить");
buttonDeleteTracktor.setFocusable(false);
buttonDeleteTracktor.setBounds(620, 150, 150, 30);
buttonDeleteTracktor.addActionListener(e -> {
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
return;
}
for (char it : textFieldNumber.getText().toCharArray())
if (it < '0' || it > '9') {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
if (textFieldNumber.getText().length() == 0) {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
int pos = Integer.parseInt(textFieldNumber.getText());
if (_traktors.remove(pos)) {
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
repaint();
});
add(buttonDeleteTracktor);
buttonRefreshCollection = new JButton("Обновить коллекцию");
buttonRefreshCollection.setFocusable(false);
buttonRefreshCollection.setBounds(620, 200, 150, 30);
buttonRefreshCollection.addActionListener(e -> repaint());
add(buttonRefreshCollection);
buttonShowDop = new JButton("Показать доп");
buttonShowDop.setFocusable(false);
buttonShowDop.setBounds(620, 250, 150, 30);
buttonShowDop.addActionListener(e -> new FrameDop());
add(buttonShowDop);
setPreferredSize(new Dimension(800, 450));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(_traktors.ShowTracktors(), 0, 0, null);
}
}

44
src/PictureBoxDop.java Normal file
View File

@ -0,0 +1,44 @@
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class PictureBoxDop extends JPanel {
GenericDop<EntityTracktor, IDrawningRollers> genericDop;
private JButton buttonCreate;
public DrawningTracktor drawingTracktor;
public PictureBoxDop(){
setLayout(null);
Random random = new Random();
setBounds(0, 0, 800, 450);
genericDop = new GenericDop<>(100, 100, this.getWidth(), this.getHeight());
genericDop.add(new EntityTracktor(100, 100, Color.BLUE));
genericDop.add(new EntityTracktor(100, 100, Color.RED));
genericDop.add(new EntityTracktor(100, 100, Color.GREEN));
genericDop.add(new EntityExcavator(100, 100, Color.BLUE, Color.BLACK, Color.RED,true, true));
genericDop.add(new EntityExcavator(100, 100, Color.GREEN, Color.MAGENTA, Color.YELLOW,true, true));
genericDop.add(new DrawningRollers(random.nextInt(4,6),Color.blue));
genericDop.add(new DrawningCrossRollers(random.nextInt(4,6),Color.blue));
genericDop.add(new DrawningSquaredRollers(random.nextInt(4,6),Color.blue));
buttonCreate = new JButton("Создать");
buttonCreate.setFocusable(false);
buttonCreate.setBounds(12, 415, 150, 30);
buttonCreate.addActionListener(e -> {
drawingTracktor = genericDop.DrawingTracktorDop();
repaint();
});
add(buttonCreate);
setPreferredSize(new Dimension(800, 450));
}
@Override
protected void paintComponent(Graphics g) {
if (drawingTracktor == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawingTracktor.SetPosition(100, 100);
drawingTracktor.DrawTransport(g2d);
}
}

View File

@ -0,0 +1,209 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
public class PictureBoxExcavator extends JPanel {
public DrawningTracktor drawingTracktor;
private AbstractStrategy abstractStrategy;
private JButton buttonLeft;
private JButton buttonUp;
private JButton buttonRight;
private JButton buttonDown;
private JButton buttonCreateExcavator;
private JButton buttonCreateTracktor;
private JComboBox comboBoxStrategy;
private JButton buttonStep;
public JButton buttonSelectTracktor;
public PictureBoxExcavator() {
setLayout(null);
setBounds(0, 0, 800, 450);
buttonCreateTracktor = new JButton("Создать Трактор");
buttonCreateTracktor.setFocusable(false);
buttonCreateTracktor.setBounds(12, 415, 150, 30);
add(buttonCreateTracktor);
buttonCreateTracktor.addActionListener(e -> {
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
bodyColor = selectedColor;
}
drawingTracktor = new DrawningTracktor((random.nextInt(200, 300)),
random.nextInt(1000, 3000),
bodyColor,
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
drawingTracktor.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
});
buttonCreateExcavator = new JButton("Создать экскаватор");
buttonCreateExcavator.setFocusable(false);
buttonCreateExcavator.setBounds(180, 415, 150, 30);
add(buttonCreateExcavator);
buttonCreateExcavator.addActionListener(e -> {
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
bodyColor = selectedColor;
}
Color bucketColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
bucketColor = selectedColor;
}
Color suppotsColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE);
if (selectedColor != null)
{
suppotsColor = selectedColor;
}
drawingTracktor = new DrawningExcavator((random.nextInt(200, 300)),
random.nextInt(1000, 3000),
bodyColor,
random.nextBoolean(), true,
bucketColor,suppotsColor,
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
drawingTracktor.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
repaint();
});
buttonSelectTracktor = new JButton("Выбрать");
buttonSelectTracktor.setFocusable(false);
buttonSelectTracktor.setBounds(350, 415, 150, 30);
add(buttonSelectTracktor);
ActionListener buttonMoveListener = e -> {
if (drawingTracktor == null)
{
return;
}
String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) {
case ("buttonUp"):
drawingTracktor.MoveTransport(Direction.Up);
break;
case ("buttonDown"):
drawingTracktor.MoveTransport(Direction.Down);
break;
case ("buttonLeft"):
drawingTracktor.MoveTransport(Direction.Left);
break;
case ("buttonRight"):
drawingTracktor.MoveTransport(Direction.Right);
break;
}
repaint();
};
buttonLeft = new JButton();
buttonLeft.setName("buttonLeft");
buttonLeft.setFocusable(false);
buttonLeft.setPreferredSize(new Dimension(30, 30));
buttonLeft.setIcon(new ImageIcon("Resources/left.png"));
buttonLeft.addActionListener(buttonMoveListener);
buttonLeft.setBounds(686, 408, 30, 30);
add(buttonLeft);
buttonRight = new JButton();
buttonRight.setName("buttonRight");
buttonRight.setFocusable(false);
buttonRight.setPreferredSize(new Dimension(30, 30));
buttonRight.setIcon(new ImageIcon("Resources/right.png"));
buttonRight.addActionListener(buttonMoveListener);
buttonRight.setBounds(758, 408, 30, 30);
add(buttonRight);
buttonDown = new JButton();
buttonDown.setName("buttonDown");
buttonDown.setFocusable(false);
buttonDown.setPreferredSize(new Dimension(30, 30));
buttonDown.setIcon(new ImageIcon("Resources/down'.png"));
buttonDown.addActionListener(buttonMoveListener);
buttonDown.setBounds(722, 408, 30, 30);
add(buttonDown);
buttonUp = new JButton();
buttonUp.setName("buttonUp");
buttonUp.setFocusable(false);
buttonUp.setPreferredSize(new Dimension(30, 30));
buttonUp.setIcon(new ImageIcon("Resources/up.png"));
buttonUp.addActionListener(buttonMoveListener);
buttonUp.setBounds(722, 372, 30, 30);
add(buttonUp);
String[] items = {
"0",
"1"
};
comboBoxStrategy = new JComboBox(items);
comboBoxStrategy.setBounds(667, 10, 120, 25);
buttonStep = new JButton("Шаг");
buttonStep.setFocusable(false);
buttonStep.setBounds(710, 40, 75, 30);
buttonStep.addActionListener(e -> {
if (drawingTracktor == null) {
return;
}
if (comboBoxStrategy.isEnabled()) {
abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()) {
case 0 -> new MoveToCenter();
case 1 -> new MoveToBorder();
default -> null;
};
if (abstractStrategy == null)
{
return;
}
abstractStrategy.setData(new DrawningObjectExcavator(drawingTracktor), this.getWidth(), this.getHeight());
}
if (abstractStrategy == null)
{
return;
}
comboBoxStrategy.setEnabled(false);
abstractStrategy.makeStep();
repaint();
if (abstractStrategy.getStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
abstractStrategy = null;
}
});
add(comboBoxStrategy);
add(buttonStep);
setPreferredSize(new Dimension(800, 450));
}
@Override
protected void paintComponent(Graphics g) {
if (drawingTracktor == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawingTracktor.DrawTransport(g2d);
}
}

View File

@ -1,8 +1,6 @@
import javax.swing.*;
public class Program {
public static void main(String[] args){
FormTracktor formTracktor = new FormTracktor();
formTracktor.setVisible(true);
formTracktor.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
new FrameTruckCollection();
}
}

45
src/SetGeneric.java Normal file
View File

@ -0,0 +1,45 @@
public class SetGeneric<T extends Object> {
private Object[] _places;
public int Count(){return _places.length;}
public SetGeneric(int count){
_places = new Object[count];
}
public int Insert(T tracktor){
return Insert(tracktor,0);
}
public int Insert(T tracktor, int position){
if(position < 0 || position >= Count())
return -1;
if (_places[position] == null){
_places[position] = tracktor;
return position;
}
int index = -1;
for(int i = position; i < Count();i++){
if(_places[i] == null){
index = i;
break;
}
}
if(index < 0)
return -1;
for(int i = index; i > position; i--){
_places[i] = _places[i - 1];
}
_places[position] = tracktor;
return position;
}
public boolean Remove(int position){
if(position < 0 || position >= Count()){
return false;
}
_places[position] = null;
return true;
}
public T Get(int position){
if(position < 0 || position >= Count()){
return null;
}
return (T) _places[position];
}
}

View File

@ -0,0 +1,56 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class TracktorsGenericCollection<T extends DrawningTracktor, U extends IMoveableObject>{
private int _pictureWidth;
private int _pictureHeight;
private final int _placeSizeWidth = 160;
private final int _placeSizeHeight = 140;
private SetGeneric<T> _collection;
public TracktorsGenericCollection(int pictureWidth, int pictureHeight){
int width = pictureWidth / _placeSizeWidth;
int height = pictureHeight / _placeSizeHeight;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_collection = new SetGeneric<T>(width*height);
}
public int Add(T obj){
if(obj == null)
return -1;
return _collection.Insert(obj);
}
public boolean remove(int pos){
T obj = _collection.Get(pos);
if(obj != null)
_collection.Remove(pos);
return false;
}
public U GetU(int pos){
return (U) _collection.Get(pos).GetMoveableObject();
}
public BufferedImage ShowTracktors(){
BufferedImage bitmap = new BufferedImage(_pictureWidth,_pictureHeight,BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = bitmap.createGraphics();
DrawBackground(g);
DrawObjects(g);
g.dispose();
return bitmap;
}
private void DrawBackground(Graphics g){
g.setColor(Color.BLACK);
for(int i = 0 ; i< _pictureWidth / _placeSizeWidth; i++){
for(int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j){
g.drawLine(i * _placeSizeWidth,j * _placeSizeHeight,i * _placeSizeWidth + _placeSizeWidth/2,j*_placeSizeHeight);
}
g.drawLine(i * _placeSizeWidth, 0,i * _placeSizeWidth,_pictureHeight/_placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics g){
for(int i = 0; i < _collection.Count();i++){
T obj = _collection.Get(i);
if(obj != null){
obj.SetPosition(i %(_pictureWidth/ _placeSizeWidth)*_placeSizeWidth,i/(_pictureWidth/_placeSizeWidth) * _placeSizeHeight);
obj.DrawTransport((Graphics2D) g);
}
}
}
}