PIbd-14_Antonova_A.A.__Hard/FormDumpTruck.java

221 lines
9.2 KiB
Java
Raw Permalink Normal View History

2024-06-10 13:56:47 +04:00
import Drawnings.CanvasDumpTruck;
import Drawnings.DirectionType;
import Drawnings.DrawningDumpTruck;
import Drawnings.DrawningTruck;
import MovementStrategy.*;
2024-05-09 20:09:37 +04:00
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.*;
import java.util.Random;
public class FormDumpTruck extends JFrame{
private CanvasDumpTruck _canvasDumpTruck = new CanvasDumpTruck();
2024-06-10 13:56:47 +04:00
private AbstractStrategy _strategy;
2024-05-09 20:09:37 +04:00
// Размер формы
private Dimension dimension;
// Название формы
private String title;
2024-06-10 13:56:47 +04:00
private JButton CreateDumpTruckButton = new JButton("Создать грузовик");
private JButton CreateTruckButton = new JButton("Создать самосвал");
2024-05-09 20:09:37 +04:00
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();;
private JButton LeftButton = new JButton();;
private JButton RightButton = new JButton();
2024-06-10 13:56:47 +04:00
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
private JButton ButtonStrategy = new JButton("Шаг");
2024-05-09 20:09:37 +04:00
public FormDumpTruck(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
2024-06-10 13:56:47 +04:00
private void CreateObject(String type){
Random random = new Random();
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
switch(type){
case "DrawningTruck":
_canvasDumpTruck._drawningTruck = new Drawnings.DrawningTruck(random.nextInt(100, 300), random.nextInt(1000, 3000),
bodyColor);
_canvasDumpTruck._drawningTruck.SetPictureSize(getWidth(), getHeight());
_canvasDumpTruck._drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
_canvasDumpTruck.repaint();
break;
case "DrawningDumpTruck":
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
_canvasDumpTruck._drawningTruck = new DrawningDumpTruck();
_canvasDumpTruck._drawningTruck = new DrawningDumpTruck(random.nextInt(100, 300), random.nextInt(1000, 3000),
bodyColor, additionalColor,
random.nextBoolean(), random.nextBoolean());
_canvasDumpTruck._drawningTruck.SetPictureSize(getWidth(), getHeight());
_canvasDumpTruck._drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
_canvasDumpTruck.repaint();
break;
default:
return;
}
_strategy = null;
ComboBoxStrategy.setEnabled(true);
}
public void Form(DrawningTruck _truck) {
CreateDumpTruckButton.setName("Создать самосвал");
CreateTruckButton.setName("Создать грузовик");
2024-05-09 20:09:37 +04:00
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2024-06-10 13:56:47 +04:00
_canvasDumpTruck._drawningTruck = _truck;
2024-05-09 20:09:37 +04:00
UpButton.setName("UP");
Icon iconUp = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowUp.png");
UpButton.setIcon(iconUp);
DownButton.setName("DOWN");
Icon iconDown = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowDown.png");
DownButton.setIcon(iconDown);
LeftButton.setName("LEFT");
Icon iconLeft = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowLeft.png");
LeftButton.setIcon(iconLeft);
RightButton.setName("RIGHT");
Icon iconRight = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowRight.png");
RightButton.setIcon(iconRight);
2024-06-10 13:56:47 +04:00
CreateTruckButton.addActionListener(new ActionListener() {
2024-05-09 20:09:37 +04:00
@Override
public void actionPerformed(ActionEvent e) {
2024-06-10 13:56:47 +04:00
CreateObject("DrawningTruck");
}
});
2024-05-09 20:09:37 +04:00
2024-06-10 13:56:47 +04:00
CreateDumpTruckButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawningDumpTruck");
}
});
ButtonStrategy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_canvasDumpTruck._drawningTruck == 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 MoveableTruck(_canvasDumpTruck._drawningTruck), getWidth() - 23, getHeight()-23);
}
if (_strategy == null)
{
return;
}
ComboBoxStrategy.setEnabled(false);
_strategy.MakeStep();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
ComboBoxStrategy.setEnabled(true);
_strategy = null;
}
2024-05-09 20:09:37 +04:00
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
2024-06-10 13:56:47 +04:00
if (_canvasDumpTruck._drawningTruck == null) return;
2024-05-09 20:09:37 +04:00
boolean result = false;
switch ((((JButton) (event.getSource())).getName())) {
case "UP":
2024-06-10 13:56:47 +04:00
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Up);
2024-05-09 20:09:37 +04:00
break;
case "DOWN":
2024-06-10 13:56:47 +04:00
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Down);
2024-05-09 20:09:37 +04:00
break;
case "LEFT":
2024-06-10 13:56:47 +04:00
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Left);
2024-05-09 20:09:37 +04:00
break;
case "RIGHT":
2024-06-10 13:56:47 +04:00
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Right);
2024-05-09 20:09:37 +04:00
break;
}
if (result) {
_canvasDumpTruck.repaint();
}
}
};
UpButton.addActionListener(actionListener);
DownButton.addActionListener(actionListener);
LeftButton.addActionListener(actionListener);
RightButton.addActionListener(actionListener);
setSize(dimension.width, dimension.height);
setLayout(null);
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
2024-06-10 13:56:47 +04:00
// кнопки создаиния самосвала и грузовика
CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40);
CreateTruckButton.setBounds(160, getHeight() - 90, 160, 40);
// кнопки направлений
2024-05-09 20:09:37 +04:00
UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60);
DownButton.setBounds(getWidth() - 140, getHeight() - 100, 60, 60);
RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60);
LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60);
2024-06-10 13:56:47 +04:00
// кнокпи для стратегий
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
// добавление кнопок
add(CreateDumpTruckButton);
add(CreateTruckButton);
2024-05-09 20:09:37 +04:00
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(_canvasDumpTruck);
2024-06-10 13:56:47 +04:00
add(ComboBoxStrategy);
add(ButtonStrategy);
2024-05-09 20:09:37 +04:00
pack();
setVisible(true);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
int Width = getWidth();
int Height = getHeight();
2024-06-10 13:56:47 +04:00
if (_canvasDumpTruck._drawningTruck != null)
_canvasDumpTruck._drawningTruck.SetPictureSize(Width, Height);
2024-05-09 20:09:37 +04:00
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
2024-06-10 13:56:47 +04:00
CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40);
CreateTruckButton.setBounds(180, getHeight() - 90, 160, 40);
2024-05-09 20:09:37 +04:00
UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60);
DownButton.setBounds(getWidth() - 150, getHeight() - 100, 60, 60);
RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60);
LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60);
2024-06-10 13:56:47 +04:00
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
2024-05-09 20:09:37 +04:00
}
});
}
}