260 lines
9.4 KiB
Java
260 lines
9.4 KiB
Java
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.image.BufferedImage;
|
|
import javax.swing.*;
|
|
|
|
import Directions.Direction;
|
|
import DrawningObjects.DrawningMonorail;
|
|
import DrawningObjects.DrawningSecondMonorail;
|
|
import MovementStrategy.*;
|
|
|
|
import java.util.Random;
|
|
|
|
class Form extends JFrame{
|
|
private DrawningMonorail _drawningMonorail;
|
|
private AbstractStrategy abstractStrategy;
|
|
private JLabel pictureBoxMonorail;
|
|
private JLabel wheelsLabel;
|
|
private JPanel mainPanel;
|
|
private JButton btUp;
|
|
private JButton btDown;
|
|
private JButton btRight;
|
|
private JButton btLeft;
|
|
private JButton btCreateMonorail;
|
|
private JButton btCreateSecondMonorail;
|
|
private JButton btStep;
|
|
private JComboBox<String> comboBoxWheel;
|
|
private JComboBox<String> comboBoxStrategy;
|
|
private JTextField wheelsTextField;
|
|
|
|
Random rd = new Random();
|
|
|
|
public Form(){
|
|
initComponents();
|
|
}
|
|
private void Draw(){
|
|
if(_drawningMonorail == null){
|
|
return;
|
|
}
|
|
BufferedImage bmp = new BufferedImage(pictureBoxMonorail.getWidth(),
|
|
pictureBoxMonorail.getHeight(),BufferedImage.TYPE_INT_ARGB);
|
|
Graphics2D gr = bmp.createGraphics();
|
|
_drawningMonorail.DrawTransport(gr);
|
|
ImageIcon imageIcon = new ImageIcon(bmp);
|
|
pictureBoxMonorail.setIcon(imageIcon);
|
|
}
|
|
private void initComponents(){
|
|
mainPanel = new JPanel();
|
|
btCreateMonorail = new JButton("Создать Монорельс");
|
|
btCreateSecondMonorail = new JButton("Создать два монорельса");
|
|
|
|
btUp = new JButton(new ImageIcon("resources/upper-arrow.png"));
|
|
btDown = new JButton(new ImageIcon("resources/down-arrow.png"));
|
|
btLeft = new JButton(new ImageIcon("resources/left-arrow.png"));
|
|
btRight = new JButton(new ImageIcon("resources/right-arrow.png"));
|
|
|
|
pictureBoxMonorail = new JLabel();
|
|
wheelsLabel = new JLabel("Кол-во колёс: ");
|
|
wheelsTextField = new JTextField();
|
|
|
|
setLayout(new BorderLayout());
|
|
add(mainPanel, BorderLayout.CENTER);
|
|
mainPanel.setLayout(null);
|
|
|
|
pictureBoxMonorail.setBounds(0, 0, 882, 453);
|
|
mainPanel.add(pictureBoxMonorail);
|
|
|
|
wheelsLabel.setBounds(22, 410, 150, 29);
|
|
mainPanel.add(wheelsLabel);
|
|
|
|
wheelsTextField.setBounds(150, 410, 80, 29);
|
|
mainPanel.add(wheelsTextField);
|
|
|
|
btCreateMonorail.setBounds(250, 410, 100, 29);
|
|
mainPanel.add(btCreateMonorail);
|
|
btCreateMonorail.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonCreateActionPerformed(e);
|
|
}
|
|
});
|
|
|
|
btCreateSecondMonorail.setBounds(500, 409, 150, 30);
|
|
mainPanel.add(btCreateSecondMonorail);
|
|
btCreateSecondMonorail.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
btCreateSecondMonorail_Click(e);
|
|
}
|
|
});
|
|
|
|
btStep = new JButton("Шаг");
|
|
mainPanel.add(btStep);
|
|
btStep.setBounds(794, 55, 76, 30);
|
|
btStep.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
btStep_Click(e);
|
|
}
|
|
});
|
|
|
|
comboBoxWheel = new JComboBox<>();
|
|
mainPanel.add(comboBoxWheel);
|
|
comboBoxWheel.setModel(new DefaultComboBoxModel<>(new String[] { "1", "2", "3" }));
|
|
comboBoxWheel.setBounds(767, 232, 103, 28);
|
|
|
|
comboBoxStrategy = new JComboBox<>();
|
|
mainPanel.add(comboBoxStrategy);
|
|
comboBoxStrategy.setModel(new DefaultComboBoxModel<>(new String[] { "0", "1" }));
|
|
comboBoxStrategy.setBounds(767, 12, 103, 28);
|
|
|
|
btUp.setBounds(790, 355, 40, 40);
|
|
mainPanel.add(btUp);
|
|
ImageIcon iconUp = new ImageIcon("resources/upper-arrow.png");
|
|
btUp.setIcon(iconUp);
|
|
btUp.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonMoveActionPerformed(btUp, e);
|
|
}
|
|
});
|
|
|
|
btDown.setBounds(790, 401, 40, 40);
|
|
mainPanel.add(btDown);
|
|
ImageIcon iconDown = new ImageIcon("resources/down-arrow.png");
|
|
btDown.setIcon(iconDown);
|
|
btDown.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonMoveActionPerformed(btDown, e);
|
|
}
|
|
});
|
|
|
|
btLeft.setBounds(744, 401, 40, 40);
|
|
mainPanel.add(btLeft);
|
|
ImageIcon iconLeft = new ImageIcon("resources/left-arrow.png");
|
|
btLeft.setIcon(iconLeft);
|
|
btLeft.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonMoveActionPerformed(btLeft, e);
|
|
}
|
|
});
|
|
|
|
btRight.setBounds(836, 401, 40, 40);
|
|
mainPanel.add(btRight);
|
|
ImageIcon iconRight = new ImageIcon("resources/right-arrow.png");
|
|
btRight.setIcon(iconRight);
|
|
btRight.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
buttonMoveActionPerformed(btRight, e);
|
|
}
|
|
});
|
|
btUp.setName("buttonUp");
|
|
btDown.setName("buttonDown");
|
|
btLeft.setName("buttonLeft");
|
|
btRight.setName("buttonRight");
|
|
|
|
setPreferredSize(new Dimension(900, 500));
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
setVisible(true);
|
|
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();
|
|
_drawningMonorail = new DrawningMonorail(wheelCount, Integer.parseInt((String) comboBoxWheel.getSelectedItem()),
|
|
random.nextInt(200) + 100, (double)random.nextInt(2000) + 1000,
|
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
|
pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight());
|
|
_drawningMonorail.setPosition(random.nextInt(90) + 10,
|
|
random.nextInt(90) + 10);
|
|
Draw();
|
|
}
|
|
}
|
|
protected void btCreateSecondMonorail_Click(ActionEvent e) {
|
|
int wheelCount;
|
|
try {
|
|
wheelCount = Integer.parseInt(wheelsTextField.getText());
|
|
} catch (NumberFormatException ex) {
|
|
wheelCount = 0;
|
|
}
|
|
|
|
if (wheelCount > 0) {
|
|
Random random = new Random();
|
|
_drawningMonorail = new DrawningSecondMonorail(wheelCount, Integer.parseInt((String) comboBoxWheel.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,
|
|
pictureBoxMonorail.getWidth(), pictureBoxMonorail.getHeight());
|
|
_drawningMonorail.setPosition(random.nextInt(90) + 20, random.nextInt(90) + 20);
|
|
Draw();
|
|
}
|
|
}
|
|
|
|
protected void btStep_Click (ActionEvent e) {
|
|
if (_drawningMonorail == 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 DrawingObjectMonorail(_drawningMonorail), pictureBoxMonorail.getWidth(), pictureBoxMonorail.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 (_drawningMonorail == null) {
|
|
return;
|
|
}
|
|
String name = (sender instanceof JButton) ? ((JButton) sender).getName() : "";
|
|
switch (name) {
|
|
case "buttonUp":
|
|
_drawningMonorail.moveTransport(Direction.UP);
|
|
break;
|
|
case "buttonDown":
|
|
_drawningMonorail.moveTransport(Direction.DOWN);
|
|
break;
|
|
case "buttonLeft":
|
|
_drawningMonorail.moveTransport(Direction.LEFT);
|
|
break;
|
|
case "buttonRight":
|
|
_drawningMonorail.moveTransport(Direction.RIGHT);
|
|
break;
|
|
}
|
|
Draw();
|
|
}
|
|
}
|
|
class MonoFrame{
|
|
public static void main(String[] args) {
|
|
Form fr = new Form();
|
|
}
|
|
} |