2023-11-26 19:10:56 +04:00

293 lines
10 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import Drawnings.*;
import MovementStrategy.*;
public class FormAirbus extends JFrame {
private int width;
private int height;
public DrawningAirbus _drawningAirbus;
private AbstractStrategy _abstractStrategy;
private Canvas canvas;
// выбор кол-ва иллюминаторов
JLabel labelCount;
private JTextField fieldCount;
// выбор стратегии
JLabel labelStrategy;
JComboBox comboBoxStrategy;
JButton buttonStrategy;
private JButton buttonCreateAirbus;
private JButton buttonCreatePlane;
public JButton buttonSelectAirbus;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonRight;
private JButton buttonLeft;
public FormAirbus() {
super("Создание самолёта");
InitializeComponent();
setVisible(true);
}
private void InitializeComponent()
{
canvas = new Canvas();
labelCount = new JLabel("Введите число иллюминаторов:");
fieldCount = new JTextField();
labelStrategy = new JLabel("Шаг стратегии:");
comboBoxStrategy = new JComboBox(new Integer[] {0, 1});
buttonStrategy = new JButton("Выбрать стратегию");
buttonStrategy.setMargin(new Insets(0, 0, 0, 0));
buttonCreateAirbus = new JButton("Создать аэробус");
buttonCreateAirbus.setMargin(new Insets(0, 0, 0, 0));
buttonCreatePlane = new JButton("Создать самолёт");
buttonCreatePlane.setMargin(new Insets(0, 0, 0, 0));
buttonSelectAirbus = new JButton("Выбрать");
buttonSelectAirbus.setMargin(new Insets(0, 0, 0, 0));
buttonUp = new JButton();
buttonUp.setName("up");
buttonUp.setIcon(new ImageIcon("images\\KeyUp.png"));
buttonRight = new JButton();
buttonRight.setName("right");
buttonRight.setIcon(new ImageIcon("images\\KeyRight.png"));
buttonLeft = new JButton();
buttonLeft.setName("left");
buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png"));
buttonDown = new JButton();
buttonDown.setName("down");
buttonDown.setIcon(new ImageIcon("images\\KeyDown.png"));
setSize(800,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
buttonCreateAirbus.setBounds(12, 355, 130, 25);
buttonCreatePlane.setBounds(buttonCreateAirbus.getX()+140, 355, 130, 25);
buttonSelectAirbus. setBounds(buttonCreatePlane.getX()+140, 355, 130, 25);
labelCount.setBounds(42, 405, 240, 20);
fieldCount.setBounds(240, 407, 48, 20);
labelStrategy.setBounds(630, 20, 146, 33);
comboBoxStrategy.setBounds(630, 50, 146, 20);
buttonStrategy.setBounds(630, 80, 146, 33);
buttonUp.setBounds(679, 313, 48, 44);
buttonRight.setBounds( 728, 358, 48, 44);
buttonLeft.setBounds(630, 358, 48, 44);
buttonDown.setBounds( 679, 358, 48, 44);
labelCount.setBounds(12, 405, 240, 20);
fieldCount.setBounds(210, 407, 48, 20);
canvas.setBounds(0,0,790, 460);
add(buttonCreateAirbus);
add(buttonCreatePlane);
add(buttonSelectAirbus);
add(labelCount);
add(fieldCount);
add(labelStrategy);
add(comboBoxStrategy);
add(buttonStrategy);
add(buttonUp);
add(buttonRight);
add(buttonDown);
add(buttonLeft);
add(labelCount);
add(fieldCount);
add(canvas);
// логика формы
buttonCreateAirbus.addActionListener(buttonCreateAirbusListener);
buttonCreatePlane.addActionListener(buttonCreatePlaneListener);
buttonStrategy.addActionListener(buttonStrategyListener);
buttonUp.addActionListener(buttonsMoveListener);
buttonRight.addActionListener(buttonsMoveListener);
buttonDown.addActionListener(buttonsMoveListener);
buttonLeft.addActionListener(buttonsMoveListener);
}
ActionListener buttonCreateAirbusListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
// базовый цвет
Color color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", Color.BLACK);
if (selectedColor != null)
{
color = selectedColor;
}
int countPortholes;
try
{
countPortholes = Integer.parseInt(fieldCount.getText());
}
catch (Exception ex)
{
countPortholes = 0;
}
if (countPortholes != 10 && countPortholes != 20 && countPortholes != 30)
{
JOptionPane.showMessageDialog(null, "Число должно быть равно 10, 20 или 30.\nКол-во иллюминаторов приравнено к 10");
countPortholes = 10;
}
_drawningAirbus = new DrawningAirbus(
rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
color,
countPortholes,
canvas.getWidth(), canvas.getHeight());
_drawningAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
comboBoxStrategy.setEnabled(true);
canvas.repaint();
}
};
ActionListener buttonCreatePlaneListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random rand = new Random();
// базовый цвет
Color color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
Color selectedColor = JColorChooser.showDialog(null, "Выберите цвет", Color.BLACK);
if (selectedColor != null)
{
color = selectedColor;
}
// доп цвет
Color additionalColor = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
selectedColor = JColorChooser.showDialog(null, "Выберите доп. цвет", Color.GRAY);
if (selectedColor != null)
{
additionalColor = selectedColor;
}
int countPortholes;
try
{
countPortholes = Integer.parseInt(fieldCount.getText());
}
catch (Exception ex)
{
countPortholes = 0;
}
if (countPortholes != 10 && countPortholes != 20 && countPortholes != 30)
{
JOptionPane.showMessageDialog(null, "Число должно быть равно 10, 20 или 30.\nКол-во иллюминаторов приравнено к 10");
countPortholes = 10;
}
_drawningAirbus = new DrawningPlane(
rand.nextInt(200) + 100, rand.nextInt(2000) + 1000,
color,
countPortholes,
additionalColor,
rand.nextBoolean(), rand.nextBoolean(),
canvas.getWidth(), canvas.getHeight());
_drawningAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10);
comboBoxStrategy.setEnabled(true);
canvas.repaint();
}
};
ActionListener buttonStrategyListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_drawningAirbus == null)
{
return;
}
if (comboBoxStrategy.isEnabled()) {
switch (comboBoxStrategy.getSelectedIndex()) {
case 0:
_abstractStrategy = new MoveToCenter();
break;
case 1:
_abstractStrategy = new MoveToBorder();
break;
default:
_abstractStrategy = null;
break;
}
;
if (_abstractStrategy == null) {
return;
}
_abstractStrategy.SetData(new DrawningObjectAirbus(_drawningAirbus), canvas.getWidth(), canvas.getHeight());
comboBoxStrategy.setEnabled(false);
}
if (_abstractStrategy == null)
{
return;
}
_abstractStrategy.MakeStep();
if (_abstractStrategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.setEnabled(true);
_abstractStrategy = null;
}
canvas.repaint();
}
};
ActionListener buttonsMoveListener = new ActionListener() {
// реакция на нажатие
public void actionPerformed(ActionEvent e) {
if (_drawningAirbus == null)
{
return;
}
String command = ((JButton)(e.getSource())).getName();
switch (command) {
case "up":
_drawningAirbus.MoveTransport(Direction.Up);
break;
case "down":
_drawningAirbus.MoveTransport(Direction.Down);
break;
case "right":
_drawningAirbus.MoveTransport(Direction.Right);
break;
case "left":
_drawningAirbus.MoveTransport(Direction.Left);
break;
}
canvas.repaint();
}
};
class Canvas extends JComponent{
public Canvas() {}
public void paintComponent (Graphics g){
if (_drawningAirbus == null){
return;
}
super.paintComponents (g) ;
Graphics2D g2d = (Graphics2D)g;
_drawningAirbus.DrawTransport(g2d);
super.repaint();
}
}
}