2024-05-11 17:47:53 +04:00
|
|
|
|
import Drawings.CanvasExcavator;
|
|
|
|
|
import Drawings.DirectionType;
|
|
|
|
|
import Drawings.DrawingBulldozer;
|
|
|
|
|
import Drawings.DrawingExcavator;
|
|
|
|
|
import MovementStrategy.*;
|
|
|
|
|
|
2024-05-11 17:38:33 +04:00
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.awt.event.ComponentAdapter;
|
|
|
|
|
import java.awt.event.ComponentEvent;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
public class FormExcavator extends JFrame {
|
|
|
|
|
private String title;
|
|
|
|
|
private Dimension dimension;
|
|
|
|
|
private int Width, Height;
|
|
|
|
|
private CanvasExcavator canvasExcavator = new CanvasExcavator();
|
2024-05-11 17:47:53 +04:00
|
|
|
|
private JButton CreateButton = new JButton("Создать экскаватор");
|
|
|
|
|
private JButton CreateBullButton = new JButton("Создать бульдозер");
|
2024-05-11 17:38:33 +04:00
|
|
|
|
private JButton UpButton = new JButton();
|
|
|
|
|
private JButton DownButton = new JButton();
|
|
|
|
|
private JButton LeftButton = new JButton();
|
|
|
|
|
private JButton RightButton = new JButton();
|
2024-05-11 17:47:53 +04:00
|
|
|
|
private AbstractStrategy _strategy;
|
|
|
|
|
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
|
|
|
|
|
private JButton ButtonStrategy = new JButton("Шаг");
|
2024-05-11 17:38:33 +04:00
|
|
|
|
public FormExcavator(String title, Dimension dimension) {
|
|
|
|
|
this.title = title;
|
|
|
|
|
this.dimension = dimension;
|
|
|
|
|
}
|
2024-05-11 17:47:53 +04:00
|
|
|
|
|
|
|
|
|
private void CreateObject(String typeOfClass) {
|
|
|
|
|
int StartPositionX = (int)(Math.random() * 90 + 10);
|
|
|
|
|
int StartPositionY = (int)(Math.random() * 90 + 10);
|
|
|
|
|
int speed = (int)(Math.random() * 300 + 100);
|
|
|
|
|
double weight = (double)(Math.random() * 3000 + 1000);
|
|
|
|
|
Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
|
|
|
|
switch (typeOfClass) {
|
|
|
|
|
case "DrawingBulldozer":
|
|
|
|
|
canvasExcavator._drawingBulldozer = new DrawingBulldozer(speed, weight, bodyColor);
|
|
|
|
|
canvasExcavator._drawingBulldozer.SetPictureSize(Width, Height);
|
|
|
|
|
canvasExcavator._drawingBulldozer.SetPosition(StartPositionX, StartPositionY);
|
|
|
|
|
canvasExcavator.repaint();
|
|
|
|
|
break;
|
|
|
|
|
case "DrawingExcavator":
|
|
|
|
|
Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));
|
|
|
|
|
boolean prop = new Random().nextBoolean();
|
|
|
|
|
boolean ladle = new Random().nextBoolean();
|
|
|
|
|
canvasExcavator._drawingBulldozer = new DrawingExcavator(speed, weight, bodyColor, additionalColor, prop, ladle);
|
|
|
|
|
canvasExcavator._drawingBulldozer.SetPictureSize(Width, Height);
|
|
|
|
|
canvasExcavator._drawingBulldozer.SetPosition(StartPositionX, StartPositionY);
|
|
|
|
|
canvasExcavator.repaint();
|
|
|
|
|
break;
|
|
|
|
|
default: return;
|
|
|
|
|
}
|
|
|
|
|
_strategy = null;
|
|
|
|
|
ComboBoxStrategy.setEnabled(true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-11 17:38:33 +04:00
|
|
|
|
public void Init() {
|
|
|
|
|
setTitle(title);
|
|
|
|
|
setMinimumSize(dimension);
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
Width = getWidth() - 15;
|
|
|
|
|
Height = getHeight() - 35;
|
2024-05-11 17:47:53 +04:00
|
|
|
|
_strategy = null;
|
2024-05-11 17:38:33 +04:00
|
|
|
|
|
|
|
|
|
CreateButton.setName("Create");
|
|
|
|
|
Icon iconUp = new ImageIcon("res/up.png");
|
|
|
|
|
UpButton.setIcon(iconUp);
|
|
|
|
|
UpButton.setName("Up");
|
|
|
|
|
DownButton.setName("Down");
|
|
|
|
|
Icon iconDown = new ImageIcon("res/down.png");
|
|
|
|
|
DownButton.setIcon(iconDown);
|
|
|
|
|
LeftButton.setName("Left");
|
|
|
|
|
Icon iconLeft = new ImageIcon("res/left.png");
|
|
|
|
|
LeftButton.setIcon(iconLeft);
|
|
|
|
|
RightButton.setName("Right");
|
|
|
|
|
Icon iconRight = new ImageIcon("res/right.png");
|
|
|
|
|
RightButton.setIcon(iconRight);
|
|
|
|
|
|
|
|
|
|
CreateButton.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2024-05-11 17:47:53 +04:00
|
|
|
|
CreateObject("DrawingExcavator");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
CreateBullButton.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
CreateObject("DrawingBulldozer");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ButtonStrategy.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
if (canvasExcavator._drawingBulldozer == null) return;
|
|
|
|
|
if (ComboBoxStrategy.isEnabled())
|
|
|
|
|
{
|
|
|
|
|
int index = ComboBoxStrategy.getSelectedIndex();
|
|
|
|
|
switch(index)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
_strategy = new MoveToCenter();
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
_strategy = new MoveToBorder();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
_strategy = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (_strategy == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_strategy.SetData(new MoveableExcavator(canvasExcavator._drawingBulldozer), Width, Height);
|
|
|
|
|
}
|
|
|
|
|
if (_strategy == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ComboBoxStrategy.setEnabled(false);
|
|
|
|
|
_strategy.MakeStep();
|
|
|
|
|
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
|
|
|
|
{
|
|
|
|
|
ComboBoxStrategy.setEnabled(true);
|
|
|
|
|
_strategy = null;
|
|
|
|
|
}
|
2024-05-11 17:38:33 +04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ActionListener actionListener = new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent event) {
|
2024-05-11 17:47:53 +04:00
|
|
|
|
if (canvasExcavator._drawingBulldozer == null) return;
|
2024-05-11 17:38:33 +04:00
|
|
|
|
boolean result = false;
|
|
|
|
|
switch ((((JButton)(event.getSource())).getName())) {
|
|
|
|
|
case "Up":
|
2024-05-11 17:47:53 +04:00
|
|
|
|
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Up);
|
2024-05-11 17:38:33 +04:00
|
|
|
|
break;
|
|
|
|
|
case "Down":
|
2024-05-11 17:47:53 +04:00
|
|
|
|
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Down);
|
2024-05-11 17:38:33 +04:00
|
|
|
|
break;
|
|
|
|
|
case "Left":
|
2024-05-11 17:47:53 +04:00
|
|
|
|
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Left);
|
2024-05-11 17:38:33 +04:00
|
|
|
|
break;
|
|
|
|
|
case "Right":
|
2024-05-11 17:47:53 +04:00
|
|
|
|
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Right);
|
2024-05-11 17:38:33 +04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (result) {
|
|
|
|
|
canvasExcavator.repaint();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
UpButton.addActionListener(actionListener);
|
|
|
|
|
DownButton.addActionListener(actionListener);
|
|
|
|
|
LeftButton.addActionListener(actionListener);
|
|
|
|
|
RightButton.addActionListener(actionListener);
|
|
|
|
|
|
|
|
|
|
setSize(dimension.width,dimension.height);
|
|
|
|
|
setLayout(null);
|
|
|
|
|
canvasExcavator.setBounds(0,0, getWidth(), getHeight());
|
2024-05-11 17:47:53 +04:00
|
|
|
|
CreateButton.setBounds(10, getHeight() - 85, 160, 35);
|
|
|
|
|
CreateBullButton.setBounds(180, getHeight() - 85, 160, 35);
|
2024-05-11 17:38:33 +04:00
|
|
|
|
UpButton.setBounds(getWidth() - 110, getHeight() - 135, 35, 35);
|
|
|
|
|
DownButton.setBounds(getWidth() - 110, getHeight() - 85, 35, 35);
|
|
|
|
|
RightButton.setBounds(getWidth() - 60, getHeight() - 85, 35, 35);
|
|
|
|
|
LeftButton.setBounds(getWidth() - 160, getHeight() - 85, 35, 35);
|
2024-05-11 17:47:53 +04:00
|
|
|
|
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 25);
|
|
|
|
|
ButtonStrategy.setBounds(getWidth() - 130, 45, 100, 25);
|
2024-05-11 17:38:33 +04:00
|
|
|
|
add(CreateButton);
|
2024-05-11 17:47:53 +04:00
|
|
|
|
add(CreateBullButton);
|
2024-05-11 17:38:33 +04:00
|
|
|
|
add(UpButton);
|
|
|
|
|
add(DownButton);
|
|
|
|
|
add(RightButton);
|
|
|
|
|
add(LeftButton);
|
2024-05-11 17:47:53 +04:00
|
|
|
|
add(ButtonStrategy);
|
|
|
|
|
add(ComboBoxStrategy);
|
2024-05-11 17:38:33 +04:00
|
|
|
|
add(canvasExcavator);
|
|
|
|
|
setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
}
|