PIbd-11_Karakozov_AK_Hard/src/FormExcavator.java

136 lines
5.2 KiB
Java
Raw Normal View History

import Drawings.*;
import MovementStrategy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormExcavator extends JFrame {
private String title;
private Dimension dimension;
private int Width, Height;
private CanvasExcavator canvasExcavator = new CanvasExcavator();
private JButton UpButton = new JButton();
private JButton DownButton = new JButton();
private JButton LeftButton = new JButton();
private JButton RightButton = new JButton();
private AbstractStrategy _strategy;
private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"});
private JButton ButtonStrategy = new JButton("Шаг");
public FormExcavator(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public void Init(DrawingBulldozer bulldozer) {
setTitle(title);
setMinimumSize(dimension);
Width = getWidth() - 15;
Height = getHeight() - 35;
ComboBoxStrategy.setEnabled(true);
_strategy = null;
canvasExcavator._drawingBulldozer = bulldozer;
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);
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;
}
}
});
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if (canvasExcavator._drawingBulldozer == null) return;
boolean result = false;
switch ((((JButton)(event.getSource())).getName())) {
case "Up":
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Up);
break;
case "Down":
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Down);
break;
case "Left":
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Left);
break;
case "Right":
result = canvasExcavator._drawingBulldozer.MoveTransport(DirectionType.Right);
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());
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);
ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 25);
ButtonStrategy.setBounds(getWidth() - 130, 45, 100, 25);
add(UpButton);
add(DownButton);
add(RightButton);
add(LeftButton);
add(ButtonStrategy);
add(ComboBoxStrategy);
add(canvasExcavator);
setVisible(true);
}
}