263 lines
9.5 KiB
Java
263 lines
9.5 KiB
Java
|
|
import javax.swing.*;
|
|
|
|
import SelfPropelledArtilleryUnit.DirectionType;
|
|
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU;
|
|
import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAUchild;
|
|
import SelfPropelledArtilleryUnit.MovementStrategy.AbstractStrategy;
|
|
import SelfPropelledArtilleryUnit.MovementStrategy.DrawningObjectSPAU;
|
|
import SelfPropelledArtilleryUnit.MovementStrategy.MoveToBorder;
|
|
import SelfPropelledArtilleryUnit.MovementStrategy.MoveToCenter;
|
|
import SelfPropelledArtilleryUnit.MovementStrategy.Status;
|
|
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import javax.swing.ImageIcon;
|
|
import java.awt.image.BufferedImage;
|
|
import java.util.Random;
|
|
|
|
public class Form extends JFrame {
|
|
|
|
private JPanel mainPanel;
|
|
private JButton buttonCreate;
|
|
private JButton buttonCreateParent;
|
|
private JButton buttonStep;
|
|
private JComboBox<String> comboBoxStrategy;
|
|
private JComboBox<String> comboBoxRoll;
|
|
private JButton buttonUp;
|
|
private JButton buttonDown;
|
|
private JButton buttonLeft;
|
|
private JButton buttonRight;
|
|
private JLabel pictureBoxSPAU;
|
|
private DrawningSPAU drawningSPAU;
|
|
private AbstractStrategy abstractStrategy;
|
|
private JTextField wheelsTextField;
|
|
private JLabel wheelsLabel;
|
|
|
|
public Form() {
|
|
initComponents();
|
|
}
|
|
|
|
private void Draw() {
|
|
if (drawningSPAU == null) {
|
|
return;
|
|
}
|
|
BufferedImage bmp = new BufferedImage(pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
|
Graphics2D gr = bmp.createGraphics();
|
|
drawningSPAU.DrawTransport(gr);
|
|
ImageIcon imageIcon = new ImageIcon(bmp);
|
|
pictureBoxSPAU.setIcon(imageIcon);
|
|
}
|
|
|
|
private void initComponents() {
|
|
mainPanel = new JPanel();
|
|
buttonCreate = new JButton("Создать c залповой установкой");
|
|
buttonCreateParent = new JButton("Создать базовый");
|
|
|
|
buttonUp = new JButton(new ImageIcon("resources/upper_arrow.png"));
|
|
buttonDown = new JButton(new ImageIcon("resources/down_arrow.png"));
|
|
buttonLeft = new JButton(new ImageIcon("resources/left_arrow.png"));
|
|
buttonRight = new JButton(new ImageIcon("resources/right_arrow.png"));
|
|
pictureBoxSPAU = new JLabel();
|
|
wheelsLabel = new JLabel("Количество колёс:");
|
|
wheelsTextField = new JTextField();
|
|
|
|
setLayout(new BorderLayout());
|
|
add(mainPanel, BorderLayout.CENTER);
|
|
mainPanel.setLayout(null);
|
|
|
|
pictureBoxSPAU.setBounds(0, 0, 882, 453);
|
|
mainPanel.add(pictureBoxSPAU);
|
|
|
|
wheelsLabel.setBounds(22, 410, 150, 29);
|
|
mainPanel.add(wheelsLabel);
|
|
|
|
wheelsTextField.setBounds(150, 410, 80, 29);
|
|
mainPanel.add(wheelsTextField);
|
|
|
|
// buttonCreate
|
|
buttonCreate.setBounds(250, 410, 250, 29);
|
|
mainPanel.add(buttonCreate);
|
|
buttonCreate.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonCreateActionPerformed(e);
|
|
}
|
|
});
|
|
|
|
// buttonCreateParent
|
|
buttonCreateParent.setBounds(500, 409, 150, 30);
|
|
mainPanel.add(buttonCreateParent);
|
|
buttonCreateParent.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonCreateParent_Click(e);
|
|
}
|
|
});
|
|
|
|
// buttonStep
|
|
buttonStep = new JButton("Шаг");
|
|
mainPanel.add(buttonStep);
|
|
buttonStep.setBounds(794, 55, 76, 30);
|
|
buttonStep.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonStep_Click(e);
|
|
}
|
|
});
|
|
|
|
// comboBoxRoll
|
|
|
|
comboBoxRoll = new JComboBox<>();
|
|
mainPanel.add(comboBoxRoll);
|
|
comboBoxRoll.setModel(new DefaultComboBoxModel<>(new String[] { "1", "2", "3" }));
|
|
comboBoxRoll.setBounds(767, 232, 103, 28);
|
|
|
|
// comboBoxStrategy
|
|
comboBoxStrategy = new JComboBox<>();
|
|
mainPanel.add(comboBoxStrategy);
|
|
comboBoxStrategy.setModel(new DefaultComboBoxModel<>(new String[] { "0", "1" }));
|
|
comboBoxStrategy.setBounds(767, 12, 103, 28);
|
|
|
|
buttonUp.setBounds(804, 336, 30, 30);
|
|
mainPanel.add(buttonUp);
|
|
ImageIcon iconUp = new ImageIcon("Arrows/upper-arrow.png");
|
|
buttonUp.setIcon(iconUp);
|
|
buttonUp.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonMoveActionPerformed(buttonUp, e);
|
|
}
|
|
});
|
|
|
|
buttonDown.setBounds(804, 409, 30, 30);
|
|
mainPanel.add(buttonDown);
|
|
ImageIcon iconDown = new ImageIcon("Arrows/down-arrow.png");
|
|
buttonDown.setIcon(iconDown);
|
|
buttonDown.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonMoveActionPerformed(buttonDown, e);
|
|
}
|
|
});
|
|
|
|
buttonLeft.setBounds(768, 372, 30, 30);
|
|
mainPanel.add(buttonLeft);
|
|
ImageIcon iconLeft = new ImageIcon("Arrows/left-arrow.png");
|
|
buttonLeft.setIcon(iconLeft);
|
|
buttonLeft.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonMoveActionPerformed(buttonLeft, e);
|
|
}
|
|
});
|
|
|
|
buttonRight.setBounds(840, 372, 30, 30);
|
|
mainPanel.add(buttonRight);
|
|
ImageIcon iconRight = new ImageIcon("Arrows/right-arrow.png");
|
|
buttonRight.setIcon(iconRight);
|
|
buttonRight.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonMoveActionPerformed(buttonRight, e);
|
|
}
|
|
});
|
|
buttonUp.setName("buttonUp");
|
|
buttonDown.setName("buttonDown");
|
|
buttonLeft.setName("buttonLeft");
|
|
buttonRight.setName("buttonRight");
|
|
setPreferredSize(new Dimension(882, 453));
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
pack();
|
|
}
|
|
|
|
protected void buttonCreateActionPerformed(ActionEvent e) {
|
|
int wheelCount;
|
|
try {
|
|
wheelCount = Integer.parseInt(wheelsTextField.getText());
|
|
} catch (NumberFormatException ex) {
|
|
wheelCount = 0;
|
|
}
|
|
|
|
if (wheelCount > 0) {
|
|
Random random = new Random();
|
|
drawningSPAU = new DrawningSPAUchild(wheelCount, Integer.parseInt((String) comboBoxRoll.getSelectedItem()), random.nextInt(200) + 100,
|
|
(double) random.nextInt(2000) + 1000,
|
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
|
true, true,
|
|
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
|
|
drawningSPAU.SetPosition(random.nextInt(90) + 20, random.nextInt(90) + 20);
|
|
Draw();
|
|
}
|
|
}
|
|
|
|
protected void buttonCreateParent_Click(ActionEvent e) {
|
|
int wheelCount;
|
|
try {
|
|
wheelCount = Integer.parseInt(wheelsTextField.getText());
|
|
} catch (NumberFormatException ex) {
|
|
wheelCount = 0;
|
|
}
|
|
|
|
if (wheelCount > 0) {
|
|
Random random = new Random();
|
|
drawningSPAU = new DrawningSPAU(wheelCount, Integer.parseInt((String) comboBoxRoll.getSelectedItem()), random.nextInt(200) + 100,
|
|
(double) random.nextInt(2000) + 1000,
|
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
|
pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
|
|
drawningSPAU.SetPosition(random.nextInt(90) + 20, random.nextInt(90) + 20);
|
|
Draw();
|
|
}
|
|
}
|
|
|
|
protected void buttonStep_Click (ActionEvent e) {
|
|
if (drawningSPAU == null) {
|
|
return;
|
|
}
|
|
if (comboBoxStrategy.isEnabled()) {
|
|
int selectedIndex = comboBoxStrategy.getSelectedIndex();
|
|
switch (selectedIndex) {
|
|
case 0:
|
|
abstractStrategy = new MoveToCenter();
|
|
break;
|
|
case 1:
|
|
abstractStrategy = new MoveToBorder();
|
|
break;
|
|
default:
|
|
abstractStrategy = null;
|
|
break;
|
|
}
|
|
if (abstractStrategy == null) {
|
|
return;
|
|
}
|
|
abstractStrategy.setData(new DrawningObjectSPAU(drawningSPAU), pictureBoxSPAU.getWidth(), pictureBoxSPAU.getHeight());
|
|
comboBoxStrategy.setEnabled(false);
|
|
}
|
|
if (abstractStrategy == null) {
|
|
return;
|
|
}
|
|
abstractStrategy.makeStep();
|
|
Draw();
|
|
if (abstractStrategy.getStatus() == Status.Finish) {
|
|
comboBoxStrategy.setEnabled(true);
|
|
abstractStrategy = null;
|
|
}
|
|
}
|
|
|
|
protected void buttonMoveActionPerformed(Object sender, ActionEvent e) {
|
|
if (drawningSPAU == null) {
|
|
return;
|
|
}
|
|
String name = (sender instanceof JButton) ? ((JButton) sender).getName() : "";
|
|
switch (name) {
|
|
case "buttonUp":
|
|
drawningSPAU.MoveTransport(DirectionType.Up);
|
|
break;
|
|
case "buttonDown":
|
|
drawningSPAU.MoveTransport(DirectionType.Down);
|
|
break;
|
|
case "buttonLeft":
|
|
drawningSPAU.MoveTransport(DirectionType.Left);
|
|
break;
|
|
case "buttonRight":
|
|
drawningSPAU.MoveTransport(DirectionType.Right);
|
|
break;
|
|
}
|
|
Draw();
|
|
}
|
|
} |