221 lines
9.2 KiB
Java
221 lines
9.2 KiB
Java
import Drawnings.CanvasDumpTruck;
|
||
import Drawnings.DirectionType;
|
||
import Drawnings.DrawningDumpTruck;
|
||
import Drawnings.DrawningTruck;
|
||
import MovementStrategy.*;
|
||
|
||
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();
|
||
|
||
private AbstractStrategy _strategy;
|
||
|
||
// Размер формы
|
||
private Dimension dimension;
|
||
// Название формы
|
||
private String title;
|
||
|
||
private JButton CreateDumpTruckButton = new JButton("Создать грузовик");
|
||
private JButton CreateTruckButton = new JButton("Создать самосвал");
|
||
private JButton UpButton = new JButton();
|
||
private JButton DownButton = new JButton();;
|
||
private JButton LeftButton = new JButton();;
|
||
private JButton RightButton = new JButton();
|
||
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
|
||
private JButton ButtonStrategy = new JButton("Шаг");
|
||
|
||
public FormDumpTruck(String title, Dimension dimension) {
|
||
this.title = title;
|
||
this.dimension = dimension;
|
||
}
|
||
|
||
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("Создать грузовик");
|
||
setMinimumSize(dimension);
|
||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
|
||
_canvasDumpTruck._drawningTruck = _truck;
|
||
|
||
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);
|
||
|
||
CreateTruckButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
CreateObject("DrawningTruck");
|
||
}
|
||
});
|
||
|
||
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;
|
||
}
|
||
}
|
||
});
|
||
|
||
ActionListener actionListener = new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent event) {
|
||
if (_canvasDumpTruck._drawningTruck == null) return;
|
||
boolean result = false;
|
||
switch ((((JButton) (event.getSource())).getName())) {
|
||
case "UP":
|
||
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Up);
|
||
break;
|
||
case "DOWN":
|
||
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Down);
|
||
break;
|
||
case "LEFT":
|
||
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Left);
|
||
break;
|
||
case "RIGHT":
|
||
result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Right);
|
||
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());
|
||
// кнопки создаиния самосвала и грузовика
|
||
CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40);
|
||
CreateTruckButton.setBounds(160, getHeight() - 90, 160, 40);
|
||
// кнопки направлений
|
||
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);
|
||
// кнокпи для стратегий
|
||
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
|
||
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
|
||
// добавление кнопок
|
||
add(CreateDumpTruckButton);
|
||
add(CreateTruckButton);
|
||
add(UpButton);
|
||
add(DownButton);
|
||
add(RightButton);
|
||
add(LeftButton);
|
||
add(_canvasDumpTruck);
|
||
add(ComboBoxStrategy);
|
||
add(ButtonStrategy);
|
||
pack();
|
||
setVisible(true);
|
||
|
||
addComponentListener(new ComponentAdapter() {
|
||
public void componentResized(ComponentEvent e) {
|
||
int Width = getWidth();
|
||
int Height = getHeight();
|
||
if (_canvasDumpTruck._drawningTruck != null)
|
||
_canvasDumpTruck._drawningTruck.SetPictureSize(Width, Height);
|
||
_canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight());
|
||
CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40);
|
||
CreateTruckButton.setBounds(180, getHeight() - 90, 160, 40);
|
||
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);
|
||
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35);
|
||
ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25);
|
||
}
|
||
});
|
||
}
|
||
}
|